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

taosdata / TDengine / #3568

27 Dec 2024 02:27PM UTC coverage: 63.286% (+0.6%) from 62.733%
#3568

push

travis-ci

web-flow
Merge pull request #29378 from taosdata/revert-29377-revert-29165-enh/TD-29974-improve-trans

Revert "Revert "Enh:[td 29974]improve trans""

139941 of 284289 branches covered (49.22%)

Branch coverage included in aggregate %.

105 of 355 new or added lines in 12 files covered. (29.58%)

4077 existing lines in 132 files now uncovered.

218122 of 281494 relevant lines covered (77.49%)

19355607.91 hits per line

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

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

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

46
  return 0;
104,313✔
47
}
48

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

55
  if (pIter->level == -1) {
473,358✔
56
    pIter->level += 1;
104,309✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
677,795✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
573,351✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
572,578✔
62
      pIter->level += 1;
204,437✔
63
      pIter->ordinalIndex = 0;
204,437✔
64
      pIter->pTask = NULL;
204,437✔
65
      continue;
204,437✔
66
    }
67

68
    pIter->pTask = taosArrayGetP(pList, pIter->ordinalIndex);
369,291✔
69
    pIter->ordinalIndex += 1;
368,933✔
70
    return true;
368,933✔
71
  }
72

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

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

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

106
  return true;
302,041✔
107
}
108

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

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

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

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

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

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

149
  return code;
14,270✔
150
}
151

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

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

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

171
    if (objStatus != SDB_STATUS_READY) {
14,813✔
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);
14,811✔
179
  }
180

181
  return TSDB_CODE_SUCCESS;
14,149✔
182
}
183

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

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

197
  while (1) {
305,747✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
320,017✔
199
    if (pIter == NULL) {
320,017✔
200
      break;
14,270✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
305,747✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
305,747✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
152,451✔
209
      if (code) {
152,451!
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) {
153,296✔
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
534!
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
534✔
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) {
305,747✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
303,078✔
227
    }
228

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

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

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

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

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

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

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

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

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

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

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

300
  SStreamObj *p = NULL;
12,382✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
20,946✔
302
    if (p->uid == streamId) {
20,942✔
303
      sdbCancelFetch(pSdb, pIter);
12,378✔
304
      *pStream = p;
12,378✔
305
      return TSDB_CODE_SUCCESS;
12,378✔
306
    }
307
    sdbRelease(pSdb, p);
8,564✔
308
  }
309

310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
4✔
311
}
312

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

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

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

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

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

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

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

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

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

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
74,421✔
396
  int32_t num = 0;
74,421✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
221,444✔
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
147,007✔
399
    num += taosArrayGetSize(pLevel);
147,046✔
400
  }
401

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

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

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

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

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

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

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

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

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

484
    for (int32_t j = 0; j < size; ++j) {
131,401✔
485
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
130,352✔
486
      if (pEntry == NULL) {
130,352!
487
        continue;
×
488
      }
489

490
      if (pEntry->nodeId == p->nodeId) {
130,352✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
6,727✔
492

493
        void *px = taosArrayPush(pValidList, p);
6,727✔
494
        if (px == NULL) {
6,727!
495
          mError("failed to put node into list, nodeId:%d", p->nodeId);
×
496
        } else {
497
          mDebug("vgId:%d ts:%" PRId64 " HbMsgId:%d is valid", p->nodeId, p->hbTimestamp, p->lastHbMsgId);
6,727✔
498
        }
499
        break;
6,727✔
500
      }
501
    }
502
  }
503

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,220✔
538
}
539

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

573
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,350✔
574
    code = doRemoveTasks(pExecNode, &id);
7,350✔
575
    if (code) {
7,350!
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));
379!
596
  }
597

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

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

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

611
    if (pEntry->nodeId == nodeId) {
149,594✔
612
      return true;
13,966✔
613
    }
614
  }
615

616
  return false;
×
617
}
618

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

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

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

637
    if (pEntry->nodeId == SNODE_HANDLE) {
14,802✔
638
      continue;
836✔
639
    }
640

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

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,327✔
656

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

661
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
25,609✔
662
  SMnode *pMnode = pReq->info.node;
25,609✔
663
  void   *pIter = NULL;
25,609✔
664
  int32_t code = 0;
25,609✔
665
  SArray *pDropped = taosArrayInit(4, sizeof(int64_t));
25,609✔
666
  if (pDropped == NULL) {
25,609!
667
    return terrno;
×
668
  }
669

670
  mDebug("start to scan checkpoint report info");
25,609✔
671
  streamMutexLock(&execInfo.lock);
25,609✔
672

673
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
85,509✔
674
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
60,979✔
675
    if (taosArrayGetSize(px->pTaskList) == 0) {
60,979✔
676
      continue;
59,831✔
677
    }
678

679
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
1,148✔
680
    if (pInfo == NULL) {
1,148!
681
      continue;
×
682
    }
683

684
    SStreamObj *pStream = NULL;
1,148✔
685
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
1,148✔
686
    if (pStream == NULL || code != 0) {
1,148!
687
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
688
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
689
      if (p == NULL) {
×
690
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
691
      }
692
      continue;
×
693
    }
694

695
    int32_t total = mndGetNumOfStreamTasks(pStream);
1,148✔
696
    int32_t existed = (int32_t)taosArrayGetSize(px->pTaskList);
1,148✔
697

698
    if (total == existed) {
1,148✔
699
      mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info",
1,079✔
700
             pStream->uid, pStream->name, total);
701

702
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,079✔
703
      if (code == 0) {
1,079!
704
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,079✔
705
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,079!
706
          taosArrayClear(px->pTaskList);
1,079✔
707
          px->reportChkpt = pInfo->checkpointId;
1,079✔
708
          mDebug("stream:0x%" PRIx64 " clear checkpoint-report list", pInfo->streamId);
1,079✔
709
        } else {
710
          mDebug("stream:0x%" PRIx64 " not launch chkpt-meta update trans, due to checkpoint not finished yet",
×
711
                 pInfo->streamId);
712
        }
713
        break;
1,079✔
714
      } else {
UNCOV
715
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
716
      }
717
    } else {
718
      mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pInfo->streamId, pStream->name,
69✔
719
             existed, total, total - existed);
720
    }
721

722
    sdbRelease(pMnode->pSdb, pStream);
69✔
723
  }
724

725
  int32_t size = taosArrayGetSize(pDropped);
25,609✔
726
  if (size > 0) {
25,609!
727
    for (int32_t i = 0; i < size; ++i) {
×
728
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
729
      if (pStreamId == NULL) {
×
730
        continue;
×
731
      }
732

733
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
734
      if (code) {
×
735
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
736
      }
737
    }
738

739
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
740
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
741
  }
742

743
  streamMutexUnlock(&execInfo.lock);
25,609✔
744

745
  taosArrayDestroy(pDropped);
25,609✔
746
  return TSDB_CODE_SUCCESS;
25,609✔
747
}
748

749
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int32_t taskId, int64_t checkpointId,
138✔
750
                                          int64_t ts) {
751
  char msg[128] = {0};
138✔
752
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for task:0x%x", taskId);
138✔
753

754
  STrans *pTrans = NULL;
138✔
755
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
138✔
756
  if (pTrans == NULL || code != 0) {
138!
757
    return terrno;
×
758
  }
759

760
  STaskId      id = {.streamId = pStream->uid, .taskId = taskId};
138✔
761
  SStreamTask *pTask = NULL;
138✔
762
  code = mndGetStreamTask(&id, pStream, &pTask);
138✔
763
  if (code) {
138!
764
    mError("failed to get task:0x%x in stream:%s, failed to create consensus-checkpointId", taskId, pStream->name);
×
765
    sdbRelease(pMnode->pSdb, pStream);
×
766
    return code;
×
767
  }
768

769
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
138✔
770
  if (code) {
138!
771
    sdbRelease(pMnode->pSdb, pStream);
×
772
    return code;
×
773
  }
774

775
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pTask, checkpointId, ts);
138✔
776
  if (code != 0) {
138!
777
    sdbRelease(pMnode->pSdb, pStream);
×
778
    mndTransDrop(pTrans);
×
779
    return code;
×
780
  }
781

782
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
138✔
783
  if (code) {
138!
784
    sdbRelease(pMnode->pSdb, pStream);
×
785
    mndTransDrop(pTrans);
×
786
    return code;
×
787
  }
788

789
  code = mndTransPrepare(pMnode, pTrans);
138✔
790
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
138!
791
    mError("trans:%d, failed to prepare set consensus-chkptId trans since %s", pTrans->id, terrstr());
5!
792
    sdbRelease(pMnode->pSdb, pStream);
5✔
793
    mndTransDrop(pTrans);
5✔
794
    return code;
5✔
795
  }
796

797
  sdbRelease(pMnode->pSdb, pStream);
133✔
798
  mndTransDrop(pTrans);
133✔
799

800
  return TSDB_CODE_ACTION_IN_PROGRESS;
133✔
801
}
802

803
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
169✔
804
  *pInfo = NULL;
169✔
805

806
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
169✔
807
  if (px != NULL) {
169✔
808
    *pInfo = px;
133✔
809
    return 0;
133✔
810
  }
811

812
  SCheckpointConsensusInfo p = {
36✔
813
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
36✔
814
      .numOfTasks = numOfTasks,
815
      .streamId = streamId,
816
  };
817

818
  if (p.pTaskList == NULL) {
36!
819
    return terrno;
×
820
  }
821

822
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
36✔
823
  if (code == 0) {
36!
824
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
36✔
825
    *pInfo = pChkptInfo;
36✔
826
  } else {
827
    *pInfo = NULL;
×
828
  }
829

830
  return code;
36✔
831
}
832

833
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
834
// discard the msg may lead to the lost of connections.
835
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
169✔
836
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
169✔
837
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
169✔
838

839
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pTaskList); ++i) {
525✔
840
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
382✔
841
    if (p == NULL) {
382!
842
      continue;
×
843
    }
844

845
    if (p->req.taskId == info.req.taskId) {
382✔
846
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update ts %" PRId64
26✔
847
             "->%" PRId64 " total existed:%d",
848
             pRestoreInfo->taskId, pRestoreInfo->streamId, p->req.startTs, info.req.startTs,
849
             (int32_t)taosArrayGetSize(pInfo->pTaskList));
850
      p->req.startTs = info.req.startTs;
26✔
851
      return;
26✔
852
    }
853
  }
854

855
  void *p = taosArrayPush(pInfo->pTaskList, &info);
143✔
856
  if (p == NULL) {
143!
857
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
858
  } else {
859
    int32_t num = taosArrayGetSize(pInfo->pTaskList);
143✔
860
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
143✔
861
           " waiting tasks:%d",
862
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
863
  }
864
}
865

866
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
35✔
867
  taosArrayDestroy(pInfo->pTaskList);
35✔
868
  pInfo->pTaskList = NULL;
35✔
869
}
35✔
870

871
int64_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
1,387✔
872
  int32_t code = 0;
1,387✔
873
  int32_t numOfStreams = taosHashGetSize(pHash);
1,387✔
874
  if (numOfStreams == 0) {
1,387✔
875
    return code;
1,352✔
876
  }
877

878
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
35✔
879
  if (code == 0) {
35!
880
    mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
35✔
881
  } else {
882
    mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
883
  }
884

885
  return code;
35✔
886
}
887

888
int64_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
1,352✔
889
  int32_t code = 0;
1,352✔
890
  int32_t numOfStreams = taosHashGetSize(pHash);
1,352✔
891
  if (numOfStreams == 0) {
1,352✔
892
    return code;
338✔
893
  }
894

895
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
1,014✔
896
  if (code == 0) {
1,014✔
897
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
635✔
898
  } else {
899
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
379!
900
  }
901

902
  return code;
1,014✔
903
}
904

905
int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
906
  SChkptReportInfo *pInfo = taosHashGet(pHash, &streamId, sizeof(streamId));
×
907
  if (pInfo != NULL) {
×
908
    taosArrayClear(pInfo->pTaskList);
×
909
    mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId,
×
910
           pInfo->reportChkpt);
911
    return 0;
×
912
  }
913

914
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
915
}
916

917
static void mndShowStreamStatus(char *dst, int8_t status) {
33,145✔
918
  if (status == STREAM_STATUS__NORMAL) {
33,145✔
919
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
33,132✔
920
  } else if (status == STREAM_STATUS__STOP) {
13!
921
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
922
  } else if (status == STREAM_STATUS__FAILED) {
13!
923
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
924
  } else if (status == STREAM_STATUS__RECOVER) {
13!
925
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
926
  } else if (status == STREAM_STATUS__PAUSE) {
13!
927
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
21✔
928
  }
929
}
33,145✔
930

931
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
33,134✔
932
  int8_t trigger = pStream->conf.trigger;
33,134✔
933
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
33,134✔
934
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
11,353✔
935
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
21,781✔
936
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
10,903✔
937
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
10,878✔
938
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
10,831✔
939
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
47!
940
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
70✔
941
  }
942
}
33,134✔
943

944
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
248,675✔
945
  memset(pBuf, 0, bufLen);
248,675✔
946
  pBuf[2] = '0';
248,675✔
947
  pBuf[3] = 'x';
248,675✔
948

949
  int32_t len = tintToHex(id, &pBuf[4]);
248,675✔
950
  varDataSetLen(pBuf, len + 2);
249,097✔
951
}
249,097✔
952

953
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
33,139✔
954
  int32_t          code = TSDB_CODE_SUCCESS;
33,139✔
955
  int32_t          lino = 0;
33,139✔
956
  SStreamTaskIter *pIter = NULL;
33,139✔
957
  bool             isPaused =  true;
33,139✔
958

959
  taosRLockLatch(&pStream->lock);
33,139✔
960
  code = createStreamTaskIter(pStream, &pIter);
33,200✔
961
  TSDB_CHECK_CODE(code, lino, _end);
33,177!
962

963
  while (streamTaskIterNextTask(pIter)) {
145,027✔
964
    SStreamTask *pTask = NULL;
110,934✔
965
    code = streamTaskIterGetCurrent(pIter, &pTask);
110,934✔
966
    TSDB_CHECK_CODE(code, lino, _end);
111,436!
967

968
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
111,436✔
969
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
111,436✔
970
    if (pe == NULL) {
111,850✔
971
      continue;
116✔
972
    }
973
    if (pe->status != TASK_STATUS__PAUSE) {
111,734✔
974
      isPaused = false;
111,727✔
975
    }
976
  }
977
  (*pRes) = isPaused;
33,130✔
978

979
_end:
33,130✔
980
  destroyStreamTaskIter(pIter);
33,130✔
981
  taosRUnLockLatch(&pStream->lock);
33,172✔
982
  if (code != TSDB_CODE_SUCCESS) {
33,195!
983
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
984
  }
985
  return code;
33,195✔
986
}
987

988
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
33,183✔
989
  int32_t code = 0;
33,183✔
990
  int32_t cols = 0;
33,183✔
991
  int32_t lino = 0;
33,183✔
992

993
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,183✔
994
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
33,183✔
995
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,188✔
996
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,158!
997

998
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
33,158✔
999
  TSDB_CHECK_CODE(code, lino, _end);
33,164!
1000

1001
  // create time
1002
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,164✔
1003
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,155!
1004
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
33,155✔
1005
  TSDB_CHECK_CODE(code, lino, _end);
33,143!
1006

1007
  // stream id
1008
  char buf[128] = {0};
33,143✔
1009
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
33,143✔
1010
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,174✔
1011
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,170!
1012
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
33,170✔
1013
  TSDB_CHECK_CODE(code, lino, _end);
33,161!
1014

1015
  // related fill-history stream id
1016
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,161✔
1017
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,159✔
1018
  if (pStream->hTaskUid != 0) {
33,150!
1019
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
1020
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1021
  } else {
1022
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
33,150✔
1023
  }
1024
  TSDB_CHECK_CODE(code, lino, _end);
33,142!
1025

1026
  // related fill-history stream id
1027
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
33,142✔
1028
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
33,142✔
1029
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,142✔
1030
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,146!
1031
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
33,146✔
1032
  TSDB_CHECK_CODE(code, lino, _end);
33,131!
1033

1034
  char status[20 + VARSTR_HEADER_SIZE] = {0};
33,131✔
1035
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
33,131✔
1036
  bool isPaused = false;
33,131✔
1037
  code = isAllTaskPaused(pStream, &isPaused);
33,131✔
1038
  TSDB_CHECK_CODE(code, lino, _end);
33,195!
1039

1040
  int8_t streamStatus = atomic_load_8(&pStream->status);
33,195✔
1041
  if (isPaused) {
33,157✔
1042
    streamStatus = STREAM_STATUS__PAUSE;
21✔
1043
  }
1044
  mndShowStreamStatus(status2, streamStatus);
33,157✔
1045
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
33,140✔
1046
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,140✔
1047
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,130!
1048

1049
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
33,130✔
1050
  TSDB_CHECK_CODE(code, lino, _end);
33,147!
1051

1052
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,147✔
1053
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
33,147✔
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,127✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,119!
1056

1057
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
33,119✔
1058
  TSDB_CHECK_CODE(code, lino, _end);
33,152!
1059

1060
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,152✔
1061
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
33,152✔
1062
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,152✔
1063
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,117!
1064

1065
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
33,117✔
1066
  TSDB_CHECK_CODE(code, lino, _end);
33,157!
1067

1068
  if (pStream->targetSTbName[0] == 0) {
33,157✔
1069
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
1070
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
2!
1071

1072
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
2✔
1073
  } else {
1074
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,155✔
1075
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
33,155✔
1076
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,167✔
1077
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,132!
1078

1079
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
33,132✔
1080
  }
1081
  TSDB_CHECK_CODE(code, lino, _end);
33,134!
1082

1083
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,134✔
1084
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,121!
1085

1086
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
33,121✔
1087
  TSDB_CHECK_CODE(code, lino, _end);
33,130!
1088

1089
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
33,130✔
1090
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
33,130✔
1091
  mndShowStreamTrigger(trigger2, pStream);
33,130✔
1092
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
33,125✔
1093
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,125✔
1094
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,151!
1095

1096
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
33,151✔
1097
  TSDB_CHECK_CODE(code, lino, _end);
33,159!
1098

1099
  // sink_quota
1100
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
33,159✔
1101
  sinkQuota[0] = '0';
33,159✔
1102
  char dstStr[20] = {0};
33,159✔
1103
  STR_TO_VARSTR(dstStr, sinkQuota)
33,159✔
1104
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,159✔
1105
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,157!
1106

1107
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,157✔
1108
  TSDB_CHECK_CODE(code, lino, _end);
33,156!
1109

1110
  // checkpoint interval
1111
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
33,156✔
1112
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
33,156✔
1113
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
33,177✔
1114

1115
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,177✔
1116
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,125!
1117

1118
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
33,125✔
1119
  TSDB_CHECK_CODE(code, lino, _end);
33,129!
1120

1121
  // checkpoint backup type
1122
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
33,129✔
1123
  STR_TO_VARSTR(backup, "none")
33,129✔
1124
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,129✔
1125
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,123!
1126

1127
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
33,123✔
1128
  TSDB_CHECK_CODE(code, lino, _end);
33,149!
1129

1130
  // history scan idle
1131
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
33,149✔
1132
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
33,149✔
1133

1134
  memset(dstStr, 0, tListLen(dstStr));
33,149✔
1135
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
33,149✔
1136
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,149✔
1137
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,142!
1138

1139
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,142✔
1140

1141
_end:
33,158✔
1142
  if (code) {
33,158!
1143
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1144
  }
1145
  return code;
33,166✔
1146
}
1147

1148
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
214,867✔
1149
                              int32_t precision) {
1150
  SColumnInfoData *pColInfo = NULL;
214,867✔
1151
  int32_t          cols = 0;
214,867✔
1152
  int32_t          code = 0;
214,867✔
1153
  int32_t          lino = 0;
214,867✔
1154

1155
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
214,867✔
1156

1157
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
214,867✔
1158
  if (pe == NULL) {
215,470!
1159
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1160
           " no valid status/stage info",
1161
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1162
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1163
  }
1164

1165
  // stream name
1166
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
215,470✔
1167
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
215,470✔
1168

1169
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,474✔
1170
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,110!
1171

1172
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
215,110✔
1173
  TSDB_CHECK_CODE(code, lino, _end);
215,062!
1174

1175
  // task id
1176
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,062✔
1177
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,874!
1178

1179
  char idstr[128] = {0};
214,874✔
1180
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
214,874✔
1181
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
215,535✔
1182
  TSDB_CHECK_CODE(code, lino, _end);
215,187!
1183

1184
  // node type
1185
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
215,187✔
1186
  varDataSetLen(nodeType, 5);
215,187✔
1187
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,187✔
1188
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,008!
1189

1190
  if (pTask->info.nodeId > 0) {
215,130✔
1191
    memcpy(varDataVal(nodeType), "vnode", 5);
197,844✔
1192
  } else {
1193
    memcpy(varDataVal(nodeType), "snode", 5);
17,286✔
1194
  }
1195
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
215,130✔
1196
  TSDB_CHECK_CODE(code, lino, _end);
215,093!
1197

1198
  // node id
1199
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,093✔
1200
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,884!
1201

1202
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
214,884✔
1203
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
214,884✔
1204
  TSDB_CHECK_CODE(code, lino, _end);
214,902!
1205

1206
  // level
1207
  char level[20 + VARSTR_HEADER_SIZE] = {0};
214,902✔
1208
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
214,902✔
1209
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
110,392✔
1210
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
104,510✔
1211
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
18,765✔
1212
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
85,745!
1213
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
86,153✔
1214
  }
1215

1216
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,902✔
1217
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,788!
1218

1219
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
214,788✔
1220
  TSDB_CHECK_CODE(code, lino, _end);
215,094!
1221

1222
  // status
1223
  char status[20 + VARSTR_HEADER_SIZE] = {0};
215,094✔
1224

1225
  const char *pStatus = streamTaskGetStatusStr(pe->status);
215,094✔
1226
  STR_TO_VARSTR(status, pStatus);
215,118✔
1227

1228
  // status
1229
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,118✔
1230
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,037!
1231

1232
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
215,037✔
1233
  TSDB_CHECK_CODE(code, lino, _end);
215,023!
1234

1235
  // stage
1236
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,023✔
1237
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,896!
1238

1239
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
214,896✔
1240
  TSDB_CHECK_CODE(code, lino, _end);
214,854!
1241

1242
  // input queue
1243
  char        vbuf[40] = {0};
214,854✔
1244
  char        buf[38] = {0};
214,854✔
1245
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
214,854✔
1246
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
214,854✔
1247
  STR_TO_VARSTR(vbuf, buf);
214,854✔
1248

1249
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,854✔
1250
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,289!
1251

1252
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
215,289✔
1253
  TSDB_CHECK_CODE(code, lino, _end);
215,156!
1254

1255
  // input total
1256
  const char *formatTotalMb = "%7.2f MiB";
215,156✔
1257
  const char *formatTotalGb = "%7.2f GiB";
215,156✔
1258
  if (pe->procsTotal < 1024) {
215,156✔
1259
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
215,133✔
1260
  } else {
1261
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
23✔
1262
  }
1263

1264
  memset(vbuf, 0, tListLen(vbuf));
215,156✔
1265
  STR_TO_VARSTR(vbuf, buf);
215,156✔
1266

1267
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,156✔
1268
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,327!
1269

1270
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
215,327✔
1271
  TSDB_CHECK_CODE(code, lino, _end);
215,158!
1272

1273
  // process throughput
1274
  const char *formatKb = "%7.2f KiB/s";
215,158✔
1275
  const char *formatMb = "%7.2f MiB/s";
215,158✔
1276
  if (pe->procsThroughput < 1024) {
215,158✔
1277
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
215,038✔
1278
  } else {
1279
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
120✔
1280
  }
1281

1282
  memset(vbuf, 0, tListLen(vbuf));
215,158✔
1283
  STR_TO_VARSTR(vbuf, buf);
215,158✔
1284

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

1288
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
215,317✔
1289
  TSDB_CHECK_CODE(code, lino, _end);
215,181!
1290

1291
  // output total
1292
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,181✔
1293
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,980!
1294

1295
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
215,301✔
1296
    colDataSetNULL(pColInfo, numOfRows);
86,152!
1297
  } else {
1298
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
129,149✔
1299
    memset(vbuf, 0, tListLen(vbuf));
129,382✔
1300
    STR_TO_VARSTR(vbuf, buf);
129,382✔
1301

1302
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
129,382✔
1303
    TSDB_CHECK_CODE(code, lino, _end);
129,302!
1304
  }
1305

1306
  // output throughput
1307
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,454✔
1308
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,021!
1309

1310
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
215,327✔
1311
    colDataSetNULL(pColInfo, numOfRows);
86,135!
1312
  } else {
1313
    if (pe->outputThroughput < 1024) {
129,192✔
1314
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
129,059✔
1315
    } else {
1316
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
133✔
1317
    }
1318

1319
    memset(vbuf, 0, tListLen(vbuf));
129,192✔
1320
    STR_TO_VARSTR(vbuf, buf);
129,192✔
1321

1322
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
129,192✔
1323
    TSDB_CHECK_CODE(code, lino, _end);
129,291!
1324
  }
1325
  // info
1326
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
215,426✔
1327
    const char *sinkStr = "%.2f MiB";
86,134✔
1328
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
86,134✔
1329
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
129,292✔
1330
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
110,530✔
1331
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
5,533✔
1332
      if (ret != 0) {
5,533!
1333
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1334
        memset(buf, 0, tListLen(buf));
×
1335
      }
1336
    } else {
1337
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
104,997✔
1338
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
104,997✔
1339
    }
1340
  } else {
1341
    memset(buf, 0, tListLen(buf));
18,762✔
1342
  }
1343

1344
  STR_TO_VARSTR(vbuf, buf);
215,426✔
1345

1346
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,426✔
1347
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
215,323!
1348

1349
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
215,323✔
1350
  TSDB_CHECK_CODE(code, lino, _end);
215,140!
1351

1352
  // start_time
1353
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
215,140✔
1354
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,921!
1355

1356
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
214,921✔
1357
  TSDB_CHECK_CODE(code, lino, _end);
214,786!
1358

1359
  // start id
1360
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,786✔
1361
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,621!
1362

1363
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
214,621✔
1364
  TSDB_CHECK_CODE(code, lino, _end);
214,629!
1365

1366
  // start ver
1367
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,629✔
1368
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,525!
1369

1370
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
214,525✔
1371
  TSDB_CHECK_CODE(code, lino, _end);
214,603!
1372

1373
  // checkpoint time
1374
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,603✔
1375
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,475!
1376

1377
  if (pe->checkpointInfo.latestTime != 0) {
214,477✔
1378
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
174,001✔
1379
  } else {
1380
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
40,476✔
1381
  }
1382
  TSDB_CHECK_CODE(code, lino, _end);
214,579!
1383

1384
  // checkpoint_id
1385
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,579✔
1386
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,455!
1387

1388
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
214,455✔
1389
  TSDB_CHECK_CODE(code, lino, _end);
214,580!
1390

1391
  // checkpoint version
1392
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,580✔
1393
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,426!
1394

1395
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
214,426✔
1396
  TSDB_CHECK_CODE(code, lino, _end);
214,516!
1397

1398
  // checkpoint size
1399
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,516✔
1400
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,340!
1401

1402
  colDataSetNULL(pColInfo, numOfRows);
214,344!
1403

1404
  // checkpoint backup status
1405
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,344✔
1406
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,261!
1407

1408
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
214,261✔
1409
  TSDB_CHECK_CODE(code, lino, _end);
214,290!
1410

1411
  // ds_err_info
1412
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,290✔
1413
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
214,168!
1414

1415
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
214,168✔
1416
  TSDB_CHECK_CODE(code, lino, _end);
214,042!
1417

1418
  // history_task_id
1419
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
214,042✔
1420
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
213,807✔
1421

1422
  if (pe->hTaskId != 0) {
213,805✔
1423
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
363✔
1424
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
363✔
1425
  } else {
1426
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
213,442✔
1427
  }
1428
  TSDB_CHECK_CODE(code, lino, _end);
213,953!
1429

1430
  // history_task_status
1431
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
213,953✔
1432
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
213,798!
1433

1434
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
213,798✔
1435
  TSDB_CHECK_CODE(code, lino, _end);
214,047!
1436

1437
_end:
214,047✔
1438
  if (code) {
214,047!
1439
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1440
  }
1441
  return code;
214,056✔
1442
}
1443

1444
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
11,928✔
1445
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
11,928✔
1446
  const SEp *p = GET_ACTIVE_EP(pCurrent);
11,928✔
1447

1448
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
11,928!
1449
    return false;
11,925✔
1450
  }
1451
  return true;
3✔
1452
}
1453

1454
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
2,537✔
1455
  if (pInfo != NULL) {
2,537!
1456
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,537✔
1457
    taosHashCleanup(pInfo->pDBMap);
2,537✔
1458
  }
1459
}
2,537✔
1460

1461
// 1. increase the replica does not affect the stream process.
1462
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1463
// tasks on the will be removed replica.
1464
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1465
// will handle it as mentioned in 1 & 2 items.
1466
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
2,537✔
1467
                               SVgroupChangeInfo *pInfo) {
1468
  int32_t code = 0;
2,537✔
1469
  int32_t lino = 0;
2,537✔
1470

1471
  if (pInfo == NULL) {
2,537!
1472
    return TSDB_CODE_INVALID_PARA;
×
1473
  }
1474

1475
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
2,537✔
1476
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
2,537✔
1477

1478
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
2,537!
1479
    mndDestroyVgroupChangeInfo(pInfo);
×
1480
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1481
  }
1482

1483
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,537✔
1484
  for (int32_t i = 0; i < numOfNodes; ++i) {
15,216✔
1485
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
12,679✔
1486
    if (pPrevEntry == NULL) {
12,679!
1487
      continue;
×
1488
    }
1489

1490
    int32_t num = taosArrayGetSize(pNodeList);
12,679✔
1491
    for (int32_t j = 0; j < num; ++j) {
215,290✔
1492
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
214,559✔
1493
      if (pCurrent == NULL) {
214,559!
1494
        continue;
×
1495
      }
1496

1497
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
214,559✔
1498
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
11,948✔
1499
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
23✔
1500

1501
          char buf[256] = {0};
23✔
1502
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
23✔
1503
          if (code) {
23!
1504
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1505
            TSDB_CHECK_CODE(code, lino, _err);
×
1506
          }
1507

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

1511
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
23✔
1512
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
23✔
1513
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
23✔
1514

1515
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
23✔
1516
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
23!
1517
        }
1518

1519
        // todo handle the snode info
1520
        if (pCurrent->nodeId != SNODE_HANDLE) {
11,948✔
1521
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
10,469✔
1522
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
10,469✔
1523
          mndReleaseVgroup(pMnode, pVgroup);
10,469✔
1524
          TSDB_CHECK_CODE(code, lino, _err);
10,469!
1525
        }
1526

1527
        break;
11,948✔
1528
      }
1529
    }
1530
  }
1531

1532
  return code;
2,537✔
1533

1534
_err:
×
1535
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1536
  mndDestroyVgroupChangeInfo(pInfo);
×
1537
  return code;
×
1538
}
1539

1540
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
1,953✔
1541
  bool              allReady = false;
1,953✔
1542
  bool              nodeUpdated = false;
1,953✔
1543
  SVgroupChangeInfo changeInfo = {0};
1,953✔
1544

1545
  int32_t numOfNodes = extractStreamNodeList(pMnode);
1,953✔
1546

1547
  if (numOfNodes == 0) {
1,953✔
1548
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
709✔
1549
    execInfo.ts = taosGetTimestampSec();
709✔
1550
    return false;
709✔
1551
  }
1552

1553
  for (int32_t i = 0; i < numOfNodes; ++i) {
7,347✔
1554
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
6,120✔
1555
    if (pNodeEntry == NULL) {
6,120!
1556
      continue;
×
1557
    }
1558

1559
    if (pNodeEntry->stageUpdated) {
6,120✔
1560
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
17✔
1561
      return true;
17✔
1562
    }
1563
  }
1564

1565
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot);
1,227✔
1566
  if (code) {
1,227!
1567
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1568
  }
1569

1570
  if (!allReady) {
1,227✔
1571
    mWarn("not all vnodes ready, quit from vnodes status check");
17!
1572
    return true;
17✔
1573
  }
1574

1575
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
1,210✔
1576
  if (code) {
1,210!
1577
    nodeUpdated = false;
×
1578
  } else {
1579
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
1,210✔
1580
    if (nodeUpdated) {
1,210✔
1581
      mDebug("stream tasks not ready due to node update");
1!
1582
    }
1583
  }
1584

1585
  mndDestroyVgroupChangeInfo(&changeInfo);
1,210✔
1586
  return nodeUpdated;
1,210✔
1587
}
1588

1589
// check if the node update happens or not
1590
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
1,953✔
1591
  SArray *pNodeSnapshot = NULL;
1,953✔
1592

1593
  streamMutexLock(&execInfo.lock);
1,953✔
1594
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
1,953✔
1595
  streamMutexUnlock(&execInfo.lock);
1,953✔
1596

1597
  taosArrayDestroy(pNodeSnapshot);
1,953✔
1598
  return updated;
1,953✔
1599
}
1600

1601
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,733✔
1602
  SSdb      *pSdb = pMnode->pSdb;
1,733✔
1603
  void      *pIter = NULL;
1,733✔
1604
  SSnodeObj *pObj = NULL;
1,733✔
1605

1606
  if (pSrcDb->cfg.replications == 1) {
1,733✔
1607
    return TSDB_CODE_SUCCESS;
1,730✔
1608
  } else {
1609
    while (1) {
1610
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
3✔
1611
      if (pIter == NULL) {
3✔
1612
        break;
2✔
1613
      }
1614

1615
      sdbRelease(pSdb, pObj);
1✔
1616
      sdbCancelFetch(pSdb, pIter);
1✔
1617
      return TSDB_CODE_SUCCESS;
1✔
1618
    }
1619

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