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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

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

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

46
  return 0;
106,844✔
47
}
48

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

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

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

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

90
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
272,955✔
91
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
549,936✔
92
    if (!pVgroup->vnodeGid[i].syncRestore) {
277,986✔
93
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,001!
94
      return false;
1,001✔
95
    }
96

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

106
  return true;
271,950✔
107
}
108

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

114
  while (1) {
6,706✔
115
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
21,369✔
116
    if (pIter == NULL) {
21,369✔
117
      break;
14,663✔
118
    }
119

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

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

146
    sdbRelease(pMnode->pSdb, pObj);
6,706✔
147
  }
148

149
  return code;
14,663✔
150
}
151

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
14,534✔
182
}
183

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

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

197
  while (1) {
275,258✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
289,921✔
199
    if (pIter == NULL) {
289,921✔
200
      break;
14,663✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
275,258✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
275,258✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
136,881✔
209
      if (code) {
136,881!
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) {
138,377✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
399!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
399✔
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) {
275,258✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
272,955✔
227
    }
228

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

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

246
    sdbRelease(pSdb, pVgroup);
275,258✔
247
  }
248

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

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

258
  *pList = NULL;
14,663✔
259
  *allReady = true;
14,663✔
260

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

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

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

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

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

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

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

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

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

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

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

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

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

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
76,853✔
396
  int32_t num = 0;
76,853✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
228,698✔
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
151,809✔
399
    num += taosArrayGetSize(pLevel);
151,870✔
400
  }
401

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

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

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

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

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

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

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

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

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

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

490
      if (pEntry->nodeId == p->nodeId) {
104,113✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
6,901✔
492

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

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,186✔
538
}
539

540
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
1,371✔
541
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
1,371!
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,371✔
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,667✔
567
    SStreamTask *pTask = NULL;
7,316✔
568
    code = streamTaskIterGetCurrent(pIter, &pTask);
7,316✔
569
    if (code) {
7,316!
570
      continue;
×
571
    }
572

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

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

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

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

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

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

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

611
    if (pEntry->nodeId == nodeId) {
113,909✔
612
      return true;
14,358✔
613
    }
614
  }
615

616
  return false;
×
617
}
618

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

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

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

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

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

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,371✔
656

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

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

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

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

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

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

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

724
  return TSDB_CODE_SUCCESS;
1,104✔
725
}
726

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

736
  mDebug("start to scan checkpoint report info");
26,743✔
737

738
  streamMutexLock(&execInfo.lock);
26,743✔
739

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
87,823✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
62,184✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
62,184✔
743
      continue;
61,043✔
744
    }
745

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

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

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

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

809
  taosArrayDestroy(pDropped);
26,743✔
810

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

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

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

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

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

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

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

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

867
  return TSDB_CODE_ACTION_IN_PROGRESS;
205✔
868
}
869

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

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

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

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

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

897
  return code;
43✔
898
}
899

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

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

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

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

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

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

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

955
  return code;
41✔
956
}
957

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

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

972
  return code;
1,013✔
973
}
974

975
int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
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,163✔
988
  if (status == STREAM_STATUS__NORMAL) {
33,163✔
989
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
33,144✔
990
  } else if (status == STREAM_STATUS__STOP) {
19!
991
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
992
  } else if (status == STREAM_STATUS__FAILED) {
19!
993
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
994
  } else if (status == STREAM_STATUS__RECOVER) {
19!
995
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
996
  } else if (status == STREAM_STATUS__PAUSE) {
19!
997
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
24✔
998
  }
999
}
33,163✔
1000

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1380
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
223,353✔
1381
    colDataSetNULL(pColInfo, numOfRows);
89,348!
1382
  } else {
1383
    if (pe->outputThroughput < 1024) {
134,005✔
1384
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
133,897✔
1385
    } else {
1386
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
108✔
1387
    }
1388

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1594
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,619✔
1595
  for (int32_t i = 0; i < numOfNodes; ++i) {
15,721✔
1596
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
13,102✔
1597
    if (pPrevEntry == NULL) {
13,102!
1598
      continue;
×
1599
    }
1600

1601
    int32_t num = taosArrayGetSize(pNodeList);
13,102✔
1602
    for (int32_t j = 0; j < num; ++j) {
171,714✔
1603
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
170,886✔
1604
      if (pCurrent == NULL) {
170,886!
1605
        continue;
×
1606
      }
1607

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

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

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

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

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

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

1638
        break;
12,274✔
1639
      }
1640
    }
1641
  }
1642

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

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

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

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

1670
    if (pNodeEntry->stageUpdated) {
6,278✔
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,259✔
1677
  if (code) {
1,259!
1678
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1679
  }
1680

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

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

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

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

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

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

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

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