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

taosdata / TDengine / #3542

27 Nov 2024 02:52AM UTC coverage: 60.819% (+0.04%) from 60.776%
#3542

push

travis-ci

web-flow
Merge pull request #28931 from taosdata/enh/jdbc-demo-3.0

update jdbc demo, and version history

120305 of 252779 branches covered (47.59%)

Branch coverage included in aggregate %.

201010 of 275538 relevant lines covered (72.95%)

19989893.51 hits per line

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

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

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

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

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

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

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

46
  return 0;
66,010✔
47
}
48

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

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

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

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter* pIter) {
65,946✔
89
  taosMemoryFree(pIter);
65,946✔
90
}
65,973✔
91

92
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
265,526✔
93
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
536,333✔
94
    if (!pVgroup->vnodeGid[i].syncRestore) {
272,031✔
95
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,207!
96
      return false;
1,207✔
97
    }
98

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

108
  return true;
264,302✔
109
}
110

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

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

135
  while (1) {
268,344✔
136
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
283,291✔
137
    if (pIter == NULL) {
283,291✔
138
      break;
14,947✔
139
    }
140

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

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

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

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

184
    sdbRelease(pSdb, pVgroup);
268,344✔
185
  }
186

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

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

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

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

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

240
  SStreamObj *p = NULL;
12,996✔
241
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22,096✔
242
    if (p->uid == streamId) {
22,093✔
243
      sdbCancelFetch(pSdb, pIter);
12,993✔
244
      *pStream = p;
12,993✔
245
      return TSDB_CODE_SUCCESS;
12,993✔
246
    }
247
    sdbRelease(pSdb, p);
9,100✔
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,501✔
268
  *hasEpset = false;
18,501✔
269

270
  pEpSet->numOfEps = 0;
18,501✔
271
  if (nodeId == SNODE_HANDLE) {
18,501✔
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,214✔
293
    if (pVgObj != NULL) {
18,214!
294
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
18,214✔
295
      mndReleaseVgroup(pMnode, pVgObj);
18,214✔
296

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

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

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

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

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

335
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
70,568✔
336
  int32_t num = 0;
70,568✔
337
  for(int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
211,910✔
338
    SArray* pLevel = taosArrayGetP(pStream->tasks, i);
141,344✔
339
    num += taosArrayGetSize(pLevel);
141,355✔
340
  }
341

342
  return num;
70,566✔
343
}
344

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

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

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

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

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

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

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

382
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
1,999✔
383

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

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

400
  execInfo.role = NODE_ROLE_UNINIT;
1,999✔
401
  execInfo.switchFromFollower = false;
1,999✔
402

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

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

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

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

424
    for (int32_t j = 0; j < size; ++j) {
103,886✔
425
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
102,846✔
426
      if (pEntry == NULL) {
102,846!
427
        continue;
×
428
      }
429

430
      if (pEntry->nodeId == p->nodeId) {
102,846✔
431
        p->hbTimestamp = pEntry->hbTimestamp;
6,666✔
432

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

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

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

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

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

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

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

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

477
  return TSDB_CODE_SUCCESS;
7,123✔
478
}
479

480
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo* pExecInfo) {
2,900✔
481
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
2,900!
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,900✔
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,585✔
507
    SStreamTask *pTask = NULL;
7,253✔
508
    code = streamTaskIterGetCurrent(pIter, &pTask);
7,253✔
509
    if (code) {
7,253!
510
      continue;
×
511
    }
512

513
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
7,253✔
514
    code = doRemoveTasks(pExecNode, &id);
7,253✔
515
    if (code) {
7,253!
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));
341!
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,474✔
543
  size_t num = taosArrayGetSize(pList);
12,474✔
544

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

551
    if (pEntry->nodeId == nodeId) {
109,170✔
552
      return true;
12,474✔
553
    }
554
  }
555

556
  return false;
×
557
}
558

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

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

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

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

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

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

595
  removeExpiredNodeInfo(pNodeSnapshot);
1,257✔
596

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

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

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

613
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
87,402✔
614
    SChkptReportInfo* px = (SChkptReportInfo *)pIter;
63,233✔
615
    if (taosArrayGetSize(px->pTaskList) == 0) {
63,233✔
616
      continue;
62,096✔
617
    }
618

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

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

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

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

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

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

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

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

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

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

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

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

729
  code = mndTransPrepare(pMnode, pTrans);
107✔
730
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
107!
731
    mError("trans:%d, failed to prepare set consensus-chkptId trans since %s", pTrans->id, terrstr());
×
732
    sdbRelease(pMnode->pSdb, pStream);
×
733
    mndTransDrop(pTrans);
×
734
    return code;
×
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) {
127✔
744
  *pInfo = NULL;
127✔
745

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

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

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

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

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

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

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

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

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

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

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

825
  return code;
23✔
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;
311✔
833
  }
834

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1274
  // checkpoint time
1275
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,071✔
1276
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,046✔
1277

1278
  if (pe->checkpointInfo.latestTime != 0) {
200,029✔
1279
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
174,557✔
1280
  } else {
1281
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
25,472✔
1282
  }
1283
  TSDB_CHECK_CODE(code, lino, _end);
200,024!
1284

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

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

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

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

1299
  // checkpoint size
1300
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
200,070✔
1301
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
200,022✔
1302

1303
  colDataSetNULL(pColInfo, numOfRows);
200,010!
1304

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

1309
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
200,014✔
1310
  TSDB_CHECK_CODE(code, lino, _end);
200,147!
1311

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

1316
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
200,097✔
1317
  TSDB_CHECK_CODE(code, lino, _end);
200,069!
1318

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

1323
  if (pe->hTaskId != 0) {
200,016✔
1324
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
216✔
1325
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
216✔
1326
  } else {
1327
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
199,800✔
1328
  }
1329
  TSDB_CHECK_CODE(code, lino, _end);
200,022!
1330

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

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

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

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

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

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

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

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

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

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

1391
    int32_t num = taosArrayGetSize(pNodeList);
12,597✔
1392
    for (int32_t j = 0; j < num; ++j) {
169,543✔
1393
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
168,861✔
1394
      if(pCurrent == NULL) {
168,861!
1395
        continue;
×
1396
      }
1397

1398
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
168,861✔
1399
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
11,915!
1400
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
4✔
1401

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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