• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #3625

26 Feb 2025 10:19AM UTC coverage: 63.633% (+0.1%) from 63.485%
#3625

push

travis-ci

web-flow
Merge pull request #29914 from taosdata/feat/TS-5613-3.0

feat:[TS-5613]support bool in cast

148738 of 299799 branches covered (49.61%)

Branch coverage included in aggregate %.

233124 of 300297 relevant lines covered (77.63%)

17654074.26 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) {
107,043✔
35
  *pIter = taosMemoryCalloc(1, sizeof(SStreamTaskIter));
107,043!
36
  if (*pIter == NULL) {
107,047!
37
    return terrno;
×
38
  }
39

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

46
  return 0;
107,027✔
47
}
48

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

55
  if (pIter->level == -1) {
486,884✔
56
    pIter->level += 1;
107,022✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
696,829✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
589,328✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
588,076✔
62
      pIter->level += 1;
209,945✔
63
      pIter->ordinalIndex = 0;
209,945✔
64
      pIter->pTask = NULL;
209,945✔
65
      continue;
209,945✔
66
    }
67

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

90
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
244,613✔
91
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
493,224✔
92
    if (!pVgroup->vnodeGid[i].syncRestore) {
249,608✔
93
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
990!
94
      return false;
990✔
95
    }
96

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

106
  return true;
243,616✔
107
}
108

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

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

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

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

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

149
  return code;
14,803✔
150
}
151

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
14,681✔
182
}
183

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

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

197
  while (1) {
246,944✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
261,747✔
199
    if (pIter == NULL) {
261,747✔
200
      break;
14,803✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
246,944✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
246,944✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
122,702✔
209
      if (code) {
122,702!
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) {
124,242✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
400!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
400✔
220
      }
221
    }
222

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

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

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

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

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

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

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

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

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

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

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

300
  SStreamObj *p = NULL;
12,668✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
21,596✔
302
    if (p->uid == streamId) {
21,592✔
303
      sdbCancelFetch(pSdb, pIter);
12,664✔
304
      *pStream = p;
12,664✔
305
      return TSDB_CODE_SUCCESS;
12,664✔
306
    }
307
    sdbRelease(pSdb, p);
8,928✔
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,775✔
328
  *hasEpset = false;
19,775✔
329

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

484
    for (int32_t j = 0; j < size; ++j) {
101,973✔
485
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
100,921✔
486
      if (pEntry == NULL) {
100,921!
487
        continue;
×
488
      }
489

490
      if (pEntry->nodeId == p->nodeId) {
100,921✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
7,211✔
492

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

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,196✔
538
}
539

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

573
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,326✔
574
    code = doRemoveTasks(pExecNode, &id);
7,326✔
575
    if (code) {
7,326!
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));
381!
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,100✔
603
  size_t num = taosArrayGetSize(pList);
15,100✔
604

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

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

616
  return false;
×
617
}
618

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

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

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

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

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

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,434✔
656

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

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

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

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

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

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

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

724
  return TSDB_CODE_SUCCESS;
1,134✔
725
}
726

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

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

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

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

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

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

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

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

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

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

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

807
  streamMutexUnlock(&execInfo.lock);
27,297✔
808

809
  taosArrayDestroy(pDropped);
27,297✔
810

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

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

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

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

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

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

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

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

867
  return TSDB_CODE_ACTION_IN_PROGRESS;
183✔
868
}
869

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

873
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
206✔
874
  if (px != NULL) {
206✔
875
    *pInfo = px;
163✔
876
    return 0;
163✔
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) {
206✔
903
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
206✔
904
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
206✔
905

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

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

925
  void *p = taosArrayPush(pInfo->pTaskList, &info);
192✔
926
  if (p == NULL) {
192!
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);
192✔
930
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
192✔
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);
632✔
968
  } else {
969
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
381!
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,154✔
988
  if (status == STREAM_STATUS__NORMAL) {
33,154✔
989
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
33,140✔
990
  } else if (status == STREAM_STATUS__STOP) {
14!
991
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
992
  } else if (status == STREAM_STATUS__FAILED) {
14!
993
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
994
  } else if (status == STREAM_STATUS__RECOVER) {
14!
995
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
996
  } else if (status == STREAM_STATUS__PAUSE) {
14!
997
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
21✔
998
  }
999
}
33,154✔
1000

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1295
  const char *pStatus = streamTaskGetStatusStr(pe->status);
224,192✔
1296
  STR_TO_VARSTR(status, pStatus);
224,235✔
1297

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

1302
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
224,205✔
1303
  TSDB_CHECK_CODE(code, lino, _end);
224,194!
1304

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

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

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

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

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

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

1334
  memset(vbuf, 0, tListLen(vbuf));
224,319✔
1335
  STR_TO_VARSTR(vbuf, buf);
224,319✔
1336

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

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

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

1352
  memset(vbuf, 0, tListLen(vbuf));
224,273✔
1353
  STR_TO_VARSTR(vbuf, buf);
224,273✔
1354

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

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

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

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

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

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

1380
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
224,524✔
1381
    colDataSetNULL(pColInfo, numOfRows);
89,907!
1382
  } else {
1383
    if (pe->outputThroughput < 1024) {
134,617✔
1384
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
134,502✔
1385
    } else {
1386
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
115✔
1387
    }
1388

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

1392
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
134,617✔
1393
    TSDB_CHECK_CODE(code, lino, _end);
134,650!
1394
  }
1395
  // info
1396
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
224,557✔
1397
    const char *sinkStr = "%.2f MiB";
89,911✔
1398
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
89,911✔
1399
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
134,646✔
1400
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
115,096✔
1401
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
5,891✔
1402
      if (ret != 0) {
5,891!
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,205✔
1408
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
109,205✔
1409
    }
1410
  } else {
1411
    memset(buf, 0, tListLen(buf));
19,550✔
1412
  }
1413

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1472
  colDataSetNULL(pColInfo, numOfRows);
223,460!
1473

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

1478
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
223,600✔
1479
  TSDB_CHECK_CODE(code, lino, _end);
223,514!
1480

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

1485
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
223,446✔
1486
  TSDB_CHECK_CODE(code, lino, _end);
223,133!
1487

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

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

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

1504
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
223,001✔
1505
  TSDB_CHECK_CODE(code, lino, _end);
223,319!
1506

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

1536
  STR_TO_VARSTR(vbuf, buf);
223,319✔
1537

1538
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
223,319✔
1539
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
224,629✔
1540

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

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

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

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

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

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

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

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

1601
    int32_t num = taosArrayGetSize(pNodeList);
13,501✔
1602
    for (int32_t j = 0; j < num; ++j) {
166,115✔
1603
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
165,403✔
1604
      if (pCurrent == NULL) {
165,403!
1605
        continue;
×
1606
      }
1607

1608
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
165,403✔
1609
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
12,789!
1610
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
9✔
1611

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1708
  taosArrayDestroy(pNodeSnapshot);
2,017✔
1709
  return updated;
2,017✔
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