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

taosdata / TDengine / #5000

22 Mar 2026 10:21AM UTC coverage: 72.307% (-0.003%) from 72.31%
#5000

push

travis-ci

web-flow
feat(subq/some): some/any/exists for stream subq (#34860)

50 of 68 new or added lines in 1 file covered. (73.53%)

614 existing lines in 138 files now uncovered.

253462 of 350536 relevant lines covered (72.31%)

134798164.58 hits per line

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

78.31
/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) {
26,450,895✔
26
  if (readLock) {
26,450,895✔
27
    while (taosRTryLockLatch(pLock)) {
23,937,616✔
28
      taosMsleep(1);
282,980✔
29
    }
30

31
    return true;
23,654,636✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
2,796,259✔
35

36
  return true;
2,796,259✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
202,365✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
202,365✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
809,460✔
48
    taosArrayDestroy(pStatus->runners[i]);
607,095✔
49
    pStatus->runners[i] = NULL;
607,095✔
50
  }
51
}
202,365✔
52

53

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

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

86
void mstDestroyScanAddrList(void* param) {
446,857✔
87
  if (NULL == param) {
446,857✔
88
    return;
×
89
  }
90
  SArray* pList = *(SArray**)param;
446,857✔
91
  taosArrayDestroy(pList);
446,857✔
92
}
93

94
void mstDestroySStmSnodeTasksDeploy(void* param) {
51,603✔
95
  SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)param;
51,603✔
96
  taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
51,603✔
97
  taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
51,603✔
98
}
51,603✔
99

100
void mstDestroySStmVgTasksToDeploy(void* param) {
148,829✔
101
  SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)param;
148,829✔
102
  taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
148,829✔
103
}
148,829✔
104

105
void mstDestroySStmSnodeStatus(void* param) {
34,140✔
106
  SStmSnodeStatus* pSnode = (SStmSnodeStatus*)param;
34,140✔
107
  taosHashCleanup(pSnode->streamTasks);
34,140✔
108
}
34,140✔
109

110
void mstDestroySStmVgroupStatus(void* param) {
83,998✔
111
  SStmVgroupStatus* pVg = (SStmVgroupStatus*)param;
83,998✔
112
  taosHashCleanup(pVg->streamTasks);
83,998✔
113
}
83,998✔
114

115
void mstFreeTrigOReaderList(void* param) {
53,533✔
116
  SArray** ppList = (SArray**)param;
53,533✔
117
  taosArrayDestroy(*ppList);
53,533✔
118
}
53,533✔
119

120
void mstResetSStmStatus(SStmStatus* pStatus) {
194,680✔
121
  (void)mstWaitLock(&pStatus->resetLock, false);
194,680✔
122

123
  taosArrayDestroy(pStatus->trigReaders);
194,680✔
124
  pStatus->trigReaders = NULL;
194,680✔
125
  taosArrayDestroyEx(pStatus->trigOReaders, mstFreeTrigOReaderList);
194,680✔
126
  pStatus->trigOReaders = NULL;
194,680✔
127
  pStatus->calcReaders = tdListFree(pStatus->calcReaders);
194,680✔
128
  if (pStatus->triggerTask) {
194,680✔
129
    (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, false);
194,128✔
130
    taosMemoryFreeClear(pStatus->triggerTask->detailStatus);
194,128✔
131
    taosWUnLockLatch(&pStatus->triggerTask->detailStatusLock);
194,128✔
132
  }
133
  taosMemoryFreeClear(pStatus->triggerTask);
194,680✔
134
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
778,720✔
135
    taosArrayDestroy(pStatus->runners[i]);
584,040✔
136
    pStatus->runners[i] = NULL;
584,040✔
137
  }
138
  pStatus->lastTrigMgmtReqId = 0;
194,680✔
139

140
  taosWUnLockLatch(&pStatus->resetLock);
194,680✔
141
}
194,680✔
142

143
void mstDestroySStmStatus(void* param) {
187,535✔
144
  SStmStatus* pStatus = (SStmStatus*)param;
187,535✔
145
  taosMemoryFreeClear(pStatus->streamName);
187,535✔
146

147
  mstResetSStmStatus(pStatus);
187,535✔
148

149
  taosWLockLatch(&pStatus->userRecalcLock);
187,535✔
150
  taosArrayDestroy(pStatus->userRecalcList);
187,535✔
151
  taosWUnLockLatch(&pStatus->userRecalcLock);
187,535✔
152

153
  tFreeSCMCreateStreamReq(pStatus->pCreate);
187,535✔
154
  taosMemoryFreeClear(pStatus->pCreate);  
187,535✔
155
}
187,535✔
156

157
void mstDestroySStmAction(void* param) {
303,038✔
158
  SStmAction* pAction = (SStmAction*)param;
303,038✔
159

160
  taosArrayDestroy(pAction->undeploy.taskList);
303,038✔
161
  taosArrayDestroy(pAction->recalc.recalcList);
303,038✔
162
}
303,038✔
163

164
void mstClearSStmStreamDeploy(SStmStreamDeploy* pDeploy) {
264,938✔
165
  pDeploy->readerTasks = NULL;
264,938✔
166
  pDeploy->triggerTask = NULL;
264,938✔
167
  pDeploy->runnerTasks = NULL;
264,938✔
168
}
264,938✔
169

170
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
245,973✔
171
  SSdb   *pSdb = pMnode->pSdb;
245,973✔
172
  void   *pIter = NULL;
245,973✔
173
  
174
  while (1) {
524,365✔
175
    SStreamObj *pStream = NULL;
770,338✔
176
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
770,338✔
177
    if (pIter == NULL) break;
770,338✔
178

179
    if (pStream->pCreate->streamId == streamId) {
580,519✔
180
      *dropped = pStream->userDropped ? true : false;
56,154✔
181
      sdbRelease(pSdb, pStream);
56,154✔
182
      sdbCancelFetch(pSdb, pIter);
56,154✔
183
      mstsDebug("stream found, dropped:%d", *dropped);
56,154✔
184
      return TSDB_CODE_SUCCESS;
56,154✔
185
    }
186
    
187
    sdbRelease(pSdb, pStream);
524,365✔
188
  }
189

190
  *dropped = true;
189,819✔
191

192
  return TSDB_CODE_SUCCESS;
189,819✔
193
}
194

195
typedef struct SStmCheckDbInUseCtx {
196
  bool* dbStream;
197
  bool* vtableStream;
198
  bool  ignoreCurrDb;
199
} SStmCheckDbInUseCtx;
200

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

207
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
38,028✔
208
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
38,028✔
209
    return true;
18,837✔
210
  }
211
  
212
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
19,191✔
213
    *pCtx->dbStream = true;
662✔
214
    return false;
662✔
215
  }
216

217
  int32_t calcDBNum = taosArrayGetSize(pStream->pCreate->calcDB);
18,529✔
218
  for (int32_t i = 0; i < calcDBNum; ++i) {
36,598✔
219
    char* calcDB = taosArrayGetP(pStream->pCreate->calcDB, i);
18,529✔
220
    if (0 == strcmp(calcDB, p1)) {
18,529✔
221
      *pCtx->dbStream = true;
460✔
222
      return false;
460✔
223
    }
224
  }
225

226
  if (pStream->pCreate->vtableCalc || STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
18,069✔
227
    *pCtx->vtableStream = true;
10,042✔
228
    return true;
10,042✔
229
  }
230
  
231
  return true;
8,027✔
232
}
233

234
void mstCheckDbInUse(SMnode *pMnode, char *dbFName, bool *dbStream, bool *vtableStream, bool ignoreCurrDb) {
708,191✔
235
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
708,191✔
236
  if (streamNum <= 0) {
708,191✔
237
    return;
698,934✔
238
  }
239

240
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
9,257✔
241
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
9,257✔
242
}
243

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

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

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

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

272
  return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
273
}
274

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

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

288
bool mndStreamActionDequeue(SStmActionQ* pQueue, SStmQNode **param) {
668,297✔
289
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
668,297✔
290
    return false;
466,482✔
291
  }
292

293
  SStmQNode *orig = pQueue->head;
201,815✔
294

295
  SStmQNode *node = pQueue->head->next;
201,815✔
296
  pQueue->head = pQueue->head->next;
201,815✔
297

298
  *param = node;
201,815✔
299

300
  taosMemoryFreeClear(orig);
201,815✔
301

302
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
201,815✔
303

304
  return true;
201,815✔
305
}
306

307
void mndStreamActionEnqueue(SStmActionQ* pQueue, SStmQNode* param) {
201,815✔
308
  taosWLockLatch(&pQueue->lock);
201,815✔
309
  pQueue->tail->next = param;
201,815✔
310
  pQueue->tail = param;
201,815✔
311
  taosWUnLockLatch(&pQueue->lock);
201,815✔
312

313
  (void)atomic_add_fetch_64(&pQueue->qRemainNum, 1);
201,815✔
314
}
201,815✔
315

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

332
  return "UNKNOWN";
×
333
}
334

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

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

352
  mndStreamActionEnqueue(actionQ, pNode);
200,343✔
353

354
  mstsDebug("stream action %s posted enqueue", mstGetStreamActionString(action));
200,343✔
355
}
356

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

365
  pNode->type = action;
1,472✔
366
  pNode->streamAct = false;
1,472✔
367
  pNode->action.task = *pAction;
1,472✔
368
  
369
  pNode->next = NULL;
1,472✔
370

371
  mndStreamActionEnqueue(actionQ, pNode);
1,472✔
372
}
373

374
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
63,435✔
375
  int32_t iter = 0;
63,435✔
376
  SDBVgHashInfo* pVg = NULL;
63,435✔
377
  void* p = NULL;
63,435✔
378
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
294,205✔
379
    pVg = (SDBVgHashInfo*)p;
230,770✔
380
    taosArrayDestroy(pVg->vgArray);
230,770✔
381
  }
382
  
383
  tSimpleHashCleanup(pDbVgs);
63,435✔
384
}
63,435✔
385

386

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

397
  SSHashObj* pDbVgroup = tSimpleHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
63,435✔
398
  TSDB_CHECK_NULL(pDbVgroup, code, lino, _exit, terrno);
63,435✔
399

400
  while (1) {
386,620✔
401
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
450,055✔
402
    if (pIter == NULL) {
450,055✔
403
      break;
63,435✔
404
    }
405

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

431
      pTarget = pNew;
230,770✔
432
    } else {
433
      pTarget = pDbInfo->vgArray;
155,850✔
434
    }
435

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

444
    if (NULL == pDbInfo) {
386,620✔
445
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
230,770✔
446
      if (code) {
230,770✔
447
        sdbRelease(pMnode->pSdb, pVgroup);
×
448
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
449
        pVgroup = NULL;
×
450
        TAOS_CHECK_EXIT(code);
×
451
      }
452
      pNew = NULL;
230,770✔
453
    }
454

455
    sdbRelease(pMnode->pSdb, pVgroup);
386,620✔
456
    pVgroup = NULL;
386,620✔
457
  }
458

459
  *ppRes = pDbVgroup;
63,435✔
460
  
461
_exit:
63,435✔
462

463
  taosArrayDestroy(pNew);
63,435✔
464

465
  if (code) {
63,435✔
466
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
467
  }
468

469
  return code;
63,435✔
470
}
471

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

481
  return 0;
×
482
}
483

484
int32_t mstTableHashValueComp(void const* lp, void const* rp) {
256,282✔
485
  uint32_t*    key = (uint32_t*)lp;
256,282✔
486
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
256,282✔
487

488
  if (*key < pVg->hashBegin) {
256,282✔
489
    return -1;
×
490
  } else if (*key > pVg->hashEnd) {
256,282✔
491
    return 1;
69,576✔
492
  }
493

494
  return 0;
186,706✔
495
}
496

497

498
int32_t mstGetTableVgId(SSHashObj* pDbVgroups, char* dbFName, char *tbName, int32_t* vgId) {
186,706✔
499
  int32_t code = 0;
186,706✔
500
  int32_t lino = 0;
186,706✔
501
  SVgroupInfo* vgInfo = NULL;
186,706✔
502
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
186,706✔
503

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

514
  if (!dbInfo->vgSorted) {
186,706✔
515
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
63,665✔
516
    dbInfo->vgSorted = true;
63,665✔
517
  }
518

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

526
  *vgId = vgInfo->vgId;
186,706✔
527

528
_exit:
186,706✔
529

530
  if (code) {
186,706✔
531
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
532
  }
533

534
  return code;
186,706✔
535
}
536

537

538
void mstLogSStreamObj(char* tips, SStreamObj* p) {
192,814✔
539
  if (!(stDebugFlag & DEBUG_DEBUG)) {
192,814✔
540
    return;
23,460✔
541
  }
542
  
543
  if (NULL == p) {
169,354✔
544
    mstDebug("%s: stream is NULL", tips);
×
545
    return;
×
546
  }
547

548
  mstDebug("%s: stream obj", tips);
169,354✔
549
  mstDebug("name:%s mainSnodeId:%d userDropped:%d userStopped:%d createTime:%" PRId64 " updateTime:%" PRId64,
169,354✔
550
      p->name, p->mainSnodeId, p->userDropped, p->userStopped, p->createTime, p->updateTime);
551

552
  SCMCreateStreamReq* q = p->pCreate;
169,354✔
553
  if (NULL == q) {
169,354✔
554
    mstDebug("stream pCreate is NULL");
×
555
    return;
×
556
  }
557

558
  int64_t streamId = q->streamId;
169,354✔
559
  int32_t calcDBNum = taosArrayGetSize(q->calcDB);
169,354✔
560
  int32_t calcScanNum = taosArrayGetSize(q->calcScanPlanList);
169,354✔
561
  int32_t notifyUrlNum = taosArrayGetSize(q->pNotifyAddrUrls);
169,354✔
562
  int32_t outColNum = taosArrayGetSize(q->outCols);
169,354✔
563
  int32_t outTagNum = taosArrayGetSize(q->outTags);
169,354✔
564
  int32_t forceOutColNum = taosArrayGetSize(q->forceOutCols);
169,354✔
565

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

581
  switch (q->triggerType) {
169,354✔
582
    case WINDOW_TYPE_INTERVAL: {
66,234✔
583
      SSlidingTrigger* t = &q->trigger.sliding;
66,234✔
584
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
66,234✔
585
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
586
      break;
66,234✔
587
    }  
588
    case WINDOW_TYPE_SESSION: {
8,338✔
589
      SSessionTrigger* t = &q->trigger.session;
8,338✔
590
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
8,338✔
591
      break;
8,338✔
592
    }
593
    case WINDOW_TYPE_STATE: {
45,297✔
594
      SStateWinTrigger* t = &q->trigger.stateWin;
45,297✔
595
      mstsDebug(
45,297✔
596
          "state trigger options, slotId:%d, expr:%s, extend:%d, zeroth:%s, trueForType: %d, trueForCount: %d, "
597
          "trueForDuration:%" PRId64,
598
          t->slotId, (char*)t->expr, t->extend, (char*)t->zeroth, t->trueForType, t->trueForCount, t->trueForDuration);
599
      break;
45,297✔
600
    }
601
    case WINDOW_TYPE_EVENT:{
21,298✔
602
      SEventTrigger* t = &q->trigger.event;
21,298✔
603
      mstsDebug(
21,298✔
604
          "event trigger options, startCond:%s, endCond:%s, trueForType: %d, trueForCount: %d, "
605
          "trueForDuration:%" PRId64,
606
          (char*)t->startCond, (char*)t->endCond, t->trueForType, t->trueForCount, t->trueForDuration);
607
      break;
21,298✔
608
    }
609
    case WINDOW_TYPE_COUNT: {
17,572✔
610
      SCountTrigger* t = &q->trigger.count;
17,572✔
611
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
17,572✔
612
      break;
17,572✔
613
    }
614
    case WINDOW_TYPE_PERIOD: {
10,615✔
615
      SPeriodTrigger* t = &q->trigger.period;
10,615✔
616
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
10,615✔
617
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
618
      break;
10,615✔
619
    }
620
    default:
×
621
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
622
      break;
×
623
  }
624

625
  mstsDebugL("create_info: triggerCols:[%s]", (char*)q->triggerCols);
169,354✔
626

627
  mstsDebugL("create_info: partitionCols:[%s]", (char*)q->partitionCols);
169,354✔
628

629
  mstsDebugL("create_info: triggerScanPlan:[%s]", (char*)q->triggerScanPlan);
169,354✔
630

631
  mstsDebugL("create_info: calcPlan:[%s]", (char*)q->calcPlan);
169,354✔
632

633
  mstsDebugL("create_info: subTblNameExpr:[%s]", (char*)q->subTblNameExpr);
169,354✔
634

635
  mstsDebugL("create_info: tagValueExpr:[%s]", (char*)q->tagValueExpr);
169,354✔
636

637

638
  for (int32_t i = 0; i < calcDBNum; ++i) {
335,401✔
639
    char* dbName = taosArrayGetP(q->calcDB, i);
166,047✔
640
    mstsDebug("create_info: calcDB[%d] - %s", i, dbName);
166,047✔
641
  }
642

643
  for (int32_t i = 0; i < calcScanNum; ++i) {
600,943✔
644
    SStreamCalcScan* pScan = taosArrayGet(q->calcScanPlanList, i);
431,589✔
645
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
431,589✔
646
    mstsDebugL("create_info: calcScanPlan[%d] - readFromCache:%d vgNum:%d scanPlan:[%s]", i, pScan->readFromCache, vgNum, (char*)pScan->scanPlan);
431,589✔
647
    for (int32_t v = 0; v < vgNum; ++v) {
863,178✔
648
      mstsDebug("create_info: calcScanPlan[%d] vg[%d] - vgId:%d", i, v, *(int32_t*)taosArrayGet(pScan->vgList, v));
431,589✔
649
    }
650
  }
651

652
  for (int32_t i = 0; i < notifyUrlNum; ++i) {
222,699✔
653
    char* url = taosArrayGetP(q->pNotifyAddrUrls, i);
53,345✔
654
    mstsDebug("create_info: notifyUrl[%d] - %s", i, url);
53,345✔
655
  }
656

657
  for (int32_t i = 0; i < outColNum; ++i) {
844,428✔
658
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
675,074✔
659
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
675,074✔
660
        i, o->name, o->type, o->flags, o->bytes, o->compress, o->typeMod);
661
  }
662
      
663
}
664

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

671
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
194,128✔
672
  if (!(stDebugFlag & DEBUG_DEBUG)) {
194,128✔
673
    return;
23,920✔
674
  }
675
  
676
  if (NULL == p) {
170,208✔
677
    mstsDebug("%s: stream status is NULL", tips);
×
678
    return;
×
679
  }
680

681
  int32_t trigReaderNum = taosArrayGetSize(p->trigReaders);
170,208✔
682
  int32_t trigOReaderNum = msmGetTrigOReaderSize(p->trigOReaders);
170,208✔
683
  int32_t calcReaderNum = MST_LIST_SIZE(p->calcReaders);
170,208✔
684
  int32_t triggerNum = p->triggerTask ? 1 : 0;
170,208✔
685
  int32_t runnerNum = 0;
170,208✔
686

687
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
670,911✔
688
    runnerNum += taosArrayGetSize(p->runners[i]);
500,703✔
689
  }
690

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

697
  SStmTaskStatus* pTask = NULL;
170,208✔
698
  for (int32_t i = 0; i < trigReaderNum; ++i) {
388,454✔
699
    pTask = taosArrayGet(p->trigReaders, i);
218,246✔
700
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
218,246✔
701
  }
702

703
  for (int32_t i = 0; i < trigOReaderNum; ++i) {
170,208✔
704
    pTask = msmGetTrigOReader(p->trigOReaders, i);
×
705
    mstLogSStmTaskStatus("trigOReader task", streamId, pTask, i);
×
706
  }
707

708
  SListNode* pNode = listHead(p->calcReaders);
170,208✔
709
  for (int32_t i = 0; i < calcReaderNum; ++i) {
543,343✔
710
    pTask = (SStmTaskStatus*)pNode->data;
373,135✔
711
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
373,135✔
712
    pNode = TD_DLIST_NODE_NEXT(pNode);
373,135✔
713
  }
714

715
  if (triggerNum > 0) {
170,208✔
716
    mstLogSStmTaskStatus("trigger task", streamId, p->triggerTask, 0);
170,208✔
717
  }
718

719
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
670,911✔
720
    int32_t num = taosArrayGetSize(p->runners[i]);
500,703✔
721
    if (num <= 0) {
500,703✔
722
      continue;
×
723
    }
724
    
725
    mstsDebug("the %dth deploy runners status", i);
500,703✔
726
    for (int32_t m = 0; m < num; ++m) {
1,109,457✔
727
      pTask = taosArrayGet(p->runners[i], m);
608,754✔
728
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
608,754✔
729
    }
730
  }
731
      
732
}
733

734
bool mstEventPassIsolation(int32_t num, int32_t event) {
3,805,676✔
735
  bool ret = ((mStreamMgmt.lastTs[event].ts + num * MST_SHORT_ISOLATION_DURATION) <= mStreamMgmt.hCtx.currentTs);
3,805,676✔
736
  if (ret) {
3,805,676✔
737
    mstDebug("event %s passed %d isolation, last:%" PRId64 ", curr:%" PRId64, 
2,967,864✔
738
        gMndStreamEvent[event], num, mStreamMgmt.lastTs[event].ts, mStreamMgmt.hCtx.currentTs);
739
  }
740

741
  return ret;
3,805,676✔
742
}
743

744
bool mstEventHandledChkSet(int32_t event) {
2,967,864✔
745
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
2,967,864✔
746
    mstDebug("event %s set handled", gMndStreamEvent[event]);
179,975✔
747
    return true;
179,975✔
748
  }
749
  return false;
2,787,889✔
750
}
751

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

761
  if (atomic_load_8(&pStream->userDropped)) {
617,539✔
762
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_DROPPING], statusSize);
×
763
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
×
764
    return TSDB_CODE_SUCCESS;
×
765
  }
766

767
  if (atomic_load_8(&pStream->userStopped)) {
617,539✔
768
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
4,755✔
769
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
4,755✔
770
    return TSDB_CODE_SUCCESS;
4,755✔
771
  }
772

773
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
612,784✔
774
  
775
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &pStream->pCreate->streamId, sizeof(pStream->pCreate->streamId));
612,784✔
776
  if (NULL == pStatus) {
612,784✔
777
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
130,007✔
778
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
130,007✔
779
    goto _exit;
130,007✔
780
  }
781

782
  char tmpBuf[256];
482,777✔
783
  int8_t stopped = atomic_load_8(&pStatus->stopped);
482,777✔
784
  switch (stopped) {
482,777✔
785
    case 1:
16,573✔
786
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
16,573✔
787
      snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s, Failed times: %" PRId64, tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes);
16,573✔
788
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
16,573✔
789
      goto _exit;
16,573✔
790
      break;
791
    case 4:
×
792
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
×
793
      snprintf(tmpBuf, sizeof(tmpBuf), "Error: %s", tstrerror(TSDB_CODE_GRANT_STREAM_EXPIRED));
×
794
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
795
      goto _exit;
×
796
      break;
797
    default:
466,204✔
798
      break;
466,204✔
799
  }
800

801
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
466,204✔
802
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_RUNNING], statusSize);
219,240✔
803
    tstrncpy(tmpBuf, "Running start from: ", sizeof(tmpBuf));
219,240✔
804
    int32_t offset = strlen(tmpBuf);
219,240✔
805
    (void)formatTimestampLocal(tmpBuf + offset, sizeof(tmpBuf) - offset, pStatus->triggerTask->runningStartTs,
219,240✔
806
                               TSDB_TIME_PRECISION_MILLI);
807
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
219,240✔
808
    goto _exit;
219,240✔
809
  }
810

811
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_INIT], statusSize);
246,964✔
812
  snprintf(tmpBuf, sizeof(tmpBuf), "Current deploy times: %" PRId64, pStatus->deployTimes);
246,964✔
813
  STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
246,964✔
814
  goto _exit;
246,964✔
815

816
_exit:
612,784✔
817
  
818
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
612,784✔
819

820
  return TSDB_CODE_SUCCESS;
612,784✔
821
}
822

823
int32_t mstSetStreamAttrResBlock(SMnode *pMnode, SStreamObj* pStream, SSDataBlock* pBlock, int32_t numOfRows) {
617,721✔
824
  int32_t code = 0;
617,721✔
825
  int32_t cols = 0;
617,721✔
826
  int32_t lino = 0;
617,721✔
827

828
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
617,721✔
829
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
617,721✔
830
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
831
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
832

833
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
617,721✔
834
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
835

836
  // db_name
837
  char streamDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
617,721✔
838
  STR_WITH_MAXSIZE_TO_VARSTR(streamDB, mndGetDbStr(pStream->pCreate->streamDB), sizeof(streamDB));
617,721✔
839
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
840
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
841
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&streamDB, false);
617,721✔
842
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
843

844
  // create time
845
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
846
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
847
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->createTime, false);
617,721✔
848
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
849

850
  // stream id
851
  char streamId2[19] = {0};
617,721✔
852
  char streamId[19 + VARSTR_HEADER_SIZE] = {0};
617,721✔
853
  snprintf(streamId2, sizeof(streamId2), "%" PRIx64, pStream->pCreate->streamId);
617,721✔
854
  STR_WITH_MAXSIZE_TO_VARSTR(streamId, streamId2, sizeof(streamId));
617,721✔
855
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
856
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
857
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamId, false);
617,721✔
858
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
859

860
  // sql
861
  char sql[TSDB_INS_STREAM_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
617,721✔
862
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->pCreate->sql, sizeof(sql));
617,721✔
863
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
864
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
865
  code = colDataSetVal(pColInfo, numOfRows, (const char*)sql, false);
617,721✔
866
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
867

868
  // status
869
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
617,721✔
870
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
617,721✔
871
  code = mstGetStreamStatusStr(pStream, status, sizeof(status), msg, sizeof(msg));
617,721✔
872
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
873

874
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
875
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
876
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
617,721✔
877
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
878

879
  // snodeLeader
880
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
881
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
882
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->mainSnodeId, false);
617,721✔
883
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
884

885
  // snodeReplica
886
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
887
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
888
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, pStream->mainSnodeId);
617,721✔
889
  int32_t replicaSnodeId = pSnode ? pSnode->replicaId : -1;
617,721✔
890
  mndReleaseSnode(pMnode, pSnode);
617,721✔
891
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&replicaSnodeId, false);
617,721✔
892
  TSDB_CHECK_CODE(code, lino, _end);
617,721✔
893

894
  // msg
895
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
617,721✔
896
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
617,721✔
897
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
617,721✔
898

899
_end:
617,721✔
900
  if (code) {
617,721✔
901
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
902
  }
903
  return code;
617,721✔
904
}
905

906

907
int32_t mstGetTaskStatusStr(SStmTaskStatus* pTask, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
18,485,281✔
908
  char tmpBuf[256];
18,462,181✔
909
  
910
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[pTask->status], statusSize);
18,485,281✔
911
  if (STREAM_STATUS_FAILED == pTask->status && pTask->errCode) {
18,485,281✔
912
    snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s", tstrerror(pTask->errCode));
×
913
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
914
    return TSDB_CODE_SUCCESS;
×
915
  }
916

917
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
18,485,281✔
918
    if (pTask->detailStatus) {
2,513,980✔
919
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
1,894,889✔
920
      snprintf(tmpBuf, sizeof(tmpBuf), "Current RT/HI/RE session num: %d/%d/%d, histroy progress:%d%%, total AUTO/USER recalc num: %d/%d", 
1,894,889✔
921
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
922
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
1,894,889✔
923
      taosRUnLockLatch(&pTask->detailStatusLock);
1,894,889✔
924
      return TSDB_CODE_SUCCESS;
1,894,889✔
925
    }
926

927
    taosRUnLockLatch(&pTask->detailStatusLock);    
619,091✔
928
  }
929
  
930
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
16,590,392✔
931
  
932
  return TSDB_CODE_SUCCESS;
16,590,392✔
933
}
934

935
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
18,485,281✔
936
  switch (pTask->type) {
18,485,281✔
937
    case STREAM_READER_TASK:
8,186,596✔
938
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
8,186,596✔
939
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
3,269,402✔
940
      } else {
941
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
4,917,194✔
942
      }
943
      return TSDB_CODE_SUCCESS;
8,186,596✔
944
    case STREAM_RUNNER_TASK:
7,784,705✔
945
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
7,784,705✔
946
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
7,566,721✔
947
        return TSDB_CODE_SUCCESS;
7,566,721✔
948
      }
949
      break;
217,984✔
950
    default:
2,513,980✔
951
      break;
2,513,980✔
952
  }
953

954
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
2,731,964✔
955
  return TSDB_CODE_SUCCESS;
2,731,964✔
956
}
957

958

959
int32_t mstSetStreamTaskResBlock(SStreamObj* pStream, SStmTaskStatus* pTask, SSDataBlock* pBlock, int32_t numOfRows) {
18,485,281✔
960
  int32_t code = 0;
18,485,281✔
961
  int32_t cols = 0;
18,485,281✔
962
  int32_t lino = 0;
18,485,281✔
963

964
  // stream_name
965
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
966
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
18,485,281✔
967
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
968
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
969

970
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
18,485,281✔
971
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
972

973
  // stream id
974
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
975
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
18,485,281✔
976
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE])); 
18,485,281✔
977
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
978
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
979
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
18,485,281✔
980
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
981

982
  // task id
983
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.taskId);
18,485,281✔
984
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
18,485,281✔
985
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
986
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
987
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
18,485,281✔
988
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
989

990
  // type
991
  char type[20 + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
992
  STR_WITH_MAXSIZE_TO_VARSTR(type, (STREAM_READER_TASK == pTask->type) ? "Reader" : ((STREAM_TRIGGER_TASK == pTask->type) ? "Trigger" : "Runner"), sizeof(type));
18,485,281✔
993
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
994
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
995
  code = colDataSetVal(pColInfo, numOfRows, (const char*)type, false);
18,485,281✔
996
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
997

998
  // serious id
999
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.seriousId);
18,485,281✔
1000
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
18,485,281✔
1001
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1002
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1003
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
18,485,281✔
1004
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1005

1006
  // deploy id
1007
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1008
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1009
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.deployId, false);
18,485,281✔
1010
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1011

1012
  // node_type
1013
  char nodeType[10 + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
1014
  STR_WITH_MAXSIZE_TO_VARSTR(nodeType, (STREAM_READER_TASK == pTask->type) ? "vnode" : "snode", sizeof(nodeType));
18,485,281✔
1015
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1016
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1017
  code = colDataSetVal(pColInfo, numOfRows, (const char*)nodeType, false);
18,485,281✔
1018
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1019

1020
  // node id
1021
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1022
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1023
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.nodeId, false);
18,485,281✔
1024
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1025

1026
  // task idx
1027
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1028
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1029
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.taskIdx, false);
18,485,281✔
1030
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1031

1032
  // status
1033
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
1034
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
1035
  code = mstGetTaskStatusStr(pTask, status, sizeof(status), msg, sizeof(msg));
18,485,281✔
1036
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1037

1038
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1039
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1040
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
18,485,281✔
1041
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1042

1043
  // start time
1044
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1045
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1046
  if (pTask->runningStartTs) {
18,485,281✔
1047
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
3,325,600✔
1048
  } else {
1049
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
15,159,681✔
1050
  }
1051
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1052

1053
  // last update
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1056
  if (pTask->lastUpTs) {
18,485,281✔
1057
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, false);
18,485,281✔
1058
  } else {
UNCOV
1059
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, true);
×
1060
  }
1061
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1062

1063
  // extra info
1064
  char extra[64 + VARSTR_HEADER_SIZE] = {0};
18,485,281✔
1065
  code = mstGetTaskExtraStr(pTask, extra, sizeof(extra));
18,485,281✔
1066
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1067
  
1068
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1069
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1070
  code = colDataSetVal(pColInfo, numOfRows, (const char*)extra, false);
18,485,281✔
1071
  TSDB_CHECK_CODE(code, lino, _end);
18,485,281✔
1072

1073
  // msg
1074
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
18,485,281✔
1075
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
18,485,281✔
1076
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
18,485,281✔
1077

1078
_end:
18,485,281✔
1079
  if (code) {
18,485,281✔
1080
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1081
  }
1082
  return code;
18,485,281✔
1083
}
1084

1085
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
2,515,367✔
1086
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
2,515,367✔
1087
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,061,468✔
1088
    num += taosArrayGetSize(pStatus->runners[i]);
7,546,101✔
1089
  }
1090

1091
  return num;
2,515,367✔
1092
}
1093

1094
int32_t mstSetStreamTasksResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
2,707,718✔
1095
  int32_t code = 0;
2,707,718✔
1096
  int32_t lino = 0;
2,707,718✔
1097
  int64_t streamId = pStream->pCreate->streamId;
2,707,718✔
1098
  bool    statusLocked = false;
2,707,718✔
1099

1100
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
2,707,718✔
1101

1102
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,707,718✔
1103
  if (NULL == pStatus) {
2,707,718✔
1104
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
141,776✔
1105
    goto _exit;
141,776✔
1106
  }
1107

1108
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,565,942✔
1109
  if (stopped) {
2,565,942✔
1110
    mstsDebug("stream stopped %d, ignore it", stopped);
50,575✔
1111
    goto _exit;
50,575✔
1112
  }
1113

1114
  (void)mstWaitLock(&pStatus->resetLock, true);
2,515,367✔
1115
  statusLocked = true;
2,515,367✔
1116
  
1117
  int32_t count = mstGetNumOfStreamTasks(pStatus);
2,515,367✔
1118

1119
  if (*numOfRows + count > rowsCapacity) {
2,515,367✔
1120
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
83,146✔
1121
    if (code) {
83,146✔
1122
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1123
      TAOS_CHECK_EXIT(code);
×
1124
    }
1125
  }
1126

1127
  SStmTaskStatus* pTask = NULL;
2,515,367✔
1128
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
2,515,367✔
1129
  for (int32_t i = 0; i < trigReaderNum; ++i) {
5,297,456✔
1130
    pTask = taosArrayGet(pStatus->trigReaders, i);
2,782,089✔
1131
  
1132
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
2,782,089✔
1133
    if (code == TSDB_CODE_SUCCESS) {
2,782,089✔
1134
      (*numOfRows)++;
2,782,089✔
1135
    }
1136
  }
1137

1138
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
2,515,367✔
1139
  for (int32_t i = 0; i < trigReaderNum; ++i) {
3,002,680✔
1140
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
487,313✔
1141
  
1142
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
487,313✔
1143
    if (code == TSDB_CODE_SUCCESS) {
487,313✔
1144
      (*numOfRows)++;
487,313✔
1145
    }
1146
  }
1147

1148
  if (pStatus->calcReaders) {
2,515,367✔
1149
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
2,515,367✔
1150
    SListNode* pNode = listHead(pStatus->calcReaders);
2,515,367✔
1151
    for (int32_t i = 0; i < calcReaderNum; ++i) {
7,432,561✔
1152
      pTask = (SStmTaskStatus*)pNode->data;
4,917,194✔
1153
    
1154
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
4,917,194✔
1155
      if (code == TSDB_CODE_SUCCESS) {
4,917,194✔
1156
        (*numOfRows)++;
4,917,194✔
1157
      }
1158
      pNode = TD_DLIST_NODE_NEXT(pNode);
4,917,194✔
1159
    }
1160
  }
1161

1162
  if (pStatus->triggerTask) {
2,515,367✔
1163
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
2,513,980✔
1164
    if (code == TSDB_CODE_SUCCESS) {
2,513,980✔
1165
      (*numOfRows)++;
2,513,980✔
1166
    }
1167
  }
1168

1169
  int32_t runnerNum = 0;
2,515,367✔
1170
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,061,468✔
1171
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
7,546,101✔
1172
    for (int32_t m = 0; m < runnerNum; ++m) {
15,330,806✔
1173
      pTask = taosArrayGet(pStatus->runners[i], m);
7,784,705✔
1174
    
1175
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
7,784,705✔
1176
      if (code == TSDB_CODE_SUCCESS) {
7,784,705✔
1177
        (*numOfRows)++;
7,784,705✔
1178
      }
1179
    }
1180
  }
1181
  
1182
  pBlock->info.rows = *numOfRows;
2,515,367✔
1183

1184
_exit:
2,707,718✔
1185

1186
  if (statusLocked) {
2,707,718✔
1187
    taosRUnLockLatch(&pStatus->resetLock);
2,515,367✔
1188
  }
1189
  
1190
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
2,707,718✔
1191

1192
  if (code) {
2,707,718✔
1193
    mError("error happens when build stream tasks result block, lino:%d, code:%s", lino, tstrerror(code));
×
1194
  }
1195
  
1196
  return code;
2,707,718✔
1197
}
1198

1199

1200
int32_t mstAppendNewRecalcRange(int64_t streamId, SStmStatus *pStream, STimeWindow* pRange) {
3,396✔
1201
  int32_t code = 0;
3,396✔
1202
  int32_t lino = 0;
3,396✔
1203
  bool    locked = false;
3,396✔
1204
  SArray* userRecalcList = NULL;
3,396✔
1205

1206
  SStreamRecalcReq req = {.recalcId = 0, .start = pRange->skey, .end = pRange->ekey};
3,396✔
1207
  TAOS_CHECK_EXIT(taosGetSystemUUIDU64(&req.recalcId));
3,396✔
1208
  
1209
  taosWLockLatch(&pStream->userRecalcLock);
3,396✔
1210
  locked = true;
3,396✔
1211
  
1212
  if (NULL == pStream->userRecalcList) {
3,396✔
1213
    userRecalcList = taosArrayInit(2, sizeof(SStreamRecalcReq));
3,396✔
1214
    if (NULL == userRecalcList) {
3,396✔
1215
      TAOS_CHECK_EXIT(terrno);
×
1216
    }
1217

1218
    TSDB_CHECK_NULL(taosArrayPush(userRecalcList, &req), code, lino, _exit, terrno);
3,396✔
1219

1220
    atomic_store_ptr(&pStream->userRecalcList, userRecalcList);
3,396✔
1221
    userRecalcList = NULL;    
3,396✔
1222
  } else {
1223
    TSDB_CHECK_NULL(taosArrayPush(pStream->userRecalcList, &req), code, lino, _exit, terrno);
×
1224
  }
1225
  
1226
  mstsInfo("stream recalc ID:%" PRIx64 " range:%" PRId64 " - %" PRId64 " added", req.recalcId, pRange->skey, pRange->ekey);
3,396✔
1227

1228
_exit:
3,396✔
1229

1230
  taosArrayDestroy(userRecalcList);
3,396✔
1231

1232
  if (locked) {
3,396✔
1233
    taosWUnLockLatch(&pStream->userRecalcLock);
3,396✔
1234
  }
1235
  
1236
  if (code) {
3,396✔
1237
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1238
  }
1239
  
1240
  return code;
3,396✔
1241
}
1242

1243

1244

1245
int32_t mstSetStreamRecalculateResBlock(SStreamObj* pStream, SSTriggerRecalcProgress* pProgress, SSDataBlock* pBlock, int32_t numOfRows) {
×
1246
  int32_t code = 0;
×
1247
  int32_t cols = 0;
×
1248
  int32_t lino = 0;
×
1249

1250
  // stream_name
1251
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1252
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
×
1253
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1254
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1255

1256
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
×
1257
  TSDB_CHECK_CODE(code, lino, _end);
×
1258

1259
  // stream id
1260
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
×
1261
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
×
1262
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE); 
×
1263
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1264
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1265
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1266
  TSDB_CHECK_CODE(code, lino, _end);
×
1267

1268
  // recalc id
1269
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pProgress->recalcId);
×
1270
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1271
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1272
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1273
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1274
  TSDB_CHECK_CODE(code, lino, _end);
×
1275

1276
  // start
1277
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1278
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1279
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->start, false);
×
1280
  TSDB_CHECK_CODE(code, lino, _end);
×
1281

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

1288
  // progress
1289
  char progress[20 + VARSTR_HEADER_SIZE] = {0};
×
1290
  snprintf(&progress[VARSTR_HEADER_SIZE], sizeof(progress) - VARSTR_HEADER_SIZE, "%d%%", pProgress->progress);
×
1291
  varDataSetLen(progress, strlen(&progress[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1292
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1293
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1294
  code = colDataSetVal(pColInfo, numOfRows, (const char*)progress, false);
×
1295
  TSDB_CHECK_CODE(code, lino, _end);
×
1296

1297
_end:
×
1298
  if (code) {
×
1299
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1300
  }
1301
  return code;
×
1302
}
1303

1304

1305
int32_t mstSetStreamRecalculatesResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
274✔
1306
  int32_t code = 0;
274✔
1307
  int32_t lino = 0;
274✔
1308
  int64_t streamId = pStream->pCreate->streamId;
274✔
1309

1310
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
274✔
1311

1312
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
274✔
1313
  if (NULL == pStatus) {
274✔
1314
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
×
1315
    goto _exit;
×
1316
  }
1317

1318
  int8_t stopped = atomic_load_8(&pStatus->stopped);
274✔
1319
  if (stopped) {
274✔
1320
    mstsDebug("stream stopped %d, ignore it", stopped);
×
1321
    goto _exit;
×
1322
  }
1323

1324
  if (NULL == pStatus->triggerTask) {
274✔
1325
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1326
    goto _exit;
×
1327
  }
1328

1329
  (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, true);
274✔
1330
  if (NULL == pStatus->triggerTask->detailStatus) {
274✔
1331
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1332
    taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1333
    goto _exit;
×
1334
  }
1335

1336
  SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pStatus->triggerTask->detailStatus;
274✔
1337
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
274✔
1338

1339
  if (*numOfRows + count > rowsCapacity) {
274✔
1340
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
×
1341
    if (code) {
×
1342
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1343
      taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1344
      TAOS_CHECK_EXIT(code);
×
1345
    }
1346
  }
1347

1348
  for (int32_t i = 0; i < count; ++i) {
274✔
1349
    SSTriggerRecalcProgress* pProgress = taosArrayGet(pTrigger->userRecalcs, i);
×
1350
  
1351
    code = mstSetStreamRecalculateResBlock(pStream, pProgress, pBlock, *numOfRows);
×
1352
    if (code == TSDB_CODE_SUCCESS) {
×
1353
      (*numOfRows)++;
×
1354
    }
1355
  }
1356

1357
  taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
274✔
1358
  
1359
  pBlock->info.rows = *numOfRows;
274✔
1360

1361
_exit:
274✔
1362
  
1363
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
274✔
1364

1365
  if (code) {
274✔
1366
    mError("error happens when build stream recalculates result block, lino:%d, code:%s", lino, tstrerror(code));
×
1367
  }
1368
  
1369
  return code;
274✔
1370
}
1371

1372
int32_t mstGetScanUidFromPlan(int64_t streamId, void* scanPlan, int64_t* uid) {
389,661✔
1373
  SSubplan* pSubplan = NULL;
389,661✔
1374
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
389,661✔
1375
  
1376
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
389,661✔
1377

1378
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
389,661✔
1379
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
150,354✔
1380
    *uid = pScanNode->uid;
150,354✔
1381
  }
1382
  
1383
_exit:
389,240✔
1384

1385
  if (code) {
389,661✔
1386
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1387
  }
1388

1389
  nodesDestroyNode((SNode *)pSubplan);
389,661✔
1390

1391
  return code;
389,661✔
1392
}
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