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

taosdata / TDengine / #3611

12 Feb 2025 09:54AM UTC coverage: 63.456% (+8.7%) from 54.713%
#3611

push

travis-ci

web-flow
Merge pull request #29745 from taosdata/fix/TD33664-3.0

fix: --version show information check for 3.0

141323 of 286257 branches covered (49.37%)

Branch coverage included in aggregate %.

220096 of 283298 relevant lines covered (77.69%)

19188455.82 hits per line

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

72.16
/source/dnode/mnode/impl/src/mndStreamUtil.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "mndDb.h"
17
#include "mndStb.h"
18
#include "mndStream.h"
19
#include "mndTrans.h"
20
#include "mndVgroup.h"
21
#include "taoserror.h"
22
#include "tmisce.h"
23

24
struct SStreamTaskIter {
25
  SStreamObj  *pStream;
26
  int32_t      level;
27
  int32_t      ordinalIndex;
28
  int32_t      totalLevel;
29
  SStreamTask *pTask;
30
};
31

32
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId);
33

34
int32_t createStreamTaskIter(SStreamObj *pStream, SStreamTaskIter **pIter) {
104,723✔
35
  *pIter = taosMemoryCalloc(1, sizeof(SStreamTaskIter));
104,723!
36
  if (*pIter == NULL) {
104,721!
37
    return terrno;
×
38
  }
39

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

46
  return 0;
104,686✔
47
}
48

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

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

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

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

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

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

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

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

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

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

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

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

149
  return code;
14,635✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
14,492✔
182
}
183

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

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

197
  while (1) {
270,988✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
285,623✔
199
    if (pIter == NULL) {
285,623✔
200
      break;
14,635✔
201
    }
202

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

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

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

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

246
    sdbRelease(pSdb, pVgroup);
270,988✔
247
  }
248

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

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

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

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

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

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

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

286
  *pList = pVgroupList;
14,635✔
287
  return code;
14,635✔
288

289
_err:
×
290
  *allReady = false;
×
291
  taosArrayDestroy(pVgroupList);
×
292
  return code;
×
293
}
294

295
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
12,514✔
296
  void *pIter = NULL;
12,514✔
297
  SSdb *pSdb = pMnode->pSdb;
12,514✔
298
  *pStream = NULL;
12,514✔
299

300
  SStreamObj *p = NULL;
12,514✔
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
21,435✔
302
    if (p->uid == streamId) {
21,431✔
303
      sdbCancelFetch(pSdb, pIter);
12,510✔
304
      *pStream = p;
12,510✔
305
      return TSDB_CODE_SUCCESS;
12,510✔
306
    }
307
    sdbRelease(pSdb, p);
8,921✔
308
  }
309

310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
4✔
311
}
312

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

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

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

335
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
304✔
336
    if (pIter != NULL) {
304!
337
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
304✔
338
      sdbRelease(pMnode->pSdb, pObj);
304✔
339
      sdbCancelFetch(pMnode->pSdb, pIter);
304✔
340
      if (code) {
304!
341
        *hasEpset = false;
×
342
        mError("failed to set epset");
×
343
      } else {
344
        *hasEpset = true;
304✔
345
      }
346
      return code;
304✔
347
    } else {
348
      mError("failed to acquire snode epset");
×
349
      return TSDB_CODE_INVALID_PARA;
×
350
    }
351
  } else {
352
    SVgObj *pVgObj = mndAcquireVgroup(pMnode, nodeId);
19,326✔
353
    if (pVgObj != NULL) {
19,326✔
354
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
19,325✔
355
      mndReleaseVgroup(pMnode, pVgObj);
19,325✔
356

357
      epsetAssign(pEpSet, &epset);
19,325✔
358
      *hasEpset = true;
19,325✔
359
      return TSDB_CODE_SUCCESS;
19,325✔
360
    } else {
361
      mDebug("orphaned task:0x%x need to be dropped, nodeId:%d, no redo action", taskId, nodeId);
1!
362
      return TSDB_CODE_SUCCESS;
1✔
363
    }
364
  }
365
}
366

367
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
175✔
368
  *pTask = NULL;
175✔
369

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

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

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

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

395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
74,804✔
396
  int32_t num = 0;
74,804✔
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
222,661✔
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
147,823✔
399
    num += taosArrayGetSize(pLevel);
147,872✔
400
  }
401

402
  return num;
74,757✔
403
}
404

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

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

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

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

426
  *pNumOfStreams = numOfStreams;
66✔
427
  mndReleaseDb(pMnode, pDb);
66✔
428
  return 0;
66✔
429
}
430

431
static void freeTaskList(void *param) {
1,552✔
432
  SArray **pList = (SArray **)param;
1,552✔
433
  taosArrayDestroy(*pList);
1,552✔
434
}
1,552✔
435

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

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

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

453
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
1,745!
454
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
1,745!
455
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
1,745!
456
    mError("failed to initialize the stream runtime env, code:%s", tstrerror(terrno));
×
457
    return terrno;
×
458
  }
459

460
  execInfo.role = NODE_ROLE_UNINIT;
1,745✔
461
  execInfo.switchFromFollower = false;
1,745✔
462

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

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

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

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

484
    for (int32_t j = 0; j < size; ++j) {
111,570✔
485
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
110,518✔
486
      if (pEntry == NULL) {
110,518!
487
        continue;
×
488
      }
489

490
      if (pEntry->nodeId == p->nodeId) {
110,518✔
491
        p->hbTimestamp = pEntry->hbTimestamp;
7,123✔
492

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

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

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

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

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

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

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

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

537
  return TSDB_CODE_SUCCESS;
7,200✔
538
}
539

540
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
1,389✔
541
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
1,389!
542
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
543
    if (pId == NULL) {
×
544
      continue;
×
545
    }
546

547
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
548
    if (code) {
×
549
      mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId);
×
550
    }
551
  }
552
}
1,389✔
553

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

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

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

573
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,330✔
574
    code = doRemoveTasks(pExecNode, &id);
7,330✔
575
    if (code) {
7,330!
576
      mError("failed to remove task in buffer list, 0x%" PRIx64, id.taskId);
×
577
    }
578
  }
579

580
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
1,352!
581
    streamMutexUnlock(&pExecNode->lock);
×
582
    destroyStreamTaskIter(pIter);
×
583
    mError("task map size, task list size, not equal");
×
584
    return;
×
585
  }
586

587
  // 2. remove stream entry in consensus hash table and checkpoint-report hash table
588
  code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, pStream->uid);
1,352✔
589
  if (code) {
1,352!
590
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
591
  }
592

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

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

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

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

611
    if (pEntry->nodeId == nodeId) {
126,498✔
612
      return true;
14,756✔
613
    }
614
  }
615

616
  return false;
×
617
}
618

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

625
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
1,389✔
626
  for (int32_t i = 0; i < numOfTask; ++i) {
17,040✔
627
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
15,651✔
628
    if (pId == NULL) {
15,651!
629
      continue;
×
630
    }
631

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

637
    if (pEntry->nodeId == SNODE_HANDLE) {
15,651✔
638
      continue;
895✔
639
    }
640

641
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
14,756✔
642
    if (!existed) {
14,756!
643
      void *p = taosArrayPush(pRemovedTasks, pId);
×
644
      if (p == NULL) {
×
645
        mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId);
×
646
      }
647
    }
648
  }
649

650
  removeTasksInBuf(pRemovedTasks, &execInfo);
1,389✔
651

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

655
  removeExpiredNodeInfo(pNodeSnapshot);
1,389✔
656

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

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

666
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
1,138✔
667
  if (existed != numOfTasks) {
1,138✔
668
    mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pReportInfo->streamId, pName,
28✔
669
           existed, numOfTasks, numOfTasks - existed);
670
    return -1;
28✔
671
  }
672

673
  // acquire current active checkpointId, and do cross-check checkpointId info in exec.pTaskList
674
  for(int32_t i = 0; i < numOfTasks; ++i) {
6,380✔
675
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
5,270✔
676
    if (pInfo == NULL) {
5,270!
677
      continue;
×
678
    }
679

680
    if (checkpointId == -1) {
5,270✔
681
      checkpointId = pInfo->checkpointId;
1,110✔
682
      transId = pInfo->transId;
1,110✔
683
      taskId = pInfo->taskId;
1,110✔
684
    } else if (checkpointId != pInfo->checkpointId) {
4,160!
685
      mError("stream:0x%" PRIx64
×
686
             " checkpointId in checkpoint-report list are not identical, type 1 taskId:0x%x checkpointId:%" PRId64
687
             ", type 2 taskId:0x%x checkpointId:%" PRId64,
688
             pReportInfo->streamId, taskId, checkpointId, pInfo->taskId, pInfo->checkpointId);
689
      return -1;
×
690
    }
691
  }
692

693
  // check for the correct checkpointId for current task info in STaskChkptInfo
694
  STaskChkptInfo  *p = taosArrayGet(pReportInfo->pTaskList, 0);
1,110✔
695
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
1,110✔
696
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
1,110✔
697

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

704
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
139!
705
      mWarn("stream:0x%" PRIx64 " active checkpointId is not equalled to the required, current:%" PRId64
×
706
            ", req:%" PRId64 " recheck next time",
707
            id.streamId, pe->checkpointInfo.activeId, checkpointId);
708
      return -1;
×
709
    } else {
710
      //  do nothing
711
    }
712
  } else {
713
    if (pTransInfo->transId != transId) {
971✔
714
      mError("stream:0x%" PRIx64
1!
715
             " checkpoint-report list info are expired, active transId:%d trans in list:%d, recheck next time",
716
             id.streamId, pTransInfo->transId, transId);
717
      return -1;
1✔
718
    }
719
  }
720

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

724
  return TSDB_CODE_SUCCESS;
1,109✔
725
}
726

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

736
  mDebug("start to scan checkpoint report info");
27,054✔
737

738
  streamMutexLock(&execInfo.lock);
27,054✔
739

740
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
89,526✔
741
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
63,581✔
742
    if (taosArrayGetSize(px->pTaskList) == 0) {
63,581✔
743
      continue;
62,443✔
744
    }
745

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

751
    SStreamObj *pStream = NULL;
1,138✔
752
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
1,138✔
753
    if (pStream == NULL || code != 0) {
1,138!
754
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
755
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
756
      if (p == NULL) {
×
757
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
758
      }
759
      continue;
×
760
    }
761

762
    int32_t total = mndGetNumOfStreamTasks(pStream);
1,138✔
763
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
1,138✔
764
    if (ret == 0) {
1,138✔
765
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,109✔
766
      if (code == 0) {
1,109!
767
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,109✔
768
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,109!
769
          taosArrayClear(px->pTaskList);
1,109✔
770
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
1,109!
771
                " to %" PRId64,
772
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
773
          px->reportChkpt = pInfo->checkpointId;
1,109✔
774
        } else {
775
          mDebug("stream:0x%" PRIx64 " not launch chkpt-info update trans, due to checkpoint not finished yet",
×
776
                 pInfo->streamId);
777
        }
778

779
        sdbRelease(pMnode->pSdb, pStream);
1,109✔
780
        break;
1,109✔
781
      } else {
782
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
783
      }
784
    }
785

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

789
  int32_t size = taosArrayGetSize(pDropped);
27,054✔
790
  if (size > 0) {
27,054!
791
    for (int32_t i = 0; i < size; ++i) {
×
792
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
793
      if (pStreamId == NULL) {
×
794
        continue;
×
795
      }
796

797
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
798
      if (code) {
×
799
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
800
      }
801
    }
802

803
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
804
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
805
  }
806

807
  streamMutexUnlock(&execInfo.lock);
27,054✔
808

809
  taosArrayDestroy(pDropped);
27,054✔
810

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

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

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

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

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

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

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

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

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

866
  return TSDB_CODE_ACTION_IN_PROGRESS;
175✔
867
}
868

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

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

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

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

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

896
  return code;
36✔
897
}
898

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

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

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

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

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

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

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

954
  return code;
35✔
955
}
956

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

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

971
  return code;
1,014✔
972
}
973

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

983
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
984
}
985

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

1000
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
33,100✔
1001
  int8_t trigger = pStream->conf.trigger;
33,100✔
1002
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
33,100✔
1003
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
11,347✔
1004
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
21,753✔
1005
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
10,898✔
1006
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
10,855✔
1007
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
10,825✔
1008
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
30!
1009
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
70✔
1010
  }
1011
}
33,100✔
1012

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1259
  if (pTask->info.nodeId > 0) {
216,795✔
1260
    memcpy(varDataVal(nodeType), "vnode", 5);
199,451✔
1261
  } else {
1262
    memcpy(varDataVal(nodeType), "snode", 5);
17,344✔
1263
  }
1264
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
216,795✔
1265
  TSDB_CHECK_CODE(code, lino, _end);
216,734!
1266

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

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

1275
  // level
1276
  char level[20 + VARSTR_HEADER_SIZE] = {0};
216,499✔
1277
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
216,499✔
1278
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
111,192✔
1279
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
105,307✔
1280
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
18,838✔
1281
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
86,469!
1282
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
86,898✔
1283
  }
1284

1285
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,499✔
1286
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,334!
1287

1288
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
216,334✔
1289
  TSDB_CHECK_CODE(code, lino, _end);
216,689!
1290

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1379
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
217,013✔
1380
    colDataSetNULL(pColInfo, numOfRows);
86,904!
1381
  } else {
1382
    if (pe->outputThroughput < 1024) {
130,109✔
1383
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
130,096✔
1384
    } else {
1385
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
13✔
1386
    }
1387

1388
    memset(vbuf, 0, tListLen(vbuf));
130,109✔
1389
    STR_TO_VARSTR(vbuf, buf);
130,109✔
1390

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

1413
  STR_TO_VARSTR(vbuf, buf);
217,079✔
1414

1415
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
217,079✔
1416
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
217,029!
1417

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

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

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

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

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

1435
  // start ver
1436
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,364✔
1437
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,252!
1438

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

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

1446
  if (pe->checkpointInfo.latestTime != 0) {
216,204✔
1447
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
173,973✔
1448
  } else {
1449
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
42,231✔
1450
  }
1451
  TSDB_CHECK_CODE(code, lino, _end);
216,289!
1452

1453
  // checkpoint_id
1454
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,289✔
1455
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,197!
1456

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

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

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

1467
  // checkpoint size
1468
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,191✔
1469
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,076!
1470

1471
  colDataSetNULL(pColInfo, numOfRows);
216,078!
1472

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

1477
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
216,089✔
1478
  TSDB_CHECK_CODE(code, lino, _end);
216,265!
1479

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

1484
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
216,217✔
1485
  TSDB_CHECK_CODE(code, lino, _end);
216,243!
1486

1487
  // history_task_id
1488
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
216,243✔
1489
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
216,075!
1490

1491
  if (pe->hTaskId != 0) {
216,106✔
1492
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
350✔
1493
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
350✔
1494
  } else {
1495
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
215,756✔
1496
  }
1497
  TSDB_CHECK_CODE(code, lino, _end);
216,291!
1498

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

1503
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
216,130✔
1504
  TSDB_CHECK_CODE(code, lino, _end);
216,376!
1505

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

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

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

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

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

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

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

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

1552
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,645✔
1553
  for (int32_t i = 0; i < numOfNodes; ++i) {
15,917✔
1554
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
13,272✔
1555
    if (pPrevEntry == NULL) {
13,272!
1556
      continue;
×
1557
    }
1558

1559
    int32_t num = taosArrayGetSize(pNodeList);
13,272✔
1560
    for (int32_t j = 0; j < num; ++j) {
181,720✔
1561
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
181,056✔
1562
      if (pCurrent == NULL) {
181,056!
1563
        continue;
×
1564
      }
1565

1566
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
181,056✔
1567
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
12,608!
1568
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
20✔
1569

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

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

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

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

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

1596
        break;
12,608✔
1597
      }
1598
    }
1599
  }
1600

1601
  return code;
2,645✔
1602

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

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

1614
  int32_t numOfNodes = extractStreamNodeList(pMnode);
1,999✔
1615

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

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

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

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

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

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

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

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

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

1666
  taosArrayDestroy(pNodeSnapshot);
1,999✔
1667
  return updated;
1,999✔
1668
}
1669

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

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

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

1689
    mError("snode not existed when trying to create stream in db with multiple replica");
2!
1690
    return TSDB_CODE_SNODE_NOT_DEPLOYED;
2✔
1691
  }
1692
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc