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

taosdata / TDengine / #4930

16 Jan 2026 02:32AM UTC coverage: 66.716% (-0.007%) from 66.723%
#4930

push

travis-ci

web-flow
enh: interp supports using non-null prev/next values to fill (#34236)

281 of 327 new or added lines in 11 files covered. (85.93%)

1747 existing lines in 129 files now uncovered.

203203 of 304580 relevant lines covered (66.72%)

131619261.81 hits per line

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

79.86
/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) {
25,098,778✔
26
  if (readLock) {
25,098,778✔
27
    while (taosRTryLockLatch(pLock)) {
23,014,954✔
28
      taosMsleep(1);
82,584✔
29
    }
30

31
    return true;
22,932,370✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
2,166,408✔
35

36
  return true;
2,166,408✔
37
}
38

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

45
void mstDestroySStmSnodeStreamStatus(void* p) { 
174,154✔
46
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
174,154✔
47
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
696,616✔
48
    taosArrayDestroy(pStatus->runners[i]);
522,462✔
49
    pStatus->runners[i] = NULL;
522,462✔
50
  }
51
}
174,154✔
52

53

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

59
void mstDestroySStmTaskToDeployExt(void* param) {
1,185,177✔
60
  SStmTaskToDeployExt* pExt = (SStmTaskToDeployExt*)param;
1,185,177✔
61
  if (pExt->deployed) {
1,185,177✔
62
    return;
1,181,113✔
63
  }
64
  
65
  switch (pExt->deploy.task.type) {
4,064✔
66
    case STREAM_TRIGGER_TASK:
733✔
67
      taosArrayDestroy(pExt->deploy.msg.trigger.readerList);
733✔
68
      pExt->deploy.msg.trigger.readerList = NULL;
733✔
69
      taosArrayDestroy(pExt->deploy.msg.trigger.runnerList);
733✔
70
      pExt->deploy.msg.trigger.runnerList = NULL;
733✔
71
      break;
733✔
72
    case STREAM_RUNNER_TASK:
2,199✔
73
      taosMemoryFreeClear(pExt->deploy.msg.runner.pPlan);
2,199✔
74
      break;
2,199✔
75
    default:  
1,132✔
76
      break;;
1,132✔
77
  }
78
}
79

80
void mstDestroyScanAddrList(void* param) {
322,477✔
81
  if (NULL == param) {
322,477✔
82
    return;
×
83
  }
84
  SArray* pList = *(SArray**)param;
322,477✔
85
  taosArrayDestroy(pList);
322,477✔
86
}
87

88
void mstDestroySStmSnodeTasksDeploy(void* param) {
36,709✔
89
  SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)param;
36,709✔
90
  taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
36,709✔
91
  taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
36,709✔
92
}
36,709✔
93

94
void mstDestroySStmVgTasksToDeploy(void* param) {
91,707✔
95
  SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)param;
91,707✔
96
  taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
91,707✔
97
}
91,707✔
98

99
void mstDestroySStmSnodeStatus(void* param) {
27,800✔
100
  SStmSnodeStatus* pSnode = (SStmSnodeStatus*)param;
27,800✔
101
  taosHashCleanup(pSnode->streamTasks);
27,800✔
102
}
27,800✔
103

104
void mstDestroySStmVgroupStatus(void* param) {
62,290✔
105
  SStmVgroupStatus* pVg = (SStmVgroupStatus*)param;
62,290✔
106
  taosHashCleanup(pVg->streamTasks);
62,290✔
107
}
62,290✔
108

109
void mstFreeTrigOReaderList(void* param) {
50,992✔
110
  SArray** ppList = (SArray**)param;
50,992✔
111
  taosArrayDestroy(*ppList);
50,992✔
112
}
50,992✔
113

114
void mstResetSStmStatus(SStmStatus* pStatus) {
165,183✔
115
  (void)mstWaitLock(&pStatus->resetLock, false);
165,183✔
116

117
  taosArrayDestroy(pStatus->trigReaders);
165,183✔
118
  pStatus->trigReaders = NULL;
165,183✔
119
  taosArrayDestroyEx(pStatus->trigOReaders, mstFreeTrigOReaderList);
165,183✔
120
  pStatus->trigOReaders = NULL;
165,183✔
121
  pStatus->calcReaders = tdListFree(pStatus->calcReaders);
165,183✔
122
  if (pStatus->triggerTask) {
165,183✔
123
    (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, false);
165,067✔
124
    taosMemoryFreeClear(pStatus->triggerTask->detailStatus);
165,067✔
125
    taosWUnLockLatch(&pStatus->triggerTask->detailStatusLock);
165,067✔
126
  }
127
  taosMemoryFreeClear(pStatus->triggerTask);
165,183✔
128
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
660,732✔
129
    taosArrayDestroy(pStatus->runners[i]);
495,549✔
130
    pStatus->runners[i] = NULL;
495,549✔
131
  }
132
  pStatus->lastTrigMgmtReqId = 0;
165,183✔
133

134
  taosWUnLockLatch(&pStatus->resetLock);
165,183✔
135
}
165,183✔
136

137
void mstDestroySStmStatus(void* param) {
158,054✔
138
  SStmStatus* pStatus = (SStmStatus*)param;
158,054✔
139
  taosMemoryFreeClear(pStatus->streamName);
158,054✔
140

141
  mstResetSStmStatus(pStatus);
158,054✔
142

143
  taosWLockLatch(&pStatus->userRecalcLock);
158,054✔
144
  taosArrayDestroy(pStatus->userRecalcList);
158,054✔
145
  taosWUnLockLatch(&pStatus->userRecalcLock);
158,054✔
146

147
  tFreeSCMCreateStreamReq(pStatus->pCreate);
158,054✔
148
  taosMemoryFreeClear(pStatus->pCreate);  
158,054✔
149
}
158,054✔
150

151
void mstDestroySStmAction(void* param) {
262,628✔
152
  SStmAction* pAction = (SStmAction*)param;
262,628✔
153

154
  taosArrayDestroy(pAction->undeploy.taskList);
262,628✔
155
  taosArrayDestroy(pAction->recalc.recalcList);
262,628✔
156
}
262,628✔
157

158
void mstClearSStmStreamDeploy(SStmStreamDeploy* pDeploy) {
230,449✔
159
  pDeploy->readerTasks = NULL;
230,449✔
160
  pDeploy->triggerTask = NULL;
230,449✔
161
  pDeploy->runnerTasks = NULL;
230,449✔
162
}
230,449✔
163

164
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
129,475✔
165
  SSdb   *pSdb = pMnode->pSdb;
129,475✔
166
  void   *pIter = NULL;
129,475✔
167
  
168
  while (1) {
360,294✔
169
    SStreamObj *pStream = NULL;
489,769✔
170
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
489,769✔
171
    if (pIter == NULL) break;
489,769✔
172

173
    if (pStream->pCreate->streamId == streamId) {
414,597✔
174
      *dropped = pStream->userDropped ? true : false;
54,303✔
175
      sdbRelease(pSdb, pStream);
54,303✔
176
      sdbCancelFetch(pSdb, pIter);
54,303✔
177
      mstsDebug("stream found, dropped:%d", *dropped);
54,303✔
178
      return TSDB_CODE_SUCCESS;
54,303✔
179
    }
180
    
181
    sdbRelease(pSdb, pStream);
360,294✔
182
  }
183

184
  *dropped = true;
75,172✔
185

186
  return TSDB_CODE_SUCCESS;
75,172✔
187
}
188

189
typedef struct SStmCheckDbInUseCtx {
190
  bool* dbStream;
191
  bool* vtableStream;
192
  bool  ignoreCurrDb;
193
} SStmCheckDbInUseCtx;
194

195
static bool mstChkSetDbInUse(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
53,237✔
196
  SStreamObj *pStream = pObj;
53,237✔
197
  if (atomic_load_8(&pStream->userDropped)) {
53,237✔
198
    return true;
×
199
  }
200

201
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
53,237✔
202
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
53,237✔
203
    return true;
17,489✔
204
  }
205
  
206
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
35,748✔
207
    *pCtx->dbStream = true;
854✔
208
    return false;
854✔
209
  }
210

211
  int32_t calcDBNum = taosArrayGetSize(pStream->pCreate->calcDB);
34,894✔
212
  for (int32_t i = 0; i < calcDBNum; ++i) {
69,135✔
213
    char* calcDB = taosArrayGetP(pStream->pCreate->calcDB, i);
34,894✔
214
    if (0 == strcmp(calcDB, p1)) {
34,894✔
215
      *pCtx->dbStream = true;
653✔
216
      return false;
653✔
217
    }
218
  }
219

220
  if (pStream->pCreate->vtableCalc || STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
34,241✔
221
    *pCtx->vtableStream = true;
9,555✔
222
    return true;
9,555✔
223
  }
224
  
225
  return true;
24,686✔
226
}
227

228
void mstCheckDbInUse(SMnode *pMnode, char *dbFName, bool *dbStream, bool *vtableStream, bool ignoreCurrDb) {
639,448✔
229
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
639,448✔
230
  if (streamNum <= 0) {
639,448✔
231
    return;
629,887✔
232
  }
233

234
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
9,561✔
235
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
9,561✔
236
}
237

238
static void mstShowStreamStatus(char *dst, int8_t status, int32_t bufLen) {
×
239
  if (status == STREAM_STATUS_INIT) {
×
240
    tstrncpy(dst, "init", bufLen);
×
241
  } else if (status == STREAM_STATUS_RUNNING) {
×
242
    tstrncpy(dst, "running", bufLen);
×
243
  } else if (status == STREAM_STATUS_STOPPED) {
×
244
    tstrncpy(dst, "stopped", bufLen);
×
245
  } else if (status == STREAM_STATUS_FAILED) {
×
246
    tstrncpy(dst, "failed", bufLen);
×
247
  }
248
}
×
249

250
int32_t mstCheckSnodeExists(SMnode *pMnode) {
×
251
  SSdb      *pSdb = pMnode->pSdb;
×
252
  void      *pIter = NULL;
×
253
  SSnodeObj *pObj = NULL;
×
254

255
  while (1) {
256
    pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
257
    if (pIter == NULL) {
×
258
      break;
×
259
    }
260

261
    sdbRelease(pSdb, pObj);
×
262
    sdbCancelFetch(pSdb, pIter);
×
263
    return TSDB_CODE_SUCCESS;
×
264
  }
265

266
  return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
267
}
268

269
void mstSetTaskStatusFromMsg(SStmGrpCtx* pCtx, SStmTaskStatus* pTask, SStmTaskStatusMsg* pMsg) {
×
270
  pTask->id.taskId = pMsg->taskId;
×
271
  pTask->id.deployId = pMsg->deployId;
×
272
  pTask->id.seriousId = pMsg->seriousId;
×
273
  pTask->id.nodeId = pMsg->nodeId;
×
274
  pTask->id.taskIdx = pMsg->taskIdx;
×
275

276
  pTask->type = pMsg->type;
×
277
  pTask->flags = pMsg->flags;
×
278
  pTask->status = pMsg->status;
×
279
  pTask->lastUpTs = pCtx->currTs;
×
280
}
×
281

282
bool mndStreamActionDequeue(SStmActionQ* pQueue, SStmQNode **param) {
584,267✔
283
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
584,267✔
284
    return false;
413,731✔
285
  }
286

287
  SStmQNode *orig = pQueue->head;
170,536✔
288

289
  SStmQNode *node = pQueue->head->next;
170,536✔
290
  pQueue->head = pQueue->head->next;
170,536✔
291

292
  *param = node;
170,536✔
293

294
  taosMemoryFreeClear(orig);
170,536✔
295

296
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
170,536✔
297

298
  return true;
170,536✔
299
}
300

301
void mndStreamActionEnqueue(SStmActionQ* pQueue, SStmQNode* param) {
170,536✔
302
  taosWLockLatch(&pQueue->lock);
170,536✔
303
  pQueue->tail->next = param;
170,536✔
304
  pQueue->tail = param;
170,536✔
305
  taosWUnLockLatch(&pQueue->lock);
170,536✔
306

307
  (void)atomic_add_fetch_64(&pQueue->qRemainNum, 1);
170,536✔
308
}
170,536✔
309

310
char* mstGetStreamActionString(int32_t action) {
142,212✔
311
  switch (action) {
142,212✔
312
    case STREAM_ACT_DEPLOY:
142,212✔
313
      return "DEPLOY";
142,212✔
314
    case STREAM_ACT_UNDEPLOY:
×
315
      return "UNDEPLOY";
×
316
    case STREAM_ACT_START:
×
317
      return "START";
×
318
    case STREAM_ACT_UPDATE_TRIGGER:
×
319
      return "UPDATE TRIGGER";
×
320
    case STREAM_ACT_RECALC:
×
321
      return "USER RECALC";
×
322
    default:
×
323
      break;
×
324
  }
325

326
  return "UNKNOWN";
×
327
}
328

329
void mstPostStreamAction(SStmActionQ*       actionQ, int64_t streamId, char* streamName, void* param, bool userAction, int32_t action) {
168,272✔
330
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
168,272✔
331
  if (NULL == pNode) {
168,272✔
332
    taosMemoryFreeClear(param);
×
333
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
334
    return;
×
335
  }
336

337
  pNode->type = action;
168,272✔
338
  pNode->streamAct = true;
168,272✔
339
  pNode->action.stream.streamId = streamId;
168,272✔
340
  TAOS_STRCPY(pNode->action.stream.streamName, streamName);
168,272✔
341
  pNode->action.stream.userAction = userAction;
168,272✔
342
  pNode->action.stream.actionParam = param;
168,272✔
343
  
344
  pNode->next = NULL;
168,272✔
345

346
  mndStreamActionEnqueue(actionQ, pNode);
168,272✔
347

348
  mstsDebug("stream action %s posted enqueue", mstGetStreamActionString(action));
168,272✔
349
}
350

351
void mstPostTaskAction(SStmActionQ*        actionQ, SStmTaskAction* pAction, int32_t action) {
2,264✔
352
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
2,264✔
353
  if (NULL == pNode) {
2,264✔
354
    int64_t streamId = pAction->streamId;
×
355
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
356
    return;
×
357
  }
358

359
  pNode->type = action;
2,264✔
360
  pNode->streamAct = false;
2,264✔
361
  pNode->action.task = *pAction;
2,264✔
362
  
363
  pNode->next = NULL;
2,264✔
364

365
  mndStreamActionEnqueue(actionQ, pNode);
2,264✔
366
}
367

368
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
60,258✔
369
  int32_t iter = 0;
60,258✔
370
  SDBVgHashInfo* pVg = NULL;
60,258✔
371
  void* p = NULL;
60,258✔
372
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
280,087✔
373
    pVg = (SDBVgHashInfo*)p;
219,829✔
374
    taosArrayDestroy(pVg->vgArray);
219,829✔
375
  }
376
  
377
  tSimpleHashCleanup(pDbVgs);
60,258✔
378
}
60,258✔
379

380

381
int32_t mstBuildDBVgroupsMap(SMnode* pMnode, SSHashObj** ppRes) {
60,258✔
382
  void*   pIter = NULL;
60,258✔
383
  int32_t code = TSDB_CODE_SUCCESS;
60,258✔
384
  int32_t lino = 0;
60,258✔
385
  SArray* pTarget = NULL;
60,258✔
386
  SArray* pNew = NULL;
60,258✔
387
  SDbObj* pDb = NULL;
60,258✔
388
  SDBVgHashInfo dbInfo = {0}, *pDbInfo = NULL;
60,258✔
389
  SVgObj* pVgroup = NULL;
60,258✔
390

391
  SSHashObj* pDbVgroup = tSimpleHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
60,258✔
392
  TSDB_CHECK_NULL(pDbVgroup, code, lino, _exit, terrno);
60,258✔
393

394
  while (1) {
368,622✔
395
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
428,880✔
396
    if (pIter == NULL) {
428,880✔
397
      break;
60,258✔
398
    }
399

400
    pDbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1);
368,622✔
401
    if (NULL == pDbInfo) {
368,622✔
402
      pNew = taosArrayInit(20, sizeof(SVGroupHashInfo));
219,829✔
403
      if (NULL == pNew) {
219,829✔
404
        sdbRelease(pMnode->pSdb, pVgroup);
×
405
        sdbCancelFetch(pMnode->pSdb, pIter);
×
406
        pVgroup = NULL;
×
407
        TSDB_CHECK_NULL(pNew, code, lino, _exit, terrno);
×
408
      }
409
      
410
      pDb = mndAcquireDb(pMnode, pVgroup->dbName);
219,829✔
411
      if (NULL == pDb) {
219,829✔
412
        sdbRelease(pMnode->pSdb, pVgroup);
×
413
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
414
        pVgroup = NULL;
×
415
        TSDB_CHECK_NULL(pDb, code, lino, _exit, terrno);
×
416
      }
417
      dbInfo.vgSorted = false;
219,829✔
418
      dbInfo.hashMethod = pDb->cfg.hashMethod;
219,829✔
419
      dbInfo.hashPrefix = pDb->cfg.hashPrefix;
219,829✔
420
      dbInfo.hashSuffix = pDb->cfg.hashSuffix;
219,829✔
421
      dbInfo.vgArray = pNew;
219,829✔
422
      
423
      mndReleaseDb(pMnode, pDb);
219,829✔
424

425
      pTarget = pNew;
219,829✔
426
    } else {
427
      pTarget = pDbInfo->vgArray;
148,793✔
428
    }
429

430
    SVGroupHashInfo vgInfo = {.vgId = pVgroup->vgId, .hashBegin = pVgroup->hashBegin, .hashEnd = pVgroup->hashEnd};
368,622✔
431
    if (NULL == taosArrayPush(pTarget, &vgInfo)) {
368,622✔
432
      sdbRelease(pMnode->pSdb, pVgroup);
×
433
      sdbCancelFetch(pMnode->pSdb, pIter);      
×
434
      pVgroup = NULL;
×
435
      TSDB_CHECK_NULL(NULL, code, lino, _exit, terrno);
×
436
    }
437

438
    if (NULL == pDbInfo) {
368,622✔
439
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
219,829✔
440
      if (code) {
219,829✔
441
        sdbRelease(pMnode->pSdb, pVgroup);
×
442
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
443
        pVgroup = NULL;
×
444
        TAOS_CHECK_EXIT(code);
×
445
      }
446
      pNew = NULL;
219,829✔
447
    }
448

449
    sdbRelease(pMnode->pSdb, pVgroup);
368,622✔
450
    pVgroup = NULL;
368,622✔
451
  }
452

453
  *ppRes = pDbVgroup;
60,258✔
454
  
455
_exit:
60,258✔
456

457
  taosArrayDestroy(pNew);
60,258✔
458

459
  if (code) {
60,258✔
460
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
461
  }
462

463
  return code;
60,258✔
464
}
465

466
int mstDbVgInfoComp(const void* lp, const void* rp) {
104,368✔
467
  SVGroupHashInfo* pLeft = (SVGroupHashInfo*)lp;
104,368✔
468
  SVGroupHashInfo* pRight = (SVGroupHashInfo*)rp;
104,368✔
469
  if (pLeft->hashBegin < pRight->hashBegin) {
104,368✔
470
    return -1;
104,368✔
471
  } else if (pLeft->hashBegin > pRight->hashBegin) {
×
472
    return 1;
×
473
  }
474

475
  return 0;
×
476
}
477

478
int32_t mstTableHashValueComp(void const* lp, void const* rp) {
246,771✔
479
  uint32_t*    key = (uint32_t*)lp;
246,771✔
480
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
246,771✔
481

482
  if (*key < pVg->hashBegin) {
246,771✔
483
    return -1;
×
484
  } else if (*key > pVg->hashEnd) {
246,771✔
485
    return 1;
67,431✔
486
  }
487

488
  return 0;
179,340✔
489
}
490

491

492
int32_t mstGetTableVgId(SSHashObj* pDbVgroups, char* dbFName, char *tbName, int32_t* vgId) {
179,340✔
493
  int32_t code = 0;
179,340✔
494
  int32_t lino = 0;
179,340✔
495
  SVgroupInfo* vgInfo = NULL;
179,340✔
496
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
179,340✔
497

498
  SDBVgHashInfo* dbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroups, dbFName, strlen(dbFName) + 1);
179,340✔
499
  if (NULL == dbInfo) {
179,340✔
500
    mstError("db %s does not exist", dbFName);
×
501
    TAOS_CHECK_EXIT(TSDB_CODE_MND_DB_NOT_EXIST);
×
502
  }
503
  
504
  (void)snprintf(tbFullName, sizeof(tbFullName), "%s.%s", dbFName, tbName);
179,340✔
505
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
358,680✔
506
                                        dbInfo->hashPrefix, dbInfo->hashSuffix);
179,340✔
507

508
  if (!dbInfo->vgSorted) {
179,340✔
509
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
60,473✔
510
    dbInfo->vgSorted = true;
60,473✔
511
  }
512

513
  vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, mstTableHashValueComp, TD_EQ);
179,340✔
514
  if (NULL == vgInfo) {
179,340✔
515
    mstError("no hash range found for hash value [%u], dbFName:%s, numOfVgId:%d", hashValue, dbFName,
×
516
             (int32_t)taosArrayGetSize(dbInfo->vgArray));
517
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
518
  }
519

520
  *vgId = vgInfo->vgId;
179,340✔
521

522
_exit:
179,340✔
523

524
  if (code) {
179,340✔
525
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
526
  }
527

528
  return code;
179,340✔
529
}
530

531

532
void mstLogSStreamObj(char* tips, SStreamObj* p) {
160,732✔
533
  if (!(stDebugFlag & DEBUG_DEBUG)) {
160,732✔
534
    return;
25,628✔
535
  }
536
  
537
  if (NULL == p) {
135,104✔
538
    mstDebug("%s: stream is NULL", tips);
×
539
    return;
×
540
  }
541

542
  mstDebug("%s: stream obj", tips);
135,104✔
543
  mstDebug("name:%s mainSnodeId:%d userDropped:%d userStopped:%d createTime:%" PRId64 " updateTime:%" PRId64,
135,104✔
544
      p->name, p->mainSnodeId, p->userDropped, p->userStopped, p->createTime, p->updateTime);
545

546
  SCMCreateStreamReq* q = p->pCreate;
135,104✔
547
  if (NULL == q) {
135,104✔
548
    mstDebug("stream pCreate is NULL");
×
549
    return;
×
550
  }
551

552
  int64_t streamId = q->streamId;
135,104✔
553
  int32_t calcDBNum = taosArrayGetSize(q->calcDB);
135,104✔
554
  int32_t calcScanNum = taosArrayGetSize(q->calcScanPlanList);
135,104✔
555
  int32_t notifyUrlNum = taosArrayGetSize(q->pNotifyAddrUrls);
135,104✔
556
  int32_t outColNum = taosArrayGetSize(q->outCols);
135,104✔
557
  int32_t outTagNum = taosArrayGetSize(q->outTags);
135,104✔
558
  int32_t forceOutColNum = taosArrayGetSize(q->forceOutCols);
135,104✔
559

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

575
  switch (q->triggerType) {
135,104✔
576
    case WINDOW_TYPE_INTERVAL: {
54,197✔
577
      SSlidingTrigger* t = &q->trigger.sliding;
54,197✔
578
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
54,197✔
579
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
580
      break;
54,197✔
581
    }  
582
    case WINDOW_TYPE_SESSION: {
7,841✔
583
      SSessionTrigger* t = &q->trigger.session;
7,841✔
584
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
7,841✔
585
      break;
7,841✔
586
    }
587
    case WINDOW_TYPE_STATE: {
32,600✔
588
      SStateWinTrigger* t = &q->trigger.stateWin;
32,600✔
589
      mstsDebug("state trigger options, slotId:%d, expr:%s, extend:%d, zeroth:%s, trueForDuration:%" PRId64, t->slotId, (char *)t->expr, t->extend, (char *)t->zeroth, t->trueForDuration);
32,600✔
590
      break;
32,600✔
591
    }
592
    case WINDOW_TYPE_EVENT:{
22,627✔
593
      SEventTrigger* t = &q->trigger.event;
22,627✔
594
      mstsDebug("event trigger options, startCond:%s, endCond:%s, trueForDuration:%" PRId64, (char*)t->startCond, (char*)t->endCond, t->trueForDuration);
22,627✔
595
      break;
22,627✔
596
    }
597
    case WINDOW_TYPE_COUNT: {
10,683✔
598
      SCountTrigger* t = &q->trigger.count;
10,683✔
599
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
10,683✔
600
      break;
10,683✔
601
    }
602
    case WINDOW_TYPE_PERIOD: {
7,156✔
603
      SPeriodTrigger* t = &q->trigger.period;
7,156✔
604
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
7,156✔
605
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
606
      break;
7,156✔
607
    }
608
    default:
×
609
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
610
      break;
×
611
  }
612

613
  mstsDebugL("create_info: triggerCols:[%s]", (char*)q->triggerCols);
135,104✔
614

615
  mstsDebugL("create_info: partitionCols:[%s]", (char*)q->partitionCols);
135,104✔
616

617
  mstsDebugL("create_info: triggerScanPlan:[%s]", (char*)q->triggerScanPlan);
135,104✔
618

619
  mstsDebugL("create_info: calcPlan:[%s]", (char*)q->calcPlan);
135,104✔
620

621
  mstsDebugL("create_info: subTblNameExpr:[%s]", (char*)q->subTblNameExpr);
135,104✔
622

623
  mstsDebugL("create_info: tagValueExpr:[%s]", (char*)q->tagValueExpr);
135,104✔
624

625

626
  for (int32_t i = 0; i < calcDBNum; ++i) {
267,014✔
627
    char* dbName = taosArrayGetP(q->calcDB, i);
131,910✔
628
    mstsDebug("create_info: calcDB[%d] - %s", i, dbName);
131,910✔
629
  }
630

631
  for (int32_t i = 0; i < calcScanNum; ++i) {
421,932✔
632
    SStreamCalcScan* pScan = taosArrayGet(q->calcScanPlanList, i);
286,828✔
633
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
286,828✔
634
    mstsDebugL("create_info: calcScanPlan[%d] - readFromCache:%d vgNum:%d scanPlan:[%s]", i, pScan->readFromCache, vgNum, (char*)pScan->scanPlan);
286,828✔
635
    for (int32_t v = 0; v < vgNum; ++v) {
573,656✔
636
      mstsDebug("create_info: calcScanPlan[%d] vg[%d] - vgId:%d", i, v, *(int32_t*)taosArrayGet(pScan->vgList, v));
286,828✔
637
    }
638
  }
639

640
  for (int32_t i = 0; i < notifyUrlNum; ++i) {
186,999✔
641
    char* url = taosArrayGetP(q->pNotifyAddrUrls, i);
51,895✔
642
    mstsDebug("create_info: notifyUrl[%d] - %s", i, url);
51,895✔
643
  }
644

645
  for (int32_t i = 0; i < outColNum; ++i) {
692,048✔
646
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
556,944✔
647
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
556,944✔
648
        i, o->name, o->type, o->flags, o->bytes, o->compress, o->typeMod);
649
  }
650
      
651
}
652

653
void mstLogSStmTaskStatus(char* name, int64_t streamId, SStmTaskStatus* pTask, int32_t idx) {
974,711✔
654
  mstsDebug("%s[%d]: task %" PRIx64 " deployId:%d SID:%" PRId64 " nodeId:%d tidx:%d type:%s flags:%" PRIx64 " status:%s lastUpTs:%" PRId64, 
974,711✔
655
      name, idx, pTask->id.taskId, pTask->id.deployId, pTask->id.seriousId, pTask->id.nodeId, pTask->id.taskIdx,
656
      gStreamTaskTypeStr[pTask->type], pTask->flags, gStreamStatusStr[pTask->status], pTask->lastUpTs);
657
}
974,711✔
658

659
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
165,067✔
660
  if (!(stDebugFlag & DEBUG_DEBUG)) {
165,067✔
661
    return;
26,060✔
662
  }
663
  
664
  if (NULL == p) {
139,007✔
665
    mstsDebug("%s: stream status is NULL", tips);
×
666
    return;
×
667
  }
668

669
  int32_t trigReaderNum = taosArrayGetSize(p->trigReaders);
139,007✔
670
  int32_t trigOReaderNum = msmGetTrigOReaderSize(p->trigOReaders);
139,007✔
671
  int32_t calcReaderNum = MST_LIST_SIZE(p->calcReaders);
139,007✔
672
  int32_t triggerNum = p->triggerTask ? 1 : 0;
139,007✔
673
  int32_t runnerNum = 0;
139,007✔
674

675
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
546,446✔
676
    runnerNum += taosArrayGetSize(p->runners[i]);
407,439✔
677
  }
678

679
  mstsDebug("%s: stream status", tips);
139,007✔
680
  mstsDebug("name:%s runnerNum:%d runnerDeploys:%d runnerReplica:%d lastActionTs:%" PRId64
139,007✔
681
           " trigReaders:%d trigOReaders:%d calcReaders:%d trigger:%d runners:%d",
682
      p->streamName, p->runnerNum, p->runnerDeploys, p->runnerReplica, p->lastActionTs,
683
      trigReaderNum, trigOReaderNum, calcReaderNum, triggerNum, runnerNum);
684

685
  SStmTaskStatus* pTask = NULL;
139,007✔
686
  for (int32_t i = 0; i < trigReaderNum; ++i) {
312,280✔
687
    pTask = taosArrayGet(p->trigReaders, i);
173,273✔
688
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
173,273✔
689
  }
690

691
  for (int32_t i = 0; i < trigOReaderNum; ++i) {
139,007✔
692
    pTask = msmGetTrigOReader(p->trigOReaders, i);
×
693
    mstLogSStmTaskStatus("trigOReader task", streamId, pTask, i);
×
694
  }
695

696
  SListNode* pNode = listHead(p->calcReaders);
139,007✔
697
  for (int32_t i = 0; i < calcReaderNum; ++i) {
373,926✔
698
    pTask = (SStmTaskStatus*)pNode->data;
234,919✔
699
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
234,919✔
700
    pNode = TD_DLIST_NODE_NEXT(pNode);
234,919✔
701
  }
702

703
  if (triggerNum > 0) {
139,007✔
704
    mstLogSStmTaskStatus("trigger task", streamId, p->triggerTask, 0);
139,007✔
705
  }
706

707
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
546,446✔
708
    int32_t num = taosArrayGetSize(p->runners[i]);
407,439✔
709
    if (num <= 0) {
407,439✔
710
      continue;
×
711
    }
712
    
713
    mstsDebug("the %dth deploy runners status", i);
407,439✔
714
    for (int32_t m = 0; m < num; ++m) {
834,951✔
715
      pTask = taosArrayGet(p->runners[i], m);
427,512✔
716
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
427,512✔
717
    }
718
  }
719
      
720
}
721

722
bool mstEventPassIsolation(int32_t num, int32_t event) {
2,966,412✔
723
  bool ret = ((mStreamMgmt.lastTs[event].ts + num * MST_SHORT_ISOLATION_DURATION) <= mStreamMgmt.hCtx.currentTs);
2,966,412✔
724
  if (ret) {
2,966,412✔
725
    mstDebug("event %s passed %d isolation, last:%" PRId64 ", curr:%" PRId64, 
2,207,417✔
726
        gMndStreamEvent[event], num, mStreamMgmt.lastTs[event].ts, mStreamMgmt.hCtx.currentTs);
727
  }
728

729
  return ret;
2,966,412✔
730
}
731

732
bool mstEventHandledChkSet(int32_t event) {
2,207,417✔
733
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
2,207,417✔
734
    mstDebug("event %s set handled", gMndStreamEvent[event]);
124,899✔
735
    return true;
124,899✔
736
  }
737
  return false;
2,082,518✔
738
}
739

740
int32_t mstGetStreamStatusStr(SStreamObj* pStream, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
562,710✔
741
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
562,710✔
742
  if (0 == active || MND_STM_STATE_NORMAL != state) {
562,710✔
743
    mstDebug("mnode streamMgmt not in active mode, active:%d, state:%d", active, state);
210✔
744
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
210✔
745
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "Mnode may be unstable, try again later", msgSize);
210✔
746
    return TSDB_CODE_SUCCESS;
210✔
747
  }
748

749
  if (atomic_load_8(&pStream->userDropped)) {
562,500✔
750
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_DROPPING], statusSize);
×
751
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
×
752
    return TSDB_CODE_SUCCESS;
×
753
  }
754

755
  if (atomic_load_8(&pStream->userStopped)) {
562,500✔
756
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
6,714✔
757
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
6,714✔
758
    return TSDB_CODE_SUCCESS;
6,714✔
759
  }
760

761
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
555,786✔
762
  
763
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &pStream->pCreate->streamId, sizeof(pStream->pCreate->streamId));
555,786✔
764
  if (NULL == pStatus) {
555,786✔
765
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
88,572✔
766
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
88,572✔
767
    goto _exit;
88,572✔
768
  }
769

770
  char tmpBuf[256];
467,214✔
771
  int8_t stopped = atomic_load_8(&pStatus->stopped);
467,214✔
772
  switch (stopped) {
467,214✔
773
    case 1:
27,615✔
774
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
27,615✔
775
      snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s, Failed times: %" PRId64, tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes);
27,615✔
776
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
27,615✔
777
      goto _exit;
27,615✔
778
      break;
779
    case 4:
×
780
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
×
781
      snprintf(tmpBuf, sizeof(tmpBuf), "Error: %s", tstrerror(TSDB_CODE_GRANT_STREAM_EXPIRED));
×
782
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
783
      goto _exit;
×
784
      break;
785
    default:
439,599✔
786
      break;
439,599✔
787
  }
788

789
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
439,599✔
790
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_RUNNING], statusSize);
212,695✔
791
    strcpy(tmpBuf, "Running start from: ");
212,695✔
792
    (void)formatTimestampLocal(&tmpBuf[strlen(tmpBuf)], pStatus->triggerTask->runningStartTs, TSDB_TIME_PRECISION_MILLI);
212,695✔
793
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
212,695✔
794
    goto _exit;
212,695✔
795
  }
796

797
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_INIT], statusSize);
226,904✔
798
  snprintf(tmpBuf, sizeof(tmpBuf), "Current deploy times: %" PRId64, pStatus->deployTimes);
226,904✔
799
  STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
226,904✔
800
  goto _exit;
226,904✔
801

802
_exit:
555,786✔
803
  
804
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
555,786✔
805

806
  return TSDB_CODE_SUCCESS;
555,786✔
807
}
808

809
int32_t mstSetStreamAttrResBlock(SMnode *pMnode, SStreamObj* pStream, SSDataBlock* pBlock, int32_t numOfRows) {
562,710✔
810
  int32_t code = 0;
562,710✔
811
  int32_t cols = 0;
562,710✔
812
  int32_t lino = 0;
562,710✔
813

814
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
562,710✔
815
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
562,710✔
816
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
817
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
818

819
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
562,710✔
820
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
821

822
  // db_name
823
  char streamDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
562,710✔
824
  STR_WITH_MAXSIZE_TO_VARSTR(streamDB, mndGetDbStr(pStream->pCreate->streamDB), sizeof(streamDB));
562,710✔
825
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
826
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
827
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&streamDB, false);
562,710✔
828
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
829

830
  // create time
831
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
832
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
833
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->createTime, false);
562,710✔
834
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
835

836
  // stream id
837
  char streamId2[19] = {0};
562,710✔
838
  char streamId[19 + VARSTR_HEADER_SIZE] = {0};
562,710✔
839
  snprintf(streamId2, sizeof(streamId2), "%" PRIx64, pStream->pCreate->streamId);
562,710✔
840
  STR_WITH_MAXSIZE_TO_VARSTR(streamId, streamId2, sizeof(streamId));
562,710✔
841
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
842
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
843
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamId, false);
562,710✔
844
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
845

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

854
  // status
855
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
562,710✔
856
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
562,710✔
857
  code = mstGetStreamStatusStr(pStream, status, sizeof(status), msg, sizeof(msg));
562,710✔
858
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
859

860
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
861
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
862
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
562,710✔
863
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
864

865
  // snodeLeader
866
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
867
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
868
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->mainSnodeId, false);
562,710✔
869
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
870

871
  // snodeReplica
872
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
873
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
874
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, pStream->mainSnodeId);
562,710✔
875
  int32_t replicaSnodeId = pSnode ? pSnode->replicaId : -1;
562,710✔
876
  mndReleaseSnode(pMnode, pSnode);
562,710✔
877
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&replicaSnodeId, false);
562,710✔
878
  TSDB_CHECK_CODE(code, lino, _end);
562,710✔
879

880
  // msg
881
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
562,710✔
882
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
562,710✔
883
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
562,710✔
884

885
_end:
562,710✔
886
  if (code) {
562,710✔
887
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
888
  }
889
  return code;
562,710✔
890
}
891

892

893
int32_t mstGetTaskStatusStr(SStmTaskStatus* pTask, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
16,408,543✔
894
  char tmpBuf[256];
16,384,603✔
895
  
896
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[pTask->status], statusSize);
16,408,543✔
897
  if (STREAM_STATUS_FAILED == pTask->status && pTask->errCode) {
16,408,543✔
898
    snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s", tstrerror(pTask->errCode));
×
899
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
900
    return TSDB_CODE_SUCCESS;
×
901
  }
902

903
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
16,408,543✔
904
    if (pTask->detailStatus) {
2,323,784✔
905
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
1,853,151✔
906
      snprintf(tmpBuf, sizeof(tmpBuf), "Current RT/HI/RE session num: %d/%d/%d, histroy progress:%d%%, total AUTO/USER recalc num: %d/%d", 
1,853,151✔
907
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
908
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
1,853,151✔
909
      taosRUnLockLatch(&pTask->detailStatusLock);
1,853,151✔
910
      return TSDB_CODE_SUCCESS;
1,853,151✔
911
    }
912

913
    taosRUnLockLatch(&pTask->detailStatusLock);    
470,633✔
914
  }
915
  
916
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
14,555,392✔
917
  
918
  return TSDB_CODE_SUCCESS;
14,555,392✔
919
}
920

921
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
16,408,543✔
922
  switch (pTask->type) {
16,408,543✔
923
    case STREAM_READER_TASK:
7,086,469✔
924
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
7,086,469✔
925
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
3,125,162✔
926
      } else {
927
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
3,961,307✔
928
      }
929
      return TSDB_CODE_SUCCESS;
7,086,469✔
930
    case STREAM_RUNNER_TASK:
6,998,290✔
931
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
6,998,290✔
932
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
6,808,284✔
933
        return TSDB_CODE_SUCCESS;
6,808,284✔
934
      }
935
      break;
190,006✔
936
    default:
2,323,784✔
937
      break;
2,323,784✔
938
  }
939

940
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
2,513,790✔
941
  return TSDB_CODE_SUCCESS;
2,513,790✔
942
}
943

944

945
int32_t mstSetStreamTaskResBlock(SStreamObj* pStream, SStmTaskStatus* pTask, SSDataBlock* pBlock, int32_t numOfRows) {
16,408,543✔
946
  int32_t code = 0;
16,408,543✔
947
  int32_t cols = 0;
16,408,543✔
948
  int32_t lino = 0;
16,408,543✔
949

950
  // stream_name
951
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
952
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
16,408,543✔
953
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
954
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
955

956
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
16,408,543✔
957
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
958

959
  // stream id
960
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
961
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
16,408,543✔
962
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE])); 
16,408,543✔
963
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
964
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
965
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
16,408,543✔
966
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
967

968
  // task id
969
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.taskId);
16,408,543✔
970
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
16,408,543✔
971
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
972
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
973
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
16,408,543✔
974
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
975

976
  // type
977
  char type[20 + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
978
  STR_WITH_MAXSIZE_TO_VARSTR(type, (STREAM_READER_TASK == pTask->type) ? "Reader" : ((STREAM_TRIGGER_TASK == pTask->type) ? "Trigger" : "Runner"), sizeof(type));
16,408,543✔
979
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
980
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
981
  code = colDataSetVal(pColInfo, numOfRows, (const char*)type, false);
16,408,543✔
982
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
983

984
  // serious id
985
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.seriousId);
16,408,543✔
986
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
16,408,543✔
987
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
988
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
989
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
16,408,543✔
990
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
991

992
  // deploy id
993
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
994
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
995
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.deployId, false);
16,408,543✔
996
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
997

998
  // node_type
999
  char nodeType[10 + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
1000
  STR_WITH_MAXSIZE_TO_VARSTR(nodeType, (STREAM_READER_TASK == pTask->type) ? "vnode" : "snode", sizeof(nodeType));
16,408,543✔
1001
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1002
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1003
  code = colDataSetVal(pColInfo, numOfRows, (const char*)nodeType, false);
16,408,543✔
1004
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1005

1006
  // node id
1007
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1008
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1009
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.nodeId, false);
16,408,543✔
1010
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1011

1012
  // task idx
1013
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1014
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1015
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.taskIdx, false);
16,408,543✔
1016
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1017

1018
  // status
1019
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
1020
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
1021
  code = mstGetTaskStatusStr(pTask, status, sizeof(status), msg, sizeof(msg));
16,408,543✔
1022
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1023

1024
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1025
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1026
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
16,408,543✔
1027
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1028

1029
  // start time
1030
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1031
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1032
  if (pTask->runningStartTs) {
16,408,543✔
1033
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
3,240,077✔
1034
  } else {
1035
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
13,168,466✔
1036
  }
1037
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1038

1039
  // last update
1040
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1041
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1042
  if (pTask->lastUpTs) {
16,408,543✔
1043
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, false);
16,408,543✔
1044
  } else {
UNCOV
1045
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, true);
×
1046
  }
1047
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1048

1049
  // extra info
1050
  char extra[64 + VARSTR_HEADER_SIZE] = {0};
16,408,543✔
1051
  code = mstGetTaskExtraStr(pTask, extra, sizeof(extra));
16,408,543✔
1052
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1053
  
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1056
  code = colDataSetVal(pColInfo, numOfRows, (const char*)extra, false);
16,408,543✔
1057
  TSDB_CHECK_CODE(code, lino, _end);
16,408,543✔
1058

1059
  // msg
1060
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
16,408,543✔
1061
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
16,408,543✔
1062
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
16,408,543✔
1063

1064
_end:
16,408,543✔
1065
  if (code) {
16,408,543✔
1066
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1067
  }
1068
  return code;
16,408,543✔
1069
}
1070

1071
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
2,325,764✔
1072
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + msmGetTrigOReaderSize(pStatus->trigOReaders) + MST_LIST_SIZE(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
2,325,764✔
1073
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
9,303,056✔
1074
    num += taosArrayGetSize(pStatus->runners[i]);
6,977,292✔
1075
  }
1076

1077
  return num;
2,325,764✔
1078
}
1079

1080
int32_t mstSetStreamTasksResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
2,472,197✔
1081
  int32_t code = 0;
2,472,197✔
1082
  int32_t lino = 0;
2,472,197✔
1083
  int64_t streamId = pStream->pCreate->streamId;
2,472,197✔
1084
  bool    statusLocked = false;
2,472,197✔
1085

1086
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
2,472,197✔
1087

1088
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,472,197✔
1089
  if (NULL == pStatus) {
2,472,197✔
1090
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
98,676✔
1091
    goto _exit;
98,676✔
1092
  }
1093

1094
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,373,521✔
1095
  if (stopped) {
2,373,521✔
1096
    mstsDebug("stream stopped %d, ignore it", stopped);
47,757✔
1097
    goto _exit;
47,757✔
1098
  }
1099

1100
  (void)mstWaitLock(&pStatus->resetLock, true);
2,325,764✔
1101
  statusLocked = true;
2,325,764✔
1102
  
1103
  int32_t count = mstGetNumOfStreamTasks(pStatus);
2,325,764✔
1104

1105
  if (*numOfRows + count > rowsCapacity) {
2,325,764✔
1106
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
×
1107
    if (code) {
×
1108
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1109
      TAOS_CHECK_EXIT(code);
×
1110
    }
1111
  }
1112

1113
  SStmTaskStatus* pTask = NULL;
2,325,764✔
1114
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
2,325,764✔
1115
  for (int32_t i = 0; i < trigReaderNum; ++i) {
4,910,433✔
1116
    pTask = taosArrayGet(pStatus->trigReaders, i);
2,584,669✔
1117
  
1118
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
2,584,669✔
1119
    if (code == TSDB_CODE_SUCCESS) {
2,584,669✔
1120
      (*numOfRows)++;
2,584,669✔
1121
    }
1122
  }
1123

1124
  trigReaderNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
2,325,764✔
1125
  for (int32_t i = 0; i < trigReaderNum; ++i) {
2,866,257✔
1126
    pTask = msmGetTrigOReader(pStatus->trigOReaders, i);
540,493✔
1127
  
1128
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
540,493✔
1129
    if (code == TSDB_CODE_SUCCESS) {
540,493✔
1130
      (*numOfRows)++;
540,493✔
1131
    }
1132
  }
1133

1134
  if (pStatus->calcReaders) {
2,325,764✔
1135
    int32_t calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
2,325,764✔
1136
    SListNode* pNode = listHead(pStatus->calcReaders);
2,325,764✔
1137
    for (int32_t i = 0; i < calcReaderNum; ++i) {
6,287,071✔
1138
      pTask = (SStmTaskStatus*)pNode->data;
3,961,307✔
1139
    
1140
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
3,961,307✔
1141
      if (code == TSDB_CODE_SUCCESS) {
3,961,307✔
1142
        (*numOfRows)++;
3,961,307✔
1143
      }
1144
      pNode = TD_DLIST_NODE_NEXT(pNode);
3,961,307✔
1145
    }
1146
  }
1147

1148
  if (pStatus->triggerTask) {
2,325,764✔
1149
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
2,323,784✔
1150
    if (code == TSDB_CODE_SUCCESS) {
2,323,784✔
1151
      (*numOfRows)++;
2,323,784✔
1152
    }
1153
  }
1154

1155
  int32_t runnerNum = 0;
2,325,764✔
1156
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
9,303,056✔
1157
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
6,977,292✔
1158
    for (int32_t m = 0; m < runnerNum; ++m) {
13,975,582✔
1159
      pTask = taosArrayGet(pStatus->runners[i], m);
6,998,290✔
1160
    
1161
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
6,998,290✔
1162
      if (code == TSDB_CODE_SUCCESS) {
6,998,290✔
1163
        (*numOfRows)++;
6,998,290✔
1164
      }
1165
    }
1166
  }
1167
  
1168
  pBlock->info.rows = *numOfRows;
2,325,764✔
1169

1170
_exit:
2,472,197✔
1171

1172
  if (statusLocked) {
2,472,197✔
1173
    taosRUnLockLatch(&pStatus->resetLock);
2,325,764✔
1174
  }
1175
  
1176
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
2,472,197✔
1177

1178
  if (code) {
2,472,197✔
1179
    mError("error happens when build stream tasks result block, lino:%d, code:%s", lino, tstrerror(code));
×
1180
  }
1181
  
1182
  return code;
2,472,197✔
1183
}
1184

1185

1186
int32_t mstAppendNewRecalcRange(int64_t streamId, SStmStatus *pStream, STimeWindow* pRange) {
3,384✔
1187
  int32_t code = 0;
3,384✔
1188
  int32_t lino = 0;
3,384✔
1189
  bool    locked = false;
3,384✔
1190
  SArray* userRecalcList = NULL;
3,384✔
1191

1192
  SStreamRecalcReq req = {.recalcId = 0, .start = pRange->skey, .end = pRange->ekey};
3,384✔
1193
  TAOS_CHECK_EXIT(taosGetSystemUUIDU64(&req.recalcId));
3,384✔
1194
  
1195
  taosWLockLatch(&pStream->userRecalcLock);
3,384✔
1196
  locked = true;
3,384✔
1197
  
1198
  if (NULL == pStream->userRecalcList) {
3,384✔
1199
    userRecalcList = taosArrayInit(2, sizeof(SStreamRecalcReq));
3,384✔
1200
    if (NULL == userRecalcList) {
3,384✔
1201
      TAOS_CHECK_EXIT(terrno);
×
1202
    }
1203

1204
    TSDB_CHECK_NULL(taosArrayPush(userRecalcList, &req), code, lino, _exit, terrno);
3,384✔
1205

1206
    atomic_store_ptr(&pStream->userRecalcList, userRecalcList);
3,384✔
1207
    userRecalcList = NULL;    
3,384✔
1208
  } else {
1209
    TSDB_CHECK_NULL(taosArrayPush(pStream->userRecalcList, &req), code, lino, _exit, terrno);
×
1210
  }
1211
  
1212
  mstsInfo("stream recalc ID:%" PRIx64 " range:%" PRId64 " - %" PRId64 " added", req.recalcId, pRange->skey, pRange->ekey);
3,384✔
1213

1214
_exit:
3,384✔
1215

1216
  taosArrayDestroy(userRecalcList);
3,384✔
1217

1218
  if (locked) {
3,384✔
1219
    taosWUnLockLatch(&pStream->userRecalcLock);
3,384✔
1220
  }
1221
  
1222
  if (code) {
3,384✔
1223
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1224
  }
1225
  
1226
  return code;
3,384✔
1227
}
1228

1229

1230

1231
int32_t mstSetStreamRecalculateResBlock(SStreamObj* pStream, SSTriggerRecalcProgress* pProgress, SSDataBlock* pBlock, int32_t numOfRows) {
×
1232
  int32_t code = 0;
×
1233
  int32_t cols = 0;
×
1234
  int32_t lino = 0;
×
1235

1236
  // stream_name
1237
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1238
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
×
1239
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1240
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1241

1242
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
×
1243
  TSDB_CHECK_CODE(code, lino, _end);
×
1244

1245
  // stream id
1246
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
×
1247
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
×
1248
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE); 
×
1249
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1250
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1251
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1252
  TSDB_CHECK_CODE(code, lino, _end);
×
1253

1254
  // recalc id
1255
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pProgress->recalcId);
×
1256
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1257
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1258
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1259
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1260
  TSDB_CHECK_CODE(code, lino, _end);
×
1261

1262
  // start
1263
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1264
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1265
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->start, false);
×
1266
  TSDB_CHECK_CODE(code, lino, _end);
×
1267

1268
  // end
1269
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1270
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1271
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->end, false);
×
1272
  TSDB_CHECK_CODE(code, lino, _end);
×
1273

1274
  // progress
1275
  char progress[20 + VARSTR_HEADER_SIZE] = {0};
×
1276
  snprintf(&progress[VARSTR_HEADER_SIZE], sizeof(progress) - VARSTR_HEADER_SIZE, "%d%%", pProgress->progress);
×
1277
  varDataSetLen(progress, strlen(&progress[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*)progress, false);
×
1281
  TSDB_CHECK_CODE(code, lino, _end);
×
1282

1283
_end:
×
1284
  if (code) {
×
1285
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1286
  }
1287
  return code;
×
1288
}
1289

1290

1291
int32_t mstSetStreamRecalculatesResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
251✔
1292
  int32_t code = 0;
251✔
1293
  int32_t lino = 0;
251✔
1294
  int64_t streamId = pStream->pCreate->streamId;
251✔
1295

1296
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
251✔
1297

1298
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
251✔
1299
  if (NULL == pStatus) {
251✔
1300
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
×
1301
    goto _exit;
×
1302
  }
1303

1304
  int8_t stopped = atomic_load_8(&pStatus->stopped);
251✔
1305
  if (stopped) {
251✔
1306
    mstsDebug("stream stopped %d, ignore it", stopped);
×
1307
    goto _exit;
×
1308
  }
1309

1310
  if (NULL == pStatus->triggerTask) {
251✔
1311
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1312
    goto _exit;
×
1313
  }
1314

1315
  (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, true);
251✔
1316
  if (NULL == pStatus->triggerTask->detailStatus) {
251✔
1317
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1318
    taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1319
    goto _exit;
×
1320
  }
1321

1322
  SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pStatus->triggerTask->detailStatus;
251✔
1323
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
251✔
1324

1325
  if (*numOfRows + count > rowsCapacity) {
251✔
1326
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
×
1327
    if (code) {
×
1328
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1329
      taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1330
      TAOS_CHECK_EXIT(code);
×
1331
    }
1332
  }
1333

1334
  for (int32_t i = 0; i < count; ++i) {
251✔
1335
    SSTriggerRecalcProgress* pProgress = taosArrayGet(pTrigger->userRecalcs, i);
×
1336
  
1337
    code = mstSetStreamRecalculateResBlock(pStream, pProgress, pBlock, *numOfRows);
×
1338
    if (code == TSDB_CODE_SUCCESS) {
×
1339
      (*numOfRows)++;
×
1340
    }
1341
  }
1342

1343
  taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
251✔
1344
  
1345
  pBlock->info.rows = *numOfRows;
251✔
1346

1347
_exit:
251✔
1348
  
1349
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
251✔
1350

1351
  if (code) {
251✔
1352
    mError("error happens when build stream recalculates result block, lino:%d, code:%s", lino, tstrerror(code));
×
1353
  }
1354
  
1355
  return code;
251✔
1356
}
1357

1358
int32_t mstGetScanUidFromPlan(int64_t streamId, void* scanPlan, int64_t* uid) {
252,260✔
1359
  SSubplan* pSubplan = NULL;
252,260✔
1360
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
252,260✔
1361
  
1362
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
252,260✔
1363

1364
  if (pSubplan->pNode && nodeType(pSubplan->pNode) == QUERY_NODE_PHYSICAL_PLAN_TABLE_SCAN) {
252,260✔
1365
    SScanPhysiNode* pScanNode = (SScanPhysiNode*)pSubplan->pNode;
94,128✔
1366
    *uid = pScanNode->uid;
94,128✔
1367
  }
1368
  
1369
_exit:
251,840✔
1370

1371
  if (code) {
252,260✔
1372
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1373
  }
1374

1375
  nodesDestroyNode((SNode *)pSubplan);
252,260✔
1376

1377
  return code;
252,260✔
1378
}
1379

1380

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