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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 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) {
35,876,166✔
26
  if (readLock) {
35,876,166✔
27
    while (taosRTryLockLatch(pLock)) {
31,846,701✔
28
      taosMsleep(1);
1,020,543✔
29
    }
30

31
    return true;
30,826,158✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
5,050,008✔
35

36
  return true;
5,050,008✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
240,760✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
240,760✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
963,040✔
48
    taosArrayDestroy(pStatus->runners[i]);
722,280✔
49
    pStatus->runners[i] = NULL;
722,280✔
50
  }
51
}
240,760✔
52

53

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

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

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

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

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

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

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

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

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

125
  taosArrayDestroy(pStatus->trigReaders);
234,998✔
126
  pStatus->trigReaders = NULL;
234,998✔
127
  taosArrayDestroyEx(pStatus->trigOReaders, mstFreeTrigOReaderList);
234,998✔
128
  pStatus->trigOReaders = NULL;
234,998✔
129
  pStatus->calcReaders = tdListFree(pStatus->calcReaders);
234,998✔
130
  if (pStatus->triggerTask) {
234,998✔
131
    (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, false);
234,392✔
132
    taosMemoryFreeClear(pStatus->triggerTask->detailStatus);
234,392✔
133
    taosWUnLockLatch(&pStatus->triggerTask->detailStatusLock);
234,392✔
134
  }
135
  taosMemoryFreeClear(pStatus->triggerTask);
234,998✔
136
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
939,992✔
137
    taosArrayDestroy(pStatus->runners[i]);
704,994✔
138
    pStatus->runners[i] = NULL;
704,994✔
139
  }
140
  pStatus->lastTrigMgmtReqId = 0;
234,998✔
141

142
  taosWUnLockLatch(&pStatus->resetLock);
234,998✔
143
}
234,998✔
144

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

149
  mstResetSStmStatus(pStatus);
227,148✔
150

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

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

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

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

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

172
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
263,474✔
173
  SSdb   *pSdb = pMnode->pSdb;
263,474✔
174
  void   *pIter = NULL;
263,474✔
175
  
176
  while (1) {
622,165✔
177
    SStreamObj *pStream = NULL;
885,639✔
178
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
885,639✔
179
    if (pIter == NULL) break;
885,639✔
180

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

192
  *dropped = true;
200,750✔
193

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

209
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
40,474✔
210
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
40,474✔
211
    return true;
19,753✔
212
  }
213
  
214
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
20,721✔
215
    *pCtx->dbStream = true;
723✔
216
    return false;
723✔
217
  }
218

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

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

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

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

295
  SStmQNode *orig = pQueue->head;
239,961✔
296

297
  SStmQNode *node = pQueue->head->next;
239,961✔
298
  pQueue->head = pQueue->head->next;
239,961✔
299

300
  *param = node;
239,961✔
301

302
  taosMemoryFreeClear(orig);
239,961✔
303

304
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
239,961✔
305

306
  return true;
239,961✔
307
}
308

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

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

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

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

354
  mndStreamActionEnqueue(actionQ, pNode);
237,792✔
355

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

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

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

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

388

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

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

402
  while (1) {
407,104✔
403
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
473,835✔
404
    if (pIter == NULL) {
473,835✔
405
      break;
66,731✔
406
    }
407

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

433
      pTarget = pNew;
244,558✔
434
    } else {
435
      pTarget = pDbInfo->vgArray;
162,546✔
436
    }
437

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

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

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

465
  taosArrayDestroy(pNew);
66,731✔
466

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

471
  return code;
66,731✔
472
}
473

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

490
  if (*key < pVg->hashBegin) {
267,799✔
491
    return -1;
×
492
  } else if (*key > pVg->hashEnd) {
267,799✔
493
    return 1;
72,430✔
494
  }
495

496
  return 0;
195,369✔
497
}
498

499

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

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

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

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

530
_exit:
195,369✔
531

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

536
  return code;
195,369✔
537
}
538

539

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

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

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

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

568
  mstsDebugL("create_info: name:%s sql:%s streamDB:%s triggerDB:%s outDB:%s calcDBNum:%d triggerTblName:%s outTblName:%s "
205,977✔
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) {
205,977✔
584
    case WINDOW_TYPE_INTERVAL: {
73,832✔
585
      SSlidingTrigger* t = &q->trigger.sliding;
73,832✔
586
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
73,832✔
587
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
588
      break;
73,832✔
589
    }  
590
    case WINDOW_TYPE_SESSION: {
8,725✔
591
      SSessionTrigger* t = &q->trigger.session;
8,725✔
592
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
8,725✔
593
      break;
8,725✔
594
    }
595
    case WINDOW_TYPE_STATE: {
66,635✔
596
      SStateWinTrigger* t = &q->trigger.stateWin;
66,635✔
597
      int32_t           slotIdNum = t->pSlotIds ? taosArrayGetSize(t->pSlotIds) : 0;
66,635✔
598
      int16_t           firstSlotId = (slotIdNum > 0) ? *(int16_t*)taosArrayGet(t->pSlotIds, 0) : -1;
66,635✔
599
      mstsDebug(
66,635✔
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;
66,635✔
605
    }
606
    case WINDOW_TYPE_EVENT:{
22,590✔
607
      SEventTrigger* t = &q->trigger.event;
22,590✔
608
      mstsDebug(
22,590✔
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;
22,590✔
613
    }
614
    case WINDOW_TYPE_COUNT: {
23,094✔
615
      SCountTrigger* t = &q->trigger.count;
23,094✔
616
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
23,094✔
617
      break;
23,094✔
618
    }
619
    case WINDOW_TYPE_PERIOD: {
11,101✔
620
      SPeriodTrigger* t = &q->trigger.period;
11,101✔
621
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
11,101✔
622
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
623
      break;
11,101✔
624
    }
625
    default:
×
626
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
627
      break;
×
628
  }
629

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

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

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

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

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

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

642

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

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

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

662
  for (int32_t i = 0; i < outColNum; ++i) {
1,031,370✔
663
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
825,393✔
664
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
825,393✔
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,599,340✔
671
  mstsDebug("%s[%d]: task %" PRIx64 " deployId:%d SID:%" PRId64 " nodeId:%d tidx:%d type:%s flags:%" PRIx64 " status:%s lastUpTs:%" PRId64, 
1,599,340✔
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,599,340✔
675

676
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
234,392✔
677
  if (!(stDebugFlag & DEBUG_DEBUG)) {
234,392✔
678
    return;
23,060✔
679
  }
680
  
681
  if (NULL == p) {
211,332✔
682
    mstsDebug("%s: stream status is NULL", tips);
×
683
    return;
×
684
  }
685

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

692
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
834,993✔
693
    runnerNum += taosArrayGetSize(p->runners[i]);
623,661✔
694
  }
695

696
  mstsDebug("%s: stream status", tips);
211,332✔
697
  mstsDebug("name:%s runnerNum:%d runnerDeploys:%d runnerReplica:%d lastActionTs:%" PRId64
211,332✔
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;
211,332✔
703
  for (int32_t i = 0; i < trigReaderNum; ++i) {
471,979✔
704
    pTask = taosArrayGet(p->trigReaders, i);
260,647✔
705
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
260,647✔
706
  }
707

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

713
  SListNode* pNode = listHead(p->calcReaders);
211,332✔
714
  for (int32_t i = 0; i < calcReaderNum; ++i) {
609,954✔
715
    pTask = (SStmTaskStatus*)pNode->data;
398,622✔
716
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
398,622✔
717
    pNode = TD_DLIST_NODE_NEXT(pNode);
398,622✔
718
  }
719

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

724
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
834,993✔
725
    int32_t num = taosArrayGetSize(p->runners[i]);
623,661✔
726
    if (num <= 0) {
623,661✔
727
      continue;
×
728
    }
729
    
730
    mstsDebug("the %dth deploy runners status", i);
623,661✔
731
    for (int32_t m = 0; m < num; ++m) {
1,352,400✔
732
      pTask = taosArrayGet(p->runners[i], m);
728,739✔
733
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
728,739✔
734
    }
735
  }
736
      
737
}
738

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

746
  return ret;
5,649,786✔
747
}
748

749
bool mstEventHandledChkSet(int32_t event) {
4,411,173✔
750
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
4,411,173✔
751
    mstDebug("event %s set handled", gMndStreamEvent[event]);
229,445✔
752
    return true;
229,445✔
753
  }
754
  return false;
4,181,728✔
755
}
756

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

766
  if (atomic_load_8(&pStream->userDropped)) {
822,187✔
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)) {
822,187✔
773
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
5,218✔
774
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
5,218✔
775
    return TSDB_CODE_SUCCESS;
5,218✔
776
  }
777

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

789
  char tmpBuf[256];
658,957✔
790
  int8_t stopped = atomic_load_8(&pStatus->stopped);
658,957✔
791
  switch (stopped) {
658,957✔
792
    case 1:
19,025✔
793
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
19,025✔
794
      snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s, Failed times: %" PRId64, tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes);
19,025✔
795
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
19,025✔
796
      goto _exit;
19,025✔
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:
639,932✔
805
      break;
639,932✔
806
  }
807

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

818
  mstsDebug("return Idle: pStatus=%p triggerTask=%p triggerStatus=%d (show streams view)",
284,122✔
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);
284,122✔
822
  snprintf(tmpBuf, sizeof(tmpBuf), "Current deploy times: %" PRId64, pStatus->deployTimes);
284,122✔
823
  STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
284,122✔
824
  goto _exit;
284,122✔
825

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

830
  return TSDB_CODE_SUCCESS;
816,969✔
831
}
832

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

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

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

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

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

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

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

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

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

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

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

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

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

916

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

927
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
23,392,398✔
928
    if (pTask->detailStatus) {
3,688,315✔
929
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
3,192,738✔
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,192,738✔
931
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
932
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
3,192,738✔
933
      taosRUnLockLatch(&pTask->detailStatusLock);
3,192,738✔
934
      return TSDB_CODE_SUCCESS;
3,192,738✔
935
    }
936

937
    taosRUnLockLatch(&pTask->detailStatusLock);    
495,577✔
938
  }
939
  
940
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
20,199,660✔
941
  
942
  return TSDB_CODE_SUCCESS;
20,199,660✔
943
}
944

945
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
23,392,621✔
946
  switch (pTask->type) {
23,392,621✔
947
    case STREAM_READER_TASK:
8,297,687✔
948
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
8,297,687✔
949
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
4,267,526✔
950
      } else {
951
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
4,030,161✔
952
      }
953
      return TSDB_CODE_SUCCESS;
8,297,687✔
954
    case STREAM_RUNNER_TASK:
11,406,619✔
955
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
11,406,619✔
956
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
11,169,409✔
957
        return TSDB_CODE_SUCCESS;
11,169,409✔
958
      }
959
      break;
237,210✔
960
    default:
3,688,315✔
961
      break;
3,688,315✔
962
  }
963

964
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
3,925,525✔
965
  return TSDB_CODE_SUCCESS;
3,925,525✔
966
}
967

968

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

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

980
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
23,392,621✔
981
  TSDB_CHECK_CODE(code, lino, _end);
23,392,621✔
982

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

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

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

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

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

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

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

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

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

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

1053
  // start time
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
23,392,621✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
23,392,621✔
1056
  if (pTask->runningStartTs) {
23,392,621✔
1057
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
7,548,716✔
1058
  } else {
1059
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
15,843,905✔
1060
  }
1061
  TSDB_CHECK_CODE(code, lino, _end);
23,392,621✔
1062

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

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

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

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

1095
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
3,694,272✔
1096
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
3,694,272✔
1097
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
14,777,088✔
1098
    num += taosArrayGetSize(pStatus->runners[i]);
11,082,816✔
1099
  }
1100

1101
  return num;
3,694,272✔
1102
}
1103

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

1110
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
3,936,466✔
1111

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

1118
  int8_t stopped = atomic_load_8(&pStatus->stopped);
3,751,064✔
1119
  if (stopped) {
3,751,064✔
1120
    mstsDebug("stream stopped %d, ignore it", stopped);
56,792✔
1121
    goto _exit;
56,792✔
1122
  }
1123

1124
  (void)mstWaitLock(&pStatus->resetLock, true);
3,694,272✔
1125
  statusLocked = true;
3,694,272✔
1126
  
1127
  int32_t count = mstGetNumOfStreamTasks(pStatus);
3,694,272✔
1128

1129
  if (*numOfRows + count > rowsCapacity) {
3,694,272✔
1130
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
66,870✔
1131
    if (code) {
66,870✔
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;
3,694,272✔
1138
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
3,694,272✔
1139
  for (int32_t i = 0; i < trigReaderNum; ++i) {
7,655,136✔
1140
    pTask = taosArrayGet(pStatus->trigReaders, i);
3,960,864✔
1141
  
1142
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
3,960,864✔
1143
    if (code == TSDB_CODE_SUCCESS) {
3,960,864✔
1144
      (*numOfRows)++;
3,960,864✔
1145
    }
1146
  }
1147

1148
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
3,694,272✔
1149
  for (int32_t i = 0; i < trigReaderNum; ++i) {
4,000,934✔
1150
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
306,662✔
1151
  
1152
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
306,662✔
1153
    if (code == TSDB_CODE_SUCCESS) {
306,662✔
1154
      (*numOfRows)++;
306,662✔
1155
    }
1156
  }
1157

1158
  if (pStatus->calcReaders) {
3,694,272✔
1159
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
3,694,272✔
1160
    SListNode* pNode = listHead(pStatus->calcReaders);
3,694,272✔
1161
    for (int32_t i = 0; i < calcReaderNum; ++i) {
7,724,433✔
1162
      pTask = (SStmTaskStatus*)pNode->data;
4,030,161✔
1163
    
1164
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
4,030,161✔
1165
      if (code == TSDB_CODE_SUCCESS) {
4,030,161✔
1166
        (*numOfRows)++;
4,030,161✔
1167
      }
1168
      pNode = TD_DLIST_NODE_NEXT(pNode);
4,030,161✔
1169
    }
1170
  }
1171

1172
  if (pStatus->triggerTask) {
3,694,272✔
1173
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
3,688,315✔
1174
    if (code == TSDB_CODE_SUCCESS) {
3,688,315✔
1175
      (*numOfRows)++;
3,688,315✔
1176
    }
1177
  }
1178

1179
  int32_t runnerNum = 0;
3,694,272✔
1180
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
14,777,088✔
1181
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
11,082,816✔
1182
    for (int32_t m = 0; m < runnerNum; ++m) {
22,489,435✔
1183
      pTask = taosArrayGet(pStatus->runners[i], m);
11,406,619✔
1184
    
1185
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
11,406,619✔
1186
      if (code == TSDB_CODE_SUCCESS) {
11,406,619✔
1187
        (*numOfRows)++;
11,406,619✔
1188
      }
1189
    }
1190
  }
1191
  
1192
  pBlock->info.rows = *numOfRows;
3,694,272✔
1193

1194
_exit:
3,936,466✔
1195

1196
  if (statusLocked) {
3,936,466✔
1197
    taosRUnLockLatch(&pStatus->resetLock);
3,694,272✔
1198
  }
1199
  
1200
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
3,936,466✔
1201

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

1209

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

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

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

1230
    atomic_store_ptr(&pStream->userRecalcList, userRecalcList);
3,716✔
1231
    userRecalcList = NULL;    
3,716✔
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);
3,716✔
1237

1238
_exit:
3,716✔
1239

1240
  taosArrayDestroy(userRecalcList);
3,716✔
1241

1242
  if (locked) {
3,716✔
1243
    taosWUnLockLatch(&pStream->userRecalcLock);
3,716✔
1244
  }
1245
  
1246
  if (code) {
3,716✔
1247
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1248
  }
1249
  
1250
  return code;
3,716✔
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) {
286✔
1316
  int32_t code = 0;
286✔
1317
  int32_t lino = 0;
286✔
1318
  int64_t streamId = pStream->pCreate->streamId;
286✔
1319

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

1322
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
286✔
1323
  if (NULL == pStatus) {
286✔
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);
286✔
1329
  if (stopped) {
286✔
1330
    mstsDebug("stream stopped %d, ignore it", stopped);
×
1331
    goto _exit;
×
1332
  }
1333

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

1339
  (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, true);
286✔
1340
  if (NULL == pStatus->triggerTask->detailStatus) {
286✔
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;
286✔
1347
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
286✔
1348

1349
  if (*numOfRows + count > rowsCapacity) {
286✔
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) {
286✔
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);
286✔
1368
  
1369
  pBlock->info.rows = *numOfRows;
286✔
1370

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

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

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

1388
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
409,406✔
1389
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
173,892✔
1390
    *uid = pScanNode->uid;
173,892✔
1391
  }
1392
  
1393
_exit:
408,969✔
1394

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

1399
  nodesDestroyNode((SNode *)pSubplan);
409,406✔
1400

1401
  return code;
409,406✔
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