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

taosdata / TDengine / #3814

01 Apr 2025 05:36AM UTC coverage: 30.169% (-3.6%) from 33.798%
#3814

push

travis-ci

happyguoxy
test:add build cmake

129680 of 592945 branches covered (21.87%)

Branch coverage included in aggregate %.

196213 of 487270 relevant lines covered (40.27%)

555639.42 hits per line

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

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

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

46
  return 0;
12✔
47
}
48

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

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

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

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

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

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

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

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

97
    ESyncState state = pVgroup->vnodeGid[i].syncState;
242✔
98
    if (state == TAOS_SYNC_STATE_OFFLINE || state == TAOS_SYNC_STATE_ERROR || state == TAOS_SYNC_STATE_LEARNER ||
242!
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;
242✔
107
}
108

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

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

120
    SNodeEntry entry = {.nodeId = SNODE_HANDLE};
27✔
121
    code = addEpIntoEpSet(&entry.epset, pObj->pDnode->fqdn, pObj->pDnode->port);
27✔
122
    if (code) {
27!
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};
27✔
130
    code = epsetToStr(&entry.epset, buf, tListLen(buf));
27✔
131
    if (code != 0) {  // print error and continue
27!
132
      mError("failed to convert epset to str, code:%s", tstrerror(code));
×
133
    }
134

135
    void *p = taosArrayPush(pVgroupList, &entry);
27✔
136
    if (p == NULL) {
27!
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);
27!
144
    }
145

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

149
  return code;
82✔
150
}
151

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

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

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

171
    if (objStatus != SDB_STATUS_READY) {
85!
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);
85✔
179
  }
180

181
  return TSDB_CODE_SUCCESS;
80✔
182
}
183

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

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

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

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
250✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
250✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
123✔
209
      if (code) {
123!
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) {
127!
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) {
250✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
247✔
227
    }
228

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

235
    void *p = taosArrayPush(pVgroupList, &entry);
250✔
236
    if (p == NULL) {
250!
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);
250!
244
    }
245

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

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

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

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

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

269
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
82✔
270
  if (pVgroupList == NULL) {
82!
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);
82✔
278
  if (code) {
82!
279
    goto _err;
×
280
  }
281

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

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

294
  *pList = pVgroupList;
82✔
295
  return code;
82✔
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) {
22✔
304
  void *pIter = NULL;
22✔
305
  SSdb *pSdb = pMnode->pSdb;
22✔
306
  *pStream = NULL;
22✔
307

308
  SStreamObj *p = NULL;
22✔
309
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
22✔
310
    if (p->uid == streamId) {
21!
311
      sdbCancelFetch(pSdb, pIter);
21✔
312
      *pStream = p;
21✔
313
      return TSDB_CODE_SUCCESS;
21✔
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) {
18✔
336
  *hasEpset = false;
18✔
337

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

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

365
      epsetAssign(pEpSet, &epset);
14✔
366
      *hasEpset = true;
14✔
367
      return TSDB_CODE_SUCCESS;
14✔
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) {
17✔
404
  int32_t num = 0;
17✔
405
  for (int32_t i = 0; i < taosArrayGetSize(pStream->pTaskList); ++i) {
68✔
406
    SArray *pLevel = taosArrayGetP(pStream->pTaskList, i);
51✔
407
    num += taosArrayGetSize(pLevel);
51✔
408
  }
409

410
  return num;
17✔
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) {
4✔
440
  SArray **pList = (SArray **)param;
4✔
441
  taosArrayDestroy(*pList);
4✔
442
}
4✔
443

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

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

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

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

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

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

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

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

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

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

498
      if (pEntry->nodeId == p->nodeId) {
30✔
499
        p->hbTimestamp = pEntry->hbTimestamp;
15✔
500

501
        void *px = taosArrayPush(pValidList, p);
15✔
502
        if (px == NULL) {
15!
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);
15!
506
        }
507
        break;
15✔
508
      }
509
    }
510
  }
511

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

515
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
5!
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) {
5✔
549
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
5!
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
}
5✔
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) {
20✔
611
  size_t num = taosArrayGetSize(pList);
20✔
612

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

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

624
  return false;
×
625
}
626

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

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

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

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

649
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
20✔
650
    if (!existed) {
20!
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);
5✔
659

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

663
  removeExpiredNodeInfo(pNodeSnapshot);
5✔
664

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

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

674
  int32_t existed = (int32_t)taosArrayGetSize(pReportInfo->pTaskList);
1✔
675
  if (existed != numOfTasks) {
1!
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) {
6✔
683
    STaskChkptInfo *pInfo = taosArrayGet(pReportInfo->pTaskList, i);
5✔
684
    if (pInfo == NULL) {
5!
685
      continue;
×
686
    }
687

688
    if (checkpointId == -1) {
5✔
689
      checkpointId = pInfo->checkpointId;
1✔
690
      transId = pInfo->transId;
1✔
691
      taskId = pInfo->taskId;
1✔
692
    } else if (checkpointId != pInfo->checkpointId) {
4!
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);
1✔
703
  STaskId id = {.streamId = p->streamId, .taskId = p->taskId};
1✔
704
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
1✔
705

706
  // cross-check failed, there must be something unknown wrong
707
  SStreamTransInfo *pTransInfo = taosHashGet(execInfo.transMgmt.pDBTrans, &id.streamId, sizeof(id.streamId));
1✔
708
  if (pTransInfo == NULL) {
1!
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) {
1!
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,
1!
730
         pName, numOfTasks);
731

732
  return TSDB_CODE_SUCCESS;
1✔
733
}
734

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

743
  mDebug("start to scan checkpoint report info");
12!
744

745
  streamMutexLock(&execInfo.lock);
12✔
746

747
  int32_t num = taosHashGetSize(execInfo.pChkptStreams);
12✔
748
  if (num == 0) {
12✔
749
    goto _end;
3✔
750
  }
751

752
  pDropped = taosArrayInit(4, sizeof(int64_t));
9✔
753
  TSDB_CHECK_NULL(pDropped, code, lino, _end, terrno);
9!
754

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

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

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

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

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

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

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

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

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

822
_end:
12✔
823

824
  ts = taosGetTimestampMs();
12✔
825
  execInfo.chkptReportScanTs = ts;
12✔
826

827
  streamMutexUnlock(&execInfo.lock);
12✔
828

829
  if (pDropped != NULL) {
12✔
830
    taosArrayDestroy(pDropped);
9✔
831
  }
832

833
  mDebug("end to scan checkpoint report info, ts:%"PRId64, ts);
12!
834
  return code;
12✔
835
}
836

837
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int64_t checkpointId, SArray* pList) {
1✔
838
  char    msg[128] = {0};
1✔
839
  STrans *pTrans = NULL;
1✔
840

841
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for stream:0x%" PRIx64, pStream->uid);
1✔
842

843
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
1✔
844
  if (pTrans == NULL || code != 0) {
1!
845
    return terrno;
×
846
  }
847

848
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
1✔
849
  if (code) {
1!
850
    sdbRelease(pMnode->pSdb, pStream);
×
851
    return code;
×
852
  }
853

854
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pStream, checkpointId, pList);
1✔
855
  if (code != 0) {
1!
856
    sdbRelease(pMnode->pSdb, pStream);
×
857
    mndTransDrop(pTrans);
×
858
    return code;
×
859
  }
860

861
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
1✔
862
  if (code) {
1!
863
    sdbRelease(pMnode->pSdb, pStream);
×
864
    mndTransDrop(pTrans);
×
865
    return code;
×
866
  }
867

868
  code = mndTransPrepare(pMnode, pTrans);
1✔
869

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

878
  sdbRelease(pMnode->pSdb, pStream);
1✔
879
  mndTransDrop(pTrans);
1✔
880

881
  return TSDB_CODE_ACTION_IN_PROGRESS;
1✔
882
}
883

884
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
6✔
885
  *pInfo = NULL;
6✔
886

887
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
6✔
888
  if (px != NULL) {
6✔
889
    *pInfo = px;
4✔
890
    return 0;
4✔
891
  }
892

893
  SCheckpointConsensusInfo p = {
2✔
894
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
2✔
895
      .numOfTasks = numOfTasks,
896
      .streamId = streamId,
897
  };
898

899
  if (p.pTaskList == NULL) {
2!
900
    return terrno;
×
901
  }
902

903
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
2✔
904
  if (code == 0) {
2!
905
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
2✔
906
    *pInfo = pChkptInfo;
2✔
907
  } else {
908
    *pInfo = NULL;
×
909
  }
910

911
  return code;
2✔
912
}
913

914
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
915
// discard the msg may lead to the lost of connections.
916
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
6✔
917
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
6✔
918
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
6✔
919

920
  int32_t num = (int32_t) taosArrayGetSize(pInfo->pTaskList);
6✔
921
  for (int32_t i = 0; i < num; ++i) {
16✔
922
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
10✔
923
    if (p == NULL) {
10!
924
      continue;
×
925
    }
926

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

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

953
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
1✔
954
  taosArrayDestroy(pInfo->pTaskList);
1✔
955
  pInfo->pTaskList = NULL;
1✔
956
}
1✔
957

958
int32_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
2✔
959
  int32_t code = 0;
2✔
960
  int32_t numOfStreams = taosHashGetSize(pHash);
2✔
961
  if (numOfStreams == 0) {
2!
962
    return code;
×
963
  }
964

965
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
2✔
966
  if (code == 0) {
2!
967
    numOfStreams = taosHashGetSize(pHash);
2✔
968
    mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
2!
969
  } else {
970
    mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
971
  }
972

973
  return code;
2✔
974
}
975

976
int32_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
977
  int32_t code = 0;
×
978
  int32_t numOfStreams = taosHashGetSize(pHash);
×
979
  if (numOfStreams == 0) {
×
980
    return code;
×
981
  }
982

983
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
×
984
  if (code == 0) {
×
985
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
986
  } else {
987
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
988
  }
989

990
  return code;
×
991
}
992

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

1002
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
1003
}
1004

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

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

1034
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
×
1035
  memset(pBuf, 0, bufLen);
×
1036
  pBuf[2] = '0';
×
1037
  pBuf[3] = 'x';
×
1038

1039
  int32_t len = tintToHex(id, &pBuf[4]);
×
1040
  varDataSetLen(pBuf, len + 2);
×
1041
}
×
1042

1043
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
×
1044
  int32_t          code = TSDB_CODE_SUCCESS;
×
1045
  int32_t          lino = 0;
×
1046
  SStreamTaskIter *pIter = NULL;
×
1047
  bool             isPaused =  true;
×
1048

1049
  taosRLockLatch(&pStream->lock);
×
1050
  code = createStreamTaskIter(pStream, &pIter);
×
1051
  TSDB_CHECK_CODE(code, lino, _end);
×
1052

1053
  while (streamTaskIterNextTask(pIter)) {
×
1054
    SStreamTask *pTask = NULL;
×
1055
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
1056
    TSDB_CHECK_CODE(code, lino, _end);
×
1057

1058
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
1059
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
1060
    if (pe == NULL) {
×
1061
      continue;
×
1062
    }
1063
    if (pe->status != TASK_STATUS__PAUSE) {
×
1064
      isPaused = false;
×
1065
    }
1066
  }
1067
  (*pRes) = isPaused;
×
1068

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

1078
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
×
1079
  int32_t code = 0;
×
1080
  int32_t cols = 0;
×
1081
  int32_t lino = 0;
×
1082

1083
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1084
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
1085
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1086
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1087

1088
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
1089
  TSDB_CHECK_CODE(code, lino, _end);
×
1090

1091
  // create time
1092
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1093
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1094
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
×
1095
  TSDB_CHECK_CODE(code, lino, _end);
×
1096

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

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

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

1124
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
1125
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
×
1126
  bool isPaused = false;
×
1127
  code = isAllTaskPaused(pStream, &isPaused);
×
1128
  TSDB_CHECK_CODE(code, lino, _end);
×
1129

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

1139
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
×
1140
  TSDB_CHECK_CODE(code, lino, _end);
×
1141

1142
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1143
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
×
1144
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1145
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1146

1147
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
×
1148
  TSDB_CHECK_CODE(code, lino, _end);
×
1149

1150
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1151
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
×
1152
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1153
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1154

1155
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
×
1156
  TSDB_CHECK_CODE(code, lino, _end);
×
1157

1158
  if (pStream->targetSTbName[0] == 0) {
×
1159
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1160
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1161

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

1169
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
×
1170
  }
1171
  TSDB_CHECK_CODE(code, lino, _end);
×
1172

1173
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1174
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1175

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

1179
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
×
1180
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
×
1181
  mndShowStreamTrigger(trigger2, pStream);
×
1182
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
×
1183
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1184
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1185

1186
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
×
1187
  TSDB_CHECK_CODE(code, lino, _end);
×
1188

1189
  // sink_quota
1190
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
×
1191
  sinkQuota[0] = '0';
×
1192
  char dstStr[20] = {0};
×
1193
  STR_TO_VARSTR(dstStr, sinkQuota)
×
1194
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1195
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1196

1197
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
×
1198
  TSDB_CHECK_CODE(code, lino, _end);
×
1199

1200
  // checkpoint interval
1201
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
×
1202
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
×
1203
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
×
1204

1205
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1206
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1207

1208
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
×
1209
  TSDB_CHECK_CODE(code, lino, _end);
×
1210

1211
  // checkpoint backup type
1212
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
×
1213
  STR_TO_VARSTR(backup, "none")
×
1214
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1215
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1216

1217
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
×
1218
  TSDB_CHECK_CODE(code, lino, _end);
×
1219

1220
  // history scan idle
1221
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
×
1222
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
×
1223

1224
  memset(dstStr, 0, tListLen(dstStr));
×
1225
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
×
1226
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1227
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1228

1229
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
×
1230
  TSDB_CHECK_CODE(code, lino, _end);
×
1231

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

1242
_end:
×
1243
  if (code) {
×
1244
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1245
  }
1246
  return code;
×
1247
}
1248

1249
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
×
1250
                              int32_t precision) {
1251
  SColumnInfoData *pColInfo = NULL;
×
1252
  int32_t          cols = 0;
×
1253
  int32_t          code = 0;
×
1254
  int32_t          lino = 0;
×
1255

1256
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
1257

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

1266
  // stream name
1267
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1268
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
1269

1270
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1271
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1272

1273
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
1274
  TSDB_CHECK_CODE(code, lino, _end);
×
1275

1276
  // task id
1277
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1278
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1279

1280
  char idstr[128] = {0};
×
1281
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
×
1282
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1283
  TSDB_CHECK_CODE(code, lino, _end);
×
1284

1285
  // node type
1286
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
×
1287
  varDataSetLen(nodeType, 5);
×
1288
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1289
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1290

1291
  if (pTask->info.nodeId > 0) {
×
1292
    memcpy(varDataVal(nodeType), "vnode", 5);
×
1293
  } else {
1294
    memcpy(varDataVal(nodeType), "snode", 5);
×
1295
  }
1296
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
×
1297
  TSDB_CHECK_CODE(code, lino, _end);
×
1298

1299
  // node id
1300
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1301
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1302

1303
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
×
1304
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
×
1305
  TSDB_CHECK_CODE(code, lino, _end);
×
1306

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

1319
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1320
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1321

1322
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
×
1323
  TSDB_CHECK_CODE(code, lino, _end);
×
1324

1325
  // status
1326
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
1327

1328
  const char *pStatus = streamTaskGetStatusStr(pe->status);
×
1329
  STR_TO_VARSTR(status, pStatus);
×
1330

1331
  // status
1332
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1333
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1334

1335
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
×
1336
  TSDB_CHECK_CODE(code, lino, _end);
×
1337

1338
  // stage
1339
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1340
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1341

1342
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
×
1343
  TSDB_CHECK_CODE(code, lino, _end);
×
1344

1345
  // input queue
1346
  char        vbuf[TSDB_STREAM_NOTIFY_STAT_LEN + 2] = {0};
×
1347
  char        buf[TSDB_STREAM_NOTIFY_STAT_LEN] = {0};
×
1348
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
×
1349
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
×
1350
  STR_TO_VARSTR(vbuf, buf);
×
1351

1352
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1353
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1354

1355
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1356
  TSDB_CHECK_CODE(code, lino, _end);
×
1357

1358
  // input total
1359
  const char *formatTotalMb = "%7.2f MiB";
×
1360
  const char *formatTotalGb = "%7.2f GiB";
×
1361
  if (pe->procsTotal < 1024) {
×
1362
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
×
1363
  } else {
1364
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1365
  }
1366

1367
  memset(vbuf, 0, tListLen(vbuf));
×
1368
  STR_TO_VARSTR(vbuf, buf);
×
1369

1370
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1371
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1372

1373
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1374
  TSDB_CHECK_CODE(code, lino, _end);
×
1375

1376
  // process throughput
1377
  const char *formatKb = "%7.2f KiB/s";
×
1378
  const char *formatMb = "%7.2f MiB/s";
×
1379
  if (pe->procsThroughput < 1024) {
×
1380
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
×
1381
  } else {
1382
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
×
1383
  }
1384

1385
  memset(vbuf, 0, tListLen(vbuf));
×
1386
  STR_TO_VARSTR(vbuf, buf);
×
1387

1388
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1389
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1390

1391
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1392
  TSDB_CHECK_CODE(code, lino, _end);
×
1393

1394
  // output total
1395
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1396
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1397

1398
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1399
    colDataSetNULL(pColInfo, numOfRows);
×
1400
  } else {
1401
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
×
1402
    memset(vbuf, 0, tListLen(vbuf));
×
1403
    STR_TO_VARSTR(vbuf, buf);
×
1404

1405
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1406
    TSDB_CHECK_CODE(code, lino, _end);
×
1407
  }
1408

1409
  // output throughput
1410
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1411
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1412

1413
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
1414
    colDataSetNULL(pColInfo, numOfRows);
×
1415
  } else {
1416
    if (pe->outputThroughput < 1024) {
×
1417
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
×
1418
    } else {
1419
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
×
1420
    }
1421

1422
    memset(vbuf, 0, tListLen(vbuf));
×
1423
    STR_TO_VARSTR(vbuf, buf);
×
1424

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

1447
  STR_TO_VARSTR(vbuf, buf);
×
1448

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

1452
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1453
  TSDB_CHECK_CODE(code, lino, _end);
×
1454

1455
  // start_time
1456
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1457
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1458

1459
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
×
1460
  TSDB_CHECK_CODE(code, lino, _end);
×
1461

1462
  // start id
1463
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1464
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1465

1466
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
×
1467
  TSDB_CHECK_CODE(code, lino, _end);
×
1468

1469
  // start ver
1470
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1471
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1472

1473
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
×
1474
  TSDB_CHECK_CODE(code, lino, _end);
×
1475

1476
  // checkpoint time
1477
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1478
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1479

1480
  if (pe->checkpointInfo.latestTime != 0) {
×
1481
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
×
1482
  } else {
1483
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1484
  }
1485
  TSDB_CHECK_CODE(code, lino, _end);
×
1486

1487
  // checkpoint_id
1488
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1489
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1490

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

1494
  // checkpoint version
1495
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1496
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1497

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

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

1505
  colDataSetNULL(pColInfo, numOfRows);
×
1506

1507
  // checkpoint backup status
1508
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1509
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1510

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

1514
  // ds_err_info
1515
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1516
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1517

1518
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1519
  TSDB_CHECK_CODE(code, lino, _end);
×
1520

1521
  // history_task_id
1522
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1523
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1524

1525
  if (pe->hTaskId != 0) {
×
1526
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
×
1527
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1528
  } else {
1529
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1530
  }
1531
  TSDB_CHECK_CODE(code, lino, _end);
×
1532

1533
  // history_task_status
1534
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1535
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1536

1537
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1538
  TSDB_CHECK_CODE(code, lino, _end);
×
1539

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

1569
  STR_TO_VARSTR(vbuf, buf);
×
1570

1571
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1572
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1573

1574
  if (offset == 0) {
×
1575
    colDataSetNULL(pColInfo, numOfRows);
×
1576
  } else {
1577
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
1578
    TSDB_CHECK_CODE(code, lino, _end);
×
1579
  }
1580

1581
_end:
×
1582
  if (code) {
×
1583
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1584
  }
1585
  return code;
×
1586
}
1587

1588
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
20✔
1589
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
20✔
1590
  const SEp *p = GET_ACTIVE_EP(pCurrent);
20✔
1591

1592
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
20!
1593
    return false;
20✔
1594
  }
1595
  return true;
×
1596
}
1597

1598
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
7✔
1599
  if (pInfo != NULL) {
7!
1600
    taosArrayDestroy(pInfo->pUpdateNodeList);
7✔
1601
    taosHashCleanup(pInfo->pDBMap);
7✔
1602
  }
1603
}
7✔
1604

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

1615
  if (pInfo == NULL) {
7!
1616
    return TSDB_CODE_INVALID_PARA;
×
1617
  }
1618

1619
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
7✔
1620
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
7✔
1621

1622
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
7!
1623
    mndDestroyVgroupChangeInfo(pInfo);
×
1624
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1625
  }
1626

1627
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
7✔
1628
  for (int32_t i = 0; i < numOfNodes; ++i) {
28✔
1629
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
21✔
1630
    if (pPrevEntry == NULL) {
21!
1631
      continue;
×
1632
    }
1633

1634
    int32_t num = taosArrayGetSize(pNodeList);
21✔
1635
    for (int32_t j = 0; j < num; ++j) {
42!
1636
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
42✔
1637
      if (pCurrent == NULL) {
42!
1638
        continue;
×
1639
      }
1640

1641
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
42✔
1642
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
21!
1643
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
1✔
1644

1645
          char buf[256] = {0};
1✔
1646
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
1✔
1647
          if (code) {
1!
1648
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1649
            TSDB_CHECK_CODE(code, lino, _err);
×
1650
          }
1651

1652
          mDebug("nodeId:%d restart/epset changed detected, old:%s:%d -> new:%s, stageUpdate:%d", pCurrent->nodeId,
1!
1653
                 pPrevEp->fqdn, pPrevEp->port, buf, pPrevEntry->stageUpdated);
1654

1655
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
1✔
1656
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
1✔
1657
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
1✔
1658

1659
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
1✔
1660
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
1!
1661
        }
1662

1663
        // todo handle the snode info
1664
        if (pCurrent->nodeId != SNODE_HANDLE) {
21✔
1665
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
14✔
1666
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
14✔
1667
          mndReleaseVgroup(pMnode, pVgroup);
14✔
1668
          TSDB_CHECK_CODE(code, lino, _err);
14!
1669
        }
1670

1671
        break;
21✔
1672
      }
1673
    }
1674
  }
1675

1676
  return code;
7✔
1677

1678
_err:
×
1679
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1680
  mndDestroyVgroupChangeInfo(pInfo);
×
1681
  return code;
×
1682
}
1683

1684
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
6✔
1685
  bool              allReady = false;
6✔
1686
  bool              nodeUpdated = false;
6✔
1687
  SVgroupChangeInfo changeInfo = {0};
6✔
1688

1689
  int32_t numOfNodes = extractStreamNodeList(pMnode);
6✔
1690

1691
  if (numOfNodes == 0) {
6✔
1692
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
3!
1693
    execInfo.ts = taosGetTimestampSec();
3✔
1694
    return false;
3✔
1695
  }
1696

1697
  for (int32_t i = 0; i < numOfNodes; ++i) {
11✔
1698
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
9✔
1699
    if (pNodeEntry == NULL) {
9!
1700
      continue;
×
1701
    }
1702

1703
    if (pNodeEntry->stageUpdated) {
9✔
1704
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
1!
1705
      return true;
1✔
1706
    }
1707
  }
1708

1709
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot, NULL);
2✔
1710
  if (code) {
2!
1711
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1712
  }
1713

1714
  if (!allReady) {
2!
1715
    mWarn("not all vnodes ready, quit from vnodes status check");
×
1716
    return true;
×
1717
  }
1718

1719
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
2✔
1720
  if (code) {
2!
1721
    nodeUpdated = false;
×
1722
  } else {
1723
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
2✔
1724
    if (nodeUpdated) {
2!
1725
      mDebug("stream tasks not ready due to node update");
×
1726
    }
1727
  }
1728

1729
  mndDestroyVgroupChangeInfo(&changeInfo);
2✔
1730
  return nodeUpdated;
2✔
1731
}
1732

1733
// check if the node update happens or not
1734
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
6✔
1735
  SArray *pNodeSnapshot = NULL;
6✔
1736

1737
  streamMutexLock(&execInfo.lock);
6✔
1738
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
6✔
1739
  streamMutexUnlock(&execInfo.lock);
6✔
1740

1741
  taosArrayDestroy(pNodeSnapshot);
6✔
1742
  return updated;
6✔
1743
}
1744

1745
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
2✔
1746
  SSdb      *pSdb = pMnode->pSdb;
2✔
1747
  void      *pIter = NULL;
2✔
1748
  SSnodeObj *pObj = NULL;
2✔
1749

1750
  if (pSrcDb->cfg.replications == 1) {
2!
1751
    return TSDB_CODE_SUCCESS;
2✔
1752
  } else {
1753
    while (1) {
1754
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
1755
      if (pIter == NULL) {
×
1756
        break;
×
1757
      }
1758

1759
      sdbRelease(pSdb, pObj);
×
1760
      sdbCancelFetch(pSdb, pIter);
×
1761
      return TSDB_CODE_SUCCESS;
×
1762
    }
1763

1764
    mError("snode not existed when trying to create stream in db with multiple replica");
×
1765
    return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
1766
  }
1767
}
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