streamMeta.c 12.3 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "executor.h"
17
#include "streamBackendRocksdb.h"
18
#include "streamInc.h"
dengyihao's avatar
dengyihao 已提交
19
#include "tref.h"
L
Liu Jicong 已提交
20
#include "ttimer.h"
L
Liu Jicong 已提交
21

dengyihao's avatar
dengyihao 已提交
22
static TdThreadOnce streamMetaModuleInit = PTHREAD_ONCE_INIT;
dengyihao's avatar
dengyihao 已提交
23
int32_t             streamBackendId = 0;
Y
yihaoDeng 已提交
24
int32_t             streamBackendCfWrapperId = 0;
Y
yihaoDeng 已提交
25 26

static void streamMetaEnvInit() {
Y
yihaoDeng 已提交
27
  streamBackendId = taosOpenRef(64, streamBackendCleanup);
Y
yihaoDeng 已提交
28
  streamBackendCfWrapperId = taosOpenRef(64, streamBackendHandleCleanup);
Y
yihaoDeng 已提交
29
}
dengyihao's avatar
dengyihao 已提交
30 31

void streamMetaInit() { taosThreadOnce(&streamMetaModuleInit, streamMetaEnvInit); }
Y
yihaoDeng 已提交
32 33
void streamMetaCleanup() {
  taosCloseRef(streamBackendId);
Y
yihaoDeng 已提交
34
  taosCloseRef(streamBackendCfWrapperId);
Y
yihaoDeng 已提交
35
}
dengyihao's avatar
dengyihao 已提交
36

37
SStreamMeta* streamMetaOpen(const char* path, void* ahandle, FTaskExpand expandFunc, int32_t vgId) {
dengyihao's avatar
dengyihao 已提交
38
  int32_t      code = -1;
L
Liu Jicong 已提交
39 40 41 42 43
  SStreamMeta* pMeta = taosMemoryCalloc(1, sizeof(SStreamMeta));
  if (pMeta == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
44

L
Liu Jicong 已提交
45 46
  int32_t len = strlen(path) + 20;
  char*   streamPath = taosMemoryCalloc(1, len);
47
  sprintf(streamPath, "%s/%s", path, "stream");
48
  pMeta->path = taosStrdup(streamPath);
49
  if (tdbOpen(pMeta->path, 16 * 1024, 1, &pMeta->db, 0) < 0) {
L
Liu Jicong 已提交
50 51
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
52
  memset(streamPath, 0, len);
L
Liu Jicong 已提交
53

L
Liu Jicong 已提交
54
  sprintf(streamPath, "%s/%s", pMeta->path, "checkpoints");
dengyihao's avatar
dengyihao 已提交
55
  code = taosMulModeMkDir(streamPath, 0755);
dengyihao's avatar
dengyihao 已提交
56 57 58 59
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }
60

61
  if (tdbTbOpen("task.db", sizeof(int32_t), -1, NULL, pMeta->db, &pMeta->pTaskDb, 0) < 0) {
L
Liu Jicong 已提交
62 63 64
    goto _err;
  }

65
  if (tdbTbOpen("checkpoint.db", sizeof(int32_t), -1, NULL, pMeta->db, &pMeta->pCheckpointDb, 0) < 0) {
L
Liu Jicong 已提交
66 67 68
    goto _err;
  }

69
  _hash_fn_t fp = taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT);
H
Haojun Liao 已提交
70
  pMeta->pTasks = taosHashInit(64, fp, true, HASH_NO_LOCK);
L
Liu Jicong 已提交
71 72 73 74
  if (pMeta->pTasks == NULL) {
    goto _err;
  }

75 76 77 78 79 80 81
  // task list
  pMeta->pTaskList = taosArrayInit(4, sizeof(int32_t));
  if (pMeta->pTaskList == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    goto _err;
  }

L
Liu Jicong 已提交
82 83 84 85
  if (streamMetaBegin(pMeta) < 0) {
    goto _err;
  }

H
Haojun Liao 已提交
86
  pMeta->walScanCounter = 0;
87
  pMeta->vgId = vgId;
L
Liu Jicong 已提交
88 89
  pMeta->ahandle = ahandle;
  pMeta->expandFunc = expandFunc;
dengyihao's avatar
dengyihao 已提交
90

dengyihao's avatar
dengyihao 已提交
91 92 93
  memset(streamPath, 0, len);
  sprintf(streamPath, "%s/%s", pMeta->path, "state");
  code = taosMulModeMkDir(streamPath, 0755);
dengyihao's avatar
dengyihao 已提交
94 95 96 97 98
  if (code != 0) {
    terrno = TAOS_SYSTEM_ERROR(code);
    goto _err;
  }

dengyihao's avatar
dengyihao 已提交
99
  pMeta->streamBackend = streamBackendInit(streamPath);
dengyihao's avatar
dengyihao 已提交
100 101 102
  if (pMeta->streamBackend == NULL) {
    goto _err;
  }
dengyihao's avatar
dengyihao 已提交
103
  pMeta->streamBackendRid = taosAddRef(streamBackendId, pMeta->streamBackend);
Y
yihaoDeng 已提交
104 105
  pMeta->pTaskBackendUnique =
      taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
dengyihao's avatar
dengyihao 已提交
106

dengyihao's avatar
dengyihao 已提交
107
  taosMemoryFree(streamPath);
dengyihao's avatar
dengyihao 已提交
108

109
  taosInitRWLatch(&pMeta->lock);
Y
yihaoDeng 已提交
110 111
  taosThreadMutexInit(&pMeta->backendMutex, NULL);

112
  return pMeta;
L
Liu Jicong 已提交
113

L
Liu Jicong 已提交
114
_err:
dengyihao's avatar
dengyihao 已提交
115
  taosMemoryFree(streamPath);
L
Liu Jicong 已提交
116
  taosMemoryFree(pMeta->path);
L
Liu Jicong 已提交
117
  if (pMeta->pTasks) taosHashCleanup(pMeta->pTasks);
118
  if (pMeta->pTaskList) taosArrayDestroy(pMeta->pTaskList);
L
Liu Jicong 已提交
119
  if (pMeta->pTaskDb) tdbTbClose(pMeta->pTaskDb);
120
  if (pMeta->pCheckpointDb) tdbTbClose(pMeta->pCheckpointDb);
L
Liu Jicong 已提交
121
  if (pMeta->db) tdbClose(pMeta->db);
dengyihao's avatar
dengyihao 已提交
122
  // if (pMeta->streamBackend) streamBackendCleanup(pMeta->streamBackend);
L
Liu Jicong 已提交
123
  taosMemoryFree(pMeta);
dengyihao's avatar
dengyihao 已提交
124
  qError("failed to open stream meta");
L
Liu Jicong 已提交
125 126 127 128
  return NULL;
}

void streamMetaClose(SStreamMeta* pMeta) {
129
  tdbAbort(pMeta->db, pMeta->txn);
130
  tdbTbClose(pMeta->pTaskDb);
131
  tdbTbClose(pMeta->pCheckpointDb);
132
  tdbClose(pMeta->db);
L
Liu Jicong 已提交
133 134 135 136

  void* pIter = NULL;
  while (1) {
    pIter = taosHashIterate(pMeta->pTasks, pIter);
137 138 139 140
    if (pIter == NULL) {
      break;
    }

L
Liu Jicong 已提交
141
    SStreamTask* pTask = *(SStreamTask**)pIter;
L
Liu Jicong 已提交
142 143 144 145
    if (pTask->timer) {
      taosTmrStop(pTask->timer);
      pTask->timer = NULL;
    }
146

147
    tFreeStreamTask(pTask);
L
Liu Jicong 已提交
148
  }
149

L
Liu Jicong 已提交
150
  taosHashCleanup(pMeta->pTasks);
dengyihao's avatar
dengyihao 已提交
151
  taosRemoveRef(streamBackendId, pMeta->streamBackendRid);
152
  pMeta->pTaskList = taosArrayDestroy(pMeta->pTaskList);
L
Liu Jicong 已提交
153
  taosMemoryFree(pMeta->path);
Y
yihaoDeng 已提交
154 155
  taosThreadMutexDestroy(&pMeta->backendMutex);
  taosHashCleanup(pMeta->pTaskBackendUnique);
L
Liu Jicong 已提交
156 157 158
  taosMemoryFree(pMeta);
}

159 160
#if 0
int32_t streamMetaAddSerializedTask(SStreamMeta* pMeta, int64_t ver, char* msg, int32_t msgLen) {
L
Liu Jicong 已提交
161 162 163 164 165 166
  SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
  if (pTask == NULL) {
    return -1;
  }
  SDecoder decoder;
  tDecoderInit(&decoder, (uint8_t*)msg, msgLen);
167
  if (tDecodeStreamTask(&decoder, pTask) < 0) {
L
Liu Jicong 已提交
168
    tDecoderClear(&decoder);
L
Liu Jicong 已提交
169 170 171 172
    goto FAIL;
  }
  tDecoderClear(&decoder);

173
  if (pMeta->expandFunc(pMeta->ahandle, pTask, ver) < 0) {
L
Liu Jicong 已提交
174 175 176 177
    ASSERT(0);
    goto FAIL;
  }

178
  if (taosHashPut(pMeta->pTasks, &pTask->id.taskId, sizeof(int32_t), &pTask, sizeof(void*)) < 0) {
L
Liu Jicong 已提交
179 180
    goto FAIL;
  }
L
Liu Jicong 已提交
181

182 183
  if (tdbTbUpsert(pMeta->pTaskDb, &pTask->id.taskId, sizeof(int32_t), msg, msgLen, pMeta->txn) < 0) {
    taosHashRemove(pMeta->pTasks, &pTask->id.taskId, sizeof(int32_t));
L
Liu Jicong 已提交
184
    ASSERT(0);
L
Liu Jicong 已提交
185
    goto FAIL;
L
Liu Jicong 已提交
186
  }
L
Liu Jicong 已提交
187

L
Liu Jicong 已提交
188 189 190
  return 0;

FAIL:
191
  if (pTask) tFreeStreamTask(pTask);
L
Liu Jicong 已提交
192
  return -1;
L
Liu Jicong 已提交
193
}
194
#endif
L
Liu Jicong 已提交
195

L
Liu Jicong 已提交
196 197
int32_t streamMetaSaveTask(SStreamMeta* pMeta, SStreamTask* pTask) {
  void*   buf = NULL;
L
Liu Jicong 已提交
198 199
  int32_t len;
  int32_t code;
200
  tEncodeSize(tEncodeStreamTask, pTask, len, code);
L
Liu Jicong 已提交
201 202 203
  if (code < 0) {
    return -1;
  }
L
Liu Jicong 已提交
204
  buf = taosMemoryCalloc(1, len);
L
Liu Jicong 已提交
205 206 207 208
  if (buf == NULL) {
    return -1;
  }

209
  SEncoder encoder = {0};
L
Liu Jicong 已提交
210
  tEncoderInit(&encoder, buf, len);
211
  tEncodeStreamTask(&encoder, pTask);
212
  tEncoderClear(&encoder);
L
Liu Jicong 已提交
213

214
  if (tdbTbUpsert(pMeta->pTaskDb, &pTask->id.taskId, sizeof(int32_t), buf, len, pMeta->txn) < 0) {
L
Liu Jicong 已提交
215 216 217
    return -1;
  }

L
Liu Jicong 已提交
218
  taosMemoryFree(buf);
L
Liu Jicong 已提交
219 220 221
  return 0;
}

222 223
// add to the ready tasks hash map, not the restored tasks hash map
int32_t streamMetaAddDeployedTask(SStreamMeta* pMeta, int64_t ver, SStreamTask* pTask) {
224 225
  void* p = taosHashGet(pMeta->pTasks, &pTask->id.taskId, sizeof(pTask->id.taskId));
  if (p == NULL) {
226 227 228 229 230 231 232 233 234 235 236 237 238 239
    if (pMeta->expandFunc(pMeta->ahandle, pTask, ver) < 0) {
      tFreeStreamTask(pTask);
      return -1;
    }

    if (streamMetaSaveTask(pMeta, pTask) < 0) {
      tFreeStreamTask(pTask);
      return -1;
    }

    if (streamMetaCommit(pMeta) < 0) {
      tFreeStreamTask(pTask);
      return -1;
    }
240
    taosArrayPush(pMeta->pTaskList, &pTask->id.taskId);
241 242
  } else {
    return 0;
243 244 245
  }

  taosHashPut(pMeta->pTasks, &pTask->id.taskId, sizeof(pTask->id.taskId), &pTask, POINTER_BYTES);
L
Liu Jicong 已提交
246 247
  return 0;
}
248 249

int32_t streamMetaGetNumOfTasks(const SStreamMeta* pMeta) {
250 251 252
  size_t size = taosHashGetSize(pMeta->pTasks);
  ASSERT(taosArrayGetSize(pMeta->pTaskList) == taosHashGetSize(pMeta->pTasks));

253
  return (int32_t)size;
254
}
L
Liu Jicong 已提交
255

L
Liu Jicong 已提交
256 257 258 259
SStreamTask* streamMetaAcquireTask(SStreamMeta* pMeta, int32_t taskId) {
  taosRLockLatch(&pMeta->lock);

  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
260
  if (ppTask != NULL) {
261
    if (!streamTaskShouldStop(&(*ppTask)->status)) {
262 263 264 265
      atomic_add_fetch_32(&(*ppTask)->refCnt, 1);
      taosRUnLockLatch(&pMeta->lock);
      return *ppTask;
    }
L
Liu Jicong 已提交
266
  }
267

L
Liu Jicong 已提交
268 269 270 271 272 273
  taosRUnLockLatch(&pMeta->lock);
  return NULL;
}

void streamMetaReleaseTask(SStreamMeta* pMeta, SStreamTask* pTask) {
  int32_t left = atomic_sub_fetch_32(&pTask->refCnt, 1);
274 275 276
  if (left < 0) {
    qError("task ref is invalid, ref:%d, %s", left, pTask->id.idStr);
  } else if (left == 0) {
277
    ASSERT(streamTaskShouldStop(&pTask->status));
278
    tFreeStreamTask(pTask);
L
Liu Jicong 已提交
279 280 281
  }
}

282
void streamMetaRemoveTask(SStreamMeta* pMeta, int32_t taskId) {
H
Haojun Liao 已提交
283
  SStreamTask* pTask = NULL;
H
Haojun Liao 已提交
284

H
Haojun Liao 已提交
285 286
  // pre-delete operation
  taosWLockLatch(&pMeta->lock);
L
Liu Jicong 已提交
287 288
  SStreamTask** ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
H
Haojun Liao 已提交
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
    pTask = *ppTask;
    atomic_store_8(&pTask->status.taskStatus, TASK_STATUS__DROPPING);
  } else {
    qDebug("vgId:%d failed to find the task:0x%x, it may be dropped already", pMeta->vgId, taskId);
    taosWUnLockLatch(&pMeta->lock);
    return;
  }
  taosWUnLockLatch(&pMeta->lock);

  qDebug("s-task:0x%x set task status:%s", taskId, streamGetTaskStatusStr(TASK_STATUS__DROPPING));

  while(1) {
    taosRLockLatch(&pMeta->lock);
    ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));

    if (ppTask) {
      if ((*ppTask)->status.timerActive == 0) {
        taosRUnLockLatch(&pMeta->lock);
        break;
      }
309

H
Haojun Liao 已提交
310 311 312 313 314 315 316 317 318 319 320 321 322
      taosMsleep(10);
      qDebug("s-task:%s wait for quit from timer", (*ppTask)->id.idStr);
      taosRUnLockLatch(&pMeta->lock);
    } else {
      taosRUnLockLatch(&pMeta->lock);
      break;
    }
  }

  // let's do delete of stream task
  taosWLockLatch(&pMeta->lock);
  ppTask = (SStreamTask**)taosHashGet(pMeta->pTasks, &taskId, sizeof(int32_t));
  if (ppTask) {
L
Liu Jicong 已提交
323
    taosHashRemove(pMeta->pTasks, &taskId, sizeof(int32_t));
L
Liu Jicong 已提交
324
    tdbTbDelete(pMeta->pTaskDb, &taskId, sizeof(int32_t), pMeta->txn);
325

dengyihao's avatar
dengyihao 已提交
326
    atomic_store_8(&pTask->status.taskStatus, TASK_STATUS__DROPPING);
H
Haojun Liao 已提交
327
    ASSERT(pTask->status.timerActive == 0);
328

H
Haojun Liao 已提交
329
    int32_t num = taosArrayGetSize(pMeta->pTaskList);
330
    qDebug("s-task:%s set the drop task flag, remain running s-task:%d", pTask->id.idStr, num - 1);
331
    for (int32_t i = 0; i < num; ++i) {
332 333 334 335 336 337 338
      int32_t* pTaskId = taosArrayGet(pMeta->pTaskList, i);
      if (*pTaskId == taskId) {
        taosArrayRemove(pMeta->pTaskList, i);
        break;
      }
    }

L
Liu Jicong 已提交
339
    streamMetaReleaseTask(pMeta, pTask);
340 341
  } else {
    qDebug("vgId:%d failed to find the task:0x%x, it may be dropped already", pMeta->vgId, taskId);
L
Liu Jicong 已提交
342
  }
H
Haojun Liao 已提交
343 344

  taosWUnLockLatch(&pMeta->lock);
L
Liu Jicong 已提交
345 346
}

L
Liu Jicong 已提交
347
int32_t streamMetaBegin(SStreamMeta* pMeta) {
348 349
  if (tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
L
Liu Jicong 已提交
350 351 352 353 354
    return -1;
  }
  return 0;
}

355
// todo add error log
L
Liu Jicong 已提交
356
int32_t streamMetaCommit(SStreamMeta* pMeta) {
357
  if (tdbCommit(pMeta->db, pMeta->txn) < 0) {
358
    qError("failed to commit stream meta");
L
Liu Jicong 已提交
359 360
    return -1;
  }
361

362
  if (tdbPostCommit(pMeta->db, pMeta->txn) < 0) {
363
    qError("failed to commit stream meta");
364 365
    return -1;
  }
366 367 368

  if (tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
369 370
    return -1;
  }
371

L
Liu Jicong 已提交
372 373 374
  return 0;
}

375
int32_t streamMetaAbort(SStreamMeta* pMeta) {
376
  if (tdbAbort(pMeta->db, pMeta->txn) < 0) {
377 378
    return -1;
  }
379 380 381

  if (tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
               TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED) < 0) {
382 383
    return -1;
  }
L
Liu Jicong 已提交
384 385
  return 0;
}
386

L
Liu Jicong 已提交
387
int32_t streamLoadTasks(SStreamMeta* pMeta, int64_t ver) {
L
Liu Jicong 已提交
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  TBC* pCur = NULL;
  if (tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL) < 0) {
    return -1;
  }

  void*    pKey = NULL;
  int32_t  kLen = 0;
  void*    pVal = NULL;
  int32_t  vLen = 0;
  SDecoder decoder;

  tdbTbcMoveToFirst(pCur);

  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
    SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
    if (pTask == NULL) {
L
Liu Jicong 已提交
404 405
      tdbFree(pKey);
      tdbFree(pVal);
5
54liuyao 已提交
406
      tdbTbcClose(pCur);
L
Liu Jicong 已提交
407 408
      return -1;
    }
409

L
Liu Jicong 已提交
410
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
411
    tDecodeStreamTask(&decoder, pTask);
L
Liu Jicong 已提交
412
    tDecoderClear(&decoder);
413

414
    // remove duplicate
415 416
    void* p = taosHashGet(pMeta->pTasks, &pTask->id.taskId, sizeof(pTask->id.taskId));
    if (p == NULL) {
417 418 419 420
      if (pMeta->expandFunc(pMeta->ahandle, pTask, pTask->chkInfo.version) < 0) {
        tdbFree(pKey);
        tdbFree(pVal);
        tdbTbcClose(pCur);
421
        taosMemoryFree(pTask);
422 423
        return -1;
      }
424
      taosArrayPush(pMeta->pTaskList, &pTask->id.taskId);
425
    } else {
426 427 428 429
      tdbFree(pKey);
      tdbFree(pVal);
      tdbTbcClose(pCur);
      taosMemoryFree(pTask);
430
      continue;
431
    }
432

433
    if (taosHashPut(pMeta->pTasks, &pTask->id.taskId, sizeof(pTask->id.taskId), &pTask, sizeof(void*)) < 0) {
L
Liu Jicong 已提交
434 435
      tdbFree(pKey);
      tdbFree(pVal);
5
54liuyao 已提交
436
      tdbTbcClose(pCur);
437
      taosMemoryFree(pTask);
438 439
      return -1;
    }
440

441
    ASSERT(pTask->status.checkDownstream == 0);
442 443
  }

444 445
  tdbFree(pKey);
  tdbFree(pVal);
446 447
  if (tdbTbcClose(pCur) < 0) {
    return -1;
L
Liu Jicong 已提交
448 449 450 451
  }

  return 0;
}