• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In
Build has been canceled!

taosdata / TDengine / #4754

25 Sep 2025 05:58AM UTC coverage: 57.946% (-1.0%) from 58.977%
#4754

push

travis-ci

web-flow
enh: taos command line support '-uroot' on windows (#33055)

133189 of 293169 branches covered (45.43%)

Branch coverage included in aggregate %.

201677 of 284720 relevant lines covered (70.83%)

5398749.0 hits per line

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

67.91
/source/dnode/mnode/impl/src/mndStreamUtil.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "mndDb.h"
17
#include "mndStb.h"
18
#include "mndStream.h"
19
#include "mndTrans.h"
20
#include "mndVgroup.h"
21
#include "taoserror.h"
22
#include "tmisce.h"
23
#include "mndSnode.h"
24

25
bool mstWaitLock(SRWLatch* pLock, bool readLock) {
43,831✔
26
  if (readLock) {
43,831✔
27
    while (taosRTryLockLatch(pLock)) {
41,250!
28
      taosMsleep(1);
×
29
    }
30

31
    return true;
41,250✔
32
  }
33

34
  taosWWaitLockLatch(pLock);
2,581✔
35

36
  return true;
2,581✔
37
}
38

39
void mndStreamDestroySStreamMgmtRsp(SStreamMgmtRsp* p) {
×
40
  taosArrayDestroy(p->cont.vgIds);
×
41
  taosArrayDestroy(p->cont.readerList);
×
42
}
×
43

44
void mstDestroySStmVgStreamStatus(void* p) { 
461✔
45
  SStmVgStreamStatus* pStatus = (SStmVgStreamStatus*)p;
461✔
46
  taosArrayDestroy(pStatus->trigReaders); 
461✔
47
  taosArrayDestroy(pStatus->calcReaders); 
461✔
48
}
461✔
49

50
void mstDestroySStmSnodeStreamStatus(void* p) { 
318✔
51
  SStmSnodeStreamStatus* pStatus = (SStmSnodeStreamStatus*)p;
318✔
52
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,272✔
53
    taosArrayDestroy(pStatus->runners[i]);
954✔
54
    pStatus->runners[i] = NULL;
954✔
55
  }
56
}
318✔
57

58

59
void mstDestroyVgroupStatus(SStmVgroupStatus* pVgStatus) {
×
60
  taosHashCleanup(pVgStatus->streamTasks);
×
61
  pVgStatus->streamTasks = NULL;
×
62
}
×
63

64
void mstDestroySStmTaskToDeployExt(void* param) {
1,805✔
65
  SStmTaskToDeployExt* pExt = (SStmTaskToDeployExt*)param;
1,805✔
66
  if (pExt->deployed) {
1,805✔
67
    return;
1,795✔
68
  }
69
  
70
  switch (pExt->deploy.task.type) {
10✔
71
    case STREAM_TRIGGER_TASK:
1✔
72
      taosArrayDestroy(pExt->deploy.msg.trigger.readerList);
1✔
73
      pExt->deploy.msg.trigger.readerList = NULL;
1✔
74
      taosArrayDestroy(pExt->deploy.msg.trigger.runnerList);
1✔
75
      pExt->deploy.msg.trigger.runnerList = NULL;
1✔
76
      break;
1✔
77
    case STREAM_RUNNER_TASK:
2✔
78
      taosMemoryFreeClear(pExt->deploy.msg.runner.pPlan);
2!
79
      break;
2✔
80
    default:  
7✔
81
      break;;
7✔
82
  }
83
}
84

85
void mstDestroyScanAddrList(void* param) {
322✔
86
  if (NULL == param) {
322!
87
    return;
×
88
  }
89
  SArray* pList = *(SArray**)param;
322✔
90
  taosArrayDestroy(pList);
322✔
91
}
92

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

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

104
void mstDestroySStmSnodeStatus(void* param) {
85✔
105
  SStmSnodeStatus* pSnode = (SStmSnodeStatus*)param;
85✔
106
  taosHashCleanup(pSnode->streamTasks);
85✔
107
}
85✔
108

109
void mstDestroySStmVgroupStatus(void* param) {
173✔
110
  SStmVgroupStatus* pVg = (SStmVgroupStatus*)param;
173✔
111
  taosHashCleanup(pVg->streamTasks);
173✔
112
}
173✔
113

114
void mstResetSStmStatus(SStmStatus* pStatus) {
286✔
115
  taosArrayDestroy(pStatus->trigReaders);
286✔
116
  pStatus->trigReaders = NULL;
286✔
117
  taosArrayDestroy(pStatus->trigOReaders);
286✔
118
  pStatus->trigOReaders = NULL;
286✔
119
  taosArrayDestroy(pStatus->calcReaders);
286✔
120
  pStatus->calcReaders = NULL;
286✔
121
  if (pStatus->triggerTask) {
286!
122
    (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, false);
286✔
123
    taosMemoryFreeClear(pStatus->triggerTask->detailStatus);
286!
124
    taosWUnLockLatch(&pStatus->triggerTask->detailStatusLock);
286✔
125
  }
126
  taosMemoryFreeClear(pStatus->triggerTask);
286!
127
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,144✔
128
    taosArrayDestroy(pStatus->runners[i]);
858✔
129
    pStatus->runners[i] = NULL;
858✔
130
  }
131
}
286✔
132

133
void mstDestroySStmStatus(void* param) {
259✔
134
  SStmStatus* pStatus = (SStmStatus*)param;
259✔
135
  taosMemoryFreeClear(pStatus->streamName);
259!
136

137
  mstResetSStmStatus(pStatus);
259✔
138

139
  taosWLockLatch(&pStatus->userRecalcLock);
259✔
140
  taosArrayDestroy(pStatus->userRecalcList);
259✔
141
  taosWUnLockLatch(&pStatus->userRecalcLock);
259✔
142

143
  tFreeSCMCreateStreamReq(pStatus->pCreate);
259✔
144
  taosMemoryFreeClear(pStatus->pCreate);  
259!
145
}
259✔
146

147
void mstDestroySStmAction(void* param) {
411✔
148
  SStmAction* pAction = (SStmAction*)param;
411✔
149

150
  taosArrayDestroy(pAction->undeploy.taskList);
411✔
151
  taosArrayDestroy(pAction->recalc.recalcList);
411✔
152
}
411✔
153

154
void mstClearSStmStreamDeploy(SStmStreamDeploy* pDeploy) {
358✔
155
  pDeploy->readerTasks = NULL;
358✔
156
  pDeploy->triggerTask = NULL;
358✔
157
  pDeploy->runnerTasks = NULL;
358✔
158
}
358✔
159

160
int32_t mstIsStreamDropped(SMnode *pMnode, int64_t streamId, bool* dropped) {
180✔
161
  SSdb   *pSdb = pMnode->pSdb;
180✔
162
  void   *pIter = NULL;
180✔
163
  
164
  while (1) {
694✔
165
    SStreamObj *pStream = NULL;
874✔
166
    pIter = sdbFetch(pSdb, SDB_STREAM, pIter, (void **)&pStream);
874✔
167
    if (pIter == NULL) break;
874✔
168

169
    if (pStream->pCreate->streamId == streamId) {
828✔
170
      *dropped = pStream->userDropped ? true : false;
134✔
171
      sdbRelease(pSdb, pStream);
134✔
172
      sdbCancelFetch(pSdb, pIter);
134✔
173
      mstsDebug("stream found, dropped:%d", *dropped);
134✔
174
      return TSDB_CODE_SUCCESS;
134✔
175
    }
176
    
177
    sdbRelease(pSdb, pStream);
694✔
178
  }
179

180
  *dropped = true;
46✔
181

182
  return TSDB_CODE_SUCCESS;
46✔
183
}
184

185
typedef struct SStmCheckDbInUseCtx {
186
  bool* dbStream;
187
  bool* vtableStream;
188
  bool  ignoreCurrDb;
189
} SStmCheckDbInUseCtx;
190

191
static bool mstChkSetDbInUse(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
67✔
192
  SStreamObj *pStream = pObj;
67✔
193
  if (atomic_load_8(&pStream->userDropped)) {
67!
194
    return true;
×
195
  }
196

197
  SStmCheckDbInUseCtx* pCtx = (SStmCheckDbInUseCtx*)p2;
67✔
198
  if (pCtx->ignoreCurrDb && 0 == strcmp(pStream->pCreate->streamDB, p1)) {
67✔
199
    return true;
2✔
200
  }
201
  
202
  if (pStream->pCreate->triggerDB && 0 == strcmp(pStream->pCreate->triggerDB, p1)) {
65!
203
    *pCtx->dbStream = true;
3✔
204
    return false;
3✔
205
  }
206

207
  int32_t calcDBNum = taosArrayGetSize(pStream->pCreate->calcDB);
62✔
208
  for (int32_t i = 0; i < calcDBNum; ++i) {
122✔
209
    char* calcDB = taosArrayGetP(pStream->pCreate->calcDB, i);
62✔
210
    if (0 == strcmp(calcDB, p1)) {
62✔
211
      *pCtx->dbStream = true;
2✔
212
      return false;
2✔
213
    }
214
  }
215

216
  if (pStream->pCreate->vtableCalc || STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
60✔
217
    *pCtx->vtableStream = true;
54✔
218
    return true;
54✔
219
  }
220
  
221
  return true;
6✔
222
}
223

224
void mstCheckDbInUse(SMnode *pMnode, char *dbFName, bool *dbStream, bool *vtableStream, bool ignoreCurrDb) {
1,949✔
225
  int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,949✔
226
  if (streamNum <= 0) {
1,949✔
227
    return;
1,940✔
228
  }
229

230
  SStmCheckDbInUseCtx ctx = {dbStream, vtableStream, ignoreCurrDb};
9✔
231
  sdbTraverse(pMnode->pSdb, SDB_STREAM, mstChkSetDbInUse, dbFName, &ctx, NULL);
9✔
232
}
233

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

246
int32_t mstCheckSnodeExists(SMnode *pMnode) {
×
247
  SSdb      *pSdb = pMnode->pSdb;
×
248
  void      *pIter = NULL;
×
249
  SSnodeObj *pObj = NULL;
×
250

251
  while (1) {
252
    pIter = sdbFetch(pSdb, SDB_SNODE, pIter, (void **)&pObj);
×
253
    if (pIter == NULL) {
×
254
      break;
×
255
    }
256

257
    sdbRelease(pSdb, pObj);
×
258
    sdbCancelFetch(pSdb, pIter);
×
259
    return TSDB_CODE_SUCCESS;
×
260
  }
261

262
  return TSDB_CODE_SNODE_NOT_DEPLOYED;
×
263
}
264

265
void mstSetTaskStatusFromMsg(SStmGrpCtx* pCtx, SStmTaskStatus* pTask, SStmTaskStatusMsg* pMsg) {
×
266
  pTask->id.taskId = pMsg->taskId;
×
267
  pTask->id.deployId = pMsg->deployId;
×
268
  pTask->id.seriousId = pMsg->seriousId;
×
269
  pTask->id.nodeId = pMsg->nodeId;
×
270
  pTask->id.taskIdx = pMsg->taskIdx;
×
271

272
  pTask->type = pMsg->type;
×
273
  pTask->flags = pMsg->flags;
×
274
  pTask->status = pMsg->status;
×
275
  pTask->lastUpTs = pCtx->currTs;
×
276
}
×
277

278
bool mndStreamActionDequeue(SStmActionQ* pQueue, SStmQNode **param) {
2,116✔
279
  while (0 == atomic_load_64(&pQueue->qRemainNum)) {
2,116✔
280
    return false;
1,782✔
281
  }
282

283
  SStmQNode *orig = pQueue->head;
334✔
284

285
  SStmQNode *node = pQueue->head->next;
334✔
286
  pQueue->head = pQueue->head->next;
334✔
287

288
  *param = node;
334✔
289

290
  taosMemoryFreeClear(orig);
334!
291

292
  (void)atomic_sub_fetch_64(&pQueue->qRemainNum, 1);
334✔
293

294
  return true;
334✔
295
}
296

297
void mndStreamActionEnqueue(SStmActionQ* pQueue, SStmQNode* param) {
334✔
298
  taosWLockLatch(&pQueue->lock);
334✔
299
  pQueue->tail->next = param;
334✔
300
  pQueue->tail = param;
334✔
301
  taosWUnLockLatch(&pQueue->lock);
334✔
302

303
  (void)atomic_add_fetch_64(&pQueue->qRemainNum, 1);
334✔
304
}
334✔
305

306
char* mstGetStreamActionString(int32_t action) {
178✔
307
  switch (action) {
178!
308
    case STREAM_ACT_DEPLOY:
178✔
309
      return "DEPLOY";
178✔
310
    case STREAM_ACT_UNDEPLOY:
×
311
      return "UNDEPLOY";
×
312
    case STREAM_ACT_START:
×
313
      return "START";
×
314
    case STREAM_ACT_UPDATE_TRIGGER:
×
315
      return "UPDATE TRIGGER";
×
316
    case STREAM_ACT_RECALC:
×
317
      return "USER RECALC";
×
318
    default:
×
319
      break;
×
320
  }
321

322
  return "UNKNOWN";
×
323
}
324

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

333
  pNode->type = action;
291✔
334
  pNode->streamAct = true;
291✔
335
  pNode->action.stream.streamId = streamId;
291✔
336
  TAOS_STRCPY(pNode->action.stream.streamName, streamName);
291✔
337
  pNode->action.stream.userAction = userAction;
291✔
338
  pNode->action.stream.actionParam = param;
291✔
339
  
340
  pNode->next = NULL;
291✔
341

342
  mndStreamActionEnqueue(actionQ, pNode);
291✔
343

344
  mstsDebug("stream action %s posted enqueue", mstGetStreamActionString(action));
291✔
345
}
346

347
void mstPostTaskAction(SStmActionQ*        actionQ, SStmTaskAction* pAction, int32_t action) {
43✔
348
  SStmQNode *pNode = taosMemoryMalloc(sizeof(SStmQNode));
43!
349
  if (NULL == pNode) {
43!
350
    int64_t streamId = pAction->streamId;
×
351
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, __LINE__, tstrerror(terrno));
×
352
    return;
×
353
  }
354

355
  pNode->type = action;
43✔
356
  pNode->streamAct = false;
43✔
357
  pNode->action.task = *pAction;
43✔
358
  
359
  pNode->next = NULL;
43✔
360

361
  mndStreamActionEnqueue(actionQ, pNode);
43✔
362
}
363

364
void mstDestroyDbVgroupsHash(SSHashObj *pDbVgs) {
59✔
365
  int32_t iter = 0;
59✔
366
  SDBVgHashInfo* pVg = NULL;
59✔
367
  void* p = NULL;
59✔
368
  while (NULL != (p = tSimpleHashIterate(pDbVgs, p, &iter))) {
520✔
369
    pVg = (SDBVgHashInfo*)p;
461✔
370
    taosArrayDestroy(pVg->vgArray);
461✔
371
  }
372
  
373
  tSimpleHashCleanup(pDbVgs);
59✔
374
}
59✔
375

376

377
int32_t mstBuildDBVgroupsMap(SMnode* pMnode, SSHashObj** ppRes) {
59✔
378
  void*   pIter = NULL;
59✔
379
  int32_t code = TSDB_CODE_SUCCESS;
59✔
380
  int32_t lino = 0;
59✔
381
  SArray* pTarget = NULL;
59✔
382
  SArray* pNew = NULL;
59✔
383
  SDbObj* pDb = NULL;
59✔
384
  SDBVgHashInfo dbInfo = {0}, *pDbInfo = NULL;
59✔
385
  SVgObj* pVgroup = NULL;
59✔
386

387
  SSHashObj* pDbVgroup = tSimpleHashInit(20, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY));
59✔
388
  TSDB_CHECK_NULL(pDbVgroup, code, lino, _exit, terrno);
59!
389

390
  while (1) {
486✔
391
    pIter = sdbFetch(pMnode->pSdb, SDB_VGROUP, pIter, (void**)&pVgroup);
545✔
392
    if (pIter == NULL) {
545✔
393
      break;
59✔
394
    }
395

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

421
      pTarget = pNew;
461✔
422
    } else {
423
      pTarget = pDbInfo->vgArray;
25✔
424
    }
425

426
    SVGroupHashInfo vgInfo = {.vgId = pVgroup->vgId, .hashBegin = pVgroup->hashBegin, .hashEnd = pVgroup->hashEnd};
486✔
427
    if (NULL == taosArrayPush(pTarget, &vgInfo)) {
486!
428
      sdbRelease(pMnode->pSdb, pVgroup);
×
429
      sdbCancelFetch(pMnode->pSdb, pIter);      
×
430
      pVgroup = NULL;
×
431
      TSDB_CHECK_NULL(NULL, code, lino, _exit, terrno);
×
432
    }
433

434
    if (NULL == pDbInfo) {
486✔
435
      code = tSimpleHashPut(pDbVgroup, pVgroup->dbName, strlen(pVgroup->dbName) + 1, &dbInfo, sizeof(dbInfo));
461✔
436
      if (code) {
461!
437
        sdbRelease(pMnode->pSdb, pVgroup);
×
438
        sdbCancelFetch(pMnode->pSdb, pIter);      
×
439
        pVgroup = NULL;
×
440
        TAOS_CHECK_EXIT(code);
×
441
      }
442
      pNew = NULL;
461✔
443
    }
444

445
    sdbRelease(pMnode->pSdb, pVgroup);
486✔
446
    pVgroup = NULL;
486✔
447
  }
448

449
  *ppRes = pDbVgroup;
59✔
450
  
451
_exit:
59✔
452

453
  taosArrayDestroy(pNew);
59✔
454

455
  if (code) {
59!
456
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
457
  }
458

459
  return code;
59✔
460
}
461

462
int mstDbVgInfoComp(const void* lp, const void* rp) {
13✔
463
  SVGroupHashInfo* pLeft = (SVGroupHashInfo*)lp;
13✔
464
  SVGroupHashInfo* pRight = (SVGroupHashInfo*)rp;
13✔
465
  if (pLeft->hashBegin < pRight->hashBegin) {
13!
466
    return -1;
13✔
467
  } else if (pLeft->hashBegin > pRight->hashBegin) {
×
468
    return 1;
×
469
  }
470

471
  return 0;
×
472
}
473

474
int32_t mstTableHashValueComp(void const* lp, void const* rp) {
129✔
475
  uint32_t*    key = (uint32_t*)lp;
129✔
476
  SVgroupInfo* pVg = (SVgroupInfo*)rp;
129✔
477

478
  if (*key < pVg->hashBegin) {
129!
479
    return -1;
×
480
  } else if (*key > pVg->hashEnd) {
129✔
481
    return 1;
7✔
482
  }
483

484
  return 0;
122✔
485
}
486

487

488
int32_t mstGetTableVgId(SSHashObj* pDbVgroups, char* dbFName, char *tbName, int32_t* vgId) {
122✔
489
  int32_t code = 0;
122✔
490
  int32_t lino = 0;
122✔
491
  SVgroupInfo* vgInfo = NULL;
122✔
492
  char         tbFullName[TSDB_TABLE_FNAME_LEN];
493

494
  SDBVgHashInfo* dbInfo = (SDBVgHashInfo*)tSimpleHashGet(pDbVgroups, dbFName, strlen(dbFName) + 1);
122✔
495
  if (NULL == dbInfo) {
122!
496
    mstError("db %s does not exist", dbFName);
×
497
    TAOS_CHECK_EXIT(TSDB_CODE_MND_DB_NOT_EXIST);
×
498
  }
499
  
500
  (void)snprintf(tbFullName, sizeof(tbFullName), "%s.%s", dbFName, tbName);
122✔
501
  uint32_t hashValue = taosGetTbHashVal(tbFullName, (uint32_t)strlen(tbFullName), dbInfo->hashMethod,
244✔
502
                                        dbInfo->hashPrefix, dbInfo->hashSuffix);
122!
503

504
  if (!dbInfo->vgSorted) {
122✔
505
    taosArraySort(dbInfo->vgArray, mstDbVgInfoComp);
59✔
506
    dbInfo->vgSorted = true;
59✔
507
  }
508

509
  vgInfo = taosArraySearch(dbInfo->vgArray, &hashValue, mstTableHashValueComp, TD_EQ);
122✔
510
  if (NULL == vgInfo) {
122!
511
    mstError("no hash range found for hash value [%u], dbFName:%s, numOfVgId:%d", hashValue, dbFName,
×
512
             (int32_t)taosArrayGetSize(dbInfo->vgArray));
513
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
514
  }
515

516
  *vgId = vgInfo->vgId;
122✔
517

518
_exit:
122✔
519

520
  if (code) {
122!
521
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
522
  }
523

524
  return code;
122✔
525
}
526

527

528
void mstLogSStreamObj(char* tips, SStreamObj* p) {
263✔
529
  if (!(stDebugFlag & DEBUG_DEBUG)) {
263✔
530
    return;
106✔
531
  }
532
  
533
  if (NULL == p) {
157!
534
    mstDebug("%s: stream is NULL", tips);
×
535
    return;
×
536
  }
537

538
  mstDebug("%s: stream obj", tips);
157!
539
  mstDebug("name:%s mainSnodeId:%d userDropped:%d userStopped:%d createTime:%" PRId64 " updateTime:%" PRId64,
157!
540
      p->name, p->mainSnodeId, p->userDropped, p->userStopped, p->createTime, p->updateTime);
541

542
  SCMCreateStreamReq* q = p->pCreate;
157✔
543
  if (NULL == q) {
157!
544
    mstDebug("stream pCreate is NULL");
×
545
    return;
×
546
  }
547

548
  int64_t streamId = q->streamId;
157✔
549
  int32_t calcDBNum = taosArrayGetSize(q->calcDB);
157✔
550
  int32_t calcScanNum = taosArrayGetSize(q->calcScanPlanList);
157✔
551
  int32_t notifyUrlNum = taosArrayGetSize(q->pNotifyAddrUrls);
157✔
552
  int32_t outColNum = taosArrayGetSize(q->outCols);
157✔
553
  int32_t outTagNum = taosArrayGetSize(q->outTags);
157✔
554
  int32_t forceOutColNum = taosArrayGetSize(q->forceOutCols);
157✔
555

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

571
  switch (q->triggerType) {
157!
572
    case WINDOW_TYPE_INTERVAL: {
91✔
573
      SSlidingTrigger* t = &q->trigger.sliding;
91✔
574
      mstsDebug("sliding trigger options, intervalUnit:%d, slidingUnit:%d, offsetUnit:%d, soffsetUnit:%d, precision:%d, interval:%" PRId64 ", offset:%" PRId64 ", sliding:%" PRId64 ", soffset:%" PRId64, 
91!
575
          t->intervalUnit, t->slidingUnit, t->offsetUnit, t->soffsetUnit, t->precision, t->interval, t->offset, t->sliding, t->soffset);
576
      break;
91✔
577
    }  
578
    case WINDOW_TYPE_SESSION: {
6✔
579
      SSessionTrigger* t = &q->trigger.session;
6✔
580
      mstsDebug("session trigger options, slotId:%d, sessionVal:%" PRId64, t->slotId, t->sessionVal);
6!
581
      break;
6✔
582
    }
583
    case WINDOW_TYPE_STATE: {
29✔
584
      SStateWinTrigger* t = &q->trigger.stateWin;
29✔
585
      mstsDebug("state trigger options, slotId:%d, trueForDuration:%" PRId64, t->slotId, t->trueForDuration);
29!
586
      break;
29✔
587
    }
588
    case WINDOW_TYPE_EVENT:{
11✔
589
      SEventTrigger* t = &q->trigger.event;
11✔
590
      mstsDebug("event trigger options, startCond:%s, endCond:%s, trueForDuration:%" PRId64, (char*)t->startCond, (char*)t->endCond, t->trueForDuration);
11!
591
      break;
11✔
592
    }
593
    case WINDOW_TYPE_COUNT: {
12✔
594
      SCountTrigger* t = &q->trigger.count;
12✔
595
      mstsDebug("count trigger options, countVal:%" PRId64 ", sliding:%" PRId64 ", condCols:%s", t->countVal, t->sliding, (char*)t->condCols);
12!
596
      break;
12✔
597
    }
598
    case WINDOW_TYPE_PERIOD: {
8✔
599
      SPeriodTrigger* t = &q->trigger.period;
8✔
600
      mstsDebug("period trigger options, periodUnit:%d, offsetUnit:%d, precision:%d, period:%" PRId64 ", offset:%" PRId64, 
8!
601
          t->periodUnit, t->offsetUnit, t->precision, t->period, t->offset);
602
      break;
8✔
603
    }
604
    default:
×
605
      mstsDebug("unknown triggerType:%d", q->triggerType);
×
606
      break;
×
607
  }
608

609
  mstsDebugL("create_info: triggerCols:[%s]", (char*)q->triggerCols);
157!
610

611
  mstsDebugL("create_info: partitionCols:[%s]", (char*)q->partitionCols);
157!
612

613
  mstsDebugL("create_info: triggerScanPlan:[%s]", (char*)q->triggerScanPlan);
157!
614

615
  mstsDebugL("create_info: calcPlan:[%s]", (char*)q->calcPlan);
157!
616

617
  mstsDebugL("create_info: subTblNameExpr:[%s]", (char*)q->subTblNameExpr);
157!
618

619
  mstsDebugL("create_info: tagValueExpr:[%s]", (char*)q->tagValueExpr);
157!
620

621

622
  for (int32_t i = 0; i < calcDBNum; ++i) {
313✔
623
    char* dbName = taosArrayGetP(q->calcDB, i);
156✔
624
    mstsDebug("create_info: calcDB[%d] - %s", i, dbName);
156!
625
  }
626

627
  for (int32_t i = 0; i < calcScanNum; ++i) {
326✔
628
    SStreamCalcScan* pScan = taosArrayGet(q->calcScanPlanList, i);
169✔
629
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
169✔
630
    mstsDebugL("create_info: calcScanPlan[%d] - readFromCache:%d vgNum:%d scanPlan:[%s]", i, pScan->readFromCache, vgNum, (char*)pScan->scanPlan);
169!
631
    for (int32_t v = 0; v < vgNum; ++v) {
338✔
632
      mstsDebug("create_info: calcScanPlan[%d] vg[%d] - vgId:%d", i, v, *(int32_t*)taosArrayGet(pScan->vgList, v));
169!
633
    }
634
  }
635

636
  for (int32_t i = 0; i < notifyUrlNum; ++i) {
161✔
637
    char* url = taosArrayGetP(q->pNotifyAddrUrls, i);
4✔
638
    mstsDebug("create_info: notifyUrl[%d] - %s", i, url);
4!
639
  }
640

641
  for (int32_t i = 0; i < outColNum; ++i) {
707✔
642
    SFieldWithOptions* o = taosArrayGet(q->outCols, i);
550✔
643
    mstsDebug("create_info: outCol[%d] - name:%s type:%d flags:%d bytes:%d compress:%u typeMod:%d", 
550!
644
        i, o->name, o->type, o->flags, o->bytes, o->compress, o->typeMod);
645
  }
646
      
647
}
648

649
void mstLogSStmTaskStatus(char* name, int64_t streamId, SStmTaskStatus* pTask, int32_t idx) {
1,138✔
650
  mstsDebug("%s[%d]: task %" PRIx64 " deployId:%d SID:%" PRId64 " nodeId:%d tidx:%d type:%s flags:%" PRIx64 " status:%s lastUpTs:%" PRId64, 
1,138!
651
      name, idx, pTask->id.taskId, pTask->id.deployId, pTask->id.seriousId, pTask->id.nodeId, pTask->id.taskIdx,
652
      gStreamTaskTypeStr[pTask->type], pTask->flags, gStreamStatusStr[pTask->status], pTask->lastUpTs);
653
}
1,138✔
654

655
void mstLogSStmStatus(char* tips, int64_t streamId, SStmStatus* p) {
286✔
656
  if (!(stDebugFlag & DEBUG_DEBUG)) {
286✔
657
    return;
113✔
658
  }
659
  
660
  if (NULL == p) {
173!
661
    mstsDebug("%s: stream status is NULL", tips);
×
662
    return;
×
663
  }
664

665
  int32_t trigReaderNum = taosArrayGetSize(p->trigReaders);
173✔
666
  int32_t trigOReaderNum = taosArrayGetSize(p->trigOReaders);
173✔
667
  int32_t calcReaderNum = taosArrayGetSize(p->calcReaders);
173✔
668
  int32_t triggerNum = p->triggerTask ? 1 : 0;
173✔
669
  int32_t runnerNum = 0;
173✔
670

671
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
689✔
672
    runnerNum += taosArrayGetSize(p->runners[i]);
516✔
673
  }
674

675
  mstsDebug("%s: stream status", tips);
173!
676
  mstsDebug("name:%s runnerNum:%d runnerDeploys:%d runnerReplica:%d lastActionTs:%" PRId64
173!
677
           " trigReaders:%d trigOReaders:%d calcReaders:%d trigger:%d runners:%d",
678
      p->streamName, p->runnerNum, p->runnerDeploys, p->runnerReplica, p->lastActionTs,
679
      trigReaderNum, trigOReaderNum, calcReaderNum, triggerNum, runnerNum);
680

681
  SStmTaskStatus* pTask = NULL;
173✔
682
  for (int32_t i = 0; i < trigReaderNum; ++i) {
431✔
683
    pTask = taosArrayGet(p->trigReaders, i);
258✔
684
    mstLogSStmTaskStatus("trigReader task", streamId, pTask, i);
258✔
685
  }
686

687
  for (int32_t i = 0; i < trigOReaderNum; ++i) {
173!
688
    pTask = taosArrayGet(p->trigOReaders, i);
×
689
    mstLogSStmTaskStatus("trigOReader task", streamId, pTask, i);
×
690
  }
691

692
  for (int32_t i = 0; i < calcReaderNum; ++i) {
298✔
693
    pTask = taosArrayGet(p->calcReaders, i);
125✔
694
    mstLogSStmTaskStatus("calcReader task", streamId, pTask, i);
125✔
695
  }
696

697
  if (triggerNum > 0) {
173!
698
    mstLogSStmTaskStatus("trigger task", streamId, p->triggerTask, 0);
173✔
699
  }
700

701
  for (int32_t i = 0; i < p->runnerDeploys; ++i) {
689✔
702
    int32_t num = taosArrayGetSize(p->runners[i]);
516✔
703
    if (num <= 0) {
516!
704
      continue;
×
705
    }
706
    
707
    mstsDebug("the %dth deploy runners status", i);
516!
708
    for (int32_t m = 0; m < num; ++m) {
1,098✔
709
      pTask = taosArrayGet(p->runners[i], m);
582✔
710
      mstLogSStmTaskStatus("runner task", streamId, pTask, m);
582✔
711
    }
712
  }
713
      
714
}
715

716
bool mstEventPassIsolation(int32_t num, int32_t event) {
5,066✔
717
  bool ret = ((mStreamMgmt.lastTs[event].ts + num * MST_ISOLATION_DURATION) <= mStreamMgmt.hCtx.currentTs);
5,066✔
718
  if (ret) {
5,066✔
719
    mstDebug("event %s passed %d isolation, last:%" PRId64 ", curr:%" PRId64, 
3,692✔
720
        gMndStreamEvent[event], num, mStreamMgmt.lastTs[event].ts, mStreamMgmt.hCtx.currentTs);
721
  }
722

723
  return ret;
5,066✔
724
}
725

726
bool mstEventHandledChkSet(int32_t event) {
3,692✔
727
  if (0 == atomic_val_compare_exchange_8((int8_t*)&mStreamMgmt.lastTs[event].handled, 0, 1)) {
3,692✔
728
    mstDebug("event %s set handled", gMndStreamEvent[event]);
330✔
729
    return true;
330✔
730
  }
731
  return false;
3,362✔
732
}
733

734
int32_t mstGetStreamStatusStr(SStreamObj* pStream, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
1,474✔
735
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
1,474✔
736
  if (0 == active || MND_STM_STATE_NORMAL != state) {
1,474!
737
    mstDebug("mnode streamMgmt not in active mode, active:%d, state:%d", active, state);
×
738
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
×
739
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "Mnode may be unstable, try again later", msgSize);
×
740
    return TSDB_CODE_SUCCESS;
×
741
  }
742

743
  if (atomic_load_8(&pStream->userDropped)) {
1,474!
744
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_DROPPING], statusSize);
×
745
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
×
746
    return TSDB_CODE_SUCCESS;
×
747
  }
748

749
  if (atomic_load_8(&pStream->userStopped)) {
1,474✔
750
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_STOPPED], statusSize);
26✔
751
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
26✔
752
    return TSDB_CODE_SUCCESS;
26✔
753
  }
754

755
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
1,448✔
756
  
757
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &pStream->pCreate->streamId, sizeof(pStream->pCreate->streamId));
1,448✔
758
  if (NULL == pStatus) {
1,448✔
759
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_UNDEPLOYED], statusSize);
190✔
760
    STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
190✔
761
    goto _exit;
190✔
762
  }
763

764
  char tmpBuf[256];
765
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,258✔
766
  switch (stopped) {
1,258!
767
    case 1:
33✔
768
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
33✔
769
      snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s, Failed times: %" PRId64, tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes);
33✔
770
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
33✔
771
      goto _exit;
33✔
772
      break;
773
    case 4:
×
774
      STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_FAILED], statusSize);
×
775
      snprintf(tmpBuf, sizeof(tmpBuf), "Error: %s", tstrerror(TSDB_CODE_GRANT_STREAM_EXPIRED));
×
776
      STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
777
      goto _exit;
×
778
      break;
779
    default:
1,225✔
780
      break;
1,225✔
781
  }
782

783
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
1,225✔
784
    STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_RUNNING], statusSize);
750✔
785
    strcpy(tmpBuf, "Running start from: ");
750✔
786
    (void)formatTimestampLocal(&tmpBuf[strlen(tmpBuf)], pStatus->triggerTask->runningStartTs, TSDB_TIME_PRECISION_MILLI);
750✔
787
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
750✔
788
    goto _exit;
750✔
789
  }
790

791
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[STREAM_STATUS_INIT], statusSize);
475✔
792
  snprintf(tmpBuf, sizeof(tmpBuf), "Current deploy times: %" PRId64, pStatus->deployTimes);
475✔
793
  STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
475✔
794
  goto _exit;
475✔
795

796
_exit:
1,448✔
797
  
798
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
1,448✔
799

800
  return TSDB_CODE_SUCCESS;
1,448✔
801
}
802

803
int32_t mstSetStreamAttrResBlock(SMnode *pMnode, SStreamObj* pStream, SSDataBlock* pBlock, int32_t numOfRows) {
1,474✔
804
  int32_t code = 0;
1,474✔
805
  int32_t cols = 0;
1,474✔
806
  int32_t lino = 0;
1,474✔
807

808
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,474✔
809
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
1,474✔
810
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
811
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
812

813
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
1,474✔
814
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
815

816
  // db_name
817
  char streamDB[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
1,474✔
818
  STR_WITH_MAXSIZE_TO_VARSTR(streamDB, mndGetDbStr(pStream->pCreate->streamDB), sizeof(streamDB));
1,474✔
819
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
820
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
821
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&streamDB, false);
1,474✔
822
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
823

824
  // create time
825
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
826
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
827
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->createTime, false);
1,474✔
828
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
829

830
  // stream id
831
  char streamId2[19] = {0};
1,474✔
832
  char streamId[19 + VARSTR_HEADER_SIZE] = {0};
1,474✔
833
  snprintf(streamId2, sizeof(streamId2), "%" PRIx64, pStream->pCreate->streamId);
1,474✔
834
  STR_WITH_MAXSIZE_TO_VARSTR(streamId, streamId2, sizeof(streamId));
1,474✔
835
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
836
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
837
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamId, false);
1,474✔
838
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
839

840
  // sql
841
  char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0};
1,474✔
842
  STR_WITH_MAXSIZE_TO_VARSTR(sql, pStream->pCreate->sql, sizeof(sql));
1,474✔
843
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
844
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
845
  code = colDataSetVal(pColInfo, numOfRows, (const char*)sql, false);
1,474✔
846
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
847

848
  // status
849
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
1,474✔
850
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
1,474✔
851
  code = mstGetStreamStatusStr(pStream, status, sizeof(status), msg, sizeof(msg));
1,474✔
852
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
853

854
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
855
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
856
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
1,474✔
857
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
858

859
  // snodeLeader
860
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
861
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
862
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pStream->mainSnodeId, false);
1,474✔
863
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
864

865
  // snodeReplica
866
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
867
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
868
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, pStream->mainSnodeId);
1,474✔
869
  int32_t replicaSnodeId = pSnode ? pSnode->replicaId : -1;
1,474!
870
  mndReleaseSnode(pMnode, pSnode);
1,474✔
871
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&replicaSnodeId, false);
1,474✔
872
  TSDB_CHECK_CODE(code, lino, _end);
1,474!
873

874
  // msg
875
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
1,474✔
876
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
1,474!
877
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
1,474✔
878

879
_end:
1,474✔
880
  if (code) {
1,474!
881
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
882
  }
883
  return code;
1,474✔
884
}
885

886

887
int32_t mstGetTaskStatusStr(SStmTaskStatus* pTask, char* status, int32_t statusSize, char* msg, int32_t msgSize) {
15,633✔
888
  char tmpBuf[256];
889
  
890
  STR_WITH_MAXSIZE_TO_VARSTR(status, gStreamStatusStr[pTask->status], statusSize);
15,633✔
891
  if (STREAM_STATUS_FAILED == pTask->status && pTask->errCode) {
15,633!
892
    snprintf(tmpBuf, sizeof(tmpBuf), "Last error: %s", tstrerror(pTask->errCode));
×
893
    STR_WITH_MAXSIZE_TO_VARSTR(msg, tmpBuf, msgSize);
×
894
    return TSDB_CODE_SUCCESS;
×
895
  }
896

897
  if (STREAM_TRIGGER_TASK == pTask->type && mstWaitLock(&pTask->detailStatusLock, true)) {
15,633!
898
    if (pTask->detailStatus) {
2,668✔
899
      SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pTask->detailStatus;
1,848✔
900
      snprintf(tmpBuf, sizeof(tmpBuf), "Current RT/HI/RE session num: %d/%d/%d, histroy progress:%d%%, total AUTO/USER recalc num: %d/%d", 
1,848✔
901
          pTrigger->realtimeSessionNum, pTrigger->historySessionNum, pTrigger->recalcSessionNum, pTrigger->histroyProgress,
902
          pTrigger->autoRecalcNum, (int32_t)taosArrayGetSize(pTrigger->userRecalcs));
1,848✔
903
      taosRUnLockLatch(&pTask->detailStatusLock);
1,848✔
904
      return TSDB_CODE_SUCCESS;
1,848✔
905
    }
906

907
    taosRUnLockLatch(&pTask->detailStatusLock);    
820✔
908
  }
909
  
910
  STR_WITH_MAXSIZE_TO_VARSTR(msg, "", msgSize);
13,785✔
911
  
912
  return TSDB_CODE_SUCCESS;
13,785✔
913
}
914

915
int32_t mstGetTaskExtraStr(SStmTaskStatus* pTask, char* extraStr, int32_t extraSize) {
15,633✔
916
  switch (pTask->type) {
15,633✔
917
    case STREAM_READER_TASK:
4,283✔
918
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
4,283✔
919
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "trigReader", extraSize);
2,909✔
920
      } else {
921
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "calcReader", extraSize);
1,374✔
922
      }
923
      return TSDB_CODE_SUCCESS;
4,283✔
924
    case STREAM_RUNNER_TASK:
8,682✔
925
      if (STREAM_IS_TOP_RUNNER(pTask->flags)) {
8,682✔
926
        STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "topRunner", extraSize);
7,974✔
927
        return TSDB_CODE_SUCCESS;
7,974✔
928
      }
929
      break;
708✔
930
    default:
2,668✔
931
      break;
2,668✔
932
  }
933

934
  STR_WITH_MAXSIZE_TO_VARSTR(extraStr, "", extraSize);
3,376✔
935
  return TSDB_CODE_SUCCESS;
3,376✔
936
}
937

938

939
int32_t mstSetStreamTaskResBlock(SStreamObj* pStream, SStmTaskStatus* pTask, SSDataBlock* pBlock, int32_t numOfRows) {
15,633✔
940
  int32_t code = 0;
15,633✔
941
  int32_t cols = 0;
15,633✔
942
  int32_t lino = 0;
15,633✔
943

944
  // stream_name
945
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
15,633✔
946
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
15,633✔
947
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
948
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
949

950
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
15,633✔
951
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
952

953
  // stream id
954
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
15,633✔
955
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
15,633✔
956
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE])); 
15,633✔
957
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
958
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
959
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
15,633✔
960
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
961

962
  // task id
963
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.taskId);
15,633✔
964
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
15,633✔
965
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
966
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
967
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
15,633✔
968
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
969

970
  // type
971
  char type[20 + VARSTR_HEADER_SIZE] = {0};
15,633✔
972
  STR_WITH_MAXSIZE_TO_VARSTR(type, (STREAM_READER_TASK == pTask->type) ? "Reader" : ((STREAM_TRIGGER_TASK == pTask->type) ? "Trigger" : "Runner"), sizeof(type));
15,633✔
973
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
974
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
975
  code = colDataSetVal(pColInfo, numOfRows, (const char*)type, false);
15,633✔
976
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
977

978
  // serious id
979
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pTask->id.seriousId);
15,633✔
980
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]));
15,633✔
981
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
982
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
983
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
15,633✔
984
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
985

986
  // deploy id
987
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
988
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
989
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.deployId, false);
15,633✔
990
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
991

992
  // node_type
993
  char nodeType[10 + VARSTR_HEADER_SIZE] = {0};
15,633✔
994
  STR_WITH_MAXSIZE_TO_VARSTR(nodeType, (STREAM_READER_TASK == pTask->type) ? "vnode" : "snode", sizeof(nodeType));
15,633✔
995
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
996
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
997
  code = colDataSetVal(pColInfo, numOfRows, (const char*)nodeType, false);
15,633✔
998
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
999

1000
  // node id
1001
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1002
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1003
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.nodeId, false);
15,633✔
1004
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1005

1006
  // task idx
1007
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1008
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1009
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->id.taskIdx, false);
15,633✔
1010
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1011

1012
  // status
1013
  char   status[20 + VARSTR_HEADER_SIZE] = {0};
15,633✔
1014
  char msg[TSDB_RESERVE_VALUE_LEN + VARSTR_HEADER_SIZE] = {0};
15,633✔
1015
  code = mstGetTaskStatusStr(pTask, status, sizeof(status), msg, sizeof(msg));
15,633✔
1016
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1017

1018
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1019
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1020
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&status, false);
15,633✔
1021
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1022

1023
  // start time
1024
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1025
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1026
  if (pTask->runningStartTs) {
15,633✔
1027
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, false);
2,290✔
1028
  } else {
1029
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->runningStartTs, true);
13,343✔
1030
  }
1031
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1032

1033
  // last update
1034
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1035
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1036
  if (pTask->lastUpTs) {
15,633!
1037
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, false);
15,633✔
1038
  } else {
1039
    code = colDataSetVal(pColInfo, numOfRows, (const char*)&pTask->lastUpTs, true);
×
1040
  }
1041
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1042

1043
  // extra info
1044
  char extra[64 + VARSTR_HEADER_SIZE] = {0};
15,633✔
1045
  code = mstGetTaskExtraStr(pTask, extra, sizeof(extra));
15,633✔
1046
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1047
  
1048
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1049
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1050
  code = colDataSetVal(pColInfo, numOfRows, (const char*)extra, false);
15,633✔
1051
  TSDB_CHECK_CODE(code, lino, _end);
15,633!
1052

1053
  // msg
1054
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
15,633✔
1055
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
15,633!
1056
  code = colDataSetVal(pColInfo, numOfRows, (const char*)msg, false);
15,633✔
1057

1058
_end:
15,633✔
1059
  if (code) {
15,633!
1060
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1061
  }
1062
  return code;
15,633✔
1063
}
1064

1065
int32_t mstGetNumOfStreamTasks(SStmStatus* pStatus) {
2,668✔
1066
  int32_t num = taosArrayGetSize(pStatus->trigReaders) + taosArrayGetSize(pStatus->trigOReaders) + taosArrayGetSize(pStatus->calcReaders) + (pStatus->triggerTask ? 1 : 0);
2,668!
1067
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,672✔
1068
    num += taosArrayGetSize(pStatus->runners[i]);
8,004✔
1069
  }
1070

1071
  return num;
2,668✔
1072
}
1073

1074
int32_t mstSetStreamTasksResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
2,906✔
1075
  int32_t code = 0;
2,906✔
1076
  int32_t lino = 0;
2,906✔
1077
  int64_t streamId = pStream->pCreate->streamId;
2,906✔
1078

1079
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
2,906✔
1080

1081
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,906✔
1082
  if (NULL == pStatus) {
2,906✔
1083
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
237✔
1084
    goto _exit;
237✔
1085
  }
1086

1087
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,669✔
1088
  if (stopped) {
2,669✔
1089
    mstsDebug("stream stopped %d, ignore it", stopped);
1!
1090
    goto _exit;
1✔
1091
  }
1092
  
1093
  int32_t count = mstGetNumOfStreamTasks(pStatus);
2,668✔
1094

1095
  if (*numOfRows + count > rowsCapacity) {
2,668✔
1096
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
62✔
1097
    if (code) {
62!
1098
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1099
      TAOS_CHECK_EXIT(code);
×
1100
    }
1101
  }
1102

1103
  SStmTaskStatus* pTask = NULL;
2,668✔
1104
  int32_t trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
2,668✔
1105
  for (int32_t i = 0; i < trigReaderNum; ++i) {
5,518✔
1106
    pTask = taosArrayGet(pStatus->trigReaders, i);
2,850✔
1107
  
1108
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
2,850✔
1109
    if (code == TSDB_CODE_SUCCESS) {
2,850!
1110
      (*numOfRows)++;
2,850✔
1111
    }
1112
  }
1113

1114
  trigReaderNum = taosArrayGetSize(pStatus->trigOReaders);
2,668✔
1115
  for (int32_t i = 0; i < trigReaderNum; ++i) {
2,727✔
1116
    pTask = taosArrayGet(pStatus->trigOReaders, i);
59✔
1117
  
1118
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
59✔
1119
    if (code == TSDB_CODE_SUCCESS) {
59!
1120
      (*numOfRows)++;
59✔
1121
    }
1122
  }
1123

1124

1125
  int32_t calcReaderNum = taosArrayGetSize(pStatus->calcReaders);
2,668✔
1126
  for (int32_t i = 0; i < calcReaderNum; ++i) {
4,042✔
1127
    pTask = taosArrayGet(pStatus->calcReaders, i);
1,374✔
1128
  
1129
    code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
1,374✔
1130
    if (code == TSDB_CODE_SUCCESS) {
1,374!
1131
      (*numOfRows)++;
1,374✔
1132
    }
1133
  }
1134

1135
  if (pStatus->triggerTask) {
2,668!
1136
    code = mstSetStreamTaskResBlock(pStream, pStatus->triggerTask, pBlock, *numOfRows);
2,668✔
1137
    if (code == TSDB_CODE_SUCCESS) {
2,668!
1138
      (*numOfRows)++;
2,668✔
1139
    }
1140
  }
1141

1142
  int32_t runnerNum = 0;
2,668✔
1143
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,672✔
1144
    runnerNum = taosArrayGetSize(pStatus->runners[i]);
8,004✔
1145
    for (int32_t m = 0; m < runnerNum; ++m) {
16,686✔
1146
      pTask = taosArrayGet(pStatus->runners[i], m);
8,682✔
1147
    
1148
      code = mstSetStreamTaskResBlock(pStream, pTask, pBlock, *numOfRows);
8,682✔
1149
      if (code == TSDB_CODE_SUCCESS) {
8,682!
1150
        (*numOfRows)++;
8,682✔
1151
      }
1152
    }
1153
  }
1154
  
1155
  pBlock->info.rows = *numOfRows;
2,668✔
1156

1157
_exit:
2,906✔
1158
  
1159
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
2,906✔
1160

1161
  if (code) {
2,906!
1162
    mError("error happens when build stream tasks result block, lino:%d, code:%s", lino, tstrerror(code));
×
1163
  }
1164
  
1165
  return code;
2,906✔
1166
}
1167

1168

1169
int32_t mstAppendNewRecalcRange(int64_t streamId, SStmStatus *pStream, STimeWindow* pRange) {
5✔
1170
  int32_t code = 0;
5✔
1171
  int32_t lino = 0;
5✔
1172
  bool    locked = false;
5✔
1173
  SArray* userRecalcList = NULL;
5✔
1174

1175
  SStreamRecalcReq req = {.recalcId = 0, .start = pRange->skey, .end = pRange->ekey};
5✔
1176
  TAOS_CHECK_EXIT(taosGetSystemUUIDU64(&req.recalcId));
5!
1177
  
1178
  taosWLockLatch(&pStream->userRecalcLock);
5✔
1179
  locked = true;
5✔
1180
  
1181
  if (NULL == pStream->userRecalcList) {
5!
1182
    userRecalcList = taosArrayInit(2, sizeof(SStreamRecalcReq));
5✔
1183
    if (NULL == userRecalcList) {
5!
1184
      TAOS_CHECK_EXIT(terrno);
×
1185
    }
1186

1187
    TSDB_CHECK_NULL(taosArrayPush(userRecalcList, &req), code, lino, _exit, terrno);
5!
1188

1189
    atomic_store_ptr(&pStream->userRecalcList, userRecalcList);
5✔
1190
    userRecalcList = NULL;    
5✔
1191
  } else {
1192
    TSDB_CHECK_NULL(taosArrayPush(pStream->userRecalcList, &req), code, lino, _exit, terrno);
×
1193
  }
1194
  
1195
  mstsInfo("stream recalc ID:%" PRIx64 " range:%" PRId64 " - %" PRId64 " added", req.recalcId, pRange->skey, pRange->ekey);
5!
1196

1197
_exit:
×
1198

1199
  taosArrayDestroy(userRecalcList);
5✔
1200

1201
  if (locked) {
5!
1202
    taosWUnLockLatch(&pStream->userRecalcLock);
5✔
1203
  }
1204
  
1205
  if (code) {
5!
1206
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1207
  }
1208
  
1209
  return code;
5✔
1210
}
1211

1212

1213

1214
int32_t mstSetStreamRecalculateResBlock(SStreamObj* pStream, SSTriggerRecalcProgress* pProgress, SSDataBlock* pBlock, int32_t numOfRows) {
×
1215
  int32_t code = 0;
×
1216
  int32_t cols = 0;
×
1217
  int32_t lino = 0;
×
1218

1219
  // stream_name
1220
  char streamName[TSDB_TABLE_NAME_LEN + VARSTR_HEADER_SIZE] = {0};
×
1221
  STR_WITH_MAXSIZE_TO_VARSTR(streamName, mndGetStableStr(pStream->name), sizeof(streamName));
×
1222
  SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1223
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1224

1225
  code = colDataSetVal(pColInfo, numOfRows, (const char*)streamName, false);
×
1226
  TSDB_CHECK_CODE(code, lino, _end);
×
1227

1228
  // stream id
1229
  char idstr[19 + VARSTR_HEADER_SIZE] = {0};
×
1230
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pStream->pCreate->streamId);
×
1231
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE); 
×
1232
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1233
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1234
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1235
  TSDB_CHECK_CODE(code, lino, _end);
×
1236

1237
  // recalc id
1238
  snprintf(&idstr[VARSTR_HEADER_SIZE], sizeof(idstr) - VARSTR_HEADER_SIZE, "%" PRIx64, pProgress->recalcId);
×
1239
  varDataSetLen(idstr, strlen(&idstr[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1240
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1241
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1242
  code = colDataSetVal(pColInfo, numOfRows, (const char*)idstr, false);
×
1243
  TSDB_CHECK_CODE(code, lino, _end);
×
1244

1245
  // start
1246
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1247
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1248
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->start, false);
×
1249
  TSDB_CHECK_CODE(code, lino, _end);
×
1250

1251
  // end
1252
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1253
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1254
  code = colDataSetVal(pColInfo, numOfRows, (const char*)&pProgress->end, false);
×
1255
  TSDB_CHECK_CODE(code, lino, _end);
×
1256

1257
  // progress
1258
  char progress[20 + VARSTR_HEADER_SIZE] = {0};
×
1259
  snprintf(&progress[VARSTR_HEADER_SIZE], sizeof(progress) - VARSTR_HEADER_SIZE, "%d%%", pProgress->progress);
×
1260
  varDataSetLen(progress, strlen(&progress[VARSTR_HEADER_SIZE]) + VARSTR_HEADER_SIZE);
×
1261
  pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
×
1262
  TSDB_CHECK_NULL(pColInfo, code, lino, _end, terrno);
×
1263
  code = colDataSetVal(pColInfo, numOfRows, (const char*)progress, false);
×
1264
  TSDB_CHECK_CODE(code, lino, _end);
×
1265

1266
_end:
×
1267
  if (code) {
×
1268
    mError("error happens when build stream attr result block, lino:%d, code:%s", lino, tstrerror(code));
×
1269
  }
1270
  return code;
×
1271
}
1272

1273

1274
int32_t mstSetStreamRecalculatesResBlock(SStreamObj* pStream, SSDataBlock* pBlock, int32_t* numOfRows, int32_t rowsCapacity) {
1✔
1275
  int32_t code = 0;
1✔
1276
  int32_t lino = 0;
1✔
1277
  int64_t streamId = pStream->pCreate->streamId;
1✔
1278

1279
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
1✔
1280

1281
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
1✔
1282
  if (NULL == pStatus) {
1!
1283
    mstsDebug("stream not in streamMap, ignore it, dropped:%d, stopped:%d", atomic_load_8(&pStream->userDropped), atomic_load_8(&pStream->userStopped));
×
1284
    goto _exit;
×
1285
  }
1286

1287
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1✔
1288
  if (stopped) {
1!
1289
    mstsDebug("stream stopped %d, ignore it", stopped);
×
1290
    goto _exit;
×
1291
  }
1292

1293
  if (NULL == pStatus->triggerTask) {
1!
1294
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1295
    goto _exit;
×
1296
  }
1297

1298
  (void)mstWaitLock(&pStatus->triggerTask->detailStatusLock, true);
1✔
1299
  if (NULL == pStatus->triggerTask->detailStatus) {
1!
1300
    mstsDebug("no trigger task now, deployTimes:%" PRId64 ", ignore it", pStatus->deployTimes);
×
1301
    taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1302
    goto _exit;
×
1303
  }
1304

1305
  SSTriggerRuntimeStatus* pTrigger = (SSTriggerRuntimeStatus*)pStatus->triggerTask->detailStatus;
1✔
1306
  int32_t count = taosArrayGetSize(pTrigger->userRecalcs);
1✔
1307

1308
  if (*numOfRows + count > rowsCapacity) {
1!
1309
    code = blockDataEnsureCapacity(pBlock, *numOfRows + count);
×
1310
    if (code) {
×
1311
      mstError("failed to prepare the result block buffer, rows:%d", *numOfRows + count);
×
1312
      taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
×
1313
      TAOS_CHECK_EXIT(code);
×
1314
    }
1315
  }
1316

1317
  for (int32_t i = 0; i < count; ++i) {
1!
1318
    SSTriggerRecalcProgress* pProgress = taosArrayGet(pTrigger->userRecalcs, i);
×
1319
  
1320
    code = mstSetStreamRecalculateResBlock(pStream, pProgress, pBlock, *numOfRows);
×
1321
    if (code == TSDB_CODE_SUCCESS) {
×
1322
      (*numOfRows)++;
×
1323
    }
1324
  }
1325

1326
  taosRUnLockLatch(&pStatus->triggerTask->detailStatusLock);
1✔
1327
  
1328
  pBlock->info.rows = *numOfRows;
1✔
1329

1330
_exit:
1✔
1331
  
1332
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
1✔
1333

1334
  if (code) {
1!
1335
    mError("error happens when build stream recalculates result block, lino:%d, code:%s", lino, tstrerror(code));
×
1336
  }
1337
  
1338
  return code;
1✔
1339
}
1340

1341

1342

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