• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3531

19 Nov 2024 10:42AM UTC coverage: 60.213% (-0.006%) from 60.219%
#3531

push

travis-ci

web-flow
Merge pull request #28777 from taosdata/fix/3.0/TD-32366

fix:TD-32366/stmt add geometry datatype check

118529 of 252344 branches covered (46.97%)

Branch coverage included in aggregate %.

7 of 48 new or added lines in 3 files covered. (14.58%)

2282 existing lines in 115 files now uncovered.

199096 of 275161 relevant lines covered (72.36%)

6067577.83 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#include "streamBackendRocksdb.h"
17
#include "streamInt.h"
18
#include "tmisce.h"
19
#include "tref.h"
20
#include "tsched.h"
21
#include "tstream.h"
22
#include "ttimer.h"
23
#include "wal.h"
24

25
static TdThreadOnce streamMetaModuleInit = PTHREAD_ONCE_INIT;
26

27
int32_t streamBackendId = 0;
28
int32_t streamBackendCfWrapperId = 0;
29
int32_t taskDbWrapperId = 0;
30
int32_t streamTaskRefPool = 0;
31

32
static int32_t streamMetaBegin(SStreamMeta* pMeta);
33
static void    streamMetaCloseImpl(void* arg);
34

35
typedef struct {
36
  TdThreadMutex mutex;
37
  SHashObj*     pTable;
38
} SMetaRefMgt;
39

40
SMetaRefMgt gMetaRefMgt;
41

42
int32_t metaRefMgtInit();
43
void    metaRefMgtCleanup();
44

45
static void streamMetaEnvInit() {
2,371✔
46
  streamBackendId = taosOpenRef(64, streamBackendCleanup);
2,371✔
47
  streamBackendCfWrapperId = taosOpenRef(64, streamBackendHandleCleanup);
2,371✔
48
  taskDbWrapperId = taosOpenRef(64, taskDbDestroy2);
2,371✔
49

50
  streamMetaRefPool = taosOpenRef(64, streamMetaCloseImpl);
2,371✔
51
  streamTaskRefPool = taosOpenRef(64, tFreeStreamTask);
2,371✔
52

53
  int32_t code = metaRefMgtInit();
2,371✔
54
  if (code) {
2,371!
55
    stError("failed to init stream meta mgmt env, start failed");
×
56
    return;
×
57
  }
58

59
  code = streamTimerInit();
2,371✔
60
  if (code) {
2,371!
61
    stError("failed to init stream meta env, start failed");
×
62
  }
63
}
64

65
void streamMetaInit() {
2,371✔
66
  int32_t code = taosThreadOnce(&streamMetaModuleInit, streamMetaEnvInit);
2,371✔
67
  if (code) {
2,371!
68
    stError("failed to init stream Meta model, code:%s", tstrerror(code));
×
69
  }
70
}
2,371✔
71

72
void streamMetaCleanup() {
2,371✔
73
  taosCloseRef(streamBackendId);
2,371✔
74
  taosCloseRef(streamBackendCfWrapperId);
2,371✔
75
  taosCloseRef(streamMetaRefPool);
2,371✔
76
  taosCloseRef(streamTaskRefPool);
2,371✔
77

78
  metaRefMgtCleanup();
2,371✔
79
  streamTimerCleanUp();
2,371✔
80
}
2,371✔
81

82
int32_t metaRefMgtInit() {
2,371✔
83
  int32_t code = taosThreadMutexInit(&(gMetaRefMgt.mutex), NULL);
2,371✔
84
  if (code) {
2,371!
85
    return code;
×
86
  }
87

88
  if (code == 0) {
2,371!
89
    gMetaRefMgt.pTable = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
2,371✔
90
  }
91

92
  if (gMetaRefMgt.pTable == NULL) {
2,371!
93
    return terrno;
×
94
  } else {
95
    return code;
2,371✔
96
  }
97
}
98

99
void metaRefMgtCleanup() {
2,371✔
100
  void* pIter = taosHashIterate(gMetaRefMgt.pTable, NULL);
2,371✔
101
  while (pIter) {
28,355✔
102
    int64_t* p = *(int64_t**) pIter;
25,984✔
103
    taosMemoryFree(p);
25,984✔
104
    pIter = taosHashIterate(gMetaRefMgt.pTable, pIter);
25,984✔
105
  }
106

107
  taosHashCleanup(gMetaRefMgt.pTable);
2,371✔
108
  streamMutexDestroy(&gMetaRefMgt.mutex);
2,371✔
109
}
2,371✔
110

111
int32_t metaRefMgtAdd(int64_t vgId, int64_t* rid) {
44,656✔
112
  int32_t code = 0;
44,656✔
113
  void*   p = NULL;
44,656✔
114

115
  streamMutexLock(&gMetaRefMgt.mutex);
44,656✔
116

117
  p = taosHashGet(gMetaRefMgt.pTable, &rid, sizeof(rid));
44,656✔
118
  if (p == NULL) {
44,656!
119
    code = taosHashPut(gMetaRefMgt.pTable, &rid, sizeof(rid), &rid, sizeof(void*));
44,656✔
120
    if (code) {
44,656!
121
      stError("vgId:%d failed to put into refId mgt, refId:%" PRId64" %p, code:%s", (int32_t)vgId, *rid, rid,
×
122
              tstrerror(code));
123
      return code;
×
124
    } else { // not
125
//      stInfo("add refId:%"PRId64" vgId:%d, %p", *rid, (int32_t)vgId, rid);
126
    }
127
  } else {
128
    stFatal("try to add refId:%"PRId64" vgId:%d, %p that already added into mgt", *rid, (int32_t) vgId, rid);
×
129
  }
130

131
  streamMutexUnlock(&gMetaRefMgt.mutex);
44,656✔
132
  return code;
44,656✔
133
}
134

135
void metaRefMgtRemove(int64_t* pRefId) {
18,672✔
136
  streamMutexLock(&gMetaRefMgt.mutex);
18,672✔
137

138
  int32_t code = taosHashRemove(gMetaRefMgt.pTable, &pRefId, sizeof(pRefId));
18,672✔
139
  taosMemoryFree(pRefId);
18,672✔
140
  streamMutexUnlock(&gMetaRefMgt.mutex);
18,672✔
141
}
18,672✔
142

143
int32_t streamMetaOpenTdb(SStreamMeta* pMeta) {
11,937✔
144
  if (tdbOpen(pMeta->path, 16 * 1024, 1, &pMeta->db, 0, 0, NULL) < 0) {
11,937!
145
    stError("vgId:%d open file:%s failed, stream meta open failed", pMeta->vgId, pMeta->path);
×
146
    return -1;
×
147
  }
148

149
  if (tdbTbOpen("task.db", STREAM_TASK_KEY_LEN, -1, NULL, pMeta->db, &pMeta->pTaskDb, 0) < 0) {
12,282!
150
    stError("vgId:%d, open task.db failed, stream meta open failed", pMeta->vgId);
×
151
    return -1;
×
152
  }
153

154
  if (tdbTbOpen("checkpoint.db", sizeof(int32_t), -1, NULL, pMeta->db, &pMeta->pCheckpointDb, 0) < 0) {
12,280!
155
    stError("vgId:%d, open checkpoint.db failed, stream meta open failed", pMeta->vgId);
×
156
    return -1;
×
157
  }
158

159
  return 0;
12,282✔
160
}
161

162
//
163
// impl later
164
//
165
enum STREAM_STATE_VER {
166
  STREAM_STATA_NO_COMPATIBLE,
167
  STREAM_STATA_COMPATIBLE,
168
  STREAM_STATA_NEED_CONVERT,
169
};
170

171
int32_t streamMetaCheckBackendCompatible(SStreamMeta* pMeta) {
12,282✔
172
  int8_t  ret = STREAM_STATA_COMPATIBLE;
12,282✔
173
  TBC*    pCur = NULL;
12,282✔
174
  int32_t code = 0;
12,282✔
175
  void*   pKey = NULL;
12,282✔
176
  int32_t kLen = 0;
12,282✔
177
  void*   pVal = NULL;
12,282✔
178
  int32_t vLen = 0;
12,282✔
179

180
  if (tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL) < 0) {  // no task info, no stream
12,282!
181
    return ret;
×
182
  }
183

184
  code = tdbTbcMoveToFirst(pCur);
12,282✔
185
  if (code) {
12,280!
186
    stError("vgId:%d failed to open stream meta file cursor, not perform compatible check, code:%s", pMeta->vgId,
×
187
            tstrerror(code));
188
    tdbTbcClose(pCur);
×
189
    return ret;
×
190
  }
191

192
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
12,280✔
193
    if (pVal == NULL || vLen == 0) {
36!
194
      break;
195
    }
196

197
    SDecoder        decoder;
198
    SCheckpointInfo info;
199
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
36✔
200
    if (tDecodeStreamTaskChkInfo(&decoder, &info) < 0) {
36!
201
      continue;
×
202
    }
203

204
    if (info.msgVer <= SSTREAM_TASK_INCOMPATIBLE_VER) {
36!
205
      ret = STREAM_STATA_NO_COMPATIBLE;
×
206
    } else if (info.msgVer >= SSTREAM_TASK_NEED_CONVERT_VER) {
36!
207
      ret = STREAM_STATA_NEED_CONVERT;
36✔
208
    }
209

210
    tDecoderClear(&decoder);
36✔
211
    break;
36✔
212
  }
213

214
  tdbFree(pKey);
12,280✔
215
  tdbFree(pVal);
12,280✔
216
  tdbTbcClose(pCur);
12,281✔
217
  return ret;
12,282✔
218
}
219

220
int32_t streamMetaCvtDbFormat(SStreamMeta* pMeta) {
36✔
221
  int32_t          code = 0;
36✔
222
  SBackendWrapper* pBackend = NULL;
36✔
223
  int64_t          chkpId = streamMetaGetLatestCheckpointId(pMeta);
36✔
224

225
  terrno = 0;
36✔
226
  bool exist = streamBackendDataIsExist(pMeta->path, chkpId);
36✔
227
  if (exist == false) {
36!
228
    code = terrno;
36✔
229
    return code;
36✔
230
  }
231

232
  code = streamBackendInit(pMeta->path, chkpId, pMeta->vgId, &pBackend);
×
233
  if (code) {
×
234
    return code;
×
235
  }
236

237
  void* pIter = taosHashIterate(pBackend->cfInst, NULL);
×
238
  while (pIter) {
×
239
    void* key = taosHashGetKey(pIter, NULL);
×
240
    code = streamStateCvtDataFormat(pMeta->path, key, *(void**)pIter);
×
241
    if (code != 0) {
×
242
      stError("failed to cvt data");
×
243
      goto _EXIT;
×
244
    }
245

246
    pIter = taosHashIterate(pBackend->cfInst, pIter);
×
247
  }
248

249
_EXIT:
×
250
  streamBackendCleanup((void*)pBackend);
×
251

252
  if (code == 0) {
×
253
    char* state = taosMemoryCalloc(1, strlen(pMeta->path) + 32);
×
254
    if (state != NULL) {
×
255
      sprintf(state, "%s%s%s", pMeta->path, TD_DIRSEP, "state");
×
256
      taosRemoveDir(state);
×
257
      taosMemoryFree(state);
×
258
    } else {
259
      stError("vgId:%d, failed to remove file dir:%s, since:%s", pMeta->vgId, pMeta->path, tstrerror(code));
×
260
    }
261
  }
262

263
  return code;
×
264
}
265

266
int32_t streamMetaMayCvtDbFormat(SStreamMeta* pMeta) {
12,282✔
267
  int8_t compatible = streamMetaCheckBackendCompatible(pMeta);
12,282✔
268
  if (compatible == STREAM_STATA_COMPATIBLE) {
12,282✔
269
    return 0;
12,246✔
270
  } else if (compatible == STREAM_STATA_NEED_CONVERT) {
36!
271
    stInfo("vgId:%d stream state need covert backend format", pMeta->vgId);
36!
272
    return streamMetaCvtDbFormat(pMeta);
36✔
273
  } else if (compatible == STREAM_STATA_NO_COMPATIBLE) {
×
274
    stError(
×
275
        "vgId:%d stream read incompatible data, rm %s/vnode/vnode*/tq/stream if taosd cannot start, and rebuild stream "
276
        "manually",
277
        pMeta->vgId, tsDataDir);
278

279
    return -1;
×
280
  }
281

282
  return 0;
×
283
}
284

285
int32_t streamTaskSetDb(SStreamMeta* pMeta, SStreamTask* pTask, const char* key) {
4,131✔
286
  int32_t code = 0;
4,131✔
287
  int64_t chkpId = pTask->chkInfo.checkpointId;
4,131✔
288

289
  streamMutexLock(&pMeta->backendMutex);
4,131✔
290
  void** ppBackend = taosHashGet(pMeta->pTaskDbUnique, key, strlen(key));
4,131✔
291
  if ((ppBackend != NULL) && (*ppBackend != NULL)) {
4,131!
292
    void* p = taskDbAddRef(*ppBackend);
1,404✔
293
    if (p == NULL) {
1,404!
294
      stError("s-task:0x%x failed to ref backend", pTask->id.taskId);
×
295
      return TSDB_CODE_FAILED;
×
296
    }
297

298
    STaskDbWrapper* pBackend = *ppBackend;
1,404✔
299
    pBackend->pMeta = pMeta;
1,404✔
300
    pTask->pBackend = pBackend;
1,404✔
301

302
    streamMutexUnlock(&pMeta->backendMutex);
1,404✔
303
    stDebug("s-task:0x%x set backend %p", pTask->id.taskId, pBackend);
1,404✔
304
    return 0;
1,404✔
305
  }
306

307
  STaskDbWrapper* pBackend = NULL;
2,727✔
308
  int64_t         processVer = -1;
2,727✔
309
  while (1) {
310
    code = taskDbOpen(pMeta->path, key, chkpId, &processVer, &pBackend);
2,727✔
311
    if (code == 0) {
2,727!
312
      break;
2,727✔
313
    }
314

315
    streamMutexUnlock(&pMeta->backendMutex);
×
316
    taosMsleep(1000);
×
317

318
    stDebug("backend held by other task, restart later, path:%s, key:%s", pMeta->path, key);
×
319
    streamMutexLock(&pMeta->backendMutex);
×
320
  }
321

322
  int64_t tref = taosAddRef(taskDbWrapperId, pBackend);
2,727✔
323
  pTask->pBackend = pBackend;
2,727✔
324
  pBackend->refId = tref;
2,727✔
325
  pBackend->pTask = pTask;
2,727✔
326
  pBackend->pMeta = pMeta;
2,727✔
327

328
  if (processVer != -1) {
2,727✔
329
    if (pTask->chkInfo.processedVer != processVer) {
17!
330
      stWarn("s-task:%s vgId:%d update checkpointVer:%" PRId64 "->%" PRId64 " for checkpointId:%" PRId64,
×
331
             pTask->id.idStr, pTask->pMeta->vgId, pTask->chkInfo.processedVer, processVer, pTask->chkInfo.checkpointId);
332
      pTask->chkInfo.processedVer = processVer;
×
333
      pTask->chkInfo.checkpointVer = processVer;
×
334
      pTask->chkInfo.nextProcessVer = processVer + 1;
×
335
    } else {
336
      stInfo("s-task:%s vgId:%d processedVer:%" PRId64
17!
337
             " in task meta equals to data in checkpoint data for checkpointId:%" PRId64,
338
             pTask->id.idStr, pTask->pMeta->vgId, pTask->chkInfo.processedVer, pTask->chkInfo.checkpointId);
339
    }
340
  }
341

342
  code = taosHashPut(pMeta->pTaskDbUnique, key, strlen(key), &pBackend, sizeof(void*));
2,727✔
343
  if (code) {
2,727!
344
    stError("s-task:0x%x failed to put taskDb backend, code:out of memory", pTask->id.taskId);
×
345
  }
346
  streamMutexUnlock(&pMeta->backendMutex);
2,727✔
347

348
  stDebug("s-task:0x%x set backend %p", pTask->id.taskId, pBackend);
2,727✔
349
  return 0;
2,727✔
350
}
351

352
void streamMetaRemoveDB(void* arg, char* key) {
2,629✔
353
  if (arg == NULL || key == NULL) return;
2,629!
354

355
  SStreamMeta* pMeta = arg;
2,629✔
356
  streamMutexLock(&pMeta->backendMutex);
2,629✔
357
  int32_t code = taosHashRemove(pMeta->pTaskDbUnique, key, strlen(key));
2,629✔
358
  if (code) {
2,629!
359
    stError("vgId:%d failed to remove key:%s in taskDbUnique map", pMeta->vgId, key);
×
360
  }
361

362
  streamMutexUnlock(&pMeta->backendMutex);
2,629✔
363
}
364

365
int32_t streamMetaOpen(const char* path, void* ahandle, FTaskBuild buildTaskFn, FTaskExpand expandTaskFn, int32_t vgId,
12,014✔
366
                       int64_t stage, startComplete_fn_t fn, SStreamMeta** p) {
367
  QRY_PARAM_CHECK(p);
12,014!
368
  int32_t code = 0;
12,014✔
369
  int32_t lino = 0;
12,014✔
370

371
  SStreamMeta* pMeta = taosMemoryCalloc(1, sizeof(SStreamMeta));
12,014✔
372
  if (pMeta == NULL) {
12,276!
373
    stError("vgId:%d failed to prepare stream meta, alloc size:%" PRIzu ", out of memory", vgId, sizeof(SStreamMeta));
×
374
    return terrno;
×
375
  }
376

377
  int32_t len = strlen(path) + 64;
12,276✔
378
  char*   tpath = taosMemoryCalloc(1, len);
12,276✔
379
  TSDB_CHECK_NULL(tpath, code, lino, _err, terrno);
12,278!
380

381
  sprintf(tpath, "%s%s%s", path, TD_DIRSEP, "stream");
12,278✔
382
  pMeta->path = tpath;
12,278✔
383

384
  code = streamMetaOpenTdb(pMeta);
12,278✔
385
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
386

387
  if ((code = streamMetaMayCvtDbFormat(pMeta)) < 0) {
12,282!
388
    stError("vgId:%d convert sub info format failed, open stream meta failed, reason: %s", pMeta->vgId,
×
389
            tstrerror(terrno));
390
    TSDB_CHECK_CODE(code, lino, _err);
×
391
  }
392

393
  if ((code = streamMetaBegin(pMeta) < 0)) {
12,281!
394
    stError("vgId:%d begin trans for stream meta failed", pMeta->vgId);
×
395
    goto _err;
×
396
  }
397

398
  _hash_fn_t fp = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
12,278✔
399
  pMeta->pTasksMap = taosHashInit(64, fp, true, HASH_NO_LOCK);
12,281✔
400
  TSDB_CHECK_NULL(pMeta->pTasksMap, code, lino, _err, terrno);
12,281!
401

402
  pMeta->updateInfo.pTasks = taosHashInit(64, fp, false, HASH_NO_LOCK);
12,281✔
403
  TSDB_CHECK_NULL(pMeta->updateInfo.pTasks, code, lino, _err, terrno);
12,282!
404

405
  code = streamMetaInitStartInfo(&pMeta->startInfo);
12,282✔
406
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
407

408
  // task list
409
  pMeta->pTaskList = taosArrayInit(4, sizeof(SStreamTaskId));
12,282✔
410
  TSDB_CHECK_NULL(pMeta->pTaskList, code, lino, _err, terrno);
12,282!
411

412
  pMeta->scanInfo.scanCounter = 0;
12,282✔
413
  pMeta->vgId = vgId;
12,282✔
414
  pMeta->ahandle = ahandle;
12,282✔
415
  pMeta->buildTaskFn = buildTaskFn;
12,282✔
416
  pMeta->expandTaskFn = expandTaskFn;
12,282✔
417
  pMeta->stage = stage;
12,282✔
418
  pMeta->role = (vgId == SNODE_HANDLE) ? NODE_ROLE_LEADER : NODE_ROLE_UNINIT;
12,282✔
419
  pMeta->updateInfo.activeTransId = -1;
12,282✔
420
  pMeta->updateInfo.completeTransId = -1;
12,282✔
421

422
  pMeta->startInfo.completeFn = fn;
12,282✔
423
  pMeta->pTaskDbUnique = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
12,282✔
424
  TSDB_CHECK_NULL(pMeta->pTaskDbUnique, code, lino, _err, terrno);
12,279!
425

426
  pMeta->numOfPausedTasks = 0;
12,279✔
427
  pMeta->numOfStreamTasks = 0;
12,279✔
428
  pMeta->closeFlag = false;
12,279✔
429

430
  stInfo("vgId:%d open stream meta succ, latest checkpoint:%" PRId64 ", stage:%" PRId64, vgId, pMeta->chkpId, stage);
12,279!
431

432
  // set the attribute when running on Linux OS
433
  TdThreadRwlockAttr attr;
434
  code = taosThreadRwlockAttrInit(&attr);
12,282✔
435
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
436

437
#ifdef LINUX
438
  code = pthread_rwlockattr_setkind_np(&attr, PTHREAD_RWLOCK_PREFER_WRITER_NONRECURSIVE_NP);
12,282✔
439
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
440
#endif
441

442
  code = taosThreadRwlockInit(&pMeta->lock, &attr);
12,282✔
443
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
444

445
  code = taosThreadRwlockAttrDestroy(&attr);
12,282✔
446
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
447

448
  code = bkdMgtCreate(tpath, (SBkdMgt**)&pMeta->bkdChkptMgt);
12,282✔
449
  TSDB_CHECK_CODE(code, lino, _err);
12,281!
450

451
  code = taosThreadMutexInit(&pMeta->backendMutex, NULL);
12,281✔
452
  TSDB_CHECK_CODE(code, lino, _err);
12,281!
453

454
  // add refId at the end of initialization function
455
  pMeta->rid = taosAddRef(streamMetaRefPool, pMeta);
12,281✔
456

457
  int64_t* pRid = taosMemoryMalloc(sizeof(int64_t));
12,282✔
458
  TSDB_CHECK_NULL(pRid, code, lino, _err, terrno);
12,282!
459

460
  memcpy(pRid, &pMeta->rid, sizeof(pMeta->rid));
12,282✔
461

462
  code = metaRefMgtAdd(pMeta->vgId, pRid);
12,282✔
463
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
464

465
  code = createMetaHbInfo(pRid, &pMeta->pHbInfo);
12,282✔
466

467
  TSDB_CHECK_CODE(code, lino, _err);
12,282!
468

469
  *p = pMeta;
12,282✔
470
  return code;
12,282✔
471

472
_err:
×
473
  taosMemoryFree(pMeta->path);
×
474
  if (pMeta->pTasksMap) taosHashCleanup(pMeta->pTasksMap);
×
475
  if (pMeta->pTaskList) taosArrayDestroy(pMeta->pTaskList);
×
476
  if (pMeta->pTaskDb) {
×
477
    tdbTbClose(pMeta->pTaskDb);
×
478
    pMeta->pTaskDb = NULL;
×
479
  }
480
  if (pMeta->pCheckpointDb) {
×
481
    tdbTbClose(pMeta->pCheckpointDb);
×
482
  }
483
  if (pMeta->db) {
×
484
    tdbClose(pMeta->db);
×
485
  }
486

487
  if (pMeta->pHbInfo) taosMemoryFreeClear(pMeta->pHbInfo);
×
488
  if (pMeta->updateInfo.pTasks) taosHashCleanup(pMeta->updateInfo.pTasks);
×
489
  if (pMeta->startInfo.pReadyTaskSet) taosHashCleanup(pMeta->startInfo.pReadyTaskSet);
×
490
  if (pMeta->startInfo.pFailedTaskSet) taosHashCleanup(pMeta->startInfo.pFailedTaskSet);
×
491
  if (pMeta->bkdChkptMgt) bkdMgtDestroy(pMeta->bkdChkptMgt);
×
492

493
  taosMemoryFree(pMeta);
×
494

495
  stError("vgId:%d failed to open stream meta, at line:%d reason:%s", vgId, lino, tstrerror(code));
×
496
  return code;
×
497
}
498

499
// todo refactor: the lock shoud be restricted in one function
500
#ifdef BUILD_NO_CALL
501
void streamMetaInitBackend(SStreamMeta* pMeta) {
502
  pMeta->streamBackend = streamBackendInit(pMeta->path, pMeta->chkpId, pMeta->vgId);
503
  if (pMeta->streamBackend == NULL) {
504
    streamMetaWUnLock(pMeta);
505

506
    while (1) {
507
      streamMetaWLock(pMeta);
508
      pMeta->streamBackend = streamBackendInit(pMeta->path, pMeta->chkpId, pMeta->vgId);
509
      if (pMeta->streamBackend != NULL) {
510
        break;
511
      }
512

513
      streamMetaWUnLock(pMeta);
514
      stInfo("vgId:%d failed to init stream backend, retry in 100ms", pMeta->vgId);
515
      taosMsleep(100);
516
    }
517
  }
518

519
  pMeta->streamBackendRid = taosAddRef(streamBackendId, pMeta->streamBackend);
520
  streamBackendLoadCheckpointInfo(pMeta);
521
}
522
#endif
523

524
void streamMetaClear(SStreamMeta* pMeta) {
12,285✔
525
  // remove all existed tasks in this vnode
526
  void* pIter = NULL;
12,285✔
527
  while ((pIter = taosHashIterate(pMeta->pTasksMap, pIter)) != NULL) {
14,036✔
528
    int64_t      refId = *(int64_t*)pIter;
1,751✔
529
    SStreamTask* p = taosAcquireRef(streamTaskRefPool, refId);
1,751✔
530
    if (p == NULL) {
1,751✔
531
      continue;
1,740✔
532
    }
533

534
    // release the ref by timer
535
    if (p->info.delaySchedParam != 0 && p->info.fillHistory == 0) {  // one more ref in timer
11!
536
      stDebug("s-task:%s stop schedTimer", p->id.idStr);
×
537
      streamTmrStop(p->schedInfo.pDelayTimer);
×
538
      p->info.delaySchedParam = 0;
×
539
    }
540

541
    int32_t code = taosRemoveRef(streamTaskRefPool, refId);
11✔
542
    if (code) {
11!
543
      stError("vgId:%d remove task refId failed, refId:%" PRId64, pMeta->vgId, refId);
×
544
    }
545

546
    code = taosReleaseRef(streamTaskRefPool, refId);
11✔
547
    if (code) {
11!
548
      stError("vgId:%d failed to release refId:%" PRId64, pMeta->vgId, refId);
×
549
    }
550
  }
551

552
  if (pMeta->streamBackendRid != 0) {
12,285!
553
    int32_t code = taosRemoveRef(streamBackendId, pMeta->streamBackendRid);
×
554
    if (code) {
×
555
      stError("vgId:%d remove stream backend Ref failed, rid:%" PRId64, pMeta->vgId, pMeta->streamBackendRid);
×
556
    }
557
  }
558

559
  taosHashClear(pMeta->pTasksMap);
12,285✔
560

561
  taosArrayClear(pMeta->pTaskList);
12,285✔
562
  taosArrayClear(pMeta->chkpSaved);
12,285✔
563
  taosArrayClear(pMeta->chkpInUse);
12,285✔
564

565
  pMeta->numOfStreamTasks = 0;
12,285✔
566
  pMeta->numOfPausedTasks = 0;
12,285✔
567

568
  // the willrestart/starting flag can NOT be cleared
569
  taosHashClear(pMeta->startInfo.pReadyTaskSet);
12,285✔
570
  taosHashClear(pMeta->startInfo.pFailedTaskSet);
12,285✔
571
  pMeta->startInfo.readyTs = 0;
12,285✔
572
}
12,285✔
573

574
void streamMetaClose(SStreamMeta* pMeta) {
12,274✔
575
  stDebug("vgId:%d start to close stream meta", pMeta->vgId);
12,274✔
576
  if (pMeta == NULL) {
12,282!
577
    return;
×
578
  }
579
  int32_t code = taosRemoveRef(streamMetaRefPool, pMeta->rid);
12,282✔
580
  if (code) {
12,282!
581
    stError("vgId:%d failed to remove meta ref:%" PRId64 ", code:%s", pMeta->vgId, pMeta->rid, tstrerror(code));
×
582
  }
583
}
584

585
void streamMetaCloseImpl(void* arg) {
12,282✔
586
  SStreamMeta* pMeta = arg;
12,282✔
587
  if (pMeta == NULL) {
12,282!
588
    return;
×
589
  }
590

591
  int32_t code = 0;
12,282✔
592
  int32_t vgId = pMeta->vgId;
12,282✔
593
  stDebug("vgId:%d start to do-close stream meta", vgId);
12,282✔
594

595
  streamMetaWLock(pMeta);
12,282✔
596
  streamMetaClear(pMeta);
12,282✔
597
  streamMetaWUnLock(pMeta);
12,282✔
598

599
  // already log the error, ignore here
600
  tdbAbort(pMeta->db, pMeta->txn);
12,282✔
601
  tdbTbClose(pMeta->pTaskDb);
12,278✔
602
  tdbTbClose(pMeta->pCheckpointDb);
12,282✔
603
  tdbClose(pMeta->db);
12,282✔
604

605
  taosArrayDestroy(pMeta->pTaskList);
12,281✔
606
  taosArrayDestroy(pMeta->chkpSaved);
12,282✔
607
  taosArrayDestroy(pMeta->chkpInUse);
12,282✔
608

609
  taosHashCleanup(pMeta->pTasksMap);
12,282✔
610
  taosHashCleanup(pMeta->pTaskDbUnique);
12,282✔
611
  taosHashCleanup(pMeta->updateInfo.pTasks);
12,282✔
612

613
  streamMetaClearStartInfo(&pMeta->startInfo);
12,282✔
614

615
  destroyMetaHbInfo(pMeta->pHbInfo);
12,282✔
616
  pMeta->pHbInfo = NULL;
12,282✔
617

618
  taosMemoryFree(pMeta->path);
12,282✔
619
  streamMutexDestroy(&pMeta->backendMutex);
12,282✔
620

621
  bkdMgtDestroy(pMeta->bkdChkptMgt);
12,281✔
622

623
  pMeta->role = NODE_ROLE_UNINIT;
12,282✔
624
  code = taosThreadRwlockDestroy(&pMeta->lock);
12,282✔
625
  if (code) {
12,281!
626
    stError("vgId:%d destroy rwlock, code:%s", vgId, tstrerror(code));
×
627
  }
628

629
  taosMemoryFree(pMeta);
12,281✔
630
  stDebug("vgId:%d end to close stream meta", vgId);
12,281✔
631
}
632

633
// todo let's check the status for each task
634
int32_t streamMetaSaveTask(SStreamMeta* pMeta, SStreamTask* pTask) {
9,962✔
635
  int32_t vgId = pTask->pMeta->vgId;
9,962✔
636
  void*   buf = NULL;
9,962✔
637
  int32_t len;
638
  int32_t code;
639
  tEncodeSize(tEncodeStreamTask, pTask, len, code);
9,962!
640
  if (code < 0) {
9,961!
641
    return -1;
×
642
  }
643

644
  buf = taosMemoryCalloc(1, len);
9,961✔
645
  if (buf == NULL) {
9,961!
646
    return terrno;
×
647
  }
648

649
  if (pTask->ver < SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
9,961✔
650
    pTask->ver = SSTREAM_TASK_VER;
22✔
651
  }
652

653
  SEncoder encoder = {0};
9,961✔
654
  tEncoderInit(&encoder, buf, len);
9,961✔
655
  code = tEncodeStreamTask(&encoder, pTask);
9,961✔
656
  tEncoderClear(&encoder);
9,960✔
657

658
  if (code == -1) {
9,960!
659
    stError("s-task:%s vgId:%d task meta encode failed, code:%s", pTask->id.idStr, vgId, tstrerror(code));
×
660
    return TSDB_CODE_INVALID_MSG;
×
661
  }
662

663
  int64_t id[2] = {pTask->id.streamId, pTask->id.taskId};
9,960✔
664

665
  code = tdbTbUpsert(pMeta->pTaskDb, id, STREAM_TASK_KEY_LEN, buf, len, pMeta->txn);
9,960✔
666
  if (code != TSDB_CODE_SUCCESS) {
9,961!
667
    code = terrno;
×
668
    stError("s-task:%s vgId:%d refId:%" PRId64 " task meta save to disk failed, remove ref, code:%s", pTask->id.idStr,
×
669
            vgId, pTask->id.refId, tstrerror(code));
670

671
    int64_t refId = pTask->id.refId;
×
672
    int32_t ret = taosRemoveRef(streamTaskRefPool, pTask->id.refId);
×
673
    if (ret != 0) {
×
674
      stError("s-task:0x%x failed to remove ref, refId:%"PRId64, (int32_t) id[1], refId);
×
675
    }
676
  } else {
677
    stDebug("s-task:%s vgId:%d refId:%" PRId64 " task meta save to disk", pTask->id.idStr, vgId, pTask->id.refId);
9,961✔
678
  }
679

680
  taosMemoryFree(buf);
9,961✔
681
  return code;
9,962✔
682
}
683

684
int32_t streamMetaRemoveTask(SStreamMeta* pMeta, STaskId* pTaskId) {
6,019✔
685
  int64_t key[2] = {pTaskId->streamId, pTaskId->taskId};
6,019✔
686
  int32_t code = tdbTbDelete(pMeta->pTaskDb, key, STREAM_TASK_KEY_LEN, pMeta->txn);
6,019✔
687
  if (code != 0) {
6,019!
688
    stError("vgId:%d failed to remove task:0x%x from metastore, code:%s", pMeta->vgId, (int32_t)pTaskId->taskId,
×
689
            tstrerror(terrno));
690
  } else {
691
    stDebug("vgId:%d remove task:0x%x from metastore", pMeta->vgId, (int32_t)pTaskId->taskId);
6,019✔
692
  }
693

694
  return code;
6,019✔
695
}
696

697
// add to the ready tasks hash map, not the restored tasks hash map
698
int32_t streamMetaRegisterTask(SStreamMeta* pMeta, int64_t ver, SStreamTask* pTask, bool* pAdded) {
7,698✔
699
  *pAdded = false;
7,698✔
700

701
  int32_t code = 0;
7,698✔
702
  int64_t refId = 0;
7,698✔
703
  STaskId id = streamTaskGetTaskId(pTask);
7,698✔
704
  void*   p = taosHashGet(pMeta->pTasksMap, &id, sizeof(id));
7,697✔
705

706
  if (p != NULL) {
7,698✔
707
    stDebug("s-task:%" PRIx64 " already exist in meta, no need to register", id.taskId);
10!
708
    tFreeStreamTask(pTask);
10✔
709
    return code;
10✔
710
  }
711

712
  if ((code = pMeta->buildTaskFn(pMeta->ahandle, pTask, ver)) != 0) {
7,688!
713
    tFreeStreamTask(pTask);
×
714
    return code;
×
715
  }
716

717
  p = taosArrayPush(pMeta->pTaskList, &pTask->id);
7,688✔
718
  if (p == NULL) {
7,688!
719
    stError("s-task:0x%" PRIx64 " failed to register task into meta-list, code: out of memory", id.taskId);
×
720
    tFreeStreamTask(pTask);
×
721
    return terrno;
×
722
  }
723

724
  pTask->id.refId = refId = taosAddRef(streamTaskRefPool, pTask);
7,688✔
725
  code = taosHashPut(pMeta->pTasksMap, &id, sizeof(id), &pTask->id.refId, sizeof(int64_t));
7,688✔
726
  if (code) {  // todo remove it from task list
7,688!
727
    stError("s-task:0x%" PRIx64 " failed to register task into meta-list, code: out of memory", id.taskId);
×
728

729
    int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
×
730
    if (ret != 0) {
×
731
      stError("s-task:0x%x failed to remove ref, refId:%"PRId64, (int32_t) id.taskId, refId);
×
732
    }
733
    return code;
×
734
  }
735

736
  if ((code = streamMetaSaveTask(pMeta, pTask)) != 0) {
7,688!
737
    int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
×
738
    if (ret) {
×
739
      stError("vgId:%d remove task refId failed, refId:%" PRId64, pMeta->vgId, refId);
×
740
    }
741
    return code;
×
742
  }
743

744
  if ((code = streamMetaCommit(pMeta)) != 0) {
7,688!
745
    int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
×
746
    if (ret) {
×
747
      stError("vgId:%d remove task refId failed, refId:%" PRId64, pMeta->vgId, refId);
×
748
    }
749

750
    return code;
×
751
  }
752

753
  if (pTask->info.fillHistory == 0) {
7,688✔
754
    int32_t val = atomic_add_fetch_32(&pMeta->numOfStreamTasks, 1);
4,926✔
755
  }
756

757
  *pAdded = true;
7,688✔
758
  return code;
7,688✔
759
}
760

761
int32_t streamMetaGetNumOfTasks(SStreamMeta* pMeta) {
2,481,256✔
762
  int32_t size = (int32_t)taosHashGetSize(pMeta->pTasksMap);
2,481,256✔
763
  int32_t sizeInList = taosArrayGetSize(pMeta->pTaskList);
2,481,400✔
764
  if (sizeInList != size) {
2,481,424!
765
    stError("vgId:%d tasks number not consistent in list:%d and map:%d, ", pMeta->vgId, sizeInList, size);
×
766
  }
767

768
  return size;
2,481,398✔
769
}
770

771
int32_t streamMetaAcquireTaskNoLock(SStreamMeta* pMeta, int64_t streamId, int32_t taskId, SStreamTask** pTask) {
315,718✔
772
  QRY_PARAM_CHECK(pTask);
315,718!
773
  STaskId  id = {.streamId = streamId, .taskId = taskId};
315,718✔
774
  int64_t* pTaskRefId = (int64_t*)taosHashGet(pMeta->pTasksMap, &id, sizeof(id));
315,718✔
775
  if (pTaskRefId == NULL) {
315,708✔
776
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
1,167✔
777
  }
778

779
  SStreamTask* p = taosAcquireRef(streamTaskRefPool, *pTaskRefId);
314,541✔
780
  if (p == NULL) {
314,560✔
781
    stDebug("s-task:%x failed to acquire task refId:%"PRId64", may have been destoried", taskId, *pTaskRefId);
2!
782
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
783
  }
784

785
  if (p->id.refId != *pTaskRefId) {
314,558!
786
    stFatal("s-task:%x inconsistent refId, task refId:%" PRId64 " try acquire:%" PRId64, taskId, *pTaskRefId,
×
787
            p->id.refId);
788
    int32_t ret = taosReleaseRef(streamTaskRefPool, *pTaskRefId);
×
789
    if (ret) {
×
790
      stError("s-task:0x%x failed to release task refId:%" PRId64, taskId, *pTaskRefId);
×
791
    }
792

793
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
794
  }
795

796
  if (streamTaskShouldStop(p)) {
314,558✔
797
    stDebug("s-task:%s is stopped, failed to acquire it now", p->id.idStr);
23✔
798
    int32_t ret = taosReleaseRef(streamTaskRefPool, *pTaskRefId);
23✔
799
    if (ret) {
23!
800
      stError("s-task:0x%x failed to release task refId:%" PRId64, taskId, *pTaskRefId);
×
801
    }
802
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
23✔
803
  }
804

805
  stDebug("s-task:%s acquire task, refId:%" PRId64, p->id.idStr, p->id.refId);
314,530✔
806
  *pTask = p;
314,541✔
807
  return TSDB_CODE_SUCCESS;
314,541✔
808
}
809

810
int32_t streamMetaAcquireTaskUnsafe(SStreamMeta* pMeta, STaskId* pId, SStreamTask** pTask) {
54,363✔
811
  QRY_PARAM_CHECK(pTask);
54,363!
812
  int64_t* pTaskRefId = (int64_t*)taosHashGet(pMeta->pTasksMap, pId, sizeof(*pId));
54,363✔
813

814
  if (pTaskRefId == NULL) {
54,361✔
815
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
1,172✔
816
  }
817

818
  SStreamTask* p = taosAcquireRef(streamTaskRefPool, *pTaskRefId);
53,189✔
819
  if (p == NULL) {
53,191!
820
    stDebug("s-task:%" PRIx64 " failed to acquire task refId:%" PRId64 ", may have been destoried", pId->taskId,
×
821
            *pTaskRefId);
822
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
823
  }
824

825
  if (p->id.refId != *pTaskRefId) {
53,191!
826
    stFatal("s-task:%" PRIx64 " inconsistent refId, task refId:%" PRId64 " try acquire:%" PRId64, pId->taskId,
×
827
            *pTaskRefId, p->id.refId);
828
    int32_t ret = taosReleaseRef(streamTaskRefPool, *pTaskRefId);
×
829
    if (ret) {
×
830
      stError("s-task:0x%" PRIx64 " failed to release task refId:%" PRId64, pId->taskId, *pTaskRefId);
×
831
    }
832

833
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
834
  }
835

836
  stDebug("s-task:%s acquire task, refId:%" PRId64, p->id.idStr, p->id.refId);
53,191✔
837
  *pTask = p;
53,191✔
838
  return TSDB_CODE_SUCCESS;
53,191✔
839
}
840

841
int32_t streamMetaAcquireTask(SStreamMeta* pMeta, int64_t streamId, int32_t taskId, SStreamTask** pTask) {
313,945✔
842
  streamMetaRLock(pMeta);
313,945✔
843
  int32_t code = streamMetaAcquireTaskNoLock(pMeta, streamId, taskId, pTask);
313,955✔
844
  streamMetaRUnLock(pMeta);
313,962✔
845
  return code;
313,961✔
846
}
847

848
void streamMetaReleaseTask(SStreamMeta* UNUSED_PARAM(pMeta), SStreamTask* pTask) {
463,647✔
849
  if (pTask == NULL) {
463,647✔
850
    return;
5✔
851
  }
852

853
  int32_t taskId = pTask->id.taskId;
463,642✔
854
  int64_t refId = pTask->id.refId;
463,642✔
855
  stDebug("s-task:0x%x release task, refId:%" PRId64, taskId, pTask->id.refId);
463,642✔
856
  int32_t ret = taosReleaseRef(streamTaskRefPool, pTask->id.refId);
463,644✔
857
  if (ret) {
463,648!
858
    stError("s-task:0x%x failed to release task refId:%" PRId64, taskId, refId);
×
859
  }
860
}
861

862
static void doRemoveIdFromList(SArray* pTaskList, int32_t num, SStreamTaskId* id) {
6,019✔
863
  bool remove = false;
6,019✔
864
  for (int32_t i = 0; i < num; ++i) {
16,138!
865
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
16,138✔
866
    if (pTaskId->streamId == id->streamId && pTaskId->taskId == id->taskId) {
16,138✔
867
      taosArrayRemove(pTaskList, i);
6,019✔
868
      remove = true;
6,019✔
869
      break;
6,019✔
870
    }
871
  }
872

873
  if (!remove) {
6,019!
874
    stError("s-task:0x%x not in meta task list, internal error", id->taskId);
×
875
  }
876
}
6,019✔
877

878
static int32_t streamTaskSendTransSuccessMsg(SStreamTask* pTask, void* param) {
6,019✔
879
  int32_t code = 0;
6,019✔
880
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
6,019✔
881
    code = streamTaskSendCheckpointSourceRsp(pTask);
2,998✔
882
    if (code) {
2,998!
883
      stError("s-task:%s vgId:%d failed to send checkpoint-source rsp, code:%s", pTask->id.idStr, pTask->pMeta->vgId,
×
884
              tstrerror(code));
885
    }
886
  }
887

888
  // let's kill the query procedure within stream, to end it ASAP.
889
  if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
6,019✔
890
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
3,094✔
891
    if (code != TSDB_CODE_SUCCESS) {
3,094!
892
      stError("s-task:%s failed to kill task related query handle, code:%s", pTask->id.idStr, tstrerror(code));
×
893
    }
894
  }
895
  return code;
6,019✔
896
}
897

898
int32_t streamMetaUnregisterTask(SStreamMeta* pMeta, int64_t streamId, int32_t taskId) {
6,033✔
899
  SStreamTask* pTask = NULL;
6,033✔
900
  int32_t      vgId = pMeta->vgId;
6,033✔
901
  int32_t      code = 0;
6,033✔
902
  STaskId      id = {.streamId = streamId, .taskId = taskId};
6,033✔
903

904
  streamMetaWLock(pMeta);
6,033✔
905

906
  code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
6,033✔
907
  if (code == 0) {
6,033✔
908
    // desc the paused task counter
909
    if (streamTaskShouldPause(pTask)) {
6,019!
910
      int32_t num = atomic_sub_fetch_32(&pMeta->numOfPausedTasks, 1);
×
911
      stInfo("vgId:%d s-task:%s drop stream task. pause task num:%d", vgId, pTask->id.idStr, num);
×
912
    }
913

914
    // handle the dropping event
915
    code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_DROPPING, streamTaskSendTransSuccessMsg, NULL);
6,019✔
916
    if (code) {
6,019!
917
      stError("s-task:0x%" PRIx64 " failed to handle dropping event async, code:%s", id.taskId, tstrerror(code));
×
918
    }
919

920
    stDebug("s-task:0x%x vgId:%d set task status:dropping and start to unregister it", taskId, vgId);
6,019✔
921

922
    // it is a fill-history task, remove the related stream task's id that points to it
923
    if (pTask->info.fillHistory == 0) {
6,019✔
924
      int32_t ret = atomic_sub_fetch_32(&pMeta->numOfStreamTasks, 1);
3,363✔
925
    }
926

927
    code = taosHashRemove(pMeta->pTasksMap, &id, sizeof(id));
6,019✔
928
    doRemoveIdFromList(pMeta->pTaskList, (int32_t)taosArrayGetSize(pMeta->pTaskList), &pTask->id);
6,019✔
929
    code = streamMetaRemoveTask(pMeta, &id);
6,019✔
930
    if (code) {
6,019!
931
      stError("vgId:%d failed to remove task:0x%" PRIx64 ", code:%s", pMeta->vgId, id.taskId, tstrerror(code));
×
932
    }
933

934
    int32_t size = (int32_t)taosHashGetSize(pMeta->pTasksMap);
6,019✔
935
    int32_t sizeInList = taosArrayGetSize(pMeta->pTaskList);
6,019✔
936
    if (sizeInList != size) {
6,019!
937
      stError("vgId:%d tasks number not consistent in list:%d and map:%d, ", vgId, sizeInList, size);
×
938
    }
939

940
    if (pTask->info.delaySchedParam != 0 && pTask->info.fillHistory == 0) {
6,019✔
941
      stDebug("s-task:%s stop schedTimer", pTask->id.idStr);
804!
942
      streamTmrStop(pTask->schedInfo.pDelayTimer);
804✔
943
      pTask->info.delaySchedParam = 0;
804✔
944
    }
945

946

947
    int64_t refId = pTask->id.refId;
6,019✔
948
    int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
6,019✔
949
    if (ret != 0) {
6,019!
950
      stError("s-task:0x%x failed to remove ref, refId:%"PRId64, (int32_t) id.taskId, refId);
×
951
    }
952

953
    streamMetaReleaseTask(pMeta, pTask);
6,019✔
954
    streamMetaWUnLock(pMeta);
6,019✔
955
  } else {
956
    stDebug("vgId:%d failed to find the task:0x%x, it may have been dropped already", vgId, taskId);
14!
957
    streamMetaWUnLock(pMeta);
14✔
958
  }
959

960
  return 0;
6,033✔
961
}
962

963
int32_t streamMetaBegin(SStreamMeta* pMeta) {
12,280✔
964
  streamMetaWLock(pMeta);
12,280✔
965
  int32_t code = tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
12,282✔
966
                          TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
967
  if (code) {
12,281!
968
    streamSetFatalError(pMeta, code, __func__, __LINE__);
×
969
  }
970
  streamMetaWUnLock(pMeta);
12,281✔
971
  return code;
12,279✔
972
}
973

974
int32_t streamMetaCommit(SStreamMeta* pMeta) {
25,631✔
975
  int32_t code = tdbCommit(pMeta->db, pMeta->txn);
25,631✔
976
  if (code != 0) {
25,631!
977
    streamSetFatalError(pMeta, code, __func__, __LINE__);
×
978
    stFatal("vgId:%d failed to commit stream meta, code:%s, line:%d", pMeta->vgId, tstrerror(code),
×
979
            pMeta->fatalInfo.line);
980
  }
981

982
  code = tdbPostCommit(pMeta->db, pMeta->txn);
25,631✔
983
  if (code != 0) {
25,632!
984
    streamSetFatalError(pMeta, code, __func__, __LINE__);
×
985
    stFatal("vgId:%d failed to do post-commit stream meta, code:%s, line:%d", pMeta->vgId, tstrerror(code),
×
986
            pMeta->fatalInfo.line);
987
    return code;
×
988
  }
989

990
  code = tdbBegin(pMeta->db, &pMeta->txn, tdbDefaultMalloc, tdbDefaultFree, NULL,
25,632✔
991
                  TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
992
  if (code != 0) {
25,632!
993
    streamSetFatalError(pMeta, code, __func__, __LINE__);
×
994
    stFatal("vgId:%d failed to begin trans, code:%s, line:%d", pMeta->vgId, tstrerror(code), pMeta->fatalInfo.line);
×
995
  } else {
996
    stDebug("vgId:%d stream meta file commit completed", pMeta->vgId);
25,632✔
997
  }
998

999
  return code;
25,632✔
1000
}
1001

1002
int64_t streamMetaGetLatestCheckpointId(SStreamMeta* pMeta) {
69✔
1003
  int64_t checkpointId = 0;
69✔
1004
  int32_t code = 0;
69✔
1005

1006
  TBC* pCur = NULL;
69✔
1007
  if (tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL) < 0) {
69!
1008
    stError("failed to open stream meta file, the latest checkpointId is 0, vgId:%d", pMeta->vgId);
×
1009
    return checkpointId;
×
1010
  }
1011

1012
  void*    pKey = NULL;
70✔
1013
  int32_t  kLen = 0;
70✔
1014
  void*    pVal = NULL;
70✔
1015
  int32_t  vLen = 0;
70✔
1016
  SDecoder decoder;
1017

1018
  code = tdbTbcMoveToFirst(pCur);
70✔
1019
  if (code) {
69!
1020
    stError("failed to move stream meta file cursor, the latest checkpointId is 0, vgId:%d", pMeta->vgId);
×
1021
    tdbTbcClose(pCur);
×
1022
    return checkpointId;
×
1023
  }
1024

1025
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
172✔
1026
    if (pVal == NULL || vLen == 0) {
103!
1027
      break;
1028
    }
1029
    SCheckpointInfo info;
1030
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
103✔
1031
    if (tDecodeStreamTaskChkInfo(&decoder, &info) < 0) {
103!
1032
      continue;
×
1033
    }
1034
    tDecoderClear(&decoder);
103✔
1035

1036
    checkpointId = TMAX(checkpointId, info.checkpointId);
103✔
1037
  }
1038

1039
  stDebug("vgId:%d get max checkpointId:%" PRId64, pMeta->vgId, checkpointId);
70✔
1040

1041
  tdbFree(pKey);
70✔
1042
  tdbFree(pVal);
70✔
1043

1044
  tdbTbcClose(pCur);
70✔
1045
  return checkpointId;
70✔
1046
}
1047

1048
// not allowed to return error code
1049
void streamMetaLoadAllTasks(SStreamMeta* pMeta) {
12,287✔
1050
  TBC*     pCur = NULL;
12,287✔
1051
  void*    pKey = NULL;
12,287✔
1052
  int32_t  kLen = 0;
12,287✔
1053
  void*    pVal = NULL;
12,287✔
1054
  int32_t  vLen = 0;
12,287✔
1055
  SDecoder decoder;
1056
  int32_t  vgId = 0;
12,287✔
1057
  int32_t  code = 0;
12,287✔
1058
  SArray*  pRecycleList = NULL;
12,287✔
1059

1060
  if (pMeta == NULL) {
12,287!
1061
    return;
×
1062
  }
1063

1064
  vgId = pMeta->vgId;
12,287✔
1065
  pRecycleList = taosArrayInit(4, sizeof(STaskId));
12,287✔
1066
  if (pRecycleList == NULL) {
12,287!
1067
    stError("vgId:%d failed prepare load all tasks, code:out of memory", vgId);
×
1068
    return;
×
1069
  }
1070

1071
  stInfo("vgId:%d load stream tasks from meta files", vgId);
12,287!
1072

1073
  code = tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL);
12,287✔
1074
  if (code != TSDB_CODE_SUCCESS) {
12,287!
1075
    stError("vgId:%d failed to open stream meta, code:%s, not load any stream tasks", vgId, tstrerror(terrno));
×
1076
    taosArrayDestroy(pRecycleList);
×
1077
    return;
×
1078
  }
1079

1080
  code = tdbTbcMoveToFirst(pCur);
12,287✔
1081
  if (code) {
12,287!
1082
    stError("vgId:%d failed to open stream meta cursor, code:%s, not load any stream tasks", vgId, tstrerror(terrno));
×
1083
    taosArrayDestroy(pRecycleList);
×
1084
    tdbTbcClose(pCur);
×
1085
    return;
×
1086
  }
1087

1088
  while (tdbTbcNext(pCur, &pKey, &kLen, &pVal, &vLen) == 0) {
12,369✔
1089
    if (pVal == NULL || vLen == 0) {
82!
1090
      break;
1091
    }
1092

1093
    SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
82✔
1094
    if (pTask == NULL) {
82!
1095
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
1096
      stError("vgId:%d failed to load stream task from meta-files, code:%s", vgId, tstrerror(terrno));
×
1097
      break;
×
1098
    }
1099

1100
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
82✔
1101
    if (tDecodeStreamTask(&decoder, pTask) < 0) {
82!
1102
      tDecoderClear(&decoder);
×
1103
      tFreeStreamTask(pTask);
×
1104
      stError(
×
1105
          "vgId:%d stream read incompatible data, rm %s/vnode/vnode*/tq/stream if taosd cannot start, and rebuild "
1106
          "stream manually",
1107
          vgId, tsDataDir);
1108
      break;
×
1109
    }
1110
    tDecoderClear(&decoder);
82✔
1111

1112
    if (pTask->status.taskStatus == TASK_STATUS__DROPPING) {
82!
1113
      int32_t taskId = pTask->id.taskId;
×
1114
      STaskId id = streamTaskGetTaskId(pTask);
×
1115

1116
      tFreeStreamTask(pTask);
×
1117
      void*   px = taosArrayPush(pRecycleList, &id);
×
1118
      if (px == NULL) {
×
1119
        stError("s-task:0x%x failed record the task into recycle list due to out of memory", taskId);
×
1120
      }
1121

1122
      int32_t total = taosArrayGetSize(pRecycleList);
×
1123
      stDebug("s-task:0x%x is already dropped, add into recycle list, total:%d", taskId, total);
×
1124
      continue;
×
1125
    }
1126

1127
    stDebug("s-task:0x%" PRIx64 "-0x%x vgId:%d loaded from meta file, checkpointId:%" PRId64 " checkpointVer:%" PRId64,
82✔
1128
            pTask->id.streamId, pTask->id.taskId, vgId, pTask->chkInfo.checkpointId, pTask->chkInfo.checkpointVer);
1129

1130
    // do duplicate task check.
1131
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
82✔
1132
    void*   p = taosHashGet(pMeta->pTasksMap, &id, sizeof(id));
82✔
1133
    if (p == NULL) {
82!
1134
      code = pMeta->buildTaskFn(pMeta->ahandle, pTask, pTask->chkInfo.checkpointVer + 1);
82✔
1135
      if (code < 0) {
82!
1136
        stError("failed to load s-task:0x%" PRIx64 ", code:%s, continue", id.taskId, tstrerror(terrno));
×
1137
        tFreeStreamTask(pTask);
×
1138
        continue;
×
1139
      }
1140

1141
      void* px = taosArrayPush(pMeta->pTaskList, &pTask->id);
82✔
1142
      if (px == NULL) {
82!
1143
        stFatal("s-task:0x%x failed to add into task list due to out of memory", pTask->id.taskId);
×
1144
      }
1145
    } else {
1146
      // todo this should replace the existed object put by replay creating stream task msg from mnode
1147
      stError("s-task:0x%x already added into table meta by replaying WAL, need check", pTask->id.taskId);
×
1148
      tFreeStreamTask(pTask);
×
1149
      continue;
×
1150
    }
1151

1152
    pTask->id.refId = taosAddRef(streamTaskRefPool, pTask);
82✔
1153

1154
    if (taosHashPut(pMeta->pTasksMap, &id, sizeof(id), &pTask->id.refId, sizeof(int64_t)) != 0) {
82!
1155
      int64_t refId = pTask->id.refId;
×
1156
      stError("s-task:0x%x failed to put into hashTable, code:%s, remove task ref, refId:%" PRId64 " continue",
×
1157
              pTask->id.taskId, tstrerror(terrno), refId);
1158

1159
      void*   px = taosArrayPop(pMeta->pTaskList);
×
1160
      int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
×
1161
      if (ret != 0) {
×
1162
        stError("s-task:0x%x failed to remove ref, refId:%" PRId64, (int32_t)id.taskId, refId);
×
1163
      }
1164
      continue;
×
1165
    }
1166

1167
    stInfo("s-task:0x%x vgId:%d set refId:%"PRId64, (int32_t) id.taskId, vgId, pTask->id.refId);
82!
1168
    if (pTask->info.fillHistory == 0) {
82✔
1169
      int32_t val = atomic_add_fetch_32(&pMeta->numOfStreamTasks, 1);
73✔
1170
    }
1171

1172
    if (streamTaskShouldPause(pTask)) {
82!
1173
      int32_t val = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
×
1174
    }
1175
  }
1176

1177
  tdbFree(pKey);
12,286✔
1178
  tdbFree(pVal);
12,286✔
1179

1180
  tdbTbcClose(pCur);
12,286✔
1181

1182
  if (taosArrayGetSize(pRecycleList) > 0) {
12,287!
1183
    for (int32_t i = 0; i < taosArrayGetSize(pRecycleList); ++i) {
×
1184
      STaskId* pId = taosArrayGet(pRecycleList, i);
×
1185
      code = streamMetaRemoveTask(pMeta, pId);
×
1186
      if (code) {
×
1187
        stError("s-task:0x%" PRIx64 " failed to remove task, code:%s", pId->taskId, tstrerror(code));
×
1188
      }
1189
    }
1190
  }
1191

1192
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
12,287✔
1193
  stDebug("vgId:%d load %d tasks into meta from disk completed, streamTask:%d, paused:%d", pMeta->vgId, numOfTasks,
12,286✔
1194
          pMeta->numOfStreamTasks, pMeta->numOfPausedTasks);
1195

1196
  taosArrayDestroy(pRecycleList);
12,286✔
1197
  code = streamMetaCommit(pMeta);
12,287✔
1198
  if (code) {
12,287!
1199
    stError("vgId:%d failed to commit, code:%s", pMeta->vgId, tstrerror(code));
×
1200
  }
1201
}
1202

1203
void streamMetaNotifyClose(SStreamMeta* pMeta) {
12,282✔
1204
  int32_t vgId = pMeta->vgId;
12,282✔
1205
  int64_t startTs = 0;
12,282✔
1206
  int32_t sendCount = 0;
12,282✔
1207

1208
  streamMetaGetHbSendInfo(pMeta->pHbInfo, &startTs, &sendCount);
12,282✔
1209
  stInfo("vgId:%d notify all stream tasks that current vnode is closing. isLeader:%d startHb:%" PRId64 ", totalHb:%d",
12,280!
1210
         vgId, (pMeta->role == NODE_ROLE_LEADER), startTs, sendCount);
1211

1212
  // wait for the stream meta hb function stopping
1213
  streamMetaWaitForHbTmrQuit(pMeta);
12,282✔
1214
  pMeta->closeFlag = true;
12,279✔
1215

1216
  stDebug("vgId:%d start to check all tasks for closing", vgId);
12,279✔
1217
  int64_t st = taosGetTimestampMs();
12,280✔
1218

1219
  streamMetaRLock(pMeta);
12,280✔
1220

1221
  SArray* pTaskList = NULL;
12,282✔
1222
  int32_t code = streamMetaSendMsgBeforeCloseTasks(pMeta, &pTaskList);
12,282✔
1223
  if (code != TSDB_CODE_SUCCESS) {
1224
  }
1225

1226
  int32_t numOfTasks = taosArrayGetSize(pTaskList);
12,281✔
1227
  for (int32_t i = 0; i < numOfTasks; ++i) {
14,026✔
1228
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
1,746✔
1229
    SStreamTask*   pTask = NULL;
1,746✔
1230

1231
    code = streamMetaAcquireTaskNoLock(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
1,746✔
1232
    if (code != TSDB_CODE_SUCCESS) {
1,746✔
1233
      continue;
6✔
1234
    }
1235

1236
    int64_t refId = pTask->id.refId;
1,740✔
1237
    int32_t ret = streamTaskStop(pTask);
1,740✔
1238
    if (ret) {
1,740!
1239
      stError("s-task:0x%x failed to stop task, code:%s", pTaskId->taskId, tstrerror(ret));
×
1240
    }
1241

1242
    streamMetaReleaseTask(pMeta, pTask);
1,740✔
1243
    ret = taosRemoveRef(streamTaskRefPool, refId);
1,740✔
1244
    if (ret) {
1,740!
1245
      stError("vgId:%d failed to remove task:0x%x, refId:%" PRId64, pMeta->vgId, pTaskId->taskId, refId);
×
1246
    }
1247
  }
1248

1249
  taosArrayDestroy(pTaskList);
12,280✔
1250

1251
  double el = (taosGetTimestampMs() - st) / 1000.0;
12,280✔
1252
  stDebug("vgId:%d stop all %d task(s) completed, elapsed time:%.2f Sec.", pMeta->vgId, numOfTasks, el);
12,280✔
1253
  streamMetaRUnLock(pMeta);
12,282✔
1254
}
12,280✔
1255

1256
void streamMetaStartHb(SStreamMeta* pMeta) {
10,198✔
1257
  int64_t* pRid = taosMemoryMalloc(sizeof(int64_t));
10,198✔
1258
  if (pRid == NULL) {
10,198!
1259
    stFatal("vgId:%d failed to prepare the metaHb to mnode, hbMsg will not started, code: out of memory", pMeta->vgId);
×
1260
    return;
×
1261
  }
1262

1263
  *pRid = pMeta->rid;
10,198✔
1264
  int32_t code = metaRefMgtAdd(pMeta->vgId, pRid);
10,198✔
1265
  if (code) {
10,198!
1266
    return;
×
1267
  }
1268

1269
  streamMetaHbToMnode(pRid, NULL);
10,198✔
1270
}
1271

1272
int32_t streamMetaSendMsgBeforeCloseTasks(SStreamMeta* pMeta, SArray** pList) {
12,292✔
1273
  QRY_PARAM_CHECK(pList);
12,292!
1274

1275
  int32_t code = 0;
12,292✔
1276
  SArray* pTaskList = taosArrayDup(pMeta->pTaskList, NULL);
12,292✔
1277
  if (pTaskList == NULL) {
12,294!
1278
    stError("failed to generate the task list during send hbMsg to mnode, vgId:%d, code: out of memory", pMeta->vgId);
×
1279
    return terrno;
×
1280
  }
1281

1282
  *pList = pTaskList;
12,294✔
1283

1284
  bool sendMsg = pMeta->sendMsgBeforeClosing;
12,294✔
1285
  if (!sendMsg) {
12,294✔
1286
    stDebug("vgId:%d no need to send msg to mnode before closing tasks", pMeta->vgId);
12,289✔
1287
    return TSDB_CODE_SUCCESS;
12,289✔
1288
  }
1289

1290
  stDebug("vgId:%d send msg to mnode before closing all tasks", pMeta->vgId);
5!
1291

1292
  // send hb msg to mnode before closing all tasks.
1293
  int32_t numOfTasks = taosArrayGetSize(pTaskList);
5✔
1294
  for (int32_t i = 0; i < numOfTasks; ++i) {
5!
1295
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
×
1296
    SStreamTask*   pTask = NULL;
×
1297

1298
    code = streamMetaAcquireTaskNoLock(pMeta, pTaskId->streamId, pTaskId->taskId, &pTask);
×
1299
    if (code != TSDB_CODE_SUCCESS) {  // this error is ignored
×
1300
      continue;
×
1301
    }
1302

1303
    streamTaskSetCheckpointFailed(pTask);
×
1304
    streamMetaReleaseTask(pMeta, pTask);
×
1305
  }
1306

1307
  code = streamMetaSendHbHelper(pMeta);
5✔
1308
  pMeta->sendMsgBeforeClosing = false;
5✔
1309
  return TSDB_CODE_SUCCESS;  // always return true
5✔
1310
}
1311

1312
void streamMetaUpdateStageRole(SStreamMeta* pMeta, int64_t stage, bool isLeader) {
14,963✔
1313
  streamMetaWLock(pMeta);
14,963✔
1314

1315
  int64_t prevStage = pMeta->stage;
14,967✔
1316
  pMeta->stage = stage;
14,967✔
1317

1318
  // mark the sign to send msg before close all tasks
1319
  // 1. for leader vnode, always send msg before closing
1320
  // 2. for follower vnode, if it's is changed from leader, also sending msg before closing.
1321
  if (pMeta->role == NODE_ROLE_LEADER) {
14,967✔
1322
    pMeta->sendMsgBeforeClosing = true;
5✔
1323
  }
1324

1325
  pMeta->role = (isLeader) ? NODE_ROLE_LEADER : NODE_ROLE_FOLLOWER;
14,967✔
1326
  streamMetaWUnLock(pMeta);
14,967✔
1327

1328
  if (isLeader) {
14,964✔
1329
    stInfo("vgId:%d update meta stage:%" PRId64 ", prev:%" PRId64 " leader:%d, start to send Hb, rid:%" PRId64,
10,198!
1330
           pMeta->vgId, prevStage, stage, isLeader, pMeta->rid);
1331
    streamMetaStartHb(pMeta);
10,198✔
1332
  } else {
1333
    stInfo("vgId:%d update meta stage:%" PRId64 " prev:%" PRId64 " leader:%d sendMsg beforeClosing:%d", pMeta->vgId,
4,766!
1334
           prevStage, stage, isLeader, pMeta->sendMsgBeforeClosing);
1335
  }
1336
}
14,965✔
1337

1338
bool streamMetaAllTasksReady(const SStreamMeta* pMeta) {
1,032✔
1339
  int32_t num = taosArrayGetSize(pMeta->pTaskList);
1,032✔
1340
  for (int32_t i = 0; i < num; ++i) {
3,128✔
1341
    SStreamTaskId* pId = taosArrayGet(pMeta->pTaskList, i);
2,096✔
1342
    STaskId        id = {.streamId = pId->streamId, .taskId = pId->taskId};
2,096✔
1343
    SStreamTask*   pTask = NULL;
2,096✔
1344
    int32_t        code = streamMetaAcquireTaskUnsafe((SStreamMeta*)pMeta, &id, &pTask);
2,096✔
1345

1346
    if (code == 0) {
2,096!
1347
      if (pTask->status.downstreamReady == 0) {
2,096!
UNCOV
1348
        streamMetaReleaseTask((SStreamMeta*)pMeta, pTask);
×
UNCOV
1349
        return false;
×
1350
      }
1351
      streamMetaReleaseTask((SStreamMeta*)pMeta, pTask);
2,096✔
1352
    }
1353
  }
1354

1355
  return true;
1,032✔
1356
}
1357

1358
int32_t streamMetaResetTaskStatus(SStreamMeta* pMeta) {
29✔
1359
  int32_t numOfTasks = taosArrayGetSize(pMeta->pTaskList);
29✔
1360

1361
  stDebug("vgId:%d reset all %d stream task(s) status to be uninit", pMeta->vgId, numOfTasks);
29✔
1362
  if (numOfTasks == 0) {
29!
1363
    return TSDB_CODE_SUCCESS;
×
1364
  }
1365

1366
  for (int32_t i = 0; i < numOfTasks; ++i) {
85✔
1367
    SStreamTaskId* pTaskId = taosArrayGet(pMeta->pTaskList, i);
56✔
1368
    STaskId        id = {.streamId = pTaskId->streamId, .taskId = pTaskId->taskId};
56✔
1369
    SStreamTask*   pTask = NULL;
56✔
1370
    int32_t        code = streamMetaAcquireTaskUnsafe(pMeta, &id, &pTask);
56✔
1371
    if (code == 0) {
56!
1372
      streamTaskResetStatus(pTask);
56✔
1373
      streamMetaReleaseTask(pMeta, pTask);
56✔
1374
    }
1375
  }
1376

1377
  return 0;
29✔
1378
}
1379

1380
void streamMetaAddIntoUpdateTaskList(SStreamMeta* pMeta, SStreamTask* pTask, SStreamTask* pHTask, int32_t transId,
5✔
1381
                                     int64_t startTs) {
1382
  const char* id = pTask->id.idStr;
5✔
1383
  int32_t     vgId = pMeta->vgId;
5✔
1384
  int32_t     code = 0;
5✔
1385

1386
  // keep the already updated info
1387
  STaskUpdateEntry entry = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .transId = transId};
5✔
1388
  code = taosHashPut(pMeta->updateInfo.pTasks, &entry, sizeof(entry), NULL, 0);
5✔
1389
  if (code != 0) {
5!
1390
    stError("s-task:%s failed to put updateTask into update list", id);
×
1391
  }
1392

1393
  int64_t el = taosGetTimestampMs() - startTs;
5✔
1394
  if (pHTask != NULL) {
5!
1395
    STaskUpdateEntry hEntry = {.streamId = pHTask->id.streamId, .taskId = pHTask->id.taskId, .transId = transId};
×
1396
    code = taosHashPut(pMeta->updateInfo.pTasks, &hEntry, sizeof(hEntry), NULL, 0);
×
1397
    if (code != 0) {
×
1398
      stError("s-task:%s failed to put updateTask into update list", id);
×
1399
    } else {
1400
      stDebug("s-task:%s vgId:%d transId:%d task nodeEp update completed, streamTask/hTask closed, elapsed:%" PRId64
×
1401
              " ms",
1402
              id, vgId, transId, el);
1403
    }
1404
  } else {
1405
    stDebug("s-task:%s vgId:%d transId:%d task nodeEp update completed, streamTask closed, elapsed time:%" PRId64 "ms",
5!
1406
            id, vgId, transId, el);
1407
  }
1408
}
5✔
1409

1410
void streamMetaClearSetUpdateTaskListComplete(SStreamMeta* pMeta) {
3✔
1411
  STaskUpdateInfo* pInfo = &pMeta->updateInfo;
3✔
1412

1413
  taosHashClear(pInfo->pTasks);
3✔
1414

1415
  int32_t prev = pInfo->completeTransId;
3✔
1416
  pInfo->completeTransId = pInfo->activeTransId;
3✔
1417
  pInfo->activeTransId = -1;
3✔
1418
  pInfo->completeTs = taosGetTimestampMs();
3✔
1419

1420
  stDebug("vgId:%d set the nodeEp update complete, ts:%" PRId64 ", complete transId:%d->%d, reset active transId",
3!
1421
          pMeta->vgId, pInfo->completeTs, prev, pInfo->completeTransId);
1422
}
3✔
1423

1424
bool streamMetaInitUpdateTaskList(SStreamMeta* pMeta, int32_t transId) {
5✔
1425
  STaskUpdateInfo* pInfo = &pMeta->updateInfo;
5✔
1426

1427
  if (transId > pInfo->completeTransId) {
5!
1428
    if (pInfo->activeTransId == -1) {
5✔
1429
      taosHashClear(pInfo->pTasks);
3✔
1430
      pInfo->activeTransId = transId;
3✔
1431

1432
      stInfo("vgId:%d set the active epset update transId:%d, prev complete transId:%d", pMeta->vgId, transId,
3!
1433
             pInfo->completeTransId);
1434
      return true;
3✔
1435
    } else {
1436
      if (pInfo->activeTransId == transId) {
2!
1437
        // do nothing
1438
        return true;
2✔
1439
      } else if (transId < pInfo->activeTransId) {
×
1440
        stError("vgId:%d invalid(out of order)epset update transId:%d, active transId:%d, complete transId:%d, discard",
×
1441
                pMeta->vgId, transId, pInfo->activeTransId, pInfo->completeTransId);
1442
        return false;
×
1443
      } else {  // transId > pInfo->activeTransId
1444
        taosHashClear(pInfo->pTasks);
×
1445
        int32_t prev = pInfo->activeTransId;
×
1446
        pInfo->activeTransId = transId;
×
1447

1448
        stInfo("vgId:%d active epset update transId updated from:%d to %d, prev complete transId:%d", pMeta->vgId,
×
1449
               transId, prev, pInfo->completeTransId);
1450
        return true;
×
1451
      }
1452
    }
1453
  } else if (transId == pInfo->completeTransId) {
×
1454
    stError("vgId:%d already handled epset update transId:%d, completeTs:%" PRId64 " ignore", pMeta->vgId, transId,
×
1455
            pInfo->completeTs);
1456
    return false;
×
1457
  } else {  // pInfo->completeTransId > transId
1458
    stError("vgId:%d disorder update nodeEp msg recv, prev completed epset update transId:%d, recv:%d, discard",
×
1459
            pMeta->vgId, pInfo->activeTransId, transId);
1460
    return false;
×
1461
  }
1462
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc