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

taosdata / TDengine / #4987

16 Mar 2026 12:26PM UTC coverage: 73.883% (+36.6%) from 37.305%
#4987

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

209 of 391 new or added lines in 24 files covered. (53.45%)

3062 existing lines in 140 files now uncovered.

261133 of 353439 relevant lines covered (73.88%)

121262425.02 hits per line

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

78.39
/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) {
29,872,288✔
26
  if (readLock) {
29,872,288✔
27
    while (taosRTryLockLatch(pLock)) {
27,020,907✔
28
      taosMsleep(1);
75,840✔
29
    }
30

31
    return true;
26,945,067✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
2,927,221✔
35

36
  return true;
2,927,221✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
197,869✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
197,869✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
791,476✔
48
    taosArrayDestroy(pStatus->runners[i]);
593,607✔
49
    pStatus->runners[i] = NULL;
593,607✔
50
  }
51
}
197,869✔
52

53

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

59
void mstDestroySStmTaskToDeployExt(void* param) {
1,408,166✔
60
  SStmTaskToDeployExt* pExt = (SStmTaskToDeployExt*)param;
1,408,166✔
61
  if (pExt->deployed) {
1,408,166✔
62
    return;
1,408,166✔
63
  }
64
  
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;
×
75
    case STREAM_READER_TASK:
×
76
      if (!pExt->deploy.msg.reader.triggerReader) {
×
UNCOV
77
        SStreamReaderDeployFromCalc* pCalcReaderDeploy = &pExt->deploy.msg.reader.msg.calc;
×
UNCOV
78
        taosMemoryFreeClear(pCalcReaderDeploy->calcScanPlan);
×
79
      }
80
      break;
×
81
    default:  
×
82
      break;;
×
83
  }
84
}
85

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

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

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

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

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

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

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

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

140
  taosWUnLockLatch(&pStatus->resetLock);
191,882✔
141
}
191,882✔
142

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

147
  mstResetSStmStatus(pStatus);
185,599✔
148

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

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

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

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

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

170
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
197,362✔
171
  SSdb   *pSdb = pMnode->pSdb;
197,362✔
172
  void   *pIter = NULL;
197,362✔
173
  
174
  while (1) {
388,988✔
175
    SStreamObj *pStream = NULL;
586,350✔
176
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
586,350✔
177
    if (pIter == NULL) break;
586,350✔
178

179
    if (pStream->pCreate->streamId == streamId) {
431,809✔
180
      *dropped = pStream->userDropped ? true : false;
42,821✔
181
      sdbRelease(pSdb, pStream);
42,821✔
182
      sdbCancelFetch(pSdb, pIter);
42,821✔
183
      mstsDebug("stream found, dropped:%d", *dropped);
42,821✔
184
      return TSDB_CODE_SUCCESS;
42,821✔
185
    }
186
    
187
    sdbRelease(pSdb, pStream);
388,988✔
188
  }
189

190
  *dropped = true;
154,541✔
191

192
  return TSDB_CODE_SUCCESS;
154,541✔
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) {
59,213✔
202
  SStreamObj *pStream = pObj;
59,213✔
203
  if (atomic_load_8(&pStream->userDropped)) {
59,213✔
UNCOV
204
    return true;
×
205
  }
206

207
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
59,213✔
208
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
59,213✔
209
    return true;
19,474✔
210
  }
211
  
212
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
39,739✔
213
    *pCtx->dbStream = true;
932✔
214
    return false;
932✔
215
  }
216

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

226
  if (pStream->pCreate->vtableCalc || STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
38,083✔
227
    *pCtx->vtableStream = true;
10,448✔
228
    return true;
10,448✔
229
  }
230
  
231
  return true;
27,635✔
232
}
233

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

240
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
11,106✔
241
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
11,106✔
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);
×
UNCOV
247
  } else if (status == STREAM_STATUS_RUNNING) {
×
248
    tstrncpy(dst, "running", bufLen);
×
UNCOV
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
  }
UNCOV
254
}
×
255

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

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

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

272
  return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
273
}
274

UNCOV
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

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

288
bool mndStreamActionDequeue(SStmActionQ* pQueue, SStmQNode **param) {
669,546✔
289
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
669,546✔
290
    return false;
469,017✔
291
  }
292

293
  SStmQNode *orig = pQueue->head;
200,529✔
294

295
  SStmQNode *node = pQueue->head->next;
200,529✔
296
  pQueue->head = pQueue->head->next;
200,529✔
297

298
  *param = node;
200,529✔
299

300
  taosMemoryFreeClear(orig);
200,529✔
301

302
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
200,529✔
303

304
  return true;
200,529✔
305
}
306

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

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

316
char* mstGetStreamActionString(int32_t action) {
170,363✔
317
  switch (action) {
170,363✔
318
    case STREAM_ACT_DEPLOY:
170,363✔
319
      return "DEPLOY";
170,363✔
320
    case STREAM_ACT_UNDEPLOY:
×
321
      return "UNDEPLOY";
×
322
    case STREAM_ACT_START:
×
323
      return "START";
×
UNCOV
324
    case STREAM_ACT_UPDATE_TRIGGER:
×
UNCOV
325
      return "UPDATE TRIGGER";
×
326
    case STREAM_ACT_RECALC:
×
UNCOV
327
      return "USER RECALC";
×
UNCOV
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) {
199,419✔
336
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
199,419✔
337
  if (NULL == pNode) {
199,419✔
338
    taosMemoryFreeClear(param);
×
339
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
340
    return;
×
341
  }
342

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

352
  mndStreamActionEnqueue(actionQ, pNode);
199,419✔
353

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

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

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

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

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

386

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

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

400
  while (1) {
381,642✔
401
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
443,756✔
402
    if (pIter == NULL) {
443,756✔
403
      break;
62,114✔
404
    }
405

406
    pDbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1);
381,642✔
407
    if (NULL == pDbInfo) {
381,642✔
408
      pNew = taosArrayInit(20, sizeof(SVGroupHashInfo));
230,012✔
409
      if (NULL == pNew) {
230,012✔
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,012✔
417
      if (NULL == pDb) {
230,012✔
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,012✔
424
      dbInfo.hashMethod = pDb->cfg.hashMethod;
230,012✔
425
      dbInfo.hashPrefix = pDb->cfg.hashPrefix;
230,012✔
426
      dbInfo.hashSuffix = pDb->cfg.hashSuffix;
230,012✔
427
      dbInfo.vgArray = pNew;
230,012✔
428
      
429
      mndReleaseDb(pMnode, pDb);
230,012✔
430

431
      pTarget = pNew;
230,012✔
432
    } else {
433
      pTarget = pDbInfo->vgArray;
151,630✔
434
    }
435

436
    SVGroupHashInfo vgInfo = {.vgId = pVgroup->vgId, .hashBegin = pVgroup->hashBegin, .hashEnd = pVgroup->hashEnd};
381,642✔
437
    if (NULL == taosArrayPush(pTarget, &vgInfo)) {
381,642✔
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) {
381,642✔
445
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
230,012✔
446
      if (code) {
230,012✔
UNCOV
447
        sdbRelease(pMnode->pSdb, pVgroup);
×
UNCOV
448
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
449
        pVgroup = NULL;
×
450
        TAOS_CHECK_EXIT(code);
×
451
      }
452
      pNew = NULL;
230,012✔
453
    }
454

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

459
  *ppRes = pDbVgroup;
62,114✔
460
  
461
_exit:
62,114✔
462

463
  taosArrayDestroy(pNew);
62,114✔
464

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

469
  return code;
62,114✔
470
}
471

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

UNCOV
481
  return 0;
×
482
}
483

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

488
  if (*key < pVg->hashBegin) {
252,780✔
UNCOV
489
    return -1;
×
490
  } else if (*key > pVg->hashEnd) {
252,780✔
491
    return 1;
68,699✔
492
  }
493

494
  return 0;
184,081✔
495
}
496

497

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

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

514
  if (!dbInfo->vgSorted) {
184,081✔
515
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
62,350✔
516
    dbInfo->vgSorted = true;
62,350✔
517
  }
518

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

526
  *vgId = vgInfo->vgId;
184,081✔
527

528
_exit:
184,081✔
529

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

534
  return code;
184,081✔
535
}
536

537

538
void mstLogSStreamObj(char* tips, SStreamObj* p) {
191,491✔
539
  if (!(stDebugFlag & DEBUG_DEBUG)) {
191,491✔
540
    return;
28,332✔
541
  }
542
  
543
  if (NULL == p) {
163,159✔
UNCOV
544
    mstDebug("%s: stream is NULL", tips);
×
UNCOV
545
    return;
×
546
  }
547

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

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

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

566
  mstsDebugL("create_info: name:%s sql:%s streamDB:%s triggerDB:%s outDB:%s calcDBNum:%d triggerTblName:%s outTblName:%s "
163,159✔
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) {
163,159✔
582
    case WINDOW_TYPE_INTERVAL: {
62,349✔
583
      SSlidingTrigger* t = &q->trigger.sliding;
62,349✔
584
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
62,349✔
585
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
586
      break;
62,349✔
587
    }  
588
    case WINDOW_TYPE_SESSION: {
8,102✔
589
      SSessionTrigger* t = &q->trigger.session;
8,102✔
590
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
8,102✔
591
      break;
8,102✔
592
    }
593
    case WINDOW_TYPE_STATE: {
45,647✔
594
      SStateWinTrigger* t = &q->trigger.stateWin;
45,647✔
595
      mstsDebug(
45,647✔
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,647✔
600
    }
601
    case WINDOW_TYPE_EVENT:{
21,998✔
602
      SEventTrigger* t = &q->trigger.event;
21,998✔
603
      mstsDebug(
21,998✔
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,998✔
608
    }
609
    case WINDOW_TYPE_COUNT: {
14,396✔
610
      SCountTrigger* t = &q->trigger.count;
14,396✔
611
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
14,396✔
612
      break;
14,396✔
613
    }
614
    case WINDOW_TYPE_PERIOD: {
10,667✔
615
      SPeriodTrigger* t = &q->trigger.period;
10,667✔
616
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
10,667✔
617
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
618
      break;
10,667✔
619
    }
UNCOV
620
    default:
×
621
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
UNCOV
622
      break;
×
623
  }
624

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

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

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

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

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

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

637

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

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

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

657
  for (int32_t i = 0; i < outColNum; ++i) {
817,264✔
658
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
654,105✔
659
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
654,105✔
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,186,426✔
666
  mstsDebug("%s[%d]: task %" PRIx64 " deployId:%d SID:%" PRId64 " nodeId:%d tidx:%d type:%s flags:%" PRIx64 " status:%s lastUpTs:%" PRId64, 
1,186,426✔
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,186,426✔
670

671
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
191,327✔
672
  if (!(stDebugFlag & DEBUG_DEBUG)) {
191,327✔
673
    return;
28,812✔
674
  }
675
  
676
  if (NULL == p) {
162,515✔
677
    mstsDebug("%s: stream status is NULL", tips);
×
678
    return;
×
679
  }
680

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

687
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
640,256✔
688
    runnerNum += taosArrayGetSize(p->runners[i]);
477,741✔
689
  }
690

691
  mstsDebug("%s: stream status", tips);
162,515✔
692
  mstsDebug("name:%s runnerNum:%d runnerDeploys:%d runnerReplica:%d lastActionTs:%" PRId64
162,515✔
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;
162,515✔
698
  for (int32_t i = 0; i < trigReaderNum; ++i) {
370,774✔
699
    pTask = taosArrayGet(p->trigReaders, i);
208,259✔
700
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
208,259✔
701
  }
702

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

708
  SListNode* pNode = listHead(p->calcReaders);
162,515✔
709
  for (int32_t i = 0; i < calcReaderNum; ++i) {
435,194✔
710
    pTask = (SStmTaskStatus*)pNode->data;
272,679✔
711
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
272,679✔
712
    pNode = TD_DLIST_NODE_NEXT(pNode);
272,679✔
713
  }
714

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

719
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
640,256✔
720
    int32_t num = taosArrayGetSize(p->runners[i]);
477,741✔
721
    if (num <= 0) {
477,741✔
722
      continue;
×
723
    }
724
    
725
    mstsDebug("the %dth deploy runners status", i);
477,741✔
726
    for (int32_t m = 0; m < num; ++m) {
1,020,714✔
727
      pTask = taosArrayGet(p->runners[i], m);
542,973✔
728
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
542,973✔
729
    }
730
  }
731
      
732
}
733

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

741
  return ret;
3,901,375✔
742
}
743

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

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

761
  if (atomic_load_8(&pStream->userDropped)) {
597,293✔
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)) {
597,293✔
768
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
4,806✔
769
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
4,806✔
770
    return TSDB_CODE_SUCCESS;
4,806✔
771
  }
772

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

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

801
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
438,046✔
802
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_RUNNING], statusSize);
208,594✔
803
    strcpy(tmpBuf, "Running start from: ");
208,594✔
804
    (void)formatTimestampLocal(&tmpBuf[strlen(tmpBuf)], pStatus->triggerTask->runningStartTs, TSDB_TIME_PRECISION_MILLI);
208,594✔
805
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
208,594✔
806
    goto _exit;
208,594✔
807
  }
808

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

814
_exit:
592,487✔
815
  
816
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
592,487✔
817

818
  return TSDB_CODE_SUCCESS;
592,487✔
819
}
820

821
int32_t mstSetStreamAttrResBlock(SMnode *pMnode, SStreamObj* pStream, SSDataBlock* pBlock, int32_t numOfRows) {
597,524✔
822
  int32_t code = 0;
597,524✔
823
  int32_t cols = 0;
597,524✔
824
  int32_t lino = 0;
597,524✔
825

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

831
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
597,524✔
832
  TSDB_CHECK_CODE(code, lino, _end);
597,524✔
833

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

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

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

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

866
  // status
867
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
597,524✔
868
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
597,524✔
869
  code = mstGetStreamStatusStr(pStream, status, sizeof(status), msg, sizeof(msg));
597,524✔
870
  TSDB_CHECK_CODE(code, lino, _end);
597,524✔
871

872
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
597,524✔
873
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
597,524✔
874
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
597,524✔
875
  TSDB_CHECK_CODE(code, lino, _end);
597,524✔
876

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

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

892
  // msg
893
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
597,524✔
894
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
597,524✔
895
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
597,524✔
896

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

904

905
int32_t mstGetTaskStatusStr(SStmTaskStatus* pTask, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
20,749,247✔
906
  char tmpBuf[256];
20,720,687✔
907
  
908
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[pTask->status], statusSize);
20,749,247✔
909
  if (STREAM_STATUS_FAILED == pTask->status && pTask->errCode) {
20,749,247✔
910
    snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s", tstrerror(pTask->errCode));
×
911
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
912
    return TSDB_CODE_SUCCESS;
×
913
  }
914

915
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
20,749,247✔
916
    if (pTask->detailStatus) {
2,883,340✔
917
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
2,347,282✔
918
      snprintf(tmpBuf, sizeof(tmpBuf), "Current RT/HI/RE session num: %d/%d/%d, histroy progress:%d%%, total AUTO/USER recalc num: %d/%d", 
2,347,282✔
919
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
920
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
2,347,282✔
921
      taosRUnLockLatch(&pTask->detailStatusLock);
2,347,282✔
922
      return TSDB_CODE_SUCCESS;
2,347,282✔
923
    }
924

925
    taosRUnLockLatch(&pTask->detailStatusLock);    
536,058✔
926
  }
927
  
928
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
18,401,965✔
929
  
930
  return TSDB_CODE_SUCCESS;
18,401,965✔
931
}
932

933
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
20,749,247✔
934
  switch (pTask->type) {
20,749,247✔
935
    case STREAM_READER_TASK:
9,218,232✔
936
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
9,218,232✔
937
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
4,085,585✔
938
      } else {
939
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
5,132,647✔
940
      }
941
      return TSDB_CODE_SUCCESS;
9,218,232✔
942
    case STREAM_RUNNER_TASK:
8,647,675✔
943
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
8,647,675✔
944
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
8,427,623✔
945
        return TSDB_CODE_SUCCESS;
8,427,623✔
946
      }
947
      break;
220,052✔
948
    default:
2,883,340✔
949
      break;
2,883,340✔
950
  }
951

952
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
3,103,392✔
953
  return TSDB_CODE_SUCCESS;
3,103,392✔
954
}
955

956

957
int32_t mstSetStreamTaskResBlock(SStreamObj* pStream, SStmTaskStatus* pTask, SSDataBlock* pBlock, int32_t numOfRows) {
20,749,247✔
958
  int32_t code = 0;
20,749,247✔
959
  int32_t cols = 0;
20,749,247✔
960
  int32_t lino = 0;
20,749,247✔
961

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

968
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
20,749,247✔
969
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
970

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

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

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

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

1004
  // deploy id
1005
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1006
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1007
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.deployId, false);
20,749,247✔
1008
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1009

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

1018
  // node id
1019
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1020
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1021
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.nodeId, false);
20,749,247✔
1022
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1023

1024
  // task idx
1025
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1026
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1027
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.taskIdx, false);
20,749,247✔
1028
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1029

1030
  // status
1031
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
20,749,247✔
1032
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
20,749,247✔
1033
  code = mstGetTaskStatusStr(pTask, status, sizeof(status), msg, sizeof(msg));
20,749,247✔
1034
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1035

1036
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1037
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1038
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
20,749,247✔
1039
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1040

1041
  // start time
1042
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1043
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1044
  if (pTask->runningStartTs) {
20,749,247✔
1045
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
4,781,837✔
1046
  } else {
1047
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
15,967,410✔
1048
  }
1049
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1050

1051
  // last update
1052
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1053
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1054
  if (pTask->lastUpTs) {
20,749,247✔
1055
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, false);
20,748,307✔
1056
  } else {
1057
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, true);
940✔
1058
  }
1059
  TSDB_CHECK_CODE(code, lino, _end);
20,749,247✔
1060

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

1071
  // msg
1072
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,749,247✔
1073
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,749,247✔
1074
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
20,749,247✔
1075

1076
_end:
20,749,247✔
1077
  if (code) {
20,749,247✔
1078
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1079
  }
1080
  return code;
20,749,247✔
1081
}
1082

1083
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
2,886,744✔
1084
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
2,886,744✔
1085
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
11,546,976✔
1086
    num += taosArrayGetSize(pStatus->runners[i]);
8,660,232✔
1087
  }
1088

1089
  return num;
2,886,744✔
1090
}
1091

1092
int32_t mstSetStreamTasksResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
3,078,749✔
1093
  int32_t code = 0;
3,078,749✔
1094
  int32_t lino = 0;
3,078,749✔
1095
  int64_t streamId = pStream->pCreate->streamId;
3,078,749✔
1096
  bool    statusLocked = false;
3,078,749✔
1097

1098
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
3,078,749✔
1099

1100
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,078,749✔
1101
  if (NULL == pStatus) {
3,078,749✔
1102
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
140,447✔
1103
    goto _exit;
140,447✔
1104
  }
1105

1106
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,938,302✔
1107
  if (stopped) {
2,938,302✔
1108
    mstsDebug("stream stopped %d, ignore it", stopped);
51,558✔
1109
    goto _exit;
51,558✔
1110
  }
1111

1112
  (void)mstWaitLock(&pStatus->resetLock, true);
2,886,744✔
1113
  statusLocked = true;
2,886,744✔
1114
  
1115
  int32_t count = mstGetNumOfStreamTasks(pStatus);
2,886,744✔
1116

1117
  if (*numOfRows + count > rowsCapacity) {
2,886,744✔
1118
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
100,667✔
1119
    if (code) {
100,667✔
1120
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1121
      TAOS_CHECK_EXIT(code);
×
1122
    }
1123
  }
1124

1125
  SStmTaskStatus* pTask = NULL;
2,886,744✔
1126
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
2,886,744✔
1127
  for (int32_t i = 0; i < trigReaderNum; ++i) {
6,082,750✔
1128
    pTask = taosArrayGet(pStatus->trigReaders, i);
3,196,006✔
1129
  
1130
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
3,196,006✔
1131
    if (code == TSDB_CODE_SUCCESS) {
3,196,006✔
1132
      (*numOfRows)++;
3,196,006✔
1133
    }
1134
  }
1135

1136
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
2,886,744✔
1137
  for (int32_t i = 0; i < trigReaderNum; ++i) {
3,776,323✔
1138
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
889,579✔
1139
  
1140
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
889,579✔
1141
    if (code == TSDB_CODE_SUCCESS) {
889,579✔
1142
      (*numOfRows)++;
889,579✔
1143
    }
1144
  }
1145

1146
  if (pStatus->calcReaders) {
2,886,744✔
1147
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
2,886,744✔
1148
    SListNode* pNode = listHead(pStatus->calcReaders);
2,886,744✔
1149
    for (int32_t i = 0; i < calcReaderNum; ++i) {
8,019,391✔
1150
      pTask = (SStmTaskStatus*)pNode->data;
5,132,647✔
1151
    
1152
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
5,132,647✔
1153
      if (code == TSDB_CODE_SUCCESS) {
5,132,647✔
1154
        (*numOfRows)++;
5,132,647✔
1155
      }
1156
      pNode = TD_DLIST_NODE_NEXT(pNode);
5,132,647✔
1157
    }
1158
  }
1159

1160
  if (pStatus->triggerTask) {
2,886,744✔
1161
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
2,883,340✔
1162
    if (code == TSDB_CODE_SUCCESS) {
2,883,340✔
1163
      (*numOfRows)++;
2,883,340✔
1164
    }
1165
  }
1166

1167
  int32_t runnerNum = 0;
2,886,744✔
1168
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
11,546,976✔
1169
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
8,660,232✔
1170
    for (int32_t m = 0; m < runnerNum; ++m) {
17,307,907✔
1171
      pTask = taosArrayGet(pStatus->runners[i], m);
8,647,675✔
1172
    
1173
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
8,647,675✔
1174
      if (code == TSDB_CODE_SUCCESS) {
8,647,675✔
1175
        (*numOfRows)++;
8,647,675✔
1176
      }
1177
    }
1178
  }
1179
  
1180
  pBlock->info.rows = *numOfRows;
2,886,744✔
1181

1182
_exit:
3,078,749✔
1183

1184
  if (statusLocked) {
3,078,749✔
1185
    taosRUnLockLatch(&pStatus->resetLock);
2,886,744✔
1186
  }
1187
  
1188
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
3,078,749✔
1189

1190
  if (code) {
3,078,749✔
UNCOV
1191
    mError("error happens when build stream tasks result block, lino:%d, code:%s", lino, tstrerror(code));
×
1192
  }
1193
  
1194
  return code;
3,078,749✔
1195
}
1196

1197

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

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

1216
    TSDB_CHECK_NULL(taosArrayPush(userRecalcList, &req), code, lino, _exit, terrno);
3,624✔
1217

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

1226
_exit:
3,624✔
1227

1228
  taosArrayDestroy(userRecalcList);
3,624✔
1229

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

1241

1242

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

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

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

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

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

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

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

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

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

1302

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

1308
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
275✔
1309

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

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

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

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

1334
  SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pStatus->triggerTask->detailStatus;
275✔
1335
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
275✔
1336

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

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

1355
  taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
275✔
1356
  
1357
  pBlock->info.rows = *numOfRows;
275✔
1358

1359
_exit:
275✔
1360
  
1361
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
275✔
1362

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

1370
int32_t mstGetScanUidFromPlan(int64_t streamId, void* scanPlan, int64_t* uid) {
291,785✔
1371
  SSubplan* pSubplan = NULL;
291,785✔
1372
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
291,785✔
1373
  
1374
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
291,785✔
1375

1376
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
291,785✔
1377
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
129,329✔
1378
    *uid = pScanNode->uid;
129,329✔
1379
  }
1380
  
1381
_exit:
291,273✔
1382

1383
  if (code) {
291,785✔
UNCOV
1384
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1385
  }
1386

1387
  nodesDestroyNode((SNode *)pSubplan);
291,785✔
1388

1389
  return code;
291,785✔
1390
}
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