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

taosdata / TDengine / #3632

08 Mar 2025 06:17AM UTC coverage: 60.719% (+0.05%) from 60.671%
#3632

push

travis-ci

web-flow
Merge pull request #29999 from taosdata/enh/TS-5089

feat: taosBenchmark supports exporting to CSV files

141890 of 300701 branches covered (47.19%)

Branch coverage included in aggregate %.

599 of 766 new or added lines in 3 files covered. (78.2%)

1025 existing lines in 124 files now uncovered.

223757 of 301490 relevant lines covered (74.22%)

17284906.68 hits per line

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

72.62
/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,964✔
35
  *pIter = taosMemoryCalloc(1, sizeof(SStreamTaskIter));
106,964!
36
  if (*pIter == NULL) {
106,951!
37
    return terrno;
×
38
  }
39

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

46
  return 0;
106,944✔
47
}
48

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

55
  if (pIter->level == -1) {
485,833✔
56
    pIter->level += 1;
106,941✔
57
  }
58

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

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

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

77
int32_t streamTaskIterGetCurrent(SStreamTaskIter *pIter, SStreamTask **pTask) {
377,911✔
78
  if (pTask) {
377,911!
79
    *pTask = pIter->pTask;
377,947✔
80
    if (*pTask != NULL) {
377,947✔
81
      return TSDB_CODE_SUCCESS;
377,922✔
82
    }
83
  }
84

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

90
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
262,335✔
91
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
528,687✔
92
    if (!pVgroup->vnodeGid[i].syncRestore) {
267,547✔
93
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,190!
94
      return false;
1,190✔
95
    }
96

97
    ESyncState state = pVgroup->vnodeGid[i].syncState;
266,357✔
98
    if (state == TAOS_SYNC_STATE_OFFLINE || state == TAOS_SYNC_STATE_ERROR || state == TAOS_SYNC_STATE_LEARNER ||
266,357!
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,
5!
101
            state);
102
      return false;
5✔
103
    }
104
  }
105

106
  return true;
261,140✔
107
}
108

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

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

120
    SNodeEntry entry = {.nodeId = SNODE_HANDLE};
7,234✔
121
    code = addEpIntoEpSet(&entry.epset, pObj->pDnode->fqdn, pObj->pDnode->port);
7,234✔
122
    if (code) {
7,234!
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,234✔
130
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
7,234✔
131
    if (code != 0) {  // print error and continue
7,234!
132
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
133
    }
134

135
    void *p = taosArrayPush(pVgroupList, &entry);
7,234✔
136
    if (p == NULL) {
7,234!
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,234✔
144
    }
145

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

149
  return code;
15,135✔
150
}
151

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

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

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

171
    if (objStatus != SDB_STATUS_READY) {
15,659✔
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);
15,657✔
179
  }
180

181
  return TSDB_CODE_SUCCESS;
15,018✔
182
}
183

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

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

197
  while (1) {
264,884✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
280,019✔
199
    if (pIter == NULL) {
280,019✔
200
      break;
15,135✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
264,884✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
264,884✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
131,937✔
209
      if (code) {
131,937!
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) {
132,947✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
436!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
436✔
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) {
264,884✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
262,335✔
227
    }
228

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

235
    void *p = taosArrayPush(pVgroupList, &entry);
264,884✔
236
    if (p == NULL) {
264,884!
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);
264,884✔
244
    }
245

246
    sdbRelease(pSdb, pVgroup);
264,884✔
247
  }
248

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

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

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

261
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
15,135✔
262
  if (pVgroupList == NULL) {
15,135!
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,135✔
270
  if (code) {
15,135!
271
    goto _err;
×
272
  }
273

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

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

286
  *pList = pVgroupList;
15,135✔
287
  return code;
15,135✔
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,832✔
296
  void *pIter = NULL;
12,832✔
297
  SSdb *pSdb = pMnode->pSdb;
12,832✔
298
  *pStream = NULL;
12,832✔
299

300
  SStreamObj *p = NULL;
12,832✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
21,880✔
302
    if (p->uid == streamId) {
21,874✔
303
      sdbCancelFetch(pSdb, pIter);
12,826✔
304
      *pStream = p;
12,826✔
305
      return TSDB_CODE_SUCCESS;
12,826✔
306
    }
307
    sdbRelease(pSdb, p);
9,048✔
308
  }
309

310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
6✔
311
}
312

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

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

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

335
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
309✔
336
    if (pIter != NULL) {
309!
337
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
309✔
338
      sdbRelease(pMnode->pSdb, pObj);
309✔
339
      sdbCancelFetch(pMnode->pSdb, pIter);
309✔
340
      if (code) {
309!
341
        *hasEpset = false;
×
342
        mError("failed to set epset");
×
343
      } else {
344
        *hasEpset = true;
309✔
345
      }
346
      return code;
309✔
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,597✔
353
    if (pVgObj != NULL) {
19,597!
354
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
19,597✔
355
      mndReleaseVgroup(pMnode, pVgObj);
19,597✔
356

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

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

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

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

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

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

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

402
  return num;
77,144✔
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,560✔
432
  SArray **pList = (SArray **)param;
1,560✔
433
  taosArrayDestroy(*pList);
1,560✔
434
}
1,560✔
435

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

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

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

453
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
1,641!
454
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
1,641!
455
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
1,641!
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,641✔
461
  execInfo.switchFromFollower = false;
1,641✔
462

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

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

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

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

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

490
      if (pEntry->nodeId == p->nodeId) {
103,883✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
7,488✔
492

493
        void *px = taosArrayPush(pValidList, p);
7,488✔
494
        if (px == NULL) {
7,488!
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,488✔
498
        }
499
        break;
7,488✔
500
      }
501
    }
502
  }
503

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,136✔
538
}
539

540
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
1,482✔
541
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
1,482!
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,482✔
553

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

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

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

573
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,266✔
574
    code = doRemoveTasks(pExecNode, &id);
7,266✔
575
    if (code) {
7,266!
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,343!
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,343✔
589
  if (code) {
1,343!
590
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
591
  }
592

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

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

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

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

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

616
  return false;
×
617
}
618

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

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

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

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

641
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
15,530✔
642
    if (!existed) {
15,530!
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,482✔
651

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,482✔
656

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

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

666
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
1,191✔
667
  if (existed != numOfTasks) {
1,191✔
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,656✔
675
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
5,497✔
676
    if (pInfo == NULL) {
5,497!
677
      continue;
×
678
    }
679

680
    if (checkpointId == -1) {
5,497✔
681
      checkpointId = pInfo->checkpointId;
1,159✔
682
      transId = pInfo->transId;
1,159✔
683
      taskId = pInfo->taskId;
1,159✔
684
    } else if (checkpointId != pInfo->checkpointId) {
4,338!
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,159✔
695
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
1,159✔
696
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
1,159✔
697

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

704
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
174!
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) {
985✔
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,158✔
722
         pName, numOfTasks);
723

724
  return TSDB_CODE_SUCCESS;
1,158✔
725
}
726

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

736
  mDebug("start to scan checkpoint report info");
29,006✔
737

738
  streamMutexLock(&execInfo.lock);
29,006✔
739

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
95,984✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
68,136✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
68,136✔
743
      continue;
66,945✔
744
    }
745

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

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

762
    int32_t total = mndGetNumOfStreamTasks(pStream);
1,191✔
763
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
1,191✔
764
    if (ret == 0) {
1,191✔
765
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,158✔
766
      if (code == 0) {
1,158!
767
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,158✔
768
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,158!
769
          taosArrayClear(px->pTaskList);
1,158✔
770
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
1,158!
771
                " to %" PRId64,
772
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
773
          px->reportChkpt = pInfo->checkpointId;
1,158✔
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,158✔
780
        break;
1,158✔
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);
29,006✔
790
  if (size > 0) {
29,006✔
791
    for (int32_t i = 0; i < size; ++i) {
4✔
792
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
3✔
793
      if (pStreamId == NULL) {
3!
794
        continue;
×
795
      }
796

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

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

807
  streamMutexUnlock(&execInfo.lock);
29,006✔
808

809
  taosArrayDestroy(pDropped);
29,006✔
810

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

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

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

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

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

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

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

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

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

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

867
  return TSDB_CODE_ACTION_IN_PROGRESS;
215✔
868
}
869

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

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

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

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

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

897
  return code;
47✔
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) {
236✔
903
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
236✔
904
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
236✔
905

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

913
    if (p->req.taskId == info.req.taskId) {
603✔
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);
224✔
926
  if (p == NULL) {
224!
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);
224✔
930
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
224✔
931
           " waiting tasks:%d",
932
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
933
  }
934
}
935

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

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

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

955
  return code;
45✔
956
}
957

958
int32_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
1,343✔
959
  int32_t code = 0;
1,343✔
960
  int32_t numOfStreams = taosHashGetSize(pHash);
1,343✔
961
  if (numOfStreams == 0) {
1,343✔
962
    return code;
330✔
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);
635✔
968
  } else {
969
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
378!
970
  }
971

972
  return code;
1,013✔
973
}
974

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

984
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
985
}
986

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1138
  if (pStream->targetSTbName[0] == 0) {
33,162✔
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,160✔
1145
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
33,160✔
1146
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,159✔
1147
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,123!
1148

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1227
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
222,884✔
1228
  if (pe == NULL) {
223,476!
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);
1232
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1233
  }
1234

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

1239
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,419✔
1240
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,042!
1241

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

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

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

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

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

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

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

1276
  // level
1277
  char level[20 + VARSTR_HEADER_SIZE] = {0};
222,671✔
1278
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
222,671✔
1279
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
114,433✔
1280
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
108,238✔
1281
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
19,483✔
1282
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
88,755!
1283
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
89,321✔
1284
  }
1285

1286
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,671✔
1287
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,613!
1288

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1414
  STR_TO_VARSTR(vbuf, buf);
223,492✔
1415

1416
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,492✔
1417
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,410!
1418

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

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

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

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

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

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

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

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

1447
  if (pe->checkpointInfo.latestTime != 0) {
222,455✔
1448
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
181,051✔
1449
  } else {
1450
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
41,404✔
1451
  }
1452
  TSDB_CHECK_CODE(code, lino, _end);
222,511!
1453

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1507
  // notify_event_stat
1508
  int32_t offset =0;
222,782✔
1509
  if (pe->notifyEventStat.notifyEventAddTimes > 0) {
222,782!
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,782!
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,782!
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,782!
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,782!
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,782!
1534
  buf[offset] = '\0';
222,782✔
1535

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

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

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

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

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

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

1565
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
2,810✔
1566
  if (pInfo != NULL) {
2,810!
1567
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,810✔
1568
    taosHashCleanup(pInfo->pDBMap);
2,810✔
1569
  }
1570
}
2,810✔
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,810✔
1578
                               SVgroupChangeInfo *pInfo) {
1579
  int32_t code = 0;
2,810✔
1580
  int32_t lino = 0;
2,810✔
1581

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

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

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

1594
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,810✔
1595
  for (int32_t i = 0; i < numOfNodes; ++i) {
16,814✔
1596
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
14,004✔
1597
    if (pPrevEntry == NULL) {
14,004!
1598
      continue;
×
1599
    }
1600

1601
    int32_t num = taosArrayGetSize(pNodeList);
14,004✔
1602
    for (int32_t j = 0; j < num; ++j) {
170,977✔
1603
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
170,228✔
1604
      if (pCurrent == NULL) {
170,228!
1605
        continue;
×
1606
      }
1607

1608
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
170,228✔
1609
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
13,255!
1610
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
16✔
1611

1612
          char buf[256] = {0};
16✔
1613
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
16✔
1614
          if (code) {
16!
1615
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
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,
16✔
1620
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1621

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

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

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

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

1643
  return code;
2,810✔
1644

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1717
  if (pSrcDb->cfg.replications == 1) {
1,751✔
1718
    return TSDB_CODE_SUCCESS;
1,748✔
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