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

taosdata / TDengine / #3544

30 Nov 2024 03:06AM UTC coverage: 60.88% (+0.04%) from 60.842%
#3544

push

travis-ci

web-flow
Merge pull request #28988 from taosdata/main

merge: from main to 3.0 branch

120724 of 253479 branches covered (47.63%)

Branch coverage included in aggregate %.

407 of 489 new or added lines in 21 files covered. (83.23%)

1148 existing lines in 113 files now uncovered.

201919 of 276488 relevant lines covered (73.03%)

18898587.44 hits per line

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

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

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

46
  return 0;
66,031✔
47
}
48

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

55
  if (pIter->level == -1) {
305,484✔
56
    pIter->level += 1;
66,031✔
57
  }
58

59
  while(pIter->level < pIter->totalLevel) {
435,488✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
369,518✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
369,691✔
62
      pIter->level += 1;
130,004✔
63
      pIter->ordinalIndex = 0;
130,004✔
64
      pIter->pTask = NULL;
130,004✔
65
      continue;
130,004✔
66
    }
67

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter* pIter) {
65,924✔
89
  taosMemoryFree(pIter);
65,924✔
90
}
66,012✔
91

92
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
260,688✔
93
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
527,639✔
94
    if (!pVgroup->vnodeGid[i].syncRestore) {
268,125✔
95
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,163!
96
      return false;
1,163✔
97
    }
98

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

108
  return true;
259,514✔
109
}
110

111
int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList) {
14,884✔
112
  SSdb     *pSdb = pMnode->pSdb;
14,884✔
113
  void     *pIter = NULL;
14,884✔
114
  SVgObj   *pVgroup = NULL;
14,884✔
115
  int32_t   code = 0;
14,884✔
116
  SArray   *pVgroupList = NULL;
14,884✔
117
  SHashObj *pHash = NULL;
14,884✔
118

119
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
14,884✔
120
  if (pVgroupList == NULL) {
14,884!
121
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
122
    code = terrno;
×
123
    goto _err;
×
124
  }
125

126
  pHash = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
14,884✔
127
  if (pHash == NULL) {
14,884!
128
    mError("failed to prepare hashmap during take vgroup snapshot, code:%s", tstrerror(terrno));
×
129
    code = terrno;
×
130
    goto _err;
×
131
  }
132

133
  *allReady = true;
14,884✔
134

135
  while (1) {
263,350✔
136
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
278,234✔
137
    if (pIter == NULL) {
278,234✔
138
      break;
14,884✔
139
    }
140

141
    SNodeEntry entry = {.nodeId = pVgroup->vgId, .hbTimestamp = pVgroup->updateTime};
263,350✔
142
    entry.epset = mndGetVgroupEpset(pMnode, pVgroup);
263,350✔
143

144
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
263,350✔
145
    if (pReplica == NULL) {  // not exist, add it into hash map
263,350✔
146
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
130,741✔
147
      if (code) {
130,741!
148
        mError("failed to put info into hashmap during task vgroup snapshot, code:%s", tstrerror(code));
×
149
        sdbRelease(pSdb, pVgroup);
×
150
        sdbCancelFetch(pSdb, pIter);
×
151
        goto _err;  // take snapshot failed, and not all ready
×
152
      }
153
    } else {
154
      if (*pReplica != pVgroup->replica) {
132,609✔
155
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
849!
156
              pVgroup->vgId, pVgroup->replica, *pReplica);
157
        *allReady = false;  // task snap success, but not all ready
849✔
158
      }
159
    }
160

161
    // if not all ready till now, no need to check the remaining vgroups.
162
    // but still we need to put the info of the existed vgroups into the snapshot list
163
    if (*allReady) {
263,350✔
164
      *allReady = checkStatusForEachReplica(pVgroup);
260,688✔
165
    }
166

167
    char buf[256] = {0};
263,350✔
168
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
263,350✔
169
    if (code != 0) {  // print error and continue
263,350!
170
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
171
    }
172

173
    void *p = taosArrayPush(pVgroupList, &entry);
263,350✔
174
    if (p == NULL) {
263,350!
175
      mError("failed to put entry in vgroup list, nodeId:%d code:out of memory", entry.nodeId);
×
176
      code = terrno;
×
177
      sdbRelease(pSdb, pVgroup);
×
178
      sdbCancelFetch(pSdb, pIter);
×
179
      goto _err;
×
180
    } else {
181
      mDebug("take node snapshot, nodeId:%d %s", entry.nodeId, buf);
263,350✔
182
    }
183

184
    sdbRelease(pSdb, pVgroup);
263,350✔
185
  }
186

187
  SSnodeObj *pObj = NULL;
14,884✔
188
  while (1) {
6,298✔
189
    pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
21,182✔
190
    if (pIter == NULL) {
21,182✔
191
      break;
14,884✔
192
    }
193

194
    SNodeEntry entry = {.nodeId = SNODE_HANDLE};
6,298✔
195
    code = addEpIntoEpSet(&entry.epset, pObj->pDnode->fqdn, pObj->pDnode->port);
6,298✔
196
    if (code) {
6,298!
197
      sdbRelease(pSdb, pObj);
×
198
      sdbCancelFetch(pSdb, pIter);
×
199
      mError("failed to extract epset for fqdn:%s during task vgroup snapshot", pObj->pDnode->fqdn);
×
200
      goto _err;
×
201
    }
202

203
    char buf[256] = {0};
6,298✔
204
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
6,298✔
205
    if (code != 0) {  // print error and continue
6,298!
206
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
207
    }
208

209
    void *p = taosArrayPush(pVgroupList, &entry);
6,298✔
210
    if (p == NULL) {
6,298!
211
      code = terrno;
×
212
      sdbRelease(pSdb, pObj);
×
213
      sdbCancelFetch(pSdb, pIter);
×
214
      mError("failed to put entry in vgroup list, nodeId:%d code:%s", entry.nodeId, tstrerror(code));
×
215
      goto _err;
×
216
    } else {
217
      mDebug("take snode snapshot, nodeId:%d %s", entry.nodeId, buf);
6,298✔
218
    }
219

220
    sdbRelease(pSdb, pObj);
6,298✔
221
  }
222

223
  *pList = pVgroupList;
14,884✔
224
  taosHashCleanup(pHash);
14,884✔
225
  return code;
14,884✔
226

227
_err:
×
228
  *allReady = false;
×
229
  taosArrayDestroy(pVgroupList);
×
230
  taosHashCleanup(pHash);
×
231

232
  return code;
×
233
}
234

235
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
13,162✔
236
  void *pIter = NULL;
13,162✔
237
  SSdb *pSdb = pMnode->pSdb;
13,162✔
238
  *pStream = NULL;
13,162✔
239

240
  SStreamObj *p = NULL;
13,162✔
241
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22,350✔
242
    if (p->uid == streamId) {
22,347✔
243
      sdbCancelFetch(pSdb, pIter);
13,159✔
244
      *pStream = p;
13,159✔
245
      return TSDB_CODE_SUCCESS;
13,159✔
246
    }
247
    sdbRelease(pSdb, p);
9,188✔
248
  }
249

250
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
3✔
251
}
252

253
void mndKillTransImpl(SMnode *pMnode, int32_t transId, const char *pDbName) {
×
254
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
×
255
  if (pTrans != NULL) {
×
256
    mInfo("kill active transId:%d in Db:%s", transId, pDbName);
×
257
    int32_t code = mndKillTrans(pMnode, pTrans);
×
258
    mndReleaseTrans(pMnode, pTrans);
×
259
    if (code) {
×
260
      mError("failed to kill transId:%d, code:%s", pTrans->id, tstrerror(code));
×
261
    }
262
  } else {
263
    mError("failed to acquire trans in Db:%s, transId:%d", pDbName, transId);
×
264
  }
265
}
×
266

267
int32_t extractNodeEpset(SMnode *pMnode, SEpSet *pEpSet, bool *hasEpset, int32_t taskId, int32_t nodeId) {
18,351✔
268
  *hasEpset = false;
18,351✔
269

270
  pEpSet->numOfEps = 0;
18,351✔
271
  if (nodeId == SNODE_HANDLE) {
18,351✔
272
    SSnodeObj *pObj = NULL;
297✔
273
    void      *pIter = NULL;
297✔
274

275
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
297✔
276
    if (pIter != NULL) {
297!
277
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
297✔
278
      sdbRelease(pMnode->pSdb, pObj);
297✔
279
      sdbCancelFetch(pMnode->pSdb, pIter);
297✔
280
      if (code) {
297!
281
        *hasEpset = false;
×
282
        mError("failed to set epset");
×
283
      } else {
284
        *hasEpset = true;
297✔
285
      }
286
      return code;
297✔
287
    } else {
288
      mError("failed to acquire snode epset");
×
289
      return TSDB_CODE_INVALID_PARA;
×
290
    }
291
  } else {
292
    SVgObj *pVgObj = mndAcquireVgroup(pMnode, nodeId);
18,054✔
293
    if (pVgObj != NULL) {
18,054!
294
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
18,054✔
295
      mndReleaseVgroup(pMnode, pVgObj);
18,054✔
296

297
      epsetAssign(pEpSet, &epset);
18,054✔
298
      *hasEpset = true;
18,054✔
299
      return TSDB_CODE_SUCCESS;
18,054✔
300
    } else {
301
      mDebug("orphaned task:0x%x need to be dropped, nodeId:%d, no redo action", taskId, nodeId);
×
302
      return TSDB_CODE_SUCCESS;
×
303
    }
304
  }
305
}
306

307
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
113✔
308
  *pTask = NULL;
113✔
309

310
  SStreamTask     *p = NULL;
113✔
311
  SStreamTaskIter *pIter = NULL;
113✔
312
  int32_t          code = createStreamTaskIter(pStream, &pIter);
113✔
313
  if (code) {
113!
314
    mError("failed to create stream task iter:%s", pStream->name);
×
315
    return code;
×
316
  }
317

318
  while (streamTaskIterNextTask(pIter)) {
387!
319
    code = streamTaskIterGetCurrent(pIter, &p);
387✔
320
    if (code) {
387!
321
      continue;
×
322
    }
323

324
    if (p->id.taskId == pId->taskId) {
387✔
325
      destroyStreamTaskIter(pIter);
113✔
326
      *pTask = p;
113✔
327
      return 0;
113✔
328
    }
329
  }
330

331
  destroyStreamTaskIter(pIter);
×
332
  return TSDB_CODE_FAILED;
×
333
}
334

335
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
70,869✔
336
  int32_t num = 0;
70,869✔
337
  for(int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
212,579✔
338
    SArray* pLevel = taosArrayGetP(pStream->tasks, i);
141,678✔
339
    num += taosArrayGetSize(pLevel);
141,749✔
340
  }
341

342
  return num;
70,822✔
343
}
344

345
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
114✔
346
  SSdb   *pSdb = pMnode->pSdb;
114✔
347
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
114✔
348
  if (pDb == NULL) {
114!
349
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
350
  }
351

352
  int32_t numOfStreams = 0;
114✔
353
  void   *pIter = NULL;
114✔
354
  while (1) {
4✔
355
    SStreamObj *pStream = NULL;
118✔
356
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
118✔
357
    if (pIter == NULL) break;
118✔
358

359
    if (pStream->sourceDbUid == pDb->uid) {
4!
360
      numOfStreams++;
4✔
361
    }
362

363
    sdbRelease(pSdb, pStream);
4✔
364
  }
365

366
  *pNumOfStreams = numOfStreams;
114✔
367
  mndReleaseDb(pMnode, pDb);
114✔
368
  return 0;
114✔
369
}
370

371
static void freeTaskList(void* param) {
1,633✔
372
  SArray** pList = (SArray **)param;
1,633✔
373
  taosArrayDestroy(*pList);
1,633✔
374
}
1,633✔
375

376
int32_t mndInitExecInfo() {
2,012✔
377
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
2,012✔
378
  if (code) {
2,012!
379
    return code;
×
380
  }
381

382
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
2,012✔
383

384
  execInfo.pTaskList = taosArrayInit(4, sizeof(STaskId));
2,012✔
385
  execInfo.pTaskMap = taosHashInit(64, fn, true, HASH_NO_LOCK);
2,012✔
386
  execInfo.transMgmt.pDBTrans = taosHashInit(32, fn, true, HASH_NO_LOCK);
2,012✔
387
  execInfo.pTransferStateStreams = taosHashInit(32, fn, true, HASH_NO_LOCK);
2,012✔
388
  execInfo.pChkptStreams = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
2,012✔
389
  execInfo.pStreamConsensus = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
2,012✔
390
  execInfo.pNodeList = taosArrayInit(4, sizeof(SNodeEntry));
2,012✔
391
  execInfo.pKilledChkptTrans = taosArrayInit(4, sizeof(SStreamTaskResetMsg));
2,012✔
392

393
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
2,012!
394
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
2,012!
395
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
2,012!
396
    mError("failed to initialize the stream runtime env, code:%s", tstrerror(terrno));
×
397
    return terrno;
×
398
  }
399

400
  execInfo.role = NODE_ROLE_UNINIT;
2,012✔
401
  execInfo.switchFromFollower = false;
2,012✔
402

403
  taosHashSetFreeFp(execInfo.pTransferStateStreams, freeTaskList);
2,012✔
404
  taosHashSetFreeFp(execInfo.pChkptStreams, freeTaskList);
2,012✔
405
  taosHashSetFreeFp(execInfo.pStreamConsensus, freeTaskList);
2,012✔
406
  return 0;
2,012✔
407
}
408

409
void removeExpiredNodeInfo(const SArray *pNodeSnapshot) {
1,262✔
410
  SArray *pValidList = taosArrayInit(4, sizeof(SNodeEntry));
1,262✔
411
  if (pValidList == NULL) {  // not continue
1,262!
412
    return;
×
413
  }
414

415
  int32_t size = taosArrayGetSize(pNodeSnapshot);
1,262✔
416
  int32_t oldSize = taosArrayGetSize(execInfo.pNodeList);
1,262✔
417

418
  for (int32_t i = 0; i < oldSize; ++i) {
8,909✔
419
    SNodeEntry *p = taosArrayGet(execInfo.pNodeList, i);
7,647✔
420
    if (p == NULL) {
7,647!
421
      continue;
×
422
    }
423

424
    for (int32_t j = 0; j < size; ++j) {
106,212✔
425
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
105,230✔
426
      if (pEntry == NULL) {
105,230!
427
        continue;
×
428
      }
429

430
      if (pEntry->nodeId == p->nodeId) {
105,230✔
431
        p->hbTimestamp = pEntry->hbTimestamp;
6,665✔
432

433
        void* px = taosArrayPush(pValidList, p);
6,665✔
434
        if (px == NULL) {
6,665!
435
          mError("failed to put node into list, nodeId:%d", p->nodeId);
×
436
        } else {
437
          mDebug("vgId:%d ts:%" PRId64 " HbMsgId:%d is valid", p->nodeId, p->hbTimestamp, p->lastHbMsgId);
6,665✔
438
        }
439
        break;
6,665✔
440
      }
441
    }
442
  }
443

444
  taosArrayDestroy(execInfo.pNodeList);
1,262✔
445
  execInfo.pNodeList = pValidList;
1,262✔
446

447
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
1,262✔
448
         (int32_t)taosArrayGetSize(pValidList), oldSize);
449
}
450

451
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) {
7,067✔
452
  void *p = taosHashGet(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
7,067✔
453
  if (p == NULL) {
7,067✔
454
    return TSDB_CODE_SUCCESS;
130✔
455
  }
456

457
  int32_t code = taosHashRemove(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
6,937✔
458
  if (code) {
6,937!
459
    return code;
×
460
  }
461

462
  for (int32_t k = 0; k < taosArrayGetSize(pExecNode->pTaskList); ++k) {
20,333!
463
    STaskId *pId = taosArrayGet(pExecNode->pTaskList, k);
20,333✔
464
    if (pId == NULL) {
20,333!
465
      continue;
×
466
    }
467

468
    if (pId->taskId == pRemovedId->taskId && pId->streamId == pRemovedId->streamId) {
20,333!
469
      taosArrayRemove(pExecNode->pTaskList, k);
6,937✔
470

471
      int32_t num = taosArrayGetSize(pExecNode->pTaskList);
6,937✔
472
      mInfo("s-task:0x%x removed from buffer, remain:%d in buffer list", (int32_t)pRemovedId->taskId, num);
6,937!
473
      break;
6,937✔
474
    }
475
  }
476

477
  return TSDB_CODE_SUCCESS;
6,937✔
478
}
479

480
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo* pExecInfo) {
2,905✔
481
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
2,905!
482
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
483
    if (pId == NULL) {
×
484
      continue;
×
485
    }
486

487
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
488
    if (code) {
×
489
      mError("failed to remove task in buffer list, 0x%"PRIx64, pId->taskId);
×
490
    }
491
  }
492
}
2,905✔
493

494
void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
1,291✔
495
  SStreamTaskIter *pIter = NULL;
1,291✔
496
  streamMutexLock(&pExecNode->lock);
1,291✔
497

498
  // 1. remove task entries
499
  int32_t code = createStreamTaskIter(pStream, &pIter);
1,291✔
500
  if (code) {
1,291!
501
    streamMutexUnlock(&pExecNode->lock);
×
502
    mError("failed to create stream task iter:%s", pStream->name);
×
503
    return;
×
504
  }
505

506
  while (streamTaskIterNextTask(pIter)) {
8,358✔
507
    SStreamTask *pTask = NULL;
7,067✔
508
    code = streamTaskIterGetCurrent(pIter, &pTask);
7,067✔
509
    if (code) {
7,067!
510
      continue;
×
511
    }
512

513
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,067✔
514
    code = doRemoveTasks(pExecNode, &id);
7,067✔
515
    if (code) {
7,067!
516
      mError("failed to remove task in buffer list, 0x%"PRIx64, id.taskId);
×
517
    }
518
  }
519

520
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
1,291!
521
    streamMutexUnlock(&pExecNode->lock);
×
522
    destroyStreamTaskIter(pIter);
×
523
    mError("task map size, task list size, not equal");
×
524
    return;
×
525
  }
526

527
  // 2. remove stream entry in consensus hash table and checkpoint-report hash table
528
  code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, pStream->uid);
1,291✔
529
  if (code) {
1,291!
530
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
531
  }
532

533
  code = mndClearChkptReportInfo(execInfo.pChkptStreams, pStream->uid);
1,291✔
534
  if (code) {
1,291✔
535
    mError("failed to clear the checkpoint report info, code:%s", tstrerror(code));
298!
536
  }
537

538
  streamMutexUnlock(&pExecNode->lock);
1,291✔
539
  destroyStreamTaskIter(pIter);
1,291✔
540
}
541

542
static bool taskNodeExists(SArray *pList, int32_t nodeId) {
12,694✔
543
  size_t num = taosArrayGetSize(pList);
12,694✔
544

545
  for (int32_t i = 0; i < num; ++i) {
116,156!
546
    SNodeEntry *pEntry = taosArrayGet(pList, i);
116,156✔
547
    if (pEntry == NULL) {
116,156!
548
      continue;
×
549
    }
550

551
    if (pEntry->nodeId == nodeId) {
116,156✔
552
      return true;
12,694✔
553
    }
554
  }
555

556
  return false;
×
557
}
558

559
int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) {
1,262✔
560
  SArray *pRemovedTasks = taosArrayInit(4, sizeof(STaskId));
1,262✔
561
  if (pRemovedTasks == NULL) {
1,262!
562
    return terrno;
×
563
  }
564

565
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
1,262✔
566
  for (int32_t i = 0; i < numOfTask; ++i) {
14,820✔
567
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
13,558✔
568
    if (pId == NULL) {
13,558!
569
      continue;
×
570
    }
571

572
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, pId, sizeof(*pId));
13,558✔
573
    if (pEntry == NULL) {
13,558!
574
      continue;
×
575
    }
576

577
    if (pEntry->nodeId == SNODE_HANDLE) {
13,558✔
578
      continue;
864✔
579
    }
580

581
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
12,694✔
582
    if (!existed) {
12,694!
583
      void* p = taosArrayPush(pRemovedTasks, pId);
×
584
      if (p == NULL) {
×
585
        mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId);
×
586
      }
587
    }
588
  }
589

590
  removeTasksInBuf(pRemovedTasks, &execInfo);
1,262✔
591

592
  mDebug("remove invalid stream tasks:%d, remain:%d", (int32_t)taosArrayGetSize(pRemovedTasks),
1,262✔
593
         (int32_t)taosArrayGetSize(execInfo.pTaskList));
594

595
  removeExpiredNodeInfo(pNodeSnapshot);
1,262✔
596

597
  taosArrayDestroy(pRemovedTasks);
1,262✔
598
  return 0;
1,262✔
599
}
600

601
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
25,880✔
602
  SMnode *pMnode = pReq->info.node;
25,880✔
603
  void   *pIter = NULL;
25,880✔
604
  int32_t code = 0;
25,880✔
605
  SArray *pDropped = taosArrayInit(4, sizeof(int64_t));
25,880✔
606
  if (pDropped == NULL) {
25,880!
607
    return terrno;
×
608
  }
609

610
  mDebug("start to scan checkpoint report info");
25,880✔
611
  streamMutexLock(&execInfo.lock);
25,880✔
612

613
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
90,143✔
614
    SChkptReportInfo* px = (SChkptReportInfo *)pIter;
65,360✔
615
    if (taosArrayGetSize(px->pTaskList) == 0) {
65,360✔
616
      continue;
64,205✔
617
    }
618

619
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
1,155✔
620
    if (pInfo == NULL) {
1,155!
621
      continue;
×
622
    }
623

624
    SStreamObj *pStream = NULL;
1,155✔
625
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
1,155✔
626
    if (pStream == NULL || code != 0) {
1,155!
627
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
628
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
629
      if (p == NULL) {
×
630
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
631
      }
632
      continue;
×
633
    }
634

635
    int32_t total = mndGetNumOfStreamTasks(pStream);
1,155✔
636
    int32_t existed = (int32_t)taosArrayGetSize(px->pTaskList);
1,155✔
637

638
    if (total == existed) {
1,155✔
639
      mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info",
1,097✔
640
             pStream->uid, pStream->name, total);
641

642
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,097✔
643
      if (code == 0) {
1,097!
644
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,097✔
645
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,097!
646
          taosArrayClear(px->pTaskList);
1,097✔
647
          px->reportChkpt = pInfo->checkpointId;
1,097✔
648
          mDebug("stream:0x%" PRIx64 " clear checkpoint-report list", pInfo->streamId);
1,097✔
649
        } else {
650
          mDebug("stream:0x%" PRIx64 " not launch chkpt-meta update trans, due to checkpoint not finished yet",
×
651
                 pInfo->streamId);
652
        }
653
        break;
1,097✔
654
      } else {
655
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
656
      }
657
    } else {
658
      mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pInfo->streamId, pStream->name,
58✔
659
             existed, total, total - existed);
660
    }
661

662
    sdbRelease(pMnode->pSdb, pStream);
58✔
663
  }
664

665
  int32_t size = taosArrayGetSize(pDropped);
25,880✔
666
  if (size > 0) {
25,880!
667
    for (int32_t i = 0; i < size; ++i) {
×
668
      int64_t* pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
669
      if (pStreamId == NULL) {
×
670
        continue;
×
671
      }
672

673
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
674
      if (code) {
×
675
        mError("failed to remove stream in buf:0x%"PRIx64, *pStreamId);
×
676
      }
677
    }
678

679
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
680
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
681
  }
682

683
  streamMutexUnlock(&execInfo.lock);
25,880✔
684

685
  taosArrayDestroy(pDropped);
25,880✔
686
  return TSDB_CODE_SUCCESS;
25,880✔
687
}
688

689
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int32_t taskId, int64_t checkpointId,
113✔
690
                                          int64_t ts) {
691
  char msg[128] = {0};
113✔
692
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for task:0x%x", taskId);
113✔
693

694
  STrans *pTrans = NULL;
113✔
695
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
113✔
696
  if (pTrans == NULL || code != 0) {
113!
697
    return terrno;
×
698
  }
699

700
  STaskId      id = {.streamId = pStream->uid, .taskId = taskId};
113✔
701
  SStreamTask *pTask = NULL;
113✔
702
  code = mndGetStreamTask(&id, pStream, &pTask);
113✔
703
  if (code) {
113!
704
    mError("failed to get task:0x%x in stream:%s, failed to create consensus-checkpointId", taskId, pStream->name);
×
705
    sdbRelease(pMnode->pSdb, pStream);
×
706
    return code;
×
707
  }
708

709
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
113✔
710
  if (code) {
113!
711
    sdbRelease(pMnode->pSdb, pStream);
×
712
    return code;
×
713
  }
714

715
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pTask, checkpointId, ts);
113✔
716
  if (code != 0) {
113!
717
    sdbRelease(pMnode->pSdb, pStream);
×
718
    mndTransDrop(pTrans);
×
719
    return code;
×
720
  }
721

722
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
113✔
723
  if (code) {
113!
724
    sdbRelease(pMnode->pSdb, pStream);
×
725
    mndTransDrop(pTrans);
×
726
    return code;
×
727
  }
728

729
  code = mndTransPrepare(pMnode, pTrans);
113✔
730
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
113!
UNCOV
731
    mError("trans:%d, failed to prepare set consensus-chkptId trans since %s", pTrans->id, terrstr());
×
UNCOV
732
    sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
733
    mndTransDrop(pTrans);
×
UNCOV
734
    return code;
×
735
  }
736

737
  sdbRelease(pMnode->pSdb, pStream);
113✔
738
  mndTransDrop(pTrans);
113✔
739

740
  return TSDB_CODE_ACTION_IN_PROGRESS;
113✔
741
}
742

743
int32_t mndGetConsensusInfo(SHashObj* pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
132✔
744
  *pInfo = NULL;
132✔
745

746
  void* px = taosHashGet(pHash, &streamId, sizeof(streamId));
132✔
747
  if (px != NULL) {
132✔
748
    *pInfo = px;
107✔
749
    return 0;
107✔
750
  }
751

752
  SCheckpointConsensusInfo p = {
25✔
753
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
25✔
754
      .numOfTasks = numOfTasks,
755
      .streamId = streamId,
756
  };
757

758
  if (p.pTaskList == NULL) {
25!
759
    return terrno;
×
760
  }
761

762
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
25✔
763
  if (code == 0) {
25!
764
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
25✔
765
    *pInfo = pChkptInfo;
25✔
766
  } else {
767
    *pInfo = NULL;
×
768
  }
769

770
  return code;
25✔
771
}
772

773
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
774
// discard the msg may lead to the lost of connections.
775
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
132✔
776
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
132✔
777
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
132✔
778

779
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pTaskList); ++i) {
425✔
780
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
307✔
781
    if (p == NULL) {
307!
782
      continue;
×
783
    }
784

785
    if (p->req.taskId == info.req.taskId) {
307✔
786
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update ts %" PRId64
14✔
787
             "->%" PRId64 " total existed:%d",
788
             pRestoreInfo->taskId, pRestoreInfo->streamId, p->req.startTs, info.req.startTs,
789
             (int32_t)taosArrayGetSize(pInfo->pTaskList));
790
      p->req.startTs = info.req.startTs;
14✔
791
      return;
14✔
792
    }
793
  }
794

795
  void *p = taosArrayPush(pInfo->pTaskList, &info);
118✔
796
  if (p == NULL) {
118!
797
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
798
  } else {
799
    int32_t num = taosArrayGetSize(pInfo->pTaskList);
118✔
800
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
118✔
801
           " waiting tasks:%d",
802
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
803
  }
804
}
805

806
void mndClearConsensusRspEntry(SCheckpointConsensusInfo* pInfo) {
24✔
807
  taosArrayDestroy(pInfo->pTaskList);
24✔
808
  pInfo->pTaskList = NULL;
24✔
809
}
24✔
810

811
int64_t mndClearConsensusCheckpointId(SHashObj* pHash, int64_t streamId) {
1,315✔
812
  int32_t code = 0;
1,315✔
813
  int32_t numOfStreams = taosHashGetSize(pHash);
1,315✔
814
  if (numOfStreams == 0) {
1,315✔
815
    return code;
1,291✔
816
  }
817

818
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
24✔
819
  if (code == 0) {
24!
820
    mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
24✔
821
  } else {
822
    mError("failed to remove stream:0x%"PRIx64" in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
823
  }
824

825
  return code;
24✔
826
}
827

828
int64_t mndClearChkptReportInfo(SHashObj* pHash, int64_t streamId) {
1,291✔
829
  int32_t code = 0;
1,291✔
830
  int32_t numOfStreams = taosHashGetSize(pHash);
1,291✔
831
  if (numOfStreams == 0) {
1,291✔
832
    return code;
295✔
833
  }
834

835
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
996✔
836
  if (code == 0) {
996✔
837
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
698✔
838
  } else {
839
    mError("failed to remove stream:0x%"PRIx64" in chkpt-report list, remain:%d", streamId, numOfStreams);
298!
840
  }
841

842
  return code;
996✔
843
}
844

845
int32_t mndResetChkptReportInfo(SHashObj* pHash, int64_t streamId) {
×
846
  SChkptReportInfo* pInfo = taosHashGet(pHash, &streamId, sizeof(streamId));
×
847
  if (pInfo != NULL) {
×
848
    taosArrayClear(pInfo->pTaskList);
×
849
    mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId,
×
850
           pInfo->reportChkpt);
851
    return 0;
×
852
  }
853

854
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
855
}
856

857
static void mndShowStreamStatus(char *dst, SStreamObj *pStream) {
32,990✔
858
  int8_t status = atomic_load_8(&pStream->status);
32,990✔
859
  if (status == STREAM_STATUS__NORMAL) {
32,993!
860
    strcpy(dst, "ready");
33,015✔
861
  } else if (status == STREAM_STATUS__STOP) {
×
862
    strcpy(dst, "stop");
×
863
  } else if (status == STREAM_STATUS__FAILED) {
×
864
    strcpy(dst, "failed");
×
865
  } else if (status == STREAM_STATUS__RECOVER) {
×
866
    strcpy(dst, "recover");
×
867
  } else if (status == STREAM_STATUS__PAUSE) {
×
868
    strcpy(dst, "paused");
×
869
  }
870
}
32,993✔
871

872
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
33,016✔
873
  int8_t trigger = pStream->conf.trigger;
33,016✔
874
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
33,016✔
875
    strcpy(dst, "at once");
11,301✔
876
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
21,715✔
877
    strcpy(dst, "window close");
10,906✔
878
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
10,809!
879
    strcpy(dst, "max delay");
10,840✔
880
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
881
    strcpy(dst, "force window close");
14✔
882
  }
883
}
33,016✔
884

885
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
233,859✔
886
  memset(pBuf, 0, bufLen);
233,859✔
887
  pBuf[2] = '0';
233,859✔
888
  pBuf[3] = 'x';
233,859✔
889

890
  int32_t len = tintToHex(id, &pBuf[4]);
233,859✔
891
  varDataSetLen(pBuf, len + 2);
234,409✔
892
}
234,409✔
893

894
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
33,091✔
895
  int32_t code = 0;
33,091✔
896
  int32_t cols = 0;
33,091✔
897
  int32_t lino = 0;
33,091✔
898

899
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,091✔
900
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
33,091✔
901
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,092✔
902
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,072!
903

904
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
33,072✔
905
  TSDB_CHECK_CODE(code, lino, _end);
33,067!
906

907
  // create time
908
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,067✔
909
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,057!
910
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
33,057✔
911
  TSDB_CHECK_CODE(code, lino, _end);
33,048!
912

913
  // stream id
914
  char buf[128] = {0};
33,048✔
915
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
33,048✔
916
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,088✔
917
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,072!
918
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
33,072✔
919
  TSDB_CHECK_CODE(code, lino, _end);
33,066!
920

921
  // related fill-history stream id
922
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,066✔
923
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,062!
924
  if (pStream->hTaskUid != 0) {
33,063!
925
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
926
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
927
  } else {
928
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
33,063✔
929
  }
930
  TSDB_CHECK_CODE(code, lino, _end);
33,042!
931

932
  // related fill-history stream id
933
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
33,042✔
934
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
33,042✔
935
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,042✔
936
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,059!
937
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
33,059✔
938
  TSDB_CHECK_CODE(code, lino, _end);
33,006!
939

940
  char status[20 + VARSTR_HEADER_SIZE] = {0};
33,006✔
941
  char status2[20] = {0};
33,006✔
942
  mndShowStreamStatus(status2, pStream);
33,006✔
943
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
33,013✔
944
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,013✔
945
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,072!
946

947
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
33,072✔
948
  TSDB_CHECK_CODE(code, lino, _end);
33,059!
949

950
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,059✔
951
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
33,059✔
952
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,054✔
953
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,049!
954

955
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
33,049✔
956
  TSDB_CHECK_CODE(code, lino, _end);
33,059!
957

958
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,059✔
959
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
33,059✔
960
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,058✔
961
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,047!
962

963
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
33,047✔
964
  TSDB_CHECK_CODE(code, lino, _end);
33,057!
965

966
  if (pStream->targetSTbName[0] == 0) {
33,057✔
967
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
2✔
968
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
2!
969

970
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
2✔
971
  } else {
972
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
33,055✔
973
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
33,055✔
974
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,065✔
975
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,051!
976

977
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
33,051✔
978
  }
979
  TSDB_CHECK_CODE(code, lino, _end);
33,037!
980

981
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,037✔
982
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,028!
983

984
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
33,028✔
985
  TSDB_CHECK_CODE(code, lino, _end);
33,001!
986

987
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
33,001✔
988
  char trigger2[20] = {0};
33,001✔
989
  mndShowStreamTrigger(trigger2, pStream);
33,001✔
990
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
33,016✔
991
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,016✔
992
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,063!
993

994
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
33,063✔
995
  TSDB_CHECK_CODE(code, lino, _end);
33,076!
996

997
  // sink_quota
998
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
33,076✔
999
  sinkQuota[0] = '0';
33,076✔
1000
  char dstStr[20] = {0};
33,076✔
1001
  STR_TO_VARSTR(dstStr, sinkQuota)
33,076✔
1002
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,076✔
1003
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,074!
1004

1005
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,074✔
1006
  TSDB_CHECK_CODE(code, lino, _end);
33,063!
1007

1008
  // checkpoint interval
1009
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
33,063✔
1010
  sprintf(varDataVal(tmp), "%d sec", tsStreamCheckpointInterval);
33,063✔
1011
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
33,063✔
1012

1013
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,063✔
1014
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,049!
1015

1016
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
33,049✔
1017
  TSDB_CHECK_CODE(code, lino, _end);
33,057!
1018

1019
  // checkpoint backup type
1020
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
33,057✔
1021
  STR_TO_VARSTR(backup, "none")
33,057✔
1022
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,057✔
1023
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,048!
1024

1025
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
33,048✔
1026
  TSDB_CHECK_CODE(code, lino, _end);
33,067!
1027

1028
  // history scan idle
1029
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
33,067✔
1030
  strcpy(scanHistoryIdle, "100a");
33,067✔
1031

1032
  memset(dstStr, 0, tListLen(dstStr));
33,067✔
1033
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
33,067✔
1034
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,067✔
1035
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,074!
1036

1037
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
33,074✔
1038

1039
_end:
33,068✔
1040
  if (code) {
33,068!
1041
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1042
  }
1043
  return code;
33,071✔
1044
}
1045

1046
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows) {
200,481✔
1047
  SColumnInfoData *pColInfo = NULL;
200,481✔
1048
  int32_t          cols = 0;
200,481✔
1049
  int32_t          code = 0;
200,481✔
1050
  int32_t          lino = 0;
200,481✔
1051

1052
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
200,481✔
1053

1054
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
200,481✔
1055
  if (pe == NULL) {
201,143!
1056
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1057
               " no valid status/stage info",
1058
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1059
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1060
  }
1061

1062
  // stream name
1063
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
201,143✔
1064
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
201,143✔
1065

1066
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
201,067✔
1067
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,718!
1068

1069
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
200,718✔
1070
  TSDB_CHECK_CODE(code, lino, _end);
200,474!
1071

1072
  // task id
1073
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,474✔
1074
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,279!
1075

1076
  char idstr[128] = {0};
200,279✔
1077
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
200,279✔
1078
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
201,111✔
1079
  TSDB_CHECK_CODE(code, lino, _end);
200,684!
1080

1081
  // node type
1082
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
200,684✔
1083
  varDataSetLen(nodeType, 5);
200,684✔
1084
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,684✔
1085
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,541!
1086

1087
  if (pTask->info.nodeId > 0) {
200,630✔
1088
    memcpy(varDataVal(nodeType), "vnode", 5);
183,346✔
1089
  } else {
1090
    memcpy(varDataVal(nodeType), "snode", 5);
17,284✔
1091
  }
1092
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
200,630✔
1093
  TSDB_CHECK_CODE(code, lino, _end);
200,603!
1094

1095
  // node id
1096
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,603✔
1097
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,429!
1098

1099
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
200,429✔
1100
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
200,429✔
1101
  TSDB_CHECK_CODE(code, lino, _end);
200,276!
1102

1103
  // level
1104
  char level[20 + VARSTR_HEADER_SIZE] = {0};
200,276✔
1105
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
200,276✔
1106
    memcpy(varDataVal(level), "source", 6);
102,305✔
1107
    varDataSetLen(level, 6);
102,305✔
1108
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
97,971✔
1109
    memcpy(varDataVal(level), "agg", 3);
18,653✔
1110
    varDataSetLen(level, 3);
18,653✔
1111
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
79,318!
1112
    memcpy(varDataVal(level), "sink", 4);
79,876✔
1113
    varDataSetLen(level, 4);
79,876✔
1114
  }
1115

1116
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,276✔
1117
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,257!
1118

1119
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
200,257✔
1120
  TSDB_CHECK_CODE(code, lino, _end);
200,540!
1121

1122
  // status
1123
  char status[20 + VARSTR_HEADER_SIZE] = {0};
200,540✔
1124

1125
  const char *pStatus = streamTaskGetStatusStr(pe->status);
200,540✔
1126
  STR_TO_VARSTR(status, pStatus);
200,581✔
1127

1128
  // status
1129
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,581✔
1130
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,493!
1131

1132
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
200,493✔
1133
  TSDB_CHECK_CODE(code, lino, _end);
200,458!
1134

1135
  // stage
1136
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,458✔
1137
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,294!
1138

1139
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
200,294✔
1140
  TSDB_CHECK_CODE(code, lino, _end);
200,200!
1141

1142
  // input queue
1143
  char        vbuf[40] = {0};
200,200✔
1144
  char        buf[38] = {0};
200,200✔
1145
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
200,200✔
1146
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
200,200✔
1147
  STR_TO_VARSTR(vbuf, buf);
200,200✔
1148

1149
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,200✔
1150
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,908!
1151

1152
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
200,908✔
1153
  TSDB_CHECK_CODE(code, lino, _end);
200,659!
1154

1155
  // input total
1156
  const char *formatTotalMb = "%7.2f MiB";
200,659✔
1157
  const char *formatTotalGb = "%7.2f GiB";
200,659✔
1158
  if (pe->procsTotal < 1024) {
200,659!
1159
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
200,707✔
1160
  } else {
1161
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1162
  }
1163

1164
  memset(vbuf, 0, tListLen(vbuf));
200,659✔
1165
  STR_TO_VARSTR(vbuf, buf);
200,659✔
1166

1167
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,659✔
1168
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,940!
1169

1170
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
200,940✔
1171
  TSDB_CHECK_CODE(code, lino, _end);
200,612!
1172

1173
  // process throughput
1174
  const char *formatKb = "%7.2f KiB/s";
200,612✔
1175
  const char *formatMb = "%7.2f MiB/s";
200,612✔
1176
  if (pe->procsThroughput < 1024) {
200,612✔
1177
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
200,510✔
1178
  } else {
1179
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
102✔
1180
  }
1181

1182
  memset(vbuf, 0, tListLen(vbuf));
200,612✔
1183
  STR_TO_VARSTR(vbuf, buf);
200,612✔
1184

1185
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,612✔
1186
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,917!
1187

1188
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
200,917✔
1189
  TSDB_CHECK_CODE(code, lino, _end);
200,585!
1190

1191
  // output total
1192
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,585✔
1193
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,396!
1194

1195
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
200,800✔
1196
    colDataSetNULL(pColInfo, numOfRows);
79,905!
1197
  } else {
1198
    sprintf(buf, formatTotalMb, pe->outputTotal);
120,895✔
1199
    memset(vbuf, 0, tListLen(vbuf));
120,895✔
1200
    STR_TO_VARSTR(vbuf, buf);
120,895✔
1201

1202
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
120,895✔
1203
    TSDB_CHECK_CODE(code, lino, _end);
121,104!
1204
  }
1205

1206
  // output throughput
1207
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
201,009✔
1208
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,581!
1209

1210
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
200,898✔
1211
    colDataSetNULL(pColInfo, numOfRows);
79,864!
1212
  } else {
1213
    if (pe->outputThroughput < 1024) {
121,034✔
1214
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
121,007✔
1215
    } else {
1216
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
27✔
1217
    }
1218

1219
    memset(vbuf, 0, tListLen(vbuf));
121,034✔
1220
    STR_TO_VARSTR(vbuf, buf);
121,034✔
1221

1222
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
121,034✔
1223
    TSDB_CHECK_CODE(code, lino, _end);
121,074!
1224
  }
1225

1226
  // output queue
1227
  //          sprintf(buf, queueInfoStr, pe->outputQUsed, pe->outputRate);
1228
  //        STR_TO_VARSTR(vbuf, buf);
1229

1230
  //        pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1231
  //        colDataSetVal(pColInfo, numOfRows, (const char*)vbuf, false);
1232

1233
  // info
1234
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
200,938✔
1235
    const char *sinkStr = "%.2f MiB";
79,859✔
1236
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
79,859✔
1237
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
121,079✔
1238
    // offset info
1239
    const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
102,450✔
1240
    snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
102,450✔
1241
  } else {
1242
    memset(buf, 0, tListLen(buf));
18,629✔
1243
  }
1244

1245
  STR_TO_VARSTR(vbuf, buf);
200,938✔
1246

1247
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,938✔
1248
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,936!
1249

1250
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
200,936✔
1251
  TSDB_CHECK_CODE(code, lino, _end);
200,561!
1252

1253
  // start_time
1254
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,561✔
1255
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,388!
1256

1257
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
200,388✔
1258
  TSDB_CHECK_CODE(code, lino, _end);
200,126!
1259

1260
  // start id
1261
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,126✔
1262
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,947!
1263

1264
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
199,947✔
1265
  TSDB_CHECK_CODE(code, lino, _end);
199,922!
1266

1267
  // start ver
1268
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,922✔
1269
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,813!
1270

1271
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
199,813✔
1272
  TSDB_CHECK_CODE(code, lino, _end);
199,803!
1273

1274
  // checkpoint time
1275
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,803✔
1276
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,718!
1277

1278
  if (pe->checkpointInfo.latestTime != 0) {
199,800✔
1279
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
173,451✔
1280
  } else {
1281
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
26,349✔
1282
  }
1283
  TSDB_CHECK_CODE(code, lino, _end);
199,892!
1284

1285
  // checkpoint_id
1286
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,892✔
1287
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,785!
1288

1289
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
199,785✔
1290
  TSDB_CHECK_CODE(code, lino, _end);
199,808!
1291

1292
  // checkpoint version
1293
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,808✔
1294
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,671!
1295

1296
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
199,671✔
1297
  TSDB_CHECK_CODE(code, lino, _end);
199,650!
1298

1299
  // checkpoint size
1300
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,650✔
1301
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,595!
1302

1303
  colDataSetNULL(pColInfo, numOfRows);
199,650!
1304

1305
  // checkpoint backup status
1306
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,650✔
1307
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,555!
1308

1309
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
199,555✔
1310
  TSDB_CHECK_CODE(code, lino, _end);
199,588!
1311

1312
  // ds_err_info
1313
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,588✔
1314
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,442!
1315

1316
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
199,442✔
1317
  TSDB_CHECK_CODE(code, lino, _end);
199,388!
1318

1319
  // history_task_id
1320
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,388✔
1321
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,131!
1322

1323
  if (pe->hTaskId != 0) {
199,177✔
1324
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
183✔
1325
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
183✔
1326
  } else {
1327
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
198,994✔
1328
  }
1329
  TSDB_CHECK_CODE(code, lino, _end);
199,404!
1330

1331
  // history_task_status
1332
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,404✔
1333
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,245!
1334

1335
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
199,245✔
1336
  TSDB_CHECK_CODE(code, lino, _end);
199,465!
1337

1338
  _end:
199,465✔
1339
  if (code) {
199,465!
1340
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1341
  }
1342
  return code;
199,482✔
1343
}
1344

1345
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
11,901✔
1346
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
11,901✔
1347
  const SEp *p = GET_ACTIVE_EP(pCurrent);
11,901✔
1348

1349
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
11,901!
1350
    return false;
11,901✔
1351
  }
UNCOV
1352
  return true;
×
1353
}
1354

1355
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo* pInfo) {
2,443✔
1356
  if (pInfo != NULL) {
2,443!
1357
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,443✔
1358
    taosHashCleanup(pInfo->pDBMap);
2,443✔
1359
  }
1360
}
2,443✔
1361

1362
// 1. increase the replica does not affect the stream process.
1363
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1364
// tasks on the will be removed replica.
1365
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1366
// will handle it as mentioned in 1 & 2 items.
1367
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
2,443✔
1368
                               SVgroupChangeInfo *pInfo) {
1369
  int32_t code = 0;
2,443✔
1370
  int32_t lino = 0;
2,443✔
1371

1372
  if (pInfo == NULL) {
2,443!
1373
    return TSDB_CODE_INVALID_PARA;
×
1374
  }
1375

1376
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo)),
2,443✔
1377
      pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
2,443✔
1378

1379
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
2,443!
1380
    mndDestroyVgroupChangeInfo(pInfo);
×
1381
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1382
  }
1383

1384
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
2,443✔
1385
  for (int32_t i = 0; i < numOfNodes; ++i) {
15,047✔
1386
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
12,604✔
1387
    if (pPrevEntry == NULL) {
12,604!
1388
      continue;
×
1389
    }
1390

1391
    int32_t num = taosArrayGetSize(pNodeList);
12,604✔
1392
    for (int32_t j = 0; j < num; ++j) {
173,971✔
1393
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
173,291✔
1394
      if(pCurrent == NULL) {
173,291!
1395
        continue;
×
1396
      }
1397

1398
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
173,291✔
1399
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
11,924!
1400
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
23✔
1401

1402
          char buf[256] = {0};
23✔
1403
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
23✔
1404
          if (code) {
23!
1405
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1406
            TSDB_CHECK_CODE(code, lino, _err);
×
1407
          }
1408

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

1412
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
23✔
1413
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
23✔
1414
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
23✔
1415

1416
          void* p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
23✔
1417
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
23!
1418
        }
1419

1420
        // todo handle the snode info
1421
        if (pCurrent->nodeId != SNODE_HANDLE) {
11,924✔
1422
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
10,395✔
1423
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
10,395✔
1424
          mndReleaseVgroup(pMnode, pVgroup);
10,395✔
1425
          TSDB_CHECK_CODE(code, lino, _err);
10,395!
1426
        }
1427

1428
        break;
11,924✔
1429
      }
1430
    }
1431
  }
1432

1433
  return code;
2,443✔
1434

1435
  _err:
×
1436
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1437
  mndDestroyVgroupChangeInfo(pInfo);
×
1438
  return code;
×
1439
  }
1440

1441
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
2,032✔
1442
  bool              allReady = false;
2,032✔
1443
  bool              nodeUpdated = false;
2,032✔
1444
  SVgroupChangeInfo changeInfo = {0};
2,032✔
1445

1446
  int32_t numOfNodes = extractStreamNodeList(pMnode);
2,032✔
1447

1448
  if (numOfNodes == 0) {
2,032✔
1449
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
803✔
1450
    execInfo.ts = taosGetTimestampSec();
803✔
1451
    return false;
803✔
1452
  }
1453

1454
  for (int32_t i = 0; i < numOfNodes; ++i) {
7,283✔
1455
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
6,084✔
1456
    if (pNodeEntry == NULL) {
6,084!
1457
      continue;
×
1458
    }
1459

1460
    if (pNodeEntry->stageUpdated) {
6,084✔
1461
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
30✔
1462
      return true;
30✔
1463
    }
1464
  }
1465

1466
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot);
1,199✔
1467
  if (code) {
1,199!
1468
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1469
  }
1470

1471
  if (!allReady) {
1,199✔
1472
    mWarn("not all vnodes ready, quit from vnodes status check");
18!
1473
    return true;
18✔
1474
  }
1475

1476
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
1,181✔
1477
  if (code) {
1,181!
1478
    nodeUpdated = false;
×
1479
  } else {
1480
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
1,181✔
1481
    if (nodeUpdated) {
1,181!
1482
      mDebug("stream tasks not ready due to node update");
×
1483
    }
1484
  }
1485

1486
  mndDestroyVgroupChangeInfo(&changeInfo);
1,181✔
1487
  return nodeUpdated;
1,181✔
1488
}
1489

1490
// check if the node update happens or not
1491
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
2,032✔
1492
  SArray *pNodeSnapshot = NULL;
2,032✔
1493

1494
  streamMutexLock(&execInfo.lock);
2,032✔
1495
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
2,032✔
1496
  streamMutexUnlock(&execInfo.lock);
2,032✔
1497

1498
  taosArrayDestroy(pNodeSnapshot);
2,032✔
1499
  return updated;
2,032✔
1500
}
1501

1502
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
1,565✔
1503
  SSdb      *pSdb = pMnode->pSdb;
1,565✔
1504
  void      *pIter = NULL;
1,565✔
1505
  SSnodeObj *pObj = NULL;
1,565✔
1506

1507
  if (pSrcDb->cfg.replications == 1) {
1,565✔
1508
    return TSDB_CODE_SUCCESS;
1,562✔
1509
  } else {
1510
    while (1) {
1511
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
3✔
1512
      if (pIter == NULL) {
3✔
1513
        break;
2✔
1514
      }
1515

1516
      sdbRelease(pSdb, pObj);
1✔
1517
      sdbCancelFetch(pSdb, pIter);
1✔
1518
      return TSDB_CODE_SUCCESS;
1✔
1519
    }
1520

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

© 2025 Coveralls, Inc