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

taosdata / TDengine / #5035

24 Apr 2026 11:25AM UTC coverage: 73.06% (+0.002%) from 73.058%
#5035

push

travis-ci

web-flow
merge: from main to 3.0 branch #35224

merge: from main to 3.0 branch[manual-only]

1344 of 1975 new or added lines in 48 files covered. (68.05%)

14127 existing lines in 142 files now uncovered.

275902 of 377640 relevant lines covered (73.06%)

132208813.58 hits per line

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

79.19
/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) {
32,025,882✔
26
  if (readLock) {
32,025,882✔
27
    while (taosRTryLockLatch(pLock)) {
40,531,192✔
28
      taosMsleep(1);
13,274,875✔
29
    }
30

31
    return true;
27,254,735✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
4,771,147✔
35

36
  return true;
4,771,147✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
224,718✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
224,718✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
898,872✔
48
    taosArrayDestroy(pStatus->runners[i]);
674,154✔
49
    pStatus->runners[i] = NULL;
674,154✔
50
  }
51
}
224,718✔
52

53

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

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

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

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

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

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

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

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

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

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

142
  taosWUnLockLatch(&pStatus->resetLock);
217,410✔
143
}
217,410✔
144

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

149
  mstResetSStmStatus(pStatus);
209,539✔
150

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

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

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

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

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

172
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
267,993✔
173
  SSdb   *pSdb = pMnode->pSdb;
267,993✔
174
  void   *pIter = NULL;
267,993✔
175
  
176
  while (1) {
565,070✔
177
    SStreamObj *pStream = NULL;
833,063✔
178
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
833,063✔
179
    if (pIter == NULL) break;
833,063✔
180

181
    if (pStream->pCreate->streamId == streamId) {
627,363✔
182
      *dropped = pStream->userDropped ? true : false;
62,293✔
183
      sdbRelease(pSdb, pStream);
62,293✔
184
      sdbCancelFetch(pSdb, pIter);
62,293✔
185
      mstsDebug("stream found, dropped:%d", *dropped);
62,293✔
186
      return TSDB_CODE_SUCCESS;
62,293✔
187
    }
188
    
189
    sdbRelease(pSdb, pStream);
565,070✔
190
  }
191

192
  *dropped = true;
205,700✔
193

194
  return TSDB_CODE_SUCCESS;
205,700✔
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) {
41,009✔
204
  SStreamObj *pStream = pObj;
41,009✔
205
  if (atomic_load_8(&pStream->userDropped)) {
41,009✔
206
    return true;
×
207
  }
208

209
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
41,009✔
210
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
41,009✔
211
    return true;
20,008✔
212
  }
213
  
214
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
21,001✔
215
    *pCtx->dbStream = true;
734✔
216
    return false;
734✔
217
  }
218

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

228
  if (pStream->pCreate->vtableCalc || STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
19,759✔
229
    *pCtx->vtableStream = true;
11,122✔
230
    return true;
11,122✔
231
  }
232
  
233
  return true;
8,637✔
234
}
235

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

242
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
9,961✔
243
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
9,961✔
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) {
738,560✔
291
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
738,560✔
292
    return false;
514,215✔
293
  }
294

295
  SStmQNode *orig = pQueue->head;
224,345✔
296

297
  SStmQNode *node = pQueue->head->next;
224,345✔
298
  pQueue->head = pQueue->head->next;
224,345✔
299

300
  *param = node;
224,345✔
301

302
  taosMemoryFreeClear(orig);
224,345✔
303

304
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
224,345✔
305

306
  return true;
224,345✔
307
}
308

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

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

318
char* mstGetStreamActionString(int32_t action) {
199,023✔
319
  switch (action) {
199,023✔
320
    case STREAM_ACT_DEPLOY:
199,023✔
321
      return "DEPLOY";
199,023✔
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) {
222,733✔
338
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
222,733✔
339
  if (NULL == pNode) {
222,733✔
340
    taosMemoryFreeClear(param);
×
341
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
342
    return;
×
343
  }
344

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

354
  mndStreamActionEnqueue(actionQ, pNode);
222,733✔
355

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

359
void mstPostTaskAction(SStmActionQ*        actionQ, SStmTaskAction* pAction, int32_t action) {
1,612✔
360
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
1,612✔
361
  if (NULL == pNode) {
1,612✔
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;
1,612✔
368
  pNode->streamAct = false;
1,612✔
369
  pNode->action.task = *pAction;
1,612✔
370
  
371
  pNode->next = NULL;
1,612✔
372

373
  mndStreamActionEnqueue(actionQ, pNode);
1,612✔
374
}
375

376
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
66,541✔
377
  int32_t iter = 0;
66,541✔
378
  SDBVgHashInfo* pVg = NULL;
66,541✔
379
  void* p = NULL;
66,541✔
380
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
311,109✔
381
    pVg = (SDBVgHashInfo*)p;
244,568✔
382
    taosArrayDestroy(pVg->vgArray);
244,568✔
383
  }
384
  
385
  tSimpleHashCleanup(pDbVgs);
66,541✔
386
}
66,541✔
387

388

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

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

402
  while (1) {
405,968✔
403
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
472,509✔
404
    if (pIter == NULL) {
472,509✔
405
      break;
66,541✔
406
    }
407

408
    pDbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1);
405,968✔
409
    if (NULL == pDbInfo) {
405,968✔
410
      pNew = taosArrayInit(20, sizeof(SVGroupHashInfo));
244,568✔
411
      if (NULL == pNew) {
244,568✔
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);
244,568✔
419
      if (NULL == pDb) {
244,568✔
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;
244,568✔
426
      dbInfo.hashMethod = pDb->cfg.hashMethod;
244,568✔
427
      dbInfo.hashPrefix = pDb->cfg.hashPrefix;
244,568✔
428
      dbInfo.hashSuffix = pDb->cfg.hashSuffix;
244,568✔
429
      dbInfo.vgArray = pNew;
244,568✔
430
      
431
      mndReleaseDb(pMnode, pDb);
244,568✔
432

433
      pTarget = pNew;
244,568✔
434
    } else {
435
      pTarget = pDbInfo->vgArray;
161,400✔
436
    }
437

438
    SVGroupHashInfo vgInfo = {.vgId = pVgroup->vgId, .hashBegin = pVgroup->hashBegin, .hashEnd = pVgroup->hashEnd};
405,968✔
439
    if (NULL == taosArrayPush(pTarget, &vgInfo)) {
405,968✔
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) {
405,968✔
447
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
244,568✔
448
      if (code) {
244,568✔
449
        sdbRelease(pMnode->pSdb, pVgroup);
×
450
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
451
        pVgroup = NULL;
×
452
        TAOS_CHECK_EXIT(code);
×
453
      }
454
      pNew = NULL;
244,568✔
455
    }
456

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

461
  *ppRes = pDbVgroup;
66,541✔
462
  
463
_exit:
66,541✔
464

465
  taosArrayDestroy(pNew);
66,541✔
466

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

471
  return code;
66,541✔
472
}
473

474
int mstDbVgInfoComp(const void* lp, const void* rp) {
114,176✔
475
  SVGroupHashInfo* pLeft = (SVGroupHashInfo*)lp;
114,176✔
476
  SVGroupHashInfo* pRight = (SVGroupHashInfo*)rp;
114,176✔
477
  if (pLeft->hashBegin < pRight->hashBegin) {
114,176✔
478
    return -1;
114,176✔
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) {
266,180✔
487
  uint32_t*    key = (uint32_t*)lp;
266,180✔
488
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
266,180✔
489

490
  if (*key < pVg->hashBegin) {
266,180✔
491
    return -1;
×
492
  } else if (*key > pVg->hashEnd) {
266,180✔
493
    return 1;
71,867✔
494
  }
495

496
  return 0;
194,313✔
497
}
498

499

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

506
  SDBVgHashInfo* dbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroups, dbFName, strlen(dbFName) + 1);
194,313✔
507
  if (NULL == dbInfo) {
194,313✔
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);
194,313✔
513
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
388,626✔
514
                                        dbInfo->hashPrefix, dbInfo->hashSuffix);
194,313✔
515

516
  if (!dbInfo->vgSorted) {
194,313✔
517
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
66,794✔
518
    dbInfo->vgSorted = true;
66,794✔
519
  }
520

521
  vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, mstTableHashValueComp, TD_EQ);
194,313✔
522
  if (NULL == vgInfo) {
194,313✔
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;
194,313✔
529

530
_exit:
194,313✔
531

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

536
  return code;
194,313✔
537
}
538

539

540
void mstLogSStreamObj(char* tips, SStreamObj* p) {
213,435✔
541
  if (!(stDebugFlag & DEBUG_DEBUG)) {
213,435✔
542
    return;
23,456✔
543
  }
544
  
545
  if (NULL == p) {
189,979✔
546
    mstDebug("%s: stream is NULL", tips);
×
547
    return;
×
548
  }
549

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

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

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

568
  mstsDebugL("create_info: name:%s sql:%s streamDB:%s triggerDB:%s outDB:%s calcDBNum:%d triggerTblName:%s outTblName:%s "
189,979✔
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) {
189,979✔
584
    case WINDOW_TYPE_INTERVAL: {
74,840✔
585
      SSlidingTrigger* t = &q->trigger.sliding;
74,840✔
586
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
74,840✔
587
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
588
      break;
74,840✔
589
    }  
590
    case WINDOW_TYPE_SESSION: {
8,724✔
591
      SSessionTrigger* t = &q->trigger.session;
8,724✔
592
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
8,724✔
593
      break;
8,724✔
594
    }
595
    case WINDOW_TYPE_STATE: {
51,856✔
596
      SStateWinTrigger* t = &q->trigger.stateWin;
51,856✔
597
      mstsDebug(
51,856✔
598
          "state trigger options, slotId:%d, expr:%s, extend:%d, zeroth:%s, trueForType: %d, trueForCount: %d, "
599
          "trueForDuration:%" PRId64,
600
          t->slotId, (char*)t->expr, t->extend, (char*)t->zeroth, t->trueForType, t->trueForCount, t->trueForDuration);
601
      break;
51,856✔
602
    }
603
    case WINDOW_TYPE_EVENT:{
22,798✔
604
      SEventTrigger* t = &q->trigger.event;
22,798✔
605
      mstsDebug(
22,798✔
606
          "event trigger options, startCond:%s, endCond:%s, trueForType: %d, trueForCount: %d, "
607
          "trueForDuration:%" PRId64,
608
          (char*)t->startCond, (char*)t->endCond, t->trueForType, t->trueForCount, t->trueForDuration);
609
      break;
22,798✔
610
    }
611
    case WINDOW_TYPE_COUNT: {
20,607✔
612
      SCountTrigger* t = &q->trigger.count;
20,607✔
613
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
20,607✔
614
      break;
20,607✔
615
    }
616
    case WINDOW_TYPE_PERIOD: {
11,154✔
617
      SPeriodTrigger* t = &q->trigger.period;
11,154✔
618
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
11,154✔
619
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
620
      break;
11,154✔
621
    }
622
    default:
×
623
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
624
      break;
×
625
  }
626

627
  mstsDebugL("create_info: triggerCols:[%s]", (char*)q->triggerCols);
189,979✔
628

629
  mstsDebugL("create_info: partitionCols:[%s]", (char*)q->partitionCols);
189,979✔
630

631
  mstsDebugL("create_info: triggerScanPlan:[%s]", (char*)q->triggerScanPlan);
189,979✔
632

633
  mstsDebugL("create_info: calcPlan:[%s]", (char*)q->calcPlan);
189,979✔
634

635
  mstsDebugL("create_info: subTblNameExpr:[%s]", (char*)q->subTblNameExpr);
189,979✔
636

637
  mstsDebugL("create_info: tagValueExpr:[%s]", (char*)q->tagValueExpr);
189,979✔
638

639

640
  for (int32_t i = 0; i < calcDBNum; ++i) {
376,540✔
641
    char* dbName = taosArrayGetP(q->calcDB, i);
186,561✔
642
    mstsDebug("create_info: calcDB[%d] - %s", i, dbName);
186,561✔
643
  }
644

645
  for (int32_t i = 0; i < calcScanNum; ++i) {
646,879✔
646
    SStreamCalcScan* pScan = taosArrayGet(q->calcScanPlanList, i);
456,900✔
647
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
456,900✔
648
    mstsDebugL("create_info: calcScanPlan[%d] - readFromCache:%d vgNum:%d scanPlan:[%s]", i, pScan->readFromCache, vgNum, (char*)pScan->scanPlan);
456,900✔
649
    for (int32_t v = 0; v < vgNum; ++v) {
913,800✔
650
      mstsDebug("create_info: calcScanPlan[%d] vg[%d] - vgId:%d", i, v, *(int32_t*)taosArrayGet(pScan->vgList, v));
456,900✔
651
    }
652
  }
653

654
  for (int32_t i = 0; i < notifyUrlNum; ++i) {
245,474✔
655
    char* url = taosArrayGetP(q->pNotifyAddrUrls, i);
55,495✔
656
    mstsDebug("create_info: notifyUrl[%d] - %s", i, url);
55,495✔
657
  }
658

659
  for (int32_t i = 0; i < outColNum; ++i) {
939,962✔
660
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
749,983✔
661
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
749,983✔
662
        i, o->name, o->type, o->flags, o->bytes, o->compress, o->typeMod);
663
  }
664
      
665
}
666

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

673
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
216,816✔
674
  if (!(stDebugFlag & DEBUG_DEBUG)) {
216,816✔
675
    return;
23,456✔
676
  }
677
  
678
  if (NULL == p) {
193,360✔
679
    mstsDebug("%s: stream status is NULL", tips);
×
680
    return;
×
681
  }
682

683
  int32_t trigReaderNum = taosArrayGetSize(p->trigReaders);
193,360✔
684
  int32_t trigOReaderNum = msmGetTrigOReaderSize(p->trigOReaders);
193,360✔
685
  int32_t calcReaderNum = MST_LIST_SIZE(p->calcReaders);
193,360✔
686
  int32_t triggerNum = p->triggerTask ? 1 : 0;
193,360✔
687
  int32_t runnerNum = 0;
193,360✔
688

689
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
763,186✔
690
    runnerNum += taosArrayGetSize(p->runners[i]);
569,826✔
691
  }
692

693
  mstsDebug("%s: stream status", tips);
193,360✔
694
  mstsDebug("name:%s runnerNum:%d runnerDeploys:%d runnerReplica:%d lastActionTs:%" PRId64
193,360✔
695
           " trigReaders:%d trigOReaders:%d calcReaders:%d trigger:%d runners:%d",
696
      p->streamName, p->runnerNum, p->runnerDeploys, p->runnerReplica, p->lastActionTs,
697
      trigReaderNum, trigOReaderNum, calcReaderNum, triggerNum, runnerNum);
698

699
  SStmTaskStatus* pTask = NULL;
193,360✔
700
  for (int32_t i = 0; i < trigReaderNum; ++i) {
433,667✔
701
    pTask = taosArrayGet(p->trigReaders, i);
240,307✔
702
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
240,307✔
703
  }
704

705
  for (int32_t i = 0; i < trigOReaderNum; ++i) {
193,360✔
706
    pTask = msmGetTrigOReader(p->trigOReaders, i);
×
707
    mstLogSStmTaskStatus("trigOReader task", streamId, pTask, i);
×
708
  }
709

710
  SListNode* pNode = listHead(p->calcReaders);
193,360✔
711
  for (int32_t i = 0; i < calcReaderNum; ++i) {
586,309✔
712
    pTask = (SStmTaskStatus*)pNode->data;
392,949✔
713
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
392,949✔
714
    pNode = TD_DLIST_NODE_NEXT(pNode);
392,949✔
715
  }
716

717
  if (triggerNum > 0) {
193,360✔
718
    mstLogSStmTaskStatus("trigger task", streamId, p->triggerTask, 0);
193,360✔
719
  }
720

721
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
763,186✔
722
    int32_t num = taosArrayGetSize(p->runners[i]);
569,826✔
723
    if (num <= 0) {
569,826✔
724
      continue;
×
725
    }
726
    
727
    mstsDebug("the %dth deploy runners status", i);
569,826✔
728
    for (int32_t m = 0; m < num; ++m) {
1,256,550✔
729
      pTask = taosArrayGet(p->runners[i], m);
686,724✔
730
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
686,724✔
731
    }
732
  }
733
      
734
}
735

736
bool mstEventPassIsolation(int32_t num, int32_t event) {
5,112,499✔
737
  bool ret = ((mStreamMgmt.lastTs[event].ts + num * MST_SHORT_ISOLATION_DURATION) <= mStreamMgmt.hCtx.currentTs);
5,112,499✔
738
  if (ret) {
5,112,499✔
739
    mstDebug("event %s passed %d isolation, last:%" PRId64 ", curr:%" PRId64, 
3,995,733✔
740
        gMndStreamEvent[event], num, mStreamMgmt.lastTs[event].ts, mStreamMgmt.hCtx.currentTs);
741
  }
742

743
  return ret;
5,112,499✔
744
}
745

746
bool mstEventHandledChkSet(int32_t event) {
3,995,733✔
747
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
3,995,733✔
748
    mstDebug("event %s set handled", gMndStreamEvent[event]);
216,540✔
749
    return true;
216,540✔
750
  }
751
  return false;
3,779,193✔
752
}
753

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

763
  if (atomic_load_8(&pStream->userDropped)) {
679,097✔
764
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_DROPPING], statusSize);
×
765
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
×
766
    return TSDB_CODE_SUCCESS;
×
767
  }
768

769
  if (atomic_load_8(&pStream->userStopped)) {
679,097✔
770
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
5,155✔
771
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
5,155✔
772
    return TSDB_CODE_SUCCESS;
5,155✔
773
  }
774

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

786
  char tmpBuf[256];
528,785✔
787
  int8_t stopped = atomic_load_8(&pStatus->stopped);
528,785✔
788
  switch (stopped) {
528,785✔
789
    case 1:
18,385✔
790
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
18,385✔
791
      snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s, Failed times: %" PRId64, tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes);
18,385✔
792
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
18,385✔
793
      goto _exit;
18,385✔
794
      break;
795
    case 4:
×
796
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
×
797
      snprintf(tmpBuf, sizeof(tmpBuf), "Error: %s", tstrerror(TSDB_CODE_GRANT_STREAM_EXPIRED));
×
798
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
799
      goto _exit;
×
800
      break;
801
    default:
510,400✔
802
      break;
510,400✔
803
  }
804

805
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
510,400✔
806
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_RUNNING], statusSize);
238,255✔
807
    tstrncpy(tmpBuf, "Running start from: ", sizeof(tmpBuf));
238,255✔
808
    int32_t offset = strlen(tmpBuf);
238,255✔
809
    (void)formatTimestampLocal(tmpBuf + offset, sizeof(tmpBuf) - offset, pStatus->triggerTask->runningStartTs,
238,255✔
810
                               TSDB_TIME_PRECISION_MILLI);
811
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
238,255✔
812
    goto _exit;
238,255✔
813
  }
814

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

823
_exit:
673,942✔
824
  
825
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
673,942✔
826

827
  return TSDB_CODE_SUCCESS;
673,942✔
828
}
829

830
int32_t mstSetStreamAttrResBlock(SMnode *pMnode, SStreamObj* pStream, SSDataBlock* pBlock, int32_t numOfRows) {
679,335✔
831
  int32_t code = 0;
679,335✔
832
  int32_t cols = 0;
679,335✔
833
  int32_t lino = 0;
679,335✔
834

835
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
679,335✔
836
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
679,335✔
837
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
679,335✔
838
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
679,335✔
839

840
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
679,335✔
841
  TSDB_CHECK_CODE(code, lino, _end);
679,335✔
842

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

851
  // create time
852
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
679,335✔
853
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
679,335✔
854
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->createTime, false);
679,335✔
855
  TSDB_CHECK_CODE(code, lino, _end);
679,335✔
856

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

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

875
  // status
876
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
679,335✔
877
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
679,335✔
878
  code = mstGetStreamStatusStr(pStream, status, sizeof(status), msg, sizeof(msg));
679,335✔
879
  TSDB_CHECK_CODE(code, lino, _end);
679,335✔
880

881
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
679,335✔
882
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
679,335✔
883
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
679,335✔
884
  TSDB_CHECK_CODE(code, lino, _end);
679,335✔
885

886
  // snodeLeader
887
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
679,335✔
888
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
679,335✔
889
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->mainSnodeId, false);
679,335✔
890
  TSDB_CHECK_CODE(code, lino, _end);
679,335✔
891

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

901
  // msg
902
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
679,335✔
903
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
679,335✔
904
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
679,335✔
905

906
_end:
679,335✔
907
  if (code) {
679,335✔
908
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
909
  }
910
  return code;
679,335✔
911
}
912

913

914
int32_t mstGetTaskStatusStr(SStmTaskStatus* pTask, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
17,429,801✔
915
  char tmpBuf[256];
17,401,241✔
916
  
917
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[pTask->status], statusSize);
17,429,801✔
918
  if (STREAM_STATUS_FAILED == pTask->status && pTask->errCode) {
17,429,801✔
919
    snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s", tstrerror(pTask->errCode));
226✔
920
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
226✔
921
    return TSDB_CODE_SUCCESS;
226✔
922
  }
923

924
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
17,429,575✔
925
    if (pTask->detailStatus) {
2,508,307✔
926
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
1,999,321✔
927
      snprintf(tmpBuf, sizeof(tmpBuf), "Current RT/HI/RE session num: %d/%d/%d, histroy progress:%d%%, total AUTO/USER recalc num: %d/%d", 
1,999,321✔
928
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
929
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
1,999,321✔
930
      taosRUnLockLatch(&pTask->detailStatusLock);
1,999,321✔
931
      return TSDB_CODE_SUCCESS;
1,999,321✔
932
    }
933

934
    taosRUnLockLatch(&pTask->detailStatusLock);    
508,986✔
935
  }
936
  
937
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
15,430,254✔
938
  
939
  return TSDB_CODE_SUCCESS;
15,430,254✔
940
}
941

942
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
17,429,801✔
943
  switch (pTask->type) {
17,429,801✔
944
    case STREAM_READER_TASK:
7,077,898✔
945
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
7,077,898✔
946
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
2,976,548✔
947
      } else {
948
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
4,101,350✔
949
      }
950
      return TSDB_CODE_SUCCESS;
7,077,898✔
951
    case STREAM_RUNNER_TASK:
7,843,596✔
952
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
7,843,596✔
953
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
7,616,700✔
954
        return TSDB_CODE_SUCCESS;
7,616,700✔
955
      }
956
      break;
226,896✔
957
    default:
2,508,307✔
958
      break;
2,508,307✔
959
  }
960

961
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
2,735,203✔
962
  return TSDB_CODE_SUCCESS;
2,735,203✔
963
}
964

965

966
int32_t mstSetStreamTaskResBlock(SStreamObj* pStream, SStmTaskStatus* pTask, SSDataBlock* pBlock, int32_t numOfRows) {
17,429,801✔
967
  int32_t code = 0;
17,429,801✔
968
  int32_t cols = 0;
17,429,801✔
969
  int32_t lino = 0;
17,429,801✔
970

971
  // stream_name
972
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
17,429,801✔
973
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
17,429,801✔
974
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
975
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
976

977
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
17,429,801✔
978
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
979

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

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

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

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

1013
  // deploy id
1014
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1015
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1016
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.deployId, false);
17,429,801✔
1017
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1018

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

1027
  // node id
1028
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1029
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1030
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.nodeId, false);
17,429,801✔
1031
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1032

1033
  // task idx
1034
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1035
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1036
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.taskIdx, false);
17,429,801✔
1037
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1038

1039
  // status
1040
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
17,429,801✔
1041
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
17,429,801✔
1042
  code = mstGetTaskStatusStr(pTask, status, sizeof(status), msg, sizeof(msg));
17,429,801✔
1043
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1044

1045
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1046
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1047
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
17,429,801✔
1048
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1049

1050
  // start time
1051
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1052
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1053
  if (pTask->runningStartTs) {
17,429,801✔
1054
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
3,772,706✔
1055
  } else {
1056
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
13,657,095✔
1057
  }
1058
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1059

1060
  // last update
1061
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1062
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1063
  if (pTask->lastUpTs) {
17,429,801✔
1064
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, false);
17,429,608✔
1065
  } else {
1066
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, true);
193✔
1067
  }
1068
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1069

1070
  // extra info
1071
  char extra[64 + VARSTR_HEADER_SIZE] = {0};
17,429,801✔
1072
  code = mstGetTaskExtraStr(pTask, extra, sizeof(extra));
17,429,801✔
1073
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1074
  
1075
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1076
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1077
  code = colDataSetVal(pColInfo, numOfRows, (const char*)extra, false);
17,429,801✔
1078
  TSDB_CHECK_CODE(code, lino, _end);
17,429,801✔
1079

1080
  // msg
1081
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
17,429,801✔
1082
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
17,429,801✔
1083
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
17,429,801✔
1084

1085
_end:
17,429,801✔
1086
  if (code) {
17,429,801✔
1087
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1088
  }
1089
  return code;
17,429,801✔
1090
}
1091

1092
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
2,513,154✔
1093
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
2,513,154✔
1094
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,052,616✔
1095
    num += taosArrayGetSize(pStatus->runners[i]);
7,539,462✔
1096
  }
1097

1098
  return num;
2,513,154✔
1099
}
1100

1101
int32_t mstSetStreamTasksResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
2,749,872✔
1102
  int32_t code = 0;
2,749,872✔
1103
  int32_t lino = 0;
2,749,872✔
1104
  int64_t streamId = pStream->pCreate->streamId;
2,749,872✔
1105
  bool    statusLocked = false;
2,749,872✔
1106

1107
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
2,749,872✔
1108

1109
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,749,872✔
1110
  if (NULL == pStatus) {
2,749,872✔
1111
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
176,624✔
1112
    goto _exit;
176,624✔
1113
  }
1114

1115
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,573,248✔
1116
  if (stopped) {
2,573,248✔
1117
    mstsDebug("stream stopped %d, ignore it", stopped);
60,094✔
1118
    goto _exit;
60,094✔
1119
  }
1120

1121
  (void)mstWaitLock(&pStatus->resetLock, true);
2,513,154✔
1122
  statusLocked = true;
2,513,154✔
1123
  
1124
  int32_t count = mstGetNumOfStreamTasks(pStatus);
2,513,154✔
1125

1126
  if (*numOfRows + count > rowsCapacity) {
2,513,154✔
1127
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
71,707✔
1128
    if (code) {
71,707✔
1129
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1130
      TAOS_CHECK_EXIT(code);
×
1131
    }
1132
  }
1133

1134
  SStmTaskStatus* pTask = NULL;
2,513,154✔
1135
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
2,513,154✔
1136
  for (int32_t i = 0; i < trigReaderNum; ++i) {
5,253,368✔
1137
    pTask = taosArrayGet(pStatus->trigReaders, i);
2,740,214✔
1138
  
1139
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
2,740,214✔
1140
    if (code == TSDB_CODE_SUCCESS) {
2,740,214✔
1141
      (*numOfRows)++;
2,740,214✔
1142
    }
1143
  }
1144

1145
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
2,513,154✔
1146
  for (int32_t i = 0; i < trigReaderNum; ++i) {
2,749,488✔
1147
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
236,334✔
1148
  
1149
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
236,334✔
1150
    if (code == TSDB_CODE_SUCCESS) {
236,334✔
1151
      (*numOfRows)++;
236,334✔
1152
    }
1153
  }
1154

1155
  if (pStatus->calcReaders) {
2,513,154✔
1156
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
2,513,154✔
1157
    SListNode* pNode = listHead(pStatus->calcReaders);
2,513,154✔
1158
    for (int32_t i = 0; i < calcReaderNum; ++i) {
6,614,504✔
1159
      pTask = (SStmTaskStatus*)pNode->data;
4,101,350✔
1160
    
1161
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
4,101,350✔
1162
      if (code == TSDB_CODE_SUCCESS) {
4,101,350✔
1163
        (*numOfRows)++;
4,101,350✔
1164
      }
1165
      pNode = TD_DLIST_NODE_NEXT(pNode);
4,101,350✔
1166
    }
1167
  }
1168

1169
  if (pStatus->triggerTask) {
2,513,154✔
1170
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
2,508,307✔
1171
    if (code == TSDB_CODE_SUCCESS) {
2,508,307✔
1172
      (*numOfRows)++;
2,508,307✔
1173
    }
1174
  }
1175

1176
  int32_t runnerNum = 0;
2,513,154✔
1177
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,052,616✔
1178
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
7,539,462✔
1179
    for (int32_t m = 0; m < runnerNum; ++m) {
15,383,058✔
1180
      pTask = taosArrayGet(pStatus->runners[i], m);
7,843,596✔
1181
    
1182
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
7,843,596✔
1183
      if (code == TSDB_CODE_SUCCESS) {
7,843,596✔
1184
        (*numOfRows)++;
7,843,596✔
1185
      }
1186
    }
1187
  }
1188
  
1189
  pBlock->info.rows = *numOfRows;
2,513,154✔
1190

1191
_exit:
2,749,872✔
1192

1193
  if (statusLocked) {
2,749,872✔
1194
    taosRUnLockLatch(&pStatus->resetLock);
2,513,154✔
1195
  }
1196
  
1197
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
2,749,872✔
1198

1199
  if (code) {
2,749,872✔
1200
    mError("error happens when build stream tasks result block, lino:%d, code:%s", lino, tstrerror(code));
×
1201
  }
1202
  
1203
  return code;
2,749,872✔
1204
}
1205

1206

1207
int32_t mstAppendNewRecalcRange(int64_t streamId, SStmStatus *pStream, STimeWindow* pRange) {
3,796✔
1208
  int32_t code = 0;
3,796✔
1209
  int32_t lino = 0;
3,796✔
1210
  bool    locked = false;
3,796✔
1211
  SArray* userRecalcList = NULL;
3,796✔
1212

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

1225
    TSDB_CHECK_NULL(taosArrayPush(userRecalcList, &req), code, lino, _exit, terrno);
3,796✔
1226

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

1235
_exit:
3,796✔
1236

1237
  taosArrayDestroy(userRecalcList);
3,796✔
1238

1239
  if (locked) {
3,796✔
1240
    taosWUnLockLatch(&pStream->userRecalcLock);
3,796✔
1241
  }
1242
  
1243
  if (code) {
3,796✔
1244
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1245
  }
1246
  
1247
  return code;
3,796✔
1248
}
1249

1250

1251

1252
int32_t mstSetStreamRecalculateResBlock(SStreamObj* pStream, SSTriggerRecalcProgress* pProgress, SSDataBlock* pBlock, int32_t numOfRows) {
×
1253
  int32_t code = 0;
×
1254
  int32_t cols = 0;
×
1255
  int32_t lino = 0;
×
1256

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

1263
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
×
1264
  TSDB_CHECK_CODE(code, lino, _end);
×
1265

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

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

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

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

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

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

1311

1312
int32_t mstSetStreamRecalculatesResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
292✔
1313
  int32_t code = 0;
292✔
1314
  int32_t lino = 0;
292✔
1315
  int64_t streamId = pStream->pCreate->streamId;
292✔
1316

1317
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
292✔
1318

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

1325
  int8_t stopped = atomic_load_8(&pStatus->stopped);
292✔
1326
  if (stopped) {
292✔
1327
    mstsDebug("stream stopped %d, ignore it", stopped);
×
1328
    goto _exit;
×
1329
  }
1330

1331
  if (NULL == pStatus->triggerTask) {
292✔
1332
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1333
    goto _exit;
×
1334
  }
1335

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

1343
  SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pStatus->triggerTask->detailStatus;
292✔
1344
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
292✔
1345

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

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

1364
  taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
292✔
1365
  
1366
  pBlock->info.rows = *numOfRows;
292✔
1367

1368
_exit:
292✔
1369
  
1370
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
292✔
1371

1372
  if (code) {
292✔
1373
    mError("error happens when build stream recalculates result block, lino:%d, code:%s", lino, tstrerror(code));
×
1374
  }
1375
  
1376
  return code;
292✔
1377
}
1378

1379
int32_t mstGetScanUidFromPlan(int64_t streamId, void* scanPlan, int64_t* uid) {
404,007✔
1380
  SSubplan* pSubplan = NULL;
404,007✔
1381
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
404,007✔
1382
  
1383
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
404,007✔
1384

1385
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
404,007✔
1386
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
167,353✔
1387
    *uid = pScanNode->uid;
167,353✔
1388
  }
1389
  
1390
_exit:
403,493✔
1391

1392
  if (code) {
404,007✔
1393
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1394
  }
1395

1396
  nodesDestroyNode((SNode *)pSubplan);
404,007✔
1397

1398
  return code;
404,007✔
1399
}
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