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

taosdata / TDengine / #3620

21 Feb 2025 09:00AM UTC coverage: 63.573% (+0.2%) from 63.423%
#3620

push

travis-ci

web-flow
ci: taosBenchmark add coverage cases branch 3.0 (#29788)

* fix: add unit test for taos-tools

* fix: only .cpp include

* fix: remove no use function

* fix: restore toolsSys.c

* fix: add toolsSys case

* fix: rebuild error fixed

* fix: fix build error

* fix: support get vgroups with core and memory limit

* fix: build error for strcasecmp

* fix: add insertBasic.py case

* fix: add command line set vgroups=3

* fix: change with ns database

* toolscJson read with int replace float and add insertPrecison.py

* fix: add insertBindVGroup.json case

* fix: remove public fun removeQuotation

* fix: vgroups change method

* fix: memory leak for runInsertLimitThread slot

* insertPrecision.py word write wrong

* fix: check isFloat number

* fix: vgroups change logic error

* fix: insertBasic.py real and expect error

* fix: adjust default vgroups

* fix: adjust default vgroups modify comment

148962 of 300203 branches covered (49.62%)

Branch coverage included in aggregate %.

15 of 16 new or added lines in 1 file covered. (93.75%)

2018 existing lines in 133 files now uncovered.

233201 of 300933 relevant lines covered (77.49%)

18174406.98 hits per line

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

72.78
/source/dnode/mnode/impl/src/mndStreamUtil.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 "mndDb.h"
17
#include "mndStb.h"
18
#include "mndStream.h"
19
#include "mndTrans.h"
20
#include "mndVgroup.h"
21
#include "taoserror.h"
22
#include "tmisce.h"
23

24
struct SStreamTaskIter {
25
  SStreamObj  *pStream;
26
  int32_t      level;
27
  int32_t      ordinalIndex;
28
  int32_t      totalLevel;
29
  SStreamTask *pTask;
30
};
31

32
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId);
33

34
int32_t createStreamTaskIter(SStreamObj *pStream, SStreamTaskIter **pIter) {
106,983✔
35
  *pIter = taosMemoryCalloc(1, sizeof(SStreamTaskIter));
106,983!
36
  if (*pIter == NULL) {
106,997!
37
    return terrno;
×
38
  }
39

40
  (*pIter)->level = -1;
106,997✔
41
  (*pIter)->ordinalIndex = 0;
106,997✔
42
  (*pIter)->pStream = pStream;
106,997✔
43
  (*pIter)->totalLevel = taosArrayGetSize(pStream->tasks);
106,997✔
44
  (*pIter)->pTask = NULL;
106,986✔
45

46
  return 0;
106,986✔
47
}
48

49
bool streamTaskIterNextTask(SStreamTaskIter *pIter) {
486,405✔
50
  if (pIter->level >= pIter->totalLevel) {
486,405!
51
    pIter->pTask = NULL;
×
52
    return false;
×
53
  }
54

55
  if (pIter->level == -1) {
486,405✔
56
    pIter->level += 1;
106,986✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
695,959✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
588,564✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
586,801✔
62
      pIter->level += 1;
209,554✔
63
      pIter->ordinalIndex = 0;
209,554✔
64
      pIter->pTask = NULL;
209,554✔
65
      continue;
209,554✔
66
    }
67

68
    pIter->pTask = taosArrayGetP(pList, pIter->ordinalIndex);
378,498✔
69
    pIter->ordinalIndex += 1;
377,721✔
70
    return true;
377,721✔
71
  }
72

73
  pIter->pTask = NULL;
107,395✔
74
  return false;
107,395✔
75
}
76

77
int32_t streamTaskIterGetCurrent(SStreamTaskIter *pIter, SStreamTask **pTask) {
378,026✔
78
  if (pTask) {
378,026!
79
    *pTask = pIter->pTask;
378,060✔
80
    if (*pTask != NULL) {
378,060!
81
      return TSDB_CODE_SUCCESS;
378,081✔
82
    }
83
  }
84

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
106,737!
89

90
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
271,170✔
91
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
546,398✔
92
    if (!pVgroup->vnodeGid[i].syncRestore) {
276,269✔
93
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,034!
94
      return false;
1,034✔
95
    }
96

97
    ESyncState state = pVgroup->vnodeGid[i].syncState;
275,235✔
98
    if (state == TAOS_SYNC_STATE_OFFLINE || state == TAOS_SYNC_STATE_ERROR || state == TAOS_SYNC_STATE_LEARNER ||
275,235!
99
        state == TAOS_SYNC_STATE_CANDIDATE) {
100
      mInfo("vgId:%d state:%d , not ready for checkpoint or other operations, not check other vgroups", pVgroup->vgId,
7!
101
            state);
102
      return false;
7✔
103
    }
104
  }
105

106
  return true;
270,129✔
107
}
108

109
static int32_t mndAddSnodeInfo(SMnode *pMnode, SArray *pVgroupList) {
15,828✔
110
  SSnodeObj *pObj = NULL;
15,828✔
111
  void      *pIter = NULL;
15,828✔
112
  int32_t    code = 0;
15,828✔
113

114
  while (1) {
7,324✔
115
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
23,152✔
116
    if (pIter == NULL) {
23,152✔
117
      break;
15,828✔
118
    }
119

120
    SNodeEntry entry = {.nodeId = SNODE_HANDLE};
7,324✔
121
    code = addEpIntoEpSet(&entry.epset, pObj->pDnode->fqdn, pObj->pDnode->port);
7,324✔
122
    if (code) {
7,324!
123
      sdbRelease(pMnode->pSdb, pObj);
×
124
      sdbCancelFetch(pMnode->pSdb, pIter);
×
125
      mError("failed to extract epset for fqdn:%s during task vgroup snapshot", pObj->pDnode->fqdn);
×
126
      return code;
×
127
    }
128

129
    char buf[256] = {0};
7,324✔
130
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
7,324✔
131
    if (code != 0) {  // print error and continue
7,324!
132
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
133
    }
134

135
    void *p = taosArrayPush(pVgroupList, &entry);
7,324✔
136
    if (p == NULL) {
7,324!
137
      code = terrno;
×
138
      sdbRelease(pMnode->pSdb, pObj);
×
139
      sdbCancelFetch(pMnode->pSdb, pIter);
×
140
      mError("failed to put entry in vgroup list, nodeId:%d code:%s", entry.nodeId, tstrerror(code));
×
141
      return code;
×
142
    } else {
143
      mDebug("take snode snapshot, nodeId:%d %s", entry.nodeId, buf);
7,324✔
144
    }
145

146
    sdbRelease(pMnode->pSdb, pObj);
7,324✔
147
  }
148

149
  return code;
15,828✔
150
}
151

152
static int32_t mndCheckMnodeStatus(SMnode* pMnode) {
15,828✔
153
  int32_t    code = 0;
15,828✔
154
  ESdbStatus objStatus;
155
  void      *pIter = NULL;
15,828✔
156
  SMnodeObj *pObj = NULL;
15,828✔
157

158
  while (1) {
159
    pIter = sdbFetchAll(pMnode->pSdb, SDB_MNODE, pIter, (void **)&pObj, &objStatus, true);
32,234✔
160
    if (pIter == NULL) {
32,234✔
161
      break;
15,698✔
162
    }
163

164
    if (pObj->syncState != TAOS_SYNC_STATE_LEADER && pObj->syncState != TAOS_SYNC_STATE_FOLLOWER) {
16,536✔
165
      mDebug("mnode sync state:%d not leader/follower", pObj->syncState);
128!
166
      sdbRelease(pMnode->pSdb, pObj);
128✔
167
      sdbCancelFetch(pMnode->pSdb, pIter);
128✔
168
      return TSDB_CODE_FAILED;
128✔
169
    }
170

171
    if (objStatus != SDB_STATUS_READY) {
16,408✔
172
      mWarn("mnode status:%d not ready", objStatus);
2!
173
      sdbRelease(pMnode->pSdb, pObj);
2✔
174
      sdbCancelFetch(pMnode->pSdb, pIter);
2✔
175
      return TSDB_CODE_FAILED;
2✔
176
    }
177

178
    sdbRelease(pMnode->pSdb, pObj);
16,406✔
179
  }
180

181
  return TSDB_CODE_SUCCESS;
15,698✔
182
}
183

184
static int32_t mndCheckAndAddVgroupsInfo(SMnode *pMnode, SArray *pVgroupList, bool* allReady) {
15,828✔
185
  SSdb     *pSdb = pMnode->pSdb;
15,828✔
186
  void     *pIter = NULL;
15,828✔
187
  SVgObj   *pVgroup = NULL;
15,828✔
188
  int32_t   code = 0;
15,828✔
189
  SHashObj *pHash = NULL;
15,828✔
190

191
  pHash = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
15,828✔
192
  if (pHash == NULL) {
15,828!
193
    mError("failed to prepare hashmap during take vgroup snapshot, code:%s", tstrerror(terrno));
×
194
    return terrno;
×
195
  }
196

197
  while (1) {
273,573✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
289,401✔
199
    if (pIter == NULL) {
289,401✔
200
      break;
15,828✔
201
    }
202

203
    SNodeEntry entry = {.nodeId = pVgroup->vgId, .hbTimestamp = pVgroup->updateTime};
273,573✔
204
    entry.epset = mndGetVgroupEpset(pMnode, pVgroup);
273,573✔
205

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
273,573✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
273,573✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
135,903✔
209
      if (code) {
135,903!
210
        mError("failed to put info into hashmap during task vgroup snapshot, code:%s", tstrerror(code));
×
211
        sdbRelease(pSdb, pVgroup);
×
212
        sdbCancelFetch(pSdb, pIter);
×
213
        goto _end;  // take snapshot failed, and not all ready
×
214
      }
215
    } else {
216
      if (*pReplica != pVgroup->replica) {
137,670✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
400!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
400✔
220
      }
221
    }
222

223
    // if not all ready till now, no need to check the remaining vgroups,
224
    // but still we need to put the info of the existed vgroups into the snapshot list
225
    if (*allReady) {
273,573✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
271,170✔
227
    }
228

229
    char buf[256] = {0};
273,573✔
230
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
273,573✔
231
    if (code != 0) {  // print error and continue
273,573!
232
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
233
    }
234

235
    void *p = taosArrayPush(pVgroupList, &entry);
273,573✔
236
    if (p == NULL) {
273,573!
237
      mError("failed to put entry in vgroup list, nodeId:%d code:out of memory", entry.nodeId);
×
238
      code = terrno;
×
239
      sdbRelease(pSdb, pVgroup);
×
240
      sdbCancelFetch(pSdb, pIter);
×
241
      goto _end;
×
242
    } else {
243
      mDebug("take node snapshot, nodeId:%d %s", entry.nodeId, buf);
273,573✔
244
    }
245

246
    sdbRelease(pSdb, pVgroup);
273,573✔
247
  }
248

249
_end:
15,828✔
250
  taosHashCleanup(pHash);
15,828✔
251
  return code;
15,828✔
252
}
253

254
int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList) {
15,828✔
255
  int32_t   code = 0;
15,828✔
256
  SArray   *pVgroupList = NULL;
15,828✔
257

258
  *pList = NULL;
15,828✔
259
  *allReady = true;
15,828✔
260

261
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
15,828✔
262
  if (pVgroupList == NULL) {
15,828!
263
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
264
    code = terrno;
×
265
    goto _err;
×
266
  }
267

268
  // 1. check for all vnodes status
269
  code = mndCheckAndAddVgroupsInfo(pMnode, pVgroupList, allReady);
15,828✔
270
  if (code) {
15,828!
271
    goto _err;
×
272
  }
273

274
  // 2. add snode info
275
  code = mndAddSnodeInfo(pMnode, pVgroupList);
15,828✔
276
  if (code) {
15,828!
277
    goto _err;
×
278
  }
279

280
  // 3. check for mnode status
281
  code = mndCheckMnodeStatus(pMnode);
15,828✔
282
  if (code != TSDB_CODE_SUCCESS) {
15,828✔
283
    *allReady = false;
130✔
284
  }
285

286
  *pList = pVgroupList;
15,828✔
287
  return code;
15,828✔
288

289
_err:
×
290
  *allReady = false;
×
291
  taosArrayDestroy(pVgroupList);
×
292
  return code;
×
293
}
294

295
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
12,886✔
296
  void *pIter = NULL;
12,886✔
297
  SSdb *pSdb = pMnode->pSdb;
12,886✔
298
  *pStream = NULL;
12,886✔
299

300
  SStreamObj *p = NULL;
12,886✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22,086✔
302
    if (p->uid == streamId) {
22,082✔
303
      sdbCancelFetch(pSdb, pIter);
12,882✔
304
      *pStream = p;
12,882✔
305
      return TSDB_CODE_SUCCESS;
12,882✔
306
    }
307
    sdbRelease(pSdb, p);
9,200✔
308
  }
309

310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
4✔
311
}
312

313
void mndKillTransImpl(SMnode *pMnode, int32_t transId, const char *pDbName) {
1✔
314
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
1✔
315
  if (pTrans != NULL) {
1!
316
    mInfo("kill active transId:%d in Db:%s", transId, pDbName);
1!
317
    int32_t code = mndKillTrans(pMnode, pTrans);
1✔
318
    mndReleaseTrans(pMnode, pTrans);
1✔
319
    if (code) {
1!
320
      mError("failed to kill transId:%d, code:%s", pTrans->id, tstrerror(code));
1!
321
    }
322
  } else {
323
    mError("failed to acquire trans in Db:%s, transId:%d", pDbName, transId);
×
324
  }
325
}
1✔
326

327
int32_t extractNodeEpset(SMnode *pMnode, SEpSet *pEpSet, bool *hasEpset, int32_t taskId, int32_t nodeId) {
19,986✔
328
  *hasEpset = false;
19,986✔
329

330
  pEpSet->numOfEps = 0;
19,986✔
331
  if (nodeId == SNODE_HANDLE) {
19,986✔
332
    SSnodeObj *pObj = NULL;
318✔
333
    void      *pIter = NULL;
318✔
334

335
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
318✔
336
    if (pIter != NULL) {
318!
337
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
318✔
338
      sdbRelease(pMnode->pSdb, pObj);
318✔
339
      sdbCancelFetch(pMnode->pSdb, pIter);
318✔
340
      if (code) {
318!
341
        *hasEpset = false;
×
342
        mError("failed to set epset");
×
343
      } else {
344
        *hasEpset = true;
318✔
345
      }
346
      return code;
318✔
347
    } else {
348
      mError("failed to acquire snode epset");
×
349
      return TSDB_CODE_INVALID_PARA;
×
350
    }
351
  } else {
352
    SVgObj *pVgObj = mndAcquireVgroup(pMnode, nodeId);
19,668✔
353
    if (pVgObj != NULL) {
19,668✔
354
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
19,667✔
355
      mndReleaseVgroup(pMnode, pVgObj);
19,667✔
356

357
      epsetAssign(pEpSet, &epset);
19,667✔
358
      *hasEpset = true;
19,667✔
359
      return TSDB_CODE_SUCCESS;
19,667✔
360
    } else {
361
      mDebug("orphaned task:0x%x need to be dropped, nodeId:%d, no redo action", taskId, nodeId);
1!
362
      return TSDB_CODE_SUCCESS;
1✔
363
    }
364
  }
365
}
366

367
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
203✔
368
  *pTask = NULL;
203✔
369

370
  SStreamTask     *p = NULL;
203✔
371
  SStreamTaskIter *pIter = NULL;
203✔
372
  int32_t          code = createStreamTaskIter(pStream, &pIter);
203✔
373
  if (code) {
203!
374
    mError("failed to create stream task iter:%s", pStream->name);
×
375
    return code;
×
376
  }
377

378
  while (streamTaskIterNextTask(pIter)) {
768!
379
    code = streamTaskIterGetCurrent(pIter, &p);
768✔
380
    if (code) {
768!
381
      continue;
×
382
    }
383

384
    if (p->id.taskId == pId->taskId) {
768✔
385
      destroyStreamTaskIter(pIter);
203✔
386
      *pTask = p;
203✔
387
      return 0;
203✔
388
    }
389
  }
390

391
  destroyStreamTaskIter(pIter);
×
392
  return TSDB_CODE_FAILED;
×
393
}
394

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
77,298✔
396
  int32_t num = 0;
77,298✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
229,967✔
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
152,612✔
399
    num += taosArrayGetSize(pLevel);
152,693✔
400
  }
401

402
  return num;
77,214✔
403
}
404

405
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
56✔
406
  SSdb   *pSdb = pMnode->pSdb;
56✔
407
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
56✔
408
  if (pDb == NULL) {
56!
409
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
410
  }
411

412
  int32_t numOfStreams = 0;
56✔
413
  void   *pIter = NULL;
56✔
414
  while (1) {
×
415
    SStreamObj *pStream = NULL;
56✔
416
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
56✔
417
    if (pIter == NULL) break;
56!
418

419
    if (pStream->sourceDbUid == pDb->uid) {
×
420
      numOfStreams++;
×
421
    }
422

423
    sdbRelease(pSdb, pStream);
×
424
  }
425

426
  *pNumOfStreams = numOfStreams;
56✔
427
  mndReleaseDb(pMnode, pDb);
56✔
428
  return 0;
56✔
429
}
430

431
static void freeTaskList(void *param) {
1,548✔
432
  SArray **pList = (SArray **)param;
1,548✔
433
  taosArrayDestroy(*pList);
1,548✔
434
}
1,548✔
435

436
int32_t mndInitExecInfo() {
1,872✔
437
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
1,872✔
438
  if (code) {
1,872!
439
    return code;
×
440
  }
441

442
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
1,872✔
443

444
  execInfo.pTaskList = taosArrayInit(4, sizeof(STaskId));
1,872✔
445
  execInfo.pTaskMap = taosHashInit(64, fn, true, HASH_NO_LOCK);
1,872✔
446
  execInfo.transMgmt.pDBTrans = taosHashInit(32, fn, true, HASH_NO_LOCK);
1,872✔
447
  execInfo.pTransferStateStreams = taosHashInit(32, fn, true, HASH_NO_LOCK);
1,872✔
448
  execInfo.pChkptStreams = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,872✔
449
  execInfo.pStreamConsensus = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
1,872✔
450
  execInfo.pNodeList = taosArrayInit(4, sizeof(SNodeEntry));
1,872✔
451
  execInfo.pKilledChkptTrans = taosArrayInit(4, sizeof(SStreamTaskResetMsg));
1,872✔
452

453
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
1,872!
454
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
1,872!
455
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
1,872!
456
    mError("failed to initialize the stream runtime env, code:%s", tstrerror(terrno));
×
457
    return terrno;
×
458
  }
459

460
  execInfo.role = NODE_ROLE_UNINIT;
1,872✔
461
  execInfo.switchFromFollower = false;
1,872✔
462

463
  taosHashSetFreeFp(execInfo.pTransferStateStreams, freeTaskList);
1,872✔
464
  taosHashSetFreeFp(execInfo.pChkptStreams, freeTaskList);
1,872✔
465
  taosHashSetFreeFp(execInfo.pStreamConsensus, freeTaskList);
1,872✔
466
  return 0;
1,872✔
467
}
468

469
void removeExpiredNodeInfo(const SArray *pNodeSnapshot) {
1,489✔
470
  SArray *pValidList = taosArrayInit(4, sizeof(SNodeEntry));
1,489✔
471
  if (pValidList == NULL) {  // not continue
1,489!
472
    return;
×
473
  }
474

475
  int32_t size = taosArrayGetSize(pNodeSnapshot);
1,489✔
476
  int32_t oldSize = taosArrayGetSize(execInfo.pNodeList);
1,489✔
477

478
  for (int32_t i = 0; i < oldSize; ++i) {
10,171✔
479
    SNodeEntry *p = taosArrayGet(execInfo.pNodeList, i);
8,682✔
480
    if (p == NULL) {
8,682!
481
      continue;
×
482
    }
483

484
    for (int32_t j = 0; j < size; ++j) {
105,347✔
485
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
104,286✔
486
      if (pEntry == NULL) {
104,286!
487
        continue;
×
488
      }
489

490
      if (pEntry->nodeId == p->nodeId) {
104,286✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
7,621✔
492

493
        void *px = taosArrayPush(pValidList, p);
7,621✔
494
        if (px == NULL) {
7,621!
495
          mError("failed to put node into list, nodeId:%d", p->nodeId);
×
496
        } else {
497
          mDebug("vgId:%d ts:%" PRId64 " HbMsgId:%d is valid", p->nodeId, p->hbTimestamp, p->lastHbMsgId);
7,621✔
498
        }
499
        break;
7,621✔
500
      }
501
    }
502
  }
503

504
  taosArrayDestroy(execInfo.pNodeList);
1,489✔
505
  execInfo.pNodeList = pValidList;
1,489✔
506

507
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
1,489✔
508
         (int32_t)taosArrayGetSize(pValidList), oldSize);
509
}
510

511
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) {
7,332✔
512
  void *p = taosHashGet(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
7,332✔
513
  if (p == NULL) {
7,332✔
514
    return TSDB_CODE_SUCCESS;
130✔
515
  }
516

517
  int32_t code = taosHashRemove(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
7,202✔
518
  if (code) {
7,202!
519
    return code;
×
520
  }
521

522
  for (int32_t k = 0; k < taosArrayGetSize(pExecNode->pTaskList); ++k) {
20,982!
523
    STaskId *pId = taosArrayGet(pExecNode->pTaskList, k);
20,982✔
524
    if (pId == NULL) {
20,982!
525
      continue;
×
526
    }
527

528
    if (pId->taskId == pRemovedId->taskId && pId->streamId == pRemovedId->streamId) {
20,982!
529
      taosArrayRemove(pExecNode->pTaskList, k);
7,202✔
530

531
      int32_t num = taosArrayGetSize(pExecNode->pTaskList);
7,202✔
532
      mInfo("s-task:0x%x removed from buffer, remain:%d in buffer list", (int32_t)pRemovedId->taskId, num);
7,202!
533
      break;
7,202✔
534
    }
535
  }
536

537
  return TSDB_CODE_SUCCESS;
7,202✔
538
}
539

540
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
1,489✔
541
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
1,489!
542
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
543
    if (pId == NULL) {
×
544
      continue;
×
545
    }
546

547
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
548
    if (code) {
×
549
      mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId);
×
550
    }
551
  }
552
}
1,489✔
553

554
void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
1,351✔
555
  SStreamTaskIter *pIter = NULL;
1,351✔
556
  streamMutexLock(&pExecNode->lock);
1,351✔
557

558
  // 1. remove task entries
559
  int32_t code = createStreamTaskIter(pStream, &pIter);
1,351✔
560
  if (code) {
1,351!
561
    streamMutexUnlock(&pExecNode->lock);
×
562
    mError("failed to create stream task iter:%s", pStream->name);
×
563
    return;
×
564
  }
565

566
  while (streamTaskIterNextTask(pIter)) {
8,683✔
567
    SStreamTask *pTask = NULL;
7,332✔
568
    code = streamTaskIterGetCurrent(pIter, &pTask);
7,332✔
569
    if (code) {
7,332!
570
      continue;
×
571
    }
572

573
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,332✔
574
    code = doRemoveTasks(pExecNode, &id);
7,332✔
575
    if (code) {
7,332!
576
      mError("failed to remove task in buffer list, 0x%" PRIx64, id.taskId);
×
577
    }
578
  }
579

580
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
1,351!
581
    streamMutexUnlock(&pExecNode->lock);
×
582
    destroyStreamTaskIter(pIter);
×
583
    mError("task map size, task list size, not equal");
×
584
    return;
×
585
  }
586

587
  // 2. remove stream entry in consensus hash table and checkpoint-report hash table
588
  code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, pStream->uid);
1,351✔
589
  if (code) {
1,351!
590
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
591
  }
592

593
  code = mndClearChkptReportInfo(execInfo.pChkptStreams, pStream->uid);
1,351✔
594
  if (code) {
1,351✔
595
    mError("failed to clear the checkpoint report info, code:%s", tstrerror(code));
386!
596
  }
597

598
  streamMutexUnlock(&pExecNode->lock);
1,351✔
599
  destroyStreamTaskIter(pIter);
1,351✔
600
}
601

602
static bool taskNodeExists(SArray *pList, int32_t nodeId) {
15,481✔
603
  size_t num = taosArrayGetSize(pList);
15,481✔
604

605
  for (int32_t i = 0; i < num; ++i) {
115,479!
606
    SNodeEntry *pEntry = taosArrayGet(pList, i);
115,479✔
607
    if (pEntry == NULL) {
115,479!
608
      continue;
×
609
    }
610

611
    if (pEntry->nodeId == nodeId) {
115,479✔
612
      return true;
15,481✔
613
    }
614
  }
615

616
  return false;
×
617
}
618

619
int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) {
1,489✔
620
  SArray *pRemovedTasks = taosArrayInit(4, sizeof(STaskId));
1,489✔
621
  if (pRemovedTasks == NULL) {
1,489!
622
    return terrno;
×
623
  }
624

625
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
1,489✔
626
  for (int32_t i = 0; i < numOfTask; ++i) {
17,930✔
627
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
16,441✔
628
    if (pId == NULL) {
16,441!
629
      continue;
×
630
    }
631

632
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, pId, sizeof(*pId));
16,441✔
633
    if (pEntry == NULL) {
16,441!
634
      continue;
×
635
    }
636

637
    if (pEntry->nodeId == SNODE_HANDLE) {
16,441✔
638
      continue;
960✔
639
    }
640

641
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
15,481✔
642
    if (!existed) {
15,481!
643
      void *p = taosArrayPush(pRemovedTasks, pId);
×
644
      if (p == NULL) {
×
645
        mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId);
×
646
      }
647
    }
648
  }
649

650
  removeTasksInBuf(pRemovedTasks, &execInfo);
1,489✔
651

652
  mDebug("remove invalid stream tasks:%d, remain:%d", (int32_t)taosArrayGetSize(pRemovedTasks),
1,489✔
653
         (int32_t)taosArrayGetSize(execInfo.pTaskList));
654

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,489✔
656

657
  taosArrayDestroy(pRemovedTasks);
1,489✔
658
  return 0;
1,489✔
659
}
660

661
static int32_t allTasksSendChkptReport(SChkptReportInfo* pReportInfo, int32_t numOfTasks, const char* pName) {
1,207✔
662
  int64_t checkpointId = -1;
1,207✔
663
  int32_t transId = -1;
1,207✔
664
  int32_t taskId = -1;
1,207✔
665

666
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
1,207✔
667
  if (existed != numOfTasks) {
1,207✔
668
    mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pReportInfo->streamId, pName,
32✔
669
           existed, numOfTasks, numOfTasks - existed);
670
    return -1;
32✔
671
  }
672

673
  // acquire current active checkpointId, and do cross-check checkpointId info in exec.pTaskList
674
  for(int32_t i = 0; i < numOfTasks; ++i) {
6,697✔
675
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
5,522✔
676
    if (pInfo == NULL) {
5,522!
677
      continue;
×
678
    }
679

680
    if (checkpointId == -1) {
5,522✔
681
      checkpointId = pInfo->checkpointId;
1,175✔
682
      transId = pInfo->transId;
1,175✔
683
      taskId = pInfo->taskId;
1,175✔
684
    } else if (checkpointId != pInfo->checkpointId) {
4,347!
685
      mError("stream:0x%" PRIx64
×
686
             " checkpointId in checkpoint-report list are not identical, type 1 taskId:0x%x checkpointId:%" PRId64
687
             ", type 2 taskId:0x%x checkpointId:%" PRId64,
688
             pReportInfo->streamId, taskId, checkpointId, pInfo->taskId, pInfo->checkpointId);
689
      return -1;
×
690
    }
691
  }
692

693
  // check for the correct checkpointId for current task info in STaskChkptInfo
694
  STaskChkptInfo  *p = taosArrayGet(pReportInfo->pTaskList, 0);
1,175✔
695
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
1,175✔
696
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
1,175✔
697

698
  // cross-check failed, there must be something unknown wrong
699
  SStreamTransInfo *pTransInfo = taosHashGet(execInfo.transMgmt.pDBTrans, &id.streamId, sizeof(id.streamId));
1,175✔
700
  if (pTransInfo == NULL) {
1,175✔
701
    mWarn("stream:0x%" PRIx64 " no active trans exists for checkpoint transId:%d, it may have been cleared already",
140!
702
           id.streamId, transId);
703

704
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
140!
705
      mWarn("stream:0x%" PRIx64 " active checkpointId is not equalled to the required, current:%" PRId64
×
706
            ", req:%" PRId64 " recheck next time",
707
            id.streamId, pe->checkpointInfo.activeId, checkpointId);
708
      return -1;
×
709
    } else {
710
      //  do nothing
711
    }
712
  } else {
713
    if (pTransInfo->transId != transId) {
1,035✔
714
      mError("stream:0x%" PRIx64
1!
715
             " checkpoint-report list info are expired, active transId:%d trans in list:%d, recheck next time",
716
             id.streamId, pTransInfo->transId, transId);
717
      return -1;
1✔
718
    }
719
  }
720

721
  mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info", id.streamId,
1,174✔
722
         pName, numOfTasks);
723

724
  return TSDB_CODE_SUCCESS;
1,174✔
725
}
726

727
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
28,988✔
728
  SMnode *pMnode = pReq->info.node;
28,988✔
729
  void   *pIter = NULL;
28,988✔
730
  int32_t code = 0;
28,988✔
731
  SArray *pDropped = taosArrayInit(4, sizeof(int64_t));
28,988✔
732
  if (pDropped == NULL) {
28,988!
733
    return terrno;
×
734
  }
735

736
  mDebug("start to scan checkpoint report info");
28,988✔
737

738
  streamMutexLock(&execInfo.lock);
28,988✔
739

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
96,838✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
69,024✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
69,024✔
743
      continue;
67,817✔
744
    }
745

746
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
1,207✔
747
    if (pInfo == NULL) {
1,207!
748
      continue;
×
749
    }
750

751
    SStreamObj *pStream = NULL;
1,207✔
752
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
1,207✔
753
    if (pStream == NULL || code != 0) {
1,207!
754
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
755
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
756
      if (p == NULL) {
×
757
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
758
      }
759
      continue;
×
760
    }
761

762
    int32_t total = mndGetNumOfStreamTasks(pStream);
1,207✔
763
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
1,207✔
764
    if (ret == 0) {
1,207✔
765
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,174✔
766
      if (code == 0) {
1,174!
767
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,174✔
768
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,174!
769
          taosArrayClear(px->pTaskList);
1,174✔
770
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
1,174!
771
                " to %" PRId64,
772
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
773
          px->reportChkpt = pInfo->checkpointId;
1,174✔
774
        } else {
775
          mDebug("stream:0x%" PRIx64 " not launch chkpt-info update trans, due to checkpoint not finished yet",
×
776
                 pInfo->streamId);
777
        }
778

779
        sdbRelease(pMnode->pSdb, pStream);
1,174✔
780
        break;
1,174✔
781
      } else {
782
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
783
      }
784
    }
785

786
    sdbRelease(pMnode->pSdb, pStream);
33✔
787
  }
788

789
  int32_t size = taosArrayGetSize(pDropped);
28,988✔
790
  if (size > 0) {
28,988!
791
    for (int32_t i = 0; i < size; ++i) {
×
792
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
793
      if (pStreamId == NULL) {
×
794
        continue;
×
795
      }
796

797
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
798
      if (code) {
×
799
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
800
      }
801
    }
802

803
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
804
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
805
  }
806

807
  streamMutexUnlock(&execInfo.lock);
28,988✔
808

809
  taosArrayDestroy(pDropped);
28,988✔
810

811
  mDebug("end to scan checkpoint report info")
28,988✔
812
  return TSDB_CODE_SUCCESS;
28,988✔
813
}
814

815
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int32_t taskId, int64_t checkpointId,
203✔
816
                                          int64_t ts) {
817
  char         msg[128] = {0};
203✔
818
  STrans      *pTrans = NULL;
203✔
819
  SStreamTask *pTask = NULL;
203✔
820

821
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for task:0x%x", taskId);
203✔
822

823
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
203✔
824
  if (pTrans == NULL || code != 0) {
203!
UNCOV
825
    return terrno;
×
826
  }
827

828
  STaskId id = {.streamId = pStream->uid, .taskId = taskId};
203✔
829
  code = mndGetStreamTask(&id, pStream, &pTask);
203✔
830
  if (code) {
203!
831
    mError("failed to get task:0x%x in stream:%s, failed to create consensus-checkpointId", taskId, pStream->name);
×
832
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
833
    return code;
×
834
  }
835

836
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
203✔
837
  if (code) {
203!
838
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
839
    return code;
×
840
  }
841

842
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pTask, checkpointId, ts);
203✔
843
  if (code != 0) {
203!
844
    sdbRelease(pMnode->pSdb, pStream);
×
845
    mndTransDrop(pTrans);
×
UNCOV
846
    return code;
×
847
  }
848

849
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
203✔
850
  if (code) {
203!
851
    sdbRelease(pMnode->pSdb, pStream);
×
852
    mndTransDrop(pTrans);
×
UNCOV
853
    return code;
×
854
  }
855

856
  code = mndTransPrepare(pMnode, pTrans);
203✔
857
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
203!
858
    mError("trans:%d, failed to prepare set consensus-chkptId trans since %s", pTrans->id, terrstr());
×
859
    sdbRelease(pMnode->pSdb, pStream);
×
860
    mndTransDrop(pTrans);
×
UNCOV
861
    return code;
×
862
  }
863

864
  sdbRelease(pMnode->pSdb, pStream);
203✔
865
  mndTransDrop(pTrans);
203✔
866

867
  return TSDB_CODE_ACTION_IN_PROGRESS;
203✔
868
}
869

870
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
215✔
871
  *pInfo = NULL;
215✔
872

873
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
215✔
874
  if (px != NULL) {
215✔
875
    *pInfo = px;
172✔
876
    return 0;
172✔
877
  }
878

879
  SCheckpointConsensusInfo p = {
43✔
880
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
43✔
881
      .numOfTasks = numOfTasks,
882
      .streamId = streamId,
883
  };
884

885
  if (p.pTaskList == NULL) {
43!
UNCOV
886
    return terrno;
×
887
  }
888

889
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
43✔
890
  if (code == 0) {
43!
891
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
43✔
892
    *pInfo = pChkptInfo;
43✔
893
  } else {
UNCOV
894
    *pInfo = NULL;
×
895
  }
896

897
  return code;
43✔
898
}
899

900
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
901
// discard the msg may lead to the lost of connections.
902
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
215✔
903
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
215✔
904
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
215✔
905

906
  int32_t num = (int32_t) taosArrayGetSize(pInfo->pTaskList);
215✔
907
  for (int32_t i = 0; i < num; ++i) {
772✔
908
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
569✔
909
    if (p == NULL) {
569!
UNCOV
910
      continue;
×
911
    }
912

913
    if (p->req.taskId == info.req.taskId) {
569✔
914
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update ts %" PRId64
12✔
915
             "->%" PRId64 " checkpointId:%" PRId64 " -> %" PRId64 " total existed:%d",
916
             pRestoreInfo->taskId, pRestoreInfo->streamId, p->req.startTs, info.req.startTs, p->req.checkpointId,
917
             info.req.checkpointId, num);
918
      p->req.startTs = info.req.startTs;
12✔
919
      p->req.checkpointId = info.req.checkpointId;
12✔
920
      p->req.transId = info.req.transId;
12✔
921
      return;
12✔
922
    }
923
  }
924

925
  void *p = taosArrayPush(pInfo->pTaskList, &info);
203✔
926
  if (p == NULL) {
203!
UNCOV
927
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
928
  } else {
929
    num = taosArrayGetSize(pInfo->pTaskList);
203✔
930
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
203✔
931
           " waiting tasks:%d",
932
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
933
  }
934
}
935

936
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
43✔
937
  taosArrayDestroy(pInfo->pTaskList);
43✔
938
  pInfo->pTaskList = NULL;
43✔
939
}
43✔
940

941
int32_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
1,394✔
942
  int32_t code = 0;
1,394✔
943
  int32_t numOfStreams = taosHashGetSize(pHash);
1,394✔
944
  if (numOfStreams == 0) {
1,394✔
945
    return code;
1,351✔
946
  }
947

948
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
43✔
949
  if (code == 0) {
43!
950
    mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
43✔
951
  } else {
UNCOV
952
    mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
953
  }
954

955
  return code;
43✔
956
}
957

958
int32_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
1,351✔
959
  int32_t code = 0;
1,351✔
960
  int32_t numOfStreams = taosHashGetSize(pHash);
1,351✔
961
  if (numOfStreams == 0) {
1,351✔
962
    return code;
338✔
963
  }
964

965
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
1,013✔
966
  if (code == 0) {
1,013✔
967
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
627✔
968
  } else {
969
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
386!
970
  }
971

972
  return code;
1,013✔
973
}
974

975
int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) {
2✔
976
  SChkptReportInfo *pInfo = taosHashGet(pHash, &streamId, sizeof(streamId));
2✔
977
  if (pInfo != NULL) {
2!
978
    taosArrayClear(pInfo->pTaskList);
2✔
979
    mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId,
2!
980
           pInfo->reportChkpt);
981
    return 0;
2✔
982
  }
983

UNCOV
984
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
985
}
986

987
static void mndShowStreamStatus(char *dst, int8_t status) {
33,147✔
988
  if (status == STREAM_STATUS__NORMAL) {
33,147✔
989
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
33,132✔
990
  } else if (status == STREAM_STATUS__STOP) {
15!
UNCOV
991
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
992
  } else if (status == STREAM_STATUS__FAILED) {
15!
UNCOV
993
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
994
  } else if (status == STREAM_STATUS__RECOVER) {
15!
UNCOV
995
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
996
  } else if (status == STREAM_STATUS__PAUSE) {
15!
997
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
27✔
998
  }
999
}
33,147✔
1000

1001
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
33,061✔
1002
  int8_t trigger = pStream->conf.trigger;
33,061✔
1003
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
33,061✔
1004
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
11,344✔
1005
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
21,717✔
1006
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
10,885✔
1007
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
10,832✔
1008
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
10,813✔
1009
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
19!
1010
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
70✔
1011
  }
1012
}
33,061✔
1013

1014
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
257,345✔
1015
  memset(pBuf, 0, bufLen);
257,345✔
1016
  pBuf[2] = '0';
257,345✔
1017
  pBuf[3] = 'x';
257,345✔
1018

1019
  int32_t len = tintToHex(id, &pBuf[4]);
257,345✔
1020
  varDataSetLen(pBuf, len + 2);
258,028✔
1021
}
258,028✔
1022

1023
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
33,109✔
1024
  int32_t          code = TSDB_CODE_SUCCESS;
33,109✔
1025
  int32_t          lino = 0;
33,109✔
1026
  SStreamTaskIter *pIter = NULL;
33,109✔
1027
  bool             isPaused =  true;
33,109✔
1028

1029
  taosRLockLatch(&pStream->lock);
33,109✔
1030
  code = createStreamTaskIter(pStream, &pIter);
33,202✔
1031
  TSDB_CHECK_CODE(code, lino, _end);
33,191!
1032

1033
  while (streamTaskIterNextTask(pIter)) {
145,058✔
1034
    SStreamTask *pTask = NULL;
110,079✔
1035
    code = streamTaskIterGetCurrent(pIter, &pTask);
110,079✔
1036
    TSDB_CHECK_CODE(code, lino, _end);
110,865!
1037

1038
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
110,865✔
1039
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
110,865✔
1040
    if (pe == NULL) {
111,867✔
1041
      continue;
116✔
1042
    }
1043
    if (pe->status != TASK_STATUS__PAUSE) {
111,751✔
1044
      isPaused = false;
111,724✔
1045
    }
1046
  }
1047
  (*pRes) = isPaused;
33,080✔
1048

1049
_end:
33,080✔
1050
  destroyStreamTaskIter(pIter);
33,080✔
1051
  taosRUnLockLatch(&pStream->lock);
33,180✔
1052
  if (code != TSDB_CODE_SUCCESS) {
33,187!
UNCOV
1053
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
1054
  }
1055
  return code;
33,188✔
1056
}
1057

1058
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
33,190✔
1059
  int32_t code = 0;
33,190✔
1060
  int32_t cols = 0;
33,190✔
1061
  int32_t lino = 0;
33,190✔
1062

1063
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,190✔
1064
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
33,190✔
1065
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,192✔
1066
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,153!
1067

1068
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
33,153✔
1069
  TSDB_CHECK_CODE(code, lino, _end);
33,147!
1070

1071
  // create time
1072
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,147✔
1073
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,133!
1074
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
33,133✔
1075
  TSDB_CHECK_CODE(code, lino, _end);
33,114!
1076

1077
  // stream id
1078
  char buf[128] = {0};
33,114✔
1079
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
33,114✔
1080
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,182✔
1081
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,160!
1082
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
33,160✔
1083
  TSDB_CHECK_CODE(code, lino, _end);
33,156!
1084

1085
  // related fill-history stream id
1086
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,156✔
1087
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,136✔
1088
  if (pStream->hTaskUid != 0) {
33,132!
1089
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
UNCOV
1090
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1091
  } else {
1092
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
33,132✔
1093
  }
1094
  TSDB_CHECK_CODE(code, lino, _end);
33,129!
1095

1096
  // related fill-history stream id
1097
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
33,129✔
1098
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
33,129✔
1099
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,129✔
1100
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,143!
1101
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
33,143✔
1102
  TSDB_CHECK_CODE(code, lino, _end);
33,111!
1103

1104
  char status[20 + VARSTR_HEADER_SIZE] = {0};
33,111✔
1105
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
33,111✔
1106
  bool isPaused = false;
33,111✔
1107
  code = isAllTaskPaused(pStream, &isPaused);
33,111✔
1108
  TSDB_CHECK_CODE(code, lino, _end);
33,189!
1109

1110
  int8_t streamStatus = atomic_load_8(&pStream->status);
33,189✔
1111
  if (isPaused) {
33,160✔
1112
    streamStatus = STREAM_STATUS__PAUSE;
27✔
1113
  }
1114
  mndShowStreamStatus(status2, streamStatus);
33,160✔
1115
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
33,141✔
1116
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,141✔
1117
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,123!
1118

1119
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
33,123✔
1120
  TSDB_CHECK_CODE(code, lino, _end);
33,137!
1121

1122
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,137✔
1123
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
33,137✔
1124
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,129✔
1125
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,103!
1126

1127
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
33,103✔
1128
  TSDB_CHECK_CODE(code, lino, _end);
33,125!
1129

1130
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,125✔
1131
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
33,125✔
1132
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,136✔
1133
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,113!
1134

1135
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
33,113✔
1136
  TSDB_CHECK_CODE(code, lino, _end);
33,118!
1137

1138
  if (pStream->targetSTbName[0] == 0) {
33,118✔
1139
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
1140
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
2!
1141

1142
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
2✔
1143
  } else {
1144
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,116✔
1145
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
33,116✔
1146
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,149✔
1147
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,090!
1148

1149
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
33,090✔
1150
  }
1151
  TSDB_CHECK_CODE(code, lino, _end);
33,090!
1152

1153
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,090✔
1154
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,061!
1155

1156
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
33,061✔
1157
  TSDB_CHECK_CODE(code, lino, _end);
33,049!
1158

1159
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
33,049✔
1160
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
33,049✔
1161
  mndShowStreamTrigger(trigger2, pStream);
33,049✔
1162
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
33,089✔
1163
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,089✔
1164
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,129!
1165

1166
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
33,129✔
1167
  TSDB_CHECK_CODE(code, lino, _end);
33,130!
1168

1169
  // sink_quota
1170
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
33,130✔
1171
  sinkQuota[0] = '0';
33,130✔
1172
  char dstStr[20] = {0};
33,130✔
1173
  STR_TO_VARSTR(dstStr, sinkQuota)
33,130✔
1174
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,130✔
1175
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,156!
1176

1177
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,156✔
1178
  TSDB_CHECK_CODE(code, lino, _end);
33,124!
1179

1180
  // checkpoint interval
1181
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
33,124✔
1182
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
33,124✔
1183
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
33,185✔
1184

1185
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,185✔
1186
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,149!
1187

1188
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
33,149✔
1189
  TSDB_CHECK_CODE(code, lino, _end);
33,110!
1190

1191
  // checkpoint backup type
1192
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
33,110✔
1193
  STR_TO_VARSTR(backup, "none")
33,110✔
1194
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,110✔
1195
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,087!
1196

1197
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
33,087✔
1198
  TSDB_CHECK_CODE(code, lino, _end);
33,132!
1199

1200
  // history scan idle
1201
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
33,132✔
1202
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
33,132✔
1203

1204
  memset(dstStr, 0, tListLen(dstStr));
33,132✔
1205
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
33,132✔
1206
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,132✔
1207
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,134!
1208

1209
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,134✔
1210

1211
_end:
33,134✔
1212
  if (code) {
33,134!
UNCOV
1213
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1214
  }
1215
  return code;
33,136✔
1216
}
1217

1218
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
223,656✔
1219
                              int32_t precision) {
1220
  SColumnInfoData *pColInfo = NULL;
223,656✔
1221
  int32_t          cols = 0;
223,656✔
1222
  int32_t          code = 0;
223,656✔
1223
  int32_t          lino = 0;
223,656✔
1224

1225
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
223,656✔
1226

1227
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
223,656✔
1228
  if (pe == NULL) {
224,413!
UNCOV
1229
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1230
           " no valid status/stage info",
1231
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
UNCOV
1232
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1233
  }
1234

1235
  // stream name
1236
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
224,413✔
1237
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
224,413✔
1238

1239
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,485✔
1240
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,025!
1241

1242
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
224,025✔
1243
  TSDB_CHECK_CODE(code, lino, _end);
223,584!
1244

1245
  // task id
1246
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,584✔
1247
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,347!
1248

1249
  char idstr[128] = {0};
223,347✔
1250
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
223,347✔
1251
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
224,451✔
1252
  TSDB_CHECK_CODE(code, lino, _end);
223,701!
1253

1254
  // node type
1255
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
223,701✔
1256
  varDataSetLen(nodeType, 5);
223,701✔
1257
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,701✔
1258
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,507!
1259

1260
  if (pTask->info.nodeId > 0) {
223,664✔
1261
    memcpy(varDataVal(nodeType), "vnode", 5);
205,616✔
1262
  } else {
1263
    memcpy(varDataVal(nodeType), "snode", 5);
18,048✔
1264
  }
1265
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
223,664✔
1266
  TSDB_CHECK_CODE(code, lino, _end);
223,607!
1267

1268
  // node id
1269
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,607✔
1270
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,380!
1271

1272
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
223,380✔
1273
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
223,380✔
1274
  TSDB_CHECK_CODE(code, lino, _end);
223,138!
1275

1276
  // level
1277
  char level[20 + VARSTR_HEADER_SIZE] = {0};
223,138✔
1278
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
223,138✔
1279
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
114,724✔
1280
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
108,414✔
1281
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
19,508✔
1282
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
88,906!
1283
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
89,649✔
1284
  }
1285

1286
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,138✔
1287
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,043!
1288

1289
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
223,043✔
1290
  TSDB_CHECK_CODE(code, lino, _end);
223,544!
1291

1292
  // status
1293
  char status[20 + VARSTR_HEADER_SIZE] = {0};
223,544✔
1294

1295
  const char *pStatus = streamTaskGetStatusStr(pe->status);
223,544✔
1296
  STR_TO_VARSTR(status, pStatus);
223,623✔
1297

1298
  // status
1299
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,623✔
1300
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,571!
1301

1302
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
223,571✔
1303
  TSDB_CHECK_CODE(code, lino, _end);
223,534!
1304

1305
  // stage
1306
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,534✔
1307
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,379!
1308

1309
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
223,379✔
1310
  TSDB_CHECK_CODE(code, lino, _end);
223,087!
1311

1312
  // input queue
1313
  char        vbuf[TSDB_STREAM_NOTIFY_STAT_LEN + 2] = {0};
223,087✔
1314
  char        buf[TSDB_STREAM_NOTIFY_STAT_LEN] = {0};
223,087✔
1315
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
223,087✔
1316
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
223,087✔
1317
  STR_TO_VARSTR(vbuf, buf);
223,087✔
1318

1319
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,087✔
1320
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,018!
1321

1322
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,018✔
1323
  TSDB_CHECK_CODE(code, lino, _end);
223,654!
1324

1325
  // input total
1326
  const char *formatTotalMb = "%7.2f MiB";
223,654✔
1327
  const char *formatTotalGb = "%7.2f GiB";
223,654✔
1328
  if (pe->procsTotal < 1024) {
223,654!
1329
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
223,661✔
1330
  } else {
UNCOV
1331
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1332
  }
1333

1334
  memset(vbuf, 0, tListLen(vbuf));
223,654✔
1335
  STR_TO_VARSTR(vbuf, buf);
223,654✔
1336

1337
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,654✔
1338
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,050!
1339

1340
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,050✔
1341
  TSDB_CHECK_CODE(code, lino, _end);
223,676!
1342

1343
  // process throughput
1344
  const char *formatKb = "%7.2f KiB/s";
223,676✔
1345
  const char *formatMb = "%7.2f MiB/s";
223,676✔
1346
  if (pe->procsThroughput < 1024) {
223,676✔
1347
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
223,499✔
1348
  } else {
1349
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
177✔
1350
  }
1351

1352
  memset(vbuf, 0, tListLen(vbuf));
223,676✔
1353
  STR_TO_VARSTR(vbuf, buf);
223,676✔
1354

1355
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,676✔
1356
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,065!
1357

1358
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,065✔
1359
  TSDB_CHECK_CODE(code, lino, _end);
223,683!
1360

1361
  // output total
1362
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,683✔
1363
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,420!
1364

1365
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
223,876✔
1366
    colDataSetNULL(pColInfo, numOfRows);
89,694!
1367
  } else {
1368
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
134,182✔
1369
    memset(vbuf, 0, tListLen(vbuf));
134,635✔
1370
    STR_TO_VARSTR(vbuf, buf);
134,635✔
1371

1372
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
134,635✔
1373
    TSDB_CHECK_CODE(code, lino, _end);
134,343!
1374
  }
1375

1376
  // output throughput
1377
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,037✔
1378
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,552!
1379

1380
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
223,967✔
1381
    colDataSetNULL(pColInfo, numOfRows);
89,690!
1382
  } else {
1383
    if (pe->outputThroughput < 1024) {
134,277✔
1384
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
134,157✔
1385
    } else {
1386
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
120✔
1387
    }
1388

1389
    memset(vbuf, 0, tListLen(vbuf));
134,277✔
1390
    STR_TO_VARSTR(vbuf, buf);
134,277✔
1391

1392
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
134,277✔
1393
    TSDB_CHECK_CODE(code, lino, _end);
134,339!
1394
  }
1395
  // info
1396
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
224,029✔
1397
    const char *sinkStr = "%.2f MiB";
89,696✔
1398
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
89,696✔
1399
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
134,333✔
1400
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
114,892✔
1401
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
5,885✔
1402
      if (ret != 0) {
5,885!
1403
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
UNCOV
1404
        memset(buf, 0, tListLen(buf));
×
1405
      }
1406
    } else {
1407
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
109,007✔
1408
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
109,007✔
1409
    }
1410
  } else {
1411
    memset(buf, 0, tListLen(buf));
19,441✔
1412
  }
1413

1414
  STR_TO_VARSTR(vbuf, buf);
224,029✔
1415

1416
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,029✔
1417
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,044!
1418

1419
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,044✔
1420
  TSDB_CHECK_CODE(code, lino, _end);
223,552!
1421

1422
  // start_time
1423
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,552✔
1424
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,339!
1425

1426
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
223,339✔
1427
  TSDB_CHECK_CODE(code, lino, _end);
222,833!
1428

1429
  // start id
1430
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,833✔
1431
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,607!
1432

1433
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
222,607✔
1434
  TSDB_CHECK_CODE(code, lino, _end);
222,596!
1435

1436
  // start ver
1437
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,596✔
1438
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,457!
1439

1440
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
222,457✔
1441
  TSDB_CHECK_CODE(code, lino, _end);
222,628!
1442

1443
  // checkpoint time
1444
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,628✔
1445
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,481!
1446

1447
  if (pe->checkpointInfo.latestTime != 0) {
222,538✔
1448
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
180,338✔
1449
  } else {
1450
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
42,200✔
1451
  }
1452
  TSDB_CHECK_CODE(code, lino, _end);
222,642!
1453

1454
  // checkpoint_id
1455
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,642✔
1456
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,527!
1457

1458
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
222,527✔
1459
  TSDB_CHECK_CODE(code, lino, _end);
222,631!
1460

1461
  // checkpoint version
1462
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,631✔
1463
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,495!
1464

1465
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
222,495✔
1466
  TSDB_CHECK_CODE(code, lino, _end);
222,683!
1467

1468
  // checkpoint size
1469
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,683✔
1470
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,541!
1471

1472
  colDataSetNULL(pColInfo, numOfRows);
222,629!
1473

1474
  // checkpoint backup status
1475
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,629✔
1476
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,785!
1477

1478
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
222,785✔
1479
  TSDB_CHECK_CODE(code, lino, _end);
222,632!
1480

1481
  // ds_err_info
1482
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,632✔
1483
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,466!
1484

1485
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
222,466✔
1486
  TSDB_CHECK_CODE(code, lino, _end);
222,119!
1487

1488
  // history_task_id
1489
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,119✔
1490
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
221,877!
1491

1492
  if (pe->hTaskId != 0) {
221,910✔
1493
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
367✔
1494
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
367✔
1495
  } else {
1496
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
221,543✔
1497
  }
1498
  TSDB_CHECK_CODE(code, lino, _end);
221,910!
1499

1500
  // history_task_status
1501
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
221,910✔
1502
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
221,815!
1503

1504
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
221,815✔
1505
  TSDB_CHECK_CODE(code, lino, _end);
222,129!
1506

1507
  // notify_event_stat
1508
  int32_t offset =0;
222,129✔
1509
  if (pe->notifyEventStat.notifyEventAddTimes > 0) {
222,129!
UNCOV
1510
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Add %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1511
                        pe->notifyEventStat.notifyEventAddTimes, pe->notifyEventStat.notifyEventAddElems,
1512
                        pe->notifyEventStat.notifyEventAddCostSec);
1513
  }
1514
  if (pe->notifyEventStat.notifyEventPushTimes > 0) {
222,129!
UNCOV
1515
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Push %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1516
                        pe->notifyEventStat.notifyEventPushTimes, pe->notifyEventStat.notifyEventPushElems,
1517
                        pe->notifyEventStat.notifyEventPushCostSec);
1518
  }
1519
  if (pe->notifyEventStat.notifyEventPackTimes > 0) {
222,129!
UNCOV
1520
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Pack %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1521
                        pe->notifyEventStat.notifyEventPackTimes, pe->notifyEventStat.notifyEventPackElems,
1522
                        pe->notifyEventStat.notifyEventPackCostSec);
1523
  }
1524
  if (pe->notifyEventStat.notifyEventSendTimes > 0) {
222,129!
UNCOV
1525
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Send %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1526
                        pe->notifyEventStat.notifyEventSendTimes, pe->notifyEventStat.notifyEventSendElems,
1527
                        pe->notifyEventStat.notifyEventSendCostSec);
1528
  }
1529
  if (pe->notifyEventStat.notifyEventHoldElems > 0) {
222,129!
UNCOV
1530
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "[Hold %" PRId64 " elems] ",
×
1531
                        pe->notifyEventStat.notifyEventHoldElems);
1532
  }
1533
  TSDB_CHECK_CONDITION(offset < sizeof(buf), code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
222,129!
1534
  buf[offset] = '\0';
222,129✔
1535

1536
  STR_TO_VARSTR(vbuf, buf);
222,129✔
1537

1538
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,129✔
1539
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,156!
1540

1541
  if (offset == 0) {
224,169!
1542
    colDataSetNULL(pColInfo, numOfRows);
224,169!
1543
  } else {
1544
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
UNCOV
1545
    TSDB_CHECK_CODE(code, lino, _end);
×
1546
  }
1547

UNCOV
1548
_end:
×
1549
  if (code) {
224,169!
UNCOV
1550
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1551
  }
1552
  return code;
224,149✔
1553
}
1554

1555
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
13,467✔
1556
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
13,467✔
1557
  const SEp *p = GET_ACTIVE_EP(pCurrent);
13,467✔
1558

1559
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
13,467!
1560
    return false;
13,467✔
1561
  }
UNCOV
1562
  return true;
×
1563
}
1564

1565
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
2,823✔
1566
  if (pInfo != NULL) {
2,823!
1567
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,823✔
1568
    taosHashCleanup(pInfo->pDBMap);
2,823✔
1569
  }
1570
}
2,823✔
1571

1572
// 1. increase the replica does not affect the stream process.
1573
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1574
// tasks on the will be removed replica.
1575
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1576
// will handle it as mentioned in 1 & 2 items.
1577
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
2,823✔
1578
                               SVgroupChangeInfo *pInfo) {
1579
  int32_t code = 0;
2,823✔
1580
  int32_t lino = 0;
2,823✔
1581

1582
  if (pInfo == NULL) {
2,823!
UNCOV
1583
    return TSDB_CODE_INVALID_PARA;
×
1584
  }
1585

1586
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
2,823✔
1587
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
2,823✔
1588

1589
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
2,823!
1590
    mndDestroyVgroupChangeInfo(pInfo);
×
UNCOV
1591
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1592
  }
1593

1594
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,823✔
1595
  for (int32_t i = 0; i < numOfNodes; ++i) {
17,033✔
1596
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
14,210✔
1597
    if (pPrevEntry == NULL) {
14,210!
UNCOV
1598
      continue;
×
1599
    }
1600

1601
    int32_t num = taosArrayGetSize(pNodeList);
14,210✔
1602
    for (int32_t j = 0; j < num; ++j) {
171,948✔
1603
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
171,219✔
1604
      if (pCurrent == NULL) {
171,219!
UNCOV
1605
        continue;
×
1606
      }
1607

1608
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
171,219✔
1609
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
13,481!
1610
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
14✔
1611

1612
          char buf[256] = {0};
14✔
1613
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
14✔
1614
          if (code) {
14!
1615
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
UNCOV
1616
            TSDB_CHECK_CODE(code, lino, _err);
×
1617
          }
1618

1619
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
14✔
1620
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1621

1622
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
14✔
1623
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
14✔
1624
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
14✔
1625

1626
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
14✔
1627
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
14!
1628
        }
1629

1630
        // todo handle the snode info
1631
        if (pCurrent->nodeId != SNODE_HANDLE) {
13,481✔
1632
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
11,806✔
1633
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
11,806✔
1634
          mndReleaseVgroup(pMnode, pVgroup);
11,806✔
1635
          TSDB_CHECK_CODE(code, lino, _err);
11,806!
1636
        }
1637

1638
        break;
13,481✔
1639
      }
1640
    }
1641
  }
1642

1643
  return code;
2,823✔
1644

1645
_err:
×
1646
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1647
  mndDestroyVgroupChangeInfo(pInfo);
×
UNCOV
1648
  return code;
×
1649
}
1650

1651
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
2,133✔
1652
  bool              allReady = false;
2,133✔
1653
  bool              nodeUpdated = false;
2,133✔
1654
  SVgroupChangeInfo changeInfo = {0};
2,133✔
1655

1656
  int32_t numOfNodes = extractStreamNodeList(pMnode);
2,133✔
1657

1658
  if (numOfNodes == 0) {
2,133✔
1659
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
785✔
1660
    execInfo.ts = taosGetTimestampSec();
785✔
1661
    return false;
785✔
1662
  }
1663

1664
  for (int32_t i = 0; i < numOfNodes; ++i) {
7,991✔
1665
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
6,649✔
1666
    if (pNodeEntry == NULL) {
6,649!
UNCOV
1667
      continue;
×
1668
    }
1669

1670
    if (pNodeEntry->stageUpdated) {
6,649✔
1671
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
6✔
1672
      return true;
6✔
1673
    }
1674
  }
1675

1676
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot);
1,342✔
1677
  if (code) {
1,342!
UNCOV
1678
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1679
  }
1680

1681
  if (!allReady) {
1,342✔
1682
    mWarn("not all vnodes ready, quit from vnodes status check");
8!
1683
    return true;
8✔
1684
  }
1685

1686
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
1,334✔
1687
  if (code) {
1,334!
UNCOV
1688
    nodeUpdated = false;
×
1689
  } else {
1690
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
1,334✔
1691
    if (nodeUpdated) {
1,334!
UNCOV
1692
      mDebug("stream tasks not ready due to node update");
×
1693
    }
1694
  }
1695

1696
  mndDestroyVgroupChangeInfo(&changeInfo);
1,334✔
1697
  return nodeUpdated;
1,334✔
1698
}
1699

1700
// check if the node update happens or not
1701
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
2,133✔
1702
  SArray *pNodeSnapshot = NULL;
2,133✔
1703

1704
  streamMutexLock(&execInfo.lock);
2,133✔
1705
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
2,133✔
1706
  streamMutexUnlock(&execInfo.lock);
2,133✔
1707

1708
  taosArrayDestroy(pNodeSnapshot);
2,133✔
1709
  return updated;
2,133✔
1710
}
1711

1712
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,742✔
1713
  SSdb      *pSdb = pMnode->pSdb;
1,742✔
1714
  void      *pIter = NULL;
1,742✔
1715
  SSnodeObj *pObj = NULL;
1,742✔
1716

1717
  if (pSrcDb->cfg.replications == 1) {
1,742✔
1718
    return TSDB_CODE_SUCCESS;
1,739✔
1719
  } else {
1720
    while (1) {
1721
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
3✔
1722
      if (pIter == NULL) {
3✔
1723
        break;
2✔
1724
      }
1725

1726
      sdbRelease(pSdb, pObj);
1✔
1727
      sdbCancelFetch(pSdb, pIter);
1✔
1728
      return TSDB_CODE_SUCCESS;
1✔
1729
    }
1730

1731
    mError("snode not existed when trying to create stream in db with multiple replica");
2!
1732
    return TSDB_CODE_SNODE_NOT_DEPLOYED;
2✔
1733
  }
1734
}
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