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

taosdata / TDengine / #3612

14 Feb 2025 06:43AM UTC coverage: 63.513% (+0.06%) from 63.456%
#3612

push

travis-ci

web-flow
Merge pull request #29757 from taosdata/feat/TS-5486

feat: support interp function with time range and time interval

141465 of 286269 branches covered (49.42%)

Branch coverage included in aggregate %.

220292 of 283307 relevant lines covered (77.76%)

18570887.65 hits per line

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

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

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

46
  return 0;
104,837✔
47
}
48

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

55
  if (pIter->level == -1) {
476,135✔
56
    pIter->level += 1;
104,843✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
681,516✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
576,361✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
575,021✔
62
      pIter->level += 1;
205,381✔
63
      pIter->ordinalIndex = 0;
205,381✔
64
      pIter->pTask = NULL;
205,381✔
65
      continue;
205,381✔
66
    }
67

68
    pIter->pTask = taosArrayGetP(pList, pIter->ordinalIndex);
371,142✔
69
    pIter->ordinalIndex += 1;
370,709✔
70
    return true;
370,709✔
71
  }
72

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
104,685!
89

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

97
    ESyncState state = pVgroup->vnodeGid[i].syncState;
281,381✔
98
    if (state == TAOS_SYNC_STATE_OFFLINE || state == TAOS_SYNC_STATE_ERROR || state == TAOS_SYNC_STATE_LEARNER ||
281,381!
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;
275,288✔
107
}
108

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

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

120
    SNodeEntry entry = {.nodeId = SNODE_HANDLE};
7,234✔
121
    code = addEpIntoEpSet(&entry.epset, pObj->pDnode->fqdn, pObj->pDnode->port);
7,234✔
122
    if (code) {
7,234!
123
      sdbRelease(pMnode->pSdb, pObj);
×
124
      sdbCancelFetch(pMnode->pSdb, pIter);
×
125
      mError("failed to extract epset for fqdn:%s during task vgroup snapshot", pObj->pDnode->fqdn);
×
126
      return code;
×
127
    }
128

129
    char buf[256] = {0};
7,234✔
130
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
7,234✔
131
    if (code != 0) {  // print error and continue
7,234!
132
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
133
    }
134

135
    void *p = taosArrayPush(pVgroupList, &entry);
7,234✔
136
    if (p == NULL) {
7,234!
137
      code = terrno;
×
138
      sdbRelease(pMnode->pSdb, pObj);
×
139
      sdbCancelFetch(pMnode->pSdb, pIter);
×
140
      mError("failed to put entry in vgroup list, nodeId:%d code:%s", entry.nodeId, tstrerror(code));
×
141
      return code;
×
142
    } else {
143
      mDebug("take snode snapshot, nodeId:%d %s", entry.nodeId, buf);
7,234✔
144
    }
145

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

149
  return code;
15,527✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
15,393✔
182
}
183

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

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

197
  while (1) {
278,430✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
293,957✔
199
    if (pIter == NULL) {
293,957✔
200
      break;
15,527✔
201
    }
202

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

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

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

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

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

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

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

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

261
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
15,527✔
262
  if (pVgroupList == NULL) {
15,527!
263
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
264
    code = terrno;
×
265
    goto _err;
×
266
  }
267

268
  // 1. check for all vnodes status
269
  code = mndCheckAndAddVgroupsInfo(pMnode, pVgroupList, allReady);
15,527✔
270
  if (code) {
15,527!
271
    goto _err;
×
272
  }
273

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

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

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

300
  SStreamObj *p = NULL;
12,979✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22,424✔
302
    if (p->uid == streamId) {
22,420✔
303
      sdbCancelFetch(pSdb, pIter);
12,975✔
304
      *pStream = p;
12,975✔
305
      return TSDB_CODE_SUCCESS;
12,975✔
306
    }
307
    sdbRelease(pSdb, p);
9,445✔
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) {
20,056✔
328
  *hasEpset = false;
20,056✔
329

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

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

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

370
  SStreamTask     *p = NULL;
182✔
371
  SStreamTaskIter *pIter = NULL;
182✔
372
  int32_t          code = createStreamTaskIter(pStream, &pIter);
182✔
373
  if (code) {
182!
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);
182✔
386
      *pTask = p;
182✔
387
      return 0;
182✔
388
    }
389
  }
390

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

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
75,233✔
396
  int32_t num = 0;
75,233✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
223,879✔
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
148,599✔
399
    num += taosArrayGetSize(pLevel);
148,675✔
400
  }
401

402
  return num;
75,180✔
403
}
404

405
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
66✔
406
  SSdb   *pSdb = pMnode->pSdb;
66✔
407
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
66✔
408
  if (pDb == NULL) {
66!
409
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
410
  }
411

412
  int32_t numOfStreams = 0;
66✔
413
  void   *pIter = NULL;
66✔
414
  while (1) {
×
415
    SStreamObj *pStream = NULL;
66✔
416
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
66✔
417
    if (pIter == NULL) break;
66!
418

419
    if (pStream->sourceDbUid == pDb->uid) {
×
420
      numOfStreams++;
×
421
    }
422

423
    sdbRelease(pSdb, pStream);
×
424
  }
425

426
  *pNumOfStreams = numOfStreams;
66✔
427
  mndReleaseDb(pMnode, pDb);
66✔
428
  return 0;
66✔
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,809✔
437
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
1,809✔
438
  if (code) {
1,809!
439
    return code;
×
440
  }
441

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

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

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

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

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

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

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

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

490
      if (pEntry->nodeId == p->nodeId) {
104,232✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
7,723✔
492

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

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,170✔
538
}
539

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

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

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

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

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

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

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

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

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

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

616
  return false;
×
617
}
618

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

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

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

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

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

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,485✔
656

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

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

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

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

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

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

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

724
  return TSDB_CODE_SUCCESS;
1,190✔
725
}
726

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

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

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

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
99,706✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
71,259✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
71,259✔
743
      continue;
70,036✔
744
    }
745

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

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

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

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

809
  taosArrayDestroy(pDropped);
29,637✔
810

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

815
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int32_t taskId, int64_t checkpointId,
182✔
816
                                          int64_t ts) {
817
  char msg[128] = {0};
182✔
818
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for task:0x%x", taskId);
182✔
819

820
  STrans *pTrans = NULL;
182✔
821
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
182✔
822
  if (pTrans == NULL || code != 0) {
182!
823
    return terrno;
×
824
  }
825

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

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

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

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

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

863
  sdbRelease(pMnode->pSdb, pStream);
182✔
864
  mndTransDrop(pTrans);
182✔
865

866
  return TSDB_CODE_ACTION_IN_PROGRESS;
182✔
867
}
868

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

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

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

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

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

896
  return code;
41✔
897
}
898

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

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

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

924
  void *p = taosArrayPush(pInfo->pTaskList, &info);
187✔
925
  if (p == NULL) {
187!
926
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
927
  } else {
928
    num = taosArrayGetSize(pInfo->pTaskList);
187✔
929
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
187✔
930
           " waiting tasks:%d",
931
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
932
  }
933
}
934

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

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

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

954
  return code;
40✔
955
}
956

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

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

971
  return code;
1,014✔
972
}
973

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

983
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
984
}
985

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

1000
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
32,972✔
1001
  int8_t trigger = pStream->conf.trigger;
32,972✔
1002
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
32,972✔
1003
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
11,322✔
1004
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
21,650✔
1005
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
10,866✔
1006
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
10,784!
1007
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
10,808✔
1008
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
1009
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
70✔
1010
  }
1011
}
32,972✔
1012

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

1018
  int32_t len = tintToHex(id, &pBuf[4]);
249,875✔
1019
  varDataSetLen(pBuf, len + 2);
250,559✔
1020
}
250,559✔
1021

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

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

1032
  while (streamTaskIterNextTask(pIter)) {
145,150✔
1033
    SStreamTask *pTask = NULL;
110,244✔
1034
    code = streamTaskIterGetCurrent(pIter, &pTask);
110,244✔
1035
    TSDB_CHECK_CODE(code, lino, _end);
111,159!
1036

1037
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
111,159✔
1038
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
111,159✔
1039
    if (pe == NULL) {
111,977✔
1040
      continue;
116✔
1041
    }
1042
    if (pe->status != TASK_STATUS__PAUSE) {
111,861✔
1043
      isPaused = false;
111,846✔
1044
    }
1045
  }
1046
  (*pRes) = isPaused;
33,120✔
1047

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

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

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

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

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

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

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

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

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

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

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

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

1126
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
33,081✔
1127
  TSDB_CHECK_CODE(code, lino, _end);
33,075!
1128

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

1134
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
33,090✔
1135
  TSDB_CHECK_CODE(code, lino, _end);
33,087!
1136

1137
  if (pStream->targetSTbName[0] == 0) {
33,087✔
1138
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
1139
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
2!
1140

1141
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
2✔
1142
  } else {
1143
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,085✔
1144
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
33,085✔
1145
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,147✔
1146
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,105!
1147

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

1152
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,043✔
1153
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,000!
1154

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

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

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

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

1176
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,137✔
1177
  TSDB_CHECK_CODE(code, lino, _end);
33,058!
1178

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

1184
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,188✔
1185
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,111!
1186

1187
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
33,111✔
1188
  TSDB_CHECK_CODE(code, lino, _end);
33,073!
1189

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

1196
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
33,042✔
1197
  TSDB_CHECK_CODE(code, lino, _end);
33,066!
1198

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

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

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

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

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

1224
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
216,352✔
1225

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

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

1238
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,919✔
1239
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,441!
1240

1241
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
216,441✔
1242
  TSDB_CHECK_CODE(code, lino, _end);
216,092!
1243

1244
  // task id
1245
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,092✔
1246
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,785!
1247

1248
  char idstr[128] = {0};
215,785✔
1249
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
215,785✔
1250
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
216,926✔
1251
  TSDB_CHECK_CODE(code, lino, _end);
216,167!
1252

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

1259
  if (pTask->info.nodeId > 0) {
216,072✔
1260
    memcpy(varDataVal(nodeType), "vnode", 5);
198,749✔
1261
  } else {
1262
    memcpy(varDataVal(nodeType), "snode", 5);
17,323✔
1263
  }
1264
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
216,072✔
1265
  TSDB_CHECK_CODE(code, lino, _end);
215,949!
1266

1267
  // node id
1268
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,949✔
1269
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,655!
1270

1271
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
215,655✔
1272
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
215,655✔
1273
  TSDB_CHECK_CODE(code, lino, _end);
215,531!
1274

1275
  // level
1276
  char level[20 + VARSTR_HEADER_SIZE] = {0};
215,531✔
1277
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
215,531✔
1278
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
110,987✔
1279
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
104,544✔
1280
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
18,784✔
1281
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
85,760!
1282
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
86,532✔
1283
  }
1284

1285
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,531✔
1286
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,380!
1287

1288
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
215,380✔
1289
  TSDB_CHECK_CODE(code, lino, _end);
215,988!
1290

1291
  // status
1292
  char status[20 + VARSTR_HEADER_SIZE] = {0};
215,988✔
1293

1294
  const char *pStatus = streamTaskGetStatusStr(pe->status);
215,988✔
1295
  STR_TO_VARSTR(status, pStatus);
216,106✔
1296

1297
  // status
1298
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,106✔
1299
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,034!
1300

1301
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
216,034✔
1302
  TSDB_CHECK_CODE(code, lino, _end);
215,950!
1303

1304
  // stage
1305
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,950✔
1306
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,705!
1307

1308
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
215,705✔
1309
  TSDB_CHECK_CODE(code, lino, _end);
215,584!
1310

1311
  // input queue
1312
  char        vbuf[40] = {0};
215,584✔
1313
  char        buf[38] = {0};
215,584✔
1314
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
215,584✔
1315
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
215,584✔
1316
  STR_TO_VARSTR(vbuf, buf);
215,584✔
1317

1318
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,584✔
1319
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,491!
1320

1321
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
216,491✔
1322
  TSDB_CHECK_CODE(code, lino, _end);
216,163!
1323

1324
  // input total
1325
  const char *formatTotalMb = "%7.2f MiB";
216,163✔
1326
  const char *formatTotalGb = "%7.2f GiB";
216,163✔
1327
  if (pe->procsTotal < 1024) {
216,163✔
1328
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
216,159✔
1329
  } else {
1330
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
4✔
1331
  }
1332

1333
  memset(vbuf, 0, tListLen(vbuf));
216,163✔
1334
  STR_TO_VARSTR(vbuf, buf);
216,163✔
1335

1336
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,163✔
1337
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,465!
1338

1339
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
216,465✔
1340
  TSDB_CHECK_CODE(code, lino, _end);
216,137!
1341

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

1351
  memset(vbuf, 0, tListLen(vbuf));
216,137✔
1352
  STR_TO_VARSTR(vbuf, buf);
216,137✔
1353

1354
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,137✔
1355
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,509!
1356

1357
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
216,509✔
1358
  TSDB_CHECK_CODE(code, lino, _end);
216,117!
1359

1360
  // output total
1361
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,117✔
1362
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,808!
1363

1364
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
216,297✔
1365
    colDataSetNULL(pColInfo, numOfRows);
86,588!
1366
  } else {
1367
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
129,709✔
1368
    memset(vbuf, 0, tListLen(vbuf));
130,169✔
1369
    STR_TO_VARSTR(vbuf, buf);
130,169✔
1370

1371
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
130,169✔
1372
    TSDB_CHECK_CODE(code, lino, _end);
129,908!
1373
  }
1374

1375
  // output throughput
1376
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,496✔
1377
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,896!
1378

1379
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
216,349✔
1380
    colDataSetNULL(pColInfo, numOfRows);
86,576!
1381
  } else {
1382
    if (pe->outputThroughput < 1024) {
129,773✔
1383
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
129,729✔
1384
    } else {
1385
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
44✔
1386
    }
1387

1388
    memset(vbuf, 0, tListLen(vbuf));
129,773✔
1389
    STR_TO_VARSTR(vbuf, buf);
129,773✔
1390

1391
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
129,773✔
1392
    TSDB_CHECK_CODE(code, lino, _end);
129,922!
1393
  }
1394
  // info
1395
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
216,498✔
1396
    const char *sinkStr = "%.2f MiB";
86,574✔
1397
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
86,574✔
1398
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
129,924✔
1399
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
111,187✔
1400
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
5,885✔
1401
      if (ret != 0) {
5,885!
1402
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1403
        memset(buf, 0, tListLen(buf));
×
1404
      }
1405
    } else {
1406
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
105,302✔
1407
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
105,302✔
1408
    }
1409
  } else {
1410
    memset(buf, 0, tListLen(buf));
18,737✔
1411
  }
1412

1413
  STR_TO_VARSTR(vbuf, buf);
216,498✔
1414

1415
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,498✔
1416
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,463!
1417

1418
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
216,463✔
1419
  TSDB_CHECK_CODE(code, lino, _end);
216,020!
1420

1421
  // start_time
1422
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,020✔
1423
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,698!
1424

1425
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
215,698✔
1426
  TSDB_CHECK_CODE(code, lino, _end);
215,426!
1427

1428
  // start id
1429
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,426✔
1430
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,171!
1431

1432
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
215,171✔
1433
  TSDB_CHECK_CODE(code, lino, _end);
215,182!
1434

1435
  // start ver
1436
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,182✔
1437
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,986!
1438

1439
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
214,986✔
1440
  TSDB_CHECK_CODE(code, lino, _end);
214,982!
1441

1442
  // checkpoint time
1443
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,982✔
1444
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,799!
1445

1446
  if (pe->checkpointInfo.latestTime != 0) {
214,813✔
1447
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
172,944✔
1448
  } else {
1449
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
41,869✔
1450
  }
1451
  TSDB_CHECK_CODE(code, lino, _end);
215,139!
1452

1453
  // checkpoint_id
1454
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,139✔
1455
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,971!
1456

1457
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
214,971✔
1458
  TSDB_CHECK_CODE(code, lino, _end);
215,216!
1459

1460
  // checkpoint version
1461
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,216✔
1462
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,009!
1463

1464
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
215,009✔
1465
  TSDB_CHECK_CODE(code, lino, _end);
215,054!
1466

1467
  // checkpoint size
1468
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,054✔
1469
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,813!
1470

1471
  colDataSetNULL(pColInfo, numOfRows);
214,828!
1472

1473
  // checkpoint backup status
1474
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,828✔
1475
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,112!
1476

1477
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
215,112✔
1478
  TSDB_CHECK_CODE(code, lino, _end);
215,318!
1479

1480
  // ds_err_info
1481
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,318✔
1482
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,274!
1483

1484
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
215,274✔
1485
  TSDB_CHECK_CODE(code, lino, _end);
215,330!
1486

1487
  // history_task_id
1488
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,330✔
1489
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,940!
1490

1491
  if (pe->hTaskId != 0) {
215,030✔
1492
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
402✔
1493
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
402✔
1494
  } else {
1495
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
214,628✔
1496
  }
1497
  TSDB_CHECK_CODE(code, lino, _end);
215,439!
1498

1499
  // history_task_status
1500
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,439✔
1501
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,191!
1502

1503
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
215,191✔
1504
  TSDB_CHECK_CODE(code, lino, _end);
215,823!
1505

1506
_end:
215,823✔
1507
  if (code) {
215,823!
1508
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1509
  }
1510
  return code;
215,825✔
1511
}
1512

1513
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
13,668✔
1514
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
13,668✔
1515
  const SEp *p = GET_ACTIVE_EP(pCurrent);
13,668✔
1516

1517
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
13,668!
1518
    return false;
13,668✔
1519
  }
1520
  return true;
×
1521
}
1522

1523
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
2,816✔
1524
  if (pInfo != NULL) {
2,816!
1525
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,816✔
1526
    taosHashCleanup(pInfo->pDBMap);
2,816✔
1527
  }
1528
}
2,816✔
1529

1530
// 1. increase the replica does not affect the stream process.
1531
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1532
// tasks on the will be removed replica.
1533
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1534
// will handle it as mentioned in 1 & 2 items.
1535
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
2,816✔
1536
                               SVgroupChangeInfo *pInfo) {
1537
  int32_t code = 0;
2,816✔
1538
  int32_t lino = 0;
2,816✔
1539

1540
  if (pInfo == NULL) {
2,816!
1541
    return TSDB_CODE_INVALID_PARA;
×
1542
  }
1543

1544
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
2,816✔
1545
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
2,816✔
1546

1547
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
2,816!
1548
    mndDestroyVgroupChangeInfo(pInfo);
×
1549
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1550
  }
1551

1552
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,816✔
1553
  for (int32_t i = 0; i < numOfNodes; ++i) {
17,277✔
1554
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
14,461✔
1555
    if (pPrevEntry == NULL) {
14,461!
1556
      continue;
×
1557
    }
1558

1559
    int32_t num = taosArrayGetSize(pNodeList);
14,461✔
1560
    for (int32_t j = 0; j < num; ++j) {
171,992✔
1561
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
171,222✔
1562
      if (pCurrent == NULL) {
171,222!
1563
        continue;
×
1564
      }
1565

1566
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
171,222✔
1567
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
13,691!
1568
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
23✔
1569

1570
          char buf[256] = {0};
23✔
1571
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
23✔
1572
          if (code) {
23!
1573
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1574
            TSDB_CHECK_CODE(code, lino, _err);
×
1575
          }
1576

1577
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
23✔
1578
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1579

1580
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
23✔
1581
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
23✔
1582
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
23✔
1583

1584
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
23✔
1585
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
23!
1586
        }
1587

1588
        // todo handle the snode info
1589
        if (pCurrent->nodeId != SNODE_HANDLE) {
13,691✔
1590
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
11,930✔
1591
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
11,930✔
1592
          mndReleaseVgroup(pMnode, pVgroup);
11,930✔
1593
          TSDB_CHECK_CODE(code, lino, _err);
11,930!
1594
        }
1595

1596
        break;
13,691✔
1597
      }
1598
    }
1599
  }
1600

1601
  return code;
2,816✔
1602

1603
_err:
×
1604
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1605
  mndDestroyVgroupChangeInfo(pInfo);
×
1606
  return code;
×
1607
}
1608

1609
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
2,116✔
1610
  bool              allReady = false;
2,116✔
1611
  bool              nodeUpdated = false;
2,116✔
1612
  SVgroupChangeInfo changeInfo = {0};
2,116✔
1613

1614
  int32_t numOfNodes = extractStreamNodeList(pMnode);
2,116✔
1615

1616
  if (numOfNodes == 0) {
2,116✔
1617
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
738✔
1618
    execInfo.ts = taosGetTimestampSec();
738✔
1619
    return false;
738✔
1620
  }
1621

1622
  for (int32_t i = 0; i < numOfNodes; ++i) {
8,253✔
1623
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
6,910✔
1624
    if (pNodeEntry == NULL) {
6,910!
1625
      continue;
×
1626
    }
1627

1628
    if (pNodeEntry->stageUpdated) {
6,910✔
1629
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
35✔
1630
      return true;
35✔
1631
    }
1632
  }
1633

1634
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot);
1,343✔
1635
  if (code) {
1,343!
1636
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1637
  }
1638

1639
  if (!allReady) {
1,343✔
1640
    mWarn("not all vnodes ready, quit from vnodes status check");
12!
1641
    return true;
12✔
1642
  }
1643

1644
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
1,331✔
1645
  if (code) {
1,331!
1646
    nodeUpdated = false;
×
1647
  } else {
1648
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
1,331✔
1649
    if (nodeUpdated) {
1,331!
1650
      mDebug("stream tasks not ready due to node update");
×
1651
    }
1652
  }
1653

1654
  mndDestroyVgroupChangeInfo(&changeInfo);
1,331✔
1655
  return nodeUpdated;
1,331✔
1656
}
1657

1658
// check if the node update happens or not
1659
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
2,116✔
1660
  SArray *pNodeSnapshot = NULL;
2,116✔
1661

1662
  streamMutexLock(&execInfo.lock);
2,116✔
1663
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
2,116✔
1664
  streamMutexUnlock(&execInfo.lock);
2,116✔
1665

1666
  taosArrayDestroy(pNodeSnapshot);
2,116✔
1667
  return updated;
2,116✔
1668
}
1669

1670
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,743✔
1671
  SSdb      *pSdb = pMnode->pSdb;
1,743✔
1672
  void      *pIter = NULL;
1,743✔
1673
  SSnodeObj *pObj = NULL;
1,743✔
1674

1675
  if (pSrcDb->cfg.replications == 1) {
1,743✔
1676
    return TSDB_CODE_SUCCESS;
1,740✔
1677
  } else {
1678
    while (1) {
1679
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
3✔
1680
      if (pIter == NULL) {
3✔
1681
        break;
2✔
1682
      }
1683

1684
      sdbRelease(pSdb, pObj);
1✔
1685
      sdbCancelFetch(pSdb, pIter);
1✔
1686
      return TSDB_CODE_SUCCESS;
1✔
1687
    }
1688

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