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

taosdata / TDengine / #5053

13 May 2026 12:00PM UTC coverage: 73.397% (+0.06%) from 73.338%
#5053

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

627 existing lines in 131 files now uncovered.

281694 of 383795 relevant lines covered (73.4%)

132505311.38 hits per line

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

80.47
/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
#include "mndSnode.h"
24

25
bool mstWaitLock(SRWLatch* pLock, bool readLock) {
38,722,627✔
26
  if (readLock) {
38,722,627✔
27
    while (taosRTryLockLatch(pLock)) {
34,474,663✔
28
      taosMsleep(1);
782,955✔
29
    }
30

31
    return true;
33,691,708✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
5,030,919✔
35

36
  return true;
5,030,919✔
37
}
38

39
void mstDestroySStmVgStreamStatus(void* p) { 
546,918✔
40
  SStmVgStreamStatus* pStatus = (SStmVgStreamStatus*)p;
546,918✔
41
  taosArrayDestroy(pStatus->trigReaders); 
546,918✔
42
  taosArrayDestroy(pStatus->calcReaders); 
546,918✔
43
}
546,918✔
44

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
254,271✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
254,271✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,017,084✔
48
    taosArrayDestroy(pStatus->runners[i]);
762,813✔
49
    pStatus->runners[i] = NULL;
762,813✔
50
  }
51
}
254,271✔
52

53

54
void mstDestroyVgroupStatus(SStmVgroupStatus* pVgStatus) {
×
55
  taosHashCleanup(pVgStatus->streamTasks);
×
56
  pVgStatus->streamTasks = NULL;
×
57
}
×
58

59
void mstDestroySStmTaskToDeployExt(void* param) {
1,879,254✔
60
  SStmTaskToDeployExt* pExt = (SStmTaskToDeployExt*)param;
1,879,254✔
61
  if (pExt->deployed) {
1,879,254✔
62
    return;
1,877,508✔
63
  }
64
  
65
  switch (pExt->deploy.task.type) {
1,746✔
66
    case STREAM_TRIGGER_TASK:
291✔
67
      taosArrayDestroy(pExt->deploy.msg.trigger.readerList);
291✔
68
      pExt->deploy.msg.trigger.readerList = NULL;
291✔
69
      taosArrayDestroy(pExt->deploy.msg.trigger.runnerList);
291✔
70
      pExt->deploy.msg.trigger.runnerList = NULL;
291✔
71
      break;
291✔
72
    case STREAM_RUNNER_TASK:
873✔
73
      taosMemoryFreeClear(pExt->deploy.msg.runner.pPlan);
873✔
74
      break;
873✔
75
    case STREAM_READER_TASK:
582✔
76
      if (!pExt->deploy.msg.reader.triggerReader) {
582✔
77
        SStreamReaderDeployFromCalc* pCalcReaderDeploy = &pExt->deploy.msg.reader.msg.calc;
291✔
78
        if (pCalcReaderDeploy->freeScanPlan) {
291✔
79
          taosMemoryFreeClear(pCalcReaderDeploy->calcScanPlan);
291✔
80
        }
81
      }
82
      break;
582✔
83
    default:  
×
84
      break;
×
85
  }
86
}
87

88
void mstDestroyScanAddrList(void* param) {
491,911✔
89
  if (NULL == param) {
491,911✔
90
    return;
×
91
  }
92
  SArray* pList = *(SArray**)param;
491,911✔
93
  taosArrayDestroy(pList);
491,911✔
94
}
95

96
void mstDestroySStmSnodeTasksDeploy(void* param) {
80,397✔
97
  SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)param;
80,397✔
98
  taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
80,397✔
99
  taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
80,397✔
100
}
80,397✔
101

102
void mstDestroySStmVgTasksToDeploy(void* param) {
187,720✔
103
  SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)param;
187,720✔
104
  taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
187,720✔
105
}
187,720✔
106

107
void mstDestroySStmSnodeStatus(void* param) {
43,304✔
108
  SStmSnodeStatus* pSnode = (SStmSnodeStatus*)param;
43,304✔
109
  taosHashCleanup(pSnode->streamTasks);
43,304✔
110
}
43,304✔
111

112
void mstDestroySStmVgroupStatus(void* param) {
106,509✔
113
  SStmVgroupStatus* pVg = (SStmVgroupStatus*)param;
106,509✔
114
  taosHashCleanup(pVg->streamTasks);
106,509✔
115
}
106,509✔
116

117
void mstFreeTrigOReaderList(void* param) {
52,663✔
118
  SArray** ppList = (SArray**)param;
52,663✔
119
  taosArrayDestroy(*ppList);
52,663✔
120
}
52,663✔
121

122
void mstResetSStmStatus(SStmStatus* pStatus) {
245,272✔
123
  (void)mstWaitLock(&pStatus->resetLock, false);
245,272✔
124

125
  taosArrayDestroy(pStatus->trigReaders);
245,272✔
126
  pStatus->trigReaders = NULL;
245,272✔
127
  taosArrayDestroyEx(pStatus->trigOReaders, mstFreeTrigOReaderList);
245,272✔
128
  pStatus->trigOReaders = NULL;
245,272✔
129
  pStatus->calcReaders = tdListFree(pStatus->calcReaders);
245,272✔
130
  if (pStatus->triggerTask) {
245,272✔
131
    (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, false);
244,600✔
132
    taosMemoryFreeClear(pStatus->triggerTask->detailStatus);
244,600✔
133
    taosWUnLockLatch(&pStatus->triggerTask->detailStatusLock);
244,600✔
134
  }
135
  taosMemoryFreeClear(pStatus->triggerTask);
245,272✔
136
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
981,088✔
137
    taosArrayDestroy(pStatus->runners[i]);
735,816✔
138
    pStatus->runners[i] = NULL;
735,816✔
139
  }
140
  pStatus->lastTrigMgmtReqId = 0;
245,272✔
141

142
  taosWUnLockLatch(&pStatus->resetLock);
245,272✔
143
}
245,272✔
144

145
void mstDestroySStmStatus(void* param) {
236,574✔
146
  SStmStatus* pStatus = (SStmStatus*)param;
236,574✔
147
  taosMemoryFreeClear(pStatus->streamName);
236,574✔
148

149
  mstResetSStmStatus(pStatus);
236,574✔
150

151
  taosWLockLatch(&pStatus->userRecalcLock);
236,574✔
152
  taosArrayDestroy(pStatus->userRecalcList);
236,574✔
153
  taosWUnLockLatch(&pStatus->userRecalcLock);
236,574✔
154

155
  tFreeSCMCreateStreamReq(pStatus->pCreate);
236,574✔
156
  taosMemoryFreeClear(pStatus->pCreate);  
236,574✔
157
}
236,574✔
158

159
void mstDestroySStmAction(void* param) {
300,621✔
160
  SStmAction* pAction = (SStmAction*)param;
300,621✔
161

162
  taosArrayDestroy(pAction->undeploy.taskList);
300,621✔
163
  taosArrayDestroy(pAction->recalc.recalcList);
300,621✔
164
}
300,621✔
165

166
void mstClearSStmStreamDeploy(SStmStreamDeploy* pDeploy) {
318,374✔
167
  pDeploy->readerTasks = NULL;
318,374✔
168
  pDeploy->triggerTask = NULL;
318,374✔
169
  pDeploy->runnerTasks = NULL;
318,374✔
170
}
318,374✔
171

172
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
298,796✔
173
  SSdb   *pSdb = pMnode->pSdb;
298,796✔
174
  void   *pIter = NULL;
298,796✔
175
  
176
  while (1) {
632,168✔
177
    SStreamObj *pStream = NULL;
930,964✔
178
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
930,964✔
179
    if (pIter == NULL) break;
930,964✔
180

181
    if (pStream->pCreate->streamId == streamId) {
698,485✔
182
      *dropped = pStream->userDropped ? true : false;
66,317✔
183
      sdbRelease(pSdb, pStream);
66,317✔
184
      sdbCancelFetch(pSdb, pIter);
66,317✔
185
      mstsDebug("stream found, dropped:%d", *dropped);
66,317✔
186
      return TSDB_CODE_SUCCESS;
66,317✔
187
    }
188
    
189
    sdbRelease(pSdb, pStream);
632,168✔
190
  }
191

192
  *dropped = true;
232,479✔
193

194
  return TSDB_CODE_SUCCESS;
232,479✔
195
}
196

197
typedef struct SStmCheckDbInUseCtx {
198
  bool* dbStream;
199
  bool* vtableStream;
200
  bool  ignoreCurrDb;
201
} SStmCheckDbInUseCtx;
202

203
static bool mstChkSetDbInUse(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
45,396✔
204
  SStreamObj *pStream = pObj;
45,396✔
205
  if (atomic_load_8(&pStream->userDropped)) {
45,396✔
206
    return true;
×
207
  }
208

209
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
45,396✔
210
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
45,396✔
211
    return true;
22,241✔
212
  }
213
  
214
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
23,155✔
215
    *pCtx->dbStream = true;
797✔
216
    return false;
797✔
217
  }
218

219
  int32_t calcDBNum = taosArrayGetSize(pStream->pCreate->calcDB);
22,358✔
220
  for (int32_t i = 0; i < calcDBNum; ++i) {
44,164✔
221
    char* calcDB = taosArrayGetP(pStream->pCreate->calcDB, i);
22,358✔
222
    if (0 == strcmp(calcDB, p1)) {
22,358✔
223
      *pCtx->dbStream = true;
552✔
224
      return false;
552✔
225
    }
226
  }
227

228
  if (pStream->pCreate->vtableCalc || STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
21,806✔
229
    *pCtx->vtableStream = true;
12,079✔
230
    return true;
12,079✔
231
  }
232
  
233
  return true;
9,727✔
234
}
235

236
void mstCheckDbInUse(SMnode *pMnode, char *dbFName, bool *dbStream, bool *vtableStream, bool ignoreCurrDb) {
850,841✔
237
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
850,841✔
238
  if (streamNum <= 0) {
850,841✔
239
    return;
839,651✔
240
  }
241

242
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
11,190✔
243
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
11,190✔
244
}
245

246
static void mstShowStreamStatus(char *dst, int8_t status, int32_t bufLen) {
×
247
  if (status == STREAM_STATUS_INIT) {
×
248
    tstrncpy(dst, "init", bufLen);
×
249
  } else if (status == STREAM_STATUS_RUNNING) {
×
250
    tstrncpy(dst, "running", bufLen);
×
251
  } else if (status == STREAM_STATUS_STOPPED) {
×
252
    tstrncpy(dst, "stopped", bufLen);
×
253
  } else if (status == STREAM_STATUS_FAILED) {
×
254
    tstrncpy(dst, "failed", bufLen);
×
255
  }
256
}
×
257

258
int32_t mstCheckSnodeExists(SMnode *pMnode) {
×
259
  SSdb      *pSdb = pMnode->pSdb;
×
260
  void      *pIter = NULL;
×
261
  SSnodeObj *pObj = NULL;
×
262

263
  while (1) {
264
    pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
265
    if (pIter == NULL) {
×
266
      break;
×
267
    }
268

269
    sdbRelease(pSdb, pObj);
×
270
    sdbCancelFetch(pSdb, pIter);
×
271
    return TSDB_CODE_SUCCESS;
×
272
  }
273

274
  return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
275
}
276

277
void mstSetTaskStatusFromMsg(SStmGrpCtx* pCtx, SStmTaskStatus* pTask, SStmTaskStatusMsg* pMsg) {
×
278
  pTask->id.taskId = pMsg->taskId;
×
279
  pTask->id.deployId = pMsg->deployId;
×
280
  pTask->id.seriousId = pMsg->seriousId;
×
281
  pTask->id.nodeId = pMsg->nodeId;
×
282
  pTask->id.taskIdx = pMsg->taskIdx;
×
283

284
  pTask->type = pMsg->type;
×
285
  pTask->flags = pMsg->flags;
×
286
  pTask->status = pMsg->status;
×
287
  pTask->lastUpTs = pCtx->currTs;
×
288
}
×
289

290
bool mndStreamActionDequeue(SStmActionQ* pQueue, SStmQNode **param) {
820,130✔
291
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
820,130✔
292
    return false;
566,178✔
293
  }
294

295
  SStmQNode *orig = pQueue->head;
253,952✔
296

297
  SStmQNode *node = pQueue->head->next;
253,952✔
298
  pQueue->head = pQueue->head->next;
253,952✔
299

300
  *param = node;
253,952✔
301

302
  taosMemoryFreeClear(orig);
253,952✔
303

304
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
253,952✔
305

306
  return true;
253,952✔
307
}
308

309
void mndStreamActionEnqueue(SStmActionQ* pQueue, SStmQNode* param) {
253,952✔
310
  taosWLockLatch(&pQueue->lock);
253,952✔
311
  pQueue->tail->next = param;
253,952✔
312
  pQueue->tail = param;
253,952✔
313
  taosWUnLockLatch(&pQueue->lock);
253,952✔
314

315
  (void)atomic_add_fetch_64(&pQueue->qRemainNum, 1);
253,952✔
316
}
253,952✔
317

318
char* mstGetStreamActionString(int32_t action) {
226,211✔
319
  switch (action) {
226,211✔
320
    case STREAM_ACT_DEPLOY:
226,211✔
321
      return "DEPLOY";
226,211✔
322
    case STREAM_ACT_UNDEPLOY:
×
323
      return "UNDEPLOY";
×
324
    case STREAM_ACT_START:
×
325
      return "START";
×
326
    case STREAM_ACT_UPDATE_TRIGGER:
×
327
      return "UPDATE TRIGGER";
×
328
    case STREAM_ACT_RECALC:
×
329
      return "USER RECALC";
×
330
    default:
×
331
      break;
×
332
  }
333

334
  return "UNKNOWN";
×
335
}
336

337
void mstPostStreamAction(SStmActionQ*       actionQ, int64_t streamId, char* streamName, void* param, bool userAction, int32_t action) {
251,915✔
338
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
251,915✔
339
  if (NULL == pNode) {
251,915✔
340
    taosMemoryFreeClear(param);
×
341
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
342
    return;
×
343
  }
344

345
  pNode->type = action;
251,915✔
346
  pNode->streamAct = true;
251,915✔
347
  pNode->action.stream.streamId = streamId;
251,915✔
348
  tstrncpy(pNode->action.stream.streamName, streamName, sizeof(pNode->action.stream.streamName));
251,915✔
349
  pNode->action.stream.userAction = userAction;
251,915✔
350
  pNode->action.stream.actionParam = param;
251,915✔
351
  
352
  pNode->next = NULL;
251,915✔
353

354
  mndStreamActionEnqueue(actionQ, pNode);
251,915✔
355

356
  mstsDebug("stream action %s posted enqueue", mstGetStreamActionString(action));
251,915✔
357
}
358

359
void mstPostTaskAction(SStmActionQ*        actionQ, SStmTaskAction* pAction, int32_t action) {
2,037✔
360
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
2,037✔
361
  if (NULL == pNode) {
2,037✔
362
    int64_t streamId = pAction->streamId;
×
363
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
364
    return;
×
365
  }
366

367
  pNode->type = action;
2,037✔
368
  pNode->streamAct = false;
2,037✔
369
  pNode->action.task = *pAction;
2,037✔
370
  
371
  pNode->next = NULL;
2,037✔
372

373
  mndStreamActionEnqueue(actionQ, pNode);
2,037✔
374
}
375

376
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
64,600✔
377
  int32_t iter = 0;
64,600✔
378
  SDBVgHashInfo* pVg = NULL;
64,600✔
379
  void* p = NULL;
64,600✔
380
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
307,312✔
381
    pVg = (SDBVgHashInfo*)p;
242,712✔
382
    taosArrayDestroy(pVg->vgArray);
242,712✔
383
  }
384
  
385
  tSimpleHashCleanup(pDbVgs);
64,600✔
386
}
64,600✔
387

388

389
int32_t mstBuildDBVgroupsMap(SMnode* pMnode, SSHashObj** ppRes) {
64,600✔
390
  void*   pIter = NULL;
64,600✔
391
  int32_t code = TSDB_CODE_SUCCESS;
64,600✔
392
  int32_t lino = 0;
64,600✔
393
  SArray* pTarget = NULL;
64,600✔
394
  SArray* pNew = NULL;
64,600✔
395
  SDbObj* pDb = NULL;
64,600✔
396
  SDBVgHashInfo dbInfo = {0}, *pDbInfo = NULL;
64,600✔
397
  SVgObj* pVgroup = NULL;
64,600✔
398

399
  SSHashObj* pDbVgroup = tSimpleHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
64,600✔
400
  TSDB_CHECK_NULL(pDbVgroup, code, lino, _exit, terrno);
64,600✔
401

402
  while (1) {
394,975✔
403
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
459,575✔
404
    if (pIter == NULL) {
459,575✔
405
      break;
64,600✔
406
    }
407

408
    pDbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1);
394,975✔
409
    if (NULL == pDbInfo) {
394,975✔
410
      pNew = taosArrayInit(20, sizeof(SVGroupHashInfo));
242,712✔
411
      if (NULL == pNew) {
242,712✔
412
        sdbRelease(pMnode->pSdb, pVgroup);
×
413
        sdbCancelFetch(pMnode->pSdb, pIter);
×
414
        pVgroup = NULL;
×
415
        TSDB_CHECK_NULL(pNew, code, lino, _exit, terrno);
×
416
      }
417
      
418
      pDb = mndAcquireDb(pMnode, pVgroup->dbName);
242,712✔
419
      if (NULL == pDb) {
242,712✔
420
        sdbRelease(pMnode->pSdb, pVgroup);
×
421
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
422
        pVgroup = NULL;
×
423
        TSDB_CHECK_NULL(pDb, code, lino, _exit, terrno);
×
424
      }
425
      dbInfo.vgSorted = false;
242,712✔
426
      dbInfo.hashMethod = pDb->cfg.hashMethod;
242,712✔
427
      dbInfo.hashPrefix = pDb->cfg.hashPrefix;
242,712✔
428
      dbInfo.hashSuffix = pDb->cfg.hashSuffix;
242,712✔
429
      dbInfo.vgArray = pNew;
242,712✔
430
      
431
      mndReleaseDb(pMnode, pDb);
242,712✔
432

433
      pTarget = pNew;
242,712✔
434
    } else {
435
      pTarget = pDbInfo->vgArray;
152,263✔
436
    }
437

438
    SVGroupHashInfo vgInfo = {.vgId = pVgroup->vgId, .hashBegin = pVgroup->hashBegin, .hashEnd = pVgroup->hashEnd};
394,975✔
439
    if (NULL == taosArrayPush(pTarget, &vgInfo)) {
394,975✔
440
      sdbRelease(pMnode->pSdb, pVgroup);
×
441
      sdbCancelFetch(pMnode->pSdb, pIter);      
×
442
      pVgroup = NULL;
×
443
      TSDB_CHECK_NULL(NULL, code, lino, _exit, terrno);
×
444
    }
445

446
    if (NULL == pDbInfo) {
394,975✔
447
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
242,712✔
448
      if (code) {
242,712✔
449
        sdbRelease(pMnode->pSdb, pVgroup);
×
450
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
451
        pVgroup = NULL;
×
452
        TAOS_CHECK_EXIT(code);
×
453
      }
454
      pNew = NULL;
242,712✔
455
    }
456

457
    sdbRelease(pMnode->pSdb, pVgroup);
394,975✔
458
    pVgroup = NULL;
394,975✔
459
  }
460

461
  *ppRes = pDbVgroup;
64,600✔
462
  
463
_exit:
64,600✔
464

465
  taosArrayDestroy(pNew);
64,600✔
466

467
  if (code) {
64,600✔
468
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
469
  }
470

471
  return code;
64,600✔
472
}
473

474
int mstDbVgInfoComp(const void* lp, const void* rp) {
108,660✔
475
  SVGroupHashInfo* pLeft = (SVGroupHashInfo*)lp;
108,660✔
476
  SVGroupHashInfo* pRight = (SVGroupHashInfo*)rp;
108,660✔
477
  if (pLeft->hashBegin < pRight->hashBegin) {
108,660✔
478
    return -1;
108,660✔
479
  } else if (pLeft->hashBegin > pRight->hashBegin) {
×
480
    return 1;
×
481
  }
482

483
  return 0;
×
484
}
485

486
int32_t mstTableHashValueComp(void const* lp, void const* rp) {
252,881✔
487
  uint32_t*    key = (uint32_t*)lp;
252,881✔
488
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
252,881✔
489

490
  if (*key < pVg->hashBegin) {
252,881✔
491
    return -1;
×
492
  } else if (*key > pVg->hashEnd) {
252,881✔
493
    return 1;
67,436✔
494
  }
495

496
  return 0;
185,445✔
497
}
498

499

500
int32_t mstGetTableVgId(SSHashObj* pDbVgroups, char* dbFName, char *tbName, int32_t* vgId) {
185,445✔
501
  int32_t code = 0;
185,445✔
502
  int32_t lino = 0;
185,445✔
503
  SVgroupInfo* vgInfo = NULL;
185,445✔
504
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
185,445✔
505

506
  SDBVgHashInfo* dbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroups, dbFName, strlen(dbFName) + 1);
185,445✔
507
  if (NULL == dbInfo) {
185,445✔
508
    mstError("db %s does not exist", dbFName);
×
509
    TAOS_CHECK_EXIT(TSDB_CODE_MND_DB_NOT_EXIST);
×
510
  }
511
  
512
  (void)snprintf(tbFullName, sizeof(tbFullName), "%s.%s", dbFName, tbName);
185,445✔
513
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
370,890✔
514
                                        dbInfo->hashPrefix, dbInfo->hashSuffix);
185,445✔
515

516
  if (!dbInfo->vgSorted) {
185,445✔
517
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
64,875✔
518
    dbInfo->vgSorted = true;
64,875✔
519
  }
520

521
  vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, mstTableHashValueComp, TD_EQ);
185,445✔
522
  if (NULL == vgInfo) {
185,445✔
523
    mstError("no hash range found for hash value [%u], dbFName:%s, numOfVgId:%d", hashValue, dbFName,
×
524
             (int32_t)taosArrayGetSize(dbInfo->vgArray));
525
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
526
  }
527

528
  *vgId = vgInfo->vgId;
185,445✔
529

530
_exit:
185,445✔
531

532
  if (code) {
185,445✔
533
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
534
  }
535

536
  return code;
185,445✔
537
}
538

539

540
void mstLogSStreamObj(char* tips, SStreamObj* p) {
242,485✔
541
  if (!(stDebugFlag & DEBUG_DEBUG)) {
242,485✔
542
    return;
25,428✔
543
  }
544
  
545
  if (NULL == p) {
217,057✔
546
    mstDebug("%s: stream is NULL", tips);
×
547
    return;
×
548
  }
549

550
  mstDebug("%s: stream obj", tips);
217,057✔
551
  mstDebug("name:%s mainSnodeId:%d userDropped:%d userStopped:%d createTime:%" PRId64 " updateTime:%" PRId64,
217,057✔
552
      p->name, p->mainSnodeId, p->userDropped, p->userStopped, p->createTime, p->updateTime);
553

554
  SCMCreateStreamReq* q = p->pCreate;
217,057✔
555
  if (NULL == q) {
217,057✔
556
    mstDebug("stream pCreate is NULL");
×
557
    return;
×
558
  }
559

560
  int64_t streamId = q->streamId;
217,057✔
561
  int32_t calcDBNum = taosArrayGetSize(q->calcDB);
217,057✔
562
  int32_t calcScanNum = taosArrayGetSize(q->calcScanPlanList);
217,057✔
563
  int32_t notifyUrlNum = taosArrayGetSize(q->pNotifyAddrUrls);
217,057✔
564
  int32_t outColNum = taosArrayGetSize(q->outCols);
217,057✔
565
  int32_t outTagNum = taosArrayGetSize(q->outTags);
217,057✔
566
  int32_t forceOutColNum = taosArrayGetSize(q->forceOutCols);
217,057✔
567

568
  mstsDebugL("create_info: name:%s sql:%s streamDB:%s triggerDB:%s outDB:%s calcDBNum:%d triggerTblName:%s outTblName:%s "
217,057✔
569
      "igExists:%d triggerType:%d igDisorder:%d deleteReCalc:%d deleteOutTbl:%d fillHistory:%d fillHistroyFirst:%d "
570
      "calcNotifyOnly:%d lowLatencyCalc:%d igNoDataTrigger:%d enableMultiGroupCalc:%d notifyUrlNum:%d notifyEventTypes:%d addOptions:%d notifyHistory:%d "
571
      "outColsNum:%d outTagsNum:%d maxDelay:%" PRId64 " fillHistoryStartTs:%" PRId64 " watermark:%" PRId64 " expiredTime:%" PRId64 " "
572
      "triggerTblType:%d triggerTblUid:%" PRIx64 " triggerTblSuid:%" PRIx64 " vtableCalc:%d outTblType:%d outStbExists:%d outStbUid:%" PRIu64 " outStbSversion:%d "
573
      "eventTypes:0x%" PRIx64 " flags:0x%" PRIx64 " tsmaId:0x%" PRIx64 " placeHolderBitmap:0x%" PRIx64 " calcTsSlotId:%d triTsSlotId:%d calcPkSlotId:%d triPkSlotId:%d "
574
      "triggerTblVgId:%d outTblVgId:%d calcScanPlanNum:%d forceOutCols:%d idleTimeoutMs:%" PRId64,
575
      q->name, q->sql, q->streamDB, q->triggerDB, q->outDB, calcDBNum, q->triggerTblName, q->outTblName,
576
      q->igExists, q->triggerType, q->igDisorder, q->deleteReCalc, q->deleteOutTbl, q->fillHistory, q->fillHistoryFirst,
577
      q->calcNotifyOnly, q->lowLatencyCalc, q->igNoDataTrigger, q->enableMultiGroupCalc, notifyUrlNum, q->notifyEventTypes, q->addOptions, q->notifyHistory,
578
      outColNum, outTagNum, q->maxDelay, q->fillHistoryStartTime, q->watermark, q->expiredTime,
579
      q->triggerTblType, q->triggerTblUid, q->triggerTblSuid, q->vtableCalc, q->outTblType, q->outStbExists, q->outStbUid, q->outStbSversion,
580
      q->eventTypes, q->flags, q->tsmaId, q->placeHolderBitmap, q->calcTsSlotId, q->triTsSlotId, q->calcPkSlotId, q->triPkSlotId,
581
      q->triggerTblVgId, q->outTblVgId, calcScanNum, forceOutColNum, q->idleTimeoutMs);
582

583
  switch (q->triggerType) {
217,057✔
584
    case WINDOW_TYPE_INTERVAL: {
78,406✔
585
      SSlidingTrigger* t = &q->trigger.sliding;
78,406✔
586
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
78,406✔
587
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
588
      break;
78,406✔
589
    }  
590
    case WINDOW_TYPE_SESSION: {
8,348✔
591
      SSessionTrigger* t = &q->trigger.session;
8,348✔
592
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
8,348✔
593
      break;
8,348✔
594
    }
595
    case WINDOW_TYPE_STATE: {
70,973✔
596
      SStateWinTrigger* t = &q->trigger.stateWin;
70,973✔
597
      int32_t           slotIdNum = t->pSlotIds ? taosArrayGetSize(t->pSlotIds) : 0;
70,973✔
598
      int16_t           firstSlotId = (slotIdNum > 0) ? *(int16_t*)taosArrayGet(t->pSlotIds, 0) : -1;
70,973✔
599
      mstsDebug(
70,973✔
600
          "state trigger options, slotIdNum:%d, firstSlotId:%d, expr:%s, extend:%d, zeroth:%s, trueForType: %d, trueForCount: %d, "
601
          "trueForDuration:%" PRId64,
602
          slotIdNum, firstSlotId, (char*)t->expr, t->extend, (char*)t->zeroth, t->trueForType, t->trueForCount,
603
          t->trueForDuration);
604
      break;
70,973✔
605
    }
606
    case WINDOW_TYPE_EVENT:{
23,057✔
607
      SEventTrigger* t = &q->trigger.event;
23,057✔
608
      mstsDebug(
23,057✔
609
          "event trigger options, startCond:%s, endCond:%s, trueForType: %d, trueForCount: %d, "
610
          "trueForDuration:%" PRId64,
611
          (char*)t->startCond, (char*)t->endCond, t->trueForType, t->trueForCount, t->trueForDuration);
612
      break;
23,057✔
613
    }
614
    case WINDOW_TYPE_COUNT: {
25,076✔
615
      SCountTrigger* t = &q->trigger.count;
25,076✔
616
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
25,076✔
617
      break;
25,076✔
618
    }
619
    case WINDOW_TYPE_PERIOD: {
11,197✔
620
      SPeriodTrigger* t = &q->trigger.period;
11,197✔
621
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
11,197✔
622
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
623
      break;
11,197✔
624
    }
625
    default:
×
626
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
627
      break;
×
628
  }
629

630
  mstsDebugL("create_info: triggerCols:[%s]", (char*)q->triggerCols);
217,057✔
631

632
  mstsDebugL("create_info: partitionCols:[%s]", (char*)q->partitionCols);
217,057✔
633

634
  mstsDebugL("create_info: triggerScanPlan:[%s]", (char*)q->triggerScanPlan);
217,057✔
635

636
  mstsDebugL("create_info: calcPlan:[%s]", (char*)q->calcPlan);
217,057✔
637

638
  mstsDebugL("create_info: subTblNameExpr:[%s]", (char*)q->subTblNameExpr);
217,057✔
639

640
  mstsDebugL("create_info: tagValueExpr:[%s]", (char*)q->tagValueExpr);
217,057✔
641

642

643
  for (int32_t i = 0; i < calcDBNum; ++i) {
430,915✔
644
    char* dbName = taosArrayGetP(q->calcDB, i);
213,858✔
645
    mstsDebug("create_info: calcDB[%d] - %s", i, dbName);
213,858✔
646
  }
647

648
  for (int32_t i = 0; i < calcScanNum; ++i) {
697,562✔
649
    SStreamCalcScan* pScan = taosArrayGet(q->calcScanPlanList, i);
480,505✔
650
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
480,505✔
651
    mstsDebugL("create_info: calcScanPlan[%d] - readFromCache:%d vgNum:%d scanPlan:[%s]", i, pScan->readFromCache, vgNum, (char*)pScan->scanPlan);
480,505✔
652
    for (int32_t v = 0; v < vgNum; ++v) {
961,010✔
653
      mstsDebug("create_info: calcScanPlan[%d] vg[%d] - vgId:%d", i, v, *(int32_t*)taosArrayGet(pScan->vgList, v));
480,505✔
654
    }
655
  }
656

657
  for (int32_t i = 0; i < notifyUrlNum; ++i) {
269,976✔
658
    char* url = taosArrayGetP(q->pNotifyAddrUrls, i);
52,919✔
659
    mstsDebug("create_info: notifyUrl[%d] - %s", i, url);
52,919✔
660
  }
661

662
  for (int32_t i = 0; i < outColNum; ++i) {
1,079,727✔
663
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
862,670✔
664
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
862,670✔
665
        i, o->name, o->type, o->flags, o->bytes, o->compress, o->typeMod);
666
  }
667
      
668
}
669

670
void mstLogSStmTaskStatus(char* name, int64_t streamId, SStmTaskStatus* pTask, int32_t idx) {
1,678,852✔
671
  mstsDebug("%s[%d]: task %" PRIx64 " deployId:%d SID:%" PRId64 " nodeId:%d tidx:%d type:%s flags:%" PRIx64 " status:%s lastUpTs:%" PRId64, 
1,678,852✔
672
      name, idx, pTask->id.taskId, pTask->id.deployId, pTask->id.seriousId, pTask->id.nodeId, pTask->id.taskIdx,
673
      gStreamTaskTypeStr[pTask->type], pTask->flags, gStreamStatusStr[pTask->status], pTask->lastUpTs);
674
}
1,678,852✔
675

676
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
244,600✔
677
  if (!(stDebugFlag & DEBUG_DEBUG)) {
244,600✔
678
    return;
25,428✔
679
  }
680
  
681
  if (NULL == p) {
219,172✔
682
    mstsDebug("%s: stream status is NULL", tips);
×
683
    return;
×
684
  }
685

686
  int32_t trigReaderNum = taosArrayGetSize(p->trigReaders);
219,172✔
687
  int32_t trigOReaderNum = msmGetTrigOReaderSize(p->trigOReaders);
219,172✔
688
  int32_t calcReaderNum = MST_LIST_SIZE(p->calcReaders);
219,172✔
689
  int32_t triggerNum = p->triggerTask ? 1 : 0;
219,172✔
690
  int32_t runnerNum = 0;
219,172✔
691

692
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
867,091✔
693
    runnerNum += taosArrayGetSize(p->runners[i]);
647,919✔
694
  }
695

696
  mstsDebug("%s: stream status", tips);
219,172✔
697
  mstsDebug("name:%s runnerNum:%d runnerDeploys:%d runnerReplica:%d lastActionTs:%" PRId64
219,172✔
698
           " trigReaders:%d trigOReaders:%d calcReaders:%d trigger:%d runners:%d",
699
      p->streamName, p->runnerNum, p->runnerDeploys, p->runnerReplica, p->lastActionTs,
700
      trigReaderNum, trigOReaderNum, calcReaderNum, triggerNum, runnerNum);
701

702
  SStmTaskStatus* pTask = NULL;
219,172✔
703
  for (int32_t i = 0; i < trigReaderNum; ++i) {
497,652✔
704
    pTask = taosArrayGet(p->trigReaders, i);
278,480✔
705
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
278,480✔
706
  }
707

708
  for (int32_t i = 0; i < trigOReaderNum; ++i) {
219,172✔
709
    pTask = msmGetTrigOReader(p->trigOReaders, i);
×
710
    mstLogSStmTaskStatus("trigOReader task", streamId, pTask, i);
×
711
  }
712

713
  SListNode* pNode = listHead(p->calcReaders);
219,172✔
714
  for (int32_t i = 0; i < calcReaderNum; ++i) {
621,989✔
715
    pTask = (SStmTaskStatus*)pNode->data;
402,817✔
716
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
402,817✔
717
    pNode = TD_DLIST_NODE_NEXT(pNode);
402,817✔
718
  }
719

720
  if (triggerNum > 0) {
219,172✔
721
    mstLogSStmTaskStatus("trigger task", streamId, p->triggerTask, 0);
219,172✔
722
  }
723

724
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
867,091✔
725
    int32_t num = taosArrayGetSize(p->runners[i]);
647,919✔
726
    if (num <= 0) {
647,919✔
727
      continue;
×
728
    }
729
    
730
    mstsDebug("the %dth deploy runners status", i);
647,919✔
731
    for (int32_t m = 0; m < num; ++m) {
1,426,302✔
732
      pTask = taosArrayGet(p->runners[i], m);
778,383✔
733
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
778,383✔
734
    }
735
  }
736
      
737
}
738

739
bool mstEventPassIsolation(int32_t num, int32_t event) {
6,052,899✔
740
  bool ret = ((mStreamMgmt.lastTs[event].ts + num * MST_SHORT_ISOLATION_DURATION) <= mStreamMgmt.hCtx.currentTs);
6,052,899✔
741
  if (ret) {
6,052,899✔
742
    mstDebug("event %s passed %d isolation, last:%" PRId64 ", curr:%" PRId64, 
4,723,760✔
743
        gMndStreamEvent[event], num, mStreamMgmt.lastTs[event].ts, mStreamMgmt.hCtx.currentTs);
744
  }
745

746
  return ret;
6,052,899✔
747
}
748

749
bool mstEventHandledChkSet(int32_t event) {
4,723,760✔
750
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
4,723,760✔
751
    mstDebug("event %s set handled", gMndStreamEvent[event]);
254,113✔
752
    return true;
254,113✔
753
  }
754
  return false;
4,469,647✔
755
}
756

757
int32_t mstGetStreamStatusStr(SStreamObj* pStream, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
891,864✔
758
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
891,864✔
759
  if (0 == active || MND_STM_STATE_NORMAL != state) {
891,864✔
760
    mstDebug("mnode streamMgmt not in active mode, active:%d, state:%d", active, state);
210✔
761
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
210✔
762
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "Mnode may be unstable, try again later", msgSize);
210✔
763
    return TSDB_CODE_SUCCESS;
210✔
764
  }
765

766
  if (atomic_load_8(&pStream->userDropped)) {
891,654✔
767
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_DROPPING], statusSize);
×
768
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
×
769
    return TSDB_CODE_SUCCESS;
×
770
  }
771

772
  if (atomic_load_8(&pStream->userStopped)) {
891,654✔
773
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
5,773✔
774
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
5,773✔
775
    return TSDB_CODE_SUCCESS;
5,773✔
776
  }
777

778
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
885,881✔
779
  
780
  int64_t streamId = pStream->pCreate->streamId;
885,881✔
781
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
885,881✔
782
  if (NULL == pStatus) {
885,881✔
783
    mstDebug("return Undeployed: stream not in streamMap (show streams)");
182,369✔
784
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
182,369✔
785
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
182,369✔
786
    goto _exit;
182,369✔
787
  }
788

789
  char tmpBuf[256];
703,512✔
790
  int8_t stopped = atomic_load_8(&pStatus->stopped);
703,512✔
791
  switch (stopped) {
703,512✔
792
    case 1:
28,437✔
793
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
28,437✔
794
      snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s, Failed times: %" PRId64, tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes);
28,437✔
795
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
28,437✔
796
      goto _exit;
28,437✔
797
      break;
798
    case 4:
×
799
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
×
800
      snprintf(tmpBuf, sizeof(tmpBuf), "Error: %s", tstrerror(TSDB_CODE_GRANT_STREAM_EXPIRED));
×
801
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
802
      goto _exit;
×
803
      break;
804
    default:
675,075✔
805
      break;
675,075✔
806
  }
807

808
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
675,075✔
809
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_RUNNING], statusSize);
398,348✔
810
    tstrncpy(tmpBuf, "Running start from: ", sizeof(tmpBuf));
398,348✔
811
    int32_t offset = strlen(tmpBuf);
398,348✔
812
    (void)formatTimestampLocal(tmpBuf + offset, sizeof(tmpBuf) - offset, pStatus->triggerTask->runningStartTs,
398,348✔
813
                               TSDB_TIME_PRECISION_MILLI);
814
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
398,348✔
815
    goto _exit;
398,348✔
816
  }
817

818
  mstsDebug("return Idle: pStatus=%p triggerTask=%p triggerStatus=%d (show streams view)",
276,727✔
819
      (void*)pStatus, (void*)(pStatus ? pStatus->triggerTask : NULL),
820
      (pStatus && pStatus->triggerTask) ? (int)pStatus->triggerTask->status : -1);
821
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_INIT], statusSize);
276,727✔
822
  snprintf(tmpBuf, sizeof(tmpBuf), "Current deploy times: %" PRId64, pStatus->deployTimes);
276,727✔
823
  STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
276,727✔
824
  goto _exit;
276,727✔
825

826
_exit:
885,881✔
827
  
828
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
885,881✔
829

830
  return TSDB_CODE_SUCCESS;
885,881✔
831
}
832

833
int32_t mstSetStreamAttrResBlock(SMnode *pMnode, SStreamObj* pStream, SSDataBlock* pBlock, int32_t numOfRows) {
891,864✔
834
  int32_t code = 0;
891,864✔
835
  int32_t cols = 0;
891,864✔
836
  int32_t lino = 0;
891,864✔
837

838
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
891,864✔
839
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
891,864✔
840
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
841
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
842

843
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
891,864✔
844
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
845

846
  // db_name
847
  char streamDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
891,864✔
848
  STR_WITH_MAXSIZE_TO_VARSTR(streamDB, mndGetDbStr(pStream->pCreate->streamDB), sizeof(streamDB));
891,864✔
849
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
850
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
851
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&streamDB, false);
891,864✔
852
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
853

854
  // create time
855
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
856
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
857
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->createTime, false);
891,864✔
858
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
859

860
  // stream id
861
  char streamId2[19] = {0};
891,864✔
862
  char streamId[19 + VARSTR_HEADER_SIZE] = {0};
891,864✔
863
  snprintf(streamId2, sizeof(streamId2), "%" PRIx64, pStream->pCreate->streamId);
891,864✔
864
  STR_WITH_MAXSIZE_TO_VARSTR(streamId, streamId2, sizeof(streamId));
891,864✔
865
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
866
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
867
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamId, false);
891,864✔
868
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
869

870
  // sql
871
  char sql[TSDB_INS_STREAM_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
891,864✔
872
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->pCreate->sql, sizeof(sql));
891,864✔
873
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
874
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
875
  code = colDataSetVal(pColInfo, numOfRows, (const char*)sql, false);
891,864✔
876
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
877

878
  // status
879
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
891,864✔
880
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
891,864✔
881
  code = mstGetStreamStatusStr(pStream, status, sizeof(status), msg, sizeof(msg));
891,864✔
882
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
883

884
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
885
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
886
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
891,864✔
887
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
888

889
  // snodeLeader
890
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
891
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
892
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->mainSnodeId, false);
891,864✔
893
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
894

895
  // snodeReplica
896
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
897
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
898
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, pStream->mainSnodeId);
891,864✔
899
  int32_t replicaSnodeId = pSnode ? pSnode->replicaId : -1;
891,864✔
900
  mndReleaseSnode(pMnode, pSnode);
891,864✔
901
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&replicaSnodeId, false);
891,864✔
902
  TSDB_CHECK_CODE(code, lino, _end);
891,864✔
903

904
  // msg
905
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
891,864✔
906
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
891,864✔
907
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
891,864✔
908

909
_end:
891,864✔
910
  if (code) {
891,864✔
911
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
912
  }
913
  return code;
891,864✔
914
}
915

916

917
int32_t mstGetTaskStatusStr(SStmTaskStatus* pTask, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
25,520,832✔
918
  char tmpBuf[256];
25,497,396✔
919
  
920
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[pTask->status], statusSize);
25,520,832✔
921
  if (STREAM_STATUS_FAILED == pTask->status && pTask->errCode) {
25,520,832✔
922
    snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s", tstrerror(pTask->errCode));
245✔
923
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
245✔
924
    return TSDB_CODE_SUCCESS;
245✔
925
  }
926

927
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
25,520,587✔
928
    if (pTask->detailStatus) {
4,056,956✔
929
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
3,432,614✔
930
      snprintf(tmpBuf, sizeof(tmpBuf), "Current RT/HI/RE session num: %d/%d/%d, histroy progress:%d%%, total AUTO/USER recalc num: %d/%d", 
3,432,614✔
931
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
932
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
3,432,614✔
933
      taosRUnLockLatch(&pTask->detailStatusLock);
3,432,614✔
934
      return TSDB_CODE_SUCCESS;
3,432,614✔
935
    }
936

937
    taosRUnLockLatch(&pTask->detailStatusLock);    
624,342✔
938
  }
939
  
940
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
22,087,973✔
941
  
942
  return TSDB_CODE_SUCCESS;
22,087,973✔
943
}
944

945
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
25,520,832✔
946
  switch (pTask->type) {
25,520,832✔
947
    case STREAM_READER_TASK:
8,951,120✔
948
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
8,951,120✔
949
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
4,570,278✔
950
      } else {
951
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
4,380,842✔
952
      }
953
      return TSDB_CODE_SUCCESS;
8,951,120✔
954
    case STREAM_RUNNER_TASK:
12,512,756✔
955
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
12,512,756✔
956
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
12,249,120✔
957
        return TSDB_CODE_SUCCESS;
12,249,120✔
958
      }
959
      break;
263,636✔
960
    default:
4,056,956✔
961
      break;
4,056,956✔
962
  }
963

964
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
4,320,592✔
965
  return TSDB_CODE_SUCCESS;
4,320,592✔
966
}
967

968

969
int32_t mstSetStreamTaskResBlock(SStreamObj* pStream, SStmTaskStatus* pTask, SSDataBlock* pBlock, int32_t numOfRows) {
25,520,832✔
970
  int32_t code = 0;
25,520,832✔
971
  int32_t cols = 0;
25,520,832✔
972
  int32_t lino = 0;
25,520,832✔
973

974
  // stream_name
975
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
976
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
25,520,832✔
977
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
978
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
979

980
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
25,520,832✔
981
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
982

983
  // stream id
984
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
985
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
25,520,832✔
986
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE])); 
25,520,832✔
987
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
988
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
989
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
25,520,832✔
990
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
991

992
  // task id
993
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.taskId);
25,520,832✔
994
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
25,520,832✔
995
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
996
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
997
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
25,520,832✔
998
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
999

1000
  // type
1001
  char type[20 + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
1002
  STR_WITH_MAXSIZE_TO_VARSTR(type, (STREAM_READER_TASK == pTask->type) ? "Reader" : ((STREAM_TRIGGER_TASK == pTask->type) ? "Trigger" : "Runner"), sizeof(type));
25,520,832✔
1003
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1004
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1005
  code = colDataSetVal(pColInfo, numOfRows, (const char*)type, false);
25,520,832✔
1006
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1007

1008
  // serious id
1009
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.seriousId);
25,520,832✔
1010
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
25,520,832✔
1011
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1012
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1013
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
25,520,832✔
1014
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1015

1016
  // deploy id
1017
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1018
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1019
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.deployId, false);
25,520,832✔
1020
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1021

1022
  // node_type
1023
  char nodeType[10 + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
1024
  STR_WITH_MAXSIZE_TO_VARSTR(nodeType, (STREAM_READER_TASK == pTask->type) ? "vnode" : "snode", sizeof(nodeType));
25,520,832✔
1025
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1026
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1027
  code = colDataSetVal(pColInfo, numOfRows, (const char*)nodeType, false);
25,520,832✔
1028
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1029

1030
  // node id
1031
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1032
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1033
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.nodeId, false);
25,520,832✔
1034
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1035

1036
  // task idx
1037
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1038
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1039
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.taskIdx, false);
25,520,832✔
1040
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1041

1042
  // status
1043
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
1044
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
1045
  code = mstGetTaskStatusStr(pTask, status, sizeof(status), msg, sizeof(msg));
25,520,832✔
1046
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1047

1048
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1049
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1050
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
25,520,832✔
1051
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1052

1053
  // start time
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1056
  if (pTask->runningStartTs) {
25,520,832✔
1057
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
7,821,321✔
1058
  } else {
1059
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
17,699,511✔
1060
  }
1061
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1062

1063
  // last update
1064
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1065
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1066
  if (pTask->lastUpTs) {
25,520,832✔
1067
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, false);
25,520,832✔
1068
  } else {
UNCOV
1069
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, true);
×
1070
  }
1071
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1072

1073
  // extra info
1074
  char extra[64 + VARSTR_HEADER_SIZE] = {0};
25,520,832✔
1075
  code = mstGetTaskExtraStr(pTask, extra, sizeof(extra));
25,520,832✔
1076
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1077
  
1078
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1079
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1080
  code = colDataSetVal(pColInfo, numOfRows, (const char*)extra, false);
25,520,832✔
1081
  TSDB_CHECK_CODE(code, lino, _end);
25,520,832✔
1082

1083
  // msg
1084
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
25,520,832✔
1085
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
25,520,832✔
1086
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
25,520,832✔
1087

1088
_end:
25,520,832✔
1089
  if (code) {
25,520,832✔
1090
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1091
  }
1092
  return code;
25,520,832✔
1093
}
1094

1095
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
4,062,256✔
1096
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
4,062,256✔
1097
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
16,249,024✔
1098
    num += taosArrayGetSize(pStatus->runners[i]);
12,186,768✔
1099
  }
1100

1101
  return num;
4,062,256✔
1102
}
1103

1104
int32_t mstSetStreamTasksResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
4,303,642✔
1105
  int32_t code = 0;
4,303,642✔
1106
  int32_t lino = 0;
4,303,642✔
1107
  int64_t streamId = pStream->pCreate->streamId;
4,303,642✔
1108
  bool    statusLocked = false;
4,303,642✔
1109

1110
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
4,303,642✔
1111

1112
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
4,303,642✔
1113
  if (NULL == pStatus) {
4,303,642✔
1114
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
192,971✔
1115
    goto _exit;
192,971✔
1116
  }
1117

1118
  int8_t stopped = atomic_load_8(&pStatus->stopped);
4,110,671✔
1119
  if (stopped) {
4,110,671✔
1120
    mstsDebug("stream stopped %d, ignore it", stopped);
48,415✔
1121
    goto _exit;
48,415✔
1122
  }
1123

1124
  (void)mstWaitLock(&pStatus->resetLock, true);
4,062,256✔
1125
  statusLocked = true;
4,062,256✔
1126
  
1127
  int32_t count = mstGetNumOfStreamTasks(pStatus);
4,062,256✔
1128

1129
  if (*numOfRows + count > rowsCapacity) {
4,062,256✔
1130
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
66,521✔
1131
    if (code) {
66,521✔
1132
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1133
      TAOS_CHECK_EXIT(code);
×
1134
    }
1135
  }
1136

1137
  SStmTaskStatus* pTask = NULL;
4,062,256✔
1138
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
4,062,256✔
1139
  for (int32_t i = 0; i < trigReaderNum; ++i) {
8,424,338✔
1140
    pTask = taosArrayGet(pStatus->trigReaders, i);
4,362,082✔
1141
  
1142
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
4,362,082✔
1143
    if (code == TSDB_CODE_SUCCESS) {
4,362,082✔
1144
      (*numOfRows)++;
4,362,082✔
1145
    }
1146
  }
1147

1148
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
4,062,256✔
1149
  for (int32_t i = 0; i < trigReaderNum; ++i) {
4,270,452✔
1150
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
208,196✔
1151
  
1152
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
208,196✔
1153
    if (code == TSDB_CODE_SUCCESS) {
208,196✔
1154
      (*numOfRows)++;
208,196✔
1155
    }
1156
  }
1157

1158
  if (pStatus->calcReaders) {
4,062,256✔
1159
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
4,062,256✔
1160
    SListNode* pNode = listHead(pStatus->calcReaders);
4,062,256✔
1161
    for (int32_t i = 0; i < calcReaderNum; ++i) {
8,443,098✔
1162
      pTask = (SStmTaskStatus*)pNode->data;
4,380,842✔
1163
    
1164
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
4,380,842✔
1165
      if (code == TSDB_CODE_SUCCESS) {
4,380,842✔
1166
        (*numOfRows)++;
4,380,842✔
1167
      }
1168
      pNode = TD_DLIST_NODE_NEXT(pNode);
4,380,842✔
1169
    }
1170
  }
1171

1172
  if (pStatus->triggerTask) {
4,062,256✔
1173
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
4,056,956✔
1174
    if (code == TSDB_CODE_SUCCESS) {
4,056,956✔
1175
      (*numOfRows)++;
4,056,956✔
1176
    }
1177
  }
1178

1179
  int32_t runnerNum = 0;
4,062,256✔
1180
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
16,249,024✔
1181
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
12,186,768✔
1182
    for (int32_t m = 0; m < runnerNum; ++m) {
24,699,524✔
1183
      pTask = taosArrayGet(pStatus->runners[i], m);
12,512,756✔
1184
    
1185
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
12,512,756✔
1186
      if (code == TSDB_CODE_SUCCESS) {
12,512,756✔
1187
        (*numOfRows)++;
12,512,756✔
1188
      }
1189
    }
1190
  }
1191
  
1192
  pBlock->info.rows = *numOfRows;
4,062,256✔
1193

1194
_exit:
4,303,642✔
1195

1196
  if (statusLocked) {
4,303,642✔
1197
    taosRUnLockLatch(&pStatus->resetLock);
4,062,256✔
1198
  }
1199
  
1200
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
4,303,642✔
1201

1202
  if (code) {
4,303,642✔
1203
    mError("error happens when build stream tasks result block, lino:%d, code:%s", lino, tstrerror(code));
×
1204
  }
1205
  
1206
  return code;
4,303,642✔
1207
}
1208

1209

1210
int32_t mstAppendNewRecalcRange(int64_t streamId, SStmStatus *pStream, STimeWindow* pRange) {
4,072✔
1211
  int32_t code = 0;
4,072✔
1212
  int32_t lino = 0;
4,072✔
1213
  bool    locked = false;
4,072✔
1214
  SArray* userRecalcList = NULL;
4,072✔
1215

1216
  SStreamRecalcReq req = {.recalcId = 0, .start = pRange->skey, .end = pRange->ekey};
4,072✔
1217
  TAOS_CHECK_EXIT(taosGetSystemUUIDU64(&req.recalcId));
4,072✔
1218
  
1219
  taosWLockLatch(&pStream->userRecalcLock);
4,072✔
1220
  locked = true;
4,072✔
1221
  
1222
  if (NULL == pStream->userRecalcList) {
4,072✔
1223
    userRecalcList = taosArrayInit(2, sizeof(SStreamRecalcReq));
4,072✔
1224
    if (NULL == userRecalcList) {
4,072✔
1225
      TAOS_CHECK_EXIT(terrno);
×
1226
    }
1227

1228
    TSDB_CHECK_NULL(taosArrayPush(userRecalcList, &req), code, lino, _exit, terrno);
4,072✔
1229

1230
    atomic_store_ptr(&pStream->userRecalcList, userRecalcList);
4,072✔
1231
    userRecalcList = NULL;    
4,072✔
1232
  } else {
1233
    TSDB_CHECK_NULL(taosArrayPush(pStream->userRecalcList, &req), code, lino, _exit, terrno);
×
1234
  }
1235
  
1236
  mstsInfo("stream recalc ID:%" PRIx64 " range:%" PRId64 " - %" PRId64 " added", req.recalcId, pRange->skey, pRange->ekey);
4,072✔
1237

1238
_exit:
4,072✔
1239

1240
  taosArrayDestroy(userRecalcList);
4,072✔
1241

1242
  if (locked) {
4,072✔
1243
    taosWUnLockLatch(&pStream->userRecalcLock);
4,072✔
1244
  }
1245
  
1246
  if (code) {
4,072✔
1247
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1248
  }
1249
  
1250
  return code;
4,072✔
1251
}
1252

1253

1254

1255
int32_t mstSetStreamRecalculateResBlock(SStreamObj* pStream, SSTriggerRecalcProgress* pProgress, SSDataBlock* pBlock, int32_t numOfRows) {
×
1256
  int32_t code = 0;
×
1257
  int32_t cols = 0;
×
1258
  int32_t lino = 0;
×
1259

1260
  // stream_name
1261
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1262
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
×
1263
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1264
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1265

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

1269
  // stream id
1270
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
×
1271
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
×
1272
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE); 
×
1273
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1274
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1275
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1276
  TSDB_CHECK_CODE(code, lino, _end);
×
1277

1278
  // recalc id
1279
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pProgress->recalcId);
×
1280
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1281
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1282
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1283
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1284
  TSDB_CHECK_CODE(code, lino, _end);
×
1285

1286
  // start
1287
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1288
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1289
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->start, false);
×
1290
  TSDB_CHECK_CODE(code, lino, _end);
×
1291

1292
  // end
1293
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1294
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1295
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->end, false);
×
1296
  TSDB_CHECK_CODE(code, lino, _end);
×
1297

1298
  // progress
1299
  char progress[20 + VARSTR_HEADER_SIZE] = {0};
×
1300
  snprintf(&progress[VARSTR_HEADER_SIZE], sizeof(progress) - VARSTR_HEADER_SIZE, "%d%%", pProgress->progress);
×
1301
  varDataSetLen(progress, strlen(&progress[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1302
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1303
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1304
  code = colDataSetVal(pColInfo, numOfRows, (const char*)progress, false);
×
1305
  TSDB_CHECK_CODE(code, lino, _end);
×
1306

1307
_end:
×
1308
  if (code) {
×
1309
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1310
  }
1311
  return code;
×
1312
}
1313

1314

1315
int32_t mstSetStreamRecalculatesResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
316✔
1316
  int32_t code = 0;
316✔
1317
  int32_t lino = 0;
316✔
1318
  int64_t streamId = pStream->pCreate->streamId;
316✔
1319

1320
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
316✔
1321

1322
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
316✔
1323
  if (NULL == pStatus) {
316✔
1324
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
×
1325
    goto _exit;
×
1326
  }
1327

1328
  int8_t stopped = atomic_load_8(&pStatus->stopped);
316✔
1329
  if (stopped) {
316✔
1330
    mstsDebug("stream stopped %d, ignore it", stopped);
×
1331
    goto _exit;
×
1332
  }
1333

1334
  if (NULL == pStatus->triggerTask) {
316✔
1335
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1336
    goto _exit;
×
1337
  }
1338

1339
  (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, true);
316✔
1340
  if (NULL == pStatus->triggerTask->detailStatus) {
316✔
1341
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1342
    taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1343
    goto _exit;
×
1344
  }
1345

1346
  SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pStatus->triggerTask->detailStatus;
316✔
1347
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
316✔
1348

1349
  if (*numOfRows + count > rowsCapacity) {
316✔
1350
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
×
1351
    if (code) {
×
1352
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1353
      taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1354
      TAOS_CHECK_EXIT(code);
×
1355
    }
1356
  }
1357

1358
  for (int32_t i = 0; i < count; ++i) {
316✔
1359
    SSTriggerRecalcProgress* pProgress = taosArrayGet(pTrigger->userRecalcs, i);
×
1360
  
1361
    code = mstSetStreamRecalculateResBlock(pStream, pProgress, pBlock, *numOfRows);
×
1362
    if (code == TSDB_CODE_SUCCESS) {
×
1363
      (*numOfRows)++;
×
1364
    }
1365
  }
1366

1367
  taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
316✔
1368
  
1369
  pBlock->info.rows = *numOfRows;
316✔
1370

1371
_exit:
316✔
1372
  
1373
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
316✔
1374

1375
  if (code) {
316✔
1376
    mError("error happens when build stream recalculates result block, lino:%d, code:%s", lino, tstrerror(code));
×
1377
  }
1378
  
1379
  return code;
316✔
1380
}
1381

1382
int32_t mstGetScanUidFromPlan(int64_t streamId, void* scanPlan, int64_t* uid) {
414,655✔
1383
  SSubplan* pSubplan = NULL;
414,655✔
1384
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
414,655✔
1385
  
1386
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
414,655✔
1387

1388
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
414,655✔
1389
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
188,388✔
1390
    *uid = pScanNode->uid;
188,388✔
1391
  }
1392
  
1393
_exit:
414,181✔
1394

1395
  if (code) {
414,655✔
1396
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1397
  }
1398

1399
  nodesDestroyNode((SNode *)pSubplan);
414,655✔
1400

1401
  return code;
414,655✔
1402
}
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