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

taosdata / TDengine / #3796

31 Mar 2025 10:39AM UTC coverage: 30.372% (-7.1%) from 37.443%
#3796

push

travis-ci

happyguoxy
test:add test cases

69287 of 309062 branches covered (22.42%)

Branch coverage included in aggregate %.

118044 of 307720 relevant lines covered (38.36%)

278592.15 hits per line

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

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

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

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

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

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

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

46
  return 0;
1✔
47
}
48

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

55
  if (pIter->level == -1) {
2✔
56
    pIter->level += 1;
1✔
57
  }
58

59
  while (pIter->level < pIter->totalLevel) {
3✔
60
    SArray *pList = taosArrayGetP(pIter->pStream->pTaskList, pIter->level);
2✔
61
    if (pIter->ordinalIndex >= taosArrayGetSize(pList)) {
2✔
62
      pIter->level += 1;
1✔
63
      pIter->ordinalIndex = 0;
1✔
64
      pIter->pTask = NULL;
1✔
65
      continue;
1✔
66
    }
67

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

73
  pIter->pTask = NULL;
1✔
74
  return false;
1✔
75
}
76

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
1!
89

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

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

106
  return true;
1✔
107
}
108

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

114
  while (1) {
×
115
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
2✔
116
    if (pIter == NULL) {
2!
117
      break;
2✔
118
    }
119

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

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

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

146
    sdbRelease(pMnode->pSdb, pObj);
×
147
  }
148

149
  return code;
2✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
2✔
182
}
183

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

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

197
  while (1) {
4✔
198
    pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
6✔
199
    if (pIter == NULL) {
6✔
200
      break;
2✔
201
    }
202

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
4✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
4✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
2✔
209
      if (code) {
2!
210
        mError("failed to put info into hashmap during task vgroup snapshot, code:%s", tstrerror(code));
×
211
        sdbRelease(pSdb, pVgroup);
×
212
        sdbCancelFetch(pSdb, pIter);
×
213
        goto _end;  // take snapshot failed, and not all ready
×
214
      }
215
    } else {
216
      if (*pReplica != pVgroup->replica) {
2!
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
×
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
219
        *allReady = false;  // task snap success, but not all ready
×
220
      }
221
    }
222

223
    // if not all ready till now, no need to check the remaining vgroups,
224
    // but still we need to put the info of the existed vgroups into the snapshot list
225
    if (*allReady) {
4✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
3✔
227
    }
228

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

235
    void *p = taosArrayPush(pVgroupList, &entry);
4✔
236
    if (p == NULL) {
4!
237
      mError("failed to put entry in vgroup list, nodeId:%d code:out of memory", entry.nodeId);
×
238
      code = terrno;
×
239
      sdbRelease(pSdb, pVgroup);
×
240
      sdbCancelFetch(pSdb, pIter);
×
241
      goto _end;
×
242
    } else {
243
      mDebug("take node snapshot, nodeId:%d %s", entry.nodeId, buf);
4!
244
    }
245

246
    if (pTermMap != NULL) {
4!
247
      int64_t term = pVgroup->vnodeGid[0].syncTerm;
4✔
248
      code = taosHashPut(pTermMap, &pVgroup->vgId, sizeof(pVgroup->vgId), &term, sizeof(term));
4✔
249
      if (code) {
4!
250
        mError("failed to put vnode:%d term into hashMap, code:%s", pVgroup->vgId, tstrerror(code));
×
251
      }
252
    }
253

254
    sdbRelease(pSdb, pVgroup);
4✔
255
  }
256

257
_end:
2✔
258
  taosHashCleanup(pHash);
2✔
259
  return code;
2✔
260
}
261

262
int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList, SHashObj* pTermMap) {
2✔
263
  int32_t   code = 0;
2✔
264
  SArray   *pVgroupList = NULL;
2✔
265

266
  *pList = NULL;
2✔
267
  *allReady = true;
2✔
268

269
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
2✔
270
  if (pVgroupList == NULL) {
2!
271
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
272
    code = terrno;
×
273
    goto _err;
×
274
  }
275

276
  // 1. check for all vnodes status
277
  code = mndCheckAndAddVgroupsInfo(pMnode, pVgroupList, allReady, pTermMap);
2✔
278
  if (code) {
2!
279
    goto _err;
×
280
  }
281

282
  // 2. add snode info
283
  code = mndAddSnodeInfo(pMnode, pVgroupList);
2✔
284
  if (code) {
2!
285
    goto _err;
×
286
  }
287

288
  // 3. check for mnode status
289
  code = mndCheckMnodeStatus(pMnode);
2✔
290
  if (code != TSDB_CODE_SUCCESS) {
2!
291
    *allReady = false;
×
292
  }
293

294
  *pList = pVgroupList;
2✔
295
  return code;
2✔
296

297
_err:
×
298
  *allReady = false;
×
299
  taosArrayDestroy(pVgroupList);
×
300
  return code;
×
301
}
302

303
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
1✔
304
  void *pIter = NULL;
1✔
305
  SSdb *pSdb = pMnode->pSdb;
1✔
306
  *pStream = NULL;
1✔
307

308
  SStreamObj *p = NULL;
1✔
309
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
1!
310
    if (p->uid == streamId) {
×
311
      sdbCancelFetch(pSdb, pIter);
×
312
      *pStream = p;
×
313
      return TSDB_CODE_SUCCESS;
×
314
    }
315
    sdbRelease(pSdb, p);
×
316
  }
317

318
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
1✔
319
}
320

321
void mndKillTransImpl(SMnode *pMnode, int32_t transId, const char *pDbName) {
×
322
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
×
323
  if (pTrans != NULL) {
×
324
    mInfo("kill active transId:%d in Db:%s", transId, pDbName);
×
325
    int32_t code = mndKillTrans(pMnode, pTrans);
×
326
    mndReleaseTrans(pMnode, pTrans);
×
327
    if (code) {
×
328
      mError("failed to kill transId:%d, code:%s", pTrans->id, tstrerror(code));
×
329
    }
330
  } else {
331
    mError("failed to acquire trans in Db:%s, transId:%d", pDbName, transId);
×
332
  }
333
}
×
334

335
int32_t extractNodeEpset(SMnode *pMnode, SEpSet *pEpSet, bool *hasEpset, int32_t taskId, int32_t nodeId) {
1✔
336
  *hasEpset = false;
1✔
337

338
  pEpSet->numOfEps = 0;
1✔
339
  if (nodeId == SNODE_HANDLE) {
1!
340
    SSnodeObj *pObj = NULL;
×
341
    void      *pIter = NULL;
×
342

343
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
344
    if (pIter != NULL) {
×
345
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
×
346
      sdbRelease(pMnode->pSdb, pObj);
×
347
      sdbCancelFetch(pMnode->pSdb, pIter);
×
348
      if (code) {
×
349
        *hasEpset = false;
×
350
        mError("failed to set epset");
×
351
      } else {
352
        *hasEpset = true;
×
353
      }
354
      return code;
×
355
    } else {
356
      mError("failed to acquire snode epset");
×
357
      return TSDB_CODE_INVALID_PARA;
×
358
    }
359
  } else {
360
    SVgObj *pVgObj = mndAcquireVgroup(pMnode, nodeId);
1✔
361
    if (pVgObj != NULL) {
1!
362
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
×
363
      mndReleaseVgroup(pMnode, pVgObj);
×
364

365
      epsetAssign(pEpSet, &epset);
×
366
      *hasEpset = true;
×
367
      return TSDB_CODE_SUCCESS;
×
368
    } else {
369
      mDebug("orphaned task:0x%x need to be dropped, nodeId:%d, no redo action", taskId, nodeId);
1!
370
      return TSDB_CODE_SUCCESS;
1✔
371
    }
372
  }
373
}
374

375
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
×
376
  *pTask = NULL;
×
377

378
  SStreamTask     *p = NULL;
×
379
  SStreamTaskIter *pIter = NULL;
×
380
  int32_t          code = createStreamTaskIter(pStream, &pIter);
×
381
  if (code) {
×
382
    mError("failed to create stream task iter:%s", pStream->name);
×
383
    return code;
×
384
  }
385

386
  while (streamTaskIterNextTask(pIter)) {
×
387
    code = streamTaskIterGetCurrent(pIter, &p);
×
388
    if (code) {
×
389
      continue;
×
390
    }
391

392
    if (p->id.taskId == pId->taskId) {
×
393
      destroyStreamTaskIter(pIter);
×
394
      *pTask = p;
×
395
      return 0;
×
396
    }
397
  }
398

399
  destroyStreamTaskIter(pIter);
×
400
  return TSDB_CODE_FAILED;
×
401
}
402

403
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
×
404
  int32_t num = 0;
×
405
  for (int32_t i = 0; i < taosArrayGetSize(pStream->pTaskList); ++i) {
×
406
    SArray *pLevel = taosArrayGetP(pStream->pTaskList, i);
×
407
    num += taosArrayGetSize(pLevel);
×
408
  }
409

410
  return num;
×
411
}
412

413
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
×
414
  SSdb   *pSdb = pMnode->pSdb;
×
415
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
×
416
  if (pDb == NULL) {
×
417
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
418
  }
419

420
  int32_t numOfStreams = 0;
×
421
  void   *pIter = NULL;
×
422
  while (1) {
×
423
    SStreamObj *pStream = NULL;
×
424
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
425
    if (pIter == NULL) break;
×
426

427
    if (pStream->sourceDbUid == pDb->uid) {
×
428
      numOfStreams++;
×
429
    }
430

431
    sdbRelease(pSdb, pStream);
×
432
  }
433

434
  *pNumOfStreams = numOfStreams;
×
435
  mndReleaseDb(pMnode, pDb);
×
436
  return 0;
×
437
}
438

439
static void freeTaskList(void *param) {
×
440
  SArray **pList = (SArray **)param;
×
441
  taosArrayDestroy(*pList);
×
442
}
×
443

444
int32_t mndInitExecInfo() {
10✔
445
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
10✔
446
  if (code) {
10!
447
    return code;
×
448
  }
449

450
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
10✔
451

452
  execInfo.pTaskList = taosArrayInit(4, sizeof(STaskId));
10✔
453
  execInfo.pTaskMap = taosHashInit(64, fn, true, HASH_NO_LOCK);
10✔
454
  execInfo.transMgmt.pDBTrans = taosHashInit(32, fn, true, HASH_NO_LOCK);
10✔
455
  execInfo.pTransferStateStreams = taosHashInit(32, fn, true, HASH_NO_LOCK);
10✔
456
  execInfo.pChkptStreams = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
10✔
457
  execInfo.pStreamConsensus = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
10✔
458
  execInfo.pNodeList = taosArrayInit(4, sizeof(SNodeEntry));
10✔
459
  execInfo.pKilledChkptTrans = taosArrayInit(4, sizeof(SStreamTaskResetMsg));
10✔
460

461
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
10!
462
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
10!
463
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
10!
464
    mError("failed to initialize the stream runtime env, code:%s", tstrerror(terrno));
×
465
    return terrno;
×
466
  }
467

468
  execInfo.role = NODE_ROLE_UNINIT;
10✔
469
  execInfo.switchFromFollower = false;
10✔
470

471
  taosHashSetFreeFp(execInfo.pTransferStateStreams, freeTaskList);
10✔
472
  taosHashSetFreeFp(execInfo.pChkptStreams, freeTaskList);
10✔
473
  taosHashSetFreeFp(execInfo.pStreamConsensus, freeTaskList);
10✔
474
  return 0;
10✔
475
}
476

477
void removeExpiredNodeInfo(const SArray *pNodeSnapshot) {
×
478
  SArray *pValidList = taosArrayInit(4, sizeof(SNodeEntry));
×
479
  if (pValidList == NULL) {  // not continue
×
480
    return;
×
481
  }
482

483
  int32_t size = taosArrayGetSize(pNodeSnapshot);
×
484
  int32_t oldSize = taosArrayGetSize(execInfo.pNodeList);
×
485

486
  for (int32_t i = 0; i < oldSize; ++i) {
×
487
    SNodeEntry *p = taosArrayGet(execInfo.pNodeList, i);
×
488
    if (p == NULL) {
×
489
      continue;
×
490
    }
491

492
    for (int32_t j = 0; j < size; ++j) {
×
493
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
×
494
      if (pEntry == NULL) {
×
495
        continue;
×
496
      }
497

498
      if (pEntry->nodeId == p->nodeId) {
×
499
        p->hbTimestamp = pEntry->hbTimestamp;
×
500

501
        void *px = taosArrayPush(pValidList, p);
×
502
        if (px == NULL) {
×
503
          mError("failed to put node into list, nodeId:%d", p->nodeId);
×
504
        } else {
505
          mDebug("vgId:%d ts:%" PRId64 " HbMsgId:%d is valid", p->nodeId, p->hbTimestamp, p->lastHbMsgId);
×
506
        }
507
        break;
×
508
      }
509
    }
510
  }
511

512
  taosArrayDestroy(execInfo.pNodeList);
×
513
  execInfo.pNodeList = pValidList;
×
514

515
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
×
516
         (int32_t)taosArrayGetSize(pValidList), oldSize);
517
}
518

519
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) {
×
520
  void *p = taosHashGet(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
×
521
  if (p == NULL) {
×
522
    return TSDB_CODE_SUCCESS;
×
523
  }
524

525
  int32_t code = taosHashRemove(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
×
526
  if (code) {
×
527
    return code;
×
528
  }
529

530
  for (int32_t k = 0; k < taosArrayGetSize(pExecNode->pTaskList); ++k) {
×
531
    STaskId *pId = taosArrayGet(pExecNode->pTaskList, k);
×
532
    if (pId == NULL) {
×
533
      continue;
×
534
    }
535

536
    if (pId->taskId == pRemovedId->taskId && pId->streamId == pRemovedId->streamId) {
×
537
      taosArrayRemove(pExecNode->pTaskList, k);
×
538

539
      int32_t num = taosArrayGetSize(pExecNode->pTaskList);
×
540
      mInfo("s-task:0x%x removed from buffer, remain:%d in buffer list", (int32_t)pRemovedId->taskId, num);
×
541
      break;
×
542
    }
543
  }
544

545
  return TSDB_CODE_SUCCESS;
×
546
}
547

548
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
×
549
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
×
550
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
551
    if (pId == NULL) {
×
552
      continue;
×
553
    }
554

555
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
556
    if (code) {
×
557
      mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId);
×
558
    }
559
  }
560
}
×
561

562
void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
×
563
  SStreamTaskIter *pIter = NULL;
×
564
  streamMutexLock(&pExecNode->lock);
×
565

566
  // 1. remove task entries
567
  int32_t code = createStreamTaskIter(pStream, &pIter);
×
568
  if (code) {
×
569
    streamMutexUnlock(&pExecNode->lock);
×
570
    mError("failed to create stream task iter:%s", pStream->name);
×
571
    return;
×
572
  }
573

574
  while (streamTaskIterNextTask(pIter)) {
×
575
    SStreamTask *pTask = NULL;
×
576
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
577
    if (code) {
×
578
      continue;
×
579
    }
580

581
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
582
    code = doRemoveTasks(pExecNode, &id);
×
583
    if (code) {
×
584
      mError("failed to remove task in buffer list, 0x%" PRIx64, id.taskId);
×
585
    }
586
  }
587

588
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
×
589
    streamMutexUnlock(&pExecNode->lock);
×
590
    destroyStreamTaskIter(pIter);
×
591
    mError("task map size, task list size, not equal");
×
592
    return;
×
593
  }
594

595
  // 2. remove stream entry in consensus hash table and checkpoint-report hash table
596
  code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, pStream->uid);
×
597
  if (code) {
×
598
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
599
  }
600

601
  code = mndClearChkptReportInfo(execInfo.pChkptStreams, pStream->uid);
×
602
  if (code) {
×
603
    mError("failed to clear the checkpoint report info, code:%s", tstrerror(code));
×
604
  }
605

606
  streamMutexUnlock(&pExecNode->lock);
×
607
  destroyStreamTaskIter(pIter);
×
608
}
609

610
static bool taskNodeExists(SArray *pList, int32_t nodeId) {
×
611
  size_t num = taosArrayGetSize(pList);
×
612

613
  for (int32_t i = 0; i < num; ++i) {
×
614
    SNodeEntry *pEntry = taosArrayGet(pList, i);
×
615
    if (pEntry == NULL) {
×
616
      continue;
×
617
    }
618

619
    if (pEntry->nodeId == nodeId) {
×
620
      return true;
×
621
    }
622
  }
623

624
  return false;
×
625
}
626

627
int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) {
×
628
  SArray *pRemovedTasks = taosArrayInit(4, sizeof(STaskId));
×
629
  if (pRemovedTasks == NULL) {
×
630
    return terrno;
×
631
  }
632

633
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
×
634
  for (int32_t i = 0; i < numOfTask; ++i) {
×
635
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
×
636
    if (pId == NULL) {
×
637
      continue;
×
638
    }
639

640
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, pId, sizeof(*pId));
×
641
    if (pEntry == NULL) {
×
642
      continue;
×
643
    }
644

645
    if (pEntry->nodeId == SNODE_HANDLE) {
×
646
      continue;
×
647
    }
648

649
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
×
650
    if (!existed) {
×
651
      void *p = taosArrayPush(pRemovedTasks, pId);
×
652
      if (p == NULL) {
×
653
        mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId);
×
654
      }
655
    }
656
  }
657

658
  removeTasksInBuf(pRemovedTasks, &execInfo);
×
659

660
  mDebug("remove invalid stream tasks:%d, remain:%d", (int32_t)taosArrayGetSize(pRemovedTasks),
×
661
         (int32_t)taosArrayGetSize(execInfo.pTaskList));
662

663
  removeExpiredNodeInfo(pNodeSnapshot);
×
664

665
  taosArrayDestroy(pRemovedTasks);
×
666
  return 0;
×
667
}
668

669
static int32_t allTasksSendChkptReport(SChkptReportInfo* pReportInfo, int32_t numOfTasks, const char* pName) {
×
670
  int64_t checkpointId = -1;
×
671
  int32_t transId = -1;
×
672
  int32_t taskId = -1;
×
673

674
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
×
675
  if (existed != numOfTasks) {
×
676
    mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pReportInfo->streamId, pName,
×
677
           existed, numOfTasks, numOfTasks - existed);
678
    return -1;
×
679
  }
680

681
  // acquire current active checkpointId, and do cross-check checkpointId info in exec.pTaskList
682
  for(int32_t i = 0; i < numOfTasks; ++i) {
×
683
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
×
684
    if (pInfo == NULL) {
×
685
      continue;
×
686
    }
687

688
    if (checkpointId == -1) {
×
689
      checkpointId = pInfo->checkpointId;
×
690
      transId = pInfo->transId;
×
691
      taskId = pInfo->taskId;
×
692
    } else if (checkpointId != pInfo->checkpointId) {
×
693
      mError("stream:0x%" PRIx64
×
694
             " checkpointId in checkpoint-report list are not identical, type 1 taskId:0x%x checkpointId:%" PRId64
695
             ", type 2 taskId:0x%x checkpointId:%" PRId64,
696
             pReportInfo->streamId, taskId, checkpointId, pInfo->taskId, pInfo->checkpointId);
697
      return -1;
×
698
    }
699
  }
700

701
  // check for the correct checkpointId for current task info in STaskChkptInfo
702
  STaskChkptInfo  *p = taosArrayGet(pReportInfo->pTaskList, 0);
×
703
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
×
704
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
705

706
  // cross-check failed, there must be something unknown wrong
707
  SStreamTransInfo *pTransInfo = taosHashGet(execInfo.transMgmt.pDBTrans, &id.streamId, sizeof(id.streamId));
×
708
  if (pTransInfo == NULL) {
×
709
    mWarn("stream:0x%" PRIx64 " no active trans exists for checkpoint transId:%d, it may have been cleared already",
×
710
           id.streamId, transId);
711

712
    if (pe->checkpointInfo.activeId != 0 && pe->checkpointInfo.activeId != checkpointId) {
×
713
      mWarn("stream:0x%" PRIx64 " active checkpointId is not equalled to the required, current:%" PRId64
×
714
            ", req:%" PRId64 " recheck next time",
715
            id.streamId, pe->checkpointInfo.activeId, checkpointId);
716
      return -1;
×
717
    } else {
718
      //  do nothing
719
    }
720
  } else {
721
    if (pTransInfo->transId != transId) {
×
722
      mError("stream:0x%" PRIx64
×
723
             " checkpoint-report list info are expired, active transId:%d trans in list:%d, recheck next time",
724
             id.streamId, pTransInfo->transId, transId);
725
      return -1;
×
726
    }
727
  }
728

729
  mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info", id.streamId,
×
730
         pName, numOfTasks);
731

732
  return TSDB_CODE_SUCCESS;
×
733
}
734

735
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
×
736
  SMnode *pMnode = pReq->info.node;
×
737
  void   *pIter = NULL;
×
738
  int32_t code = 0;
×
739
  int32_t lino = 0;
×
740
  SArray *pDropped = NULL;
×
741

742
  mDebug("start to scan checkpoint report info");
×
743

744
  streamMutexLock(&execInfo.lock);
×
745

746
  int32_t num = taosHashGetSize(execInfo.pChkptStreams);
×
747
  if (num == 0) {
×
748
    goto _end;
×
749
  }
750

751
  pDropped = taosArrayInit(4, sizeof(int64_t));
×
752
  TSDB_CHECK_NULL(pDropped, code, lino, _end, terrno);
×
753

754
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
×
755
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
×
756
    if (taosArrayGetSize(px->pTaskList) == 0) {
×
757
      continue;
×
758
    }
759

760
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
×
761
    if (pInfo == NULL) {
×
762
      continue;
×
763
    }
764

765
    SStreamObj *pStream = NULL;
×
766
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
×
767
    if (pStream == NULL || code != 0) {
×
768
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
769
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
770
      if (p == NULL) {
×
771
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
772
      }
773
      continue;
×
774
    }
775

776
    int32_t total = mndGetNumOfStreamTasks(pStream);
×
777
    int32_t ret = allTasksSendChkptReport(px, total, pStream->name);
×
778
    if (ret == 0) {
×
779
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
×
780
      if (code == 0) {
×
781
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
×
782
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
×
783
          taosArrayClear(px->pTaskList);
×
784
          mInfo("stream:0x%" PRIx64 " clear checkpoint-report list and update the report checkpointId from:%" PRId64
×
785
                " to %" PRId64,
786
                pInfo->streamId, px->reportChkpt, pInfo->checkpointId);
787
          px->reportChkpt = pInfo->checkpointId;
×
788
        } else {
789
          mDebug("stream:0x%" PRIx64 " not launch chkpt-info update trans, due to checkpoint not finished yet",
×
790
                 pInfo->streamId);
791
        }
792

793
        sdbRelease(pMnode->pSdb, pStream);
×
794
        break;
×
795
      } else {
796
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
797
      }
798
    }
799

800
    sdbRelease(pMnode->pSdb, pStream);
×
801
  }
802

803
  int32_t size = taosArrayGetSize(pDropped);
×
804
  if (size > 0) {
×
805
    for (int32_t i = 0; i < size; ++i) {
×
806
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
807
      if (pStreamId == NULL) {
×
808
        continue;
×
809
      }
810

811
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
812
      if (code) {
×
813
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
814
      }
815
    }
816

817
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
818
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
819
  }
820

821
_end:
×
822
  streamMutexUnlock(&execInfo.lock);
×
823

824
  if (pDropped != NULL) {
×
825
    taosArrayDestroy(pDropped);
×
826
  }
827

828
  mDebug("end to scan checkpoint report info")
×
829
  return code;
×
830
}
831

832
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId, SArray* pList) {
×
833
  char    msg[128] = {0};
×
834
  STrans *pTrans = NULL;
×
835

836
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for stream:0x%" PRIx64, pStream->uid);
×
837

838
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
×
839
  if (pTrans == NULL || code != 0) {
×
840
    return terrno;
×
841
  }
842

843
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
×
844
  if (code) {
×
845
    sdbRelease(pMnode->pSdb, pStream);
×
846
    return code;
×
847
  }
848

849
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pStream, checkpointId, pList);
×
850
  if (code != 0) {
×
851
    sdbRelease(pMnode->pSdb, pStream);
×
852
    mndTransDrop(pTrans);
×
853
    return code;
×
854
  }
855

856
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
857
  if (code) {
×
858
    sdbRelease(pMnode->pSdb, pStream);
×
859
    mndTransDrop(pTrans);
×
860
    return code;
×
861
  }
862

863
  code = mndTransPrepare(pMnode, pTrans);
×
864

865
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
866
    mError("trans:%d, failed to prepare set consensus-chkptId trans for stream:0x%" PRId64 " since %s", pTrans->id,
×
867
           pStream->uid, tstrerror(code));
868
    sdbRelease(pMnode->pSdb, pStream);
×
869
    mndTransDrop(pTrans);
×
870
    return code;
×
871
  }
872

873
  sdbRelease(pMnode->pSdb, pStream);
×
874
  mndTransDrop(pTrans);
×
875

876
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
877
}
878

879
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
×
880
  *pInfo = NULL;
×
881

882
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
×
883
  if (px != NULL) {
×
884
    *pInfo = px;
×
885
    return 0;
×
886
  }
887

888
  SCheckpointConsensusInfo p = {
×
889
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
×
890
      .numOfTasks = numOfTasks,
891
      .streamId = streamId,
892
  };
893

894
  if (p.pTaskList == NULL) {
×
895
    return terrno;
×
896
  }
897

898
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
×
899
  if (code == 0) {
×
900
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
×
901
    *pInfo = pChkptInfo;
×
902
  } else {
903
    *pInfo = NULL;
×
904
  }
905

906
  return code;
×
907
}
908

909
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
910
// discard the msg may lead to the lost of connections.
911
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
×
912
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
×
913
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
×
914

915
  int32_t num = (int32_t) taosArrayGetSize(pInfo->pTaskList);
×
916
  for (int32_t i = 0; i < num; ++i) {
×
917
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
×
918
    if (p == NULL) {
×
919
      continue;
×
920
    }
921

922
    if (p->req.taskId == info.req.taskId) {
×
923
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update send reqTs %" PRId64
×
924
             "->%" PRId64 " checkpointId:%" PRId64 " -> %" PRId64 " term:%d->%d total existed:%d",
925
             pRestoreInfo->taskId, pRestoreInfo->streamId, p->req.startTs, info.req.startTs, p->req.checkpointId,
926
             info.req.checkpointId, p->req.term, info.req.term, num);
927
      p->req.startTs = info.req.startTs;
×
928
      p->req.checkpointId = info.req.checkpointId;
×
929
      p->req.transId = info.req.transId;
×
930
      p->req.nodeId = info.req.nodeId;
×
931
      p->req.term = info.req.term;
×
932
      return;
×
933
    }
934
  }
935

936
  void *p = taosArrayPush(pInfo->pTaskList, &info);
×
937
  if (p == NULL) {
×
938
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
939
  } else {
940
    num = taosArrayGetSize(pInfo->pTaskList);
×
941
    mDebug("s-task:0x%x (vgId:%d) checkpointId:%" PRId64 " term:%d, reqTs:%" PRId64
×
942
           " added into consensus-checkpointId list, stream:0x%" PRIx64 " waiting tasks:%d",
943
           pRestoreInfo->taskId, pRestoreInfo->nodeId, pRestoreInfo->checkpointId, info.req.term,
944
           info.req.startTs, pRestoreInfo->streamId, num);
945
  }
946
}
947

948
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
×
949
  taosArrayDestroy(pInfo->pTaskList);
×
950
  pInfo->pTaskList = NULL;
×
951
}
×
952

953
int32_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
×
954
  int32_t code = 0;
×
955
  int32_t numOfStreams = taosHashGetSize(pHash);
×
956
  if (numOfStreams == 0) {
×
957
    return code;
×
958
  }
959

960
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
×
961
  if (code == 0) {
×
962
    numOfStreams = taosHashGetSize(pHash);
×
963
    mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
964
  } else {
965
    mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
966
  }
967

968
  return code;
×
969
}
970

971
int32_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
972
  int32_t code = 0;
×
973
  int32_t numOfStreams = taosHashGetSize(pHash);
×
974
  if (numOfStreams == 0) {
×
975
    return code;
×
976
  }
977

978
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
×
979
  if (code == 0) {
×
980
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
981
  } else {
982
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
983
  }
984

985
  return code;
×
986
}
987

988
int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
989
  SChkptReportInfo *pInfo = taosHashGet(pHash, &streamId, sizeof(streamId));
×
990
  if (pInfo != NULL) {
×
991
    taosArrayClear(pInfo->pTaskList);
×
992
    mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId,
×
993
           pInfo->reportChkpt);
994
    return 0;
×
995
  }
996

997
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
998
}
999

1000
static void mndShowStreamStatus(char *dst, int8_t status) {
×
1001
  if (status == STREAM_STATUS__NORMAL) {
×
1002
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
×
1003
  } else if (status == STREAM_STATUS__STOP) {
×
1004
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
1005
  } else if (status == STREAM_STATUS__FAILED) {
×
1006
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
1007
  } else if (status == STREAM_STATUS__RECOVER) {
×
1008
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
1009
  } else if (status == STREAM_STATUS__PAUSE) {
×
1010
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
×
1011
  } else if (status == STREAM_STATUS__INIT) {
×
1012
    tstrncpy(dst, "init", MND_STREAM_TRIGGER_NAME_SIZE);
×
1013
  }
1014
}
×
1015

1016
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
×
1017
  int8_t trigger = pStream->conf.trigger;
×
1018
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
×
1019
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
×
1020
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
×
1021
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
×
1022
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
×
1023
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
×
1024
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
1025
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
×
1026
  }
1027
}
×
1028

1029
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
×
1030
  memset(pBuf, 0, bufLen);
×
1031
  pBuf[2] = '0';
×
1032
  pBuf[3] = 'x';
×
1033

1034
  int32_t len = tintToHex(id, &pBuf[4]);
×
1035
  varDataSetLen(pBuf, len + 2);
×
1036
}
×
1037

1038
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
×
1039
  int32_t          code = TSDB_CODE_SUCCESS;
×
1040
  int32_t          lino = 0;
×
1041
  SStreamTaskIter *pIter = NULL;
×
1042
  bool             isPaused =  true;
×
1043

1044
  taosRLockLatch(&pStream->lock);
×
1045
  code = createStreamTaskIter(pStream, &pIter);
×
1046
  TSDB_CHECK_CODE(code, lino, _end);
×
1047

1048
  while (streamTaskIterNextTask(pIter)) {
×
1049
    SStreamTask *pTask = NULL;
×
1050
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
1051
    TSDB_CHECK_CODE(code, lino, _end);
×
1052

1053
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
1054
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
1055
    if (pe == NULL) {
×
1056
      continue;
×
1057
    }
1058
    if (pe->status != TASK_STATUS__PAUSE) {
×
1059
      isPaused = false;
×
1060
    }
1061
  }
1062
  (*pRes) = isPaused;
×
1063

1064
_end:
×
1065
  destroyStreamTaskIter(pIter);
×
1066
  taosRUnLockLatch(&pStream->lock);
×
1067
  if (code != TSDB_CODE_SUCCESS) {
×
1068
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
1069
  }
1070
  return code;
×
1071
}
1072

1073
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
×
1074
  int32_t code = 0;
×
1075
  int32_t cols = 0;
×
1076
  int32_t lino = 0;
×
1077

1078
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1079
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
1080
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1081
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1082

1083
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
1084
  TSDB_CHECK_CODE(code, lino, _end);
×
1085

1086
  // create time
1087
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1088
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1089
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
×
1090
  TSDB_CHECK_CODE(code, lino, _end);
×
1091

1092
  // stream id
1093
  char buf[128] = {0};
×
1094
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
×
1095
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1096
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1097
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1098
  TSDB_CHECK_CODE(code, lino, _end);
×
1099

1100
  // related fill-history stream id
1101
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1102
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1103
  if (pStream->hTaskUid != 0) {
×
1104
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
1105
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1106
  } else {
1107
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
×
1108
  }
1109
  TSDB_CHECK_CODE(code, lino, _end);
×
1110

1111
  // related fill-history stream id
1112
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
×
1113
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
×
1114
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1115
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1116
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
×
1117
  TSDB_CHECK_CODE(code, lino, _end);
×
1118

1119
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
1120
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
×
1121
  bool isPaused = false;
×
1122
  code = isAllTaskPaused(pStream, &isPaused);
×
1123
  TSDB_CHECK_CODE(code, lino, _end);
×
1124

1125
  int8_t streamStatus = atomic_load_8(&pStream->status);
×
1126
  if (isPaused && pStream->pTaskList != NULL) {
×
1127
    streamStatus = STREAM_STATUS__PAUSE;
×
1128
  }
1129
  mndShowStreamStatus(status2, streamStatus);
×
1130
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
×
1131
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1132
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1133

1134
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
×
1135
  TSDB_CHECK_CODE(code, lino, _end);
×
1136

1137
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1138
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
×
1139
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1140
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1141

1142
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
×
1143
  TSDB_CHECK_CODE(code, lino, _end);
×
1144

1145
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1146
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
×
1147
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1148
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1149

1150
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
×
1151
  TSDB_CHECK_CODE(code, lino, _end);
×
1152

1153
  if (pStream->targetSTbName[0] == 0) {
×
1154
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1155
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1156

1157
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
×
1158
  } else {
1159
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1160
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
×
1161
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1162
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1163

1164
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
×
1165
  }
1166
  TSDB_CHECK_CODE(code, lino, _end);
×
1167

1168
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1169
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1170

1171
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
×
1172
  TSDB_CHECK_CODE(code, lino, _end);
×
1173

1174
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
×
1175
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
×
1176
  mndShowStreamTrigger(trigger2, pStream);
×
1177
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
×
1178
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1179
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1180

1181
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
×
1182
  TSDB_CHECK_CODE(code, lino, _end);
×
1183

1184
  // sink_quota
1185
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
×
1186
  sinkQuota[0] = '0';
×
1187
  char dstStr[20] = {0};
×
1188
  STR_TO_VARSTR(dstStr, sinkQuota)
×
1189
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1190
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1191

1192
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
×
1193
  TSDB_CHECK_CODE(code, lino, _end);
×
1194

1195
  // checkpoint interval
1196
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
×
1197
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
×
1198
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
×
1199

1200
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1201
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1202

1203
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
×
1204
  TSDB_CHECK_CODE(code, lino, _end);
×
1205

1206
  // checkpoint backup type
1207
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
×
1208
  STR_TO_VARSTR(backup, "none")
×
1209
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1210
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1211

1212
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
×
1213
  TSDB_CHECK_CODE(code, lino, _end);
×
1214

1215
  // history scan idle
1216
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
×
1217
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
×
1218

1219
  memset(dstStr, 0, tListLen(dstStr));
×
1220
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
×
1221
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1222
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1223

1224
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
×
1225
  TSDB_CHECK_CODE(code, lino, _end);
×
1226

1227
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1228
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1229
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
×
1230
  if (streamStatus == STREAM_STATUS__FAILED){
×
1231
    STR_TO_VARSTR(msg, pStream->reserve)
×
1232
  } else {
1233
    STR_TO_VARSTR(msg, " ")
×
1234
  }
1235
  code = colDataSetVal(pColInfo, numOfRows, (const char *)msg, false);
×
1236

1237
_end:
×
1238
  if (code) {
×
1239
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1240
  }
1241
  return code;
×
1242
}
1243

1244
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
×
1245
                              int32_t precision) {
1246
  SColumnInfoData *pColInfo = NULL;
×
1247
  int32_t          cols = 0;
×
1248
  int32_t          code = 0;
×
1249
  int32_t          lino = 0;
×
1250

1251
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
1252

1253
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
1254
  if (pe == NULL) {
×
1255
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1256
           " no valid status/stage info",
1257
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1258
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1259
  }
1260

1261
  // stream name
1262
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1263
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
1264

1265
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1266
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1267

1268
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
1269
  TSDB_CHECK_CODE(code, lino, _end);
×
1270

1271
  // task id
1272
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1273
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1274

1275
  char idstr[128] = {0};
×
1276
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
×
1277
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1278
  TSDB_CHECK_CODE(code, lino, _end);
×
1279

1280
  // node type
1281
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
×
1282
  varDataSetLen(nodeType, 5);
×
1283
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1284
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1285

1286
  if (pTask->info.nodeId > 0) {
×
1287
    memcpy(varDataVal(nodeType), "vnode", 5);
×
1288
  } else {
1289
    memcpy(varDataVal(nodeType), "snode", 5);
×
1290
  }
1291
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
×
1292
  TSDB_CHECK_CODE(code, lino, _end);
×
1293

1294
  // node id
1295
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1296
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1297

1298
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
×
1299
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
×
1300
  TSDB_CHECK_CODE(code, lino, _end);
×
1301

1302
  // level
1303
  char level[20 + VARSTR_HEADER_SIZE] = {0};
×
1304
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
1305
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
×
1306
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
×
1307
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
×
1308
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1309
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
×
1310
  } else if (pTask->info.taskLevel == TASK_LEVEL__MERGE) {
×
1311
    STR_WITH_SIZE_TO_VARSTR(level, "merge", 5);
×
1312
  }
1313

1314
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1315
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1316

1317
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
×
1318
  TSDB_CHECK_CODE(code, lino, _end);
×
1319

1320
  // status
1321
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
1322

1323
  const char *pStatus = streamTaskGetStatusStr(pe->status);
×
1324
  STR_TO_VARSTR(status, pStatus);
×
1325

1326
  // status
1327
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1328
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1329

1330
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
×
1331
  TSDB_CHECK_CODE(code, lino, _end);
×
1332

1333
  // stage
1334
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1335
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1336

1337
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
×
1338
  TSDB_CHECK_CODE(code, lino, _end);
×
1339

1340
  // input queue
1341
  char        vbuf[TSDB_STREAM_NOTIFY_STAT_LEN + 2] = {0};
×
1342
  char        buf[TSDB_STREAM_NOTIFY_STAT_LEN] = {0};
×
1343
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
×
1344
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
×
1345
  STR_TO_VARSTR(vbuf, buf);
×
1346

1347
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1348
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1349

1350
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1351
  TSDB_CHECK_CODE(code, lino, _end);
×
1352

1353
  // input total
1354
  const char *formatTotalMb = "%7.2f MiB";
×
1355
  const char *formatTotalGb = "%7.2f GiB";
×
1356
  if (pe->procsTotal < 1024) {
×
1357
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
×
1358
  } else {
1359
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1360
  }
1361

1362
  memset(vbuf, 0, tListLen(vbuf));
×
1363
  STR_TO_VARSTR(vbuf, buf);
×
1364

1365
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1366
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1367

1368
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1369
  TSDB_CHECK_CODE(code, lino, _end);
×
1370

1371
  // process throughput
1372
  const char *formatKb = "%7.2f KiB/s";
×
1373
  const char *formatMb = "%7.2f MiB/s";
×
1374
  if (pe->procsThroughput < 1024) {
×
1375
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
×
1376
  } else {
1377
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
×
1378
  }
1379

1380
  memset(vbuf, 0, tListLen(vbuf));
×
1381
  STR_TO_VARSTR(vbuf, buf);
×
1382

1383
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1384
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1385

1386
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1387
  TSDB_CHECK_CODE(code, lino, _end);
×
1388

1389
  // output total
1390
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1391
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1392

1393
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1394
    colDataSetNULL(pColInfo, numOfRows);
×
1395
  } else {
1396
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
×
1397
    memset(vbuf, 0, tListLen(vbuf));
×
1398
    STR_TO_VARSTR(vbuf, buf);
×
1399

1400
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1401
    TSDB_CHECK_CODE(code, lino, _end);
×
1402
  }
1403

1404
  // output throughput
1405
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1406
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1407

1408
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1409
    colDataSetNULL(pColInfo, numOfRows);
×
1410
  } else {
1411
    if (pe->outputThroughput < 1024) {
×
1412
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
×
1413
    } else {
1414
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
×
1415
    }
1416

1417
    memset(vbuf, 0, tListLen(vbuf));
×
1418
    STR_TO_VARSTR(vbuf, buf);
×
1419

1420
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1421
    TSDB_CHECK_CODE(code, lino, _end);
×
1422
  }
1423
  // info
1424
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1425
    const char *sinkStr = "%.2f MiB";
×
1426
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
×
1427
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
×
1428
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
1429
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
×
1430
      if (ret != 0) {
×
1431
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1432
        memset(buf, 0, tListLen(buf));
×
1433
      }
1434
    } else {
1435
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
×
1436
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
×
1437
    }
1438
  } else {
1439
    memset(buf, 0, tListLen(buf));
×
1440
  }
1441

1442
  STR_TO_VARSTR(vbuf, buf);
×
1443

1444
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1445
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1446

1447
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1448
  TSDB_CHECK_CODE(code, lino, _end);
×
1449

1450
  // start_time
1451
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1452
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1453

1454
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
×
1455
  TSDB_CHECK_CODE(code, lino, _end);
×
1456

1457
  // start id
1458
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1459
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1460

1461
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
×
1462
  TSDB_CHECK_CODE(code, lino, _end);
×
1463

1464
  // start ver
1465
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1466
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1467

1468
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
×
1469
  TSDB_CHECK_CODE(code, lino, _end);
×
1470

1471
  // checkpoint time
1472
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1473
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1474

1475
  if (pe->checkpointInfo.latestTime != 0) {
×
1476
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
×
1477
  } else {
1478
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1479
  }
1480
  TSDB_CHECK_CODE(code, lino, _end);
×
1481

1482
  // checkpoint_id
1483
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1484
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1485

1486
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
×
1487
  TSDB_CHECK_CODE(code, lino, _end);
×
1488

1489
  // checkpoint version
1490
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1491
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1492

1493
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
×
1494
  TSDB_CHECK_CODE(code, lino, _end);
×
1495

1496
  // checkpoint size
1497
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1498
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1499

1500
  colDataSetNULL(pColInfo, numOfRows);
×
1501

1502
  // checkpoint backup status
1503
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1504
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1505

1506
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1507
  TSDB_CHECK_CODE(code, lino, _end);
×
1508

1509
  // ds_err_info
1510
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1511
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1512

1513
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1514
  TSDB_CHECK_CODE(code, lino, _end);
×
1515

1516
  // history_task_id
1517
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1518
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1519

1520
  if (pe->hTaskId != 0) {
×
1521
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
×
1522
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1523
  } else {
1524
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1525
  }
1526
  TSDB_CHECK_CODE(code, lino, _end);
×
1527

1528
  // history_task_status
1529
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1530
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1531

1532
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1533
  TSDB_CHECK_CODE(code, lino, _end);
×
1534

1535
  // notify_event_stat
1536
  int32_t offset =0;
×
1537
  if (pe->notifyEventStat.notifyEventAddTimes > 0) {
×
1538
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Add %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1539
                        pe->notifyEventStat.notifyEventAddTimes, pe->notifyEventStat.notifyEventAddElems,
1540
                        pe->notifyEventStat.notifyEventAddCostSec);
1541
  }
1542
  if (pe->notifyEventStat.notifyEventPushTimes > 0) {
×
1543
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Push %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1544
                        pe->notifyEventStat.notifyEventPushTimes, pe->notifyEventStat.notifyEventPushElems,
1545
                        pe->notifyEventStat.notifyEventPushCostSec);
1546
  }
1547
  if (pe->notifyEventStat.notifyEventPackTimes > 0) {
×
1548
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Pack %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1549
                        pe->notifyEventStat.notifyEventPackTimes, pe->notifyEventStat.notifyEventPackElems,
1550
                        pe->notifyEventStat.notifyEventPackCostSec);
1551
  }
1552
  if (pe->notifyEventStat.notifyEventSendTimes > 0) {
×
1553
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "Send %" PRId64 "x, %" PRId64 " elems in %lfs; ",
×
1554
                        pe->notifyEventStat.notifyEventSendTimes, pe->notifyEventStat.notifyEventSendElems,
1555
                        pe->notifyEventStat.notifyEventSendCostSec);
1556
  }
1557
  if (pe->notifyEventStat.notifyEventHoldElems > 0) {
×
1558
    offset += tsnprintf(buf + offset, sizeof(buf) - offset, "[Hold %" PRId64 " elems] ",
×
1559
                        pe->notifyEventStat.notifyEventHoldElems);
1560
  }
1561
  TSDB_CHECK_CONDITION(offset < sizeof(buf), code, lino, _end, TSDB_CODE_INTERNAL_ERROR);
×
1562
  buf[offset] = '\0';
×
1563

1564
  STR_TO_VARSTR(vbuf, buf);
×
1565

1566
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1567
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1568

1569
  if (offset == 0) {
×
1570
    colDataSetNULL(pColInfo, numOfRows);
×
1571
  } else {
1572
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1573
    TSDB_CHECK_CODE(code, lino, _end);
×
1574
  }
1575

1576
_end:
×
1577
  if (code) {
×
1578
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1579
  }
1580
  return code;
×
1581
}
1582

1583
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
×
1584
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
×
1585
  const SEp *p = GET_ACTIVE_EP(pCurrent);
×
1586

1587
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
×
1588
    return false;
×
1589
  }
1590
  return true;
×
1591
}
1592

1593
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
×
1594
  if (pInfo != NULL) {
×
1595
    taosArrayDestroy(pInfo->pUpdateNodeList);
×
1596
    taosHashCleanup(pInfo->pDBMap);
×
1597
  }
1598
}
×
1599

1600
// 1. increase the replica does not affect the stream process.
1601
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1602
// tasks on the will be removed replica.
1603
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1604
// will handle it as mentioned in 1 & 2 items.
1605
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
×
1606
                               SVgroupChangeInfo *pInfo) {
1607
  int32_t code = 0;
×
1608
  int32_t lino = 0;
×
1609

1610
  if (pInfo == NULL) {
×
1611
    return TSDB_CODE_INVALID_PARA;
×
1612
  }
1613

1614
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
×
1615
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
×
1616

1617
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
×
1618
    mndDestroyVgroupChangeInfo(pInfo);
×
1619
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1620
  }
1621

1622
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
×
1623
  for (int32_t i = 0; i < numOfNodes; ++i) {
×
1624
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
×
1625
    if (pPrevEntry == NULL) {
×
1626
      continue;
×
1627
    }
1628

1629
    int32_t num = taosArrayGetSize(pNodeList);
×
1630
    for (int32_t j = 0; j < num; ++j) {
×
1631
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
×
1632
      if (pCurrent == NULL) {
×
1633
        continue;
×
1634
      }
1635

1636
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
×
1637
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
×
1638
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
×
1639

1640
          char buf[256] = {0};
×
1641
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
×
1642
          if (code) {
×
1643
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1644
            TSDB_CHECK_CODE(code, lino, _err);
×
1645
          }
1646

1647
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
×
1648
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1649

1650
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
×
1651
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
×
1652
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
×
1653

1654
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
×
1655
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
×
1656
        }
1657

1658
        // todo handle the snode info
1659
        if (pCurrent->nodeId != SNODE_HANDLE) {
×
1660
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
×
1661
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
×
1662
          mndReleaseVgroup(pMnode, pVgroup);
×
1663
          TSDB_CHECK_CODE(code, lino, _err);
×
1664
        }
1665

1666
        break;
×
1667
      }
1668
    }
1669
  }
1670

1671
  return code;
×
1672

1673
_err:
×
1674
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1675
  mndDestroyVgroupChangeInfo(pInfo);
×
1676
  return code;
×
1677
}
1678

1679
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
×
1680
  bool              allReady = false;
×
1681
  bool              nodeUpdated = false;
×
1682
  SVgroupChangeInfo changeInfo = {0};
×
1683

1684
  int32_t numOfNodes = extractStreamNodeList(pMnode);
×
1685

1686
  if (numOfNodes == 0) {
×
1687
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
×
1688
    execInfo.ts = taosGetTimestampSec();
×
1689
    return false;
×
1690
  }
1691

1692
  for (int32_t i = 0; i < numOfNodes; ++i) {
×
1693
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
×
1694
    if (pNodeEntry == NULL) {
×
1695
      continue;
×
1696
    }
1697

1698
    if (pNodeEntry->stageUpdated) {
×
1699
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
×
1700
      return true;
×
1701
    }
1702
  }
1703

1704
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot, NULL);
×
1705
  if (code) {
×
1706
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1707
  }
1708

1709
  if (!allReady) {
×
1710
    mWarn("not all vnodes ready, quit from vnodes status check");
×
1711
    return true;
×
1712
  }
1713

1714
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
×
1715
  if (code) {
×
1716
    nodeUpdated = false;
×
1717
  } else {
1718
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
×
1719
    if (nodeUpdated) {
×
1720
      mDebug("stream tasks not ready due to node update");
×
1721
    }
1722
  }
1723

1724
  mndDestroyVgroupChangeInfo(&changeInfo);
×
1725
  return nodeUpdated;
×
1726
}
1727

1728
// check if the node update happens or not
1729
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
×
1730
  SArray *pNodeSnapshot = NULL;
×
1731

1732
  streamMutexLock(&execInfo.lock);
×
1733
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
×
1734
  streamMutexUnlock(&execInfo.lock);
×
1735

1736
  taosArrayDestroy(pNodeSnapshot);
×
1737
  return updated;
×
1738
}
1739

1740
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
×
1741
  SSdb      *pSdb = pMnode->pSdb;
×
1742
  void      *pIter = NULL;
×
1743
  SSnodeObj *pObj = NULL;
×
1744

1745
  if (pSrcDb->cfg.replications == 1) {
×
1746
    return TSDB_CODE_SUCCESS;
×
1747
  } else {
1748
    while (1) {
1749
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
1750
      if (pIter == NULL) {
×
1751
        break;
×
1752
      }
1753

1754
      sdbRelease(pSdb, pObj);
×
1755
      sdbCancelFetch(pSdb, pIter);
×
1756
      return TSDB_CODE_SUCCESS;
×
1757
    }
1758

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

© 2026 Coveralls, Inc