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

taosdata / TDengine / #4488

12 Jul 2025 07:47AM UTC coverage: 62.207% (-0.7%) from 62.948%
#4488

push

travis-ci

web-flow
docs: update stream docs (#31822)

157961 of 324087 branches covered (48.74%)

Branch coverage included in aggregate %.

244465 of 322830 relevant lines covered (75.73%)

6561668.76 hits per line

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

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

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

46
  return 0;
17,817✔
47
}
48

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

55
  if (pIter->level == -1) {
84,131✔
56
    pIter->level += 1;
17,817✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
114,297✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->pTaskList, pIter->level);
96,480✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
96,480✔
62
      pIter->level += 1;
30,166✔
63
      pIter->ordinalIndex = 0;
30,166✔
64
      pIter->pTask = NULL;
30,166✔
65
      continue;
30,166✔
66
    }
67

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
17,817!
89

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

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

106
  return true;
1,077✔
107
}
108

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

114
  while (1) {
48✔
115
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
357✔
116
    if (pIter == NULL) {
357✔
117
      break;
309✔
118
    }
119

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

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

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

149
  return code;
309✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
309✔
182
}
183

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

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

197
  while (1) {
1,091✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,400✔
199
    if (pIter == NULL) {
1,400✔
200
      break;
309✔
201
    }
202
    if(pVgroup->mountVgId) {
1,091!
203
      sdbRelease(pSdb, pVgroup);
×
204
      continue;
×
205
    }
206

207
    SNodeEntry entry = {.nodeId = pVgroup->vgId, .hbTimestamp = pVgroup->updateTime};
1,091✔
208
    entry.epset = mndGetVgroupEpset(pMnode, pVgroup);
1,091✔
209

210
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
1,091✔
211
    if (pReplica == NULL) {  // not exist, add it into hash map
1,091✔
212
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
463✔
213
      if (code) {
463!
214
        mError("failed to put info into hashmap during task vgroup snapshot, code:%s", tstrerror(code));
×
215
        sdbRelease(pSdb, pVgroup);
×
216
        sdbCancelFetch(pSdb, pIter);
×
217
        goto _end;  // take snapshot failed, and not all ready
×
218
      }
219
    } else {
220
      if (*pReplica != pVgroup->replica) {
628✔
221
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
2!
222
              pVgroup->vgId, pVgroup->replica, *pReplica);
223
        *allReady = false;  // task snap success, but not all ready
2✔
224
      }
225
    }
226

227
    // if not all ready till now, no need to check the remaining vgroups,
228
    // but still we need to put the info of the existed vgroups into the snapshot list
229
    if (*allReady) {
1,091✔
230
      *allReady = checkStatusForEachReplica(pVgroup);
1,083✔
231
    }
232

233
    char buf[256] = {0};
1,091✔
234
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
1,091✔
235
    if (code != 0) {  // print error and continue
1,091!
236
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
237
    }
238

239
    void *p = taosArrayPush(pVgroupList, &entry);
1,091✔
240
    if (p == NULL) {
1,091!
241
      mError("failed to put entry in vgroup list, nodeId:%d code:out of memory", entry.nodeId);
×
242
      code = terrno;
×
243
      sdbRelease(pSdb, pVgroup);
×
244
      sdbCancelFetch(pSdb, pIter);
×
245
      goto _end;
×
246
    } else {
247
      mDebug("take node snapshot, nodeId:%d %s", entry.nodeId, buf);
1,091!
248
    }
249

250
    if (pTermMap != NULL) {
1,091✔
251
      int64_t term = pVgroup->vnodeGid[0].syncTerm;
59✔
252
      code = taosHashPut(pTermMap, &pVgroup->vgId, sizeof(pVgroup->vgId), &term, sizeof(term));
59✔
253
      if (code) {
59!
254
        mError("failed to put vnode:%d term into hashMap, code:%s", pVgroup->vgId, tstrerror(code));
×
255
      }
256
    }
257

258
    sdbRelease(pSdb, pVgroup);
1,091✔
259
  }
260

261
_end:
309✔
262
  taosHashCleanup(pHash);
309✔
263
  return code;
309✔
264
}
265

266
int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList, SHashObj* pTermMap) {
309✔
267
  int32_t   code = 0;
309✔
268
  SArray   *pVgroupList = NULL;
309✔
269

270
  *pList = NULL;
309✔
271
  *allReady = true;
309✔
272

273
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
309✔
274
  if (pVgroupList == NULL) {
309!
275
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
276
    code = terrno;
×
277
    goto _err;
×
278
  }
279

280
  // 1. check for all vnodes status
281
  code = mndCheckAndAddVgroupsInfo(pMnode, pVgroupList, allReady, pTermMap);
309✔
282
  if (code) {
309!
283
    goto _err;
×
284
  }
285

286
  // 2. add snode info
287
  code = mndAddSnodeInfo(pMnode, pVgroupList);
309✔
288
  if (code) {
309!
289
    goto _err;
×
290
  }
291

292
  // 3. check for mnode status
293
  code = mndCheckMnodeStatus(pMnode);
309✔
294
  if (code != TSDB_CODE_SUCCESS) {
309!
295
    *allReady = false;
×
296
  }
297

298
  *pList = pVgroupList;
309✔
299
  return code;
309✔
300

301
_err:
×
302
  *allReady = false;
×
303
  taosArrayDestroy(pVgroupList);
×
304
  return code;
×
305
}
306

307
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
3,785✔
308
  void *pIter = NULL;
3,785✔
309
  SSdb *pSdb = pMnode->pSdb;
3,785✔
310
  *pStream = NULL;
3,785✔
311

312
  SStreamObj *p = NULL;
3,785✔
313
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
6,030✔
314
    if (p->uid == streamId) {
6,029✔
315
      sdbCancelFetch(pSdb, pIter);
3,784✔
316
      *pStream = p;
3,784✔
317
      return TSDB_CODE_SUCCESS;
3,784✔
318
    }
319
    sdbRelease(pSdb, p);
2,245✔
320
  }
321

322
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
1✔
323
}
324

325
void mndKillTransImpl(SMnode *pMnode, int32_t transId, const char *pDbName) {
×
326
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
×
327
  if (pTrans != NULL) {
×
328
    mInfo("kill active transId:%d in Db:%s", transId, pDbName);
×
329
    int32_t code = mndKillTrans(pMnode, pTrans);
×
330
    mndReleaseTrans(pMnode, pTrans);
×
331
    if (code) {
×
332
      mError("failed to kill transId:%d, code:%s", pTrans->id, tstrerror(code));
×
333
    }
334
  } else {
335
    mError("failed to acquire trans in Db:%s, transId:%d", pDbName, transId);
×
336
  }
337
}
×
338

339
int32_t extractNodeEpset(SMnode *pMnode, SEpSet *pEpSet, bool *hasEpset, int32_t taskId, int32_t nodeId) {
5,089✔
340
  *hasEpset = false;
5,089✔
341

342
  pEpSet->numOfEps = 0;
5,089✔
343
  if (nodeId == SNODE_HANDLE) {
5,089✔
344
    SSnodeObj *pObj = NULL;
39✔
345
    void      *pIter = NULL;
39✔
346

347
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
39✔
348
    if (pIter != NULL) {
39!
349
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
39✔
350
      sdbRelease(pMnode->pSdb, pObj);
39✔
351
      sdbCancelFetch(pMnode->pSdb, pIter);
39✔
352
      if (code) {
39!
353
        *hasEpset = false;
×
354
        mError("failed to set epset");
×
355
      } else {
356
        *hasEpset = true;
39✔
357
      }
358
      return code;
39✔
359
    } else {
360
      mError("failed to acquire snode epset");
×
361
      return TSDB_CODE_INVALID_PARA;
×
362
    }
363
  } else {
364
    SVgObj *pVgObj = mndAcquireVgroup(pMnode, nodeId);
5,050✔
365
    if (pVgObj != NULL) {
5,050✔
366
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
5,049✔
367
      mndReleaseVgroup(pMnode, pVgObj);
5,049✔
368

369
      epsetAssign(pEpSet, &epset);
5,049✔
370
      *hasEpset = true;
5,049✔
371
      return TSDB_CODE_SUCCESS;
5,049✔
372
    } else {
373
      mDebug("orphaned task:0x%x need to be dropped, nodeId:%d, no redo action", taskId, nodeId);
1!
374
      return TSDB_CODE_SUCCESS;
1✔
375
    }
376
  }
377
}
378

379
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
×
380
  *pTask = NULL;
×
381

382
  SStreamTask     *p = NULL;
×
383
  SStreamTaskIter *pIter = NULL;
×
384
  int32_t          code = createStreamTaskIter(pStream, &pIter);
×
385
  if (code) {
×
386
    mError("failed to create stream task iter:%s", pStream->name);
×
387
    return code;
×
388
  }
389

390
  while (streamTaskIterNextTask(pIter)) {
×
391
    code = streamTaskIterGetCurrent(pIter, &p);
×
392
    if (code) {
×
393
      continue;
×
394
    }
395

396
    if (p->id.taskId == pId->taskId) {
×
397
      destroyStreamTaskIter(pIter);
×
398
      *pTask = p;
×
399
      return 0;
×
400
    }
401
  }
402

403
  destroyStreamTaskIter(pIter);
×
404
  return TSDB_CODE_FAILED;
×
405
}
406

407
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
17,302✔
408
  int32_t num = 0;
17,302✔
409
  for (int32_t i = 0; i < taosArrayGetSize(pStream->pTaskList); ++i) {
47,174✔
410
    SArray *pLevel = taosArrayGetP(pStream->pTaskList, i);
29,872✔
411
    num += taosArrayGetSize(pLevel);
29,872✔
412
  }
413

414
  return num;
17,302✔
415
}
416

417
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
58✔
418
  SSdb   *pSdb = pMnode->pSdb;
58✔
419
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
58✔
420
  if (pDb == NULL) {
58!
421
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
422
  }
423

424
  int32_t numOfStreams = 0;
58✔
425
  void   *pIter = NULL;
58✔
426
  while (1) {
×
427
    SStreamObj *pStream = NULL;
58✔
428
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
58✔
429
    if (pIter == NULL) break;
58!
430

431
    if (pStream->sourceDbUid == pDb->uid) {
×
432
      numOfStreams++;
×
433
    }
434

435
    sdbRelease(pSdb, pStream);
×
436
  }
437

438
  *pNumOfStreams = numOfStreams;
58✔
439
  mndReleaseDb(pMnode, pDb);
58✔
440
  return 0;
58✔
441
}
442

443
static void freeTaskList(void *param) {
672✔
444
  SArray **pList = (SArray **)param;
672✔
445
  taosArrayDestroy(*pList);
672✔
446
}
672✔
447

448
int32_t mndInitExecInfo() {
2,468✔
449
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
2,468✔
450
  if (code) {
2,468!
451
    return code;
×
452
  }
453

454
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
2,468✔
455

456
  execInfo.pTaskList = taosArrayInit(4, sizeof(STaskId));
2,468✔
457
  execInfo.pTaskMap = taosHashInit(64, fn, true, HASH_NO_LOCK);
2,468✔
458
  execInfo.transMgmt.pDBTrans = taosHashInit(32, fn, true, HASH_NO_LOCK);
2,468✔
459
  execInfo.pTransferStateStreams = taosHashInit(32, fn, true, HASH_NO_LOCK);
2,468✔
460
  execInfo.pChkptStreams = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
2,468✔
461
  execInfo.pStreamConsensus = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
2,468✔
462
  execInfo.pNodeList = taosArrayInit(4, sizeof(SNodeEntry));
2,468✔
463
  execInfo.pKilledChkptTrans = taosArrayInit(4, sizeof(SStreamTaskResetMsg));
2,468✔
464

465
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
2,468!
466
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
2,468!
467
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
2,468!
468
    mError("failed to initialize the stream runtime env, code:%s", tstrerror(terrno));
×
469
    return terrno;
×
470
  }
471

472
  execInfo.role = NODE_ROLE_UNINIT;
2,468✔
473
  execInfo.switchFromFollower = false;
2,468✔
474

475
  taosHashSetFreeFp(execInfo.pTransferStateStreams, freeTaskList);
2,468✔
476
  taosHashSetFreeFp(execInfo.pChkptStreams, freeTaskList);
2,468✔
477
  taosHashSetFreeFp(execInfo.pStreamConsensus, freeTaskList);
2,468✔
478
  return 0;
2,468✔
479
}
480

481
void removeExpiredNodeInfo(const SArray *pNodeSnapshot) {
11✔
482
  SArray *pValidList = taosArrayInit(4, sizeof(SNodeEntry));
11✔
483
  if (pValidList == NULL) {  // not continue
11!
484
    return;
×
485
  }
486

487
  int32_t size = taosArrayGetSize(pNodeSnapshot);
11✔
488
  int32_t oldSize = taosArrayGetSize(execInfo.pNodeList);
11✔
489

490
  for (int32_t i = 0; i < oldSize; ++i) {
179✔
491
    SNodeEntry *p = taosArrayGet(execInfo.pNodeList, i);
168✔
492
    if (p == NULL) {
168!
493
      continue;
×
494
    }
495

496
    for (int32_t j = 0; j < size; ++j) {
474✔
497
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
332✔
498
      if (pEntry == NULL) {
332!
499
        continue;
×
500
      }
501

502
      if (pEntry->nodeId == p->nodeId) {
332✔
503
        p->hbTimestamp = pEntry->hbTimestamp;
26✔
504

505
        void *px = taosArrayPush(pValidList, p);
26✔
506
        if (px == NULL) {
26!
507
          mError("failed to put node into list, nodeId:%d", p->nodeId);
×
508
        } else {
509
          mDebug("vgId:%d ts:%" PRId64 " HbMsgId:%d is valid", p->nodeId, p->hbTimestamp, p->lastHbMsgId);
26!
510
        }
511
        break;
26✔
512
      }
513
    }
514
  }
515

516
  taosArrayDestroy(execInfo.pNodeList);
11✔
517
  execInfo.pNodeList = pValidList;
11✔
518

519
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
11!
520
         (int32_t)taosArrayGetSize(pValidList), oldSize);
521
}
522

523
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) {
2,771✔
524
  void *p = taosHashGet(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
2,771✔
525
  if (p == NULL) {
2,771!
526
    return TSDB_CODE_SUCCESS;
×
527
  }
528

529
  int32_t code = taosHashRemove(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
2,771✔
530
  if (code) {
2,771!
531
    return code;
×
532
  }
533

534
  for (int32_t k = 0; k < taosArrayGetSize(pExecNode->pTaskList); ++k) {
6,500!
535
    STaskId *pId = taosArrayGet(pExecNode->pTaskList, k);
6,500✔
536
    if (pId == NULL) {
6,500!
537
      continue;
×
538
    }
539

540
    if (pId->taskId == pRemovedId->taskId && pId->streamId == pRemovedId->streamId) {
6,500!
541
      taosArrayRemove(pExecNode->pTaskList, k);
2,771✔
542

543
      int32_t num = taosArrayGetSize(pExecNode->pTaskList);
2,771✔
544
      mInfo("s-task:0x%x removed from buffer, remain:%d in buffer list", (int32_t)pRemovedId->taskId, num);
2,771!
545
      break;
2,771✔
546
    }
547
  }
548

549
  return TSDB_CODE_SUCCESS;
2,771✔
550
}
551

552
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
11✔
553
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
11!
554
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
555
    if (pId == NULL) {
×
556
      continue;
×
557
    }
558

559
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
560
    if (code) {
×
561
      mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId);
×
562
    }
563
  }
564
}
11✔
565

566
void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
621✔
567
  SStreamTaskIter *pIter = NULL;
621✔
568
  streamMutexLock(&pExecNode->lock);
621✔
569

570
  // 1. remove task entries
571
  int32_t code = createStreamTaskIter(pStream, &pIter);
621✔
572
  if (code) {
621!
573
    streamMutexUnlock(&pExecNode->lock);
×
574
    mError("failed to create stream task iter:%s", pStream->name);
×
575
    return;
×
576
  }
577

578
  while (streamTaskIterNextTask(pIter)) {
3,392✔
579
    SStreamTask *pTask = NULL;
2,771✔
580
    code = streamTaskIterGetCurrent(pIter, &pTask);
2,771✔
581
    if (code) {
2,771!
582
      continue;
×
583
    }
584

585
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
2,771✔
586
    code = doRemoveTasks(pExecNode, &id);
2,771✔
587
    if (code) {
2,771!
588
      mError("failed to remove task in buffer list, 0x%" PRIx64, id.taskId);
×
589
    }
590
  }
591

592
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
621!
593
    streamMutexUnlock(&pExecNode->lock);
×
594
    destroyStreamTaskIter(pIter);
×
595
    mError("task map size, task list size, not equal");
×
596
    return;
×
597
  }
598

599
  // 2. remove stream entry in consensus hash table and checkpoint-report hash table
600
  code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, pStream->uid);
621✔
601
  if (code) {
621!
602
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
603
  }
604

605
  code = mndClearChkptReportInfo(execInfo.pChkptStreams, pStream->uid);
621✔
606
  if (code) {
621✔
607
    mError("failed to clear the checkpoint report info, code:%s", tstrerror(code));
94!
608
  }
609

610
  streamMutexUnlock(&pExecNode->lock);
621✔
611
  destroyStreamTaskIter(pIter);
621✔
612
}
613

614
static bool taskNodeExists(SArray *pList, int32_t nodeId) {
134✔
615
  size_t num = taosArrayGetSize(pList);
134✔
616

617
  for (int32_t i = 0; i < num; ++i) {
206!
618
    SNodeEntry *pEntry = taosArrayGet(pList, i);
206✔
619
    if (pEntry == NULL) {
206!
620
      continue;
×
621
    }
622

623
    if (pEntry->nodeId == nodeId) {
206✔
624
      return true;
134✔
625
    }
626
  }
627

628
  return false;
×
629
}
630

631
int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) {
11✔
632
  SArray *pRemovedTasks = taosArrayInit(4, sizeof(STaskId));
11✔
633
  if (pRemovedTasks == NULL) {
11!
634
    return terrno;
×
635
  }
636

637
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
11✔
638
  for (int32_t i = 0; i < numOfTask; ++i) {
148✔
639
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
137✔
640
    if (pId == NULL) {
137!
641
      continue;
×
642
    }
643

644
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, pId, sizeof(*pId));
137✔
645
    if (pEntry == NULL) {
137!
646
      continue;
×
647
    }
648

649
    if (pEntry->nodeId == SNODE_HANDLE) {
137✔
650
      continue;
3✔
651
    }
652

653
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
134✔
654
    if (!existed) {
134!
655
      void *p = taosArrayPush(pRemovedTasks, pId);
×
656
      if (p == NULL) {
×
657
        mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId);
×
658
      }
659
    }
660
  }
661

662
  removeTasksInBuf(pRemovedTasks, &execInfo);
11✔
663

664
  mDebug("remove invalid stream tasks:%d, remain:%d", (int32_t)taosArrayGetSize(pRemovedTasks),
11!
665
         (int32_t)taosArrayGetSize(execInfo.pTaskList));
666

667
  removeExpiredNodeInfo(pNodeSnapshot);
11✔
668

669
  taosArrayDestroy(pRemovedTasks);
11✔
670
  return 0;
11✔
671
}
672

673
static int32_t allTasksSendChkptReport(SChkptReportInfo* pReportInfo, int32_t numOfTasks, const char* pName) {
232✔
674
  int64_t checkpointId = -1;
232✔
675
  int32_t transId = -1;
232✔
676
  int32_t taskId = -1;
232✔
677

678
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
232✔
679
  if (existed != numOfTasks) {
232!
680
    mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pReportInfo->streamId, pName,
×
681
           existed, numOfTasks, numOfTasks - existed);
682
    return -1;
×
683
  }
684

685
  // acquire current active checkpointId, and do cross-check checkpointId info in exec.pTaskList
686
  for(int32_t i = 0; i < numOfTasks; ++i) {
1,524✔
687
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
1,292✔
688
    if (pInfo == NULL) {
1,292!
689
      continue;
×
690
    }
691

692
    if (checkpointId == -1) {
1,292✔
693
      checkpointId = pInfo->checkpointId;
232✔
694
      transId = pInfo->transId;
232✔
695
      taskId = pInfo->taskId;
232✔
696
    } else if (checkpointId != pInfo->checkpointId) {
1,060!
697
      mError("stream:0x%" PRIx64
×
698
             " checkpointId in checkpoint-report list are not identical, type 1 taskId:0x%x checkpointId:%" PRId64
699
             ", type 2 taskId:0x%x checkpointId:%" PRId64,
700
             pReportInfo->streamId, taskId, checkpointId, pInfo->taskId, pInfo->checkpointId);
701
      return -1;
×
702
    }
703
  }
704

705
  // check for the correct checkpointId for current task info in STaskChkptInfo
706
  STaskChkptInfo  *p = taosArrayGet(pReportInfo->pTaskList, 0);
232✔
707
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
232✔
708
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
232✔
709

710
  // cross-check failed, there must be something unknown wrong
711
  SStreamTransInfo *pTransInfo = taosHashGet(execInfo.transMgmt.pDBTrans, &id.streamId, sizeof(id.streamId));
232✔
712
  if (pTransInfo == NULL) {
232✔
713
    mWarn("stream:0x%" PRIx64 " no active trans exists for checkpoint transId:%d, it may have been cleared already",
59!
714
           id.streamId, transId);
715

716
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
59!
717
      mWarn("stream:0x%" PRIx64 " active checkpointId is not equalled to the required, current:%" PRId64
×
718
            ", req:%" PRId64 " recheck next time",
719
            id.streamId, pe->checkpointInfo.activeId, checkpointId);
720
      return -1;
×
721
    } else {
722
      //  do nothing
723
    }
724
  } else {
725
    if (pTransInfo->transId != transId) {
173!
726
      mError("stream:0x%" PRIx64
×
727
             " checkpoint-report list info are expired, active transId:%d trans in list:%d, recheck next time",
728
             id.streamId, pTransInfo->transId, transId);
729
      return -1;
×
730
    }
731
  }
732

733
  mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info", id.streamId,
232!
734
         pName, numOfTasks);
735

736
  return TSDB_CODE_SUCCESS;
232✔
737
}
738

739
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
944✔
740
  SMnode *pMnode = pReq->info.node;
944✔
741
  void   *pIter = NULL;
944✔
742
  int32_t code = 0;
944✔
743
  int32_t lino = 0;
944✔
744
  SArray *pDropped = NULL;
944✔
745
  int64_t ts = 0;
944✔
746

747
  mDebug("start to scan checkpoint report info");
944!
748

749
  streamMutexLock(&execInfo.lock);
944✔
750

751
  int32_t num = taosHashGetSize(execInfo.pChkptStreams);
944✔
752
  if (num == 0) {
944✔
753
    goto _end;
566✔
754
  }
755

756
  pDropped = taosArrayInit(4, sizeof(int64_t));
378✔
757
  TSDB_CHECK_NULL(pDropped, code, lino, _end, terrno);
378!
758

759
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
642✔
760
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
496✔
761
    if (taosArrayGetSize(px->pTaskList) == 0) {
496✔
762
      continue;
264✔
763
    }
764

765
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
232✔
766
    if (pInfo == NULL) {
232!
767
      continue;
×
768
    }
769

770
    SStreamObj *pStream = NULL;
232✔
771
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
232✔
772
    if (pStream == NULL || code != 0) {
232!
773
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
774
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
775
      if (p == NULL) {
×
776
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
777
      }
778
      continue;
×
779
    }
780

781
    int32_t total = mndGetNumOfStreamTasks(pStream);
232✔
782
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
232✔
783
    if (ret == 0) {
232!
784
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
232✔
785
      if (code == 0) {
232!
786
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
232✔
787
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
232!
788
          taosArrayClear(px->pTaskList);
232✔
789
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
232!
790
                " to %" PRId64,
791
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
792
          px->reportChkpt = pInfo->checkpointId;
232✔
793
        } else {
794
          mDebug("stream:0x%" PRIx64 " not launch chkpt-info update trans, due to checkpoint not finished yet",
×
795
                 pInfo->streamId);
796
        }
797

798
        sdbRelease(pMnode->pSdb, pStream);
232✔
799
        break;
232✔
800
      } else {
801
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
802
      }
803
    }
804

805
    sdbRelease(pMnode->pSdb, pStream);
×
806
  }
807

808
  int32_t size = taosArrayGetSize(pDropped);
378✔
809
  if (size > 0) {
378!
810
    for (int32_t i = 0; i < size; ++i) {
×
811
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
812
      if (pStreamId == NULL) {
×
813
        continue;
×
814
      }
815

816
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
817
      if (code) {
×
818
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
819
      }
820
    }
821

822
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
823
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
824
  }
825

826
_end:
944✔
827

828
  ts = taosGetTimestampMs();
944✔
829
  execInfo.chkptReportScanTs = ts;
944✔
830

831
  streamMutexUnlock(&execInfo.lock);
944✔
832

833
  if (pDropped != NULL) {
944✔
834
    taosArrayDestroy(pDropped);
378✔
835
  }
836

837
  mDebug("end to scan checkpoint report info, ts:%"PRId64, ts);
944!
838
  return code;
944✔
839
}
840

841
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId, SArray* pList) {
11✔
842
  char    msg[128] = {0};
11✔
843
  STrans *pTrans = NULL;
11✔
844

845
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for stream:0x%" PRIx64, pStream->uid);
11✔
846

847
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
11✔
848
  if (pTrans == NULL || code != 0) {
11!
849
    return terrno;
×
850
  }
851

852
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
11✔
853
  if (code) {
11!
854
    return code;
×
855
  }
856

857
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pStream, checkpointId, pList);
11✔
858
  if (code != 0) {
11!
859
    mndTransDrop(pTrans);
×
860
    return code;
×
861
  }
862

863
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
11✔
864
  if (code) {
11!
865
    mndTransDrop(pTrans);
×
866
    return code;
×
867
  }
868

869
  code = mndTransPrepare(pMnode, pTrans);
11✔
870

871
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
11!
872
    mError("trans:%d, failed to prepare set consensus-chkptId trans for stream:0x%" PRId64 " since %s", pTrans->id,
×
873
           pStream->uid, tstrerror(code));
874
    mndTransDrop(pTrans);
×
875
    return code;
×
876
  }
877

878
  mndTransDrop(pTrans);
11✔
879
  return TSDB_CODE_ACTION_IN_PROGRESS;
11✔
880
}
881

882
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
102✔
883
  *pInfo = NULL;
102✔
884

885
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
102✔
886
  if (px != NULL) {
102✔
887
    *pInfo = px;
81✔
888
    return 0;
81✔
889
  }
890

891
  SCheckpointConsensusInfo p = {
21✔
892
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
21✔
893
      .numOfTasks = numOfTasks,
894
      .streamId = streamId,
895
  };
896

897
  if (p.pTaskList == NULL) {
21!
898
    return terrno;
×
899
  }
900

901
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
21✔
902
  if (code == 0) {
21!
903
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
21✔
904
    *pInfo = pChkptInfo;
21✔
905
  } else {
906
    *pInfo = NULL;
×
907
  }
908

909
  return code;
21✔
910
}
911

912
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
913
// discard the msg may lead to the lost of connections.
914
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
102✔
915
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
102✔
916
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
102✔
917

918
  int32_t num = (int32_t) taosArrayGetSize(pInfo->pTaskList);
102✔
919
  for (int32_t i = 0; i < num; ++i) {
419✔
920
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
323✔
921
    if (p == NULL) {
323!
922
      continue;
×
923
    }
924

925
    if (p->req.taskId == info.req.taskId) {
323✔
926
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update send reqTs %" PRId64
6!
927
             "->%" PRId64 " checkpointId:%" PRId64 " -> %" PRId64 " term:%d->%d total existed:%d",
928
             pRestoreInfo->taskId, pRestoreInfo->streamId, p->req.startTs, info.req.startTs, p->req.checkpointId,
929
             info.req.checkpointId, p->req.term, info.req.term, num);
930
      p->req.startTs = info.req.startTs;
6✔
931
      p->req.checkpointId = info.req.checkpointId;
6✔
932
      p->req.transId = info.req.transId;
6✔
933
      p->req.nodeId = info.req.nodeId;
6✔
934
      p->req.term = info.req.term;
6✔
935
      return;
6✔
936
    }
937
  }
938

939
  void *p = taosArrayPush(pInfo->pTaskList, &info);
96✔
940
  if (p == NULL) {
96!
941
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
942
  } else {
943
    num = taosArrayGetSize(pInfo->pTaskList);
96✔
944
    mInfo("s-task:0x%x (vgId:%d) checkpointId:%" PRId64 " term:%d, reqTs:%" PRId64
96!
945
           " added into consensus-checkpointId list, stream:0x%" PRIx64 " waiting tasks:%d, total tasks:%d",
946
           pRestoreInfo->taskId, pRestoreInfo->nodeId, pRestoreInfo->checkpointId, info.req.term,
947
           info.req.startTs, pRestoreInfo->streamId, num, pInfo->numOfTasks);
948
  }
949
}
950

951
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
11✔
952
  taosArrayDestroy(pInfo->pTaskList);
11✔
953
  pInfo->pTaskList = NULL;
11✔
954
}
11✔
955

956
int32_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
635✔
957
  int32_t code = 0;
635✔
958
  int32_t numOfStreams = taosHashGetSize(pHash);
635✔
959
  if (numOfStreams == 0) {
635✔
960
    return code;
622✔
961
  }
962

963
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
13✔
964
  if (code == 0) {
13!
965
    numOfStreams = taosHashGetSize(pHash);
13✔
966
    mInfo("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
13!
967
  } else {
968
    mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
969
  }
970

971
  return code;
13✔
972
}
973

974
int32_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
621✔
975
  int32_t code = 0;
621✔
976
  int32_t numOfStreams = taosHashGetSize(pHash);
621✔
977
  if (numOfStreams == 0) {
621✔
978
    return code;
279✔
979
  }
980

981
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
342✔
982
  if (code == 0) {
342✔
983
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
248!
984
  } else {
985
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
94!
986
  }
987

988
  return code;
342✔
989
}
990

991
int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
992
  SChkptReportInfo *pInfo = taosHashGet(pHash, &streamId, sizeof(streamId));
×
993
  if (pInfo != NULL) {
×
994
    taosArrayClear(pInfo->pTaskList);
×
995
    mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId,
×
996
           pInfo->reportChkpt);
997
    return 0;
×
998
  }
999

1000
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
1001
}
1002

1003
static void mndShowStreamStatus(char *dst, int8_t status) {
374✔
1004
  if (status == STREAM_STATUS__NORMAL) {
374✔
1005
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
369✔
1006
  } else if (status == STREAM_STATUS__STOP) {
5!
1007
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
1008
  } else if (status == STREAM_STATUS__FAILED) {
5✔
1009
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
1✔
1010
  } else if (status == STREAM_STATUS__RECOVER) {
4!
1011
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
1012
  } else if (status == STREAM_STATUS__PAUSE) {
4✔
1013
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
2✔
1014
  } else if (status == STREAM_STATUS__INIT) {
2!
1015
    tstrncpy(dst, "init", MND_STREAM_TRIGGER_NAME_SIZE);
2✔
1016
  }
1017
}
374✔
1018

1019
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
374✔
1020
  int8_t trigger = pStream->conf.trigger;
374✔
1021
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
374✔
1022
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
187✔
1023
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
187✔
1024
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
76✔
1025
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
111✔
1026
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
27✔
1027
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
84!
1028
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
84✔
1029
  }
1030
}
374✔
1031

1032
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
48,616✔
1033
  memset(pBuf, 0, bufLen);
48,616✔
1034
  pBuf[2] = '0';
48,616✔
1035
  pBuf[3] = 'x';
48,616✔
1036

1037
  int32_t len = tintToHex(id, &pBuf[4]);
48,616✔
1038
  varDataSetLen(pBuf, len + 2);
48,616✔
1039
}
48,616✔
1040

1041
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
374✔
1042
  int32_t          code = TSDB_CODE_SUCCESS;
374✔
1043
  int32_t          lino = 0;
374✔
1044
  SStreamTaskIter *pIter = NULL;
374✔
1045
  bool             isPaused = true;
374✔
1046
  int32_t          num = 0;
374✔
1047

1048
  taosRLockLatch(&pStream->lock);
374✔
1049
  code = createStreamTaskIter(pStream, &pIter);
374✔
1050
  TSDB_CHECK_CODE(code, lino, _end);
374!
1051

1052
  while (streamTaskIterNextTask(pIter)) {
1,852✔
1053
    SStreamTask *pTask = NULL;
1,478✔
1054
    code = streamTaskIterGetCurrent(pIter, &pTask);
1,478✔
1055
    TSDB_CHECK_CODE(code, lino, _end);
1,478!
1056

1057
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
1,478✔
1058
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
1,478✔
1059
    if (pe == NULL) {
1,478!
1060
      continue;
×
1061
    }
1062

1063
    if (pe->status != TASK_STATUS__PAUSE) {
1,478✔
1064
      isPaused = false;
1,476✔
1065
      mInfo("stream:0x%" PRIx64 " taskId:0x%" PRIx64 ", status:%d not paused, stream status not paused",
1,476!
1066
            pe->id.streamId, pe->id.taskId, pe->status);
1067
    } else {
1068
      mInfo("stream:0x%" PRIx64 " taskId:0x%" PRIx64 ", status: paused, stream status paused", pe->id.streamId,
2!
1069
            pe->id.taskId);
1070
    }
1071

1072
    num += 1;
1,478✔
1073
  }
1074

1075
  if (num > 0) {
374✔
1076
    (*pRes) = isPaused;
371✔
1077
  }
1078

1079
_end:
3✔
1080
  destroyStreamTaskIter(pIter);
374✔
1081
  taosRUnLockLatch(&pStream->lock);
374✔
1082
  if (code != TSDB_CODE_SUCCESS) {
374!
1083
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
1084
  }
1085
  return code;
374✔
1086
}
1087

1088
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
374✔
1089
  int32_t code = 0;
374✔
1090
  int32_t cols = 0;
374✔
1091
  int32_t lino = 0;
374✔
1092

1093
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
374✔
1094
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
374✔
1095
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1096
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1097

1098
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
374✔
1099
  TSDB_CHECK_CODE(code, lino, _end);
374!
1100

1101
  // create time
1102
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1103
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1104
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
374✔
1105
  TSDB_CHECK_CODE(code, lino, _end);
374!
1106

1107
  // stream id
1108
  char buf[128] = {0};
374✔
1109
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
374✔
1110
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1111
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1112
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
374✔
1113
  TSDB_CHECK_CODE(code, lino, _end);
374!
1114

1115
  // related fill-history stream id
1116
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1117
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1118
  if (pStream->hTaskUid != 0) {
374!
1119
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
1120
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1121
  } else {
1122
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
374✔
1123
  }
1124
  TSDB_CHECK_CODE(code, lino, _end);
374!
1125

1126
  // related fill-history stream id
1127
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
374✔
1128
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
374✔
1129
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1130
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1131
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
374✔
1132
  TSDB_CHECK_CODE(code, lino, _end);
374!
1133

1134
  char status[20 + VARSTR_HEADER_SIZE] = {0};
374✔
1135
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
374✔
1136
  bool isPaused = false;
374✔
1137
  code = isAllTaskPaused(pStream, &isPaused);
374✔
1138
  TSDB_CHECK_CODE(code, lino, _end);
374!
1139

1140
  int8_t streamStatus = atomic_load_8(&pStream->status);
374✔
1141
  if (isPaused && pStream->pTaskList != NULL) {
374!
1142
    streamStatus = STREAM_STATUS__PAUSE;
2✔
1143
  }
1144
  mndShowStreamStatus(status2, streamStatus);
374✔
1145
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
374✔
1146
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1147
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1148

1149
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
374✔
1150
  TSDB_CHECK_CODE(code, lino, _end);
374!
1151

1152
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
374✔
1153
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
374✔
1154
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1155
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1156

1157
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
374✔
1158
  TSDB_CHECK_CODE(code, lino, _end);
374!
1159

1160
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
374✔
1161
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
374✔
1162
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1163
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1164

1165
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
374✔
1166
  TSDB_CHECK_CODE(code, lino, _end);
374!
1167

1168
  if (pStream->targetSTbName[0] == 0) {
374!
1169
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1170
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1171

1172
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
×
1173
  } else {
1174
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
374✔
1175
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
374✔
1176
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1177
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1178

1179
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
374✔
1180
  }
1181
  TSDB_CHECK_CODE(code, lino, _end);
374!
1182

1183
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1184
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1185

1186
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
374✔
1187
  TSDB_CHECK_CODE(code, lino, _end);
374!
1188

1189
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
374✔
1190
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
374✔
1191
  mndShowStreamTrigger(trigger2, pStream);
374✔
1192
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
374✔
1193
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1194
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1195

1196
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
374✔
1197
  TSDB_CHECK_CODE(code, lino, _end);
374!
1198

1199
  // sink_quota
1200
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
374✔
1201
  sinkQuota[0] = '0';
374✔
1202
  char dstStr[20] = {0};
374✔
1203
  STR_TO_VARSTR(dstStr, sinkQuota)
374✔
1204
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1205
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1206

1207
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
374✔
1208
  TSDB_CHECK_CODE(code, lino, _end);
374!
1209

1210
  // checkpoint interval
1211
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
374✔
1212
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
374✔
1213
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
374✔
1214

1215
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1216
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1217

1218
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
374✔
1219
  TSDB_CHECK_CODE(code, lino, _end);
374!
1220

1221
  // checkpoint backup type
1222
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
374✔
1223
  STR_TO_VARSTR(backup, "none")
374✔
1224
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1225
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1226

1227
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
374✔
1228
  TSDB_CHECK_CODE(code, lino, _end);
374!
1229

1230
  // history scan idle
1231
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
374✔
1232
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
374✔
1233

1234
  memset(dstStr, 0, tListLen(dstStr));
374✔
1235
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
374✔
1236
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1237
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1238

1239
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
374✔
1240
  TSDB_CHECK_CODE(code, lino, _end);
374!
1241

1242
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
374✔
1243
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
374!
1244
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
374✔
1245
  if (streamStatus == STREAM_STATUS__FAILED){
374✔
1246
    STR_TO_VARSTR(msg, pStream->reserve)
1✔
1247
  } else {
1248
    STR_TO_VARSTR(msg, " ")
373✔
1249
  }
1250
  code = colDataSetVal(pColInfo, numOfRows, (const char *)msg, false);
374✔
1251

1252
_end:
374✔
1253
  if (code) {
374!
1254
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1255
  }
1256
  return code;
374✔
1257
}
1258

1259
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
47,626✔
1260
                              int32_t precision) {
1261
  SColumnInfoData *pColInfo = NULL;
47,626✔
1262
  int32_t          cols = 0;
47,626✔
1263
  int32_t          code = 0;
47,626✔
1264
  int32_t          lino = 0;
47,626✔
1265

1266
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
47,626✔
1267

1268
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
47,626✔
1269
  if (pe == NULL) {
47,626!
1270
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1271
           " no valid status/stage info",
1272
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1273
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1274
  }
1275

1276
  // stream name
1277
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
47,626✔
1278
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
47,626✔
1279

1280
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1281
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1282

1283
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
47,626✔
1284
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1285

1286
  // task id
1287
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1288
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1289

1290
  char idstr[128] = {0};
47,626✔
1291
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
47,626✔
1292
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
47,626✔
1293
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1294

1295
  // node type
1296
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
47,626✔
1297
  varDataSetLen(nodeType, 5);
47,626✔
1298
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1299
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1300

1301
  if (pTask->info.nodeId > 0) {
47,626✔
1302
    memcpy(varDataVal(nodeType), "vnode", 5);
47,511✔
1303
  } else {
1304
    memcpy(varDataVal(nodeType), "snode", 5);
115✔
1305
  }
1306
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
47,626✔
1307
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1308

1309
  // node id
1310
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1311
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1312

1313
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
47,626✔
1314
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
47,626✔
1315
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1316

1317
  // level
1318
  char level[20 + VARSTR_HEADER_SIZE] = {0};
47,626✔
1319
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
47,626✔
1320
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
27,560✔
1321
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
20,066✔
1322
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
1,609✔
1323
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
18,457!
1324
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
18,457✔
1325
  } else if (pTask->info.taskLevel == TASK_LEVEL__MERGE) {
×
1326
    STR_WITH_SIZE_TO_VARSTR(level, "merge", 5);
×
1327
  }
1328

1329
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1330
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1331

1332
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
47,626✔
1333
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1334

1335
  // status
1336
  char status[20 + VARSTR_HEADER_SIZE] = {0};
47,626✔
1337

1338
  const char *pStatus = streamTaskGetStatusStr(pe->status);
47,626✔
1339
  STR_TO_VARSTR(status, pStatus);
47,626✔
1340

1341
  // status
1342
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1343
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1344

1345
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
47,626✔
1346
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1347

1348
  // stage
1349
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1350
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1351

1352
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
47,626✔
1353
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1354

1355
  // input queue
1356
  char        vbuf[TSDB_STREAM_NOTIFY_STAT_LEN + 2] = {0};
47,626✔
1357
  char        buf[TSDB_STREAM_NOTIFY_STAT_LEN] = {0};
47,626✔
1358
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
47,626✔
1359
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
47,626✔
1360
  STR_TO_VARSTR(vbuf, buf);
47,626✔
1361

1362
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1363
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1364

1365
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
47,626✔
1366
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1367

1368
  // input total
1369
  const char *formatTotalMb = "%7.2f MiB";
47,626✔
1370
  const char *formatTotalGb = "%7.2f GiB";
47,626✔
1371
  if (pe->procsTotal < 1024) {
47,626!
1372
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
47,626✔
1373
  } else {
1374
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1375
  }
1376

1377
  memset(vbuf, 0, tListLen(vbuf));
47,626✔
1378
  STR_TO_VARSTR(vbuf, buf);
47,626✔
1379

1380
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1381
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1382

1383
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
47,626✔
1384
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1385

1386
  // process throughput
1387
  const char *formatKb = "%7.2f KiB/s";
47,626✔
1388
  const char *formatMb = "%7.2f MiB/s";
47,626✔
1389
  if (pe->procsThroughput < 1024) {
47,626✔
1390
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
47,536✔
1391
  } else {
1392
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
90✔
1393
  }
1394

1395
  memset(vbuf, 0, tListLen(vbuf));
47,626✔
1396
  STR_TO_VARSTR(vbuf, buf);
47,626✔
1397

1398
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1399
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1400

1401
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
47,626✔
1402
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1403

1404
  // output total
1405
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1406
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1407

1408
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
47,626✔
1409
    colDataSetNULL(pColInfo, numOfRows);
18,457!
1410
  } else {
1411
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
29,169✔
1412
    memset(vbuf, 0, tListLen(vbuf));
29,169✔
1413
    STR_TO_VARSTR(vbuf, buf);
29,169✔
1414

1415
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
29,169✔
1416
    TSDB_CHECK_CODE(code, lino, _end);
29,169!
1417
  }
1418

1419
  // output throughput
1420
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1421
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1422

1423
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
47,626✔
1424
    colDataSetNULL(pColInfo, numOfRows);
18,457!
1425
  } else {
1426
    if (pe->outputThroughput < 1024) {
29,169✔
1427
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
29,163✔
1428
    } else {
1429
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
6✔
1430
    }
1431

1432
    memset(vbuf, 0, tListLen(vbuf));
29,169✔
1433
    STR_TO_VARSTR(vbuf, buf);
29,169✔
1434

1435
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
29,169✔
1436
    TSDB_CHECK_CODE(code, lino, _end);
29,169!
1437
  }
1438
  // info
1439
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
47,626✔
1440
    const char *sinkStr = "%.2f MiB";
18,457✔
1441
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
18,457✔
1442
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
29,169✔
1443
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
27,560✔
1444
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
8,495✔
1445
      if (ret != 0) {
8,495!
1446
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1447
        memset(buf, 0, tListLen(buf));
×
1448
      }
1449
    } else {
1450
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
19,065✔
1451
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
19,065✔
1452
    }
1453
  } else {
1454
    memset(buf, 0, tListLen(buf));
1,609✔
1455
  }
1456

1457
  STR_TO_VARSTR(vbuf, buf);
47,626✔
1458

1459
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1460
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1461

1462
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
47,626✔
1463
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1464

1465
  // start_time
1466
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1467
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1468

1469
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
47,626✔
1470
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1471

1472
  // start id
1473
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1474
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1475

1476
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
47,626✔
1477
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1478

1479
  // start ver
1480
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1481
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1482

1483
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
47,626✔
1484
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1485

1486
  // checkpoint time
1487
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1488
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1489

1490
  if (pe->checkpointInfo.latestTime != 0) {
47,626✔
1491
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
797✔
1492
  } else {
1493
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
46,829✔
1494
  }
1495
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1496

1497
  // checkpoint_id
1498
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1499
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1500

1501
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
47,626✔
1502
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1503

1504
  // checkpoint version
1505
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1506
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1507

1508
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
47,626✔
1509
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1510

1511
  // checkpoint size
1512
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1513
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1514

1515
  colDataSetNULL(pColInfo, numOfRows);
47,626!
1516

1517
  // checkpoint backup status
1518
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1519
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1520

1521
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
47,626✔
1522
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1523

1524
  // ds_err_info
1525
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1526
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1527

1528
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
47,626✔
1529
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1530

1531
  // history_task_id
1532
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1533
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1534

1535
  if (pe->hTaskId != 0) {
47,626✔
1536
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
616✔
1537
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
616✔
1538
  } else {
1539
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
47,010✔
1540
  }
1541
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1542

1543
  // history_task_status
1544
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1545
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1546

1547
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
47,626✔
1548
  TSDB_CHECK_CODE(code, lino, _end);
47,626!
1549

1550
  // notify_event_stat
1551
  int32_t offset =0;
47,626✔
1552
  if (pe->notifyEventStat.notifyEventAddTimes > 0) {
47,626!
1553
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Add %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1554
                        pe->notifyEventStat.notifyEventAddTimes, pe->notifyEventStat.notifyEventAddElems,
1555
                        pe->notifyEventStat.notifyEventAddCostSec);
1556
  }
1557
  if (pe->notifyEventStat.notifyEventPushTimes > 0) {
47,626!
1558
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Push %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1559
                        pe->notifyEventStat.notifyEventPushTimes, pe->notifyEventStat.notifyEventPushElems,
1560
                        pe->notifyEventStat.notifyEventPushCostSec);
1561
  }
1562
  if (pe->notifyEventStat.notifyEventPackTimes > 0) {
47,626!
1563
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Pack %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1564
                        pe->notifyEventStat.notifyEventPackTimes, pe->notifyEventStat.notifyEventPackElems,
1565
                        pe->notifyEventStat.notifyEventPackCostSec);
1566
  }
1567
  if (pe->notifyEventStat.notifyEventSendTimes > 0) {
47,626!
1568
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Send %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1569
                        pe->notifyEventStat.notifyEventSendTimes, pe->notifyEventStat.notifyEventSendElems,
1570
                        pe->notifyEventStat.notifyEventSendCostSec);
1571
  }
1572
  if (pe->notifyEventStat.notifyEventHoldElems > 0) {
47,626!
1573
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "[Hold %" PRId64 " elems] ",
×
1574
                        pe->notifyEventStat.notifyEventHoldElems);
1575
  }
1576
  TSDB_CHECK_CONDITION(offset < sizeof(buf), code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
47,626!
1577
  buf[offset] = '\0';
47,626✔
1578

1579
  STR_TO_VARSTR(vbuf, buf);
47,626✔
1580

1581
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
47,626✔
1582
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
47,626!
1583

1584
  if (offset == 0) {
47,626!
1585
    colDataSetNULL(pColInfo, numOfRows);
47,626!
1586
  } else {
1587
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1588
    TSDB_CHECK_CODE(code, lino, _end);
×
1589
  }
1590

1591
_end:
×
1592
  if (code) {
47,626!
1593
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1594
  }
1595
  return code;
47,626✔
1596
}
1597

1598
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
944✔
1599
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
944✔
1600
  const SEp *p = GET_ACTIVE_EP(pCurrent);
944✔
1601

1602
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
944!
1603
    return false;
944✔
1604
  }
1605
  return true;
×
1606
}
1607

1608
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
279✔
1609
  if (pInfo != NULL) {
279!
1610
    taosArrayDestroy(pInfo->pUpdateNodeList);
279✔
1611
    taosHashCleanup(pInfo->pDBMap);
279✔
1612
  }
1613
}
279✔
1614

1615
// 1. increase the replica does not affect the stream process.
1616
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1617
// tasks on the will be removed replica.
1618
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1619
// will handle it as mentioned in 1 & 2 items.
1620
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
279✔
1621
                               SVgroupChangeInfo *pInfo) {
1622
  int32_t code = 0;
279✔
1623
  int32_t lino = 0;
279✔
1624

1625
  if (pInfo == NULL) {
279!
1626
    return TSDB_CODE_INVALID_PARA;
×
1627
  }
1628

1629
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
279✔
1630
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
279✔
1631

1632
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
279!
1633
    mndDestroyVgroupChangeInfo(pInfo);
×
1634
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1635
  }
1636

1637
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
279✔
1638
  for (int32_t i = 0; i < numOfNodes; ++i) {
2,489✔
1639
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
2,210✔
1640
    if (pPrevEntry == NULL) {
2,210!
1641
      continue;
×
1642
    }
1643

1644
    int32_t num = taosArrayGetSize(pNodeList);
2,210✔
1645
    for (int32_t j = 0; j < num; ++j) {
7,880✔
1646
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
6,621✔
1647
      if (pCurrent == NULL) {
6,621!
1648
        continue;
×
1649
      }
1650

1651
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
6,621✔
1652
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
951!
1653
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
7✔
1654

1655
          char buf[256] = {0};
7✔
1656
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
7✔
1657
          if (code) {
7!
1658
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1659
            TSDB_CHECK_CODE(code, lino, _err);
×
1660
          }
1661

1662
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
7!
1663
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1664

1665
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
7✔
1666
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
7✔
1667
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
7✔
1668

1669
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
7✔
1670
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
7!
1671
        }
1672

1673
        // todo handle the snode info
1674
        if (pCurrent->nodeId != SNODE_HANDLE) {
951✔
1675
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
919✔
1676
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
919✔
1677
          mndReleaseVgroup(pMnode, pVgroup);
919✔
1678
          TSDB_CHECK_CODE(code, lino, _err);
919!
1679
        }
1680

1681
        break;
951✔
1682
      }
1683
    }
1684
  }
1685

1686
  return code;
279✔
1687

1688
_err:
×
1689
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1690
  mndDestroyVgroupChangeInfo(pInfo);
×
1691
  return code;
×
1692
}
1693

1694
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
1,385✔
1695
  bool              allReady = false;
1,385✔
1696
  bool              nodeUpdated = false;
1,385✔
1697
  SVgroupChangeInfo changeInfo = {0};
1,385✔
1698

1699
  int32_t numOfNodes = extractStreamNodeList(pMnode);
1,385✔
1700

1701
  if (numOfNodes == 0) {
1,385✔
1702
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
1,090✔
1703
    execInfo.ts = taosGetTimestampSec();
1,090✔
1704
    return false;
1,090✔
1705
  }
1706

1707
  for (int32_t i = 0; i < numOfNodes; ++i) {
2,532✔
1708
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
2,258✔
1709
    if (pNodeEntry == NULL) {
2,258!
1710
      continue;
×
1711
    }
1712

1713
    if (pNodeEntry->stageUpdated) {
2,258✔
1714
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
21!
1715
      return true;
21✔
1716
    }
1717
  }
1718

1719
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot, NULL);
274✔
1720
  if (code) {
274!
1721
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1722
  }
1723

1724
  if (!allReady) {
274✔
1725
    mWarn("not all vnodes ready, quit from vnodes status check");
6!
1726
    return true;
6✔
1727
  }
1728

1729
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
268✔
1730
  if (code) {
268!
1731
    nodeUpdated = false;
×
1732
  } else {
1733
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
268✔
1734
    if (nodeUpdated) {
268!
1735
      mDebug("stream tasks not ready due to node update");
×
1736
    }
1737
  }
1738

1739
  mndDestroyVgroupChangeInfo(&changeInfo);
268✔
1740
  return nodeUpdated;
268✔
1741
}
1742

1743
// check if the node update happens or not
1744
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
1,385✔
1745
  SArray *pNodeSnapshot = NULL;
1,385✔
1746

1747
  streamMutexLock(&execInfo.lock);
1,385✔
1748
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
1,385✔
1749
  streamMutexUnlock(&execInfo.lock);
1,385✔
1750

1751
  taosArrayDestroy(pNodeSnapshot);
1,385✔
1752
  return updated;
1,385✔
1753
}
1754

1755
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,057✔
1756
  SSdb      *pSdb = pMnode->pSdb;
1,057✔
1757
  void      *pIter = NULL;
1,057✔
1758
  SSnodeObj *pObj = NULL;
1,057✔
1759

1760
  if (pSrcDb->cfg.replications == 1) {
1,057✔
1761
    return TSDB_CODE_SUCCESS;
1,054✔
1762
  } else {
1763
    while (1) {
1764
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
3✔
1765
      if (pIter == NULL) {
3✔
1766
        break;
2✔
1767
      }
1768

1769
      sdbRelease(pSdb, pObj);
1✔
1770
      sdbCancelFetch(pSdb, pIter);
1✔
1771
      return TSDB_CODE_SUCCESS;
1✔
1772
    }
1773

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