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

taosdata / TDengine / #3661

17 Mar 2025 05:39AM UTC coverage: 62.007% (-0.03%) from 62.039%
#3661

push

travis-ci

web-flow
tests: add tdb ut (#30093)

* fix: compile warnings

* tests: add tdb ut

* test(tdb): fix return code

* test: recover ut

* fix: minor changes

* fix: enable test

* fix: ut errors

---------

Co-authored-by: Minglei Jin <mljin@taosdata.com>

153829 of 317582 branches covered (48.44%)

Branch coverage included in aggregate %.

240310 of 318051 relevant lines covered (75.56%)

19602636.8 hits per line

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

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

40
  (*pIter)->level = -1;
109,530✔
41
  (*pIter)->ordinalIndex = 0;
109,530✔
42
  (*pIter)->pStream = pStream;
109,530✔
43
  (*pIter)->totalLevel = taosArrayGetSize(pStream->pTaskList);
109,530✔
44
  (*pIter)->pTask = NULL;
109,506✔
45

46
  return 0;
109,506✔
47
}
48

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

55
  if (pIter->level == -1) {
496,577✔
56
    pIter->level += 1;
109,505✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
711,191✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->pTaskList, pIter->level);
601,362✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
600,319✔
62
      pIter->level += 1;
214,614✔
63
      pIter->ordinalIndex = 0;
214,614✔
64
      pIter->pTask = NULL;
214,614✔
65
      continue;
214,614✔
66
    }
67

68
    pIter->pTask = taosArrayGetP(pList, pIter->ordinalIndex);
386,717✔
69
    pIter->ordinalIndex += 1;
386,471✔
70
    return true;
386,471✔
71
  }
72

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

77
int32_t streamTaskIterGetCurrent(SStreamTaskIter *pIter, SStreamTask **pTask) {
386,559✔
78
  if (pTask) {
386,559!
79
    *pTask = pIter->pTask;
386,570✔
80
    if (*pTask != NULL) {
386,570✔
81
      return TSDB_CODE_SUCCESS;
386,553✔
82
    }
83
  }
84

85
  return TSDB_CODE_INVALID_PARA;
6✔
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
109,357!
89

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

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

106
  return true;
265,505✔
107
}
108

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

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

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

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

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

149
  return code;
15,610✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
15,483✔
182
}
183

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

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

197
  while (1) {
270,443✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
286,053✔
199
    if (pIter == NULL) {
286,053✔
200
      break;
15,610✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
270,443✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
270,443✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
134,467✔
209
      if (code) {
134,467!
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) {
135,976✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
364!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
364✔
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) {
270,443✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
266,749✔
227
    }
228

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

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

246
    sdbRelease(pSdb, pVgroup);
270,443✔
247
  }
248

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

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

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

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

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

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

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

300
  SStreamObj *p = NULL;
12,583✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
21,499✔
302
    if (p->uid == streamId) {
21,496✔
303
      sdbCancelFetch(pSdb, pIter);
12,580✔
304
      *pStream = p;
12,580✔
305
      return TSDB_CODE_SUCCESS;
12,580✔
306
    }
307
    sdbRelease(pSdb, p);
8,916✔
308
  }
309

310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
3✔
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,654✔
328
  *hasEpset = false;
19,654✔
329

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

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

357
      epsetAssign(pEpSet, &epset);
19,363✔
358
      *hasEpset = true;
19,363✔
359
      return TSDB_CODE_SUCCESS;
19,363✔
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) {
218✔
368
  *pTask = NULL;
218✔
369

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

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

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

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

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
77,414✔
396
  int32_t num = 0;
77,414✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->pTaskList); ++i) {
230,226✔
398
    SArray *pLevel = taosArrayGetP(pStream->pTaskList, i);
152,754✔
399
    num += taosArrayGetSize(pLevel);
152,827✔
400
  }
401

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,188✔
538
}
539

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

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

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

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

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

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

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

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

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

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

616
  return false;
×
617
}
618

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

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

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

637
    if (pEntry->nodeId == SNODE_HANDLE) {
15,944✔
638
      continue;
888✔
639
    }
640

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

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,431✔
656

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

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

666
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
1,152✔
667
  if (existed != numOfTasks) {
1,152✔
668
    mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pReportInfo->streamId, pName,
18✔
669
           existed, numOfTasks, numOfTasks - existed);
670
    return -1;
18✔
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,527✔
675
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
5,393✔
676
    if (pInfo == NULL) {
5,393!
677
      continue;
×
678
    }
679

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

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

704
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
187!
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) {
947!
714
      mError("stream:0x%" PRIx64
×
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;
×
718
    }
719
  }
720

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

724
  return TSDB_CODE_SUCCESS;
1,134✔
725
}
726

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

736
  mDebug("start to scan checkpoint report info");
27,696✔
737

738
  streamMutexLock(&execInfo.lock);
27,696✔
739

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
91,099✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
64,537✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
64,537✔
743
      continue;
63,385✔
744
    }
745

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

751
    SStreamObj *pStream = NULL;
1,152✔
752
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
1,152✔
753
    if (pStream == NULL || code != 0) {
1,152!
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,152✔
763
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
1,152✔
764
    if (ret == 0) {
1,152✔
765
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,134✔
766
      if (code == 0) {
1,134!
767
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,134✔
768
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,134!
769
          taosArrayClear(px->pTaskList);
1,134✔
770
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
1,134!
771
                " to %" PRId64,
772
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
773
          px->reportChkpt = pInfo->checkpointId;
1,134✔
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,134✔
780
        break;
1,134✔
781
      } else {
782
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
783
      }
784
    }
785

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

789
  int32_t size = taosArrayGetSize(pDropped);
27,696✔
790
  if (size > 0) {
27,696!
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);
27,696✔
808

809
  taosArrayDestroy(pDropped);
27,696✔
810

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

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

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

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

828
  STaskId id = {.streamId = pStream->uid, .taskId = taskId};
218✔
829
  code = mndGetStreamTask(&id, pStream, &pTask);
218✔
830
  if (code) {
218!
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);
218✔
837
  if (code) {
218!
838
    sdbRelease(pMnode->pSdb, pStream);
×
839
    return code;
×
840
  }
841

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

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

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

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

867
  return TSDB_CODE_ACTION_IN_PROGRESS;
218✔
868
}
869

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

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

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

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

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

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

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

913
    if (p->req.taskId == info.req.taskId) {
610✔
914
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update ts %" PRId64
14✔
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;
14✔
919
      p->req.checkpointId = info.req.checkpointId;
14✔
920
      p->req.transId = info.req.transId;
14✔
921
      return;
14✔
922
    }
923
  }
924

925
  void *p = taosArrayPush(pInfo->pTaskList, &info);
218✔
926
  if (p == NULL) {
218!
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);
218✔
930
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
218✔
931
           " waiting tasks:%d",
932
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
933
  }
934
}
935

936
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
46✔
937
  taosArrayDestroy(pInfo->pTaskList);
46✔
938
  pInfo->pTaskList = NULL;
46✔
939
}
46✔
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,348✔
946
  }
947

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

955
  return code;
46✔
956
}
957

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

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

972
  return code;
1,010✔
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) {
35,333✔
988
  if (status == STREAM_STATUS__NORMAL) {
35,333✔
989
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
35,312✔
990
  } else if (status == STREAM_STATUS__STOP) {
21!
991
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
992
  } else if (status == STREAM_STATUS__FAILED) {
21✔
993
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
1✔
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);
24✔
998
  } else if (status == STREAM_STATUS__INIT) {
×
999
    tstrncpy(dst, "init", MND_STREAM_TRIGGER_NAME_SIZE);
2✔
1000
  }
1001
}
35,333✔
1002

1003
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
35,218✔
1004
  int8_t trigger = pStream->conf.trigger;
35,218✔
1005
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
35,218✔
1006
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
12,064✔
1007
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
23,154✔
1008
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
11,589✔
1009
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
11,565✔
1010
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
11,547✔
1011
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
18!
1012
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
70✔
1013
  }
1014
}
35,218✔
1015

1016
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
260,966✔
1017
  memset(pBuf, 0, bufLen);
260,966✔
1018
  pBuf[2] = '0';
260,966✔
1019
  pBuf[3] = 'x';
260,966✔
1020

1021
  int32_t len = tintToHex(id, &pBuf[4]);
260,966✔
1022
  varDataSetLen(pBuf, len + 2);
261,592✔
1023
}
261,592✔
1024

1025
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
35,271✔
1026
  int32_t          code = TSDB_CODE_SUCCESS;
35,271✔
1027
  int32_t          lino = 0;
35,271✔
1028
  SStreamTaskIter *pIter = NULL;
35,271✔
1029
  bool             isPaused =  true;
35,271✔
1030

1031
  taosRLockLatch(&pStream->lock);
35,271✔
1032
  code = createStreamTaskIter(pStream, &pIter);
35,378✔
1033
  TSDB_CHECK_CODE(code, lino, _end);
35,346!
1034

1035
  while (streamTaskIterNextTask(pIter)) {
154,434✔
1036
    SStreamTask *pTask = NULL;
117,941✔
1037
    code = streamTaskIterGetCurrent(pIter, &pTask);
117,941✔
1038
    TSDB_CHECK_CODE(code, lino, _end);
118,518!
1039

1040
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
118,518✔
1041
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
118,518✔
1042
    if (pe == NULL) {
119,088✔
1043
      continue;
116✔
1044
    }
1045
    if (pe->status != TASK_STATUS__PAUSE) {
118,972✔
1046
      isPaused = false;
118,956✔
1047
    }
1048
  }
1049
  (*pRes) = isPaused;
35,288✔
1050

1051
_end:
35,288✔
1052
  destroyStreamTaskIter(pIter);
35,288✔
1053
  taosRUnLockLatch(&pStream->lock);
35,359✔
1054
  if (code != TSDB_CODE_SUCCESS) {
35,377!
1055
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
1056
  }
1057
  return code;
35,376✔
1058
}
1059

1060
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
35,355✔
1061
  int32_t code = 0;
35,355✔
1062
  int32_t cols = 0;
35,355✔
1063
  int32_t lino = 0;
35,355✔
1064

1065
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
35,355✔
1066
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
35,355✔
1067
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,349✔
1068
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,311!
1069

1070
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
35,311✔
1071
  TSDB_CHECK_CODE(code, lino, _end);
35,314!
1072

1073
  // create time
1074
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,314✔
1075
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,298!
1076
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
35,298✔
1077
  TSDB_CHECK_CODE(code, lino, _end);
35,297!
1078

1079
  // stream id
1080
  char buf[128] = {0};
35,297✔
1081
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
35,297✔
1082
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,366✔
1083
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,346!
1084
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
35,346✔
1085
  TSDB_CHECK_CODE(code, lino, _end);
35,326!
1086

1087
  // related fill-history stream id
1088
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,326✔
1089
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,302✔
1090
  if (pStream->hTaskUid != 0) {
35,297!
1091
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
1092
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1093
  } else {
1094
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
35,297✔
1095
  }
1096
  TSDB_CHECK_CODE(code, lino, _end);
35,301!
1097

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

1106
  char status[20 + VARSTR_HEADER_SIZE] = {0};
35,280✔
1107
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
35,280✔
1108
  bool isPaused = false;
35,280✔
1109
  code = isAllTaskPaused(pStream, &isPaused);
35,280✔
1110
  TSDB_CHECK_CODE(code, lino, _end);
35,367!
1111

1112
  int8_t streamStatus = atomic_load_8(&pStream->status);
35,367✔
1113
  if (isPaused && pStream->pTaskList != NULL) {
35,344✔
1114
    streamStatus = STREAM_STATUS__PAUSE;
24✔
1115
  }
1116
  mndShowStreamStatus(status2, streamStatus);
35,344✔
1117
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
35,319✔
1118
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,319✔
1119
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,275!
1120

1121
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
35,275✔
1122
  TSDB_CHECK_CODE(code, lino, _end);
35,302!
1123

1124
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
35,302✔
1125
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
35,302✔
1126
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,275✔
1127
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,260!
1128

1129
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
35,260✔
1130
  TSDB_CHECK_CODE(code, lino, _end);
35,302!
1131

1132
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
35,302✔
1133
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
35,302✔
1134
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,303✔
1135
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,279!
1136

1137
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
35,279✔
1138
  TSDB_CHECK_CODE(code, lino, _end);
35,310!
1139

1140
  if (pStream->targetSTbName[0] == 0) {
35,310!
1141
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1142
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1143

1144
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
×
1145
  } else {
1146
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
35,310✔
1147
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
35,310✔
1148
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,335✔
1149
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,270!
1150

1151
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
35,270✔
1152
  }
1153
  TSDB_CHECK_CODE(code, lino, _end);
35,284!
1154

1155
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,284✔
1156
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,253!
1157

1158
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
35,253✔
1159
  TSDB_CHECK_CODE(code, lino, _end);
35,229!
1160

1161
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
35,229✔
1162
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
35,229✔
1163
  mndShowStreamTrigger(trigger2, pStream);
35,229✔
1164
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
35,279✔
1165
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,279✔
1166
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,318!
1167

1168
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
35,318✔
1169
  TSDB_CHECK_CODE(code, lino, _end);
35,318!
1170

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

1179
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
35,325✔
1180
  TSDB_CHECK_CODE(code, lino, _end);
35,315!
1181

1182
  // checkpoint interval
1183
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
35,315✔
1184
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
35,315✔
1185
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
35,322✔
1186

1187
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,322✔
1188
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,291!
1189

1190
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
35,291✔
1191
  TSDB_CHECK_CODE(code, lino, _end);
35,319!
1192

1193
  // checkpoint backup type
1194
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
35,319✔
1195
  STR_TO_VARSTR(backup, "none")
35,319✔
1196
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,319✔
1197
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,283!
1198

1199
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
35,283✔
1200
  TSDB_CHECK_CODE(code, lino, _end);
35,320!
1201

1202
  // history scan idle
1203
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
35,320✔
1204
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
35,320✔
1205

1206
  memset(dstStr, 0, tListLen(dstStr));
35,320✔
1207
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
35,320✔
1208
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,320✔
1209
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,330!
1210

1211
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
35,330✔
1212
  TSDB_CHECK_CODE(code, lino, _end);
35,313!
1213

1214
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
35,313✔
1215
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
35,291!
1216
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
35,327✔
1217
  if (streamStatus == STREAM_STATUS__FAILED){
35,327✔
1218
    STR_TO_VARSTR(msg, pStream->reserve)
1✔
1219
  } else {
1220
    STR_TO_VARSTR(msg, " ")
35,326✔
1221
  }
1222
  code = colDataSetVal(pColInfo, numOfRows, (const char *)msg, false);
35,327✔
1223

1224
_end:
35,301✔
1225
  if (code) {
35,301!
1226
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1227
  }
1228
  return code;
35,305✔
1229
}
1230

1231
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
224,516✔
1232
                              int32_t precision) {
1233
  SColumnInfoData *pColInfo = NULL;
224,516✔
1234
  int32_t          cols = 0;
224,516✔
1235
  int32_t          code = 0;
224,516✔
1236
  int32_t          lino = 0;
224,516✔
1237

1238
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
224,516✔
1239

1240
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
224,516✔
1241
  if (pe == NULL) {
225,038!
1242
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1243
           " no valid status/stage info",
1244
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1245
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1246
  }
1247

1248
  // stream name
1249
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
225,038✔
1250
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
225,038✔
1251

1252
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
225,017✔
1253
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,432!
1254

1255
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
224,432✔
1256
  TSDB_CHECK_CODE(code, lino, _end);
224,313!
1257

1258
  // task id
1259
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,313✔
1260
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,999!
1261

1262
  char idstr[128] = {0};
223,999✔
1263
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
223,999✔
1264
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
225,086✔
1265
  TSDB_CHECK_CODE(code, lino, _end);
224,435!
1266

1267
  // node type
1268
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
224,435✔
1269
  varDataSetLen(nodeType, 5);
224,435✔
1270
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,435✔
1271
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,150!
1272

1273
  if (pTask->info.nodeId > 0) {
224,280✔
1274
    memcpy(varDataVal(nodeType), "vnode", 5);
206,223✔
1275
  } else {
1276
    memcpy(varDataVal(nodeType), "snode", 5);
18,057✔
1277
  }
1278
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
224,280✔
1279
  TSDB_CHECK_CODE(code, lino, _end);
224,299!
1280

1281
  // node id
1282
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,299✔
1283
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,966!
1284

1285
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
223,966✔
1286
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
223,966✔
1287
  TSDB_CHECK_CODE(code, lino, _end);
223,826!
1288

1289
  // level
1290
  char level[20 + VARSTR_HEADER_SIZE] = {0};
223,826✔
1291
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
223,826✔
1292
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
115,111✔
1293
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
108,715✔
1294
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
19,689✔
1295
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
89,026!
1296
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
89,803✔
1297
  }
1298

1299
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,826✔
1300
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,790!
1301

1302
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
223,790✔
1303
  TSDB_CHECK_CODE(code, lino, _end);
224,312!
1304

1305
  // status
1306
  char status[20 + VARSTR_HEADER_SIZE] = {0};
224,312✔
1307

1308
  const char *pStatus = streamTaskGetStatusStr(pe->status);
224,312✔
1309
  STR_TO_VARSTR(status, pStatus);
224,383✔
1310

1311
  // status
1312
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,383✔
1313
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,193!
1314

1315
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
224,193✔
1316
  TSDB_CHECK_CODE(code, lino, _end);
224,252!
1317

1318
  // stage
1319
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,252✔
1320
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,891!
1321

1322
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
223,891✔
1323
  TSDB_CHECK_CODE(code, lino, _end);
223,827!
1324

1325
  // input queue
1326
  char        vbuf[TSDB_STREAM_NOTIFY_STAT_LEN + 2] = {0};
223,827✔
1327
  char        buf[TSDB_STREAM_NOTIFY_STAT_LEN] = {0};
223,827✔
1328
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
223,827✔
1329
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
223,827✔
1330
  STR_TO_VARSTR(vbuf, buf);
223,827✔
1331

1332
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,827✔
1333
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,547!
1334

1335
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,547✔
1336
  TSDB_CHECK_CODE(code, lino, _end);
224,362!
1337

1338
  // input total
1339
  const char *formatTotalMb = "%7.2f MiB";
224,362✔
1340
  const char *formatTotalGb = "%7.2f GiB";
224,362✔
1341
  if (pe->procsTotal < 1024) {
224,362!
1342
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
224,389✔
1343
  } else {
1344
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1345
  }
1346

1347
  memset(vbuf, 0, tListLen(vbuf));
224,362✔
1348
  STR_TO_VARSTR(vbuf, buf);
224,362✔
1349

1350
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,362✔
1351
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,609!
1352

1353
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,609✔
1354
  TSDB_CHECK_CODE(code, lino, _end);
224,336!
1355

1356
  // process throughput
1357
  const char *formatKb = "%7.2f KiB/s";
224,336✔
1358
  const char *formatMb = "%7.2f MiB/s";
224,336✔
1359
  if (pe->procsThroughput < 1024) {
224,336✔
1360
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
224,186✔
1361
  } else {
1362
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
150✔
1363
  }
1364

1365
  memset(vbuf, 0, tListLen(vbuf));
224,336✔
1366
  STR_TO_VARSTR(vbuf, buf);
224,336✔
1367

1368
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,336✔
1369
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,591!
1370

1371
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,591✔
1372
  TSDB_CHECK_CODE(code, lino, _end);
224,361!
1373

1374
  // output total
1375
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,361✔
1376
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,012!
1377

1378
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
224,468✔
1379
    colDataSetNULL(pColInfo, numOfRows);
89,758!
1380
  } else {
1381
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
134,710✔
1382
    memset(vbuf, 0, tListLen(vbuf));
135,138✔
1383
    STR_TO_VARSTR(vbuf, buf);
135,138✔
1384

1385
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
135,138✔
1386
    TSDB_CHECK_CODE(code, lino, _end);
134,924!
1387
  }
1388

1389
  // output throughput
1390
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,682✔
1391
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,079!
1392

1393
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
224,514✔
1394
    colDataSetNULL(pColInfo, numOfRows);
89,720!
1395
  } else {
1396
    if (pe->outputThroughput < 1024) {
134,794✔
1397
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
134,704✔
1398
    } else {
1399
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
90✔
1400
    }
1401

1402
    memset(vbuf, 0, tListLen(vbuf));
134,794✔
1403
    STR_TO_VARSTR(vbuf, buf);
134,794✔
1404

1405
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
134,794✔
1406
    TSDB_CHECK_CODE(code, lino, _end);
134,910!
1407
  }
1408
  // info
1409
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
224,630✔
1410
    const char *sinkStr = "%.2f MiB";
89,717✔
1411
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
89,717✔
1412
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
134,913✔
1413
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
115,289✔
1414
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
5,539✔
1415
      if (ret != 0) {
5,539!
1416
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1417
        memset(buf, 0, tListLen(buf));
×
1418
      }
1419
    } else {
1420
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
109,750✔
1421
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
109,750✔
1422
    }
1423
  } else {
1424
    memset(buf, 0, tListLen(buf));
19,624✔
1425
  }
1426

1427
  STR_TO_VARSTR(vbuf, buf);
224,630✔
1428

1429
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,630✔
1430
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,536!
1431

1432
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
224,536✔
1433
  TSDB_CHECK_CODE(code, lino, _end);
224,268!
1434

1435
  // start_time
1436
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
224,268✔
1437
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,985!
1438

1439
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
223,985✔
1440
  TSDB_CHECK_CODE(code, lino, _end);
223,705!
1441

1442
  // start id
1443
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,705✔
1444
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,400!
1445

1446
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
223,400✔
1447
  TSDB_CHECK_CODE(code, lino, _end);
223,520!
1448

1449
  // start ver
1450
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,520✔
1451
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,300!
1452

1453
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
223,300✔
1454
  TSDB_CHECK_CODE(code, lino, _end);
223,446!
1455

1456
  // checkpoint time
1457
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,446✔
1458
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,224!
1459

1460
  if (pe->checkpointInfo.latestTime != 0) {
223,240✔
1461
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
180,452✔
1462
  } else {
1463
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
42,788✔
1464
  }
1465
  TSDB_CHECK_CODE(code, lino, _end);
223,479!
1466

1467
  // checkpoint_id
1468
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,479✔
1469
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,310!
1470

1471
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
223,310✔
1472
  TSDB_CHECK_CODE(code, lino, _end);
223,529!
1473

1474
  // checkpoint version
1475
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,529✔
1476
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,300!
1477

1478
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
223,300✔
1479
  TSDB_CHECK_CODE(code, lino, _end);
223,476!
1480

1481
  // checkpoint size
1482
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,476✔
1483
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,174!
1484

1485
  colDataSetNULL(pColInfo, numOfRows);
223,186!
1486

1487
  // checkpoint backup status
1488
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,186✔
1489
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,340!
1490

1491
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
223,340✔
1492
  TSDB_CHECK_CODE(code, lino, _end);
223,598!
1493

1494
  // ds_err_info
1495
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,598✔
1496
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,498!
1497

1498
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
223,498✔
1499
  TSDB_CHECK_CODE(code, lino, _end);
223,644!
1500

1501
  // history_task_id
1502
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,644✔
1503
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,244!
1504

1505
  if (pe->hTaskId != 0) {
223,273✔
1506
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
1,132✔
1507
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
1,132✔
1508
  } else {
1509
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
222,141✔
1510
  }
1511
  TSDB_CHECK_CODE(code, lino, _end);
223,618!
1512

1513
  // history_task_status
1514
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,618✔
1515
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
223,301!
1516

1517
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
223,301✔
1518
  TSDB_CHECK_CODE(code, lino, _end);
223,893!
1519

1520
  // notify_event_stat
1521
  int32_t offset =0;
223,893✔
1522
  if (pe->notifyEventStat.notifyEventAddTimes > 0) {
223,893!
1523
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Add %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1524
                        pe->notifyEventStat.notifyEventAddTimes, pe->notifyEventStat.notifyEventAddElems,
1525
                        pe->notifyEventStat.notifyEventAddCostSec);
1526
  }
1527
  if (pe->notifyEventStat.notifyEventPushTimes > 0) {
223,893!
1528
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Push %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1529
                        pe->notifyEventStat.notifyEventPushTimes, pe->notifyEventStat.notifyEventPushElems,
1530
                        pe->notifyEventStat.notifyEventPushCostSec);
1531
  }
1532
  if (pe->notifyEventStat.notifyEventPackTimes > 0) {
223,893!
1533
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Pack %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1534
                        pe->notifyEventStat.notifyEventPackTimes, pe->notifyEventStat.notifyEventPackElems,
1535
                        pe->notifyEventStat.notifyEventPackCostSec);
1536
  }
1537
  if (pe->notifyEventStat.notifyEventSendTimes > 0) {
223,893!
1538
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Send %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1539
                        pe->notifyEventStat.notifyEventSendTimes, pe->notifyEventStat.notifyEventSendElems,
1540
                        pe->notifyEventStat.notifyEventSendCostSec);
1541
  }
1542
  if (pe->notifyEventStat.notifyEventHoldElems > 0) {
223,893!
1543
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "[Hold %" PRId64 " elems] ",
×
1544
                        pe->notifyEventStat.notifyEventHoldElems);
1545
  }
1546
  TSDB_CHECK_CONDITION(offset < sizeof(buf), code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
223,893!
1547
  buf[offset] = '\0';
223,893✔
1548

1549
  STR_TO_VARSTR(vbuf, buf);
223,893✔
1550

1551
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,893✔
1552
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,628✔
1553

1554
  if (offset == 0) {
224,624!
1555
    colDataSetNULL(pColInfo, numOfRows);
224,624!
1556
  } else {
1557
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1558
    TSDB_CHECK_CODE(code, lino, _end);
×
1559
  }
1560

1561
_end:
×
1562
  if (code) {
224,624!
1563
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1564
  }
1565
  return code;
224,587✔
1566
}
1567

1568
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
12,834✔
1569
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
12,834✔
1570
  const SEp *p = GET_ACTIVE_EP(pCurrent);
12,834✔
1571

1572
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
12,834!
1573
    return false;
12,834✔
1574
  }
1575
  return true;
×
1576
}
1577

1578
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
2,727✔
1579
  if (pInfo != NULL) {
2,727!
1580
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,727✔
1581
    taosHashCleanup(pInfo->pDBMap);
2,727✔
1582
  }
1583
}
2,727✔
1584

1585
// 1. increase the replica does not affect the stream process.
1586
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1587
// tasks on the will be removed replica.
1588
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1589
// will handle it as mentioned in 1 & 2 items.
1590
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
2,727✔
1591
                               SVgroupChangeInfo *pInfo) {
1592
  int32_t code = 0;
2,727✔
1593
  int32_t lino = 0;
2,727✔
1594

1595
  if (pInfo == NULL) {
2,727!
1596
    return TSDB_CODE_INVALID_PARA;
×
1597
  }
1598

1599
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
2,727✔
1600
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
2,727✔
1601

1602
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
2,727!
1603
    mndDestroyVgroupChangeInfo(pInfo);
×
1604
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1605
  }
1606

1607
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,727✔
1608
  for (int32_t i = 0; i < numOfNodes; ++i) {
16,303✔
1609
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
13,576✔
1610
    if (pPrevEntry == NULL) {
13,576!
1611
      continue;
×
1612
    }
1613

1614
    int32_t num = taosArrayGetSize(pNodeList);
13,576✔
1615
    for (int32_t j = 0; j < num; ++j) {
170,770✔
1616
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
170,036✔
1617
      if (pCurrent == NULL) {
170,036!
1618
        continue;
×
1619
      }
1620

1621
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
170,036✔
1622
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
12,842!
1623
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
8✔
1624

1625
          char buf[256] = {0};
8✔
1626
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
8✔
1627
          if (code) {
8!
1628
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1629
            TSDB_CHECK_CODE(code, lino, _err);
×
1630
          }
1631

1632
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
8✔
1633
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1634

1635
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
8✔
1636
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
8✔
1637
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
8✔
1638

1639
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
8✔
1640
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
8!
1641
        }
1642

1643
        // todo handle the snode info
1644
        if (pCurrent->nodeId != SNODE_HANDLE) {
12,842✔
1645
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
11,283✔
1646
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
11,283✔
1647
          mndReleaseVgroup(pMnode, pVgroup);
11,283✔
1648
          TSDB_CHECK_CODE(code, lino, _err);
11,283!
1649
        }
1650

1651
        break;
12,842✔
1652
      }
1653
    }
1654
  }
1655

1656
  return code;
2,727✔
1657

1658
_err:
×
1659
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1660
  mndDestroyVgroupChangeInfo(pInfo);
×
1661
  return code;
×
1662
}
1663

1664
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
2,147✔
1665
  bool              allReady = false;
2,147✔
1666
  bool              nodeUpdated = false;
2,147✔
1667
  SVgroupChangeInfo changeInfo = {0};
2,147✔
1668

1669
  int32_t numOfNodes = extractStreamNodeList(pMnode);
2,147✔
1670

1671
  if (numOfNodes == 0) {
2,147✔
1672
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
823✔
1673
    execInfo.ts = taosGetTimestampSec();
823✔
1674
    return false;
823✔
1675
  }
1676

1677
  for (int32_t i = 0; i < numOfNodes; ++i) {
7,780✔
1678
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
6,463✔
1679
    if (pNodeEntry == NULL) {
6,463!
1680
      continue;
×
1681
    }
1682

1683
    if (pNodeEntry->stageUpdated) {
6,463✔
1684
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
7✔
1685
      return true;
7✔
1686
    }
1687
  }
1688

1689
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot);
1,317✔
1690
  if (code) {
1,317!
1691
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1692
  }
1693

1694
  if (!allReady) {
1,317✔
1695
    mWarn("not all vnodes ready, quit from vnodes status check");
21!
1696
    return true;
21✔
1697
  }
1698

1699
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
1,296✔
1700
  if (code) {
1,296!
1701
    nodeUpdated = false;
×
1702
  } else {
1703
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
1,296✔
1704
    if (nodeUpdated) {
1,296!
1705
      mDebug("stream tasks not ready due to node update");
×
1706
    }
1707
  }
1708

1709
  mndDestroyVgroupChangeInfo(&changeInfo);
1,296✔
1710
  return nodeUpdated;
1,296✔
1711
}
1712

1713
// check if the node update happens or not
1714
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
2,147✔
1715
  SArray *pNodeSnapshot = NULL;
2,147✔
1716

1717
  streamMutexLock(&execInfo.lock);
2,147✔
1718
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
2,147✔
1719
  streamMutexUnlock(&execInfo.lock);
2,147✔
1720

1721
  taosArrayDestroy(pNodeSnapshot);
2,147✔
1722
  return updated;
2,147✔
1723
}
1724

1725
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,782✔
1726
  SSdb      *pSdb = pMnode->pSdb;
1,782✔
1727
  void      *pIter = NULL;
1,782✔
1728
  SSnodeObj *pObj = NULL;
1,782✔
1729

1730
  if (pSrcDb->cfg.replications == 1) {
1,782✔
1731
    return TSDB_CODE_SUCCESS;
1,779✔
1732
  } else {
1733
    while (1) {
1734
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
3✔
1735
      if (pIter == NULL) {
3✔
1736
        break;
2✔
1737
      }
1738

1739
      sdbRelease(pSdb, pObj);
1✔
1740
      sdbCancelFetch(pSdb, pIter);
1✔
1741
      return TSDB_CODE_SUCCESS;
1✔
1742
    }
1743

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