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

taosdata / TDengine / #3562

20 Dec 2024 09:57AM UTC coverage: 26.655% (-32.2%) from 58.812%
#3562

push

travis-ci

web-flow
Merge pull request #29229 from taosdata/enh/TS-5749-3.0

enh: seperate tsdb async tasks to different thread pools

21498 of 109421 branches covered (19.65%)

Branch coverage included in aggregate %.

66 of 96 new or added lines in 7 files covered. (68.75%)

39441 existing lines in 157 files now uncovered.

35007 of 102566 relevant lines covered (34.13%)

53922.97 hits per line

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

10.24
/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

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

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

UNCOV
46
  return 0;
×
47
}
48

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

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

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

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

UNCOV
73
  pIter->pTask = NULL;
×
UNCOV
74
  return false;
×
75
}
76

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

85
  return TSDB_CODE_INVALID_PARA;
×
86
}
87

UNCOV
88
void destroyStreamTaskIter(SStreamTaskIter *pIter) { taosMemoryFree(pIter); }
×
89

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

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

106
  return true;
296✔
107
}
108

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

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

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

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

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

149
  return code;
321✔
150
}
151

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

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

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

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

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

181
  return TSDB_CODE_SUCCESS;
321✔
182
}
183

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

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

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

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

206
    int8_t *pReplica = taosHashGet(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid));
527✔
207
    if (pReplica == NULL) {  // not exist, add it into hash map
527✔
208
      code = taosHashPut(pHash, &pVgroup->dbUid, sizeof(pVgroup->dbUid), &pVgroup->replica, sizeof(pVgroup->replica));
420✔
209
      if (code) {
420!
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) {
107!
UNCOV
217
        mInfo("vgId:%d replica:%d inconsistent with other vgroups replica:%d, not ready for stream operations",
×
218
              pVgroup->vgId, pVgroup->replica, *pReplica);
UNCOV
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) {
527✔
226
      *allReady = checkStatusForEachReplica(pVgroup);
479✔
227
    }
228

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

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

246
    sdbRelease(pSdb, pVgroup);
527✔
247
  }
248

249
_end:
321✔
250
  taosHashCleanup(pHash);
321✔
251
  return code;
321✔
252
}
253

254
int32_t mndTakeVgroupSnapshot(SMnode *pMnode, bool *allReady, SArray **pList) {
321✔
255
  int32_t   code = 0;
321✔
256
  SArray   *pVgroupList = NULL;
321✔
257

258
  *pList = NULL;
321✔
259
  *allReady = true;
321✔
260

261
  pVgroupList = taosArrayInit(4, sizeof(SNodeEntry));
321✔
262
  if (pVgroupList == NULL) {
321!
263
    mError("failed to prepare arraylist during take vgroup snapshot, code:%s", tstrerror(terrno));
×
264
    code = terrno;
×
265
    goto _err;
×
266
  }
267

268
  // 1. check for all vnodes status
269
  code = mndCheckAndAddVgroupsInfo(pMnode, pVgroupList, allReady);
321✔
270
  if (code) {
321!
271
    goto _err;
×
272
  }
273

274
  // 2. add snode info
275
  code = mndAddSnodeInfo(pMnode, pVgroupList);
321✔
276
  if (code) {
321!
277
    goto _err;
×
278
  }
279

280
  // 3. check for mnode status
281
  code = mndCheckMnodeStatus(pMnode);
321✔
282
  if (code != TSDB_CODE_SUCCESS) {
321!
UNCOV
283
    *allReady = false;
×
284
  }
285

286
  *pList = pVgroupList;
321✔
287
  return code;
321✔
288

289
_err:
×
290
  *allReady = false;
×
291
  taosArrayDestroy(pVgroupList);
×
292
  return code;
×
293
}
294

UNCOV
295
int32_t mndGetStreamObj(SMnode *pMnode, int64_t streamId, SStreamObj **pStream) {
×
UNCOV
296
  void *pIter = NULL;
×
UNCOV
297
  SSdb *pSdb = pMnode->pSdb;
×
UNCOV
298
  *pStream = NULL;
×
299

UNCOV
300
  SStreamObj *p = NULL;
×
UNCOV
301
  while ((pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&p)) != NULL) {
×
UNCOV
302
    if (p->uid == streamId) {
×
UNCOV
303
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
304
      *pStream = p;
×
UNCOV
305
      return TSDB_CODE_SUCCESS;
×
306
    }
UNCOV
307
    sdbRelease(pSdb, p);
×
308
  }
309

UNCOV
310
  return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
311
}
312

313
void mndKillTransImpl(SMnode *pMnode, int32_t transId, const char *pDbName) {
×
314
  STrans *pTrans = mndAcquireTrans(pMnode, transId);
×
315
  if (pTrans != NULL) {
×
316
    mInfo("kill active transId:%d in Db:%s", transId, pDbName);
×
317
    int32_t code = mndKillTrans(pMnode, pTrans);
×
318
    mndReleaseTrans(pMnode, pTrans);
×
319
    if (code) {
×
320
      mError("failed to kill transId:%d, code:%s", pTrans->id, tstrerror(code));
×
321
    }
322
  } else {
323
    mError("failed to acquire trans in Db:%s, transId:%d", pDbName, transId);
×
324
  }
325
}
×
326

UNCOV
327
int32_t extractNodeEpset(SMnode *pMnode, SEpSet *pEpSet, bool *hasEpset, int32_t taskId, int32_t nodeId) {
×
UNCOV
328
  *hasEpset = false;
×
329

UNCOV
330
  pEpSet->numOfEps = 0;
×
UNCOV
331
  if (nodeId == SNODE_HANDLE) {
×
UNCOV
332
    SSnodeObj *pObj = NULL;
×
UNCOV
333
    void      *pIter = NULL;
×
334

UNCOV
335
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
UNCOV
336
    if (pIter != NULL) {
×
UNCOV
337
      int32_t code = addEpIntoEpSet(pEpSet, pObj->pDnode->fqdn, pObj->pDnode->port);
×
UNCOV
338
      sdbRelease(pMnode->pSdb, pObj);
×
UNCOV
339
      sdbCancelFetch(pMnode->pSdb, pIter);
×
UNCOV
340
      if (code) {
×
341
        *hasEpset = false;
×
342
        mError("failed to set epset");
×
343
      } else {
UNCOV
344
        *hasEpset = true;
×
345
      }
UNCOV
346
      return code;
×
347
    } else {
348
      mError("failed to acquire snode epset");
×
349
      return TSDB_CODE_INVALID_PARA;
×
350
    }
351
  } else {
UNCOV
352
    SVgObj *pVgObj = mndAcquireVgroup(pMnode, nodeId);
×
UNCOV
353
    if (pVgObj != NULL) {
×
UNCOV
354
      SEpSet epset = mndGetVgroupEpset(pMnode, pVgObj);
×
UNCOV
355
      mndReleaseVgroup(pMnode, pVgObj);
×
356

UNCOV
357
      epsetAssign(pEpSet, &epset);
×
UNCOV
358
      *hasEpset = true;
×
UNCOV
359
      return TSDB_CODE_SUCCESS;
×
360
    } else {
UNCOV
361
      mDebug("orphaned task:0x%x need to be dropped, nodeId:%d, no redo action", taskId, nodeId);
×
UNCOV
362
      return TSDB_CODE_SUCCESS;
×
363
    }
364
  }
365
}
366

UNCOV
367
int32_t mndGetStreamTask(STaskId *pId, SStreamObj *pStream, SStreamTask **pTask) {
×
UNCOV
368
  *pTask = NULL;
×
369

UNCOV
370
  SStreamTask     *p = NULL;
×
UNCOV
371
  SStreamTaskIter *pIter = NULL;
×
UNCOV
372
  int32_t          code = createStreamTaskIter(pStream, &pIter);
×
UNCOV
373
  if (code) {
×
374
    mError("failed to create stream task iter:%s", pStream->name);
×
375
    return code;
×
376
  }
377

UNCOV
378
  while (streamTaskIterNextTask(pIter)) {
×
UNCOV
379
    code = streamTaskIterGetCurrent(pIter, &p);
×
UNCOV
380
    if (code) {
×
381
      continue;
×
382
    }
383

UNCOV
384
    if (p->id.taskId == pId->taskId) {
×
UNCOV
385
      destroyStreamTaskIter(pIter);
×
UNCOV
386
      *pTask = p;
×
UNCOV
387
      return 0;
×
388
    }
389
  }
390

391
  destroyStreamTaskIter(pIter);
×
392
  return TSDB_CODE_FAILED;
×
393
}
394

UNCOV
395
int32_t mndGetNumOfStreamTasks(const SStreamObj *pStream) {
×
UNCOV
396
  int32_t num = 0;
×
UNCOV
397
  for (int32_t i = 0; i < taosArrayGetSize(pStream->tasks); ++i) {
×
UNCOV
398
    SArray *pLevel = taosArrayGetP(pStream->tasks, i);
×
UNCOV
399
    num += taosArrayGetSize(pLevel);
×
400
  }
401

UNCOV
402
  return num;
×
403
}
404

UNCOV
405
int32_t mndGetNumOfStreams(SMnode *pMnode, char *dbName, int32_t *pNumOfStreams) {
×
UNCOV
406
  SSdb   *pSdb = pMnode->pSdb;
×
UNCOV
407
  SDbObj *pDb = mndAcquireDb(pMnode, dbName);
×
UNCOV
408
  if (pDb == NULL) {
×
409
    TAOS_RETURN(TSDB_CODE_MND_DB_NOT_SELECTED);
×
410
  }
411

UNCOV
412
  int32_t numOfStreams = 0;
×
UNCOV
413
  void   *pIter = NULL;
×
414
  while (1) {
×
UNCOV
415
    SStreamObj *pStream = NULL;
×
UNCOV
416
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
×
UNCOV
417
    if (pIter == NULL) break;
×
418

419
    if (pStream->sourceDbUid == pDb->uid) {
×
420
      numOfStreams++;
×
421
    }
422

423
    sdbRelease(pSdb, pStream);
×
424
  }
425

UNCOV
426
  *pNumOfStreams = numOfStreams;
×
UNCOV
427
  mndReleaseDb(pMnode, pDb);
×
UNCOV
428
  return 0;
×
429
}
430

UNCOV
431
static void freeTaskList(void *param) {
×
UNCOV
432
  SArray **pList = (SArray **)param;
×
UNCOV
433
  taosArrayDestroy(*pList);
×
UNCOV
434
}
×
435

436
int32_t mndInitExecInfo() {
13✔
437
  int32_t code = taosThreadMutexInit(&execInfo.lock, NULL);
13✔
438
  if (code) {
13!
439
    return code;
×
440
  }
441

442
  _hash_fn_t fn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR);
13✔
443

444
  execInfo.pTaskList = taosArrayInit(4, sizeof(STaskId));
13✔
445
  execInfo.pTaskMap = taosHashInit(64, fn, true, HASH_NO_LOCK);
13✔
446
  execInfo.transMgmt.pDBTrans = taosHashInit(32, fn, true, HASH_NO_LOCK);
13✔
447
  execInfo.pTransferStateStreams = taosHashInit(32, fn, true, HASH_NO_LOCK);
13✔
448
  execInfo.pChkptStreams = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
13✔
449
  execInfo.pStreamConsensus = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK);
13✔
450
  execInfo.pNodeList = taosArrayInit(4, sizeof(SNodeEntry));
13✔
451
  execInfo.pKilledChkptTrans = taosArrayInit(4, sizeof(SStreamTaskResetMsg));
13✔
452

453
  if (execInfo.pTaskList == NULL || execInfo.pTaskMap == NULL || execInfo.transMgmt.pDBTrans == NULL ||
13!
454
      execInfo.pTransferStateStreams == NULL || execInfo.pChkptStreams == NULL || execInfo.pStreamConsensus == NULL ||
13!
455
      execInfo.pNodeList == NULL || execInfo.pKilledChkptTrans == NULL) {
13!
456
    mError("failed to initialize the stream runtime env, code:%s", tstrerror(terrno));
×
457
    return terrno;
×
458
  }
459

460
  execInfo.role = NODE_ROLE_UNINIT;
13✔
461
  execInfo.switchFromFollower = false;
13✔
462

463
  taosHashSetFreeFp(execInfo.pTransferStateStreams, freeTaskList);
13✔
464
  taosHashSetFreeFp(execInfo.pChkptStreams, freeTaskList);
13✔
465
  taosHashSetFreeFp(execInfo.pStreamConsensus, freeTaskList);
13✔
466
  return 0;
13✔
467
}
468

UNCOV
469
void removeExpiredNodeInfo(const SArray *pNodeSnapshot) {
×
UNCOV
470
  SArray *pValidList = taosArrayInit(4, sizeof(SNodeEntry));
×
UNCOV
471
  if (pValidList == NULL) {  // not continue
×
472
    return;
×
473
  }
474

UNCOV
475
  int32_t size = taosArrayGetSize(pNodeSnapshot);
×
UNCOV
476
  int32_t oldSize = taosArrayGetSize(execInfo.pNodeList);
×
477

UNCOV
478
  for (int32_t i = 0; i < oldSize; ++i) {
×
UNCOV
479
    SNodeEntry *p = taosArrayGet(execInfo.pNodeList, i);
×
UNCOV
480
    if (p == NULL) {
×
481
      continue;
×
482
    }
483

UNCOV
484
    for (int32_t j = 0; j < size; ++j) {
×
UNCOV
485
      SNodeEntry *pEntry = taosArrayGet(pNodeSnapshot, j);
×
UNCOV
486
      if (pEntry == NULL) {
×
487
        continue;
×
488
      }
489

UNCOV
490
      if (pEntry->nodeId == p->nodeId) {
×
UNCOV
491
        p->hbTimestamp = pEntry->hbTimestamp;
×
492

UNCOV
493
        void *px = taosArrayPush(pValidList, p);
×
UNCOV
494
        if (px == NULL) {
×
495
          mError("failed to put node into list, nodeId:%d", p->nodeId);
×
496
        } else {
UNCOV
497
          mDebug("vgId:%d ts:%" PRId64 " HbMsgId:%d is valid", p->nodeId, p->hbTimestamp, p->lastHbMsgId);
×
498
        }
UNCOV
499
        break;
×
500
      }
501
    }
502
  }
503

UNCOV
504
  taosArrayDestroy(execInfo.pNodeList);
×
UNCOV
505
  execInfo.pNodeList = pValidList;
×
506

UNCOV
507
  mDebug("remain %d valid node entries after clean expired nodes info, prev size:%d",
×
508
         (int32_t)taosArrayGetSize(pValidList), oldSize);
509
}
510

UNCOV
511
int32_t doRemoveTasks(SStreamExecInfo *pExecNode, STaskId *pRemovedId) {
×
UNCOV
512
  void *p = taosHashGet(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
×
UNCOV
513
  if (p == NULL) {
×
UNCOV
514
    return TSDB_CODE_SUCCESS;
×
515
  }
516

UNCOV
517
  int32_t code = taosHashRemove(pExecNode->pTaskMap, pRemovedId, sizeof(*pRemovedId));
×
UNCOV
518
  if (code) {
×
519
    return code;
×
520
  }
521

UNCOV
522
  for (int32_t k = 0; k < taosArrayGetSize(pExecNode->pTaskList); ++k) {
×
UNCOV
523
    STaskId *pId = taosArrayGet(pExecNode->pTaskList, k);
×
UNCOV
524
    if (pId == NULL) {
×
525
      continue;
×
526
    }
527

UNCOV
528
    if (pId->taskId == pRemovedId->taskId && pId->streamId == pRemovedId->streamId) {
×
UNCOV
529
      taosArrayRemove(pExecNode->pTaskList, k);
×
530

UNCOV
531
      int32_t num = taosArrayGetSize(pExecNode->pTaskList);
×
UNCOV
532
      mInfo("s-task:0x%x removed from buffer, remain:%d in buffer list", (int32_t)pRemovedId->taskId, num);
×
UNCOV
533
      break;
×
534
    }
535
  }
536

UNCOV
537
  return TSDB_CODE_SUCCESS;
×
538
}
539

540
void removeTasksInBuf(SArray *pTaskIds, SStreamExecInfo *pExecInfo) {
50✔
541
  for (int32_t i = 0; i < taosArrayGetSize(pTaskIds); ++i) {
50!
542
    STaskId *pId = taosArrayGet(pTaskIds, i);
×
543
    if (pId == NULL) {
×
544
      continue;
×
545
    }
546

547
    int32_t code = doRemoveTasks(pExecInfo, pId);
×
548
    if (code) {
×
549
      mError("failed to remove task in buffer list, 0x%" PRIx64, pId->taskId);
×
550
    }
551
  }
552
}
50✔
553

UNCOV
554
void removeStreamTasksInBuf(SStreamObj *pStream, SStreamExecInfo *pExecNode) {
×
UNCOV
555
  SStreamTaskIter *pIter = NULL;
×
UNCOV
556
  streamMutexLock(&pExecNode->lock);
×
557

558
  // 1. remove task entries
UNCOV
559
  int32_t code = createStreamTaskIter(pStream, &pIter);
×
UNCOV
560
  if (code) {
×
561
    streamMutexUnlock(&pExecNode->lock);
×
562
    mError("failed to create stream task iter:%s", pStream->name);
×
563
    return;
×
564
  }
565

UNCOV
566
  while (streamTaskIterNextTask(pIter)) {
×
UNCOV
567
    SStreamTask *pTask = NULL;
×
UNCOV
568
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
UNCOV
569
    if (code) {
×
570
      continue;
×
571
    }
572

UNCOV
573
    STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
UNCOV
574
    code = doRemoveTasks(pExecNode, &id);
×
UNCOV
575
    if (code) {
×
576
      mError("failed to remove task in buffer list, 0x%" PRIx64, id.taskId);
×
577
    }
578
  }
579

UNCOV
580
  if (taosHashGetSize(pExecNode->pTaskMap) != taosArrayGetSize(pExecNode->pTaskList)) {
×
581
    streamMutexUnlock(&pExecNode->lock);
×
582
    destroyStreamTaskIter(pIter);
×
583
    mError("task map size, task list size, not equal");
×
584
    return;
×
585
  }
586

587
  // 2. remove stream entry in consensus hash table and checkpoint-report hash table
UNCOV
588
  code = mndClearConsensusCheckpointId(execInfo.pStreamConsensus, pStream->uid);
×
UNCOV
589
  if (code) {
×
590
    mError("failed to clear consensus checkpointId, code:%s", tstrerror(code));
×
591
  }
592

UNCOV
593
  code = mndClearChkptReportInfo(execInfo.pChkptStreams, pStream->uid);
×
UNCOV
594
  if (code) {
×
UNCOV
595
    mError("failed to clear the checkpoint report info, code:%s", tstrerror(code));
×
596
  }
597

UNCOV
598
  streamMutexUnlock(&pExecNode->lock);
×
UNCOV
599
  destroyStreamTaskIter(pIter);
×
600
}
601

UNCOV
602
static bool taskNodeExists(SArray *pList, int32_t nodeId) {
×
UNCOV
603
  size_t num = taosArrayGetSize(pList);
×
604

UNCOV
605
  for (int32_t i = 0; i < num; ++i) {
×
UNCOV
606
    SNodeEntry *pEntry = taosArrayGet(pList, i);
×
UNCOV
607
    if (pEntry == NULL) {
×
608
      continue;
×
609
    }
610

UNCOV
611
    if (pEntry->nodeId == nodeId) {
×
UNCOV
612
      return true;
×
613
    }
614
  }
615

616
  return false;
×
617
}
618

UNCOV
619
int32_t removeExpiredNodeEntryAndTaskInBuf(SArray *pNodeSnapshot) {
×
UNCOV
620
  SArray *pRemovedTasks = taosArrayInit(4, sizeof(STaskId));
×
UNCOV
621
  if (pRemovedTasks == NULL) {
×
622
    return terrno;
×
623
  }
624

UNCOV
625
  int32_t numOfTask = taosArrayGetSize(execInfo.pTaskList);
×
UNCOV
626
  for (int32_t i = 0; i < numOfTask; ++i) {
×
UNCOV
627
    STaskId *pId = taosArrayGet(execInfo.pTaskList, i);
×
UNCOV
628
    if (pId == NULL) {
×
629
      continue;
×
630
    }
631

UNCOV
632
    STaskStatusEntry *pEntry = taosHashGet(execInfo.pTaskMap, pId, sizeof(*pId));
×
UNCOV
633
    if (pEntry == NULL) {
×
634
      continue;
×
635
    }
636

UNCOV
637
    if (pEntry->nodeId == SNODE_HANDLE) {
×
UNCOV
638
      continue;
×
639
    }
640

UNCOV
641
    bool existed = taskNodeExists(pNodeSnapshot, pEntry->nodeId);
×
UNCOV
642
    if (!existed) {
×
643
      void *p = taosArrayPush(pRemovedTasks, pId);
×
644
      if (p == NULL) {
×
645
        mError("failed to put task entry into remove list, taskId:0x%" PRIx64, pId->taskId);
×
646
      }
647
    }
648
  }
649

UNCOV
650
  removeTasksInBuf(pRemovedTasks, &execInfo);
×
651

UNCOV
652
  mDebug("remove invalid stream tasks:%d, remain:%d", (int32_t)taosArrayGetSize(pRemovedTasks),
×
653
         (int32_t)taosArrayGetSize(execInfo.pTaskList));
654

UNCOV
655
  removeExpiredNodeInfo(pNodeSnapshot);
×
656

UNCOV
657
  taosArrayDestroy(pRemovedTasks);
×
UNCOV
658
  return 0;
×
659
}
660

UNCOV
661
int32_t mndScanCheckpointReportInfo(SRpcMsg *pReq) {
×
UNCOV
662
  SMnode *pMnode = pReq->info.node;
×
UNCOV
663
  void   *pIter = NULL;
×
UNCOV
664
  int32_t code = 0;
×
UNCOV
665
  SArray *pDropped = taosArrayInit(4, sizeof(int64_t));
×
UNCOV
666
  if (pDropped == NULL) {
×
667
    return terrno;
×
668
  }
669

UNCOV
670
  mDebug("start to scan checkpoint report info");
×
UNCOV
671
  streamMutexLock(&execInfo.lock);
×
672

UNCOV
673
  while ((pIter = taosHashIterate(execInfo.pChkptStreams, pIter)) != NULL) {
×
UNCOV
674
    SChkptReportInfo *px = (SChkptReportInfo *)pIter;
×
UNCOV
675
    if (taosArrayGetSize(px->pTaskList) == 0) {
×
UNCOV
676
      continue;
×
677
    }
678

UNCOV
679
    STaskChkptInfo *pInfo = taosArrayGet(px->pTaskList, 0);
×
UNCOV
680
    if (pInfo == NULL) {
×
681
      continue;
×
682
    }
683

UNCOV
684
    SStreamObj *pStream = NULL;
×
UNCOV
685
    code = mndGetStreamObj(pMnode, pInfo->streamId, &pStream);
×
UNCOV
686
    if (pStream == NULL || code != 0) {
×
687
      mDebug("failed to acquire stream:0x%" PRIx64 " remove it from checkpoint-report list", pInfo->streamId);
×
688
      void *p = taosArrayPush(pDropped, &pInfo->streamId);
×
689
      if (p == NULL) {
×
690
        mError("failed to put stream into drop list:0x%" PRIx64, pInfo->streamId);
×
691
      }
692
      continue;
×
693
    }
694

UNCOV
695
    int32_t total = mndGetNumOfStreamTasks(pStream);
×
UNCOV
696
    int32_t existed = (int32_t)taosArrayGetSize(px->pTaskList);
×
697

UNCOV
698
    if (total == existed) {
×
UNCOV
699
      mDebug("stream:0x%" PRIx64 " %s all %d tasks send checkpoint-report, start to update checkpoint-info",
×
700
             pStream->uid, pStream->name, total);
701

UNCOV
702
      code = mndStreamTransConflictCheck(pMnode, pStream->uid, MND_STREAM_CHKPT_UPDATE_NAME, false);
×
UNCOV
703
      if (code == 0) {
×
UNCOV
704
        code = mndCreateStreamChkptInfoUpdateTrans(pMnode, pStream, px->pTaskList);
×
UNCOV
705
        if (code == TSDB_CODE_SUCCESS || code == TSDB_CODE_ACTION_IN_PROGRESS) {  // remove this entry
×
UNCOV
706
          taosArrayClear(px->pTaskList);
×
UNCOV
707
          px->reportChkpt = pInfo->checkpointId;
×
UNCOV
708
          mDebug("stream:0x%" PRIx64 " clear checkpoint-report list", pInfo->streamId);
×
709
        } else {
710
          mDebug("stream:0x%" PRIx64 " not launch chkpt-meta update trans, due to checkpoint not finished yet",
×
711
                 pInfo->streamId);
712
        }
UNCOV
713
        break;
×
714
      } else {
715
        mDebug("stream:0x%" PRIx64 " active checkpoint trans not finished yet, wait", pInfo->streamId);
×
716
      }
717
    } else {
UNCOV
718
      mDebug("stream:0x%" PRIx64 " %s %d/%d tasks send checkpoint-report, %d not send", pInfo->streamId, pStream->name,
×
719
             existed, total, total - existed);
720
    }
721

UNCOV
722
    sdbRelease(pMnode->pSdb, pStream);
×
723
  }
724

UNCOV
725
  int32_t size = taosArrayGetSize(pDropped);
×
UNCOV
726
  if (size > 0) {
×
727
    for (int32_t i = 0; i < size; ++i) {
×
728
      int64_t *pStreamId = (int64_t *)taosArrayGet(pDropped, i);
×
729
      if (pStreamId == NULL) {
×
730
        continue;
×
731
      }
732

733
      code = taosHashRemove(execInfo.pChkptStreams, pStreamId, sizeof(*pStreamId));
×
734
      if (code) {
×
735
        mError("failed to remove stream in buf:0x%" PRIx64, *pStreamId);
×
736
      }
737
    }
738

739
    int32_t numOfStreams = taosHashGetSize(execInfo.pChkptStreams);
×
740
    mDebug("drop %d stream(s) in checkpoint-report list, remain:%d", size, numOfStreams);
×
741
  }
742

UNCOV
743
  streamMutexUnlock(&execInfo.lock);
×
744

UNCOV
745
  taosArrayDestroy(pDropped);
×
UNCOV
746
  return TSDB_CODE_SUCCESS;
×
747
}
748

UNCOV
749
int32_t mndCreateSetConsensusChkptIdTrans(SMnode *pMnode, SStreamObj *pStream, int32_t taskId, int64_t checkpointId,
×
750
                                          int64_t ts) {
UNCOV
751
  char msg[128] = {0};
×
UNCOV
752
  snprintf(msg, tListLen(msg), "set consen-chkpt-id for task:0x%x", taskId);
×
753

UNCOV
754
  STrans *pTrans = NULL;
×
UNCOV
755
  int32_t code = doCreateTrans(pMnode, pStream, NULL, TRN_CONFLICT_NOTHING, MND_STREAM_CHKPT_CONSEN_NAME, msg, &pTrans);
×
UNCOV
756
  if (pTrans == NULL || code != 0) {
×
757
    return terrno;
×
758
  }
759

UNCOV
760
  STaskId      id = {.streamId = pStream->uid, .taskId = taskId};
×
UNCOV
761
  SStreamTask *pTask = NULL;
×
UNCOV
762
  code = mndGetStreamTask(&id, pStream, &pTask);
×
UNCOV
763
  if (code) {
×
764
    mError("failed to get task:0x%x in stream:%s, failed to create consensus-checkpointId", taskId, pStream->name);
×
765
    sdbRelease(pMnode->pSdb, pStream);
×
766
    return code;
×
767
  }
768

UNCOV
769
  code = mndStreamRegisterTrans(pTrans, MND_STREAM_CHKPT_CONSEN_NAME, pStream->uid);
×
UNCOV
770
  if (code) {
×
771
    sdbRelease(pMnode->pSdb, pStream);
×
772
    return code;
×
773
  }
774

UNCOV
775
  code = mndStreamSetChkptIdAction(pMnode, pTrans, pTask, checkpointId, ts);
×
UNCOV
776
  if (code != 0) {
×
777
    sdbRelease(pMnode->pSdb, pStream);
×
778
    mndTransDrop(pTrans);
×
779
    return code;
×
780
  }
781

UNCOV
782
  code = mndPersistTransLog(pStream, pTrans, SDB_STATUS_READY);
×
UNCOV
783
  if (code) {
×
784
    sdbRelease(pMnode->pSdb, pStream);
×
785
    mndTransDrop(pTrans);
×
786
    return code;
×
787
  }
788

UNCOV
789
  code = mndTransPrepare(pMnode, pTrans);
×
UNCOV
790
  if (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS) {
×
791
    mError("trans:%d, failed to prepare set consensus-chkptId trans since %s", pTrans->id, terrstr());
×
792
    sdbRelease(pMnode->pSdb, pStream);
×
793
    mndTransDrop(pTrans);
×
794
    return code;
×
795
  }
796

UNCOV
797
  sdbRelease(pMnode->pSdb, pStream);
×
UNCOV
798
  mndTransDrop(pTrans);
×
799

UNCOV
800
  return TSDB_CODE_ACTION_IN_PROGRESS;
×
801
}
802

UNCOV
803
int32_t mndGetConsensusInfo(SHashObj *pHash, int64_t streamId, int32_t numOfTasks, SCheckpointConsensusInfo **pInfo) {
×
UNCOV
804
  *pInfo = NULL;
×
805

UNCOV
806
  void *px = taosHashGet(pHash, &streamId, sizeof(streamId));
×
UNCOV
807
  if (px != NULL) {
×
UNCOV
808
    *pInfo = px;
×
UNCOV
809
    return 0;
×
810
  }
811

UNCOV
812
  SCheckpointConsensusInfo p = {
×
UNCOV
813
      .pTaskList = taosArrayInit(4, sizeof(SCheckpointConsensusEntry)),
×
814
      .numOfTasks = numOfTasks,
815
      .streamId = streamId,
816
  };
817

UNCOV
818
  if (p.pTaskList == NULL) {
×
819
    return terrno;
×
820
  }
821

UNCOV
822
  int32_t code = taosHashPut(pHash, &streamId, sizeof(streamId), &p, sizeof(p));
×
UNCOV
823
  if (code == 0) {
×
UNCOV
824
    void *pChkptInfo = (SCheckpointConsensusInfo *)taosHashGet(pHash, &streamId, sizeof(streamId));
×
UNCOV
825
    *pInfo = pChkptInfo;
×
826
  } else {
827
    *pInfo = NULL;
×
828
  }
829

UNCOV
830
  return code;
×
831
}
832

833
// no matter existed or not, add the request into info list anyway, since we need to send rsp mannually
834
// discard the msg may lead to the lost of connections.
UNCOV
835
void mndAddConsensusTasks(SCheckpointConsensusInfo *pInfo, const SRestoreCheckpointInfo *pRestoreInfo) {
×
UNCOV
836
  SCheckpointConsensusEntry info = {.ts = taosGetTimestampMs()};
×
UNCOV
837
  memcpy(&info.req, pRestoreInfo, sizeof(info.req));
×
838

UNCOV
839
  for (int32_t i = 0; i < taosArrayGetSize(pInfo->pTaskList); ++i) {
×
UNCOV
840
    SCheckpointConsensusEntry *p = taosArrayGet(pInfo->pTaskList, i);
×
UNCOV
841
    if (p == NULL) {
×
842
      continue;
×
843
    }
844

UNCOV
845
    if (p->req.taskId == info.req.taskId) {
×
UNCOV
846
      mDebug("s-task:0x%x already in consensus-checkpointId list for stream:0x%" PRIx64 ", update ts %" PRId64
×
847
             "->%" PRId64 " total existed:%d",
848
             pRestoreInfo->taskId, pRestoreInfo->streamId, p->req.startTs, info.req.startTs,
849
             (int32_t)taosArrayGetSize(pInfo->pTaskList));
UNCOV
850
      p->req.startTs = info.req.startTs;
×
UNCOV
851
      return;
×
852
    }
853
  }
854

UNCOV
855
  void *p = taosArrayPush(pInfo->pTaskList, &info);
×
UNCOV
856
  if (p == NULL) {
×
857
    mError("s-task:0x%x failed to put task into consensus-checkpointId list, code: out of memory", info.req.taskId);
×
858
  } else {
UNCOV
859
    int32_t num = taosArrayGetSize(pInfo->pTaskList);
×
UNCOV
860
    mDebug("s-task:0x%x checkpointId:%" PRId64 " added into consensus-checkpointId list, stream:0x%" PRIx64
×
861
           " waiting tasks:%d",
862
           pRestoreInfo->taskId, pRestoreInfo->checkpointId, pRestoreInfo->streamId, num);
863
  }
864
}
865

UNCOV
866
void mndClearConsensusRspEntry(SCheckpointConsensusInfo *pInfo) {
×
UNCOV
867
  taosArrayDestroy(pInfo->pTaskList);
×
UNCOV
868
  pInfo->pTaskList = NULL;
×
UNCOV
869
}
×
870

UNCOV
871
int64_t mndClearConsensusCheckpointId(SHashObj *pHash, int64_t streamId) {
×
UNCOV
872
  int32_t code = 0;
×
UNCOV
873
  int32_t numOfStreams = taosHashGetSize(pHash);
×
UNCOV
874
  if (numOfStreams == 0) {
×
UNCOV
875
    return code;
×
876
  }
877

UNCOV
878
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
×
UNCOV
879
  if (code == 0) {
×
UNCOV
880
    mDebug("drop stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
881
  } else {
882
    mError("failed to remove stream:0x%" PRIx64 " in consensus-checkpointId list, remain:%d", streamId, numOfStreams);
×
883
  }
884

UNCOV
885
  return code;
×
886
}
887

UNCOV
888
int64_t mndClearChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
UNCOV
889
  int32_t code = 0;
×
UNCOV
890
  int32_t numOfStreams = taosHashGetSize(pHash);
×
UNCOV
891
  if (numOfStreams == 0) {
×
UNCOV
892
    return code;
×
893
  }
894

UNCOV
895
  code = taosHashRemove(pHash, &streamId, sizeof(streamId));
×
UNCOV
896
  if (code == 0) {
×
UNCOV
897
    mDebug("drop stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
898
  } else {
UNCOV
899
    mError("failed to remove stream:0x%" PRIx64 " in chkpt-report list, remain:%d", streamId, numOfStreams);
×
900
  }
901

UNCOV
902
  return code;
×
903
}
904

905
int32_t mndResetChkptReportInfo(SHashObj *pHash, int64_t streamId) {
×
906
  SChkptReportInfo *pInfo = taosHashGet(pHash, &streamId, sizeof(streamId));
×
907
  if (pInfo != NULL) {
×
908
    taosArrayClear(pInfo->pTaskList);
×
909
    mDebug("stream:0x%" PRIx64 " checkpoint-report list cleared, prev report checkpointId:%" PRId64, streamId,
×
910
           pInfo->reportChkpt);
911
    return 0;
×
912
  }
913

914
  return TSDB_CODE_MND_STREAM_NOT_EXIST;
×
915
}
916

UNCOV
917
static void mndShowStreamStatus(char *dst, int8_t status) {
×
UNCOV
918
  if (status == STREAM_STATUS__NORMAL) {
×
UNCOV
919
    tstrncpy(dst, "ready", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
920
  } else if (status == STREAM_STATUS__STOP) {
×
921
    tstrncpy(dst, "stop", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
922
  } else if (status == STREAM_STATUS__FAILED) {
×
923
    tstrncpy(dst, "failed", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
924
  } else if (status == STREAM_STATUS__RECOVER) {
×
925
    tstrncpy(dst, "recover", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
926
  } else if (status == STREAM_STATUS__PAUSE) {
×
UNCOV
927
    tstrncpy(dst, "paused", MND_STREAM_TRIGGER_NAME_SIZE);
×
928
  }
UNCOV
929
}
×
930

UNCOV
931
static void mndShowStreamTrigger(char *dst, SStreamObj *pStream) {
×
UNCOV
932
  int8_t trigger = pStream->conf.trigger;
×
UNCOV
933
  if (trigger == STREAM_TRIGGER_AT_ONCE) {
×
UNCOV
934
    tstrncpy(dst, "at once", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
935
  } else if (trigger == STREAM_TRIGGER_WINDOW_CLOSE) {
×
UNCOV
936
    tstrncpy(dst, "window close", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
937
  } else if (trigger == STREAM_TRIGGER_MAX_DELAY) {
×
UNCOV
938
    tstrncpy(dst, "max delay", MND_STREAM_TRIGGER_NAME_SIZE);
×
UNCOV
939
  } else if (trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
UNCOV
940
    tstrncpy(dst, "force window close", MND_STREAM_TRIGGER_NAME_SIZE);
×
941
  }
UNCOV
942
}
×
943

UNCOV
944
static void int64ToHexStr(int64_t id, char *pBuf, int32_t bufLen) {
×
UNCOV
945
  memset(pBuf, 0, bufLen);
×
UNCOV
946
  pBuf[2] = '0';
×
UNCOV
947
  pBuf[3] = 'x';
×
948

UNCOV
949
  int32_t len = tintToHex(id, &pBuf[4]);
×
UNCOV
950
  varDataSetLen(pBuf, len + 2);
×
UNCOV
951
}
×
952

UNCOV
953
static int32_t isAllTaskPaused(SStreamObj *pStream, bool *pRes) {
×
UNCOV
954
  int32_t          code = TSDB_CODE_SUCCESS;
×
UNCOV
955
  int32_t          lino = 0;
×
UNCOV
956
  SStreamTaskIter *pIter = NULL;
×
UNCOV
957
  bool             isPaused =  true;
×
958

UNCOV
959
  taosRLockLatch(&pStream->lock);
×
UNCOV
960
  code = createStreamTaskIter(pStream, &pIter);
×
UNCOV
961
  TSDB_CHECK_CODE(code, lino, _end);
×
962

UNCOV
963
  while (streamTaskIterNextTask(pIter)) {
×
UNCOV
964
    SStreamTask *pTask = NULL;
×
UNCOV
965
    code = streamTaskIterGetCurrent(pIter, &pTask);
×
UNCOV
966
    TSDB_CHECK_CODE(code, lino, _end);
×
967

UNCOV
968
    STaskId           id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
UNCOV
969
    STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
UNCOV
970
    if (pe == NULL) {
×
UNCOV
971
      continue;
×
972
    }
UNCOV
973
    if (pe->status != TASK_STATUS__PAUSE) {
×
UNCOV
974
      isPaused = false;
×
975
    }
976
  }
UNCOV
977
  (*pRes) = isPaused;
×
978

UNCOV
979
_end:
×
UNCOV
980
  destroyStreamTaskIter(pIter);
×
UNCOV
981
  taosRUnLockLatch(&pStream->lock);
×
UNCOV
982
  if (code != TSDB_CODE_SUCCESS) {
×
983
    mError("error happens when get stream status, lino:%d, code:%s", lino, tstrerror(code));
×
984
  }
UNCOV
985
  return code;
×
986
}
987

UNCOV
988
int32_t setStreamAttrInResBlock(SStreamObj *pStream, SSDataBlock *pBlock, int32_t numOfRows) {
×
UNCOV
989
  int32_t code = 0;
×
UNCOV
990
  int32_t cols = 0;
×
UNCOV
991
  int32_t lino = 0;
×
992

UNCOV
993
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
994
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
UNCOV
995
  SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
996
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
997

UNCOV
998
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
UNCOV
999
  TSDB_CHECK_CODE(code, lino, _end);
×
1000

1001
  // create time
UNCOV
1002
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1003
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
UNCOV
1004
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->createTime, false);
×
UNCOV
1005
  TSDB_CHECK_CODE(code, lino, _end);
×
1006

1007
  // stream id
UNCOV
1008
  char buf[128] = {0};
×
UNCOV
1009
  int64ToHexStr(pStream->uid, buf, tListLen(buf));
×
UNCOV
1010
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1011
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
UNCOV
1012
  code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
UNCOV
1013
  TSDB_CHECK_CODE(code, lino, _end);
×
1014

1015
  // related fill-history stream id
UNCOV
1016
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1017
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
UNCOV
1018
  if (pStream->hTaskUid != 0) {
×
1019
    int64ToHexStr(pStream->hTaskUid, buf, tListLen(buf));
×
1020
    code = colDataSetVal(pColInfo, numOfRows, buf, false);
×
1021
  } else {
UNCOV
1022
    code = colDataSetVal(pColInfo, numOfRows, buf, true);
×
1023
  }
UNCOV
1024
  TSDB_CHECK_CODE(code, lino, _end);
×
1025

1026
  // related fill-history stream id
UNCOV
1027
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1028
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->sql, sizeof(sql));
×
UNCOV
1029
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1030
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
UNCOV
1031
  code = colDataSetVal(pColInfo, numOfRows, (const char *)sql, false);
×
UNCOV
1032
  TSDB_CHECK_CODE(code, lino, _end);
×
1033

UNCOV
1034
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1035
  char status2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
×
UNCOV
1036
  bool isPaused = false;
×
UNCOV
1037
  code = isAllTaskPaused(pStream, &isPaused);
×
UNCOV
1038
  TSDB_CHECK_CODE(code, lino, _end);
×
1039

UNCOV
1040
  int8_t streamStatus = atomic_load_8(&pStream->status);
×
UNCOV
1041
  if (isPaused) {
×
UNCOV
1042
    streamStatus = STREAM_STATUS__PAUSE;
×
1043
  }
UNCOV
1044
  mndShowStreamStatus(status2, streamStatus);
×
UNCOV
1045
  STR_WITH_MAXSIZE_TO_VARSTR(status, status2, sizeof(status));
×
UNCOV
1046
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1047
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1048

UNCOV
1049
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&status, false);
×
UNCOV
1050
  TSDB_CHECK_CODE(code, lino, _end);
×
1051

UNCOV
1052
  char sourceDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1053
  STR_WITH_MAXSIZE_TO_VARSTR(sourceDB, mndGetDbStr(pStream->sourceDb), sizeof(sourceDB));
×
UNCOV
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1056

UNCOV
1057
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&sourceDB, false);
×
UNCOV
1058
  TSDB_CHECK_CODE(code, lino, _end);
×
1059

UNCOV
1060
  char targetDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1061
  STR_WITH_MAXSIZE_TO_VARSTR(targetDB, mndGetDbStr(pStream->targetDb), sizeof(targetDB));
×
UNCOV
1062
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1063
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1064

UNCOV
1065
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetDB, false);
×
UNCOV
1066
  TSDB_CHECK_CODE(code, lino, _end);
×
1067

UNCOV
1068
  if (pStream->targetSTbName[0] == 0) {
×
UNCOV
1069
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1070
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1071

UNCOV
1072
    code = colDataSetVal(pColInfo, numOfRows, NULL, true);
×
1073
  } else {
UNCOV
1074
    char targetSTB[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1075
    STR_WITH_MAXSIZE_TO_VARSTR(targetSTB, mndGetStbStr(pStream->targetSTbName), sizeof(targetSTB));
×
UNCOV
1076
    pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1077
    TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1078

UNCOV
1079
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&targetSTB, false);
×
1080
  }
UNCOV
1081
  TSDB_CHECK_CODE(code, lino, _end);
×
1082

UNCOV
1083
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1084
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1085

UNCOV
1086
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pStream->conf.watermark, false);
×
UNCOV
1087
  TSDB_CHECK_CODE(code, lino, _end);
×
1088

UNCOV
1089
  char trigger[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1090
  char trigger2[MND_STREAM_TRIGGER_NAME_SIZE] = {0};
×
UNCOV
1091
  mndShowStreamTrigger(trigger2, pStream);
×
UNCOV
1092
  STR_WITH_MAXSIZE_TO_VARSTR(trigger, trigger2, sizeof(trigger));
×
UNCOV
1093
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1094
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1095

UNCOV
1096
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&trigger, false);
×
UNCOV
1097
  TSDB_CHECK_CODE(code, lino, _end);
×
1098

1099
  // sink_quota
UNCOV
1100
  char sinkQuota[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1101
  sinkQuota[0] = '0';
×
UNCOV
1102
  char dstStr[20] = {0};
×
UNCOV
1103
  STR_TO_VARSTR(dstStr, sinkQuota)
×
UNCOV
1104
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1105
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1106

UNCOV
1107
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
×
UNCOV
1108
  TSDB_CHECK_CODE(code, lino, _end);
×
1109

1110
  // checkpoint interval
UNCOV
1111
  char tmp[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1112
  (void)tsnprintf(varDataVal(tmp), sizeof(tmp) - VARSTR_HEADER_SIZE, "%d sec", tsStreamCheckpointInterval);
×
UNCOV
1113
  varDataSetLen(tmp, strlen(varDataVal(tmp)));
×
1114

UNCOV
1115
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1116
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1117

UNCOV
1118
  code = colDataSetVal(pColInfo, numOfRows, (const char *)tmp, false);
×
UNCOV
1119
  TSDB_CHECK_CODE(code, lino, _end);
×
1120

1121
  // checkpoint backup type
UNCOV
1122
  char backup[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1123
  STR_TO_VARSTR(backup, "none")
×
UNCOV
1124
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1125
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1126

UNCOV
1127
  code = colDataSetVal(pColInfo, numOfRows, (const char *)backup, false);
×
UNCOV
1128
  TSDB_CHECK_CODE(code, lino, _end);
×
1129

1130
  // history scan idle
UNCOV
1131
  char scanHistoryIdle[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1132
  tstrncpy(scanHistoryIdle, "100a", sizeof(scanHistoryIdle));
×
1133

UNCOV
1134
  memset(dstStr, 0, tListLen(dstStr));
×
UNCOV
1135
  STR_TO_VARSTR(dstStr, scanHistoryIdle)
×
UNCOV
1136
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1137
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1138

UNCOV
1139
  code = colDataSetVal(pColInfo, numOfRows, (const char *)dstStr, false);
×
1140

UNCOV
1141
_end:
×
UNCOV
1142
  if (code) {
×
1143
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1144
  }
UNCOV
1145
  return code;
×
1146
}
1147

UNCOV
1148
int32_t setTaskAttrInResBlock(SStreamObj *pStream, SStreamTask *pTask, SSDataBlock *pBlock, int32_t numOfRows,
×
1149
                              int32_t precision) {
UNCOV
1150
  SColumnInfoData *pColInfo = NULL;
×
UNCOV
1151
  int32_t          cols = 0;
×
UNCOV
1152
  int32_t          code = 0;
×
UNCOV
1153
  int32_t          lino = 0;
×
1154

UNCOV
1155
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
×
1156

UNCOV
1157
  STaskStatusEntry *pe = taosHashGet(execInfo.pTaskMap, &id, sizeof(id));
×
UNCOV
1158
  if (pe == NULL) {
×
1159
    mError("task:0x%" PRIx64 " not exists in any vnodes, streamName:%s, streamId:0x%" PRIx64 " createTs:%" PRId64
×
1160
           " no valid status/stage info",
1161
           id.taskId, pStream->name, pStream->uid, pStream->createTime);
1162
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
1163
  }
1164

1165
  // stream name
UNCOV
1166
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1167
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetDbStr(pStream->name), sizeof(streamName));
×
1168

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

UNCOV
1172
  code = colDataSetVal(pColInfo, numOfRows, (const char *)streamName, false);
×
UNCOV
1173
  TSDB_CHECK_CODE(code, lino, _end);
×
1174

1175
  // task id
UNCOV
1176
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1177
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1178

UNCOV
1179
  char idstr[128] = {0};
×
UNCOV
1180
  int64ToHexStr(pTask->id.taskId, idstr, tListLen(idstr));
×
UNCOV
1181
  code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
UNCOV
1182
  TSDB_CHECK_CODE(code, lino, _end);
×
1183

1184
  // node type
UNCOV
1185
  char nodeType[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1186
  varDataSetLen(nodeType, 5);
×
UNCOV
1187
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1188
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1189

UNCOV
1190
  if (pTask->info.nodeId > 0) {
×
UNCOV
1191
    memcpy(varDataVal(nodeType), "vnode", 5);
×
1192
  } else {
UNCOV
1193
    memcpy(varDataVal(nodeType), "snode", 5);
×
1194
  }
UNCOV
1195
  code = colDataSetVal(pColInfo, numOfRows, nodeType, false);
×
UNCOV
1196
  TSDB_CHECK_CODE(code, lino, _end);
×
1197

1198
  // node id
UNCOV
1199
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1200
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1201

UNCOV
1202
  int64_t nodeId = TMAX(pTask->info.nodeId, 0);
×
UNCOV
1203
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&nodeId, false);
×
UNCOV
1204
  TSDB_CHECK_CODE(code, lino, _end);
×
1205

1206
  // level
UNCOV
1207
  char level[20 + VARSTR_HEADER_SIZE] = {0};
×
UNCOV
1208
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
UNCOV
1209
    STR_WITH_SIZE_TO_VARSTR(level, "source", 6);
×
UNCOV
1210
  } else if (pTask->info.taskLevel == TASK_LEVEL__AGG) {
×
UNCOV
1211
    STR_WITH_SIZE_TO_VARSTR(level, "agg", 3);
×
UNCOV
1212
  } else if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
UNCOV
1213
    STR_WITH_SIZE_TO_VARSTR(level, "sink", 4);
×
1214
  }
1215

UNCOV
1216
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1217
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1218

UNCOV
1219
  code = colDataSetVal(pColInfo, numOfRows, (const char *)level, false);
×
UNCOV
1220
  TSDB_CHECK_CODE(code, lino, _end);
×
1221

1222
  // status
UNCOV
1223
  char status[20 + VARSTR_HEADER_SIZE] = {0};
×
1224

UNCOV
1225
  const char *pStatus = streamTaskGetStatusStr(pe->status);
×
UNCOV
1226
  STR_TO_VARSTR(status, pStatus);
×
1227

1228
  // status
UNCOV
1229
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1230
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1231

UNCOV
1232
  code = colDataSetVal(pColInfo, numOfRows, (const char *)status, false);
×
UNCOV
1233
  TSDB_CHECK_CODE(code, lino, _end);
×
1234

1235
  // stage
UNCOV
1236
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1237
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1238

UNCOV
1239
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->stage, false);
×
UNCOV
1240
  TSDB_CHECK_CODE(code, lino, _end);
×
1241

1242
  // input queue
UNCOV
1243
  char        vbuf[40] = {0};
×
UNCOV
1244
  char        buf[38] = {0};
×
UNCOV
1245
  const char *queueInfoStr = "%4.2f MiB (%6.2f%)";
×
UNCOV
1246
  snprintf(buf, tListLen(buf), queueInfoStr, pe->inputQUsed, pe->inputRate);
×
UNCOV
1247
  STR_TO_VARSTR(vbuf, buf);
×
1248

UNCOV
1249
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1250
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1251

UNCOV
1252
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
UNCOV
1253
  TSDB_CHECK_CODE(code, lino, _end);
×
1254

1255
  // input total
UNCOV
1256
  const char *formatTotalMb = "%7.2f MiB";
×
UNCOV
1257
  const char *formatTotalGb = "%7.2f GiB";
×
UNCOV
1258
  if (pe->procsTotal < 1024) {
×
UNCOV
1259
    snprintf(buf, tListLen(buf), formatTotalMb, pe->procsTotal);
×
1260
  } else {
1261
    snprintf(buf, tListLen(buf), formatTotalGb, pe->procsTotal / 1024);
×
1262
  }
1263

UNCOV
1264
  memset(vbuf, 0, tListLen(vbuf));
×
UNCOV
1265
  STR_TO_VARSTR(vbuf, buf);
×
1266

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

UNCOV
1270
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
UNCOV
1271
  TSDB_CHECK_CODE(code, lino, _end);
×
1272

1273
  // process throughput
UNCOV
1274
  const char *formatKb = "%7.2f KiB/s";
×
UNCOV
1275
  const char *formatMb = "%7.2f MiB/s";
×
UNCOV
1276
  if (pe->procsThroughput < 1024) {
×
UNCOV
1277
    snprintf(buf, tListLen(buf), formatKb, pe->procsThroughput);
×
1278
  } else {
UNCOV
1279
    snprintf(buf, tListLen(buf), formatMb, pe->procsThroughput / 1024);
×
1280
  }
1281

UNCOV
1282
  memset(vbuf, 0, tListLen(vbuf));
×
UNCOV
1283
  STR_TO_VARSTR(vbuf, buf);
×
1284

UNCOV
1285
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1286
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1287

UNCOV
1288
  code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
UNCOV
1289
  TSDB_CHECK_CODE(code, lino, _end);
×
1290

1291
  // output total
UNCOV
1292
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1293
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1294

UNCOV
1295
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
UNCOV
1296
    colDataSetNULL(pColInfo, numOfRows);
×
1297
  } else {
UNCOV
1298
    (void)tsnprintf(buf, sizeof(buf), formatTotalMb, pe->outputTotal);
×
UNCOV
1299
    memset(vbuf, 0, tListLen(vbuf));
×
UNCOV
1300
    STR_TO_VARSTR(vbuf, buf);
×
1301

UNCOV
1302
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
UNCOV
1303
    TSDB_CHECK_CODE(code, lino, _end);
×
1304
  }
1305

1306
  // output throughput
UNCOV
1307
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1308
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1309

UNCOV
1310
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
UNCOV
1311
    colDataSetNULL(pColInfo, numOfRows);
×
1312
  } else {
UNCOV
1313
    if (pe->outputThroughput < 1024) {
×
UNCOV
1314
      snprintf(buf, tListLen(buf), formatKb, pe->outputThroughput);
×
1315
    } else {
UNCOV
1316
      snprintf(buf, tListLen(buf), formatMb, pe->outputThroughput / 1024);
×
1317
    }
1318

UNCOV
1319
    memset(vbuf, 0, tListLen(vbuf));
×
UNCOV
1320
    STR_TO_VARSTR(vbuf, buf);
×
1321

UNCOV
1322
    code = colDataSetVal(pColInfo, numOfRows, (const char *)vbuf, false);
×
UNCOV
1323
    TSDB_CHECK_CODE(code, lino, _end);
×
1324
  }
1325
  // info
UNCOV
1326
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
×
UNCOV
1327
    const char *sinkStr = "%.2f MiB";
×
UNCOV
1328
    snprintf(buf, tListLen(buf), sinkStr, pe->sinkDataSize);
×
UNCOV
1329
  } else if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {  // offset info
×
UNCOV
1330
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
×
UNCOV
1331
      int32_t ret = taosFormatUtcTime(buf, tListLen(buf), pe->processedVer, precision);
×
UNCOV
1332
      if (ret != 0) {
×
1333
        mError("failed to format processed timewindow, skey:%" PRId64, pe->processedVer);
×
1334
        memset(buf, 0, tListLen(buf));
×
1335
      }
1336
    } else {
UNCOV
1337
      const char *offsetStr = "%" PRId64 " [%" PRId64 ", %" PRId64 "]";
×
UNCOV
1338
      snprintf(buf, tListLen(buf), offsetStr, pe->processedVer, pe->verRange.minVer, pe->verRange.maxVer);
×
1339
    }
1340
  } else {
UNCOV
1341
    memset(buf, 0, tListLen(buf));
×
1342
  }
1343

UNCOV
1344
  STR_TO_VARSTR(vbuf, buf);
×
1345

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

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

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

UNCOV
1356
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startTime, false);
×
UNCOV
1357
  TSDB_CHECK_CODE(code, lino, _end);
×
1358

1359
  // start id
UNCOV
1360
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1361
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1362

UNCOV
1363
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointId, false);
×
UNCOV
1364
  TSDB_CHECK_CODE(code, lino, _end);
×
1365

1366
  // start ver
UNCOV
1367
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1368
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1369

UNCOV
1370
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->startCheckpointVer, false);
×
UNCOV
1371
  TSDB_CHECK_CODE(code, lino, _end);
×
1372

1373
  // checkpoint time
UNCOV
1374
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1375
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1376

UNCOV
1377
  if (pe->checkpointInfo.latestTime != 0) {
×
UNCOV
1378
    code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestTime, false);
×
1379
  } else {
UNCOV
1380
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1381
  }
UNCOV
1382
  TSDB_CHECK_CODE(code, lino, _end);
×
1383

1384
  // checkpoint_id
UNCOV
1385
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1386
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1387

UNCOV
1388
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestId, false);
×
UNCOV
1389
  TSDB_CHECK_CODE(code, lino, _end);
×
1390

1391
  // checkpoint version
UNCOV
1392
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1393
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1394

UNCOV
1395
  code = colDataSetVal(pColInfo, numOfRows, (const char *)&pe->checkpointInfo.latestVer, false);
×
UNCOV
1396
  TSDB_CHECK_CODE(code, lino, _end);
×
1397

1398
  // checkpoint size
UNCOV
1399
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1400
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1401

UNCOV
1402
  colDataSetNULL(pColInfo, numOfRows);
×
1403

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

UNCOV
1408
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
UNCOV
1409
  TSDB_CHECK_CODE(code, lino, _end);
×
1410

1411
  // ds_err_info
UNCOV
1412
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1413
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1414

UNCOV
1415
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
UNCOV
1416
  TSDB_CHECK_CODE(code, lino, _end);
×
1417

1418
  // history_task_id
UNCOV
1419
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1420
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1421

UNCOV
1422
  if (pe->hTaskId != 0) {
×
UNCOV
1423
    int64ToHexStr(pe->hTaskId, idstr, tListLen(idstr));
×
UNCOV
1424
    code = colDataSetVal(pColInfo, numOfRows, idstr, false);
×
1425
  } else {
UNCOV
1426
    code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
1427
  }
UNCOV
1428
  TSDB_CHECK_CODE(code, lino, _end);
×
1429

1430
  // history_task_status
UNCOV
1431
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
UNCOV
1432
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1433

UNCOV
1434
  code = colDataSetVal(pColInfo, numOfRows, 0, true);
×
UNCOV
1435
  TSDB_CHECK_CODE(code, lino, _end);
×
1436

UNCOV
1437
_end:
×
UNCOV
1438
  if (code) {
×
1439
    mError("error happens during build task attr result blocks, lino:%d, code:%s", lino, tstrerror(code));
×
1440
  }
UNCOV
1441
  return code;
×
1442
}
1443

UNCOV
1444
static bool isNodeEpsetChanged(const SEpSet *pPrevEpset, const SEpSet *pCurrent) {
×
UNCOV
1445
  const SEp *pEp = GET_ACTIVE_EP(pPrevEpset);
×
UNCOV
1446
  const SEp *p = GET_ACTIVE_EP(pCurrent);
×
1447

UNCOV
1448
  if (pEp->port == p->port && strncmp(pEp->fqdn, p->fqdn, TSDB_FQDN_LEN) == 0) {
×
UNCOV
1449
    return false;
×
1450
  }
1451
  return true;
×
1452
}
1453

UNCOV
1454
void mndDestroyVgroupChangeInfo(SVgroupChangeInfo *pInfo) {
×
UNCOV
1455
  if (pInfo != NULL) {
×
UNCOV
1456
    taosArrayDestroy(pInfo->pUpdateNodeList);
×
UNCOV
1457
    taosHashCleanup(pInfo->pDBMap);
×
1458
  }
UNCOV
1459
}
×
1460

1461
// 1. increase the replica does not affect the stream process.
1462
// 2. decreasing the replica may affect the stream task execution in the way that there is one or more running stream
1463
// tasks on the will be removed replica.
1464
// 3. vgroup redistribution is an combination operation of first increase replica and then decrease replica. So we
1465
// will handle it as mentioned in 1 & 2 items.
UNCOV
1466
int32_t mndFindChangedNodeInfo(SMnode *pMnode, const SArray *pPrevNodeList, const SArray *pNodeList,
×
1467
                               SVgroupChangeInfo *pInfo) {
UNCOV
1468
  int32_t code = 0;
×
UNCOV
1469
  int32_t lino = 0;
×
1470

UNCOV
1471
  if (pInfo == NULL) {
×
1472
    return TSDB_CODE_INVALID_PARA;
×
1473
  }
1474

UNCOV
1475
  pInfo->pUpdateNodeList = taosArrayInit(4, sizeof(SNodeUpdateInfo));
×
UNCOV
1476
  pInfo->pDBMap = taosHashInit(32, taosGetDefaultHashFunction(TSDB_DATA_TYPE_VARCHAR), true, HASH_NO_LOCK);
×
1477

UNCOV
1478
  if (pInfo->pUpdateNodeList == NULL || pInfo->pDBMap == NULL) {
×
1479
    mndDestroyVgroupChangeInfo(pInfo);
×
1480
    TSDB_CHECK_NULL(NULL, code, lino, _err, terrno);
×
1481
  }
1482

UNCOV
1483
  int32_t numOfNodes = taosArrayGetSize(pPrevNodeList);
×
UNCOV
1484
  for (int32_t i = 0; i < numOfNodes; ++i) {
×
UNCOV
1485
    SNodeEntry *pPrevEntry = taosArrayGet(pPrevNodeList, i);
×
UNCOV
1486
    if (pPrevEntry == NULL) {
×
1487
      continue;
×
1488
    }
1489

UNCOV
1490
    int32_t num = taosArrayGetSize(pNodeList);
×
UNCOV
1491
    for (int32_t j = 0; j < num; ++j) {
×
UNCOV
1492
      SNodeEntry *pCurrent = taosArrayGet(pNodeList, j);
×
UNCOV
1493
      if (pCurrent == NULL) {
×
1494
        continue;
×
1495
      }
1496

UNCOV
1497
      if (pCurrent->nodeId == pPrevEntry->nodeId) {
×
UNCOV
1498
        if (pPrevEntry->stageUpdated || isNodeEpsetChanged(&pPrevEntry->epset, &pCurrent->epset)) {
×
UNCOV
1499
          const SEp *pPrevEp = GET_ACTIVE_EP(&pPrevEntry->epset);
×
1500

UNCOV
1501
          char buf[256] = {0};
×
UNCOV
1502
          code = epsetToStr(&pCurrent->epset, buf, tListLen(buf));  // ignore this error
×
UNCOV
1503
          if (code) {
×
1504
            mError("failed to convert epset string, code:%s", tstrerror(code));
×
1505
            TSDB_CHECK_CODE(code, lino, _err);
×
1506
          }
1507

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

UNCOV
1511
          SNodeUpdateInfo updateInfo = {.nodeId = pPrevEntry->nodeId};
×
UNCOV
1512
          epsetAssign(&updateInfo.prevEp, &pPrevEntry->epset);
×
UNCOV
1513
          epsetAssign(&updateInfo.newEp, &pCurrent->epset);
×
1514

UNCOV
1515
          void *p = taosArrayPush(pInfo->pUpdateNodeList, &updateInfo);
×
UNCOV
1516
          TSDB_CHECK_NULL(p, code, lino, _err, terrno);
×
1517
        }
1518

1519
        // todo handle the snode info
UNCOV
1520
        if (pCurrent->nodeId != SNODE_HANDLE) {
×
UNCOV
1521
          SVgObj *pVgroup = mndAcquireVgroup(pMnode, pCurrent->nodeId);
×
UNCOV
1522
          code = taosHashPut(pInfo->pDBMap, pVgroup->dbName, strlen(pVgroup->dbName), NULL, 0);
×
UNCOV
1523
          mndReleaseVgroup(pMnode, pVgroup);
×
UNCOV
1524
          TSDB_CHECK_CODE(code, lino, _err);
×
1525
        }
1526

UNCOV
1527
        break;
×
1528
      }
1529
    }
1530
  }
1531

UNCOV
1532
  return code;
×
1533

1534
_err:
×
1535
  mError("failed to find node change info, code:%s at %s line:%d", tstrerror(code), __func__, lino);
×
1536
  mndDestroyVgroupChangeInfo(pInfo);
×
1537
  return code;
×
1538
}
1539

1540
static int32_t doCheckForUpdated(SMnode *pMnode, SArray **ppNodeSnapshot) {
50✔
1541
  bool              allReady = false;
50✔
1542
  bool              nodeUpdated = false;
50✔
1543
  SVgroupChangeInfo changeInfo = {0};
50✔
1544

1545
  int32_t numOfNodes = extractStreamNodeList(pMnode);
50✔
1546

1547
  if (numOfNodes == 0) {
50!
1548
    mDebug("stream task node change checking done, no vgroups exist, do nothing");
50✔
1549
    execInfo.ts = taosGetTimestampSec();
50✔
1550
    return false;
50✔
1551
  }
1552

UNCOV
1553
  for (int32_t i = 0; i < numOfNodes; ++i) {
×
UNCOV
1554
    SNodeEntry *pNodeEntry = taosArrayGet(execInfo.pNodeList, i);
×
UNCOV
1555
    if (pNodeEntry == NULL) {
×
1556
      continue;
×
1557
    }
1558

UNCOV
1559
    if (pNodeEntry->stageUpdated) {
×
UNCOV
1560
      mDebug("stream task not ready due to node update detected, checkpoint not issued");
×
UNCOV
1561
      return true;
×
1562
    }
1563
  }
1564

UNCOV
1565
  int32_t code = mndTakeVgroupSnapshot(pMnode, &allReady, ppNodeSnapshot);
×
UNCOV
1566
  if (code) {
×
1567
    mError("failed to get the vgroup snapshot, ignore it and continue");
×
1568
  }
1569

UNCOV
1570
  if (!allReady) {
×
UNCOV
1571
    mWarn("not all vnodes ready, quit from vnodes status check");
×
UNCOV
1572
    return true;
×
1573
  }
1574

UNCOV
1575
  code = mndFindChangedNodeInfo(pMnode, execInfo.pNodeList, *ppNodeSnapshot, &changeInfo);
×
UNCOV
1576
  if (code) {
×
1577
    nodeUpdated = false;
×
1578
  } else {
UNCOV
1579
    nodeUpdated = (taosArrayGetSize(changeInfo.pUpdateNodeList) > 0);
×
UNCOV
1580
    if (nodeUpdated) {
×
1581
      mDebug("stream tasks not ready due to node update");
×
1582
    }
1583
  }
1584

UNCOV
1585
  mndDestroyVgroupChangeInfo(&changeInfo);
×
UNCOV
1586
  return nodeUpdated;
×
1587
}
1588

1589
// check if the node update happens or not
1590
bool mndStreamNodeIsUpdated(SMnode *pMnode) {
50✔
1591
  SArray *pNodeSnapshot = NULL;
50✔
1592

1593
  streamMutexLock(&execInfo.lock);
50✔
1594
  bool updated = doCheckForUpdated(pMnode, &pNodeSnapshot);
50✔
1595
  streamMutexUnlock(&execInfo.lock);
50✔
1596

1597
  taosArrayDestroy(pNodeSnapshot);
50✔
1598
  return updated;
50✔
1599
}
1600

UNCOV
1601
int32_t mndCheckForSnode(SMnode *pMnode, SDbObj *pSrcDb) {
×
UNCOV
1602
  SSdb      *pSdb = pMnode->pSdb;
×
UNCOV
1603
  void      *pIter = NULL;
×
UNCOV
1604
  SSnodeObj *pObj = NULL;
×
1605

UNCOV
1606
  if (pSrcDb->cfg.replications == 1) {
×
UNCOV
1607
    return TSDB_CODE_SUCCESS;
×
1608
  } else {
1609
    while (1) {
UNCOV
1610
      pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
UNCOV
1611
      if (pIter == NULL) {
×
UNCOV
1612
        break;
×
1613
      }
1614

UNCOV
1615
      sdbRelease(pSdb, pObj);
×
UNCOV
1616
      sdbCancelFetch(pSdb, pIter);
×
UNCOV
1617
      return TSDB_CODE_SUCCESS;
×
1618
    }
1619

UNCOV
1620
    mError("snode not existed when trying to create stream in db with multiple replica");
×
UNCOV
1621
    return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
1622
  }
1623
}
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