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

taosdata / TDengine / #3542

27 Nov 2024 02:52AM UTC coverage: 60.819% (+0.04%) from 60.776%
#3542

push

travis-ci

web-flow
Merge pull request #28931 from taosdata/enh/jdbc-demo-3.0

update jdbc demo, and version history

120305 of 252779 branches covered (47.59%)

Branch coverage included in aggregate %.

201010 of 275538 relevant lines covered (72.95%)

19989893.51 hits per line

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

59.77
/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,431✔
46
  streamBackendId = taosOpenRef(64, streamBackendCleanup);
2,431✔
47
  streamBackendCfWrapperId = taosOpenRef(64, streamBackendHandleCleanup);
2,431✔
48
  taskDbWrapperId = taosOpenRef(64, taskDbDestroy2);
2,431✔
49

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

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

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

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

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

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

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

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

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

99
void metaRefMgtCleanup() {
2,431✔
100
  void* pIter = taosHashIterate(gMetaRefMgt.pTable, NULL);
2,431✔
101
  while (pIter) {
33,660✔
102
    int64_t* p = *(int64_t**) pIter;
31,229✔
103
    taosMemoryFree(p);
31,229✔
104
    pIter = taosHashIterate(gMetaRefMgt.pTable, pIter);
31,229✔
105
  }
106

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

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

115
  streamMutexLock(&gMetaRefMgt.mutex);
65,453✔
116

117
  p = taosHashGet(gMetaRefMgt.pTable, &rid, sizeof(rid));
65,473✔
118
  if (p == NULL) {
65,473!
119
    code = taosHashPut(gMetaRefMgt.pTable, &rid, sizeof(rid), &rid, sizeof(void*));
65,473✔
120
    if (code) {
65,473!
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);
65,473✔
132
  return code;
65,473✔
133
}
134

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

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

143
int32_t streamMetaOpenTdb(SStreamMeta* pMeta) {
13,740✔
144
  if (tdbOpen(pMeta->path, 16 * 1024, 1, &pMeta->db, 0, 0, NULL) < 0) {
13,740!
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) {
13,745!
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) {
13,747!
155
    stError("vgId:%d, open checkpoint.db failed, stream meta open failed", pMeta->vgId);
×
156
    return -1;
×
157
  }
158

159
  return 0;
13,744✔
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) {
13,744✔
172
  int8_t  ret = STREAM_STATA_COMPATIBLE;
13,744✔
173
  TBC*    pCur = NULL;
13,744✔
174
  int32_t code = 0;
13,744✔
175
  void*   pKey = NULL;
13,744✔
176
  int32_t kLen = 0;
13,744✔
177
  void*   pVal = NULL;
13,744✔
178
  int32_t vLen = 0;
13,744✔
179

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

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

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

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

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

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

214
  tdbFree(pKey);
13,743✔
215
  tdbFree(pVal);
13,743✔
216
  tdbTbcClose(pCur);
13,743✔
217
  return ret;
13,747✔
218
}
219

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

225
  terrno = 0;
120✔
226
  bool exist = streamBackendDataIsExist(pMeta->path, chkpId);
120✔
227
  if (exist == false) {
120!
228
    code = terrno;
120✔
229
    return code;
120✔
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) {
13,744✔
267
  int8_t compatible = streamMetaCheckBackendCompatible(pMeta);
13,744✔
268
  if (compatible == STREAM_STATA_COMPATIBLE) {
13,743✔
269
    return 0;
13,626✔
270
  } else if (compatible == STREAM_STATA_NEED_CONVERT) {
117!
271
    stInfo("vgId:%d stream state need covert backend format", pMeta->vgId);
120!
272
    return streamMetaCvtDbFormat(pMeta);
120✔
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) {
7,144✔
286
  int32_t code = 0;
7,144✔
287
  int64_t chkpId = pTask->chkInfo.checkpointId;
7,144✔
288

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

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

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

307
  STaskDbWrapper* pBackend = NULL;
4,670✔
308
  int64_t         processVer = -1;
4,670✔
309
  while (1) {
310
    code = taskDbOpen(pMeta->path, key, chkpId, &processVer, &pBackend);
4,670✔
311
    if (code == 0) {
4,670!
312
      break;
4,670✔
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);
4,670✔
323
  pTask->pBackend = pBackend;
4,670✔
324
  pBackend->refId = tref;
4,670✔
325
  pBackend->pTask = pTask;
4,670✔
326
  pBackend->pMeta = pMeta;
4,670✔
327

328
  if (processVer != -1) {
4,670✔
329
    if (pTask->chkInfo.processedVer != processVer) {
25!
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
25!
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*));
4,670✔
343
  if (code) {
4,669!
344
    stError("s-task:0x%x failed to put taskDb backend, code:out of memory", pTask->id.taskId);
×
345
  }
346
  streamMutexUnlock(&pMeta->backendMutex);
4,669✔
347

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

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

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

362
  streamMutexUnlock(&pMeta->backendMutex);
4,508✔
363
}
364

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

371
  SStreamMeta* pMeta = taosMemoryCalloc(1, sizeof(SStreamMeta));
13,638✔
372
  if (pMeta == NULL) {
13,738!
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;
13,738✔
378
  char*   tpath = taosMemoryCalloc(1, len);
13,738✔
379
  TSDB_CHECK_NULL(tpath, code, lino, _err, terrno);
13,743!
380

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

384
  code = streamMetaOpenTdb(pMeta);
13,743✔
385
  TSDB_CHECK_CODE(code, lino, _err);
13,744!
386

387
  if ((code = streamMetaMayCvtDbFormat(pMeta)) < 0) {
13,744!
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)) {
13,741!
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);
13,744✔
399
  pMeta->pTasksMap = taosHashInit(64, fp, true, HASH_NO_LOCK);
13,744✔
400
  TSDB_CHECK_NULL(pMeta->pTasksMap, code, lino, _err, terrno);
13,744!
401

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

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

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

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

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

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

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

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

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

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

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

448
  code = bkdMgtCreate(tpath, (SBkdMgt**)&pMeta->bkdChkptMgt);
13,747✔
449
  TSDB_CHECK_CODE(code, lino, _err);
13,746!
450

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

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

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

460
  memcpy(pRid, &pMeta->rid, sizeof(pMeta->rid));
13,746✔
461

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

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

467
  TSDB_CHECK_CODE(code, lino, _err);
13,747!
468

469
  *p = pMeta;
13,747✔
470
  return code;
13,747✔
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) {
13,761✔
525
  // remove all existed tasks in this vnode
526
  void* pIter = NULL;
13,761✔
527
  while ((pIter = taosHashIterate(pMeta->pTasksMap, pIter)) != NULL) {
15,983✔
528
    int64_t      refId = *(int64_t*)pIter;
2,222✔
529
    SStreamTask* p = taosAcquireRef(streamTaskRefPool, refId);
2,222✔
530
    if (p == NULL) {
2,222✔
531
      continue;
2,156✔
532
    }
533

534
    // release the ref by timer
535
    if (p->info.delaySchedParam != 0 && p->info.fillHistory == 0) {  // one more ref in timer
66!
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);
66✔
542
    if (code) {
66!
543
      stError("vgId:%d remove task refId failed, refId:%" PRId64, pMeta->vgId, refId);
×
544
    }
545

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

552
  if (pMeta->streamBackendRid != 0) {
13,760!
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);
13,760✔
560

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

565
  pMeta->numOfStreamTasks = 0;
13,761✔
566
  pMeta->numOfPausedTasks = 0;
13,761✔
567

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

574
void streamMetaClose(SStreamMeta* pMeta) {
13,739✔
575
  stDebug("vgId:%d start to close stream meta", pMeta->vgId);
13,739✔
576
  if (pMeta == NULL) {
13,747!
577
    return;
×
578
  }
579
  int32_t code = taosRemoveRef(streamMetaRefPool, pMeta->rid);
13,747✔
580
  if (code) {
13,747!
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) {
13,747✔
586
  SStreamMeta* pMeta = arg;
13,747✔
587
  if (pMeta == NULL) {
13,747!
588
    return;
×
589
  }
590

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

595
  streamMetaWLock(pMeta);
13,747✔
596
  streamMetaClear(pMeta);
13,747✔
597
  streamMetaWUnLock(pMeta);
13,747✔
598

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

605
  taosArrayDestroy(pMeta->pTaskList);
13,746✔
606
  taosArrayDestroy(pMeta->chkpSaved);
13,747✔
607
  taosArrayDestroy(pMeta->chkpInUse);
13,747✔
608

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

613
  streamMetaClearStartInfo(&pMeta->startInfo);
13,747✔
614

615
  destroyMetaHbInfo(pMeta->pHbInfo);
13,747✔
616
  pMeta->pHbInfo = NULL;
13,747✔
617

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

621
  bkdMgtDestroy(pMeta->bkdChkptMgt);
13,747✔
622

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

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

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

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

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

653
  SEncoder encoder = {0};
19,109✔
654
  tEncoderInit(&encoder, buf, len);
19,109✔
655
  code = tEncodeStreamTask(&encoder, pTask);
19,110✔
656
  tEncoderClear(&encoder);
19,113✔
657

658
  if (code == -1) {
19,109!
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};
19,109✔
664

665
  code = tdbTbUpsert(pMeta->pTaskDb, id, STREAM_TASK_KEY_LEN, buf, len, pMeta->txn);
19,109✔
666
  if (code != TSDB_CODE_SUCCESS) {
19,100!
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);
19,100✔
678
  }
679

680
  taosMemoryFree(buf);
19,102✔
681
  return code;
19,105✔
682
}
683

684
int32_t streamMetaRemoveTask(SStreamMeta* pMeta, STaskId* pTaskId) {
11,854✔
685
  int64_t key[2] = {pTaskId->streamId, pTaskId->taskId};
11,854✔
686
  int32_t code = tdbTbDelete(pMeta->pTaskDb, key, STREAM_TASK_KEY_LEN, pMeta->txn);
11,854✔
687
  if (code != 0) {
11,852!
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);
11,852✔
692
  }
693

694
  return code;
11,849✔
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) {
13,822✔
699
  *pAdded = false;
13,822✔
700

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

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

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

717
  p = taosArrayPush(pMeta->pTaskList, &pTask->id);
13,634✔
718
  if (p == NULL) {
13,634!
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);
13,634✔
725
  code = taosHashPut(pMeta->pTasksMap, &id, sizeof(id), &pTask->id.refId, sizeof(int64_t));
13,634✔
726
  if (code) {  // todo remove it from task list
13,634!
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) {
13,634!
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) {
13,628!
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) {
13,634✔
754
    int32_t val = atomic_add_fetch_32(&pMeta->numOfStreamTasks, 1);
8,666✔
755
  }
756

757
  *pAdded = true;
13,633✔
758
  return code;
13,633✔
759
}
760

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

768
  return size;
14,346,023✔
769
}
770

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

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

785
  if (p->id.refId != *pTaskRefId) {
647,744!
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)) {
647,744✔
797
    stDebug("s-task:%s is stopped, failed to acquire it now", p->id.idStr);
87✔
798
    int32_t ret = taosReleaseRef(streamTaskRefPool, *pTaskRefId);
87✔
799
    if (ret) {
87!
800
      stError("s-task:0x%x failed to release task refId:%" PRId64, taskId, *pTaskRefId);
×
801
    }
802
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
87✔
803
  }
804

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

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

814
  if (pTaskRefId == NULL) {
125,633✔
815
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
1,992✔
816
  }
817

818
  SStreamTask* p = taosAcquireRef(streamTaskRefPool, *pTaskRefId);
123,641✔
819
  if (p == NULL) {
123,676!
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) {
123,678!
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);
123,678✔
837
  *pTask = p;
123,676✔
838
  return TSDB_CODE_SUCCESS;
123,676✔
839
}
840

841
int32_t streamMetaAcquireTask(SStreamMeta* pMeta, int64_t streamId, int32_t taskId, SStreamTask** pTask) {
647,161✔
842
  streamMetaRLock(pMeta);
647,161✔
843
  int32_t code = streamMetaAcquireTaskNoLock(pMeta, streamId, taskId, pTask);
647,277✔
844
  streamMetaRUnLock(pMeta);
647,124✔
845
  return code;
647,175✔
846
}
847

848
void streamMetaReleaseTask(SStreamMeta* UNUSED_PARAM(pMeta), SStreamTask* pTask) {
1,016,648✔
849
  if (pTask == NULL) {
1,016,648✔
850
    return;
12✔
851
  }
852

853
  int32_t taskId = pTask->id.taskId;
1,016,636✔
854
  int64_t refId = pTask->id.refId;
1,016,636✔
855
  stDebug("s-task:0x%x release task, refId:%" PRId64, taskId, pTask->id.refId);
1,016,636✔
856
  int32_t ret = taosReleaseRef(streamTaskRefPool, pTask->id.refId);
1,016,645✔
857
  if (ret) {
1,017,041!
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) {
11,866✔
863
  bool remove = false;
11,866✔
864
  for (int32_t i = 0; i < num; ++i) {
30,521✔
865
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
30,512✔
866
    if (pTaskId->streamId == id->streamId && pTaskId->taskId == id->taskId) {
30,513✔
867
      taosArrayRemove(pTaskList, i);
11,858✔
868
      remove = true;
11,857✔
869
      break;
11,857✔
870
    }
871
  }
872

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

878
static int32_t streamTaskSendTransSuccessMsg(SStreamTask* pTask, void* param) {
11,853✔
879
  int32_t code = 0;
11,853✔
880
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
11,853✔
881
    code = streamTaskSendCheckpointSourceRsp(pTask);
5,885✔
882
    if (code) {
5,884!
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) {
11,848✔
890
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
5,920✔
891
    if (code != TSDB_CODE_SUCCESS) {
5,921!
892
      stError("s-task:%s failed to kill task related query handle, code:%s", pTask->id.idStr, tstrerror(code));
×
893
    }
894
  }
895
  return code;
11,852✔
896
}
897

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

904
  streamMetaWLock(pMeta);
11,875✔
905

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

914
    // handle the dropping event
915
    code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_DROPPING, streamTaskSendTransSuccessMsg, NULL);
11,866✔
916
    if (code) {
11,859!
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);
11,856✔
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) {
11,856✔
924
      int32_t ret = atomic_sub_fetch_32(&pMeta->numOfStreamTasks, 1);
7,007✔
925
    }
926

927
    code = taosHashRemove(pMeta->pTasksMap, &id, sizeof(id));
11,865✔
928
    doRemoveIdFromList(pMeta->pTaskList, (int32_t)taosArrayGetSize(pMeta->pTaskList), &pTask->id);
11,866✔
929
    code = streamMetaRemoveTask(pMeta, &id);
11,854✔
930
    if (code) {
11,853!
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);
11,853✔
935
    int32_t sizeInList = taosArrayGetSize(pMeta->pTaskList);
11,850✔
936
    if (sizeInList != size) {
11,855!
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) {
11,855✔
941
      stDebug("s-task:%s stop schedTimer", pTask->id.idStr);
953✔
942
      streamTmrStop(pTask->schedInfo.pDelayTimer);
953✔
943
      pTask->info.delaySchedParam = 0;
953✔
944
    }
945

946

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

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

960
  return 0;
11,880✔
961
}
962

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

974
int32_t streamMetaCommit(SStreamMeta* pMeta) {
40,008✔
975
  int32_t code = tdbCommit(pMeta->db, pMeta->txn);
40,008✔
976
  if (code != 0) {
40,027!
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);
40,027✔
983
  if (code != 0) {
40,026!
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,
40,026✔
991
                  TDB_TXN_WRITE | TDB_TXN_READ_UNCOMMITTED);
992
  if (code != 0) {
40,024!
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);
40,024✔
997
  }
998

999
  return code;
40,025✔
1000
}
1001

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

1006
  TBC* pCur = NULL;
154✔
1007
  if (tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL) < 0) {
154!
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;
154✔
1013
  int32_t  kLen = 0;
154✔
1014
  void*    pVal = NULL;
154✔
1015
  int32_t  vLen = 0;
154✔
1016
  SDecoder decoder;
1017

1018
  code = tdbTbcMoveToFirst(pCur);
154✔
1019
  if (code) {
154!
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) {
592✔
1026
    if (pVal == NULL || vLen == 0) {
438!
1027
      break;
1028
    }
1029
    SCheckpointInfo info;
1030
    tDecoderInit(&decoder, (uint8_t*)pVal, vLen);
438✔
1031
    if (tDecodeStreamTaskChkInfo(&decoder, &info) < 0) {
438!
1032
      continue;
×
1033
    }
1034
    tDecoderClear(&decoder);
438✔
1035

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

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

1041
  tdbFree(pKey);
153✔
1042
  tdbFree(pVal);
154✔
1043

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

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

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

1064
  vgId = pMeta->vgId;
13,765✔
1065
  pRecycleList = taosArrayInit(4, sizeof(STaskId));
13,765✔
1066
  if (pRecycleList == NULL) {
13,765!
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);
13,765!
1072

1073
  code = tdbTbcOpen(pMeta->pTaskDb, &pCur, NULL);
13,765✔
1074
  if (code != TSDB_CODE_SUCCESS) {
13,765!
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);
13,765✔
1081
  if (code) {
13,765!
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) {
14,221✔
1089
    if (pVal == NULL || vLen == 0) {
456!
1090
      break;
1091
    }
1092

1093
    SStreamTask* pTask = taosMemoryCalloc(1, sizeof(SStreamTask));
456✔
1094
    if (pTask == NULL) {
456!
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);
456✔
1101
    if (tDecodeStreamTask(&decoder, pTask) < 0) {
456!
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);
456✔
1111

1112
    if (pTask->status.taskStatus == TASK_STATUS__DROPPING) {
456!
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,
456✔
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};
456✔
1132
    void*   p = taosHashGet(pMeta->pTasksMap, &id, sizeof(id));
456✔
1133
    if (p == NULL) {
456!
1134
      code = pMeta->buildTaskFn(pMeta->ahandle, pTask, pTask->chkInfo.checkpointVer + 1);
456✔
1135
      if (code < 0) {
456!
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);
456✔
1142
      if (px == NULL) {
456!
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);
456✔
1153

1154
    if (taosHashPut(pMeta->pTasksMap, &id, sizeof(id), &pTask->id.refId, sizeof(int64_t)) != 0) {
456!
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);
456!
1168
    if (pTask->info.fillHistory == 0) {
456✔
1169
      int32_t val = atomic_add_fetch_32(&pMeta->numOfStreamTasks, 1);
400✔
1170
    }
1171

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

1177
  tdbFree(pKey);
13,765✔
1178
  tdbFree(pVal);
13,765✔
1179

1180
  tdbTbcClose(pCur);
13,765✔
1181

1182
  if (taosArrayGetSize(pRecycleList) > 0) {
13,765!
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);
13,765✔
1193
  stDebug("vgId:%d load %d tasks into meta from disk completed, streamTask:%d, paused:%d", pMeta->vgId, numOfTasks,
13,765✔
1194
          pMeta->numOfStreamTasks, pMeta->numOfPausedTasks);
1195

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

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

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

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

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

1219
  streamMetaRLock(pMeta);
13,746✔
1220

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

1226
  int32_t numOfTasks = taosArrayGetSize(pTaskList);
13,747✔
1227
  for (int32_t i = 0; i < numOfTasks; ++i) {
15,928✔
1228
    SStreamTaskId* pTaskId = taosArrayGet(pTaskList, i);
2,182✔
1229
    SStreamTask*   pTask = NULL;
2,182✔
1230

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

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

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

1249
  taosArrayDestroy(pTaskList);
13,746✔
1250

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

1256
void streamMetaStartHb(SStreamMeta* pMeta) {
11,568✔
1257
  int64_t* pRid = taosMemoryMalloc(sizeof(int64_t));
11,568✔
1258
  if (pRid == NULL) {
11,568!
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;
11,568✔
1264
  int32_t code = metaRefMgtAdd(pMeta->vgId, pRid);
11,568✔
1265
  if (code) {
11,568!
1266
    return;
×
1267
  }
1268

1269
  streamMetaHbToMnode(pRid, NULL);
11,568✔
1270
}
1271

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

1275
  int32_t code = 0;
13,790✔
1276
  SArray* pTaskList = taosArrayDup(pMeta->pTaskList, NULL);
13,790✔
1277
  if (pTaskList == NULL) {
13,790!
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;
13,790✔
1283

1284
  bool sendMsg = pMeta->sendMsgBeforeClosing;
13,790✔
1285
  if (!sendMsg) {
13,790✔
1286
    stDebug("vgId:%d no need to send msg to mnode before closing tasks", pMeta->vgId);
13,785✔
1287
    return TSDB_CODE_SUCCESS;
13,785✔
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) {
16,504✔
1313
  streamMetaWLock(pMeta);
16,504✔
1314

1315
  int64_t prevStage = pMeta->stage;
16,505✔
1316
  pMeta->stage = stage;
16,505✔
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) {
16,505✔
1322
    pMeta->sendMsgBeforeClosing = true;
5✔
1323
  }
1324

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

1328
  if (isLeader) {
16,504✔
1329
    stInfo("vgId:%d update meta stage:%" PRId64 ", prev:%" PRId64 " leader:%d, start to send Hb, rid:%" PRId64,
11,567!
1330
           pMeta->vgId, prevStage, stage, isLeader, pMeta->rid);
1331
    streamMetaStartHb(pMeta);
11,568✔
1332
  } else {
1333
    stInfo("vgId:%d update meta stage:%" PRId64 " prev:%" PRId64 " leader:%d sendMsg beforeClosing:%d", pMeta->vgId,
4,937!
1334
           prevStage, stage, isLeader, pMeta->sendMsgBeforeClosing);
1335
  }
1336
}
16,505✔
1337

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

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

1355
  return true;
1,729✔
1356
}
1357

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

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

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

1377
  return 0;
109✔
1378
}
1379

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

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

1393
  int64_t el = taosGetTimestampMs() - startTs;
26✔
1394
  if (pHTask != NULL) {
26✔
1395
    STaskUpdateEntry hEntry = {.streamId = pHTask->id.streamId, .taskId = pHTask->id.taskId, .transId = transId};
14✔
1396
    code = taosHashPut(pMeta->updateInfo.pTasks, &hEntry, sizeof(hEntry), NULL, 0);
14✔
1397
    if (code != 0) {
14!
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
14!
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",
12!
1406
            id, vgId, transId, el);
1407
  }
1408
}
26✔
1409

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

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

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

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

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

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

1432
      stInfo("vgId:%d set the active epset update transId:%d, prev complete transId:%d", pMeta->vgId, transId,
14!
1433
             pInfo->completeTransId);
1434
      return true;
14✔
1435
    } else {
1436
      if (pInfo->activeTransId == transId) {
12!
1437
        // do nothing
1438
        return true;
12✔
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