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

taosdata / TDengine / #3663

19 Mar 2025 09:21AM UTC coverage: 61.664% (-0.6%) from 62.28%
#3663

push

travis-ci

web-flow
docs: add defination of tmq_config_res_t & fix spell error (#30271)

153169 of 318241 branches covered (48.13%)

Branch coverage included in aggregate %.

239405 of 318390 relevant lines covered (75.19%)

5762846.6 hits per line

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

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

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

46
  return 0;
21,161✔
47
}
48

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

55
  if (pIter->level == -1) {
115,249✔
56
    pIter->level += 1;
21,161✔
57
  }
58

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

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
21,161!
89

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

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

106
  return true;
32,799✔
107
}
108

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

114
  while (1) {
1,464✔
115
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
11,230✔
116
    if (pIter == NULL) {
11,230✔
117
      break;
9,766✔
118
    }
119

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

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

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

149
  return code;
9,766✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
9,643✔
182
}
183

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

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

197
  while (1) {
36,277✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
46,043✔
199
    if (pIter == NULL) {
46,043✔
200
      break;
9,766✔
201
    }
202

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

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

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

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

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

254
    sdbRelease(pSdb, pVgroup);
36,277✔
255
  }
256

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

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

266
  *pList = NULL;
9,766✔
267
  *allReady = true;
9,766✔
268

269
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
9,766✔
270
  if (pVgroupList == NULL) {
9,766!
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);
9,766✔
278
  if (code) {
9,766!
279
    goto _err;
×
280
  }
281

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

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

294
  *pList = pVgroupList;
9,766✔
295
  return code;
9,766✔
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) {
9,311✔
304
  void *pIter = NULL;
9,311✔
305
  SSdb *pSdb = pMnode->pSdb;
9,311✔
306
  *pStream = NULL;
9,311✔
307

308
  SStreamObj *p = NULL;
9,311✔
309
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
14,347✔
310
    if (p->uid == streamId) {
14,346✔
311
      sdbCancelFetch(pSdb, pIter);
9,310✔
312
      *pStream = p;
9,310✔
313
      return TSDB_CODE_SUCCESS;
9,310✔
314
    }
315
    sdbRelease(pSdb, p);
5,036✔
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) {
14,642✔
336
  *hasEpset = false;
14,642✔
337

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

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

365
      epsetAssign(pEpSet, &epset);
14,526✔
366
      *hasEpset = true;
14,526✔
367
      return TSDB_CODE_SUCCESS;
14,526✔
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) {
22,247✔
404
  int32_t num = 0;
22,247✔
405
  for (int32_t i = 0; i < taosArrayGetSize(pStream->pTaskList); ++i) {
63,998✔
406
    SArray *pLevel = taosArrayGetP(pStream->pTaskList, i);
41,751✔
407
    num += taosArrayGetSize(pLevel);
41,751✔
408
  }
409

410
  return num;
22,247✔
411
}
412

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

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

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

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

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

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

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

450
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
1,754✔
451

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

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

468
  execInfo.role = NODE_ROLE_UNINIT;
1,754✔
469
  execInfo.switchFromFollower = false;
1,754✔
470

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

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

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

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

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

498
      if (pEntry->nodeId == p->nodeId) {
11,085✔
499
        p->hbTimestamp = pEntry->hbTimestamp;
2,072✔
500

501
        void *px = taosArrayPush(pValidList, p);
2,072✔
502
        if (px == NULL) {
2,072!
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);
2,072✔
506
        }
507
        break;
2,072✔
508
      }
509
    }
510
  }
511

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

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

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

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

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

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

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

545
  return TSDB_CODE_SUCCESS;
7,131✔
546
}
547

548
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
539✔
549
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
539!
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
}
539✔
561

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

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

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

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

588
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
1,332!
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);
1,332✔
597
  if (code) {
1,332!
598
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
599
  }
600

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

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

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

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

619
    if (pEntry->nodeId == nodeId) {
23,129✔
620
      return true;
7,180✔
621
    }
622
  }
623

624
  return false;
×
625
}
626

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

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

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

645
    if (pEntry->nodeId == SNODE_HANDLE) {
7,251✔
646
      continue;
71✔
647
    }
648

649
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
7,180✔
650
    if (!existed) {
7,180!
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);
539✔
659

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

663
  removeExpiredNodeInfo(pNodeSnapshot);
539✔
664

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

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

674
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
575✔
675
  if (existed != numOfTasks) {
575✔
676
    mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pReportInfo->streamId, pName,
11!
677
           existed, numOfTasks, numOfTasks - existed);
678
    return -1;
11✔
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) {
4,015✔
683
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
3,451✔
684
    if (pInfo == NULL) {
3,451!
685
      continue;
×
686
    }
687

688
    if (checkpointId == -1) {
3,451✔
689
      checkpointId = pInfo->checkpointId;
564✔
690
      transId = pInfo->transId;
564✔
691
      taskId = pInfo->taskId;
564✔
692
    } else if (checkpointId != pInfo->checkpointId) {
2,887!
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);
564✔
703
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
564✔
704
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
564✔
705

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

712
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
166!
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) {
398!
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,
564✔
730
         pName, numOfTasks);
731

732
  return TSDB_CODE_SUCCESS;
564✔
733
}
734

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

742
  mDebug("start to scan checkpoint report info");
7,637✔
743

744
  streamMutexLock(&execInfo.lock);
7,637✔
745

746
  int32_t num = taosHashGetSize(execInfo.pChkptStreams);
7,637✔
747
  if (num == 0) {
7,637✔
748
    goto _end;
3,297✔
749
  }
750

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

754
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
9,965✔
755
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
6,189✔
756
    if (taosArrayGetSize(px->pTaskList) == 0) {
6,189✔
757
      continue;
5,614✔
758
    }
759

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

765
    SStreamObj *pStream = NULL;
575✔
766
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
575✔
767
    if (pStream == NULL || code != 0) {
575!
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);
575✔
777
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
575✔
778
    if (ret == 0) {
575✔
779
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
564✔
780
      if (code == 0) {
564!
781
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
564✔
782
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
564!
783
          taosArrayClear(px->pTaskList);
564✔
784
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
564!
785
                " to %" PRId64,
786
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
787
          px->reportChkpt = pInfo->checkpointId;
564✔
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);
564✔
794
        break;
564✔
795
      } else {
796
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
797
      }
798
    }
799

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

803
  int32_t size = taosArrayGetSize(pDropped);
4,340✔
804
  if (size > 0) {
4,340!
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:
4,340✔
822
  streamMutexUnlock(&execInfo.lock);
7,637✔
823

824
  if (pDropped != NULL) {
7,637✔
825
    taosArrayDestroy(pDropped);
4,340✔
826
  }
827

828
  mDebug("end to scan checkpoint report info")
7,637✔
829
  return code;
7,637✔
830
}
831

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

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

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

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

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

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

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

865
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
30!
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);
30✔
874
  mndTransDrop(pTrans);
30✔
875

876
  return TSDB_CODE_ACTION_IN_PROGRESS;
30✔
877
}
878

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

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

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

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

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

906
  return code;
36✔
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) {
192✔
912
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
192✔
913
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
192✔
914

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

922
    if (p->req.taskId == info.req.taskId) {
543✔
923
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update send reqTs %" PRId64
8✔
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;
8✔
928
      p->req.checkpointId = info.req.checkpointId;
8✔
929
      p->req.transId = info.req.transId;
8✔
930
      p->req.nodeId = info.req.nodeId;
8✔
931
      p->req.term = info.req.term;
8✔
932
      return;
8✔
933
    }
934
  }
935

936
  void *p = taosArrayPush(pInfo->pTaskList, &info);
184✔
937
  if (p == NULL) {
184!
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);
184✔
941
    mDebug("s-task:0x%x (vgId:%d) checkpointId:%" PRId64 " term:%d, reqTs:%" PRId64
184✔
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) {
30✔
949
  taosArrayDestroy(pInfo->pTaskList);
30✔
950
  pInfo->pTaskList = NULL;
30✔
951
}
30✔
952

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

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

968
  return code;
32✔
969
}
970

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

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

985
  return code;
995✔
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) {
801✔
1001
  if (status == STREAM_STATUS__NORMAL) {
801✔
1002
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
777✔
1003
  } else if (status == STREAM_STATUS__STOP) {
24!
1004
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
1005
  } else if (status == STREAM_STATUS__FAILED) {
24✔
1006
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
1✔
1007
  } else if (status == STREAM_STATUS__RECOVER) {
23!
1008
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
1009
  } else if (status == STREAM_STATUS__PAUSE) {
23✔
1010
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
21✔
1011
  } else if (status == STREAM_STATUS__INIT) {
2!
1012
    tstrncpy(dst, "init", MND_STREAM_TRIGGER_NAME_SIZE);
2✔
1013
  }
1014
}
801✔
1015

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1153
  if (pStream->targetSTbName[0] == 0) {
801!
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};
801✔
1160
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
801✔
1161
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
801✔
1162
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
801!
1163

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1251
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
50,832✔
1252

1253
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
50,832✔
1254
  if (pe == NULL) {
50,832!
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};
50,832✔
1263
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
50,832✔
1264

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

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

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

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

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

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

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

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

1302
  // level
1303
  char level[20 + VARSTR_HEADER_SIZE] = {0};
50,832✔
1304
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
50,832✔
1305
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
28,372✔
1306
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
22,460✔
1307
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
1,829✔
1308
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
20,631!
1309
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
20,631✔
1310
  }
1311

1312
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1313
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1314

1315
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
50,832✔
1316
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1317

1318
  // status
1319
  char status[20 + VARSTR_HEADER_SIZE] = {0};
50,832✔
1320

1321
  const char *pStatus = streamTaskGetStatusStr(pe->status);
50,832✔
1322
  STR_TO_VARSTR(status, pStatus);
50,832✔
1323

1324
  // status
1325
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1326
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1327

1328
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
50,832✔
1329
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1330

1331
  // stage
1332
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1333
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1334

1335
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
50,832✔
1336
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1337

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

1345
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1346
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1347

1348
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
50,832✔
1349
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1350

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

1360
  memset(vbuf, 0, tListLen(vbuf));
50,832✔
1361
  STR_TO_VARSTR(vbuf, buf);
50,832✔
1362

1363
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1364
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1365

1366
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
50,832✔
1367
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1368

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

1378
  memset(vbuf, 0, tListLen(vbuf));
50,832✔
1379
  STR_TO_VARSTR(vbuf, buf);
50,832✔
1380

1381
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1382
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1383

1384
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
50,832✔
1385
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1386

1387
  // output total
1388
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1389
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1390

1391
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
50,832✔
1392
    colDataSetNULL(pColInfo, numOfRows);
20,631!
1393
  } else {
1394
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
30,201✔
1395
    memset(vbuf, 0, tListLen(vbuf));
30,201✔
1396
    STR_TO_VARSTR(vbuf, buf);
30,201✔
1397

1398
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
30,201✔
1399
    TSDB_CHECK_CODE(code, lino, _end);
30,201!
1400
  }
1401

1402
  // output throughput
1403
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1404
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1405

1406
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
50,832✔
1407
    colDataSetNULL(pColInfo, numOfRows);
20,631!
1408
  } else {
1409
    if (pe->outputThroughput < 1024) {
30,201✔
1410
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
30,188✔
1411
    } else {
1412
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
13✔
1413
    }
1414

1415
    memset(vbuf, 0, tListLen(vbuf));
30,201✔
1416
    STR_TO_VARSTR(vbuf, buf);
30,201✔
1417

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

1440
  STR_TO_VARSTR(vbuf, buf);
50,832✔
1441

1442
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1443
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1444

1445
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
50,832✔
1446
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1447

1448
  // start_time
1449
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1450
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1451

1452
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
50,832✔
1453
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1454

1455
  // start id
1456
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1457
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1458

1459
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
50,832✔
1460
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1461

1462
  // start ver
1463
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1464
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1465

1466
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
50,832✔
1467
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1468

1469
  // checkpoint time
1470
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1471
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1472

1473
  if (pe->checkpointInfo.latestTime != 0) {
50,832✔
1474
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
2,513✔
1475
  } else {
1476
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
48,319✔
1477
  }
1478
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1479

1480
  // checkpoint_id
1481
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1482
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1483

1484
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
50,832✔
1485
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1486

1487
  // checkpoint version
1488
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1489
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1490

1491
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
50,832✔
1492
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1493

1494
  // checkpoint size
1495
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1496
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1497

1498
  colDataSetNULL(pColInfo, numOfRows);
50,832!
1499

1500
  // checkpoint backup status
1501
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1502
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1503

1504
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
50,832✔
1505
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1506

1507
  // ds_err_info
1508
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1509
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1510

1511
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
50,832✔
1512
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1513

1514
  // history_task_id
1515
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1516
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1517

1518
  if (pe->hTaskId != 0) {
50,832✔
1519
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
1,131✔
1520
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
1,131✔
1521
  } else {
1522
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
49,701✔
1523
  }
1524
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1525

1526
  // history_task_status
1527
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1528
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1529

1530
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
50,832✔
1531
  TSDB_CHECK_CODE(code, lino, _end);
50,832!
1532

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

1562
  STR_TO_VARSTR(vbuf, buf);
50,832✔
1563

1564
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
50,832✔
1565
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
50,832!
1566

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

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

1581
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
3,942✔
1582
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
3,942✔
1583
  const SEp *p = GET_ACTIVE_EP(pCurrent);
3,942✔
1584

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

1591
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
1,057✔
1592
  if (pInfo != NULL) {
1,057!
1593
    taosArrayDestroy(pInfo->pUpdateNodeList);
1,057✔
1594
    taosHashCleanup(pInfo->pDBMap);
1,057✔
1595
  }
1596
}
1,057✔
1597

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

1608
  if (pInfo == NULL) {
1,057!
1609
    return TSDB_CODE_INVALID_PARA;
×
1610
  }
1611

1612
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
1,057✔
1613
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
1,057✔
1614

1615
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
1,057!
1616
    mndDestroyVgroupChangeInfo(pInfo);
×
1617
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1618
  }
1619

1620
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
1,057✔
1621
  for (int32_t i = 0; i < numOfNodes; ++i) {
5,730✔
1622
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
4,673✔
1623
    if (pPrevEntry == NULL) {
4,673!
1624
      continue;
×
1625
    }
1626

1627
    int32_t num = taosArrayGetSize(pNodeList);
4,673✔
1628
    for (int32_t j = 0; j < num; ++j) {
16,769✔
1629
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
16,046✔
1630
      if (pCurrent == NULL) {
16,046!
1631
        continue;
×
1632
      }
1633

1634
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
16,046✔
1635
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
3,950!
1636
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
8✔
1637

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

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

1648
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
8✔
1649
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
8✔
1650
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
8✔
1651

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

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

1664
        break;
3,950✔
1665
      }
1666
    }
1667
  }
1668

1669
  return code;
1,057✔
1670

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

1677
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
1,272✔
1678
  bool              allReady = false;
1,272✔
1679
  bool              nodeUpdated = false;
1,272✔
1680
  SVgroupChangeInfo changeInfo = {0};
1,272✔
1681

1682
  int32_t numOfNodes = extractStreamNodeList(pMnode);
1,272✔
1683

1684
  if (numOfNodes == 0) {
1,272✔
1685
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
720✔
1686
    execInfo.ts = taosGetTimestampSec();
720✔
1687
    return false;
720✔
1688
  }
1689

1690
  for (int32_t i = 0; i < numOfNodes; ++i) {
3,462✔
1691
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
2,925✔
1692
    if (pNodeEntry == NULL) {
2,925!
1693
      continue;
×
1694
    }
1695

1696
    if (pNodeEntry->stageUpdated) {
2,925✔
1697
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
15✔
1698
      return true;
15✔
1699
    }
1700
  }
1701

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

1707
  if (!allReady) {
537✔
1708
    mWarn("not all vnodes ready, quit from vnodes status check");
19!
1709
    return true;
19✔
1710
  }
1711

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

1722
  mndDestroyVgroupChangeInfo(&changeInfo);
518✔
1723
  return nodeUpdated;
518✔
1724
}
1725

1726
// check if the node update happens or not
1727
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
1,272✔
1728
  SArray *pNodeSnapshot = NULL;
1,272✔
1729

1730
  streamMutexLock(&execInfo.lock);
1,272✔
1731
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
1,272✔
1732
  streamMutexUnlock(&execInfo.lock);
1,272✔
1733

1734
  taosArrayDestroy(pNodeSnapshot);
1,272✔
1735
  return updated;
1,272✔
1736
}
1737

1738
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,772✔
1739
  SSdb      *pSdb = pMnode->pSdb;
1,772✔
1740
  void      *pIter = NULL;
1,772✔
1741
  SSnodeObj *pObj = NULL;
1,772✔
1742

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

1752
      sdbRelease(pSdb, pObj);
1✔
1753
      sdbCancelFetch(pSdb, pIter);
1✔
1754
      return TSDB_CODE_SUCCESS;
1✔
1755
    }
1756

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