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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

web-flow
Merge pull request #28973 from taosdata/merge/mainto3.0

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

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

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

46
  return 0;
66,135✔
47
}
48

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

55
  if (pIter->level == -1) {
306,027✔
56
    pIter->level += 1;
66,134✔
57
  }
58

59
  while(pIter->level < pIter->totalLevel) {
436,453✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->tasks, pIter->level);
370,246✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
370,348✔
62
      pIter->level += 1;
130,426✔
63
      pIter->ordinalIndex = 0;
130,426✔
64
      pIter->pTask = NULL;
130,426✔
65
      continue;
130,426✔
66
    }
67

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter* pIter) {
66,024✔
89
  taosMemoryFree(pIter);
66,024✔
90
}
66,115✔
91

92
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
249,562✔
93
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
505,299✔
94
    if (!pVgroup->vnodeGid[i].syncRestore) {
256,915✔
95
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,172!
96
      return false;
1,172✔
97
    }
98

99
    ESyncState state = pVgroup->vnodeGid[i].syncState;
255,743✔
100
    if (state == TAOS_SYNC_STATE_OFFLINE || state == TAOS_SYNC_STATE_ERROR || state == TAOS_SYNC_STATE_LEARNER ||
255,743!
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,
6!
103
            state);
104
      return false;
6✔
105
    }
106
  }
107

108
  return true;
248,384✔
109
}
110

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

119
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
14,744✔
120
  if (pVgroupList == NULL) {
14,744!
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,744✔
127
  if (pHash == NULL) {
14,744!
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,744✔
134

135
  while (1) {
251,815✔
136
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
266,559✔
137
    if (pIter == NULL) {
266,559✔
138
      break;
14,744✔
139
    }
140

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

144
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
251,815✔
145
    if (pReplica == NULL) {  // not exist, add it into hash map
251,815✔
146
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
125,155✔
147
      if (code) {
125,155!
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) {
126,660✔
155
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
800!
156
              pVgroup->vgId, pVgroup->replica, *pReplica);
157
        *allReady = false;  // task snap success, but not all ready
800✔
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) {
251,815✔
164
      *allReady = checkStatusForEachReplica(pVgroup);
249,562✔
165
    }
166

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

173
    void *p = taosArrayPush(pVgroupList, &entry);
251,815✔
174
    if (p == NULL) {
251,815!
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);
251,815✔
182
    }
183

184
    sdbRelease(pSdb, pVgroup);
251,815✔
185
  }
186

187
  SSnodeObj *pObj = NULL;
14,744✔
188
  while (1) {
6,224✔
189
    pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
20,968✔
190
    if (pIter == NULL) {
20,968✔
191
      break;
14,744✔
192
    }
193

194
    SNodeEntry entry = {.nodeId = SNODE_HANDLE};
6,224✔
195
    code = addEpIntoEpSet(&entry.epset, pObj->pDnode->fqdn, pObj->pDnode->port);
6,224✔
196
    if (code) {
6,224!
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,224✔
204
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
6,224✔
205
    if (code != 0) {  // print error and continue
6,224!
206
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
207
    }
208

209
    void *p = taosArrayPush(pVgroupList, &entry);
6,224✔
210
    if (p == NULL) {
6,224!
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,224✔
218
    }
219

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

223
  *pList = pVgroupList;
14,744✔
224
  taosHashCleanup(pHash);
14,744✔
225
  return code;
14,744✔
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,086✔
236
  void *pIter = NULL;
13,086✔
237
  SSdb *pSdb = pMnode->pSdb;
13,086✔
238
  *pStream = NULL;
13,086✔
239

240
  SStreamObj *p = NULL;
13,086✔
241
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22,248✔
242
    if (p->uid == streamId) {
22,245✔
243
      sdbCancelFetch(pSdb, pIter);
13,083✔
244
      *pStream = p;
13,083✔
245
      return TSDB_CODE_SUCCESS;
13,083✔
246
    }
247
    sdbRelease(pSdb, p);
9,162✔
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,513✔
268
  *hasEpset = false;
18,513✔
269

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

275
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
287✔
276
    if (pIter != NULL) {
287!
277
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
287✔
278
      sdbRelease(pMnode->pSdb, pObj);
287✔
279
      sdbCancelFetch(pMnode->pSdb, pIter);
287✔
280
      if (code) {
287!
281
        *hasEpset = false;
×
282
        mError("failed to set epset");
×
283
      } else {
284
        *hasEpset = true;
287✔
285
      }
286
      return code;
287✔
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,226✔
293
    if (pVgObj != NULL) {
18,226!
294
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
18,226✔
295
      mndReleaseVgroup(pMnode, pVgObj);
18,226✔
296

297
      epsetAssign(pEpSet, &epset);
18,226✔
298
      *hasEpset = true;
18,226✔
299
      return TSDB_CODE_SUCCESS;
18,226✔
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) {
111✔
308
  *pTask = NULL;
111✔
309

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

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

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

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

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

342
  return num;
70,703✔
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,621✔
372
  SArray** pList = (SArray **)param;
1,621✔
373
  taosArrayDestroy(*pList);
1,621✔
374
}
1,621✔
375

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

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

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

393
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
2,007!
394
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
2,007!
395
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
2,007!
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,007✔
401
  execInfo.switchFromFollower = false;
2,007✔
402

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

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

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

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

424
    for (int32_t j = 0; j < size; ++j) {
100,908✔
425
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
99,869✔
426
      if (pEntry == NULL) {
99,869!
427
        continue;
×
428
      }
429

430
      if (pEntry->nodeId == p->nodeId) {
99,869✔
431
        p->hbTimestamp = pEntry->hbTimestamp;
6,637✔
432

433
        void* px = taosArrayPush(pValidList, p);
6,637✔
434
        if (px == NULL) {
6,637!
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,637✔
438
        }
439
        break;
6,637✔
440
      }
441
    }
442
  }
443

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

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

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

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

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

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

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

477
  return TSDB_CODE_SUCCESS;
7,117✔
478
}
479

480
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo* pExecInfo) {
2,893✔
481
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
2,893!
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,893✔
493

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

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

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

513
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,247✔
514
    code = doRemoveTasks(pExecNode, &id);
7,247✔
515
    if (code) {
7,247!
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,332!
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,332✔
529
  if (code) {
1,332!
530
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
531
  }
532

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

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

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

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

551
    if (pEntry->nodeId == nodeId) {
110,133✔
552
      return true;
12,685✔
553
    }
554
  }
555

556
  return false;
×
557
}
558

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

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

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

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

581
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
12,685✔
582
    if (!existed) {
12,685!
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,258✔
591

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

595
  removeExpiredNodeInfo(pNodeSnapshot);
1,258✔
596

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

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

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

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

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

624
    SStreamObj *pStream = NULL;
1,140✔
625
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
1,140✔
626
    if (pStream == NULL || code != 0) {
1,140!
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,140✔
636
    int32_t existed = (int32_t)taosArrayGetSize(px->pTaskList);
1,140✔
637

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

642
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
1,107✔
643
      if (code == 0) {
1,107!
644
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
1,107✔
645
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
1,107!
646
          taosArrayClear(px->pTaskList);
1,107✔
647
          px->reportChkpt = pInfo->checkpointId;
1,107✔
648
          mDebug("stream:0x%" PRIx64 " clear checkpoint-report list", pInfo->streamId);
1,107✔
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,107✔
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,
33✔
659
             existed, total, total - existed);
660
    }
661

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

665
  int32_t size = taosArrayGetSize(pDropped);
25,778✔
666
  if (size > 0) {
25,778!
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,778✔
684

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

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

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

700
  STaskId      id = {.streamId = pStream->uid, .taskId = taskId};
111✔
701
  SStreamTask *pTask = NULL;
111✔
702
  code = mndGetStreamTask(&id, pStream, &pTask);
111✔
703
  if (code) {
111!
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);
111✔
710
  if (code) {
111!
711
    sdbRelease(pMnode->pSdb, pStream);
×
712
    return code;
×
713
  }
714

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

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

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

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

740
  return TSDB_CODE_ACTION_IN_PROGRESS;
107✔
741
}
742

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

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

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

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

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

770
  return code;
26✔
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) {
125✔
776
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
125✔
777
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
125✔
778

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

785
    if (p->req.taskId == info.req.taskId) {
268✔
786
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update ts %" PRId64
10✔
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;
10✔
791
      return;
10✔
792
    }
793
  }
794

795
  void *p = taosArrayPush(pInfo->pTaskList, &info);
115✔
796
  if (p == NULL) {
115!
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);
115✔
800
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
115✔
801
           " waiting tasks:%d",
802
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
803
  }
804
}
805

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

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

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

825
  return code;
25✔
826
}
827

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

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

842
  return code;
1,022✔
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) {
33,033✔
858
  int8_t status = atomic_load_8(&pStream->status);
33,033✔
859
  if (status == STREAM_STATUS__NORMAL) {
33,048!
860
    strcpy(dst, "ready");
33,067✔
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
}
33,048✔
871

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

966
  if (pStream->targetSTbName[0] == 0) {
33,071✔
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,069✔
973
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
33,069✔
974
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
33,110✔
975
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
33,079!
976

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1054
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
200,241✔
1055
  if (pe == NULL) {
200,857!
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};
200,857✔
1064
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
200,857✔
1065

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1210
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
200,706✔
1211
    colDataSetNULL(pColInfo, numOfRows);
79,757!
1212
  } else {
1213
    if (pe->outputThroughput < 1024) {
120,949✔
1214
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
120,872✔
1215
    } else {
1216
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
77✔
1217
    }
1218

1219
    memset(vbuf, 0, tListLen(vbuf));
120,949✔
1220
    STR_TO_VARSTR(vbuf, buf);
120,949✔
1221

1222
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
120,949✔
1223
    TSDB_CHECK_CODE(code, lino, _end);
121,001!
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,758✔
1235
    const char *sinkStr = "%.2f MiB";
79,756✔
1236
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
79,756✔
1237
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
121,002✔
1238
    // offset info
1239
    const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
102,285✔
1240
    snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
102,285✔
1241
  } else {
1242
    memset(buf, 0, tListLen(buf));
18,717✔
1243
  }
1244

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1355
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo* pInfo) {
2,426✔
1356
  if (pInfo != NULL) {
2,426!
1357
    taosArrayDestroy(pInfo->pUpdateNodeList);
2,426✔
1358
    taosHashCleanup(pInfo->pDBMap);
2,426✔
1359
  }
1360
}
2,426✔
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,426✔
1368
                               SVgroupChangeInfo *pInfo) {
1369
  int32_t code = 0;
2,426✔
1370
  int32_t lino = 0;
2,426✔
1371

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

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

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

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

1391
    int32_t num = taosArrayGetSize(pNodeList);
12,533✔
1392
    for (int32_t j = 0; j < num; ++j) {
164,663✔
1393
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
163,982✔
1394
      if(pCurrent == NULL) {
163,982!
1395
        continue;
×
1396
      }
1397

1398
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
163,982✔
1399
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
11,852✔
1400
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
18✔
1401

1402
          char buf[256] = {0};
18✔
1403
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
18✔
1404
          if (code) {
18!
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,
18✔
1410
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1411

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

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

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

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

1433
  return code;
2,426✔
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) {
1,998✔
1442
  bool              allReady = false;
1,998✔
1443
  bool              nodeUpdated = false;
1,998✔
1444
  SVgroupChangeInfo changeInfo = {0};
1,998✔
1445

1446
  int32_t numOfNodes = extractStreamNodeList(pMnode);
1,998✔
1447

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

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

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

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

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

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

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

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

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

1498
  taosArrayDestroy(pNodeSnapshot);
1,998✔
1499
  return updated;
1,998✔
1500
}
1501

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

1507
  if (pSrcDb->cfg.replications == 1) {
1,608✔
1508
    return TSDB_CODE_SUCCESS;
1,605✔
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