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

taosdata / TDengine / #3777

28 Mar 2025 10:15AM UTC coverage: 34.001% (+0.3%) from 33.709%
#3777

push

travis-ci

happyguoxy
test:alter lcov result

147935 of 598399 branches covered (24.72%)

Branch coverage included in aggregate %.

221604 of 488434 relevant lines covered (45.37%)

767728.4 hits per line

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

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

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

46
  return 0;
25✔
47
}
48

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

55
  if (pIter->level == -1) {
111✔
56
    pIter->level += 1;
25✔
57
  }
58

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

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

73
  pIter->pTask = NULL;
25✔
74
  return false;
25✔
75
}
76

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
25!
89

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

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

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

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

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

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

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

149
  return code;
356✔
150
}
151

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
353✔
182
}
183

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

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

197
  while (1) {
1,201✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,557✔
199
    if (pIter == NULL) {
1,557✔
200
      break;
356✔
201
    }
202

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

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

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

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

246
    if (pTermMap != NULL) {
1,201✔
247
      int64_t term = pVgroup->vnodeGid[0].syncTerm;
1,141✔
248
      code = taosHashPut(pTermMap, &pVgroup->vgId, sizeof(pVgroup->vgId), &term, sizeof(term));
1,141✔
249
      if (code) {
1,141!
250
        mError("failed to put vnode:%d term into hashMap, code:%s", pVgroup->vgId, tstrerror(code));
×
251
      }
252
    }
253

254
    sdbRelease(pSdb, pVgroup);
1,201✔
255
  }
256

257
_end:
356✔
258
  taosHashCleanup(pHash);
356✔
259
  return code;
356✔
260
}
261

262
int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList, SHashObj* pTermMap) {
356✔
263
  int32_t   code = 0;
356✔
264
  SArray   *pVgroupList = NULL;
356✔
265

266
  *pList = NULL;
356✔
267
  *allReady = true;
356✔
268

269
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
356✔
270
  if (pVgroupList == NULL) {
356!
271
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
272
    code = terrno;
×
273
    goto _err;
×
274
  }
275

276
  // 1. check for all vnodes status
277
  code = mndCheckAndAddVgroupsInfo(pMnode, pVgroupList, allReady, pTermMap);
356✔
278
  if (code) {
356!
279
    goto _err;
×
280
  }
281

282
  // 2. add snode info
283
  code = mndAddSnodeInfo(pMnode, pVgroupList);
356✔
284
  if (code) {
356!
285
    goto _err;
×
286
  }
287

288
  // 3. check for mnode status
289
  code = mndCheckMnodeStatus(pMnode);
356✔
290
  if (code != TSDB_CODE_SUCCESS) {
356✔
291
    *allReady = false;
3✔
292
  }
293

294
  *pList = pVgroupList;
356✔
295
  return code;
356✔
296

297
_err:
×
298
  *allReady = false;
×
299
  taosArrayDestroy(pVgroupList);
×
300
  return code;
×
301
}
302

303
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
25✔
304
  void *pIter = NULL;
25✔
305
  SSdb *pSdb = pMnode->pSdb;
25✔
306
  *pStream = NULL;
25✔
307

308
  SStreamObj *p = NULL;
25✔
309
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
27✔
310
    if (p->uid == streamId) {
26✔
311
      sdbCancelFetch(pSdb, pIter);
24✔
312
      *pStream = p;
24✔
313
      return TSDB_CODE_SUCCESS;
24✔
314
    }
315
    sdbRelease(pSdb, p);
2✔
316
  }
317

318
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
1✔
319
}
320

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

335
int32_t extractNodeEpset(SMnode *pMnode, SEpSet *pEpSet, bool *hasEpset, int32_t taskId, int32_t nodeId) {
20✔
336
  *hasEpset = false;
20✔
337

338
  pEpSet->numOfEps = 0;
20✔
339
  if (nodeId == SNODE_HANDLE) {
20✔
340
    SSnodeObj *pObj = NULL;
3✔
341
    void      *pIter = NULL;
3✔
342

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

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

375
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
×
376
  *pTask = NULL;
×
377

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

386
  while (streamTaskIterNextTask(pIter)) {
×
387
    code = streamTaskIterGetCurrent(pIter, &p);
×
388
    if (code) {
×
389
      continue;
×
390
    }
391

392
    if (p->id.taskId == pId->taskId) {
×
393
      destroyStreamTaskIter(pIter);
×
394
      *pTask = p;
×
395
      return 0;
×
396
    }
397
  }
398

399
  destroyStreamTaskIter(pIter);
×
400
  return TSDB_CODE_FAILED;
×
401
}
402

403
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
19✔
404
  int32_t num = 0;
19✔
405
  for (int32_t i = 0; i < taosArrayGetSize(pStream->pTaskList); ++i) {
72✔
406
    SArray *pLevel = taosArrayGetP(pStream->pTaskList, i);
53✔
407
    num += taosArrayGetSize(pLevel);
53✔
408
  }
409

410
  return num;
19✔
411
}
412

413
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
×
414
  SSdb   *pSdb = pMnode->pSdb;
×
415
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
×
416
  if (pDb == NULL) {
×
417
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
418
  }
419

420
  int32_t numOfStreams = 0;
×
421
  void   *pIter = NULL;
×
422
  while (1) {
×
423
    SStreamObj *pStream = NULL;
×
424
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
425
    if (pIter == NULL) break;
×
426

427
    if (pStream->sourceDbUid == pDb->uid) {
×
428
      numOfStreams++;
×
429
    }
430

431
    sdbRelease(pSdb, pStream);
×
432
  }
433

434
  *pNumOfStreams = numOfStreams;
×
435
  mndReleaseDb(pMnode, pDb);
×
436
  return 0;
×
437
}
438

439
static void freeTaskList(void *param) {
6✔
440
  SArray **pList = (SArray **)param;
6✔
441
  taosArrayDestroy(*pList);
6✔
442
}
6✔
443

444
int32_t mndInitExecInfo() {
64✔
445
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
64✔
446
  if (code) {
64!
447
    return code;
×
448
  }
449

450
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
64✔
451

452
  execInfo.pTaskList = taosArrayInit(4, sizeof(STaskId));
64✔
453
  execInfo.pTaskMap = taosHashInit(64, fn, true, HASH_NO_LOCK);
64✔
454
  execInfo.transMgmt.pDBTrans = taosHashInit(32, fn, true, HASH_NO_LOCK);
64✔
455
  execInfo.pTransferStateStreams = taosHashInit(32, fn, true, HASH_NO_LOCK);
64✔
456
  execInfo.pChkptStreams = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
64✔
457
  execInfo.pStreamConsensus = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
64✔
458
  execInfo.pNodeList = taosArrayInit(4, sizeof(SNodeEntry));
64✔
459
  execInfo.pKilledChkptTrans = taosArrayInit(4, sizeof(SStreamTaskResetMsg));
64✔
460

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

468
  execInfo.role = NODE_ROLE_UNINIT;
64✔
469
  execInfo.switchFromFollower = false;
64✔
470

471
  taosHashSetFreeFp(execInfo.pTransferStateStreams, freeTaskList);
64✔
472
  taosHashSetFreeFp(execInfo.pChkptStreams, freeTaskList);
64✔
473
  taosHashSetFreeFp(execInfo.pStreamConsensus, freeTaskList);
64✔
474
  return 0;
64✔
475
}
476

477
void removeExpiredNodeInfo(const SArray *pNodeSnapshot) {
8✔
478
  SArray *pValidList = taosArrayInit(4, sizeof(SNodeEntry));
8✔
479
  if (pValidList == NULL) {  // not continue
8!
480
    return;
×
481
  }
482

483
  int32_t size = taosArrayGetSize(pNodeSnapshot);
8✔
484
  int32_t oldSize = taosArrayGetSize(execInfo.pNodeList);
8✔
485

486
  for (int32_t i = 0; i < oldSize; ++i) {
27✔
487
    SNodeEntry *p = taosArrayGet(execInfo.pNodeList, i);
19✔
488
    if (p == NULL) {
19!
489
      continue;
×
490
    }
491

492
    for (int32_t j = 0; j < size; ++j) {
35!
493
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
35✔
494
      if (pEntry == NULL) {
35!
495
        continue;
×
496
      }
497

498
      if (pEntry->nodeId == p->nodeId) {
35✔
499
        p->hbTimestamp = pEntry->hbTimestamp;
19✔
500

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

512
  taosArrayDestroy(execInfo.pNodeList);
8✔
513
  execInfo.pNodeList = pValidList;
8✔
514

515
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
8!
516
         (int32_t)taosArrayGetSize(pValidList), oldSize);
517
}
518

519
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) {
2✔
520
  void *p = taosHashGet(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
2✔
521
  if (p == NULL) {
2!
522
    return TSDB_CODE_SUCCESS;
×
523
  }
524

525
  int32_t code = taosHashRemove(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
2✔
526
  if (code) {
2!
527
    return code;
×
528
  }
529

530
  for (int32_t k = 0; k < taosArrayGetSize(pExecNode->pTaskList); ++k) {
2!
531
    STaskId *pId = taosArrayGet(pExecNode->pTaskList, k);
2✔
532
    if (pId == NULL) {
2!
533
      continue;
×
534
    }
535

536
    if (pId->taskId == pRemovedId->taskId && pId->streamId == pRemovedId->streamId) {
2!
537
      taosArrayRemove(pExecNode->pTaskList, k);
2✔
538

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

545
  return TSDB_CODE_SUCCESS;
2✔
546
}
547

548
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
8✔
549
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
8!
550
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
551
    if (pId == NULL) {
×
552
      continue;
×
553
    }
554

555
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
556
    if (code) {
×
557
      mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId);
×
558
    }
559
  }
560
}
8✔
561

562
void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
2✔
563
  SStreamTaskIter *pIter = NULL;
2✔
564
  streamMutexLock(&pExecNode->lock);
2✔
565

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

574
  while (streamTaskIterNextTask(pIter)) {
4✔
575
    SStreamTask *pTask = NULL;
2✔
576
    code = streamTaskIterGetCurrent(pIter, &pTask);
2✔
577
    if (code) {
2!
578
      continue;
×
579
    }
580

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

588
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
2!
589
    streamMutexUnlock(&pExecNode->lock);
×
590
    destroyStreamTaskIter(pIter);
×
591
    mError("task map size, task list size, not equal");
×
592
    return;
×
593
  }
594

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

601
  code = mndClearChkptReportInfo(execInfo.pChkptStreams, pStream->uid);
2✔
602
  if (code) {
2!
603
    mError("failed to clear the checkpoint report info, code:%s", tstrerror(code));
×
604
  }
605

606
  streamMutexUnlock(&pExecNode->lock);
2✔
607
  destroyStreamTaskIter(pIter);
2✔
608
}
609

610
static bool taskNodeExists(SArray *pList, int32_t nodeId) {
28✔
611
  size_t num = taosArrayGetSize(pList);
28✔
612

613
  for (int32_t i = 0; i < num; ++i) {
40!
614
    SNodeEntry *pEntry = taosArrayGet(pList, i);
40✔
615
    if (pEntry == NULL) {
40!
616
      continue;
×
617
    }
618

619
    if (pEntry->nodeId == nodeId) {
40✔
620
      return true;
28✔
621
    }
622
  }
623

624
  return false;
×
625
}
626

627
int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) {
8✔
628
  SArray *pRemovedTasks = taosArrayInit(4, sizeof(STaskId));
8✔
629
  if (pRemovedTasks == NULL) {
8!
630
    return terrno;
×
631
  }
632

633
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
8✔
634
  for (int32_t i = 0; i < numOfTask; ++i) {
41✔
635
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
33✔
636
    if (pId == NULL) {
33!
637
      continue;
×
638
    }
639

640
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, pId, sizeof(*pId));
33✔
641
    if (pEntry == NULL) {
33!
642
      continue;
×
643
    }
644

645
    if (pEntry->nodeId == SNODE_HANDLE) {
33✔
646
      continue;
5✔
647
    }
648

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

658
  removeTasksInBuf(pRemovedTasks, &execInfo);
8✔
659

660
  mDebug("remove invalid stream tasks:%d, remain:%d", (int32_t)taosArrayGetSize(pRemovedTasks),
8!
661
         (int32_t)taosArrayGetSize(execInfo.pTaskList));
662

663
  removeExpiredNodeInfo(pNodeSnapshot);
8✔
664

665
  taosArrayDestroy(pRemovedTasks);
8✔
666
  return 0;
8✔
667
}
668

669
static int32_t allTasksSendChkptReport(SChkptReportInfo* pReportInfo, int32_t numOfTasks, const char* pName) {
1✔
670
  int64_t checkpointId = -1;
1✔
671
  int32_t transId = -1;
1✔
672
  int32_t taskId = -1;
1✔
673

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

681
  // acquire current active checkpointId, and do cross-check checkpointId info in exec.pTaskList
682
  for(int32_t i = 0; i < numOfTasks; ++i) {
6✔
683
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
5✔
684
    if (pInfo == NULL) {
5!
685
      continue;
×
686
    }
687

688
    if (checkpointId == -1) {
5✔
689
      checkpointId = pInfo->checkpointId;
1✔
690
      transId = pInfo->transId;
1✔
691
      taskId = pInfo->taskId;
1✔
692
    } else if (checkpointId != pInfo->checkpointId) {
4!
693
      mError("stream:0x%" PRIx64
×
694
             " checkpointId in checkpoint-report list are not identical, type 1 taskId:0x%x checkpointId:%" PRId64
695
             ", type 2 taskId:0x%x checkpointId:%" PRId64,
696
             pReportInfo->streamId, taskId, checkpointId, pInfo->taskId, pInfo->checkpointId);
697
      return -1;
×
698
    }
699
  }
700

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

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

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

729
  mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info", id.streamId,
1!
730
         pName, numOfTasks);
731

732
  return TSDB_CODE_SUCCESS;
1✔
733
}
734

735
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
80✔
736
  SMnode *pMnode = pReq->info.node;
80✔
737
  void   *pIter = NULL;
80✔
738
  int32_t code = 0;
80✔
739
  int32_t lino = 0;
80✔
740
  SArray *pDropped = NULL;
80✔
741

742
  mDebug("start to scan checkpoint report info");
80!
743

744
  streamMutexLock(&execInfo.lock);
80✔
745

746
  int32_t num = taosHashGetSize(execInfo.pChkptStreams);
80✔
747
  if (num == 0) {
80✔
748
    goto _end;
23✔
749
  }
750

751
  pDropped = taosArrayInit(4, sizeof(int64_t));
57✔
752
  TSDB_CHECK_NULL(pDropped, code, lino, _end, terrno);
57!
753

754
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
113✔
755
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
57✔
756
    if (taosArrayGetSize(px->pTaskList) == 0) {
57✔
757
      continue;
56✔
758
    }
759

760
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
1✔
761
    if (pInfo == NULL) {
1!
762
      continue;
×
763
    }
764

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

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

793
        sdbRelease(pMnode->pSdb, pStream);
1✔
794
        break;
1✔
795
      } else {
796
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
797
      }
798
    }
799

800
    sdbRelease(pMnode->pSdb, pStream);
×
801
  }
802

803
  int32_t size = taosArrayGetSize(pDropped);
57✔
804
  if (size > 0) {
57!
805
    for (int32_t i = 0; i < size; ++i) {
×
806
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
807
      if (pStreamId == NULL) {
×
808
        continue;
×
809
      }
810

811
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
812
      if (code) {
×
813
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
814
      }
815
    }
816

817
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
818
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
819
  }
820

821
_end:
57✔
822
  streamMutexUnlock(&execInfo.lock);
80✔
823

824
  if (pDropped != NULL) {
80✔
825
    taosArrayDestroy(pDropped);
57✔
826
  }
827

828
  mDebug("end to scan checkpoint report info")
80!
829
  return code;
80✔
830
}
831

832
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId, SArray* pList) {
3✔
833
  char    msg[128] = {0};
3✔
834
  STrans *pTrans = NULL;
3✔
835

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

838
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
3✔
839
  if (pTrans == NULL || code != 0) {
3!
840
    return terrno;
×
841
  }
842

843
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
3✔
844
  if (code) {
3!
845
    sdbRelease(pMnode->pSdb, pStream);
×
846
    return code;
×
847
  }
848

849
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pStream, checkpointId, pList);
3✔
850
  if (code != 0) {
3!
851
    sdbRelease(pMnode->pSdb, pStream);
×
852
    mndTransDrop(pTrans);
×
853
    return code;
×
854
  }
855

856
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
3✔
857
  if (code) {
3!
858
    sdbRelease(pMnode->pSdb, pStream);
×
859
    mndTransDrop(pTrans);
×
860
    return code;
×
861
  }
862

863
  code = mndTransPrepare(pMnode, pTrans);
3✔
864

865
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
3!
866
    mError("trans:%d, failed to prepare set consensus-chkptId trans for stream:0x%" PRId64 " since %s", pTrans->id,
×
867
           pStream->uid, tstrerror(code));
868
    sdbRelease(pMnode->pSdb, pStream);
×
869
    mndTransDrop(pTrans);
×
870
    return code;
×
871
  }
872

873
  sdbRelease(pMnode->pSdb, pStream);
3✔
874
  mndTransDrop(pTrans);
3✔
875

876
  return TSDB_CODE_ACTION_IN_PROGRESS;
3✔
877
}
878

879
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
8✔
880
  *pInfo = NULL;
8✔
881

882
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
8✔
883
  if (px != NULL) {
8✔
884
    *pInfo = px;
4✔
885
    return 0;
4✔
886
  }
887

888
  SCheckpointConsensusInfo p = {
4✔
889
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
4✔
890
      .numOfTasks = numOfTasks,
891
      .streamId = streamId,
892
  };
893

894
  if (p.pTaskList == NULL) {
4!
895
    return terrno;
×
896
  }
897

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

906
  return code;
4✔
907
}
908

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

915
  int32_t num = (int32_t) taosArrayGetSize(pInfo->pTaskList);
8✔
916
  for (int32_t i = 0; i < num; ++i) {
18✔
917
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
10✔
918
    if (p == NULL) {
10!
919
      continue;
×
920
    }
921

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

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

948
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
3✔
949
  taosArrayDestroy(pInfo->pTaskList);
3✔
950
  pInfo->pTaskList = NULL;
3✔
951
}
3✔
952

953
int32_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
6✔
954
  int32_t code = 0;
6✔
955
  int32_t numOfStreams = taosHashGetSize(pHash);
6✔
956
  if (numOfStreams == 0) {
6✔
957
    return code;
2✔
958
  }
959

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

968
  return code;
4✔
969
}
970

971
int32_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
2✔
972
  int32_t code = 0;
2✔
973
  int32_t numOfStreams = taosHashGetSize(pHash);
2✔
974
  if (numOfStreams == 0) {
2!
975
    return code;
2✔
976
  }
977

978
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
×
979
  if (code == 0) {
×
980
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
981
  } else {
982
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
983
  }
984

985
  return code;
×
986
}
987

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

997
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
998
}
999

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

1016
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
4✔
1017
  int8_t trigger = pStream->conf.trigger;
4✔
1018
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
4✔
1019
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
2✔
1020
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
2!
1021
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
2✔
1022
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
×
1023
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
×
1024
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
1025
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
×
1026
  }
1027
}
4✔
1028

1029
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
4✔
1030
  memset(pBuf, 0, bufLen);
4✔
1031
  pBuf[2] = '0';
4✔
1032
  pBuf[3] = 'x';
4✔
1033

1034
  int32_t len = tintToHex(id, &pBuf[4]);
4✔
1035
  varDataSetLen(pBuf, len + 2);
4✔
1036
}
4✔
1037

1038
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
4✔
1039
  int32_t          code = TSDB_CODE_SUCCESS;
4✔
1040
  int32_t          lino = 0;
4✔
1041
  SStreamTaskIter *pIter = NULL;
4✔
1042
  bool             isPaused =  true;
4✔
1043

1044
  taosRLockLatch(&pStream->lock);
4✔
1045
  code = createStreamTaskIter(pStream, &pIter);
4✔
1046
  TSDB_CHECK_CODE(code, lino, _end);
4!
1047

1048
  while (streamTaskIterNextTask(pIter)) {
8✔
1049
    SStreamTask *pTask = NULL;
4✔
1050
    code = streamTaskIterGetCurrent(pIter, &pTask);
4✔
1051
    TSDB_CHECK_CODE(code, lino, _end);
4!
1052

1053
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
4✔
1054
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
4✔
1055
    if (pe == NULL) {
4!
1056
      continue;
×
1057
    }
1058
    if (pe->status != TASK_STATUS__PAUSE) {
4!
1059
      isPaused = false;
4✔
1060
    }
1061
  }
1062
  (*pRes) = isPaused;
4✔
1063

1064
_end:
4✔
1065
  destroyStreamTaskIter(pIter);
4✔
1066
  taosRUnLockLatch(&pStream->lock);
4✔
1067
  if (code != TSDB_CODE_SUCCESS) {
4!
1068
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
1069
  }
1070
  return code;
4✔
1071
}
1072

1073
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
4✔
1074
  int32_t code = 0;
4✔
1075
  int32_t cols = 0;
4✔
1076
  int32_t lino = 0;
4✔
1077

1078
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
1079
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
4✔
1080
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1081
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1082

1083
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
4✔
1084
  TSDB_CHECK_CODE(code, lino, _end);
4!
1085

1086
  // create time
1087
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1088
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1089
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
4✔
1090
  TSDB_CHECK_CODE(code, lino, _end);
4!
1091

1092
  // stream id
1093
  char buf[128] = {0};
4✔
1094
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
4✔
1095
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1096
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1097
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
4✔
1098
  TSDB_CHECK_CODE(code, lino, _end);
4!
1099

1100
  // related fill-history stream id
1101
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1102
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1103
  if (pStream->hTaskUid != 0) {
4!
1104
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
1105
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1106
  } else {
1107
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
4✔
1108
  }
1109
  TSDB_CHECK_CODE(code, lino, _end);
4!
1110

1111
  // related fill-history stream id
1112
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
1113
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
4✔
1114
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1115
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1116
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
4✔
1117
  TSDB_CHECK_CODE(code, lino, _end);
4!
1118

1119
  char status[20 + VARSTR_HEADER_SIZE] = {0};
4✔
1120
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
4✔
1121
  bool isPaused = false;
4✔
1122
  code = isAllTaskPaused(pStream, &isPaused);
4✔
1123
  TSDB_CHECK_CODE(code, lino, _end);
4!
1124

1125
  int8_t streamStatus = atomic_load_8(&pStream->status);
4✔
1126
  if (isPaused && pStream->pTaskList != NULL) {
4!
1127
    streamStatus = STREAM_STATUS__PAUSE;
×
1128
  }
1129
  mndShowStreamStatus(status2, streamStatus);
4✔
1130
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
4✔
1131
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1132
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1133

1134
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
4✔
1135
  TSDB_CHECK_CODE(code, lino, _end);
4!
1136

1137
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
1138
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
4✔
1139
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1140
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1141

1142
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
4✔
1143
  TSDB_CHECK_CODE(code, lino, _end);
4!
1144

1145
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
1146
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
4✔
1147
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1148
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1149

1150
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
4✔
1151
  TSDB_CHECK_CODE(code, lino, _end);
4!
1152

1153
  if (pStream->targetSTbName[0] == 0) {
4!
1154
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1155
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1156

1157
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
×
1158
  } else {
1159
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
1160
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
4✔
1161
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1162
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1163

1164
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
4✔
1165
  }
1166
  TSDB_CHECK_CODE(code, lino, _end);
4!
1167

1168
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1169
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1170

1171
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
4✔
1172
  TSDB_CHECK_CODE(code, lino, _end);
4!
1173

1174
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
4✔
1175
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
4✔
1176
  mndShowStreamTrigger(trigger2, pStream);
4✔
1177
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
4✔
1178
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1179
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1180

1181
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
4✔
1182
  TSDB_CHECK_CODE(code, lino, _end);
4!
1183

1184
  // sink_quota
1185
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
4✔
1186
  sinkQuota[0] = '0';
4✔
1187
  char dstStr[20] = {0};
4✔
1188
  STR_TO_VARSTR(dstStr, sinkQuota)
4✔
1189
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1190
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1191

1192
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
4✔
1193
  TSDB_CHECK_CODE(code, lino, _end);
4!
1194

1195
  // checkpoint interval
1196
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
4✔
1197
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
4✔
1198
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
4✔
1199

1200
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1201
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1202

1203
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
4✔
1204
  TSDB_CHECK_CODE(code, lino, _end);
4!
1205

1206
  // checkpoint backup type
1207
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
4✔
1208
  STR_TO_VARSTR(backup, "none")
4✔
1209
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1210
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1211

1212
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
4✔
1213
  TSDB_CHECK_CODE(code, lino, _end);
4!
1214

1215
  // history scan idle
1216
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
4✔
1217
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
4✔
1218

1219
  memset(dstStr, 0, tListLen(dstStr));
4✔
1220
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
4✔
1221
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1222
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1223

1224
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
4✔
1225
  TSDB_CHECK_CODE(code, lino, _end);
4!
1226

1227
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
4✔
1228
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
4!
1229
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
4✔
1230
  if (streamStatus == STREAM_STATUS__FAILED){
4!
1231
    STR_TO_VARSTR(msg, pStream->reserve)
×
1232
  } else {
1233
    STR_TO_VARSTR(msg, " ")
4✔
1234
  }
1235
  code = colDataSetVal(pColInfo, numOfRows, (const char *)msg, false);
4✔
1236

1237
_end:
4✔
1238
  if (code) {
4!
1239
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1240
  }
1241
  return code;
4✔
1242
}
1243

1244
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
×
1245
                              int32_t precision) {
1246
  SColumnInfoData *pColInfo = NULL;
×
1247
  int32_t          cols = 0;
×
1248
  int32_t          code = 0;
×
1249
  int32_t          lino = 0;
×
1250

1251
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
1252

1253
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
1254
  if (pe == NULL) {
×
1255
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1256
           " no valid status/stage info",
1257
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1258
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1259
  }
1260

1261
  // stream name
1262
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1263
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
1264

1265
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1266
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1267

1268
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
1269
  TSDB_CHECK_CODE(code, lino, _end);
×
1270

1271
  // task id
1272
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1273
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1274

1275
  char idstr[128] = {0};
×
1276
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
×
1277
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1278
  TSDB_CHECK_CODE(code, lino, _end);
×
1279

1280
  // node type
1281
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
×
1282
  varDataSetLen(nodeType, 5);
×
1283
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1284
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1285

1286
  if (pTask->info.nodeId > 0) {
×
1287
    memcpy(varDataVal(nodeType), "vnode", 5);
×
1288
  } else {
1289
    memcpy(varDataVal(nodeType), "snode", 5);
×
1290
  }
1291
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
×
1292
  TSDB_CHECK_CODE(code, lino, _end);
×
1293

1294
  // node id
1295
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1296
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1297

1298
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
×
1299
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
×
1300
  TSDB_CHECK_CODE(code, lino, _end);
×
1301

1302
  // level
1303
  char level[20 + VARSTR_HEADER_SIZE] = {0};
×
1304
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1305
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
×
1306
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
×
1307
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
×
1308
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1309
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
×
1310
  } else if (pTask->info.taskLevel == TASK_LEVEL__MERGE) {
×
1311
    STR_WITH_SIZE_TO_VARSTR(level, "merge", 5);
×
1312
  }
1313

1314
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1315
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1316

1317
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
×
1318
  TSDB_CHECK_CODE(code, lino, _end);
×
1319

1320
  // status
1321
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
1322

1323
  const char *pStatus = streamTaskGetStatusStr(pe->status);
×
1324
  STR_TO_VARSTR(status, pStatus);
×
1325

1326
  // status
1327
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1328
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1329

1330
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
×
1331
  TSDB_CHECK_CODE(code, lino, _end);
×
1332

1333
  // stage
1334
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1335
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1336

1337
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
×
1338
  TSDB_CHECK_CODE(code, lino, _end);
×
1339

1340
  // input queue
1341
  char        vbuf[TSDB_STREAM_NOTIFY_STAT_LEN + 2] = {0};
×
1342
  char        buf[TSDB_STREAM_NOTIFY_STAT_LEN] = {0};
×
1343
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
×
1344
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
×
1345
  STR_TO_VARSTR(vbuf, buf);
×
1346

1347
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1348
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1349

1350
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1351
  TSDB_CHECK_CODE(code, lino, _end);
×
1352

1353
  // input total
1354
  const char *formatTotalMb = "%7.2f MiB";
×
1355
  const char *formatTotalGb = "%7.2f GiB";
×
1356
  if (pe->procsTotal < 1024) {
×
1357
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
×
1358
  } else {
1359
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1360
  }
1361

1362
  memset(vbuf, 0, tListLen(vbuf));
×
1363
  STR_TO_VARSTR(vbuf, buf);
×
1364

1365
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1366
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1367

1368
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1369
  TSDB_CHECK_CODE(code, lino, _end);
×
1370

1371
  // process throughput
1372
  const char *formatKb = "%7.2f KiB/s";
×
1373
  const char *formatMb = "%7.2f MiB/s";
×
1374
  if (pe->procsThroughput < 1024) {
×
1375
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
×
1376
  } else {
1377
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
×
1378
  }
1379

1380
  memset(vbuf, 0, tListLen(vbuf));
×
1381
  STR_TO_VARSTR(vbuf, buf);
×
1382

1383
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1384
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1385

1386
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1387
  TSDB_CHECK_CODE(code, lino, _end);
×
1388

1389
  // output total
1390
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1391
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1392

1393
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1394
    colDataSetNULL(pColInfo, numOfRows);
×
1395
  } else {
1396
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
×
1397
    memset(vbuf, 0, tListLen(vbuf));
×
1398
    STR_TO_VARSTR(vbuf, buf);
×
1399

1400
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1401
    TSDB_CHECK_CODE(code, lino, _end);
×
1402
  }
1403

1404
  // output throughput
1405
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1406
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1407

1408
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1409
    colDataSetNULL(pColInfo, numOfRows);
×
1410
  } else {
1411
    if (pe->outputThroughput < 1024) {
×
1412
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
×
1413
    } else {
1414
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
×
1415
    }
1416

1417
    memset(vbuf, 0, tListLen(vbuf));
×
1418
    STR_TO_VARSTR(vbuf, buf);
×
1419

1420
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1421
    TSDB_CHECK_CODE(code, lino, _end);
×
1422
  }
1423
  // info
1424
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1425
    const char *sinkStr = "%.2f MiB";
×
1426
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
×
1427
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
×
1428
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
1429
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
×
1430
      if (ret != 0) {
×
1431
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1432
        memset(buf, 0, tListLen(buf));
×
1433
      }
1434
    } else {
1435
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
×
1436
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
×
1437
    }
1438
  } else {
1439
    memset(buf, 0, tListLen(buf));
×
1440
  }
1441

1442
  STR_TO_VARSTR(vbuf, buf);
×
1443

1444
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1445
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1446

1447
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1448
  TSDB_CHECK_CODE(code, lino, _end);
×
1449

1450
  // start_time
1451
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1452
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1453

1454
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
×
1455
  TSDB_CHECK_CODE(code, lino, _end);
×
1456

1457
  // start id
1458
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1459
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1460

1461
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
×
1462
  TSDB_CHECK_CODE(code, lino, _end);
×
1463

1464
  // start ver
1465
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1466
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1467

1468
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
×
1469
  TSDB_CHECK_CODE(code, lino, _end);
×
1470

1471
  // checkpoint time
1472
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1473
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1474

1475
  if (pe->checkpointInfo.latestTime != 0) {
×
1476
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
×
1477
  } else {
1478
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1479
  }
1480
  TSDB_CHECK_CODE(code, lino, _end);
×
1481

1482
  // checkpoint_id
1483
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1484
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1485

1486
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
×
1487
  TSDB_CHECK_CODE(code, lino, _end);
×
1488

1489
  // checkpoint version
1490
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1491
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1492

1493
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
×
1494
  TSDB_CHECK_CODE(code, lino, _end);
×
1495

1496
  // checkpoint size
1497
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1498
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1499

1500
  colDataSetNULL(pColInfo, numOfRows);
×
1501

1502
  // checkpoint backup status
1503
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1504
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1505

1506
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1507
  TSDB_CHECK_CODE(code, lino, _end);
×
1508

1509
  // ds_err_info
1510
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1511
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1512

1513
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1514
  TSDB_CHECK_CODE(code, lino, _end);
×
1515

1516
  // history_task_id
1517
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1518
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1519

1520
  if (pe->hTaskId != 0) {
×
1521
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
×
1522
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1523
  } else {
1524
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1525
  }
1526
  TSDB_CHECK_CODE(code, lino, _end);
×
1527

1528
  // history_task_status
1529
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1530
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1531

1532
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1533
  TSDB_CHECK_CODE(code, lino, _end);
×
1534

1535
  // notify_event_stat
1536
  int32_t offset =0;
×
1537
  if (pe->notifyEventStat.notifyEventAddTimes > 0) {
×
1538
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Add %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1539
                        pe->notifyEventStat.notifyEventAddTimes, pe->notifyEventStat.notifyEventAddElems,
1540
                        pe->notifyEventStat.notifyEventAddCostSec);
1541
  }
1542
  if (pe->notifyEventStat.notifyEventPushTimes > 0) {
×
1543
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Push %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1544
                        pe->notifyEventStat.notifyEventPushTimes, pe->notifyEventStat.notifyEventPushElems,
1545
                        pe->notifyEventStat.notifyEventPushCostSec);
1546
  }
1547
  if (pe->notifyEventStat.notifyEventPackTimes > 0) {
×
1548
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Pack %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1549
                        pe->notifyEventStat.notifyEventPackTimes, pe->notifyEventStat.notifyEventPackElems,
1550
                        pe->notifyEventStat.notifyEventPackCostSec);
1551
  }
1552
  if (pe->notifyEventStat.notifyEventSendTimes > 0) {
×
1553
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Send %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1554
                        pe->notifyEventStat.notifyEventSendTimes, pe->notifyEventStat.notifyEventSendElems,
1555
                        pe->notifyEventStat.notifyEventSendCostSec);
1556
  }
1557
  if (pe->notifyEventStat.notifyEventHoldElems > 0) {
×
1558
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "[Hold %" PRId64 " elems] ",
×
1559
                        pe->notifyEventStat.notifyEventHoldElems);
1560
  }
1561
  TSDB_CHECK_CONDITION(offset < sizeof(buf), code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1562
  buf[offset] = '\0';
×
1563

1564
  STR_TO_VARSTR(vbuf, buf);
×
1565

1566
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1567
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1568

1569
  if (offset == 0) {
×
1570
    colDataSetNULL(pColInfo, numOfRows);
×
1571
  } else {
1572
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1573
    TSDB_CHECK_CODE(code, lino, _end);
×
1574
  }
1575

1576
_end:
×
1577
  if (code) {
×
1578
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1579
  }
1580
  return code;
×
1581
}
1582

1583
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
33✔
1584
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
33✔
1585
  const SEp *p = GET_ACTIVE_EP(pCurrent);
33✔
1586

1587
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
33!
1588
    return false;
33✔
1589
  }
1590
  return true;
×
1591
}
1592

1593
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
14✔
1594
  if (pInfo != NULL) {
14!
1595
    taosArrayDestroy(pInfo->pUpdateNodeList);
14✔
1596
    taosHashCleanup(pInfo->pDBMap);
14✔
1597
  }
1598
}
14✔
1599

1600
// 1. increase the replica does not affect the stream process.
1601
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1602
// tasks on the will be removed replica.
1603
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1604
// will handle it as mentioned in 1 & 2 items.
1605
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
14✔
1606
                               SVgroupChangeInfo *pInfo) {
1607
  int32_t code = 0;
14✔
1608
  int32_t lino = 0;
14✔
1609

1610
  if (pInfo == NULL) {
14!
1611
    return TSDB_CODE_INVALID_PARA;
×
1612
  }
1613

1614
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
14✔
1615
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
14✔
1616

1617
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
14!
1618
    mndDestroyVgroupChangeInfo(pInfo);
×
1619
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1620
  }
1621

1622
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
14✔
1623
  for (int32_t i = 0; i < numOfNodes; ++i) {
48✔
1624
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
34✔
1625
    if (pPrevEntry == NULL) {
34!
1626
      continue;
×
1627
    }
1628

1629
    int32_t num = taosArrayGetSize(pNodeList);
34✔
1630
    for (int32_t j = 0; j < num; ++j) {
69!
1631
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
69✔
1632
      if (pCurrent == NULL) {
69!
1633
        continue;
×
1634
      }
1635

1636
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
69✔
1637
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
34!
1638
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
1✔
1639

1640
          char buf[256] = {0};
1✔
1641
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
1✔
1642
          if (code) {
1!
1643
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1644
            TSDB_CHECK_CODE(code, lino, _err);
×
1645
          }
1646

1647
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
1!
1648
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1649

1650
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
1✔
1651
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
1✔
1652
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
1✔
1653

1654
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
1✔
1655
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
1!
1656
        }
1657

1658
        // todo handle the snode info
1659
        if (pCurrent->nodeId != SNODE_HANDLE) {
34✔
1660
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
26✔
1661
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
26✔
1662
          mndReleaseVgroup(pMnode, pVgroup);
26✔
1663
          TSDB_CHECK_CODE(code, lino, _err);
26!
1664
        }
1665

1666
        break;
34✔
1667
      }
1668
    }
1669
  }
1670

1671
  return code;
14✔
1672

1673
_err:
×
1674
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1675
  mndDestroyVgroupChangeInfo(pInfo);
×
1676
  return code;
×
1677
}
1678

1679
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
42✔
1680
  bool              allReady = false;
42✔
1681
  bool              nodeUpdated = false;
42✔
1682
  SVgroupChangeInfo changeInfo = {0};
42✔
1683

1684
  int32_t numOfNodes = extractStreamNodeList(pMnode);
42✔
1685

1686
  if (numOfNodes == 0) {
42✔
1687
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
36✔
1688
    execInfo.ts = taosGetTimestampSec();
36✔
1689
    return false;
36✔
1690
  }
1691

1692
  for (int32_t i = 0; i < numOfNodes; ++i) {
21✔
1693
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
15✔
1694
    if (pNodeEntry == NULL) {
15!
1695
      continue;
×
1696
    }
1697

1698
    if (pNodeEntry->stageUpdated) {
15!
1699
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
×
1700
      return true;
×
1701
    }
1702
  }
1703

1704
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot, NULL);
6✔
1705
  if (code) {
6!
1706
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1707
  }
1708

1709
  if (!allReady) {
6!
1710
    mWarn("not all vnodes ready, quit from vnodes status check");
×
1711
    return true;
×
1712
  }
1713

1714
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
6✔
1715
  if (code) {
6!
1716
    nodeUpdated = false;
×
1717
  } else {
1718
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
6✔
1719
    if (nodeUpdated) {
6!
1720
      mDebug("stream tasks not ready due to node update");
×
1721
    }
1722
  }
1723

1724
  mndDestroyVgroupChangeInfo(&changeInfo);
6✔
1725
  return nodeUpdated;
6✔
1726
}
1727

1728
// check if the node update happens or not
1729
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
42✔
1730
  SArray *pNodeSnapshot = NULL;
42✔
1731

1732
  streamMutexLock(&execInfo.lock);
42✔
1733
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
42✔
1734
  streamMutexUnlock(&execInfo.lock);
42✔
1735

1736
  taosArrayDestroy(pNodeSnapshot);
42✔
1737
  return updated;
42✔
1738
}
1739

1740
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
3✔
1741
  SSdb      *pSdb = pMnode->pSdb;
3✔
1742
  void      *pIter = NULL;
3✔
1743
  SSnodeObj *pObj = NULL;
3✔
1744

1745
  if (pSrcDb->cfg.replications == 1) {
3!
1746
    return TSDB_CODE_SUCCESS;
3✔
1747
  } else {
1748
    while (1) {
1749
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
1750
      if (pIter == NULL) {
×
1751
        break;
×
1752
      }
1753

1754
      sdbRelease(pSdb, pObj);
×
1755
      sdbCancelFetch(pSdb, pIter);
×
1756
      return TSDB_CODE_SUCCESS;
×
1757
    }
1758

1759
    mError("snode not existed when trying to create stream in db with multiple replica");
×
1760
    return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
1761
  }
1762
}
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