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

taosdata / TDengine / #3629

04 Mar 2025 01:45PM UTC coverage: 63.692% (-0.1%) from 63.79%
#3629

push

travis-ci

web-flow
Merge pull request #30007 from taosdata/revert-29951-docs/update-exception-handling-strategy

Revert "docs: update exception handling strategy"

149369 of 300378 branches covered (49.73%)

Branch coverage included in aggregate %.

233614 of 300930 relevant lines covered (77.63%)

18792670.99 hits per line

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

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

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

46
  return 0;
106,847✔
47
}
48

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

55
  if (pIter->level == -1) {
484,902✔
56
    pIter->level += 1;
106,848✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
694,730✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
587,968✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
587,971✔
62
      pIter->level += 1;
209,828✔
63
      pIter->ordinalIndex = 0;
209,828✔
64
      pIter->pTask = NULL;
209,828✔
65
      continue;
209,828✔
66
    }
67

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

90
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
282,242✔
91
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
568,455✔
92
    if (!pVgroup->vnodeGid[i].syncRestore) {
287,473✔
93
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,247!
94
      return false;
1,247✔
95
    }
96

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

106
  return true;
280,982✔
107
}
108

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

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

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

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

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

149
  return code;
15,909✔
150
}
151

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

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

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

171
    if (objStatus != SDB_STATUS_READY) {
16,487✔
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,486✔
179
  }
180

181
  return TSDB_CODE_SUCCESS;
15,786✔
182
}
183

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

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

197
  while (1) {
285,013✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
300,922✔
199
    if (pIter == NULL) {
300,922✔
200
      break;
15,909✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
285,013✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
285,013✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
141,902✔
209
      if (code) {
141,902!
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) {
143,111✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
442!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
442✔
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) {
285,013✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
282,242✔
227
    }
228

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

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

246
    sdbRelease(pSdb, pVgroup);
285,013✔
247
  }
248

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

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

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

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

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

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

286
  *pList = pVgroupList;
15,909✔
287
  return code;
15,909✔
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) {
13,725✔
296
  void *pIter = NULL;
13,725✔
297
  SSdb *pSdb = pMnode->pSdb;
13,725✔
298
  *pStream = NULL;
13,725✔
299

300
  SStreamObj *p = NULL;
13,725✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22,865✔
302
    if (p->uid == streamId) {
22,860✔
303
      sdbCancelFetch(pSdb, pIter);
13,720✔
304
      *pStream = p;
13,720✔
305
      return TSDB_CODE_SUCCESS;
13,720✔
306
    }
307
    sdbRelease(pSdb, p);
9,140✔
308
  }
309

310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
5✔
311
}
312

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

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

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

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

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

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

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

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

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

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

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
78,005✔
396
  int32_t num = 0;
78,005✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
231,471✔
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
153,445✔
399
    num += taosArrayGetSize(pLevel);
153,476✔
400
  }
401

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

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

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

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

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

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

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

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

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

484
    for (int32_t j = 0; j < size; ++j) {
111,369✔
485
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
110,344✔
486
      if (pEntry == NULL) {
110,344!
487
        continue;
×
488
      }
489

490
      if (pEntry->nodeId == p->nodeId) {
110,344✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
7,949✔
492

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

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,168✔
538
}
539

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

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

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

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

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

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

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

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

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

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

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

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

616
  return false;
×
617
}
618

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

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

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

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

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

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,562✔
656

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

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

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

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

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

704
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
176!
705
      mWarn("stream:0x%" PRIx64 " active checkpointId is not equalled to the required, current:%" PRId64
×
706
            ", req:%" PRId64 " recheck next time",
707
            id.streamId, pe->checkpointInfo.activeId, checkpointId);
708
      return -1;
×
709
    } else {
710
      //  do nothing
711
    }
712
  } else {
713
    if (pTransInfo->transId != transId) {
1,768✔
714
      mError("stream:0x%" PRIx64
37!
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;
37✔
718
    }
719
  }
720

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

724
  return TSDB_CODE_SUCCESS;
1,907✔
725
}
726

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

736
  mDebug("start to scan checkpoint report info");
30,616✔
737

738
  streamMutexLock(&execInfo.lock);
30,616✔
739

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
103,312✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
73,901✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
73,901✔
743
      continue;
71,927✔
744
    }
745

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

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

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

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

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

797
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
1✔
798
      if (code) {
1!
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);
30,616✔
808

809
  taosArrayDestroy(pDropped);
30,616✔
810

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

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

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

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

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

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

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

856
  code = mndTransPrepare(pMnode, pTrans);
127✔
857
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
127!
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);
127✔
865
  mndTransDrop(pTrans);
127✔
866

867
  return TSDB_CODE_ACTION_IN_PROGRESS;
127✔
868
}
869

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

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

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

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

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

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

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

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

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

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

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

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

955
  return code;
31✔
956
}
957

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

965
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
1,013✔
966
  if (code == 0) {
1,013✔
967
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
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,193✔
988
  if (status == STREAM_STATUS__NORMAL) {
33,193✔
989
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
33,164✔
990
  } else if (status == STREAM_STATUS__STOP) {
29!
991
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
992
  } else if (status == STREAM_STATUS__FAILED) {
29!
993
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
994
  } else if (status == STREAM_STATUS__RECOVER) {
29!
995
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
996
  } else if (status == STREAM_STATUS__PAUSE) {
29!
997
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
30✔
998
  }
999
}
33,193✔
1000

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

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

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

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

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

1033
  while (streamTaskIterNextTask(pIter)) {
145,275✔
1034
    SStreamTask *pTask = NULL;
111,958✔
1035
    code = streamTaskIterGetCurrent(pIter, &pTask);
111,958✔
1036
    TSDB_CHECK_CODE(code, lino, _end);
112,013!
1037

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1227
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
222,878✔
1228
  if (pe == NULL) {
222,896!
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};
222,896✔
1237
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
222,896✔
1238

1239
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,939✔
1240
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,655!
1241

1242
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
222,655✔
1243
  TSDB_CHECK_CODE(code, lino, _end);
222,700!
1244

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

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

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

1260
  if (pTask->info.nodeId > 0) {
222,856✔
1261
    memcpy(varDataVal(nodeType), "vnode", 5);
204,827✔
1262
  } else {
1263
    memcpy(varDataVal(nodeType), "snode", 5);
18,029✔
1264
  }
1265
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
222,856✔
1266
  TSDB_CHECK_CODE(code, lino, _end);
222,872!
1267

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

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

1276
  // level
1277
  char level[20 + VARSTR_HEADER_SIZE] = {0};
222,893✔
1278
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
222,893✔
1279
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
114,224✔
1280
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
108,669✔
1281
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
19,508✔
1282
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
89,161✔
1283
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
89,064✔
1284
  }
1285

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

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

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

1295
  const char *pStatus = streamTaskGetStatusStr(pe->status);
222,861✔
1296
  STR_TO_VARSTR(status, pStatus);
222,788✔
1297

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

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

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

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

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

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

1322
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
222,620✔
1323
  TSDB_CHECK_CODE(code, lino, _end);
222,764!
1324

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

1334
  memset(vbuf, 0, tListLen(vbuf));
222,764✔
1335
  STR_TO_VARSTR(vbuf, buf);
222,764✔
1336

1337
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,764✔
1338
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,732!
1339

1340
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
222,732✔
1341
  TSDB_CHECK_CODE(code, lino, _end);
222,811!
1342

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

1352
  memset(vbuf, 0, tListLen(vbuf));
222,811✔
1353
  STR_TO_VARSTR(vbuf, buf);
222,811✔
1354

1355
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,811✔
1356
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,793!
1357

1358
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
222,793✔
1359
  TSDB_CHECK_CODE(code, lino, _end);
222,818!
1360

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

1365
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
222,826✔
1366
    colDataSetNULL(pColInfo, numOfRows);
89,064!
1367
  } else {
1368
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
133,762✔
1369
    memset(vbuf, 0, tListLen(vbuf));
133,793✔
1370
    STR_TO_VARSTR(vbuf, buf);
133,793✔
1371

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

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

1380
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
222,829✔
1381
    colDataSetNULL(pColInfo, numOfRows);
89,049!
1382
  } else {
1383
    if (pe->outputThroughput < 1024) {
133,780✔
1384
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
133,708✔
1385
    } else {
1386
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
72✔
1387
    }
1388

1389
    memset(vbuf, 0, tListLen(vbuf));
133,780✔
1390
    STR_TO_VARSTR(vbuf, buf);
133,780✔
1391

1392
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
133,780✔
1393
    TSDB_CHECK_CODE(code, lino, _end);
133,815!
1394
  }
1395
  // info
1396
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
222,864✔
1397
    const char *sinkStr = "%.2f MiB";
89,039✔
1398
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
89,039✔
1399
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
133,825✔
1400
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
114,286✔
1401
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
5,539✔
1402
      if (ret != 0) {
5,539!
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 "]";
108,747✔
1408
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
108,747✔
1409
    }
1410
  } else {
1411
    memset(buf, 0, tListLen(buf));
19,539✔
1412
  }
1413

1414
  STR_TO_VARSTR(vbuf, buf);
222,864✔
1415

1416
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
222,864✔
1417
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
222,713!
1418

1419
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
222,713✔
1420
  TSDB_CHECK_CODE(code, lino, _end);
222,818!
1421

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

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

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

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

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

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

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

1447
  if (pe->checkpointInfo.latestTime != 0) {
222,737✔
1448
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
182,210✔
1449
  } else {
1450
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
40,527✔
1451
  }
1452
  TSDB_CHECK_CODE(code, lino, _end);
222,780!
1453

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1541
  if (offset == 0) {
222,725!
1542
    colDataSetNULL(pColInfo, numOfRows);
222,725!
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) {
222,725!
1550
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1551
  }
1552
  return code;
222,733✔
1553
}
1554

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

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

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

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

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

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

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

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

1608
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
181,224✔
1609
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
14,030!
1610
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
6✔
1611

1612
          char buf[256] = {0};
6✔
1613
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
6✔
1614
          if (code) {
6!
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,
6✔
1620
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1621

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

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

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

1638
        break;
14,030✔
1639
      }
1640
    }
1641
  }
1642

1643
  return code;
2,945✔
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,141✔
1652
  bool              allReady = false;
2,141✔
1653
  bool              nodeUpdated = false;
2,141✔
1654
  SVgroupChangeInfo changeInfo = {0};
2,141✔
1655

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

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

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

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

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

1681
  if (!allReady) {
1,404✔
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,383✔
1687
  if (code) {
1,383!
1688
    nodeUpdated = false;
×
1689
  } else {
1690
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
1,383✔
1691
    if (nodeUpdated) {
1,383!
1692
      mDebug("stream tasks not ready due to node update");
×
1693
    }
1694
  }
1695

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

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

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

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

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

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