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

taosdata / TDengine / #3541

26 Nov 2024 03:56AM UTC coverage: 60.776% (-0.07%) from 60.846%
#3541

push

travis-ci

web-flow
Merge pull request #28920 from taosdata/fix/TD-33008-3.0

fix(query)[TD-33008]. fix error handling in tsdbCacheRead

120076 of 252763 branches covered (47.51%)

Branch coverage included in aggregate %.

0 of 2 new or added lines in 1 file covered. (0.0%)

1395 existing lines in 154 files now uncovered.

200995 of 275526 relevant lines covered (72.95%)

19612328.37 hits per line

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

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

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

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

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

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

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

46
  return 0;
66,013✔
47
}
48

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

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

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

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

92
static bool checkStatusForEachReplica(SVgObj *pVgroup) {
243,501✔
93
  for (int32_t i = 0; i < pVgroup->replica; ++i) {
492,330✔
94
    if (!pVgroup->vnodeGid[i].syncRestore) {
250,082✔
95
      mInfo("vgId:%d not restored, not ready for checkpoint or other operations", pVgroup->vgId);
1,245!
96
      return false;
1,245✔
97
    }
98

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

108
  return true;
242,248✔
109
}
110

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

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

135
  while (1) {
246,392✔
136
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
261,338✔
137
    if (pIter == NULL) {
261,338✔
138
      break;
14,946✔
139
    }
140

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

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

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

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

184
    sdbRelease(pSdb, pVgroup);
246,392✔
185
  }
186

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

430
      if (pEntry->nodeId == p->nodeId) {
98,853✔
431
        p->hbTimestamp = pEntry->hbTimestamp;
6,911✔
432

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

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

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

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

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

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

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

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

477
  return TSDB_CODE_SUCCESS;
7,099✔
478
}
479

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

487
    int32_t code = doRemoveTasks(pExecInfo, pId);
4✔
488
    if (code) {
4!
489
      mError("failed to remove task in buffer list, 0x%"PRIx64, pId->taskId);
×
490
    }
491
  }
492
}
2,919✔
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,557✔
507
    SStreamTask *pTask = NULL;
7,225✔
508
    code = streamTaskIterGetCurrent(pIter, &pTask);
7,225✔
509
    if (code) {
7,225!
510
      continue;
×
511
    }
512

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

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

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

556
  return false;
4✔
557
}
558

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

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

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

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

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

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

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

595
  removeExpiredNodeInfo(pNodeSnapshot);
1,296✔
596

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

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

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

613
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
94,798✔
614
    SChkptReportInfo* px = (SChkptReportInfo *)pIter;
68,942✔
615
    if (taosArrayGetSize(px->pTaskList) == 0) {
68,942✔
616
      continue;
67,747✔
617
    }
618

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

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

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

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

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

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

685
  taosArrayDestroy(pDropped);
27,021✔
686
  return TSDB_CODE_SUCCESS;
27,021✔
687
}
688

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

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

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

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

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

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

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

740
  return TSDB_CODE_ACTION_IN_PROGRESS;
91✔
741
}
742

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

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

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

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

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

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

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

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

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

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

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

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

825
  return code;
20✔
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);
701✔
838
  } else {
839
    mError("failed to remove stream:0x%"PRIx64" in chkpt-report list, remain:%d", streamId, numOfStreams);
320!
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,073✔
858
  int8_t status = atomic_load_8(&pStream->status);
33,073✔
859
  if (status == STREAM_STATUS__NORMAL) {
33,093!
860
    strcpy(dst, "ready");
33,100✔
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,093✔
871

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1066
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,651✔
1067
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,452!
1068

1069
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
199,452✔
1070
  TSDB_CHECK_CODE(code, lino, _end);
199,362!
1071

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

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

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

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

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

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

1103
  // level
1104
  char level[20 + VARSTR_HEADER_SIZE] = {0};
199,307✔
1105
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
199,307✔
1106
    memcpy(varDataVal(level), "source", 6);
101,615✔
1107
    varDataSetLen(level, 6);
101,615✔
1108
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
97,692✔
1109
    memcpy(varDataVal(level), "agg", 3);
18,759✔
1110
    varDataSetLen(level, 3);
18,759✔
1111
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
78,933!
1112
    memcpy(varDataVal(level), "sink", 4);
79,180✔
1113
    varDataSetLen(level, 4);
79,180✔
1114
  }
1115

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

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

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

1125
  const char *pStatus = streamTaskGetStatusStr(pe->status);
199,383✔
1126
  STR_TO_VARSTR(status, pStatus);
199,363✔
1127

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

1132
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
199,411✔
1133
  TSDB_CHECK_CODE(code, lino, _end);
199,375!
1134

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

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

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

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

1152
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
199,575✔
1153
  TSDB_CHECK_CODE(code, lino, _end);
199,409!
1154

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

1164
  memset(vbuf, 0, tListLen(vbuf));
199,409✔
1165
  STR_TO_VARSTR(vbuf, buf);
199,409✔
1166

1167
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,409✔
1168
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,609!
1169

1170
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
199,609✔
1171
  TSDB_CHECK_CODE(code, lino, _end);
199,420!
1172

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

1182
  memset(vbuf, 0, tListLen(vbuf));
199,420✔
1183
  STR_TO_VARSTR(vbuf, buf);
199,420✔
1184

1185
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,420✔
1186
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,621!
1187

1188
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
199,621✔
1189
  TSDB_CHECK_CODE(code, lino, _end);
199,437!
1190

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

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

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

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

1210
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
199,574✔
1211
    colDataSetNULL(pColInfo, numOfRows);
79,203!
1212
  } else {
1213
    if (pe->outputThroughput < 1024) {
120,371✔
1214
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
120,327✔
1215
    } else {
1216
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
44✔
1217
    }
1218

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

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

1245
  STR_TO_VARSTR(vbuf, buf);
199,595✔
1246

1247
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
199,595✔
1248
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
199,583!
1249

1250
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
199,583✔
1251
  TSDB_CHECK_CODE(code, lino, _end);
199,415!
1252

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

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

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

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

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

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

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

1278
  if (pe->checkpointInfo.latestTime != 0) {
199,158✔
1279
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
174,337✔
1280
  } else {
1281
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
24,821✔
1282
  }
1283
  TSDB_CHECK_CODE(code, lino, _end);
199,159!
1284

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1398
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
162,782✔
1399
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
12,290!
1400
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
2✔
1401

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

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

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

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

1428
        break;
12,290✔
1429
      }
1430
    }
1431
  }
1432

1433
  return code;
2,483✔
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");
753✔
1450
    execInfo.ts = taosGetTimestampSec();
753✔
1451
    return false;
753✔
1452
  }
1453

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

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

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

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

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

1486
  mndDestroyVgroupChangeInfo(&changeInfo);
1,187✔
1487
  return nodeUpdated;
1,187✔
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,599✔
1503
  SSdb      *pSdb = pMnode->pSdb;
1,599✔
1504
  void      *pIter = NULL;
1,599✔
1505
  SSnodeObj *pObj = NULL;
1,599✔
1506

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