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

taosdata / TDengine / #5097

17 May 2026 01:15AM UTC coverage: 73.316% (+0.04%) from 73.276%
#5097

push

travis-ci

web-flow
feat (TDgpt): Dynamic Model Synchronization Enhancements (#35344)

* refactor: do some internal refactor.

* fix: fix multiprocess sync issue.

* feat: add dynamic anomaly detection and forecasting services

* fix: log error message for undeploying model in exception handling

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* Potential fix for pull request finding

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>

* fix: handle undeploy when model exists only on disk

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/286aafa0-c3ce-4c27-b803-2707571e9dc1

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: guard dynamic registry concurrent access

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: tighten service list locking scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/5e4db858-6458-40f4-ac28-d1b1b7f97c18

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: restore prophet support and update tests per review feedback

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* fix: improve test name and move copy inside lock scope

Agent-Logs-Url: https://github.com/taosdata/TDengine/sessions/92298ae1-7da6-4d07-b20e-101c7cd0b26b

Co-authored-by: hjxilinx <8252296+hjxilinx@users.noreply.github.com>

* Potential fix for pull request finding

Co-au... (continued)

281382 of 383795 relevant lines covered (73.32%)

132379014.15 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) {
43,278,435✔
26
  if (readLock) {
43,278,435✔
27
    while (taosRTryLockLatch(pLock)) {
37,479,847✔
28
      taosMsleep(1);
522,898✔
29
    }
30

31
    return true;
36,956,949✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
6,321,486✔
35

36
  return true;
6,321,486✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
257,576✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
257,576✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,030,304✔
48
    taosArrayDestroy(pStatus->runners[i]);
772,728✔
49
    pStatus->runners[i] = NULL;
772,728✔
50
  }
51
}
257,576✔
52

53

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

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

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

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

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

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

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

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

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

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

142
  taosWUnLockLatch(&pStatus->resetLock);
248,848✔
143
}
248,848✔
144

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

149
  mstResetSStmStatus(pStatus);
239,787✔
150

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

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

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

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

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

172
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
303,890✔
173
  SSdb   *pSdb = pMnode->pSdb;
303,890✔
174
  void   *pIter = NULL;
303,890✔
175
  
176
  while (1) {
658,343✔
177
    SStreamObj *pStream = NULL;
962,233✔
178
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
962,233✔
179
    if (pIter == NULL) break;
962,233✔
180

181
    if (pStream->pCreate->streamId == streamId) {
728,481✔
182
      *dropped = pStream->userDropped ? true : false;
70,138✔
183
      sdbRelease(pSdb, pStream);
70,138✔
184
      sdbCancelFetch(pSdb, pIter);
70,138✔
185
      mstsDebug("stream found, dropped:%d", *dropped);
70,138✔
186
      return TSDB_CODE_SUCCESS;
70,138✔
187
    }
188
    
189
    sdbRelease(pSdb, pStream);
658,343✔
190
  }
191

192
  *dropped = true;
233,752✔
193

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

209
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
44,676✔
210
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
44,676✔
211
    return true;
21,950✔
212
  }
213
  
214
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
22,726✔
215
    *pCtx->dbStream = true;
783✔
216
    return false;
783✔
217
  }
218

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

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

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

242
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
11,001✔
243
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
11,001✔
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) {
828,793✔
291
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
828,793✔
292
    return false;
565,662✔
293
  }
294

295
  SStmQNode *orig = pQueue->head;
263,131✔
296

297
  SStmQNode *node = pQueue->head->next;
263,131✔
298
  pQueue->head = pQueue->head->next;
263,131✔
299

300
  *param = node;
263,131✔
301

302
  taosMemoryFreeClear(orig);
263,131✔
303

304
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
263,131✔
305

306
  return true;
263,131✔
307
}
308

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

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

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

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

354
  mndStreamActionEnqueue(actionQ, pNode);
256,525✔
355

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

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

373
  mndStreamActionEnqueue(actionQ, pNode);
6,606✔
374
}
375

376
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
69,953✔
377
  int32_t iter = 0;
69,953✔
378
  SDBVgHashInfo* pVg = NULL;
69,953✔
379
  void* p = NULL;
69,953✔
380
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
327,240✔
381
    pVg = (SDBVgHashInfo*)p;
257,287✔
382
    taosArrayDestroy(pVg->vgArray);
257,287✔
383
  }
384
  
385
  tSimpleHashCleanup(pDbVgs);
69,953✔
386
}
69,953✔
387

388

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

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

402
  while (1) {
426,627✔
403
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
496,580✔
404
    if (pIter == NULL) {
496,580✔
405
      break;
69,953✔
406
    }
407

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

433
      pTarget = pNew;
257,287✔
434
    } else {
435
      pTarget = pDbInfo->vgArray;
169,340✔
436
    }
437

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

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

461
  *ppRes = pDbVgroup;
69,953✔
462
  
463
_exit:
69,953✔
464

465
  taosArrayDestroy(pNew);
69,953✔
466

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

471
  return code;
69,953✔
472
}
473

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

490
  if (*key < pVg->hashBegin) {
279,300✔
491
    return -1;
×
492
  } else if (*key > pVg->hashEnd) {
279,300✔
493
    return 1;
75,341✔
494
  }
495

496
  return 0;
203,959✔
497
}
498

499

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

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

516
  if (!dbInfo->vgSorted) {
203,959✔
517
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
70,226✔
518
    dbInfo->vgSorted = true;
70,226✔
519
  }
520

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

530
_exit:
203,959✔
531

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

536
  return code;
203,959✔
537
}
538

539

540
void mstLogSStreamObj(char* tips, SStreamObj* p) {
246,621✔
541
  if (!(stDebugFlag & DEBUG_DEBUG)) {
246,621✔
542
    return;
24,888✔
543
  }
544
  
545
  if (NULL == p) {
221,733✔
546
    mstDebug("%s: stream is NULL", tips);
×
547
    return;
×
548
  }
549

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

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

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

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

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

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

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

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

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

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

642

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

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

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

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

676
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
248,167✔
677
  if (!(stDebugFlag & DEBUG_DEBUG)) {
248,167✔
678
    return;
24,888✔
679
  }
680
  
681
  if (NULL == p) {
223,279✔
682
    mstsDebug("%s: stream status is NULL", tips);
×
683
    return;
×
684
  }
685

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

692
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
882,373✔
693
    runnerNum += taosArrayGetSize(p->runners[i]);
659,094✔
694
  }
695

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

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

713
  SListNode* pNode = listHead(p->calcReaders);
223,279✔
714
  for (int32_t i = 0; i < calcReaderNum; ++i) {
653,171✔
715
    pTask = (SStmTaskStatus*)pNode->data;
429,892✔
716
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
429,892✔
717
    pNode = TD_DLIST_NODE_NEXT(pNode);
429,892✔
718
  }
719

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

724
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
882,373✔
725
    int32_t num = taosArrayGetSize(p->runners[i]);
659,094✔
726
    if (num <= 0) {
659,094✔
727
      continue;
×
728
    }
729
    
730
    mstsDebug("the %dth deploy runners status", i);
659,094✔
731
    for (int32_t m = 0; m < num; ++m) {
1,450,071✔
732
      pTask = taosArrayGet(p->runners[i], m);
790,977✔
733
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
790,977✔
734
    }
735
  }
736
      
737
}
738

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

746
  return ret;
7,199,564✔
747
}
748

749
bool mstEventHandledChkSet(int32_t event) {
5,662,661✔
750
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
5,662,661✔
751
    mstDebug("event %s set handled", gMndStreamEvent[event]);
254,005✔
752
    return true;
254,005✔
753
  }
754
  return false;
5,408,656✔
755
}
756

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

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

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

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

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

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

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

830
  return TSDB_CODE_SUCCESS;
900,276✔
831
}
832

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

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

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

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

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

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

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

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

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

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

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

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

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

916

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

927
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
32,109,098✔
928
    if (pTask->detailStatus) {
4,340,972✔
929
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
3,649,181✔
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,649,181✔
931
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
932
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
3,649,181✔
933
      taosRUnLockLatch(&pTask->detailStatusLock);
3,649,181✔
934
      return TSDB_CODE_SUCCESS;
3,649,181✔
935
    }
936

937
    taosRUnLockLatch(&pTask->detailStatusLock);    
691,791✔
938
  }
939
  
940
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
28,459,917✔
941
  
942
  return TSDB_CODE_SUCCESS;
28,459,917✔
943
}
944

945
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
32,109,341✔
946
  switch (pTask->type) {
32,109,341✔
947
    case STREAM_READER_TASK:
13,572,735✔
948
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
13,572,735✔
949
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
4,938,606✔
950
      } else {
951
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
8,634,129✔
952
      }
953
      return TSDB_CODE_SUCCESS;
13,572,735✔
954
    case STREAM_RUNNER_TASK:
14,195,634✔
955
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
14,195,634✔
956
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
13,919,588✔
957
        return TSDB_CODE_SUCCESS;
13,919,588✔
958
      }
959
      break;
276,046✔
960
    default:
4,340,972✔
961
      break;
4,340,972✔
962
  }
963

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

968

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

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

980
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
32,109,341✔
981
  TSDB_CHECK_CODE(code, lino, _end);
32,109,341✔
982

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

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

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

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

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

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

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

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

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

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

1053
  // start time
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
32,109,341✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
32,109,341✔
1056
  if (pTask->runningStartTs) {
32,109,341✔
1057
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
8,335,019✔
1058
  } else {
1059
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
23,774,322✔
1060
  }
1061
  TSDB_CHECK_CODE(code, lino, _end);
32,109,341✔
1062

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

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

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

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

1095
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
4,348,099✔
1096
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
4,348,099✔
1097
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
17,392,396✔
1098
    num += taosArrayGetSize(pStatus->runners[i]);
13,044,297✔
1099
  }
1100

1101
  return num;
4,348,099✔
1102
}
1103

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

1110
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
4,576,262✔
1111

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

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

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

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

1137
  SStmTaskStatus* pTask = NULL;
4,348,099✔
1138
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
4,348,099✔
1139
  for (int32_t i = 0; i < trigReaderNum; ++i) {
9,020,539✔
1140
    pTask = taosArrayGet(pStatus->trigReaders, i);
4,672,440✔
1141
  
1142
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
4,672,440✔
1143
    if (code == TSDB_CODE_SUCCESS) {
4,672,440✔
1144
      (*numOfRows)++;
4,672,440✔
1145
    }
1146
  }
1147

1148
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
4,348,099✔
1149
  for (int32_t i = 0; i < trigReaderNum; ++i) {
4,614,265✔
1150
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
266,166✔
1151
  
1152
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
266,166✔
1153
    if (code == TSDB_CODE_SUCCESS) {
266,166✔
1154
      (*numOfRows)++;
266,166✔
1155
    }
1156
  }
1157

1158
  if (pStatus->calcReaders) {
4,348,099✔
1159
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
4,348,099✔
1160
    SListNode* pNode = listHead(pStatus->calcReaders);
4,348,099✔
1161
    for (int32_t i = 0; i < calcReaderNum; ++i) {
12,982,228✔
1162
      pTask = (SStmTaskStatus*)pNode->data;
8,634,129✔
1163
    
1164
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
8,634,129✔
1165
      if (code == TSDB_CODE_SUCCESS) {
8,634,129✔
1166
        (*numOfRows)++;
8,634,129✔
1167
      }
1168
      pNode = TD_DLIST_NODE_NEXT(pNode);
8,634,129✔
1169
    }
1170
  }
1171

1172
  if (pStatus->triggerTask) {
4,348,099✔
1173
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
4,340,972✔
1174
    if (code == TSDB_CODE_SUCCESS) {
4,340,972✔
1175
      (*numOfRows)++;
4,340,972✔
1176
    }
1177
  }
1178

1179
  int32_t runnerNum = 0;
4,348,099✔
1180
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
17,392,396✔
1181
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
13,044,297✔
1182
    for (int32_t m = 0; m < runnerNum; ++m) {
27,239,931✔
1183
      pTask = taosArrayGet(pStatus->runners[i], m);
14,195,634✔
1184
    
1185
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
14,195,634✔
1186
      if (code == TSDB_CODE_SUCCESS) {
14,195,634✔
1187
        (*numOfRows)++;
14,195,634✔
1188
      }
1189
    }
1190
  }
1191
  
1192
  pBlock->info.rows = *numOfRows;
4,348,099✔
1193

1194
_exit:
4,576,262✔
1195

1196
  if (statusLocked) {
4,576,262✔
1197
    taosRUnLockLatch(&pStatus->resetLock);
4,348,099✔
1198
  }
1199
  
1200
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
4,576,262✔
1201

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

1209

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

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

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

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

1238
_exit:
4,016✔
1239

1240
  taosArrayDestroy(userRecalcList);
4,016✔
1241

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

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

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

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

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

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

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

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

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

1388
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
441,512✔
1389
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
187,207✔
1390
    *uid = pScanNode->uid;
187,207✔
1391
  }
1392
  
1393
_exit:
441,054✔
1394

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

1399
  nodesDestroyNode((SNode *)pSubplan);
441,512✔
1400

1401
  return code;
441,512✔
1402
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc