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

taosdata / TDengine / #5032

24 Apr 2026 11:25AM UTC coverage: 73.076% (+0.2%) from 72.876%
#5032

push

travis-ci

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

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

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

527 existing lines in 138 files now uncovered.

275965 of 377640 relevant lines covered (73.08%)

132797765.0 hits per line

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

79.75
/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) {
31,990,469✔
26
  if (readLock) {
31,990,469✔
27
    while (taosRTryLockLatch(pLock)) {
29,409,606✔
28
      taosMsleep(1);
2,095,236✔
29
    }
30

31
    return true;
27,314,370✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
4,676,099✔
35

36
  return true;
4,676,099✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
212,788✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
212,788✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
851,152✔
48
    taosArrayDestroy(pStatus->runners[i]);
638,364✔
49
    pStatus->runners[i] = NULL;
638,364✔
50
  }
51
}
212,788✔
52

53

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

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

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

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

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

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

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

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

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

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

142
  taosWUnLockLatch(&pStatus->resetLock);
206,595✔
143
}
206,595✔
144

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

149
  mstResetSStmStatus(pStatus);
199,112✔
150

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

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

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

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

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

172
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
256,044✔
173
  SSdb   *pSdb = pMnode->pSdb;
256,044✔
174
  void   *pIter = NULL;
256,044✔
175
  
176
  while (1) {
535,033✔
177
    SStreamObj *pStream = NULL;
791,077✔
178
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
791,077✔
179
    if (pIter == NULL) break;
791,077✔
180

181
    if (pStream->pCreate->streamId == streamId) {
595,577✔
182
      *dropped = pStream->userDropped ? true : false;
60,544✔
183
      sdbRelease(pSdb, pStream);
60,544✔
184
      sdbCancelFetch(pSdb, pIter);
60,544✔
185
      mstsDebug("stream found, dropped:%d", *dropped);
60,544✔
186
      return TSDB_CODE_SUCCESS;
60,544✔
187
    }
188
    
189
    sdbRelease(pSdb, pStream);
535,033✔
190
  }
191

192
  *dropped = true;
195,500✔
193

194
  return TSDB_CODE_SUCCESS;
195,500✔
195
}
196

197
typedef struct SStmCheckDbInUseCtx {
198
  bool* dbStream;
199
  bool* vtableStream;
200
  bool  ignoreCurrDb;
201
} SStmCheckDbInUseCtx;
202

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

209
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
39,142✔
210
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
39,142✔
211
    return true;
19,570✔
212
  }
213
  
214
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
19,572✔
215
    *pCtx->dbStream = true;
662✔
216
    return false;
662✔
217
  }
218

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

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

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

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

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

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

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

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

274
  return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
275
}
276

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

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

290
bool mndStreamActionDequeue(SStmActionQ* pQueue, SStmQNode **param) {
727,498✔
291
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
727,498✔
292
    return false;
505,917✔
293
  }
294

295
  SStmQNode *orig = pQueue->head;
221,581✔
296

297
  SStmQNode *node = pQueue->head->next;
221,581✔
298
  pQueue->head = pQueue->head->next;
221,581✔
299

300
  *param = node;
221,581✔
301

302
  taosMemoryFreeClear(orig);
221,581✔
303

304
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
221,581✔
305

306
  return true;
221,581✔
307
}
308

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

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

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

334
  return "UNKNOWN";
×
335
}
336

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

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

354
  mndStreamActionEnqueue(actionQ, pNode);
214,063✔
355

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

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

367
  pNode->type = action;
7,518✔
368
  pNode->streamAct = false;
7,518✔
369
  pNode->action.task = *pAction;
7,518✔
370
  
371
  pNode->next = NULL;
7,518✔
372

373
  mndStreamActionEnqueue(actionQ, pNode);
7,518✔
374
}
375

376
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
64,795✔
377
  int32_t iter = 0;
64,795✔
378
  SDBVgHashInfo* pVg = NULL;
64,795✔
379
  void* p = NULL;
64,795✔
380
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
303,707✔
381
    pVg = (SDBVgHashInfo*)p;
238,912✔
382
    taosArrayDestroy(pVg->vgArray);
238,912✔
383
  }
384
  
385
  tSimpleHashCleanup(pDbVgs);
64,795✔
386
}
64,795✔
387

388

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

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

402
  while (1) {
397,229✔
403
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
462,024✔
404
    if (pIter == NULL) {
462,024✔
405
      break;
64,795✔
406
    }
407

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

433
      pTarget = pNew;
238,912✔
434
    } else {
435
      pTarget = pDbInfo->vgArray;
158,317✔
436
    }
437

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

446
    if (NULL == pDbInfo) {
397,229✔
447
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
238,912✔
448
      if (code) {
238,912✔
449
        sdbRelease(pMnode->pSdb, pVgroup);
×
450
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
451
        pVgroup = NULL;
×
452
        TAOS_CHECK_EXIT(code);
×
453
      }
454
      pNew = NULL;
238,912✔
455
    }
456

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

461
  *ppRes = pDbVgroup;
64,795✔
462
  
463
_exit:
64,795✔
464

465
  taosArrayDestroy(pNew);
64,795✔
466

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

471
  return code;
64,795✔
472
}
473

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

483
  return 0;
×
484
}
485

486
int32_t mstTableHashValueComp(void const* lp, void const* rp) {
261,678✔
487
  uint32_t*    key = (uint32_t*)lp;
261,678✔
488
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
261,678✔
489

490
  if (*key < pVg->hashBegin) {
261,678✔
491
    return -1;
×
492
  } else if (*key > pVg->hashEnd) {
261,678✔
493
    return 1;
70,986✔
494
  }
495

496
  return 0;
190,692✔
497
}
498

499

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

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

516
  if (!dbInfo->vgSorted) {
190,692✔
517
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
65,042✔
518
    dbInfo->vgSorted = true;
65,042✔
519
  }
520

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

528
  *vgId = vgInfo->vgId;
190,692✔
529

530
_exit:
190,692✔
531

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

536
  return code;
190,692✔
537
}
538

539

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

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

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

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

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

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

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

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

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

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

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

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

639

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

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

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

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

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

673
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
206,004✔
674
  if (!(stDebugFlag & DEBUG_DEBUG)) {
206,004✔
675
    return;
22,720✔
676
  }
677
  
678
  if (NULL == p) {
183,284✔
679
    mstsDebug("%s: stream status is NULL", tips);
×
680
    return;
×
681
  }
682

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

689
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
722,942✔
690
    runnerNum += taosArrayGetSize(p->runners[i]);
539,658✔
691
  }
692

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

699
  SStmTaskStatus* pTask = NULL;
183,284✔
700
  for (int32_t i = 0; i < trigReaderNum; ++i) {
415,688✔
701
    pTask = taosArrayGet(p->trigReaders, i);
232,404✔
702
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
232,404✔
703
  }
704

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

710
  SListNode* pNode = listHead(p->calcReaders);
183,284✔
711
  for (int32_t i = 0; i < calcReaderNum; ++i) {
561,300✔
712
    pTask = (SStmTaskStatus*)pNode->data;
378,016✔
713
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
378,016✔
714
    pNode = TD_DLIST_NODE_NEXT(pNode);
378,016✔
715
  }
716

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

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

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

743
  return ret;
5,116,023✔
744
}
745

746
bool mstEventHandledChkSet(int32_t event) {
4,000,866✔
747
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
4,000,866✔
748
    mstDebug("event %s set handled", gMndStreamEvent[event]);
208,965✔
749
    return true;
208,965✔
750
  }
751
  return false;
3,791,901✔
752
}
753

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

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

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

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

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

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

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

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

827
  return TSDB_CODE_SUCCESS;
653,817✔
828
}
829

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

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

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

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

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

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

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

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

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

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

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

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

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

913

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

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

934
    taosRUnLockLatch(&pTask->detailStatusLock);    
464,695✔
935
  }
936
  
937
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
18,042,571✔
938
  
939
  return TSDB_CODE_SUCCESS;
18,042,571✔
940
}
941

942
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
20,026,821✔
943
  switch (pTask->type) {
20,026,821✔
944
    case STREAM_READER_TASK:
9,363,207✔
945
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
9,363,207✔
946
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
2,939,763✔
947
      } else {
948
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
6,423,444✔
949
      }
950
      return TSDB_CODE_SUCCESS;
9,363,207✔
951
    case STREAM_RUNNER_TASK:
8,214,839✔
952
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
8,214,839✔
953
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
8,001,067✔
954
        return TSDB_CODE_SUCCESS;
8,001,067✔
955
      }
956
      break;
213,772✔
957
    default:
2,448,775✔
958
      break;
2,448,775✔
959
  }
960

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

965

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

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

977
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
20,026,821✔
978
  TSDB_CHECK_CODE(code, lino, _end);
20,026,821✔
979

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

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

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

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

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

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

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

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

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

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

1050
  // start time
1051
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
20,026,821✔
1052
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
20,026,821✔
1053
  if (pTask->runningStartTs) {
20,026,821✔
1054
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
3,778,852✔
1055
  } else {
1056
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
16,247,969✔
1057
  }
1058
  TSDB_CHECK_CODE(code, lino, _end);
20,026,821✔
1059

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

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

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

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

1092
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
2,454,165✔
1093
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
2,454,165✔
1094
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
9,816,660✔
1095
    num += taosArrayGetSize(pStatus->runners[i]);
7,362,495✔
1096
  }
1097

1098
  return num;
2,454,165✔
1099
}
1100

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

1107
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
2,655,035✔
1108

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

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

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

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

1134
  SStmTaskStatus* pTask = NULL;
2,454,165✔
1135
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
2,454,165✔
1136
  for (int32_t i = 0; i < trigReaderNum; ++i) {
5,120,364✔
1137
    pTask = taosArrayGet(pStatus->trigReaders, i);
2,666,199✔
1138
  
1139
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
2,666,199✔
1140
    if (code == TSDB_CODE_SUCCESS) {
2,666,199✔
1141
      (*numOfRows)++;
2,666,199✔
1142
    }
1143
  }
1144

1145
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
2,454,165✔
1146
  for (int32_t i = 0; i < trigReaderNum; ++i) {
2,727,729✔
1147
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
273,564✔
1148
  
1149
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
273,564✔
1150
    if (code == TSDB_CODE_SUCCESS) {
273,564✔
1151
      (*numOfRows)++;
273,564✔
1152
    }
1153
  }
1154

1155
  if (pStatus->calcReaders) {
2,454,165✔
1156
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
2,454,165✔
1157
    SListNode* pNode = listHead(pStatus->calcReaders);
2,454,165✔
1158
    for (int32_t i = 0; i < calcReaderNum; ++i) {
8,877,609✔
1159
      pTask = (SStmTaskStatus*)pNode->data;
6,423,444✔
1160
    
1161
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
6,423,444✔
1162
      if (code == TSDB_CODE_SUCCESS) {
6,423,444✔
1163
        (*numOfRows)++;
6,423,444✔
1164
      }
1165
      pNode = TD_DLIST_NODE_NEXT(pNode);
6,423,444✔
1166
    }
1167
  }
1168

1169
  if (pStatus->triggerTask) {
2,454,165✔
1170
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
2,448,775✔
1171
    if (code == TSDB_CODE_SUCCESS) {
2,448,775✔
1172
      (*numOfRows)++;
2,448,775✔
1173
    }
1174
  }
1175

1176
  int32_t runnerNum = 0;
2,454,165✔
1177
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
9,816,660✔
1178
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
7,362,495✔
1179
    for (int32_t m = 0; m < runnerNum; ++m) {
15,577,334✔
1180
      pTask = taosArrayGet(pStatus->runners[i], m);
8,214,839✔
1181
    
1182
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
8,214,839✔
1183
      if (code == TSDB_CODE_SUCCESS) {
8,214,839✔
1184
        (*numOfRows)++;
8,214,839✔
1185
      }
1186
    }
1187
  }
1188
  
1189
  pBlock->info.rows = *numOfRows;
2,454,165✔
1190

1191
_exit:
2,655,035✔
1192

1193
  if (statusLocked) {
2,655,035✔
1194
    taosRUnLockLatch(&pStatus->resetLock);
2,454,165✔
1195
  }
1196
  
1197
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
2,655,035✔
1198

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

1206

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

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

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

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

1235
_exit:
3,420✔
1236

1237
  taosArrayDestroy(userRecalcList);
3,420✔
1238

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

1250

1251

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

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

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

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

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

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

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

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

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

1311

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

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

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

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

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

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

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

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

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

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

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

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

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

1385
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
388,788✔
1386
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
159,650✔
1387
    *uid = pScanNode->uid;
159,650✔
1388
  }
1389
  
1390
_exit:
388,316✔
1391

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

1396
  nodesDestroyNode((SNode *)pSubplan);
388,788✔
1397

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

© 2026 Coveralls, Inc