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

taosdata / TDengine / #5044

06 May 2026 02:35AM UTC coverage: 73.169% (+0.06%) from 73.107%
#5044

push

travis-ci

web-flow
feat: [6659794715] cpu limit (#35153)

244 of 275 new or added lines in 23 files covered. (88.73%)

526 existing lines in 141 files now uncovered.

277745 of 379596 relevant lines covered (73.17%)

133740972.66 hits per line

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

76.86
/source/dnode/mnode/impl/src/mndStreamMgmt.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 "mndStream.h"
17
#include "mndDb.h"
18
#include "mndPrivilege.h"
19
#include "mndShow.h"
20
#include "mndStb.h"
21
#include "mndTrans.h"
22
#include "osMemory.h"
23
#include "parser.h"
24
#include "taoserror.h"
25
#include "tmisce.h"
26
#include "tname.h"
27
#include "mndDnode.h"
28
#include "mndVgroup.h"
29
#include "mndSnode.h"
30
#include "mndMnode.h"
31
#include "cmdnodes.h"
32

33
void msmDestroyActionQ() {
909,626✔
34
  SStmQNode* pQNode = NULL;
909,626✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
909,626✔
37
    return;
454,813✔
38
  }
39

40
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
456,704✔
41
  }
42

43
  taosMemoryFreeClear(mStreamMgmt.actionQ->head);
454,813✔
44
  taosMemoryFreeClear(mStreamMgmt.actionQ);
454,813✔
45
}
46

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,271,442✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
13,628,652✔
49
    taosHashCleanup(pCtx->deployStm[m]);
11,357,210✔
50
    taosHashCleanup(pCtx->actionStm[m]);
11,357,210✔
51
  }
52
}
2,271,442✔
53

54
void msmDestroyThreadCtxs() {
909,626✔
55
  if (NULL == mStreamMgmt.tCtx) {
909,626✔
56
    return;
454,813✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,726,255✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,271,442✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
454,813✔
63
}
64

65

66
void msmDestroyRuntimeInfo(SMnode *pMnode) {
909,626✔
67
  msmDestroyActionQ();
909,626✔
68
  msmDestroyThreadCtxs();
909,626✔
69

70
  taosHashCleanup(mStreamMgmt.toUpdateScanMap);
909,626✔
71
  mStreamMgmt.toUpdateScanMap = NULL;
909,626✔
72
  mStreamMgmt.toUpdateScanNum = 0;
909,626✔
73
  taosHashCleanup(mStreamMgmt.toDeployVgMap);
909,626✔
74
  mStreamMgmt.toDeployVgMap = NULL;
909,626✔
75
  mStreamMgmt.toDeployVgTaskNum = 0;
909,626✔
76
  taosHashCleanup(mStreamMgmt.toDeploySnodeMap);
909,626✔
77
  mStreamMgmt.toDeploySnodeMap = NULL;
909,626✔
78
  mStreamMgmt.toDeploySnodeTaskNum = 0;
909,626✔
79

80
  taosHashCleanup(mStreamMgmt.dnodeMap);
909,626✔
81
  mStreamMgmt.dnodeMap = NULL;
909,626✔
82
  taosHashCleanup(mStreamMgmt.snodeMap);
909,626✔
83
  mStreamMgmt.snodeMap = NULL;
909,626✔
84
  taosHashCleanup(mStreamMgmt.vgroupMap);
909,626✔
85
  mStreamMgmt.vgroupMap = NULL;
909,626✔
86
  taosHashCleanup(mStreamMgmt.taskMap);
909,626✔
87
  mStreamMgmt.taskMap = NULL;
909,626✔
88
  taosHashCleanup(mStreamMgmt.streamMap);
909,626✔
89
  mStreamMgmt.streamMap = NULL;
909,626✔
90

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
909,626✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
909,626✔
94
}
909,626✔
95

96

97
void msmStopStreamByError(int64_t streamId, SStmStatus* pStatus, int32_t errCode, int64_t currTs) {
6,737✔
98
  int32_t code = TSDB_CODE_SUCCESS;
6,737✔
99
  int32_t lino = 0;
6,737✔
100
  SStmStatus* pStream = NULL;
6,737✔
101

102
  mstsInfo("try to stop stream for error: %s", tstrerror(errCode));
6,737✔
103

104
  if (NULL == pStatus) {
6,737✔
105
    pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
404✔
106
    if (NULL == pStream) {
404✔
107
      mstsInfo("stream already not in streamMap, error:%s", tstrerror(terrno));
×
108
      goto _exit;
×
109
    }
110

111
    pStatus = pStream;
404✔
112
  }
113

114
  int8_t stopped = atomic_load_8(&pStatus->stopped);
6,737✔
115
  if (stopped) {
6,737✔
116
    mstsDebug("stream already stopped %d, ignore stop", stopped);
×
117
    goto _exit;
×
118
  }
119

120
  if (pStatus->triggerTask && pStatus->triggerTask->runningStartTs && (currTs - pStatus->triggerTask->runningStartTs) > 2 * MST_ISOLATION_DURATION) {
6,737✔
121
    pStatus->fatalRetryTimes = 0;
223✔
122
    mstsDebug("reset stream retryTimes, running duation:%" PRId64 "ms", currTs - pStatus->triggerTask->runningStartTs);
223✔
123
  }
124

125
  pStatus->fatalRetryTimes++;
6,737✔
126
  pStatus->fatalError = errCode;
6,737✔
127
  pStatus->fatalRetryDuration = (pStatus->fatalRetryTimes > 10) ? MST_MAX_RETRY_DURATION : MST_ISOLATION_DURATION;
6,737✔
128
  pStatus->fatalRetryTs = currTs + pStatus->fatalRetryDuration;
6,737✔
129

130
  pStatus->stat.lastError = errCode;
6,737✔
131
    
132
  if (0 == atomic_val_compare_exchange_8(&pStatus->stopped, 0, 1)) {
6,737✔
133
    MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, currTs);
6,737✔
134
  }
135

136
_exit:
×
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
6,737✔
139

140
  if (code) {
6,737✔
141
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
142
  }
143
}
6,737✔
144

145

146
static void msmSetInitRuntimeState(int8_t state) {
455,181✔
147
  switch (state) {
455,181✔
148
    case MND_STM_STATE_WATCH:
718✔
149
      mStreamMgmt.watch.ending = 0;
718✔
150
      mStreamMgmt.watch.taskRemains = 0;
718✔
151
      mStreamMgmt.watch.processing = 0;
718✔
152
      mstInfo("switch to WATCH state");
718✔
153
      break;
718✔
154
    case MND_STM_STATE_NORMAL:
454,463✔
155
      MND_STREAM_SET_LAST_TS(STM_EVENT_NORMAL_BEGIN, taosGetTimestampMs());
907,933✔
156
      mstInfo("switch to NORMAL state");
454,463✔
157
      break;
454,463✔
158
    default:
×
159
      return;
×
160
  }
161
  
162
  atomic_store_8(&mStreamMgmt.state, state);
455,181✔
163
}
164

165
void msmSTDeleteSnodeFromMap(int32_t snodeId) {
×
166
  int32_t code = taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
×
167
  if (code) {
×
168
    mstWarn("remove snode %d from snodeMap failed, error:%s", snodeId, tstrerror(code));
×
169
  } else {
170
    mstInfo("snode %d removed from snodeMap", snodeId);
×
171
  }
172
}
×
173

174
static int32_t msmSTAddSnodesToMap(SMnode* pMnode) {
487,630✔
175
  int32_t code = TSDB_CODE_SUCCESS;
487,630✔
176
  int32_t lino = 0;
487,630✔
177
  SStmSnodeStatus tasks = {0};
487,630✔
178
  SSnodeObj *pSnode = NULL;
487,630✔
179
  void *pIter = NULL;
487,630✔
180
  while (1) {
181
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pSnode);
541,040✔
182
    if (pIter == NULL) {
541,040✔
183
      break;
487,630✔
184
    }
185

186
    tasks.lastUpTs = taosGetTimestampMs();
53,410✔
187
    code = taosHashPut(mStreamMgmt.snodeMap, &pSnode->id, sizeof(pSnode->id), &tasks, sizeof(tasks));
53,410✔
188
    if (code && TSDB_CODE_DUP_KEY != code) {
53,410✔
189
      sdbRelease(pMnode->pSdb, pSnode);
×
190
      sdbCancelFetch(pMnode->pSdb, pIter);
×
191
      pSnode = NULL;
×
192
      TAOS_CHECK_EXIT(code);
×
193
    }
194

195
    code = TSDB_CODE_SUCCESS;
53,410✔
196
  
197
    sdbRelease(pMnode->pSdb, pSnode);
53,410✔
198
  }
199

200
  pSnode = NULL;
487,630✔
201

202
_exit:
487,630✔
203

204
  if (code) {
487,630✔
205
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
206
  }
207

208
  return code;
487,630✔
209
}
210

211
static int32_t msmSTAddDnodesToMap(SMnode* pMnode) {
1,012,316✔
212
  int32_t code = TSDB_CODE_SUCCESS;
1,012,316✔
213
  int32_t lino = 0;
1,012,316✔
214
  int64_t lastUpTs = INT64_MIN;
1,012,316✔
215
  SDnodeObj *pDnode = NULL;
1,012,316✔
216
  void *pIter = NULL;
1,012,316✔
217
  while (1) {
218
    pIter = sdbFetch(pMnode->pSdb, SDB_DNODE, pIter, (void **)&pDnode);
2,111,177✔
219
    if (pIter == NULL) {
2,111,177✔
220
      break;
1,012,316✔
221
    }
222

223
    code = taosHashPut(mStreamMgmt.dnodeMap, &pDnode->id, sizeof(pDnode->id), &lastUpTs, sizeof(lastUpTs));
1,098,861✔
224
    if (code && TSDB_CODE_DUP_KEY != code) {
1,098,861✔
225
      sdbRelease(pMnode->pSdb, pDnode);
×
226
      sdbCancelFetch(pMnode->pSdb, pIter);
×
227
      pDnode = NULL;
×
228
      TAOS_CHECK_EXIT(code);
×
229
    }
230

231
    code = TSDB_CODE_SUCCESS;
1,098,861✔
232
    sdbRelease(pMnode->pSdb, pDnode);
1,098,861✔
233
  }
234

235
  pDnode = NULL;
1,012,316✔
236

237
_exit:
1,012,316✔
238

239
  if (code) {
1,012,316✔
240
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
241
  }
242

243
  return code;
1,012,316✔
244
}
245

246

247

248
static int32_t msmSTAddToTaskMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask) {
1,463,833✔
249
  int32_t code = TSDB_CODE_SUCCESS;
1,463,833✔
250
  int32_t lino = 0;
1,463,833✔
251
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) : taosArrayGetSize(pTasks));
1,463,833✔
252
  int64_t key[2] = {streamId, 0};
1,463,833✔
253
  SListNode* pNode = pList ? listHead(pList) : NULL;
1,463,833✔
254
  
255
  for (int32_t i = 0; i < taskNum; ++i) {
3,252,149✔
256
    SStmTaskStatus* pStatus = pTask ? pTask : (pList ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
1,788,316✔
257
    key[1] = pStatus->id.taskId;
1,788,316✔
258
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.taskMap, key, sizeof(key), &pStatus, POINTER_BYTES));
1,788,316✔
259
    mstsDebug("task %" PRIx64" tidx %d added to taskMap", pStatus->id.taskId, pStatus->id.taskIdx);
1,788,316✔
260
    if (pNode) {
1,788,316✔
261
      pNode = TD_DLIST_NODE_NEXT(pNode);
404,375✔
262
    }
263
  }
264
  
265
_exit:
1,463,833✔
266

267
  if (code) {
1,463,833✔
268
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
269
  }
270
  
271
  return code;
1,463,833✔
272
}
273

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
755,289✔
275
  int32_t code = TSDB_CODE_SUCCESS;
755,289✔
276
  int32_t lino = 0;
755,289✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
755,289✔
278
  if (NULL == pStream) {
755,289✔
279
    SStmVgStreamStatus stream = {0};
538,386✔
280
    if (trigReader) {
538,386✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
323,775✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
323,775✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
647,550✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
214,611✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
214,611✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
429,222✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
538,386✔
290
    goto _exit;
538,386✔
291
  }
292
  
293
  if (trigReader) {
216,903✔
294
    if (NULL == pStream->trigReaders) {
24,521✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
24,521✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
24,521✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
49,042✔
300
    goto _exit;
24,521✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
192,382✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
131,806✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
131,806✔
306
  }
307

308
  TSDB_CHECK_NULL(taosArrayPush(pStream->calcReaders, &pStatus), code, lino, _exit, terrno);
384,764✔
309

310
_exit:
192,382✔
311

312
  if (code) {
755,289✔
313
    mstsError("%s task %" PRIx64 " SID:%" PRIx64 " failed to add to vgroup %d streamHash in %s at line %d, error:%s", 
×
314
        trigReader ? "trigReader" : "calcReader", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId, __FUNCTION__, lino, tstrerror(code));
315
  } else {
316
    mstsDebug("%s task %" PRIx64 " SID:%" PRIx64 " added to vgroup %d streamHash", 
755,289✔
317
        trigReader ? "trigReader" : "calcReader", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId);
318
  }
319

320
  return code;
755,289✔
321
}
322

323
static int32_t msmSTAddToVgroupMapImpl(int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
755,289✔
324
  int32_t code = TSDB_CODE_SUCCESS;
755,289✔
325
  int32_t lino = 0;
755,289✔
326
  SStmVgroupStatus vg = {0};
755,289✔
327

328
  SStmVgroupStatus* pVg = taosHashGet(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId));
755,289✔
329
  if (NULL == pVg) {
755,289✔
330
    vg.streamTasks = taosHashInit(2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
96,792✔
331
    TSDB_CHECK_NULL(vg.streamTasks, code, lino, _exit, terrno);
96,792✔
332
    taosHashSetFreeFp(vg.streamTasks, mstDestroySStmVgStreamStatus);
96,792✔
333

334
    vg.lastUpTs = taosGetTimestampMs();
96,792✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
96,792✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
96,792✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
658,497✔
339
  }
340
  
341
_exit:
755,137✔
342

343
  if (code) {
755,289✔
344
    mstDestroyVgroupStatus(&vg);
×
345
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
346
  } else {
347
    mstsDebug("task %" PRIx64 " tidx %d added to vgroupMap %d", pStatus->id.taskId, pStatus->id.taskIdx, pStatus->id.nodeId);
755,289✔
348
  }
349

350
  return code;
755,289✔
351
}
352

353
static int32_t msmTDAddToVgroupMap(SHashObj* pVgMap, SStmTaskDeploy* pDeploy, int64_t streamId) {
756,974✔
354
  int32_t code = TSDB_CODE_SUCCESS;
756,974✔
355
  int32_t lino = 0;
756,974✔
356
  SStmVgTasksToDeploy vg = {0};
756,974✔
357
  SStreamTask* pTask = &pDeploy->task;
756,974✔
358
  SStmTaskToDeployExt ext = {0};
756,974✔
359
  ext.deploy = *pDeploy;
756,974✔
360

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
756,974✔
363
    if (NULL == pVg) {
756,974✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
219,536✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
219,536✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
439,072✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
219,536✔
368
      if (TSDB_CODE_SUCCESS == code) {
219,536✔
369
        goto _return;
219,536✔
370
      }
371

372
      if (TSDB_CODE_DUP_KEY != code) {
×
373
        goto _return;
×
374
      }    
375

376
      taosArrayDestroy(vg.taskList);
×
377
      continue;
×
378
    }
379

380
    taosWLockLatch(&pVg->lock);
537,438✔
381
    if (NULL == pVg->taskList) {
537,438✔
382
      pVg->taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
×
383
      TSDB_CHECK_NULL(pVg->taskList, code, lino, _return, terrno);
×
384
    }
385
    if (NULL == taosArrayPush(pVg->taskList, &ext)) {
1,074,876✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
537,438✔
390
    
391
    taosHashRelease(pVgMap, pVg);
537,438✔
392
    break;
537,438✔
393
  }
394
  
395
_return:
756,974✔
396

397
  if (code) {
756,974✔
398
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
399
  } else {
400
    int32_t num = atomic_add_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
756,974✔
401
    msttDebug("task added to toDeployVgTaskNum, vgToDeployTaskNum:%d", num);
756,974✔
402
  }
403

404
  return code;
756,974✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
1,033,512✔
409
  int32_t code = TSDB_CODE_SUCCESS;
1,033,512✔
410
  int32_t lino = 0;
1,033,512✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
1,033,512✔
412
  if (NULL == pStream) {
1,033,512✔
413
    SStmSnodeStreamStatus stream = {0};
240,904✔
414
    if (deployId < 0) {
240,904✔
415
      stream.trigger = pStatus;
3,445✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
237,459✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
237,459✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
474,918✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
240,904✔
423
    goto _exit;
240,904✔
424
  }
425
  
426
  if (deployId < 0) {
792,608✔
427
    if (NULL != pStream->trigger) {
231,081✔
428
      mstsWarn("stream already got trigger task %" PRIx64 " SID:%" PRIx64 " in snode %d, replace it with task %" PRIx64 " SID:%" PRIx64, 
×
429
          pStream->trigger->id.taskId, pStream->trigger->id.seriousId, pStatus->id.nodeId, pStatus->id.taskId, pStatus->id.seriousId);
430
    }
431
    
432
    pStream->trigger = pStatus;
231,081✔
433
    goto _exit;
231,081✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
561,527✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
456,719✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
456,719✔
439
  }
440

441
  TSDB_CHECK_NULL(taosArrayPush(pStream->runners[deployId], &pStatus), code, lino, _exit, terrno);
1,123,054✔
442

443
_exit:
561,527✔
444

445
  if (code) {
1,033,512✔
446
    mstsError("%s task %" PRIx64 " SID:%" PRIx64 " failed to add to snode %d streamHash deployId:%d in %s at line %d, error:%s", 
×
447
        (deployId < 0) ? "trigger" : "runner", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId, deployId, __FUNCTION__, lino, tstrerror(code));
448
  } else {
449
    mstsDebug("%s task %" PRIx64 " SID:%" PRIx64 " added to snode %d streamHash deployId:%d", 
1,033,512✔
450
        (deployId < 0) ? "trigger" : "runner", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId, deployId);
451
  }
452

453
  return code;
1,033,512✔
454
}
455

456

457
static int32_t msmSTAddToSnodeMapImpl(int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
1,033,512✔
458
  int32_t code = TSDB_CODE_SUCCESS;
1,033,512✔
459
  int32_t lino = 0;
1,033,512✔
460

461
  SStmSnodeStatus* pSnode = taosHashGet(mStreamMgmt.snodeMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId));
1,033,512✔
462
  if (NULL == pSnode) {
1,033,512✔
463
    mstsWarn("snode %d not exists in snodeMap anymore, may be dropped", pStatus->id.nodeId);
×
464
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
465
  } else {
466
    if (NULL == pSnode->streamTasks) {
1,033,512✔
467
      pSnode->streamTasks = taosHashInit(2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
27,645✔
468
      TSDB_CHECK_NULL(pSnode->streamTasks, code, lino, _exit, terrno);
27,645✔
469
      taosHashSetFreeFp(pSnode->streamTasks, mstDestroySStmSnodeStreamStatus);
27,645✔
470
    }
471
    
472
    TAOS_CHECK_EXIT(msmSTAddToSnodeStreamHash(pSnode->streamTasks, streamId, pStatus, deployId));
1,033,512✔
473
  }
474
  
475
_exit:
1,033,512✔
476

477
  if (code) {
1,033,512✔
478
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
479
  } else {
480
    mstsDebug("%s task %" PRIx64 " tidx %d added to snodeMap, snodeId:%d", (deployId < 0) ? "trigger" : "runner", 
1,033,512✔
481
        pStatus->id.taskId, pStatus->id.taskIdx, pStatus->id.nodeId);
482
  }
483

484
  return code;
1,033,512✔
485
}
486

487

488

489
static int32_t msmTDAddTriggerToSnodeMap(SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
234,526✔
490
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
491
  int32_t lino = 0;
234,526✔
492
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
493
  SStmSnodeTasksDeploy snode = {0};
234,526✔
494
  SStmTaskToDeployExt ext;
234,108✔
495
  SStreamTask* pTask = &pDeploy->task;
234,526✔
496

497
  while (true) {
×
498
    SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
234,526✔
499
    if (NULL == pSnode) {
234,526✔
500
      snode.triggerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
1,048✔
501
      TSDB_CHECK_NULL(snode.triggerList, code, lino, _return, terrno);
1,048✔
502

503
      ext.deploy = *pDeploy;
1,048✔
504
      ext.deployed = false;
1,048✔
505
      TSDB_CHECK_NULL(taosArrayPush(snode.triggerList, &ext), code, lino, _return, terrno);
2,096✔
506

507
      code = taosHashPut(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &snode, sizeof(snode));
1,048✔
508
      if (TSDB_CODE_SUCCESS == code) {
1,048✔
509
        goto _return;
1,048✔
510
      }
511

512
      if (TSDB_CODE_DUP_KEY != code) {
×
513
        goto _return;
×
514
      }    
515

516
      taosArrayDestroy(snode.triggerList);
×
517
      continue;
×
518
    }
519
    
520
    taosWLockLatch(&pSnode->lock);
233,478✔
521
    if (NULL == pSnode->triggerList) {
233,478✔
522
      pSnode->triggerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
75,287✔
523
      if (NULL == pSnode->triggerList) {
75,287✔
524
        taosWUnLockLatch(&pSnode->lock);
×
525
        TSDB_CHECK_NULL(pSnode->triggerList, code, lino, _return, terrno);
×
526
      }
527
    }
528
    
529
    ext.deploy = *pDeploy;
233,478✔
530
    ext.deployed = false;
233,478✔
531
    
532
    if (NULL == taosArrayPush(pSnode->triggerList, &ext)) {
466,956✔
533
      taosWUnLockLatch(&pSnode->lock);
×
534
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
535
    }
536
    taosWUnLockLatch(&pSnode->lock);
233,478✔
537
    
538
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
233,478✔
539
    break;
233,478✔
540
  }
541
  
542
_return:
234,526✔
543

544
  if (code) {
234,526✔
545
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
546
  } else {
547
    msttDebug("trigger task added to toDeploySnodeMap, tidx:%d", pTask->taskIdx);
234,526✔
548
  }
549

550
  return code;
234,526✔
551
}
552

553
static int32_t msmTDAddRunnerToSnodeMap(SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
798,986✔
554
  int32_t code = TSDB_CODE_SUCCESS;
798,986✔
555
  int32_t lino = 0;
798,986✔
556
  int64_t streamId = pStream->pCreate->streamId;
798,986✔
557
  SStmSnodeTasksDeploy snode = {0};
798,986✔
558
  SStmTaskToDeployExt ext;
797,618✔
559
  SStreamTask* pTask = &pDeploy->task;
798,986✔
560

561
  while (true) {
×
562
    SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
798,986✔
563
    if (NULL == pSnode) {
798,986✔
564
      snode.runnerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
78,553✔
565
      TSDB_CHECK_NULL(snode.runnerList, code, lino, _return, terrno);
78,553✔
566

567
      ext.deploy = *pDeploy;
78,553✔
568
      ext.deployed = false;
78,553✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
157,106✔
570

571
      code = taosHashPut(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &snode, sizeof(snode));
78,553✔
572
      if (TSDB_CODE_SUCCESS == code) {
78,553✔
573
        goto _return;
78,553✔
574
      }
575

576
      if (TSDB_CODE_DUP_KEY != code) {
×
577
        goto _return;
×
578
      }    
579

580
      taosArrayDestroy(snode.runnerList);
×
581
      continue;
×
582
    }
583
    
584
    taosWLockLatch(&pSnode->lock);
720,433✔
585
    if (NULL == pSnode->runnerList) {
720,433✔
586
      pSnode->runnerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
1,048✔
587
      if (NULL == pSnode->runnerList) {
1,048✔
588
        taosWUnLockLatch(&pSnode->lock);
×
589
        TSDB_CHECK_NULL(pSnode->runnerList, code, lino, _return, terrno);
×
590
      }
591
    }
592
    
593
    ext.deploy = *pDeploy;
720,433✔
594
    ext.deployed = false;
720,433✔
595
    
596
    if (NULL == taosArrayPush(pSnode->runnerList, &ext)) {
1,440,866✔
597
      taosWUnLockLatch(&pSnode->lock);
×
598
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
599
    }
600
    taosWUnLockLatch(&pSnode->lock);
720,433✔
601
    
602
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
720,433✔
603
    break;
720,433✔
604
  }
605
  
606
_return:
798,986✔
607

608
  if (code) {
798,986✔
609
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
610
  } else {
611
    msttDebug("task added to toDeploySnodeMap, tidx:%d", pTask->taskIdx);
798,986✔
612
  }
613

614
  return code;
798,986✔
615
}
616

617
static int32_t msmTDAddRunnersToSnodeMap(SArray* runnerList, SStreamObj* pStream) {
738,098✔
618
  int32_t code = TSDB_CODE_SUCCESS;
738,098✔
619
  int32_t lino = 0;
738,098✔
620
  int32_t runnerNum = taosArrayGetSize(runnerList);
738,098✔
621
  SStmTaskDeploy* pDeploy = NULL;
738,098✔
622
  int64_t streamId = pStream->pCreate->streamId;
738,098✔
623

624
  for (int32_t i = 0; i < runnerNum; ++i) {
1,537,084✔
625
    pDeploy = taosArrayGet(runnerList, i);
798,986✔
626
    
627
    TAOS_CHECK_EXIT(msmTDAddRunnerToSnodeMap(pDeploy, pStream));
798,986✔
628
    
629
    (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);    
798,986✔
630
  }
631

632
_exit:
738,098✔
633

634
  if (code) {
738,098✔
635
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
636
  }
637

638
  return code;
738,098✔
639
}
640

641

642
int32_t msmUpdateSnodeUpTs(SStmGrpCtx* pCtx) {
1,746,353✔
643
  int32_t  code = TSDB_CODE_SUCCESS;
1,746,353✔
644
  int32_t  lino = 0;
1,746,353✔
645
  SStmSnodeStatus* pStatus = NULL;
1,746,353✔
646
  bool     noExists = false;
1,746,353✔
647

648
  while (true) {
649
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &pCtx->pReq->snodeId, sizeof(pCtx->pReq->snodeId));
1,764,308✔
650
    if (NULL == pStatus) {
1,764,308✔
651
      if (noExists) {
18,310✔
652
        mstWarn("snode %d not exists in snodeMap, may be dropped, ignore it", pCtx->pReq->snodeId);
355✔
653
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
355✔
654
      }
655

656
      noExists = true;
17,955✔
657
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pCtx->pMnode));
17,955✔
658
      
659
      continue;
17,955✔
660
    }
661

662
    atomic_store_32(&pStatus->runnerThreadNum, pCtx->pReq->runnerThreadNum);
1,745,998✔
663
    
664
    while (true) {
×
665
      int64_t lastTsValue = atomic_load_64(&pStatus->lastUpTs);
1,745,998✔
666
      if (pCtx->currTs > lastTsValue) {
1,745,998✔
667
        if (lastTsValue == atomic_val_compare_exchange_64(&pStatus->lastUpTs, lastTsValue, pCtx->currTs)) {
1,728,398✔
668
          mstDebug("snode %d lastUpTs updated", pCtx->pReq->snodeId);
1,728,398✔
669
          return code;
1,728,398✔
670
        }
671

672
        continue;
×
673
      }
674

675
      return code;
17,600✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
355✔
682

683
  if (code) {
355✔
684
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
355✔
685
  }
686

687
  return code;  
355✔
688
}
689

690
void msmUpdateVgroupUpTs(SStmGrpCtx* pCtx, int32_t vgId) {
59,228,172✔
691
  int32_t  code = TSDB_CODE_SUCCESS;
59,228,172✔
692
  int32_t  lino = 0;
59,228,172✔
693
  SStmVgroupStatus* pStatus = taosHashGet(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
59,228,172✔
694
  if (NULL == pStatus) {
59,228,172✔
695
    mstDebug("vgroup %d not exists in vgroupMap, ignore update upTs", vgId);
52,769,035✔
696
    return;
52,769,035✔
697
  }
698

699
  while (true) {
×
700
    int64_t lastTsValue = atomic_load_64(&pStatus->lastUpTs);
6,459,137✔
701
    if (pCtx->currTs > lastTsValue) {
6,459,137✔
702
      if (lastTsValue == atomic_val_compare_exchange_64(&pStatus->lastUpTs, lastTsValue, pCtx->currTs)) {
6,336,685✔
703
        mstDebug("vgroup %d lastUpTs updated to %" PRId64, vgId, pCtx->currTs);
6,336,685✔
704
        return;
6,336,685✔
705
      }
706

707
      continue;
×
708
    }
709

710
    return;
122,452✔
711
  }  
712
}
713

714
int32_t msmUpdateVgroupsUpTs(SStmGrpCtx* pCtx) {
18,318,543✔
715
  int32_t code = TSDB_CODE_SUCCESS;
18,318,543✔
716
  int32_t lino = 0;
18,318,543✔
717
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
18,318,543✔
718

719
  mstDebug("start to update vgroups upTs");
18,318,543✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
76,941,725✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
58,623,182✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
58,623,182✔
725
  }
726

727
_exit:
18,318,543✔
728

729
  if (code) {
18,318,543✔
730
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
731
  }
732

733
  return code;
18,318,543✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
416,600✔
739
  int32_t num = taosArrayGetSize(pList);
416,600✔
740
  for (int32_t i = 0; i < num; ++i) {
1,249,913✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
1,032,973✔
742
    if (pScan->readFromCache) {
1,032,973✔
743
      return pScan->scanPlan;
199,660✔
744
    }
745
  }
746

747
  return NULL;
216,940✔
748
}
749

750
int32_t msmBuildReaderDeployInfo(SStmTaskDeploy* pDeploy, void* calcScanPlan, SStmStatus* pInfo, bool triggerReader) {
756,974✔
751
  SStreamReaderDeployMsg* pMsg = &pDeploy->msg.reader;
756,974✔
752
  pMsg->triggerReader = triggerReader;
756,974✔
753
  
754
  if (triggerReader) {
756,974✔
755
    SStreamReaderDeployFromTrigger* pTrigger = &pMsg->msg.trigger;
349,125✔
756
    pTrigger->triggerTblName = pInfo->pCreate->triggerTblName;
349,125✔
757
    pTrigger->triggerTblUid = pInfo->pCreate->triggerTblUid;
349,125✔
758
    pTrigger->triggerTblSuid = pInfo->pCreate->triggerTblSuid;
349,125✔
759
    pTrigger->triggerTblType = pInfo->pCreate->triggerTblType;
349,125✔
760
    pTrigger->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pInfo->pCreate->triggerTblType, pInfo->pCreate->flags);
349,125✔
761
    pTrigger->deleteReCalc = pInfo->pCreate->deleteReCalc;
349,125✔
762
    pTrigger->deleteOutTbl = pInfo->pCreate->deleteOutTbl;
349,125✔
763
    pTrigger->partitionCols = pInfo->pCreate->partitionCols;
349,125✔
764
    pTrigger->triggerCols = pInfo->pCreate->triggerCols;
349,125✔
765
    //pTrigger->triggerPrevFilter = pStream->pCreate->triggerPrevFilter;
766
    pTrigger->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
349,125✔
767
    pTrigger->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
349,125✔
768
  } else {
769
    SStreamReaderDeployFromCalc* pCalc = &pMsg->msg.calc;
407,849✔
770
    pCalc->execReplica = pInfo->runnerDeploys * pInfo->runnerReplica;
407,849✔
771
    if (calcScanPlan) {
407,849✔
772
      pCalc->calcScanPlan = taosStrdup(calcScanPlan);
407,849✔
773
      if (NULL == pCalc->calcScanPlan) {
407,849✔
774
        return terrno;
×
775
      }
776
      pCalc->freeScanPlan = true;
407,849✔
777
    }
778
  }
779

780
  return TSDB_CODE_SUCCESS;
756,974✔
781
}
782

783
int32_t msmBuildTriggerRunnerTargets(SMnode* pMnode, SStmStatus* pInfo, int64_t streamId, SArray** ppRes) {
231,081✔
784
  int32_t code = TSDB_CODE_SUCCESS;
231,081✔
785
  int32_t lino = 0;
231,081✔
786

787
  if (pInfo->runnerDeploys > 0) {
231,081✔
788
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
231,081✔
789
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
231,081✔
790
  }
791
  
792
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
924,324✔
793
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
693,243✔
794
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
693,243✔
795

796
    if (!STREAM_IS_TOP_RUNNER(pStatus->flags)) {
693,243✔
797
      mstsError("the last runner task %" PRIx64 " SID:%" PRId64 " tidx:%d in deploy %d is not top runner", 
×
798
          pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.taskIdx, i);
799
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
800
    }
801
    
802
    SStreamRunnerTarget runner;
691,989✔
803
    runner.addr.taskId = pStatus->id.taskId;
693,243✔
804
    runner.addr.nodeId = pStatus->id.nodeId;
693,243✔
805
    runner.addr.epset = mndGetDnodeEpsetById(pMnode, pStatus->id.nodeId);
693,243✔
806
    runner.execReplica = pInfo->runnerReplica; 
693,243✔
807
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &runner), code, lino, _exit, terrno);
1,386,486✔
808
    mstsDebug("the %dth runner target added to trigger's runnerList, TASK:%" PRIx64 , i, runner.addr.taskId);
693,243✔
809
  }
810

811
_exit:
231,081✔
812

813
  if (code) {
231,081✔
814
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
815
  }
816
  
817
  return TSDB_CODE_SUCCESS;
231,081✔
818
}
819

820
int32_t msmBuildStreamSnodeInfo(SMnode* pMnode, SStreamObj* pStream, SStreamSnodeInfo* pInfo) {
×
821
  int64_t streamId = pStream->pCreate->streamId;
×
822
  int32_t leaderSnodeId = atomic_load_32(&pStream->mainSnodeId);
×
823
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, leaderSnodeId);
×
824
  if (NULL == pSnode) {
×
825
    mstsError("snode %d not longer exists, ignore build stream snode info", leaderSnodeId);
×
826
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
827
  }
828
  
829
  pInfo->leaderSnodeId = leaderSnodeId;
×
830
  pInfo->replicaSnodeId = pSnode->replicaId;
×
831

832
  mndReleaseSnode(pMnode, pSnode);
×
833

834
  pInfo->leaderEpSet = mndGetDnodeEpsetById(pMnode, pInfo->leaderSnodeId);
×
835
  if (GOT_SNODE(pInfo->replicaSnodeId)) {
×
836
    pInfo->replicaEpSet = mndGetDnodeEpsetById(pMnode, pInfo->replicaSnodeId);
×
837
  }
838

839
  return TSDB_CODE_SUCCESS;
×
840
}
841

842
int32_t msmBuildTriggerDeployInfo(SMnode* pMnode, SStmStatus* pInfo, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
234,526✔
843
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
844
  int32_t lino = 0;
234,526✔
845
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
846
  SStreamTriggerDeployMsg* pMsg = &pDeploy->msg.trigger;
234,526✔
847
  
848
  pMsg->triggerType = pStream->pCreate->triggerType;
234,526✔
849
  pMsg->igDisorder = pStream->pCreate->igDisorder;
234,526✔
850
  pMsg->fillHistory = pStream->pCreate->fillHistory;
234,526✔
851
  pMsg->fillHistoryFirst = pStream->pCreate->fillHistoryFirst;
234,526✔
852
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
234,526✔
853
  pMsg->igNoDataTrigger = pStream->pCreate->igNoDataTrigger;
234,526✔
854
  pMsg->enableMultiGroupCalc = pStream->pCreate->enableMultiGroupCalc;
234,526✔
855
  pMsg->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags);
234,526✔
856
  pMsg->triggerHasPF = pStream->pCreate->triggerHasPF;
234,526✔
857
  pMsg->isTriggerTblStb = (pStream->pCreate->triggerTblType == TSDB_SUPER_TABLE);
234,526✔
858
  pMsg->precision = pStream->pCreate->triggerPrec;
234,526✔
859
  pMsg->partitionCols = pInfo->pCreate->partitionCols;
234,526✔
860

861
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
234,526✔
862
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
234,526✔
863
  pMsg->addOptions = pStream->pCreate->addOptions;
234,526✔
864
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
234,526✔
865

866
  pMsg->maxDelay = pStream->pCreate->maxDelay;
234,526✔
867
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
234,526✔
868
  pMsg->watermark = pStream->pCreate->watermark;
234,526✔
869
  pMsg->expiredTime = pStream->pCreate->expiredTime;
234,526✔
870
  pMsg->idleTimeoutMs = pStream->pCreate->idleTimeoutMs;
234,526✔
871
  pMsg->trigger = pInfo->pCreate->trigger;
234,526✔
872

873
  pMsg->eventTypes = pStream->pCreate->eventTypes;
234,526✔
874
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
234,526✔
875
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
234,526✔
876
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
234,526✔
877
  pMsg->calcPkSlotId = pStream->pCreate->calcPkSlotId;
234,526✔
878
  pMsg->triPkSlotId = pStream->pCreate->triPkSlotId;
234,526✔
879
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
234,526✔
880
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
234,526✔
881
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
67,475✔
882
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
67,475✔
883
  }
884

885
  SStreamTaskAddr addr;
234,108✔
886
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
234,526✔
887
  if (triggerReaderNum > 0) {
234,526✔
888
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
230,120✔
889
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
230,120✔
890
  }
891
  
892
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
518,428✔
893
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
283,902✔
894
    addr.taskId = pStatus->id.taskId;
283,902✔
895
    addr.nodeId = pStatus->id.nodeId;
283,902✔
896
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
283,902✔
897
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
567,804✔
898
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
283,902✔
899
  }
900

901
  pMsg->leaderSnodeId = pStream->mainSnodeId;
234,526✔
902
  pMsg->streamName = pInfo->streamName;
234,526✔
903
  pMsg->nodelayCreateSubtable = pStream->pCreate->nodelayCreateSubtable;
234,526✔
904

905
  if (0 == pInfo->runnerNum) {
234,526✔
906
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
3,445✔
907
    return code;
3,445✔
908
  }
909

910
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
231,081✔
911

912
_exit:
231,081✔
913

914
  if (code) {
231,081✔
915
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
916
  } else {
917
    mstsDebug("trigger deploy info built, readerNum:%d, runnerNum:%d", (int32_t)taosArrayGetSize(pMsg->readerList), (int32_t)taosArrayGetSize(pMsg->runnerList));
231,081✔
918
  }
919
  
920
  return TSDB_CODE_SUCCESS;
231,081✔
921
}
922

923

924
int32_t msmBuildRunnerDeployInfo(SStmTaskDeploy* pDeploy, SSubplan *plan, SStreamObj* pStream, SStmStatus* pInfo, bool topPlan) {
798,986✔
925
  int32_t code = TSDB_CODE_SUCCESS;
798,986✔
926
  int32_t lino = 0;
798,986✔
927
  int64_t streamId = pStream->pCreate->streamId;
798,986✔
928
  SStreamRunnerDeployMsg* pMsg = &pDeploy->msg.runner;
798,986✔
929
  //TAOS_CHECK_EXIT(qSubPlanToString(plan, &pMsg->pPlan, NULL));
930

931
  pMsg->execReplica = pInfo->runnerReplica;
798,986✔
932
  pMsg->streamName = pInfo->streamName;
798,986✔
933
  //TAOS_CHECK_EXIT(nodesCloneNode((SNode*)plan, (SNode**)&pMsg->pPlan));
934
  pMsg->pPlan = plan;
798,986✔
935
  pMsg->outDBFName = pInfo->pCreate->outDB;
798,986✔
936
  pMsg->outTblName = pInfo->pCreate->outTblName;
798,986✔
937
  pMsg->outTblType = pStream->pCreate->outTblType;
798,986✔
938
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
798,986✔
939
  pMsg->calcNotifyOnly = pStream->pCreate->calcNotifyOnly;
798,986✔
940
  pMsg->topPlan = topPlan;
798,986✔
941
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
798,986✔
942
  pMsg->addOptions = pStream->pCreate->addOptions;
798,986✔
943
  if ((WINDOW_TYPE_INTERVAL == pStream->pCreate->triggerType && pStream->pCreate->trigger.sliding.overlap) ||
798,986✔
944
      (WINDOW_TYPE_COUNT == pStream->pCreate->triggerType &&
785,627✔
945
       pStream->pCreate->trigger.count.sliding < pStream->pCreate->trigger.count.countVal)) {
104,274✔
946
    pMsg->addOptions |= CALC_SLIDING_OVERLAP;
67,131✔
947
  }
948
  pMsg->outCols = pInfo->pCreate->outCols;
798,986✔
949
  pMsg->outTags = pInfo->pCreate->outTags;
798,986✔
950
  pMsg->outStbUid = pStream->pCreate->outStbUid;
798,986✔
951
  pMsg->outStbSversion = pStream->pCreate->outStbSversion;
798,986✔
952
  
953
  pMsg->subTblNameExpr = pInfo->pCreate->subTblNameExpr;
798,986✔
954
  pMsg->tagValueExpr = pInfo->pCreate->tagValueExpr;
798,986✔
955
  pMsg->forceOutCols = pInfo->pCreate->forceOutCols;
798,986✔
956

957
  pMsg->colCids = pInfo->pCreate->colCids;
798,986✔
958
  pMsg->tagCids = pInfo->pCreate->tagCids;
798,986✔
959
_exit:
798,986✔
960

961
  if (code) {
798,986✔
962
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
963
  }
964
  
965
  return code;
798,986✔
966
}
967

968

969
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
536,064✔
970
  int32_t code = TSDB_CODE_SUCCESS;
536,064✔
971
  int32_t lino = 0;
536,064✔
972
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
536,064✔
973
  SListNode* pNode = pList ? listHead(pList) : NULL;
536,064✔
974
  
975
  for (int32_t i = 0; i < taskNum; ++i) {
1,291,353✔
976
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
755,289✔
977
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
755,289✔
978
    if (pNode) {
755,289✔
979
      pNode = TD_DLIST_NODE_NEXT(pNode);
404,375✔
980
    }
981
  }
982
  
983
_exit:
536,064✔
984

985
  if (code) {
536,064✔
986
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
987
  }
988
  
989
  return code;
536,064✔
990
}
991

992

993
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
928,254✔
994
  int32_t code = TSDB_CODE_SUCCESS;
928,254✔
995
  int32_t lino = 0;
928,254✔
996
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
928,254✔
997
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
928,254✔
998
  
999
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,961,766✔
1000
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
1,033,512✔
1001
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
1,033,512✔
1002
  }
1003
  
1004
_exit:
928,254✔
1005

1006
  if (code) {
928,254✔
1007
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1008
  }
1009

1010
  return code;
928,254✔
1011
}
1012

1013
int64_t msmAssignTaskId(void) {
1,788,316✔
1014
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,788,316✔
1015
}
1016

1017
int64_t msmAssignTaskSeriousId(void) {
1,788,316✔
1018
  return taosGetTimestampNs();
1,788,316✔
1019
}
1020

1021

1022
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
1,230,897✔
1023
  int32_t code = TSDB_CODE_SUCCESS;
1,230,897✔
1024
  int32_t lino = 0;
1,230,897✔
1025
  bool     noExists = false;
1,230,897✔
1026
  SStmSnodeStatus* pStatus = NULL;
1,230,897✔
1027

1028
  while (true) {
1029
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
1,242,364✔
1030
    if (NULL == pStatus) {
1,242,364✔
1031
      if (noExists) {
11,467✔
1032
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1033
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1034
      }
1035

1036
      noExists = true;
11,467✔
1037
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
11,467✔
1038
      
1039
      continue;
11,467✔
1040
    }
1041

1042
    *alive = (pStatus->runnerThreadNum >= 0);
1,230,897✔
1043
    break;
1,230,897✔
1044
  }
1045

1046
_exit:
1,230,897✔
1047

1048
  if (code) {
1,230,897✔
1049
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1050
  }
1051

1052
  return code;
1,230,897✔
1053
}
1054

1055
int32_t msmRetrieveStaticSnodeId(SMnode* pMnode, SStreamObj* pStream) {
480,882✔
1056
  int32_t code = TSDB_CODE_SUCCESS;
480,882✔
1057
  int32_t lino = 0;
480,882✔
1058
  bool alive = false;
480,882✔
1059
  int32_t mainSnodeId = atomic_load_32(&pStream->mainSnodeId);
480,882✔
1060
  int32_t snodeId = mainSnodeId;
480,882✔
1061
  int64_t streamId = pStream->pCreate->streamId;
480,882✔
1062
  
1063
  while (true) {
1064
    TAOS_CHECK_EXIT(msmIsSnodeAlive(pMnode, snodeId, streamId, &alive));
480,882✔
1065

1066
    if (alive) {
480,882✔
1067
      return snodeId;
480,882✔
1068
    }
1069
    
1070
    if (snodeId == mainSnodeId) {
×
1071
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
1072
      if (NULL == pSnode) {
×
1073
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1074
        return 0;
×
1075
      }
1076
      
1077
      if (pSnode->replicaId <= 0) {
×
1078
        mstsError("no available snode now, mainSnodeId:%d, replicaId:%d", mainSnodeId, pSnode->replicaId);
×
1079
        mndReleaseSnode(pMnode, pSnode);
×
1080
        return 0;
×
1081
      }
1082

1083
      snodeId = pSnode->replicaId;
×
1084
      mndReleaseSnode(pMnode, pSnode);
×
1085
      
1086
      continue;
×
1087
    }
1088

1089
    mstsError("no available snode now, mainSnodeId:%d, followerSnodeId:%d", mainSnodeId, snodeId);
×
1090
    return 0;
×
1091
  }
1092

1093
_exit:
×
1094

1095
  if (code) {
×
1096
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1097
  }
1098

1099
  return 0;
×
1100
}
1101

1102
int32_t msmAssignRandomSnodeId(SMnode* pMnode, int64_t streamId) {
729,355✔
1103
  int32_t code = TSDB_CODE_SUCCESS;
729,355✔
1104
  int32_t lino = 0;
729,355✔
1105
  int32_t snodeIdx = 0;
729,355✔
1106
  int32_t snodeId = 0;
729,355✔
1107
  void      *pIter = NULL;
729,355✔
1108
  SSnodeObj *pObj = NULL;
729,355✔
1109
  bool alive = false;
729,355✔
1110
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
729,355✔
1111
  if (snodeNum <= 0) {
729,355✔
1112
    mstsInfo("no available snode now, num:%d", snodeNum);
2,840✔
1113
    goto _exit;
2,840✔
1114
  }
1115

1116
  int32_t snodeTarget = taosRand() % snodeNum;
726,515✔
1117

1118
  while (1) {
1119
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
750,015✔
1120
    if (pIter == NULL) {
750,015✔
1121
      if (0 == snodeId) {
×
1122
        mstsError("no alive snode now, snodeNum:%d", snodeNum);
×
1123
        break;
×
1124
      }
1125
      
1126
      snodeId = 0;
×
1127
      continue;
×
1128
    }
1129

1130
    code = msmIsSnodeAlive(pMnode, pObj->id, streamId, &alive);
750,015✔
1131
    if (code) {
750,015✔
1132
      sdbRelease(pMnode->pSdb, pObj);
×
1133
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1134
      pObj = NULL;
×
1135
      TAOS_CHECK_EXIT(code);
×
1136
    }
1137
    
1138
    if (!alive) {
750,015✔
1139
      sdbRelease(pMnode->pSdb, pObj);
×
1140
      continue;
×
1141
    }
1142

1143
    snodeId = pObj->id;
750,015✔
1144
    if (snodeIdx == snodeTarget) {
750,015✔
1145
      sdbRelease(pMnode->pSdb, pObj);
726,515✔
1146
      sdbCancelFetch(pMnode->pSdb, pIter);
726,515✔
1147
      pObj = NULL;
726,515✔
1148
      goto _exit;
726,515✔
1149
    }
1150

1151
    sdbRelease(pMnode->pSdb, pObj);
23,500✔
1152
    snodeIdx++;
23,500✔
1153
  }
1154

1155
_exit:
729,355✔
1156

1157
  if (code) {
729,355✔
1158
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1159
  }
1160

1161
  if (0 == snodeId) {
729,355✔
1162
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,840✔
1163
  }
1164

1165
  return snodeId;
729,355✔
1166
}
1167

1168
int32_t msmAssignTaskSnodeId(SMnode* pMnode, SStreamObj* pStream, bool isStatic) {
972,624✔
1169
  int64_t streamId = pStream->pCreate->streamId;
972,624✔
1170
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
972,624✔
1171
  int32_t snodeId = 0;
972,624✔
1172
  if (snodeNum <= 0) {
972,624✔
1173
    mstsInfo("no available snode now, num:%d", snodeNum);
×
1174
    goto _exit;
×
1175
  }
1176

1177
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
972,624✔
1178

1179
_exit:
972,624✔
1180

1181
  if (0 == snodeId) {
972,624✔
1182
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1183
  }
1184

1185
  return snodeId;
972,624✔
1186
}
1187

1188

1189
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
234,526✔
1190
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
1191
  int32_t lino = 0;
234,526✔
1192
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
1193

1194
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
234,526✔
1195
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
234,526✔
1196

1197
  pInfo->triggerTask->id.taskId = pCtx->triggerTaskId;
234,526✔
1198
  pInfo->triggerTask->id.deployId = 0;
234,526✔
1199
  pInfo->triggerTask->id.seriousId = msmAssignTaskSeriousId();
234,526✔
1200
  pInfo->triggerTask->id.nodeId = pCtx->triggerNodeId;
234,526✔
1201
  pInfo->triggerTask->id.taskIdx = 0;
234,526✔
1202
  pInfo->triggerTask->type = STREAM_TRIGGER_TASK;
234,526✔
1203
  pInfo->triggerTask->lastUpTs = pCtx->currTs;
234,526✔
1204
  pInfo->triggerTask->pStream = pInfo;
234,526✔
1205

1206
  SStmTaskDeploy info = {0};
234,526✔
1207
  info.task.type = pInfo->triggerTask->type;
234,526✔
1208
  info.task.streamId = streamId;
234,526✔
1209
  info.task.taskId =  pInfo->triggerTask->id.taskId;
234,526✔
1210
  info.task.seriousId = pInfo->triggerTask->id.seriousId;
234,526✔
1211
  info.task.nodeId =  pInfo->triggerTask->id.nodeId;
234,526✔
1212
  info.task.taskIdx =  pInfo->triggerTask->id.taskIdx;
234,526✔
1213
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pInfo, &info, pStream));
234,526✔
1214
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
234,526✔
1215
  
1216
  (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
234,526✔
1217

1218
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
234,526✔
1219
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
234,526✔
1220

1221
_exit:
234,526✔
1222

1223
  if (code) {
234,526✔
1224
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1225
  }
1226

1227
  return code;
234,526✔
1228
}
1229

1230
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
348,296✔
1231
  int32_t code = TSDB_CODE_SUCCESS;
348,296✔
1232
  int32_t lino = 0;
348,296✔
1233

1234
  pState->id.taskId = msmAssignTaskId();
348,296✔
1235
  pState->id.deployId = 0;
348,296✔
1236
  pState->id.seriousId = msmAssignTaskSeriousId();
348,296✔
1237
  pState->id.nodeId = nodeId;
348,296✔
1238
  pState->id.taskIdx = 0;
348,296✔
1239
  pState->type = STREAM_READER_TASK;
348,296✔
1240
  pState->flags = STREAM_FLAG_TRIGGER_READER;
348,296✔
1241
  pState->status = STREAM_STATUS_UNDEPLOYED;
348,296✔
1242
  pState->lastUpTs = pCtx->currTs;
348,296✔
1243
  pState->pStream = pInfo;
348,296✔
1244
  
1245
  SStmTaskDeploy info = {0};
348,296✔
1246
  info.task.type = pState->type;
348,296✔
1247
  info.task.streamId = streamId;
348,296✔
1248
  info.task.taskId = pState->id.taskId;
348,296✔
1249
  info.task.flags = pState->flags;
348,296✔
1250
  info.task.seriousId = pState->id.seriousId;
348,296✔
1251
  info.task.nodeId = pState->id.nodeId;
348,296✔
1252
  info.task.taskIdx = pState->id.taskIdx;
348,296✔
1253
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, NULL, pInfo, true));
348,296✔
1254
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
348,296✔
1255

1256
_exit:
348,296✔
1257

1258
  if (code) {
348,296✔
1259
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1260
  }
1261

1262
  return code;
348,296✔
1263
}
1264

1265
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
234,526✔
1266
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
1267
  int32_t lino = 0;
234,526✔
1268
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
1269
  SSdb   *pSdb = pCtx->pMnode->pSdb;
234,526✔
1270
  SStmTaskStatus* pState = NULL;
234,526✔
1271
  SVgObj *pVgroup = NULL;
234,526✔
1272
  SDbObj* pDb = NULL;
234,526✔
1273
  
1274
  switch (pStream->pCreate->triggerTblType) {
234,526✔
1275
    case TSDB_NORMAL_TABLE:
139,337✔
1276
    case TSDB_CHILD_TABLE:
1277
    case TSDB_VIRTUAL_CHILD_TABLE:
1278
    case TSDB_VIRTUAL_NORMAL_TABLE: {
1279
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
139,337✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
139,337✔
1281
      pState = taosArrayGet(pInfo->trigReaders, 0);
139,337✔
1282
      
1283
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
139,337✔
1284
      break;
139,337✔
1285
    }
1286
    case TSDB_SUPER_TABLE: {
90,783✔
1287
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
90,783✔
1288
      if (NULL == pDb) {
90,783✔
1289
        code = terrno;
×
1290
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
1291
        goto _exit;
×
1292
      }
1293

1294
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
90,783✔
1295
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
90,783✔
1296
      
1297
      void *pIter = NULL;
90,783✔
1298
      while (1) {
459,414✔
1299
        SStmTaskDeploy info = {0};
550,197✔
1300
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
550,197✔
1301
        if (pIter == NULL) {
550,197✔
1302
          break;
90,783✔
1303
        }
1304
      
1305
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
459,414✔
1306
          pState = taosArrayReserve(pInfo->trigReaders, 1);
144,565✔
1307

1308
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
144,565✔
1309
          if (code) {
144,565✔
1310
            sdbRelease(pSdb, pVgroup);
×
1311
            sdbCancelFetch(pSdb, pIter);
×
1312
            pVgroup = NULL;
×
1313
            TAOS_CHECK_EXIT(code);
×
1314
          }
1315
        }
1316

1317
        sdbRelease(pSdb, pVgroup);
459,414✔
1318
      }
1319
      break;
90,783✔
1320
    }
1321
    default:
4,406✔
1322
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
4,406✔
1323
      break;
4,406✔
1324
  }
1325

1326
_exit:
234,526✔
1327

1328
  mndReleaseDb(pCtx->pMnode, pDb);
234,526✔
1329

1330
  if (code) {
234,526✔
1331
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1332
  }
1333

1334
  return code;
234,526✔
1335
}
1336

1337
int32_t msmUPAddScanTask(SStmGrpCtx* pCtx, SStreamObj* pStream, char* scanPlan, int32_t vgId, int64_t taskId) {
404,860✔
1338
  int32_t code = TSDB_CODE_SUCCESS;
404,860✔
1339
  int32_t lino = 0;
404,860✔
1340
  SSubplan* pSubplan = NULL;
404,860✔
1341
  int64_t streamId = pStream->pCreate->streamId;
404,860✔
1342
  int64_t key[2] = {streamId, 0};
404,860✔
1343
  SStmTaskSrcAddr addr;
404,423✔
1344
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
404,860✔
1345
  addr.isFromCache = false;
404,860✔
1346
  
1347
  if (MNODE_HANDLE == vgId) {
404,860✔
1348
    mndGetMnodeEpSet(pCtx->pMnode, &addr.epset);
×
1349
  } else if (vgId > MNODE_HANDLE) {
404,860✔
1350
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
404,860✔
1351
  } else {
1352
    mstsError("invalid vgId %d in scanPlan", vgId);
×
1353
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1354
  }
1355
  
1356
  addr.taskId = taskId;
404,860✔
1357
  addr.vgId = vgId;
404,860✔
1358
  addr.groupId = pSubplan->id.groupId;
404,860✔
1359

1360
  key[1] = pSubplan->id.subplanId;
404,860✔
1361

1362
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
404,860✔
1363
  if (NULL == ppRes) {
404,860✔
1364
    SArray* pRes = taosArrayInit(1, sizeof(addr));
384,849✔
1365
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
384,849✔
1366
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
769,698✔
1367
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
384,849✔
1368
  } else {
1369
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
40,022✔
1370
  }
1371

1372
  mstsDebug("calcReader %" PRIx64 " added to toUpdateScan, vgId:%d, groupId:%d, subplanId:%d", taskId, vgId, pSubplan->id.groupId, pSubplan->id.subplanId);
404,860✔
1373
  
1374
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
404,860✔
1375
  
1376
_exit:
404,860✔
1377

1378
  nodesDestroyNode((SNode*)pSubplan);
404,860✔
1379

1380
  if (code) {
404,860✔
1381
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1382
  }
1383

1384
  return code;
404,860✔
1385
}
1386

1387
int32_t msmUPAddCacheTask(SStmGrpCtx* pCtx, SStreamCalcScan* pScan, SStreamObj* pStream) {
98,908✔
1388
  int32_t code = TSDB_CODE_SUCCESS;
98,908✔
1389
  int32_t lino = 0;
98,908✔
1390
  SSubplan* pSubplan = NULL;
98,908✔
1391
  int64_t streamId = pStream->pCreate->streamId;
98,908✔
1392
  int64_t key[2] = {streamId, 0};
98,908✔
1393
  TAOS_CHECK_EXIT(nodesStringToNode(pScan->scanPlan, (SNode**)&pSubplan));
98,908✔
1394

1395
  SStmTaskSrcAddr addr;
98,908✔
1396
  addr.isFromCache = true;
98,908✔
1397
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
98,908✔
1398
  addr.taskId = pCtx->triggerTaskId;
98,908✔
1399
  addr.vgId = pCtx->triggerNodeId;
98,908✔
1400
  addr.groupId = pSubplan->id.groupId;
98,908✔
1401

1402
  key[1] = pSubplan->id.subplanId;
98,908✔
1403
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
98,908✔
1404
  if (NULL == ppRes) {
98,908✔
1405
    SArray* pRes = taosArrayInit(1, sizeof(addr));
98,908✔
1406
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
98,908✔
1407
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
197,816✔
1408
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
98,908✔
1409
  } else {
1410
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1411
  }
1412
  
1413
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
98,908✔
1414
  
1415
_exit:
98,908✔
1416

1417
  nodesDestroyNode((SNode*)pSubplan);
98,908✔
1418
  
1419
  if (code) {
98,908✔
1420
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1421
  }
1422

1423
  return code;
98,908✔
1424
}
1425

1426

1427
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
406,993✔
1428
  int32_t code = TSDB_CODE_SUCCESS;
406,993✔
1429
  int32_t lino = 0;
406,993✔
1430

1431
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
406,993✔
1432

1433
  pState->id.taskId = msmAssignTaskId();
406,993✔
1434
  pState->id.deployId = 0;
406,993✔
1435
  pState->id.seriousId = msmAssignTaskSeriousId();
406,993✔
1436
  pState->id.nodeId = nodeId;
406,993✔
1437
  pState->id.taskIdx = taskIdx;
406,993✔
1438
  pState->type = STREAM_READER_TASK;
406,993✔
1439
  pState->flags = 0;
406,993✔
1440
  pState->status = STREAM_STATUS_UNDEPLOYED;
406,993✔
1441
  pState->lastUpTs = pCtx->currTs;
406,993✔
1442
  pState->pStream = pInfo;
406,993✔
1443
  
1444
  SStmTaskDeploy info = {0};
406,993✔
1445
  info.task.type = pState->type;
406,993✔
1446
  info.task.streamId = streamId;
406,993✔
1447
  info.task.taskId = pState->id.taskId;
406,993✔
1448
  info.task.flags = pState->flags;
406,993✔
1449
  info.task.seriousId = pState->id.seriousId;
406,993✔
1450
  info.task.nodeId = pState->id.nodeId;
406,993✔
1451
  info.task.taskIdx = pState->id.taskIdx;
406,993✔
1452
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, calcScanPlan, pInfo, false));
406,993✔
1453
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
406,993✔
1454

1455
_exit:
406,993✔
1456

1457
  if (code) {
406,993✔
1458
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1459
  }
1460

1461
  return code;
406,993✔
1462
}
1463

1464

1465
static int32_t msmTDAddCalcReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
234,526✔
1466
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
1467
  int32_t lino = 0;
234,526✔
1468
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
234,526✔
1469
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
1470
  SStmTaskStatus* pState = NULL;
234,526✔
1471
  pInfo->calcReaders = tdListNew(sizeof(SStmTaskStatus));
234,526✔
1472
  TSDB_CHECK_NULL(pInfo->calcReaders, code, lino, _exit, terrno);
234,526✔
1473

1474
  
1475
  for (int32_t i = 0; i < calcTasksNum; ++i) {
737,809✔
1476
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
503,283✔
1477
    if (pScan->readFromCache) {
503,283✔
1478
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
98,908✔
1479
      continue;
98,908✔
1480
    }
1481
    
1482
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
404,375✔
1483
    for (int32_t m = 0; m < vgNum; ++m) {
808,750✔
1484
      pState = tdListReserve(pInfo->calcReaders);
404,375✔
1485
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
404,375✔
1486

1487
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
404,375✔
1488
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
404,375✔
1489
    }
1490
  }
1491

1492
_exit:
234,526✔
1493

1494
  if (code) {
234,526✔
1495
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1496
  }
1497

1498
  return code;
234,526✔
1499
}
1500

1501

1502

1503
static int32_t msmUPPrepareReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
485✔
1504
  int32_t code = TSDB_CODE_SUCCESS;
485✔
1505
  int32_t lino = 0;
485✔
1506
  int64_t streamId = pStream->pCreate->streamId;
485✔
1507
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
485✔
1508
  if (calcTasksNum <= 0) {
485✔
1509
    mstsDebug("no calc scan plan, ignore parepare reader tasks, readerNum:%d", (int32_t)MST_LIST_SIZE(pInfo->calcReaders));
×
1510
    return code;    
×
1511
  }
1512
  
1513
  SListNode* pNode = listHead(pInfo->calcReaders);
485✔
1514
  
1515
  for (int32_t i = 0; i < calcTasksNum; ++i) {
970✔
1516
    SStreamCalcScan* pScan = taosArrayGet(pStream->pCreate->calcScanPlanList, i);
485✔
1517
    if (pScan->readFromCache) {
485✔
1518
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
×
1519
      continue;
×
1520
    }
1521
    
1522
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
485✔
1523
    for (int32_t m = 0; m < vgNum; ++m) {
970✔
1524
      SStmTaskStatus* pReader = (SStmTaskStatus*)pNode->data;
485✔
1525
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pReader->id.nodeId, pReader->id.taskId));
485✔
1526
      pNode = TD_DLIST_NODE_NEXT(pNode);
485✔
1527
    }
1528
  }
1529

1530
_exit:
485✔
1531

1532
  if (code) {
485✔
1533
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1534
  }
1535

1536
  return code;
485✔
1537
}
1538

1539
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
234,526✔
1540
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
1541
  int32_t lino = 0;
234,526✔
1542
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
1543
  
1544
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
234,526✔
1545
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
234,526✔
1546

1547
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
234,526✔
1548
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
234,526✔
1549
  
1550
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
234,526✔
1551
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
234,526✔
1552
  
1553
_exit:
234,526✔
1554

1555
  if (code) {
234,526✔
1556
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1557
  }
1558
  
1559
  return code;
234,526✔
1560
}
1561

1562
int32_t msmUpdatePlanSourceAddr(SStreamTask* pTask, int64_t streamId, SSubplan* plan, int64_t clientId, SStmTaskSrcAddr* pSrc, int32_t msgType, int64_t srcSubplanId) {
1,699,298✔
1563
  SDownstreamSourceNode source = {
1,699,298✔
1564
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1565
      .clientId = clientId,
1566
      .taskId = pSrc->taskId,
1,699,298✔
1567
      .sId = 0,
1568
      .execId = 0,
1569
      .fetchMsgType = msgType,
1570
      .localExec = false,
1571
  };
1572

1573
  source.addr.epSet = pSrc->epset;
1,699,298✔
1574
  source.addr.nodeId = pSrc->vgId;
1,699,298✔
1575

1576
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
1,699,298✔
1577
      plan->id.subplanId, pSrc->groupId, srcSubplanId, source.clientId, source.taskId, source.addr.nodeId, TMSG_INFO(source.fetchMsgType));
1578
  
1579
  return qSetSubplanExecutionNode(plan, pSrc->groupId, &source);
1,699,298✔
1580
}
1581

1582
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
60,888✔
1583
  int64_t streamId = pStream->pCreate->streamId;
60,888✔
1584
  int32_t runnerNum = taosArrayGetSize(pRunners);
60,888✔
1585
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
184,521✔
1586
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
184,521✔
1587
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
184,521✔
1588
    if (pPlan->id.subplanId == subplanId) {
184,521✔
1589
      *taskId = pDeploy->task.taskId;
60,888✔
1590
      *ppParent = &pDeploy->task;
60,888✔
1591
      return TSDB_CODE_SUCCESS;
60,888✔
1592
    }
1593
  }
1594

1595
  mstsError("subplanId %d not found in runner list", subplanId);
×
1596

1597
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1598
}
1599

1600
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
798,986✔
1601
  int32_t code = TSDB_CODE_SUCCESS;
798,986✔
1602
  int32_t lino = 0;
798,986✔
1603
  int64_t key[2] = {streamId, -1};
798,986✔
1604
  SNode* pNode = NULL;
798,986✔
1605
  SStreamTask* pTask = &pDeploy->task;
798,986✔
1606
  FOREACH(pNode, pPlan->pChildren) {
2,370,208✔
1607
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,571,222✔
1608
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
60,888✔
1609
      continue;
60,888✔
1610
    }
1611
    
1612
    SValueNode* pVal = (SValueNode*)pNode;
1,510,334✔
1613
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,510,334✔
1614
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1615
      continue;
×
1616
    }
1617

1618
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,510,334✔
1619

1620
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
1,510,334✔
1621
    if (NULL == ppRes) {
1,510,334✔
1622
      msttError("lowest runner subplan ID:%d,%d can't get its child ID:%" PRId64 " addr", pPlan->id.groupId, pPlan->id.subplanId, key[1]);
×
1623
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1624
    }
1625

1626
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,510,334✔
1627
    for (int32_t i = 0; i < childrenNum; ++i) {
3,148,744✔
1628
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,638,410✔
1629
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,638,410✔
1630
    }
1631
  }
1632

1633
_exit:
798,986✔
1634

1635
  if (code) {
798,986✔
1636
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1637
  }
1638

1639
  return code;
798,986✔
1640
}
1641

1642
int32_t msmUpdateRunnerPlan(SStmGrpCtx* pCtx, SArray* pRunners, int32_t beginIdx, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
798,986✔
1643
  int32_t code = TSDB_CODE_SUCCESS;
798,986✔
1644
  int32_t lino = 0;
798,986✔
1645
  SSubplan* pPlan = pDeploy->msg.runner.pPlan;
798,986✔
1646
  SStreamTask* pTask = &pDeploy->task;
798,986✔
1647
  SStreamTask* parentTask = NULL;
798,986✔
1648
  int64_t streamId = pStream->pCreate->streamId;
798,986✔
1649

1650
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
798,986✔
1651

1652
  SNode* pTmp = NULL;
798,986✔
1653
  WHERE_EACH(pTmp, pPlan->pChildren) {
2,370,208✔
1654
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,571,222✔
1655
      ERASE_NODE(pPlan->pChildren);
1,510,334✔
1656
      continue;
1,510,334✔
1657
    }
1658
    WHERE_NEXT;
60,888✔
1659
  }
1660
  nodesClearList(pPlan->pChildren);
798,986✔
1661
  pPlan->pChildren = NULL;
798,986✔
1662

1663
  if (NULL == pPlan->pParents) {
798,986✔
1664
    goto _exit;
738,098✔
1665
  }
1666

1667
  SNode* pNode = NULL;
60,888✔
1668
  int64_t parentTaskId = 0;
60,888✔
1669
  SStmTaskSrcAddr addr = {0};
60,888✔
1670
  addr.taskId = pDeploy->task.taskId;
60,888✔
1671
  addr.vgId = pDeploy->task.nodeId;
60,888✔
1672
  addr.groupId = pPlan->id.groupId;
60,888✔
1673
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
60,888✔
1674
  FOREACH(pNode, pPlan->pParents) {
121,776✔
1675
    SSubplan* pSubplan = (SSubplan*)pNode;
60,888✔
1676
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
60,888✔
1677
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
60,888✔
1678
  }
1679
  
1680
_exit:
797,732✔
1681

1682
  if (code) {
798,986✔
1683
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1684
  }
1685

1686
  return code;
798,986✔
1687
}
1688

1689
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
738,098✔
1690
  int32_t code = TSDB_CODE_SUCCESS;
738,098✔
1691
  int32_t lino = 0;
738,098✔
1692
  int64_t streamId = pStream->pCreate->streamId;
738,098✔
1693
  int32_t runnerNum = taosArrayGetSize(pRunners);
738,098✔
1694
  
1695
  for (int32_t i = 0; i < runnerNum; ++i) {
1,537,084✔
1696
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
798,986✔
1697
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
798,986✔
1698
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
798,986✔
1699

1700
    SStreamTask* pTask = &pDeploy->task;
798,986✔
1701
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
798,986✔
1702
  }
1703

1704
_exit:
738,098✔
1705

1706
  if (code) {
738,098✔
1707
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1708
  }
1709

1710
  return code;
738,098✔
1711
}
1712

1713
static int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, int32_t dagIdx, SStmStatus* pInfo, SStreamObj* pStream, SQueryPlan** pRoot, SNodeList** subEP) {
245,871✔
1714
  int32_t code = 0;
245,871✔
1715
  int32_t lino = 0;
245,871✔
1716
  int64_t streamId = pStream->pCreate->streamId;
245,871✔
1717
  SArray* deployTaskList = NULL;
245,871✔
1718
  SArray* deployList = NULL;
245,871✔
1719
  int32_t deployNodeId = 0;
245,871✔
1720
  SStmTaskStatus* pState = NULL;
245,871✔
1721
  int32_t taskIdx = 0;
245,871✔
1722
  SNodeListNode *plans = NULL;
245,871✔
1723
  SQueryPlan    *pDag = NULL;
245,871✔
1724
  int32_t        taskNum = 0;
245,871✔
1725
  int32_t        totalTaskNum = 0;
245,871✔
1726
  bool           subQ = false;
245,871✔
1727

1728
  if (dagIdx >= 0) {
245,871✔
1729
    subQ = true;
14,790✔
1730
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
14,790✔
1731
  } else {
1732
    pDag = *pRoot;
231,081✔
1733
  }
1734

1735
  if (pDag->numOfSubplans <= 0) {
245,871✔
1736
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1737
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1738
  }
1739

1740
  if ((*pRoot)->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
245,871✔
1741
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1742
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1743
  }
1744

1745
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
245,871✔
1746
  if (levelNum <= 0) {
245,871✔
1747
    mstsError("invalid level num:%d", levelNum);
×
1748
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1749
  }
1750

1751
  int32_t        lowestLevelIdx = levelNum - 1;
245,871✔
1752
  
1753
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
245,871✔
1754
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
245,871✔
1755
    mstsError("invalid level plan, level:0, planNodeType:%d", nodeType(plans));
×
1756
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1757
  }
1758
  
1759
  taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
245,871✔
1760
  if (taskNum != 1) {
245,871✔
1761
    mstsError("invalid level plan number:%d, level:0", taskNum);
×
1762
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1763
  }
1764

1765
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
245,871✔
1766
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
245,871✔
1767
  
1768
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
983,484✔
1769
    totalTaskNum = 0;
737,613✔
1770

1771
    deployList = pInfo->runners[deployId];
737,613✔
1772
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
737,613✔
1773
    if (!GOT_SNODE(deployNodeId)) {
737,613✔
1774
      TAOS_CHECK_EXIT(terrno);
×
1775
    }
1776

1777
    taskIdx = 0;
737,613✔
1778

1779
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,497,501✔
1780
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
759,888✔
1781
      if (NULL == plans) {
759,888✔
1782
        mstsError("empty level plan, level:%d", i);
×
1783
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1784
      }
1785

1786
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
759,888✔
1787
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1788
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1789
      }
1790

1791
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
759,888✔
1792
      if (taskNum <= 0) {
759,888✔
1793
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1794
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1795
      }
1796

1797
      totalTaskNum += taskNum;
759,888✔
1798
      if (totalTaskNum > pDag->numOfSubplans) {
759,888✔
1799
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1800
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1801
      }
1802

1803
      for (int32_t n = 0; n < taskNum; ++n) {
1,558,389✔
1804
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
798,501✔
1805
        pState = taosArrayReserve(deployList, 1);
798,501✔
1806

1807
        pState->id.taskId = msmAssignTaskId();
798,501✔
1808
        pState->id.deployId = deployId;
798,501✔
1809
        pState->id.seriousId = msmAssignTaskSeriousId();
798,501✔
1810
        pState->id.nodeId = deployNodeId;
798,501✔
1811
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
798,501✔
1812
        pState->type = STREAM_RUNNER_TASK;
798,501✔
1813
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
798,501✔
1814
        pState->status = STREAM_STATUS_UNDEPLOYED;
798,501✔
1815
        pState->lastUpTs = pCtx->currTs;
798,501✔
1816
        pState->pStream = pInfo;
798,501✔
1817

1818
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
798,501✔
1819
        pDeploy->task.type = pState->type;
798,501✔
1820
        pDeploy->task.streamId = streamId;
798,501✔
1821
        pDeploy->task.taskId = pState->id.taskId;
798,501✔
1822
        pDeploy->task.flags = pState->flags;
798,501✔
1823
        pDeploy->task.seriousId = pState->id.seriousId;
798,501✔
1824
        pDeploy->task.deployId = pState->id.deployId;
798,501✔
1825
        pDeploy->task.nodeId = pState->id.nodeId;
798,501✔
1826
        pDeploy->task.taskIdx = pState->id.taskIdx;
798,501✔
1827
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i && !subQ));
798,501✔
1828

1829
        SStreamTask* pTask = &pDeploy->task;
798,501✔
1830
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
798,501✔
1831
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1832

1833
        if (subQ) {
798,501✔
1834
          if (i == 0) {
57,114✔
1835
            SStmTaskSrcAddr addr = {0};
44,370✔
1836
            SDownstreamSourceNode* pSource = NULL;
44,370✔
1837

1838
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
44,370✔
1839

1840
            addr.taskId = pDeploy->task.taskId;
44,370✔
1841
            addr.vgId = pDeploy->task.nodeId;
44,370✔
1842
            addr.groupId = plan->id.groupId;
44,370✔
1843
            addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
44,370✔
1844

1845
            pSource->addr.epSet = addr.epset;
44,370✔
1846
            pSource->addr.nodeId = addr.vgId;
44,370✔
1847

1848
            pSource->clientId = streamId;
44,370✔
1849
            pSource->taskId = pDeploy->task.taskId;
44,370✔
1850
            pSource->sId = 0;
44,370✔
1851
            pSource->execId = 0;
44,370✔
1852
            pSource->fetchMsgType = TDMT_STREAM_FETCH_FROM_RUNNER;
44,370✔
1853
            pSource->localExec = false;
44,370✔
1854

1855
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
44,370✔
1856
          }
1857
        } else {
1858
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
741,387✔
1859
        }
1860
      }
1861

1862
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
759,888✔
1863
    }
1864

1865
    if (totalTaskNum != pDag->numOfSubplans) {
737,613✔
1866
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1867
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1868
    }
1869

1870
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
737,613✔
1871

1872
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
737,613✔
1873

1874
    nodesDestroyNode((SNode *)(*pRoot));
737,613✔
1875
    *pRoot = NULL;
737,613✔
1876
    
1877
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
737,613✔
1878
    if (subQ) {
737,613✔
1879
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
44,370✔
1880
    } else {
1881
      pDag = *pRoot;
693,243✔
1882
    }
1883

1884
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
737,613✔
1885
  }
1886

1887
  if (!subQ) {
245,871✔
1888
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
924,324✔
1889
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
693,243✔
1890
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
693,243✔
1891
    }
1892
  }
1893
  
1894
  pInfo->runnerNum += totalTaskNum;
245,871✔
1895
  
1896
_exit:
245,871✔
1897

1898
  if (code) {
245,871✔
1899
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1900
  }
1901

1902
  taosArrayDestroy(deployTaskList);
245,871✔
1903

1904
  return code;
245,871✔
1905
}
1906

1907
int32_t msmReBuildRunnerTasks(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream, SStmTaskAction* pAction) {
485✔
1908
  int32_t code = 0;
485✔
1909
  int32_t lino = 0;
485✔
1910
  int64_t streamId = pStream->pCreate->streamId;
485✔
1911
  int32_t newNodeId = 0;
485✔
1912
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
485✔
1913
  int32_t        lowestLevelIdx = levelNum - 1;
485✔
1914
  SNodeListNode *plans = NULL;
485✔
1915
  int32_t        taskNum = 0;
485✔
1916
  int32_t        totalTaskNum = 0;
485✔
1917
  int32_t        deployId = 0;
485✔
1918
  SStmTaskStatus* pRunner = NULL;
485✔
1919
  SStmTaskStatus* pStartRunner = NULL;
485✔
1920
  int32_t taskIdx = 0;
485✔
1921
  SArray* deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
485✔
1922
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
485✔
1923

1924
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
970✔
1925
    deployId = pAction->deployId[r];
485✔
1926

1927
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
485✔
1928

1929
    pStartRunner = pRunner;
485✔
1930
    totalTaskNum = 0;
485✔
1931

1932
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
485✔
1933
    if (!GOT_SNODE(newNodeId)) {
485✔
1934
      TAOS_CHECK_EXIT(terrno);
×
1935
    }
1936

1937
    taskIdx = 0;
485✔
1938
    
1939
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
970✔
1940
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
485✔
1941
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
485✔
1942
      totalTaskNum += taskNum;
485✔
1943

1944
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
485✔
1945
      
1946
      for (int32_t n = 0; n < taskNum; ++n) {
970✔
1947
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
485✔
1948

1949
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
485✔
1950
        if (pRunner->id.taskIdx != newTaskIdx) {
485✔
1951
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1952
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1953
        }
1954

1955
        pRunner->id.nodeId = newNodeId;
485✔
1956

1957
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
485✔
1958
        pDeploy->task.type = pRunner->type;
485✔
1959
        pDeploy->task.streamId = streamId;
485✔
1960
        pDeploy->task.taskId = pRunner->id.taskId;
485✔
1961
        pDeploy->task.flags = pRunner->flags;
485✔
1962
        pDeploy->task.seriousId = pRunner->id.seriousId;
485✔
1963
        pDeploy->task.nodeId = pRunner->id.nodeId;
485✔
1964
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
485✔
1965
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
485✔
1966

1967
        pRunner++;
485✔
1968
      }
1969

1970
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
485✔
1971
    }
1972

1973
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
485✔
1974

1975
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
485✔
1976

1977
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
485✔
1978

1979
    nodesDestroyNode((SNode *)pDag);
485✔
1980
    pDag = NULL;
485✔
1981

1982
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
485✔
1983
  }
1984

1985
_exit:
485✔
1986

1987
  if (code) {
485✔
1988
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1989
  }
1990

1991
  nodesDestroyNode((SNode *)pDag);
485✔
1992
  taosArrayDestroy(deployTaskList);
485✔
1993

1994
  return code;
485✔
1995
}
1996

1997

1998
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
223,837✔
1999
  int32_t code = TSDB_CODE_SUCCESS;
223,837✔
2000
  int32_t lino = 0;
223,837✔
2001
  //STREAMTODO 
2002
  
2003
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
223,837✔
2004
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
223,837✔
2005

2006
_exit:
223,837✔
2007

2008
  if (code) {
223,837✔
2009
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2010
  }
2011

2012
  return code;
223,837✔
2013
}
2014

2015

2016
static int32_t msmUpdateCalcReaderTasks(SStreamObj* pStream, SNodeList* pSubEP) {
231,081✔
2017
  int32_t   code = TSDB_CODE_SUCCESS;
231,081✔
2018
  int32_t   lino = 0;
231,081✔
2019
  int64_t   streamId = pStream->pCreate->streamId;
231,081✔
2020
  void*     pIter = NULL;
231,081✔
2021
  SSubplan* pSubplan = NULL;
231,081✔
2022
  SStmVgTasksToDeploy* pVg = NULL;
231,081✔
2023

2024
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
890,492✔
2025
    pVg = (SStmVgTasksToDeploy*)pIter;
659,411✔
2026
    (void)mstWaitLock(&pVg->lock, false);
659,411✔
2027

2028
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
659,411✔
2029
    if (atomic_load_32(&pVg->deployed) == taskNum) {
659,411✔
2030
      taosWUnLockLatch(&pVg->lock);
2,430✔
2031
      pVg = NULL;
2,430✔
2032
      continue;
2,430✔
2033
    }
2034

2035
    for (int32_t i = 0; i < taskNum; ++i) {
3,085,519✔
2036
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
2,428,538✔
2037
      if (pExt->deployed || pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
2,428,538✔
2038
        continue;
1,761,095✔
2039
      }
2040

2041
      if (!pExt->deploy.msg.reader.triggerReader) {
667,443✔
2042
        SStreamReaderDeployFromCalc* pCalcReaderDeploy = &pExt->deploy.msg.reader.msg.calc;
390,735✔
2043
        TAOS_CHECK_EXIT(nodesStringToNode(pCalcReaderDeploy->calcScanPlan, (SNode**)&pSubplan));
390,735✔
2044
        TAOS_CHECK_EXIT(nodesCloneList(pSubEP, &pSubplan->pSubQ));
390,735✔
2045
        
2046
        // Free old calcScanPlan before nodesNodeToString overwrites the pointer
2047
        if (pCalcReaderDeploy->freeScanPlan) {
390,735✔
2048
          taosMemoryFreeClear(pCalcReaderDeploy->calcScanPlan);
390,735✔
2049
        }
2050
        TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pSubplan, false, (char**)&pCalcReaderDeploy->calcScanPlan, NULL));
390,735✔
2051
        pCalcReaderDeploy->freeScanPlan = true;
390,735✔
2052
        nodesDestroyNode((SNode *)pSubplan);
390,735✔
2053
        pSubplan = NULL;
390,735✔
2054
      }
2055
    }
2056

2057
    taosWUnLockLatch(&pVg->lock);
656,981✔
2058
    pVg = NULL;
656,981✔
2059
  }
2060

2061
_exit:
231,081✔
2062
  if (code) {
231,081✔
2063
    if (pVg) taosWUnLockLatch(&pVg->lock);
×
2064
    if (pIter) taosHashCancelIterate(mStreamMgmt.toDeployVgMap, pIter);
×
2065
    nodesDestroyNode((SNode*)pSubplan);
×
2066
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2067
  }
2068

2069
  return code;
231,081✔
2070
}
2071

2072
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
234,526✔
2073
  if (NULL == pStream->pCreate->calcPlan) {
234,526✔
2074
    return TSDB_CODE_SUCCESS;
3,445✔
2075
  }
2076

2077
  int32_t code = TSDB_CODE_SUCCESS;
231,081✔
2078
  int32_t lino = 0;
231,081✔
2079
  int64_t streamId = pStream->pCreate->streamId;
231,081✔
2080
  SQueryPlan* pPlan = NULL;
231,081✔
2081
  SNodeList*  pSubEP = NULL;
231,081✔
2082
  SNode*      pNode = NULL;
231,081✔
2083
  int32_t     subQueryPlans = 0;
231,081✔
2084

2085
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
231,081✔
2086

2087
  FOREACH(pNode, pPlan->pChildren) {
245,871✔
2088
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
14,790✔
2089

2090
    subQueryPlans += calcSubQPlan->numOfSubplans;
14,790✔
2091
  }
2092

2093
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
924,324✔
2094
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans + subQueryPlans, sizeof(SStmTaskStatus));
693,243✔
2095
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
693,243✔
2096
  }
2097

2098
  for (int32_t i = 0; i < LIST_LENGTH(pPlan->pChildren); ++i) {
245,871✔
2099
    code = msmBuildRunnerTasksImpl(pCtx, i, pInfo, pStream, &pPlan, &pSubEP);
14,790✔
2100
    TAOS_CHECK_EXIT(code);
14,790✔
2101
  }
2102

2103
  code = msmBuildRunnerTasksImpl(pCtx, -1, pInfo, pStream, &pPlan, &pSubEP);
231,081✔
2104
  TAOS_CHECK_EXIT(code);
231,081✔
2105

2106
  taosHashClear(mStreamMgmt.toUpdateScanMap);
231,081✔
2107
  mStreamMgmt.toUpdateScanNum = 0;
231,081✔
2108

2109
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
231,081✔
2110

2111
_exit:
231,081✔
2112
  nodesDestroyNode((SNode *)pPlan);
231,081✔
2113
  if (pSubEP) {
231,081✔
2114
    nodesDestroyList(pSubEP);
13,101✔
2115
  }
2116

2117
  if (code) {
231,081✔
2118
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2119
  }
2120

2121
  return code;
231,081✔
2122
}
2123

2124
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
234,526✔
2125
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
2126
  int32_t lino = 0;
234,526✔
2127
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
2128

2129
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
234,526✔
2130

2131
  pCtx->triggerTaskId = msmAssignTaskId();
234,526✔
2132
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
234,526✔
2133
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
234,526✔
2134
    TAOS_CHECK_EXIT(terrno);
×
2135
  }
2136

2137
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
234,526✔
2138
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
234,526✔
2139
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
234,526✔
2140
  
2141
_exit:
234,526✔
2142

2143
  if (code) {
234,526✔
2144
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2145
  }
2146

2147
  return code;
234,526✔
2148
}
2149

2150
static int32_t msmInitTrigReaderList(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
×
2151
  int32_t code = TSDB_CODE_SUCCESS;
×
2152
  int32_t lino = 0;
×
2153
  int64_t streamId = pStream->pCreate->streamId;
×
2154
  SSdb   *pSdb = pCtx->pMnode->pSdb;
×
2155
  SStmTaskStatus* pState = NULL;
×
2156
  SDbObj* pDb = NULL;
×
2157
  
2158
  switch (pStream->pCreate->triggerTblType) {
×
2159
    case TSDB_NORMAL_TABLE:
×
2160
    case TSDB_CHILD_TABLE:
2161
    case TSDB_VIRTUAL_CHILD_TABLE:
2162
    case TSDB_VIRTUAL_NORMAL_TABLE: {
2163
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
×
2164
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2165
      pInfo->trigReaderNum = 1;
×
2166
      break;
×
2167
    }
2168
    case TSDB_SUPER_TABLE: {
×
2169
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
×
2170
      if (NULL == pDb) {
×
2171
        code = terrno;
×
2172
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
2173
        goto _exit;
×
2174
      }
2175

2176
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
2177
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2178
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2179
      mndReleaseDb(pCtx->pMnode, pDb);
×
2180
      pDb = NULL;
×
2181
      break;
×
2182
    }
2183
    default:
×
2184
      pInfo->trigReaderNum = 0;
×
2185
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2186
      break;
×
2187
  }
2188

2189
_exit:
×
2190

2191
  if (code) {
×
2192
    mndReleaseDb(pCtx->pMnode, pDb);
×
2193
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2194
  }
2195

2196
  return code;
×
2197
}
2198

2199

2200
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
227,282✔
2201
  int32_t code = TSDB_CODE_SUCCESS;
227,282✔
2202
  int32_t lino = 0;
227,282✔
2203
  int64_t streamId = pStream->pCreate->streamId;
227,282✔
2204

2205
  pStatus->lastActionTs = INT64_MIN;
227,282✔
2206

2207
  if (NULL == pStatus->streamName) {
227,282✔
2208
    pStatus->streamName = taosStrdup(pStream->name);
227,282✔
2209
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
227,282✔
2210
  }
2211

2212
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
227,282✔
2213
  
2214
  if (pStream->pCreate->numOfCalcSubplan > 0) {
227,282✔
2215
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
223,837✔
2216
    
2217
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
223,837✔
2218
  }
2219

2220
  if (initList) {
227,282✔
2221
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2222

2223
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
2224
    if (subPlanNum > 0) {
×
2225
      pStatus->calcReaderNum = subPlanNum;
×
2226
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2227
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2228
    }
2229

2230
    if (pStatus->runnerNum > 0) {
×
2231
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2232
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2233
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2234
      }
2235
    }
2236
  }
2237
  
2238
_exit:
227,282✔
2239

2240
  if (code) {
227,282✔
2241
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2242
  }
2243

2244
  return code;
227,282✔
2245
}
2246

2247
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
234,526✔
2248
  int32_t code = TSDB_CODE_SUCCESS;
234,526✔
2249
  int32_t lino = 0;
234,526✔
2250
  int64_t streamId = pStream->pCreate->streamId;
234,526✔
2251
  SStmStatus info = {0};
234,526✔
2252

2253
  if (NULL == pStatus) {
234,526✔
2254
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
227,282✔
2255

2256
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
227,282✔
2257

2258
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
227,282✔
2259
  }
2260
  
2261
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
234,526✔
2262

2263
  mstLogSStmStatus("stream deployed", streamId, pStatus);
234,526✔
2264

2265
_exit:
234,526✔
2266

2267
  if (code) {
234,526✔
2268
    if (NULL != pStatus) {
×
2269
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2270
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2271
    }
2272
    
2273
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2274
  }
2275

2276
  return code;
234,526✔
2277
}
2278

2279

2280
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
8,323✔
2281
  int32_t code = TSDB_CODE_SUCCESS;
8,323✔
2282
  void* pIter = NULL;
8,323✔
2283

2284
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
16,442✔
2285
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
8,119✔
2286
    (void)mstWaitLock(&pVg->lock, false);
8,119✔
2287

2288
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
8,119✔
2289
    if (atomic_load_32(&pVg->deployed) == taskNum) {
8,119✔
2290
      taosWUnLockLatch(&pVg->lock);
×
2291
      continue;
×
2292
    }
2293

2294
    for (int32_t i = 0; i < taskNum; ++i) {
34,321✔
2295
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
26,202✔
2296
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
26,202✔
2297
        continue;
26,202✔
2298
      }
2299

2300
      mstDestroySStmTaskToDeployExt(pExt);
×
2301
      pExt->deployed = true;
×
2302
    }
2303
    
2304
    taosWUnLockLatch(&pVg->lock);
8,119✔
2305
  }
2306

2307
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
16,531✔
2308
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
8,208✔
2309
    (void)mstWaitLock(&pSnode->lock, false);
8,208✔
2310

2311
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
8,208✔
2312
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
8,208✔
2313
      for (int32_t i = 0; i < taskNum; ++i) {
21,468✔
2314
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
14,674✔
2315
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
14,674✔
2316
          continue;
14,270✔
2317
        }
2318
        
2319
        mstDestroySStmTaskToDeployExt(pExt);
404✔
2320
        pExt->deployed = true;
404✔
2321
      }
2322
    }
2323

2324
    taskNum = taosArrayGetSize(pSnode->runnerList);
8,208✔
2325
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
8,208✔
2326
      for (int32_t i = 0; i < taskNum; ++i) {
50,614✔
2327
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
42,406✔
2328
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
42,406✔
2329
          continue;
41,396✔
2330
        }
2331
        
2332
        mstDestroySStmTaskToDeployExt(pExt);
1,010✔
2333
        pExt->deployed = true;
1,010✔
2334
      }
2335
    }
2336

2337
    taosWUnLockLatch(&pSnode->lock);
8,208✔
2338
  }
2339

2340
  
2341
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
23,918✔
2342
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
15,595✔
2343
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
15,595✔
2344
    if (TSDB_CODE_SUCCESS == code) {
15,595✔
2345
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
9,333✔
2346
    }
2347
  }
2348

2349
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
37,690✔
2350
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
29,367✔
2351
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
29,367✔
2352
    if (TSDB_CODE_SUCCESS == code) {
29,367✔
2353
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
12,893✔
2354
    }
2355
  }
2356

2357
  size_t keyLen = 0;
8,323✔
2358
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
597,863✔
2359
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
589,540✔
2360
    if (*pStreamId == streamId) {
589,540✔
2361
      int64_t taskId = *(pStreamId + 1);
49,863✔
2362
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
49,863✔
2363
      if (code) {
49,863✔
2364
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2365
      } else {
2366
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
49,863✔
2367
      }
2368
    }
2369
  }
2370

2371
  if (fromStreamMap) {
8,323✔
2372
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
473✔
2373
    if (code) {
473✔
2374
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2375
    } else {
2376
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
473✔
2377
    }
2378
  }
2379
  
2380
  return code;
8,323✔
2381
}
2382

2383
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
7,850✔
2384
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
7,850✔
2385
  
2386
  (void)msmSTRemoveStream(streamId, false);  
7,850✔
2387

2388
  mstResetSStmStatus(pStatus);
7,850✔
2389

2390
  pStatus->deployTimes++;
7,850✔
2391
}
7,850✔
2392

2393
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
236,038✔
2394
  int32_t code = TSDB_CODE_SUCCESS;
236,038✔
2395
  int32_t lino = 0;
236,038✔
2396
  int64_t streamId = pAction->streamId;
236,038✔
2397
  char* streamName = pAction->streamName;
236,038✔
2398
  SStreamObj* pStream = NULL;
236,038✔
2399
  int8_t stopped = 0;
236,038✔
2400

2401
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
236,038✔
2402
  if (pStatus) {
236,038✔
2403
    stopped = atomic_load_8(&pStatus->stopped);
7,850✔
2404
    if (0 == stopped) {
7,850✔
2405
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
606✔
2406
      msmResetStreamForRedeploy(streamId, pStatus);
606✔
2407
    } else {
2408
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
7,244✔
2409
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2410
        return code;
×
2411
      }
2412
      
2413
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
7,244✔
2414
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
7,244✔
2415
        msmResetStreamForRedeploy(streamId, pStatus);
7,244✔
2416
      }
2417
    }
2418
  }
2419

2420
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
236,038✔
2421
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
236,038✔
2422
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
906✔
2423
    return TSDB_CODE_SUCCESS;
906✔
2424
  }
2425

2426
  TAOS_CHECK_EXIT(code);
235,132✔
2427

2428
  if (pStatus && pStream->pCreate->streamId != streamId) {
235,132✔
2429
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2430
    atomic_store_8(&pStatus->stopped, 2);
×
2431
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2432
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2433
  }
2434

2435
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
235,132✔
2436
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
235,132✔
2437
  if (userStopped || userDropped) {
235,132✔
2438
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
606✔
2439
    goto _exit;
606✔
2440
  }
2441
  
2442
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
234,526✔
2443

2444
_exit:
235,132✔
2445

2446
  mndReleaseStream(pCtx->pMnode, pStream);
235,132✔
2447

2448
  if (code) {
235,132✔
2449
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2450
  }
2451

2452
  return code;
235,132✔
2453
}
2454

2455
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
1,685✔
2456
  int32_t code = TSDB_CODE_SUCCESS;
1,685✔
2457
  int32_t lino = 0;
1,685✔
2458
  int64_t streamId = pAction->streamId;
1,685✔
2459
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
1,685✔
2460
  if (NULL == ppTask) {
1,685✔
2461
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
×
2462
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2463
  }
2464
  
2465
  SStmTaskDeploy info = {0};
1,685✔
2466
  info.task.type = pAction->type;
1,685✔
2467
  info.task.streamId = pAction->streamId;
1,685✔
2468
  info.task.taskId = pAction->id.taskId;
1,685✔
2469
  info.task.seriousId = (*ppTask)->id.seriousId;
1,685✔
2470
  info.task.nodeId = pAction->id.nodeId;
1,685✔
2471
  info.task.taskIdx = pAction->id.taskIdx;
1,685✔
2472
  
2473
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
1,685✔
2474
  SStreamCalcScan* scanPlan = NULL;
1,685✔
2475
  if (!isTriggerReader) {
1,685✔
2476
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
856✔
2477
    if (NULL == scanPlan) {
856✔
2478
      mstsError("fail to get TASK:%" PRId64 " scanPlan, taskIdx:%d, scanPlanNum:%zu", 
×
2479
          pAction->id.taskId, pAction->id.taskIdx, taosArrayGetSize(pStatus->pCreate->calcScanPlanList));
2480
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2481
    }
2482
  }
2483
  
2484
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, scanPlan ? scanPlan->scanPlan : NULL, pStatus, isTriggerReader));
1,685✔
2485
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
1,685✔
2486

2487
_exit:
1,685✔
2488

2489
  if (code) {
1,685✔
2490
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2491
  }
2492

2493
  return code;
1,685✔
2494
}
2495

2496
/*
2497
static int32_t msmReLaunchTriggerTask(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2498
  int32_t code = TSDB_CODE_SUCCESS;
2499
  int32_t lino = 0;
2500
  int64_t streamId = pAction->streamId;
2501
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2502
  if (NULL == ppTask) {
2503
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
2504
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
2505
  }
2506
  
2507
  (*ppTask)->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2508
  if (!GOT_SNODE((*ppTask)->id.nodeId)) {
2509
    mstsError("no avaible snode for deploying trigger task, seriousId: %" PRId64, (*ppTask)->id.seriousId);
2510
    return TSDB_CODE_SUCCESS;
2511
  }
2512
  
2513
  SStmTaskDeploy info = {0};
2514
  info.task.type = pAction->type;
2515
  info.task.streamId = streamId;
2516
  info.task.taskId = pAction->id.taskId;
2517
  info.task.seriousId = (*ppTask)->id.seriousId;
2518
  info.task.nodeId = (*ppTask)->id.nodeId;
2519
  info.task.taskIdx = pAction->id.taskIdx;
2520
  
2521
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2522
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2523
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, *ppTask, 1, -1));
2524
  
2525
  atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2526

2527
_exit:
2528

2529
  if (code) {
2530
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2531
  }
2532

2533
  return code;
2534
}
2535
*/
2536

2537
static int32_t msmReLaunchRunnerDeploy(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
485✔
2538
  int32_t code = TSDB_CODE_SUCCESS;
485✔
2539
  int32_t lino = 0;
485✔
2540
  int64_t streamId = pAction->streamId;
485✔
2541
  
2542
/*
2543
  if (pAction->triggerStatus) {
2544
    pCtx->triggerTaskId = pAction->triggerStatus->id.taskId;
2545
    pAction->triggerStatus->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2546
    if (!GOT_SNODE(pAction->triggerStatus->id.nodeId)) {
2547
      mstsError("no avaible snode for deploying trigger task, seriousId:%" PRId64, pAction->triggerStatus->id.seriousId);
2548
      return TSDB_CODE_SUCCESS;
2549
    }
2550
  
2551
    pCtx->triggerNodeId = pAction->triggerStatus->id.nodeId;
2552
  } else {
2553
*/
2554
  pCtx->triggerTaskId = pStatus->triggerTask->id.taskId;
485✔
2555
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
485✔
2556
//  }
2557
  
2558
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
485✔
2559
  
2560
  SQueryPlan* pPlan = NULL;
485✔
2561
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
485✔
2562
  
2563
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
485✔
2564
  
2565
  taosHashClear(mStreamMgmt.toUpdateScanMap);
485✔
2566
  mStreamMgmt.toUpdateScanNum = 0;
485✔
2567
  
2568
/*
2569
  if (pAction->triggerStatus) {
2570
    SStmTaskDeploy info = {0};
2571
    info.task.type = STREAM_TRIGGER_TASK;
2572
    info.task.streamId = streamId;
2573
    info.task.taskId = pCtx->triggerTaskId;
2574
    info.task.seriousId = pAction->triggerStatus->id.seriousId;
2575
    info.task.nodeId = pCtx->triggerNodeId;
2576
    info.task.taskIdx = 0;
2577
  
2578
    TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2579
    TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2580
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pAction->triggerStatus, 1, -1));
2581
    
2582
    atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2583
  }
2584
*/
2585

2586
_exit:
485✔
2587

2588
  if (code) {
485✔
2589
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2590
  }
2591

2592
  return code;
485✔
2593
}
2594

2595

2596
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
2,170✔
2597
  int32_t code = TSDB_CODE_SUCCESS;
2,170✔
2598
  int32_t lino = 0;
2,170✔
2599
  int64_t streamId = pAction->streamId;
2,170✔
2600
  int64_t taskId = pAction->id.taskId;
2,170✔
2601
  SStreamObj* pStream = NULL;
2,170✔
2602

2603
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
2,170✔
2604

2605
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
2,170✔
2606
  if (NULL == pStatus) {
2,170✔
2607
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2608
    return TSDB_CODE_SUCCESS;
×
2609
  }
2610

2611
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,170✔
2612
  if (stopped) {
2,170✔
2613
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
2614
    return TSDB_CODE_SUCCESS;
×
2615
  }
2616

2617
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
2,170✔
2618
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
2,170✔
2619
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2620
    return TSDB_CODE_SUCCESS;
×
2621
  }
2622

2623
  TAOS_CHECK_EXIT(code);
2,170✔
2624

2625
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
2,170✔
2626
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
2,170✔
2627
  if (userStopped || userDropped) {
2,170✔
2628
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2629
    goto _exit;
×
2630
  }
2631

2632
  switch (pAction->type) {
2,170✔
2633
    case STREAM_READER_TASK:
1,685✔
2634
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,685✔
2635
      break;
1,685✔
2636
/*
2637
    case STREAM_TRIGGER_TASK:
2638
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2639
      break;
2640
*/
2641
    case STREAM_RUNNER_TASK:
485✔
2642
      if (pAction->multiRunner) {
485✔
2643
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
485✔
2644
      } else {
2645
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
2646
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2647
      }
2648
      break;
485✔
2649
    default:
×
2650
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2651
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2652
      break;
×
2653
  }
2654

2655
_exit:
2,170✔
2656

2657
  if (pStream) {
2,170✔
2658
    mndReleaseStream(pCtx->pMnode, pStream);
2,170✔
2659
  }
2660

2661
  if (code) {
2,170✔
2662
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2663
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2664
  }
2665

2666
  return code;
2,170✔
2667
}
2668

2669
static int32_t msmTDRemoveStream(int64_t streamId) {
×
2670
  void* pIter = NULL;
×
2671
  
2672
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
×
2673
    while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
×
2674
      SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
×
2675
      int32_t taskNum = taosArrayGetSize(pVg->taskList);
×
2676
      if (atomic_load_32(&pVg->deployed) == taskNum) {
×
2677
        continue;
×
2678
      }
2679
      
2680
      for (int32_t i = 0; i < taskNum; ++i) {
×
2681
        SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
×
2682
        if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2683
          pExt->deployed = true;
×
2684
        }
2685
      }
2686
    }
2687
  }
2688

2689
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
2690
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2691
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2692
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2693
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2694
        for (int32_t i = 0; i < taskNum; ++i) {
×
2695
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2696
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2697
            pExt->deployed = true;
×
2698
          }
2699
        }
2700
      }
2701

2702
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
2703
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2704
        for (int32_t i = 0; i < taskNum; ++i) {
×
2705
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2706
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2707
            pExt->deployed = true;
×
2708
          }
2709
        }
2710
      }
2711
    }
2712
  }
2713

2714
  return TSDB_CODE_SUCCESS;
×
2715
}
2716

2717
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
473✔
2718
  int32_t code = TSDB_CODE_SUCCESS;
473✔
2719
  int32_t lino = 0;
473✔
2720

2721
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
473✔
2722

2723
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
473✔
2724

2725
_exit:
473✔
2726

2727
  if (code) {
473✔
2728
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2729
  } else {
2730
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
473✔
2731
  }
2732

2733
  return code;
473✔
2734
}
2735

2736
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
38,635✔
2737
  int32_t code = TSDB_CODE_SUCCESS;
38,635✔
2738
  int32_t lino = 0;
38,635✔
2739

2740
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
38,635✔
2741
  if (0 == active || MND_STM_STATE_NORMAL != state) {
38,635✔
2742
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
341✔
2743
    return;
341✔
2744
  }
2745

2746
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
38,294✔
2747
  if (NULL == pStream) {
38,294✔
2748
    mstsInfo("stream %s already not in streamMap", streamName);
870✔
2749
    goto _exit;
870✔
2750
  }
2751

2752
  atomic_store_8(&pStream->stopped, 2);
37,424✔
2753

2754
  mstsInfo("set stream %s stopped by user", streamName);
37,424✔
2755

2756
_exit:
×
2757

2758
  taosHashRelease(mStreamMgmt.streamMap, pStream);
38,294✔
2759

2760
  if (code) {
38,294✔
2761
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2762
  }
2763

2764
  return;
38,294✔
2765
}
2766

2767
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,724✔
2768
  int32_t code = TSDB_CODE_SUCCESS;
3,724✔
2769
  int32_t lino = 0;
3,724✔
2770

2771
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,724✔
2772
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,724✔
2773
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2774
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2775
  }
2776

2777
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,724✔
2778
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,724✔
2779
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2780
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2781
    goto _exit;
×
2782
  }
2783

2784
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,724✔
2785

2786
_exit:
3,724✔
2787

2788
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,724✔
2789

2790
  if (code) {
3,724✔
2791
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2792
  }
2793

2794
  return code;
3,724✔
2795
}
2796

2797
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
73,933✔
2798
  int32_t code = TSDB_CODE_SUCCESS;
73,933✔
2799
  int32_t lino = 0;
73,933✔
2800
  SStmQNode* pQNode = NULL;
73,933✔
2801

2802
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
312,141✔
2803
    switch (pQNode->type) {
238,208✔
2804
      case STREAM_ACT_DEPLOY:
238,208✔
2805
        if (pQNode->streamAct) {
238,208✔
2806
          mstDebug("start to handle stream deploy action");
236,038✔
2807
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
236,038✔
2808
        } else {
2809
          mstDebug("start to handle task deploy action");
2,170✔
2810
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
2,170✔
2811
        }
2812
        break;
238,208✔
2813
      default:
×
2814
        break;
×
2815
    }
2816
  }
2817

2818
_exit:
73,933✔
2819

2820
  if (code) {
73,933✔
2821
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2822
  }
2823
}
73,933✔
2824

2825
void msmStopAllStreamsByGrant(int32_t errCode) {
×
2826
  SStmStatus* pStatus = NULL;
×
2827
  void* pIter = NULL;
×
2828
  int64_t streamId = 0;
×
2829
  
2830
  while (true) {
2831
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
2832
    if (NULL == pIter) {
×
2833
      break;
×
2834
    }
2835

2836
    pStatus = (SStmStatus*)pIter;
×
2837

2838
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2839
    atomic_store_8(&pStatus->stopped, 4);
×
2840

2841
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2842
  }
2843
}
×
2844

2845
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2846
  mstInfo("stream grant expired");
×
2847

2848
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2849
    mstWarn("mnode stream is NOT active, ignore handling");
×
2850
    return errCode;
×
2851
  }
2852

2853
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2854

2855
  msmStopAllStreamsByGrant(errCode);
×
2856

2857
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2858
  
2859
  return errCode;
×
2860
}
2861

2862
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,787,983✔
2863
  int32_t code = TSDB_CODE_SUCCESS;
1,787,983✔
2864
  int32_t lino = 0;
1,787,983✔
2865
  int64_t streamId = pDeploy->task.streamId;
1,787,983✔
2866
  
2867
  switch (pDeploy->task.type) {
1,787,983✔
2868
    case STREAM_READER_TASK:
756,168✔
2869
      if (NULL == pStream->readerTasks) {
756,168✔
2870
        pStream->streamId = streamId;
301,528✔
2871
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
301,528✔
2872
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
301,528✔
2873
      }
2874
      
2875
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,512,336✔
2876
      break;
756,168✔
2877
    case STREAM_TRIGGER_TASK:
234,122✔
2878
      pStream->streamId = streamId;
234,122✔
2879
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
234,122✔
2880
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
234,122✔
2881
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
234,122✔
2882
      break;
234,122✔
2883
    case STREAM_RUNNER_TASK:
797,693✔
2884
      if (NULL == pStream->runnerTasks) {
797,693✔
2885
        pStream->streamId = streamId;
237,854✔
2886
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
237,854✔
2887
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
237,854✔
2888
      }      
2889
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,595,386✔
2890
      break;
797,693✔
2891
    default:
×
2892
      break;
×
2893
  }
2894

2895
_exit:
1,787,983✔
2896

2897
  if (code) {
1,787,983✔
2898
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2899
  }
2900

2901
  return code;
1,787,983✔
2902
}
2903

2904
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,787,983✔
2905
  int32_t code = TSDB_CODE_SUCCESS;
1,787,983✔
2906
  int32_t lino = 0;
1,787,983✔
2907
  int64_t streamId = pDeploy->task.streamId;
1,787,983✔
2908
  SStreamTask* pTask = &pDeploy->task;
1,787,983✔
2909
  SStmStreamDeploy streamDeploy = {0};
1,787,983✔
2910
  SStmStreamDeploy* pStream = NULL;
1,787,983✔
2911
   
2912
  while (true) {
2913
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,787,983✔
2914
    if (NULL == pStream) {
1,787,983✔
2915
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
327,696✔
2916
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
327,696✔
2917
      if (TSDB_CODE_SUCCESS == code) {
327,696✔
2918
        goto _exit;
327,696✔
2919
      }
2920

2921
      if (TSDB_CODE_DUP_KEY != code) {
×
2922
        goto _exit;
×
2923
      }    
2924

2925
      tFreeSStmStreamDeploy(&streamDeploy);
×
2926
      continue;
×
2927
    }
2928

2929
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,460,287✔
2930
    
2931
    break;
1,460,287✔
2932
  }
2933
  
2934
_exit:
1,787,983✔
2935

2936
  taosHashRelease(pHash, pStream);
1,787,983✔
2937

2938
  if (code) {
1,787,983✔
2939
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2940
  } else {
2941
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,787,983✔
2942
  }
2943

2944
  return code;
1,787,983✔
2945
}
2946

2947

2948
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
376,125✔
2949
  int32_t code = TSDB_CODE_SUCCESS;
376,125✔
2950
  int32_t lino = 0;
376,125✔
2951
  int32_t taskNum = taosArrayGetSize(pTasks);
376,125✔
2952

2953
  for (int32_t i = 0; i < taskNum; ++i) {
2,175,564✔
2954
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,799,439✔
2955
    if (pExt->deployed) {
1,799,439✔
2956
      continue;
11,456✔
2957
    }
2958

2959
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,787,983✔
2960
    pExt->deployed = true;
1,787,983✔
2961

2962
    (void)atomic_add_fetch_32(deployed, 1);
1,787,983✔
2963
  }
2964

2965
_exit:
376,125✔
2966

2967
  if (code) {
376,125✔
2968
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2969
  }
2970

2971
  return code;
376,125✔
2972
}
2973

2974
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
106,973✔
2975
  int32_t code = TSDB_CODE_SUCCESS;
106,973✔
2976
  int32_t lino = 0;
106,973✔
2977
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
106,973✔
2978
  SStmVgTasksToDeploy* pVg = NULL;
106,973✔
2979
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2980

2981
  mstDebug("start to add stream vgroup tasks deploy");
106,973✔
2982
  
2983
  for (int32_t i = 0; i < vgNum; ++i) {
711,963✔
2984
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
604,990✔
2985

2986
    msmUpdateVgroupUpTs(pCtx, *vgId);
604,990✔
2987

2988
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
604,990✔
2989
    if (NULL == pVg) {
604,990✔
2990
      continue;
364,591✔
2991
    }
2992

2993
    if (taosWTryLockLatch(&pVg->lock)) {
240,399✔
2994
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
5,064✔
2995
      pVg = NULL;
5,064✔
2996
      continue;
5,064✔
2997
    }
2998

2999
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
235,335✔
3000
      taosWUnLockLatch(&pVg->lock);
14,729✔
3001
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
14,729✔
3002
      pVg = NULL;
14,729✔
3003
      continue;
14,729✔
3004
    }
3005

3006
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
220,606✔
3007
    taosWUnLockLatch(&pVg->lock);
220,606✔
3008
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
220,606✔
3009
    pVg = NULL;
220,606✔
3010
  }
3011

3012
_exit:
106,973✔
3013

3014
  if (code) {
106,973✔
3015
    if (pVg) {
×
3016
      taosWUnLockLatch(&pVg->lock);
×
3017
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3018
    }
3019

3020
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3021
  }
3022

3023
  return code;
106,973✔
3024
}
3025

3026
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
145,454✔
3027
  int32_t code = TSDB_CODE_SUCCESS;
145,454✔
3028
  int32_t lino = 0;
145,454✔
3029
  SStmSnodeTasksDeploy* pSnode = NULL;
145,454✔
3030
  SStreamHbMsg* pReq = pCtx->pReq;
145,454✔
3031

3032
  mstDebug("start to add stream snode tasks deploy");
145,454✔
3033
  
3034
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
145,454✔
3035
  if (NULL == pSnode) {
145,454✔
3036
    return TSDB_CODE_SUCCESS;
63,671✔
3037
  }
3038

3039
  (void)mstWaitLock(&pSnode->lock, false);
81,783✔
3040
  
3041
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
81,783✔
3042
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
76,403✔
3043
  }
3044

3045
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
81,783✔
3046
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
79,116✔
3047
  }
3048
  
3049
  taosWUnLockLatch(&pSnode->lock);
81,783✔
3050

3051
_exit:
81,783✔
3052

3053
  if (code) {
81,783✔
3054
    if (pSnode) {
×
3055
      taosWUnLockLatch(&pSnode->lock);
×
3056
    }
3057

3058
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3059
  }
3060

3061
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
81,783✔
3062
  return code;
81,783✔
3063
}
3064

3065
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
819,583✔
3066
  int32_t code = TSDB_CODE_SUCCESS;
819,583✔
3067
  int32_t lino = 0;
819,583✔
3068
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
819,583✔
3069
  if (NULL == pStatus) {
819,583✔
3070
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3071
    return TSDB_CODE_SUCCESS;
×
3072
  }
3073
  
3074
  pStatus->lastActionTs = currTs;
819,583✔
3075

3076
_exit:
819,583✔
3077

3078
  if (code) {
819,583✔
3079
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3080
  }
3081

3082
  return code;
819,583✔
3083
}
3084

3085
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
99,678✔
3086
  int32_t code = TSDB_CODE_SUCCESS;
99,678✔
3087
  int32_t lino = 0;
99,678✔
3088
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
99,678✔
3089
  void* pIter = NULL;
99,678✔
3090

3091
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
99,678✔
3092
  
3093
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
99,678✔
3094
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
99,678✔
3095

3096
  while (1) {
327,696✔
3097
    pIter = taosHashIterate(pCtx->deployStm, pIter);
427,374✔
3098
    if (pIter == NULL) {
427,374✔
3099
      break;
99,678✔
3100
    }
3101
    
3102
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
327,696✔
3103
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
655,392✔
3104

3105
    int64_t streamId = pDeploy->streamId;
327,696✔
3106
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
327,696✔
3107
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
3108

3109
    mstClearSStmStreamDeploy(pDeploy);
327,696✔
3110
    
3111
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
327,696✔
3112
  }
3113
  
3114
_exit:
99,678✔
3115

3116
  if (pIter) {
99,678✔
3117
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3118
  }
3119

3120
  if (code) {
99,678✔
3121
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3122
  }
3123
  
3124
  return code;
99,678✔
3125
}
3126

3127
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
104,306✔
3128
  int32_t code = TSDB_CODE_SUCCESS;
104,306✔
3129
  int32_t lino = 0;
104,306✔
3130
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
104,306✔
3131
  SStmVgTasksToDeploy* pVg = NULL;
104,306✔
3132
  
3133
  for (int32_t i = 0; i < vgNum; ++i) {
686,037✔
3134
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
581,731✔
3135
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
581,731✔
3136
    if (NULL == pVg) {
581,731✔
3137
      continue;
361,924✔
3138
    }
3139

3140
    if (taosWTryLockLatch(&pVg->lock)) {
219,807✔
3141
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
799✔
3142
      continue;
799✔
3143
    }
3144
    
3145
    if (atomic_load_32(&pVg->deployed) <= 0) {
219,008✔
3146
      taosWUnLockLatch(&pVg->lock);
×
3147
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3148
      continue;
×
3149
    }
3150

3151
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
219,008✔
3152
    if (atomic_load_32(&pVg->deployed) == taskNum) {
219,008✔
3153
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
219,008✔
3154
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
219,008✔
3155
      pVg->taskList = NULL;
219,008✔
3156
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
219,008✔
3157
      taosWUnLockLatch(&pVg->lock);
219,008✔
3158
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
219,008✔
3159
      continue;
219,008✔
3160
    }
3161

UNCOV
3162
    for (int32_t m = taskNum - 1; m >= 0; --m) {
×
UNCOV
3163
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
×
UNCOV
3164
      if (!pExt->deployed) {
×
UNCOV
3165
        continue;
×
3166
      }
3167

UNCOV
3168
      mstDestroySStmTaskToDeployExt(pExt);
×
3169

UNCOV
3170
      taosArrayRemove(pVg->taskList, m);
×
UNCOV
3171
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
×
3172
    }
UNCOV
3173
    atomic_store_32(&pVg->deployed, 0);
×
UNCOV
3174
    taosWUnLockLatch(&pVg->lock);
×
UNCOV
3175
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3176
  }
3177

3178
_exit:
104,306✔
3179

3180
  if (code) {
104,306✔
3181
    if (pVg) {
×
3182
      taosWUnLockLatch(&pVg->lock);
×
3183
    }
3184

3185
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3186
  }
3187
}
104,306✔
3188

3189
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
167,181✔
3190
  if (!GOT_SNODE(snodeId)) {
167,181✔
3191
    return;
24,394✔
3192
  }
3193
  
3194
  int32_t code = TSDB_CODE_SUCCESS;
142,787✔
3195
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
142,787✔
3196
  if (NULL == pSnode) {
142,787✔
3197
    return;
63,671✔
3198
  }
3199

3200
  if (taosWTryLockLatch(&pSnode->lock)) {
79,116✔
3201
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3202
    return;
×
3203
  }
3204

3205
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
79,116✔
3206
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
79,116✔
3207
  
3208
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
79,116✔
3209
    taosWUnLockLatch(&pSnode->lock);
×
3210
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3211
    return;
×
3212
  }
3213

3214
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
79,116✔
3215
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
79,116✔
3216
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
79,116✔
3217
    pSnode->triggerList = NULL;
79,116✔
3218
  }
3219

3220
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
79,116✔
3221
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
79,116✔
3222
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
79,116✔
3223
    pSnode->runnerList = NULL;
79,116✔
3224
  }
3225

3226
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
79,116✔
3227
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
79,116✔
3228
    taosWUnLockLatch(&pSnode->lock);
79,116✔
3229
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
79,116✔
3230
    return;
79,116✔
3231
  }
3232

UNCOV
3233
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
×
UNCOV
3234
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
×
UNCOV
3235
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
×
UNCOV
3236
      if (!pExt->deployed) {
×
3237
        continue;
×
3238
      }
3239

UNCOV
3240
      mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
3241
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
UNCOV
3242
      taosArrayRemove(pSnode->triggerList, m);
×
3243
    }
3244
    
UNCOV
3245
    pSnode->triggerDeployed = 0;
×
3246
  }
3247

UNCOV
3248
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
×
UNCOV
3249
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
×
UNCOV
3250
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
×
UNCOV
3251
      if (!pExt->deployed) {
×
UNCOV
3252
        continue;
×
3253
      }
3254

UNCOV
3255
      mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
3256
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
UNCOV
3257
      taosArrayRemove(pSnode->runnerList, m);
×
3258
    }
3259
    
UNCOV
3260
    pSnode->runnerDeployed = 0;
×
3261
  }
3262
  
UNCOV
3263
  taosWUnLockLatch(&pSnode->lock);
×
UNCOV
3264
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3265
}
3266

3267
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
18,675,991✔
3268
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
18,675,991✔
3269
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
104,306✔
3270
  }
3271

3272
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
18,675,991✔
3273
    msmCleanDeployedSnodeTasks(pHb->snodeId);
167,181✔
3274
  }
3275
}
18,675,991✔
3276

3277
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
18,675,991✔
3278
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
18,675,991✔
3279
  if (mStreamMgmt.tCtx) {
18,675,991✔
3280
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
18,597,098✔
3281
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
18,597,098✔
3282
  }
3283
}
18,675,991✔
3284

3285
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
228,009✔
3286
  int32_t code = TSDB_CODE_SUCCESS;
228,009✔
3287
  int32_t lino = 0;
228,009✔
3288
  int32_t action = STREAM_ACT_START;
228,009✔
3289
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
228,009✔
3290
  if (pAction) {
228,009✔
3291
    pAction->actions |= action;
×
3292
    pAction->start.triggerId = *pId;
×
3293
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3294
  } else {
3295
    SStmAction newAction = {0};
228,009✔
3296
    newAction.actions = action;
228,009✔
3297
    newAction.start.triggerId = *pId;
228,009✔
3298
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
228,009✔
3299
    mstsDebug("stream add START action, actions:%x", newAction.actions);
228,009✔
3300
  }
3301

3302
_exit:
228,009✔
3303

3304
  if (code) {
228,009✔
3305
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3306
  }
3307

3308
  return code;
228,009✔
3309
}
3310

3311
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
3312
  int32_t code = TSDB_CODE_SUCCESS;
×
3313
  int32_t lino = 0;
×
3314
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3315
  
3316
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
3317
  if (pAction) {
×
3318
    pAction->actions |= action;
×
3319
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3320
  } else {
3321
    SStmAction newAction = {0};
×
3322
    newAction.actions = action;
×
3323
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3324
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3325
  }
3326

3327
_exit:
×
3328

3329
  if (code) {
×
3330
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3331
  }
3332

3333
  return code;
×
3334
}
3335

3336

3337

3338
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
263,878✔
3339
  int32_t code = TSDB_CODE_SUCCESS;
263,878✔
3340
  int32_t lino = 0;
263,878✔
3341
  int32_t action = STREAM_ACT_UNDEPLOY;
263,878✔
3342
  bool    dropped = false;
263,878✔
3343

3344
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
263,878✔
3345
  mstsDebug("stream dropped: %d", dropped);
263,878✔
3346
  
3347
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
263,878✔
3348
  if (pAction) {
263,878✔
3349
    pAction->actions |= action;
212,418✔
3350
    if (NULL == pAction->undeploy.taskList) {
212,418✔
3351
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3352
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3353
    }
3354

3355
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
424,836✔
3356
    if (pAction->undeploy.doCheckpoint) {
212,418✔
3357
      pAction->undeploy.doCheckpoint = dropped ? false : true;
32,122✔
3358
    }
3359
    if (!pAction->undeploy.doCleanup) {
212,418✔
3360
      pAction->undeploy.doCleanup = dropped ? true : false;
32,122✔
3361
    }
3362
    
3363
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
212,418✔
3364
  } else {
3365
    SStmAction newAction = {0};
51,460✔
3366
    newAction.actions = action;
51,460✔
3367
    newAction.undeploy.doCheckpoint = dropped ? false : true;
51,460✔
3368
    newAction.undeploy.doCleanup = dropped ? true : false;
51,460✔
3369
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
51,460✔
3370
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
51,460✔
3371
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
102,920✔
3372
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
51,460✔
3373
    
3374
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
51,460✔
3375
  }
3376

3377
_exit:
263,878✔
3378

3379
  if (code) {
263,878✔
3380
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3381
  }
3382

3383
  return code;
263,878✔
3384
}
3385

3386
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,724✔
3387
  int32_t code = TSDB_CODE_SUCCESS;
3,724✔
3388
  int32_t lino = 0;
3,724✔
3389
  int32_t action = STREAM_ACT_RECALC;
3,724✔
3390
  SStmAction newAction = {0};
3,724✔
3391
  
3392
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,724✔
3393
  if (pAction) {
3,724✔
3394
    pAction->actions |= action;
×
3395
    pAction->recalc.recalcList = recalcList;
×
3396

3397
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3398
  } else {
3399
    newAction.actions = action;
3,724✔
3400
    newAction.recalc.recalcList = recalcList;
3,724✔
3401
    
3402
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,724✔
3403
    
3404
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,724✔
3405
  }
3406

3407
_exit:
3,724✔
3408

3409
  if (code) {
3,724✔
3410
    mstDestroySStmAction(&newAction);
×
3411
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3412
  }
3413

3414
  return code;
3,724✔
3415
}
3416

3417
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
304,385✔
3418
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
304,385✔
3419
  if (NULL == pStream) {
304,385✔
3420
    return false;
×
3421
  }
3422

3423
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
304,385✔
3424
    return false;
×
3425
  }
3426

3427
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
304,385✔
3428
  for (int32_t i = 0; i < readerNum; ++i) {
668,896✔
3429
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
364,511✔
3430
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
364,511✔
3431
      return false;
×
3432
    }
3433
  }
3434

3435
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
304,385✔
3436
  for (int32_t i = 0; i < readerNum; ++i) {
304,385✔
3437
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
×
3438
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
×
3439
      return false;
×
3440
    }
3441
  }
3442

3443
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
304,385✔
3444
  SListNode* pNode = listHead(pStream->calcReaders);
304,385✔
3445
  for (int32_t i = 0; i < readerNum; ++i) {
866,932✔
3446
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
562,547✔
3447
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
562,547✔
3448
      return false;
×
3449
    }
3450
    pNode = TD_DLIST_NODE_NEXT(pNode);
562,547✔
3451
  }
3452

3453
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
989,756✔
3454
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
761,747✔
3455
    for (int32_t m = 0; m < runnerNum; ++m) {
1,549,874✔
3456
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
864,503✔
3457
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
864,503✔
3458
        return false;
76,376✔
3459
      }
3460
    }
3461
  }
3462
  
3463
  return true;
228,009✔
3464
}
3465

3466

3467
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
16,630,926✔
3468
  int32_t code = TSDB_CODE_SUCCESS;
16,630,926✔
3469
  int32_t lino = 0;
16,630,926✔
3470
  int32_t action = 0;
16,630,926✔
3471
  int64_t streamId = pMsg->streamId;
16,630,926✔
3472
  SStreamTask* pTask = (SStreamTask*)pMsg;
16,630,926✔
3473
  int8_t  stopped = 0;
16,630,926✔
3474

3475
  msttDebug("start to handle task abnormal status %d", pTask->status);
16,630,926✔
3476
  
3477
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
16,630,926✔
3478
  if (NULL == pStatus) {
16,630,926✔
3479
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3480
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3481
    return;
12,080✔
3482
  }
3483

3484
  stopped = atomic_load_8(&pStatus->stopped);
16,630,926✔
3485
  if (stopped) {
16,630,926✔
3486
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3487
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3488
    return;
×
3489
  }
3490
  
3491
  switch (pMsg->status) {
16,630,926✔
3492
    case STREAM_STATUS_INIT:      
16,623,571✔
3493
      if (STREAM_TRIGGER_TASK != pMsg->type) {
16,623,571✔
3494
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
16,160,896✔
3495
        return;
16,160,896✔
3496
      }
3497
      
3498
      if (INT64_MIN == pStatus->lastActionTs) {
462,675✔
3499
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3500
        return;
×
3501
      }
3502
      
3503
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
462,675✔
3504
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
158,290✔
3505
        return;
158,290✔
3506
      }
3507

3508
      if (STREAM_IS_RUNNING(pStatus)) {
304,385✔
3509
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3510
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
304,385✔
3511
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
228,009✔
3512
      }
3513
      break;
304,385✔
3514
    case STREAM_STATUS_FAILED:
7,355✔
3515
      //STREAMTODO ADD ERRCODE HANDLE
3516
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
7,355✔
3517
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
6,333✔
3518
                 pMsg->taskIdx);
3519
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
6,333✔
3520
      } else {
3521
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
1,022✔
3522
                 pMsg->taskIdx);
3523
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
1,022✔
3524
      }
3525
      break;
7,355✔
3526
    default:
×
3527
      break;
×
3528
  }
3529

3530
_exit:
311,740✔
3531

3532
  if (code) {
311,740✔
3533
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3534
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3535
  }
3536
}
3537

3538
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
262,856✔
3539
  int32_t code = TSDB_CODE_SUCCESS;
262,856✔
3540
  int32_t lino = 0;
262,856✔
3541
  SStreamTask* pTask = (SStreamTask*)pStatus;
262,856✔
3542
  int64_t streamId = pStatus->streamId;
262,856✔
3543

3544
  msttInfo("start to handle task error, type: %d", err);
262,856✔
3545

3546
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
262,856✔
3547

3548
_exit:
262,856✔
3549

3550
  if (code) {
262,856✔
3551
    // IGNORE STOP STREAM BY ERROR  
3552
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3553
  }
3554
}
262,856✔
3555

3556
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
6,562,058✔
3557
  int32_t code = TSDB_CODE_SUCCESS;
6,562,058✔
3558
  int32_t lino = 0;
6,562,058✔
3559
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
6,562,058✔
3560

3561
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
6,562,058✔
3562
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3563
  }
3564
  
3565
  SArray* userRecalcList = NULL;
6,562,058✔
3566
  if (atomic_load_ptr(&pStream->userRecalcList)) {
6,562,058✔
3567
    taosWLockLatch(&pStream->userRecalcLock);
3,724✔
3568
    if (pStream->userRecalcList) {
3,724✔
3569
      userRecalcList = pStream->userRecalcList;
3,724✔
3570
      pStream->userRecalcList = NULL;
3,724✔
3571
    }
3572
    taosWUnLockLatch(&pStream->userRecalcLock);
3,724✔
3573
    
3574
    if (userRecalcList) {
3,724✔
3575
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,724✔
3576
    }
3577
  }
3578

3579
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
6,562,058✔
3580
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
3,280,219✔
3581
    if (NULL == pStatus->detailStatus) {
3,280,219✔
3582
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
227,114✔
3583
      if (NULL == pStatus->detailStatus) {
227,114✔
3584
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3585
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3586
      }
3587
    }
3588
    
3589
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
3,280,219✔
3590
    taosWUnLockLatch(&pStatus->detailStatusLock);
3,280,219✔
3591
  }
3592

3593
_exit:
3,281,839✔
3594

3595
  if (code) {
6,562,058✔
3596
    // IGNORE STOP STREAM BY ERROR
3597
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3598
  }
3599
}
6,562,058✔
3600

3601
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,566,196✔
3602
  int32_t code = TSDB_CODE_SUCCESS;
1,566,196✔
3603
  int32_t lino = 0;
1,566,196✔
3604
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,566,196✔
3605

3606
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,566,196✔
3607

3608
  for (int32_t i = 0; i < num; ++i) {
54,677,821✔
3609
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
53,111,625✔
3610
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
53,111,625✔
3611
    
3612
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
53,111,625✔
3613
    if (NULL == ppStatus) {
53,111,625✔
3614
      msttWarn("task no longer exists in taskMap, streamId:0x%" PRIx64 " taskId:0x%" PRIx64 " type:%s status:%s taskIdx:%d",
1,716✔
3615
          (uint64_t)pTask->streamId, (uint64_t)pTask->taskId, gStreamTaskTypeStr[pTask->type], gStreamStatusStr[pTask->status], pTask->taskIdx);
3616
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
1,716✔
3617
      continue;
1,716✔
3618
    }
3619

3620
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
53,109,909✔
3621
    int8_t stopped = atomic_load_8(&pStream->stopped);
53,109,909✔
3622
    if (stopped) {
53,109,909✔
3623
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
261,140✔
3624
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
261,140✔
3625
      continue;
261,140✔
3626
    }
3627

3628
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
52,848,769✔
3629
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3630
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3631
          
3632
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3633
      continue;
×
3634
    }
3635

3636
    if ((*ppStatus)->status != pTask->status) {
52,848,769✔
3637
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,918,687✔
3638
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,230,474✔
3639
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,688,213✔
3640
        if (pStream->triggerTask) {
×
3641
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3642
        }
3643
        
3644
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3645
      }
3646
    }
3647
    
3648
    (*ppStatus)->errCode = pTask->errorCode;
52,848,769✔
3649
    (*ppStatus)->status = pTask->status;
52,848,769✔
3650
    (*ppStatus)->lastUpTs = pCtx->currTs;
52,848,769✔
3651
    
3652
    if (STREAM_TRIGGER_TASK == pTask->type && STREAM_STATUS_RUNNING == pTask->status) {
52,848,769✔
3653
      mstInfo("streamId:0x%" PRIx64 " trigger task status updated to Running in streamMap", (uint64_t)pTask->streamId);
6,096,376✔
3654
    }
3655
    
3656
    if (STREAM_STATUS_RUNNING != pTask->status) {
52,848,769✔
3657
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
16,630,926✔
3658
    }
3659
    
3660
    if (STREAM_TRIGGER_TASK == pTask->type) {
52,848,769✔
3661
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
6,562,058✔
3662
    }
3663
  }
3664

3665
_exit:
1,566,196✔
3666

3667
  if (code) {
1,566,196✔
3668
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3669
  }
3670

3671
  return code;
1,566,196✔
3672
}
3673

3674
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3675
  int32_t code = TSDB_CODE_SUCCESS;
×
3676
  int32_t lino = 0;
×
3677
  int64_t streamId = pTask->streamId;
×
3678
  SStreamObj* pStream = NULL;
×
3679

3680
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3681
  if (NULL == pStatus) {
×
3682
    SStmStatus status = {0};
×
3683
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3684
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3685
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
3686
      mndReleaseStream(pCtx->pMnode, pStream);
×
3687
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3688
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3689
      return code;
×
3690
    }
3691

3692
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3693
    mndReleaseStream(pCtx->pMnode, pStream);
×
3694

3695
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3696
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3697
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3698
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3699
  }
3700

3701
  SStmTaskStatus* pNewTask = NULL;
×
3702
  switch (pTask->type) {
×
3703
    case STREAM_READER_TASK: {
×
3704
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3705
      if (NULL == pList) {
×
3706
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
3707
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3708
      }
3709
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3710
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3711
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3712
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3713
            (int32_t)taosArrayGetSize(pList), readerSize);
3714
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3715
      }
3716
      
3717
      SStmTaskStatus taskStatus = {0};
×
3718
      taskStatus.pStream = pStatus;
×
3719
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3720
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3721
        pNewTask = taosArrayPush(pList, &taskStatus);
×
3722
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3723
      } else {
3724
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3725
        SListNode* pTailNode = tdListGetTail(pStatus->calcReaders);
×
3726
        QUERY_CHECK_NULL(pTailNode, code, lino, _exit, TSDB_CODE_INTERNAL_ERROR);
×
3727
        pNewTask = (SStmTaskStatus*)pTailNode->data;
×
3728
      }
3729
      
3730
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3731
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3732
      break;
×
3733
    }
3734
    case STREAM_TRIGGER_TASK: {
×
3735
      taosMemoryFreeClear(pStatus->triggerTask);
×
3736
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3737
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3738
      pStatus->triggerTask->pStream = pStatus;
×
3739
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
3740
      pNewTask = pStatus->triggerTask;
×
3741

3742
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3743
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3744
      break;
×
3745
    }
3746
    case STREAM_RUNNER_TASK:{
×
3747
      if (NULL == pStatus->runners[pTask->deployId]) {
×
3748
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3749
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3750
      }
3751
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
3752
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3753
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3754
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3755
      }    
3756
      
3757
      SStmTaskStatus taskStatus = {0};
×
3758
      taskStatus.pStream = pStatus;
×
3759
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3760
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3761
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3762

3763
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3764
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3765
      break;
×
3766
    }
3767
    default: {
×
3768
      msttError("invalid task type:%d in task status", pTask->type);
×
3769
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3770
      break;
×
3771
    }
3772
  }
3773

3774
_exit:
×
3775

3776
  if (code) {
×
3777
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3778
  } else {
3779
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3780
  }
3781

3782
  return code;
×
3783
}
3784

3785
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3786
  int32_t code = TSDB_CODE_SUCCESS;
×
3787
  int32_t lino = 0;
×
3788
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3789

3790
  mstDebug("WATCH: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
×
3791

3792
  for (int32_t i = 0; i < num; ++i) {
×
3793
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
×
3794
    msttDebug("task status %s got, taskIdx:%d", gStreamStatusStr[pTask->status], pTask->taskIdx);
×
3795

3796
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3797
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3798
    }
3799
    
3800
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3801
    if (NULL == ppStatus) {
×
3802
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3803
      
3804
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3805
      
3806
      continue;
×
3807
    }
3808
    
3809
    (*ppStatus)->status = pTask->status;
×
3810
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3811
  }
3812

3813
_exit:
×
3814

3815
  if (code) {
×
3816
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3817
  }
3818

3819
  return code;
×
3820
}
3821

3822
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
228,009✔
3823
  int32_t code = TSDB_CODE_SUCCESS;
228,009✔
3824
  int32_t lino = 0;
228,009✔
3825
  if (NULL == pCtx->pRsp->start.taskList) {
228,009✔
3826
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
123,139✔
3827
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
123,139✔
3828
  }
3829

3830
  SStmTaskId* pId = &pAction->start.triggerId;
228,009✔
3831
  SStreamTaskStart start = {0};
228,009✔
3832
  start.task.type = STREAM_TRIGGER_TASK;
228,009✔
3833
  start.task.streamId = streamId;
228,009✔
3834
  start.task.taskId = pId->taskId;
228,009✔
3835
  start.task.seriousId = pId->seriousId;
228,009✔
3836
  start.task.nodeId = pId->nodeId;
228,009✔
3837
  start.task.taskIdx = pId->taskIdx;
228,009✔
3838

3839
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
456,018✔
3840
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
228,009✔
3841

3842
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
228,009✔
3843

3844
  return;
228,009✔
3845

3846
_exit:
×
3847

3848
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3849
}
3850

3851

3852
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
51,460✔
3853
  int32_t code = TSDB_CODE_SUCCESS;
51,460✔
3854
  int32_t lino = 0;
51,460✔
3855
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
51,460✔
3856
  if (NULL == pCtx->pRsp->undeploy.taskList) {
51,460✔
3857
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
34,621✔
3858
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
34,621✔
3859
  }
3860

3861
  SStreamTaskUndeploy undeploy;
51,460✔
3862
  for (int32_t i = 0; i < dropNum; ++i) {
315,338✔
3863
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
263,878✔
3864
    undeploy.task = *pTask;
263,878✔
3865
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
263,878✔
3866
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
263,878✔
3867

3868
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
527,756✔
3869
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
263,878✔
3870

3871
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
263,878✔
3872
  }
3873

3874
  return;
51,460✔
3875

3876
_exit:
×
3877

3878
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3879
}
3880

3881
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3882
  int32_t code = TSDB_CODE_SUCCESS;
×
3883
  int32_t lino = 0;
×
3884

3885
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3886
  if (NULL == pStream) {
×
3887
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3888
    return;
×
3889
  }
3890

3891
  if (NULL == pStream->triggerTask) {
×
3892
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3893
    return;
×
3894
  }
3895

3896
  SStreamMgmtRsp rsp = {0};
×
3897
  rsp.reqId = INT64_MIN;
×
3898
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3899
  rsp.task.streamId = streamId;
×
3900
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3901

3902
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3903

3904
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3905
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3906
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3907
  }
3908

3909
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
×
3910

3911
_exit:
×
3912

3913
  if (code) {
×
3914
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3915
  } else {
3916
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3917
  }
3918
}
3919

3920
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,724✔
3921
  int32_t code = TSDB_CODE_SUCCESS;
3,724✔
3922
  int32_t lino = 0;
3,724✔
3923

3924
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,724✔
3925
  if (NULL == pStream) {
3,724✔
3926
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3927
    return;
×
3928
  }
3929

3930
  if (NULL == pStream->triggerTask) {
3,724✔
3931
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3932
    return;
×
3933
  }
3934

3935
  SStreamMgmtRsp rsp = {0};
3,724✔
3936
  rsp.reqId = INT64_MIN;
3,724✔
3937
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,724✔
3938
  rsp.task.streamId = streamId;
3,724✔
3939
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,724✔
3940
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,724✔
3941

3942
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,724✔
3943
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,501✔
3944
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,501✔
3945
  }
3946

3947
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
7,448✔
3948

3949
_exit:
3,724✔
3950

3951
  if (code) {
3,724✔
3952
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3953
  } else {
3954
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,724✔
3955
  }
3956
}
3957

3958

3959
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
160,624✔
3960
  int32_t code = TSDB_CODE_SUCCESS;
160,624✔
3961
  int32_t lino = 0;
160,624✔
3962
  void* pIter = NULL;
160,624✔
3963
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
160,624✔
3964

3965
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
160,624✔
3966

3967
  while (1) {
283,193✔
3968
    pIter = taosHashIterate(pCtx->actionStm, pIter);
443,817✔
3969
    if (pIter == NULL) {
443,817✔
3970
      break;
160,624✔
3971
    }
3972

3973
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
283,193✔
3974
    SStmAction *pAction = (SStmAction *)pIter;
283,193✔
3975
    
3976
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
283,193✔
3977
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
51,460✔
3978
      continue;
51,460✔
3979
    }
3980

3981
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
231,733✔
3982
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3983
    }
3984

3985
    if (STREAM_ACT_RECALC & pAction->actions) {
231,733✔
3986
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,724✔
3987
    }
3988

3989
    if (STREAM_ACT_START & pAction->actions) {
231,733✔
3990
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
228,009✔
3991
    }
3992
  }
3993
  
3994
_exit:
160,624✔
3995

3996
  if (pIter) {
160,624✔
3997
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3998
  }
3999

4000
  if (code) {
160,624✔
4001
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4002
  }
4003

4004
  return code;
160,624✔
4005
}
4006

4007
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
18,597,098✔
4008
  int32_t  code = TSDB_CODE_SUCCESS;
18,597,098✔
4009
  int32_t  lino = 0;
18,597,098✔
4010
  int64_t* lastTs = NULL;
18,597,098✔
4011
  bool     noExists = false;
18,597,098✔
4012

4013
  while (true) {
4014
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
19,154,601✔
4015
    if (NULL == lastTs) {
19,154,601✔
4016
      if (noExists) {
716,498✔
4017
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
158,995✔
4018
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
158,995✔
4019
      }
4020

4021
      noExists = true;
557,503✔
4022
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
557,503✔
4023
      
4024
      continue;
557,503✔
4025
    }
4026

4027
    while (true) {
×
4028
      int64_t lastTsValue = atomic_load_64(lastTs);
18,438,103✔
4029
      if (pCtx->currTs > lastTsValue) {
18,438,103✔
4030
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
18,437,127✔
4031
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
18,437,127✔
4032
          return code;
18,437,127✔
4033
        }
4034

4035
        continue;
×
4036
      }
4037

4038
      return code;
976✔
4039
    }
4040

4041
    break;
4042
  }
4043

4044
_exit:
158,995✔
4045

4046
  if (code) {
158,995✔
4047
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
158,995✔
4048
  }
4049

4050
  return code;  
158,995✔
4051
}
4052

4053
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
4054
  SStmStatus* pStatus = NULL;
×
4055
  int32_t trigReaderNum = 0;
×
4056
  int32_t calcReaderNum = 0;
×
4057
  int32_t runnerNum = 0;
×
4058
  int64_t streamId = 0;
×
4059
  void* pIter = NULL;
×
4060
  while (true) {
4061
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
4062
    if (NULL == pIter) {
×
4063
      return;
×
4064
    }
4065

4066
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4067
    pStatus = (SStmStatus*)pIter;
×
4068

4069
    if (NULL == pStatus->triggerTask) {
×
4070
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
4071
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4072
      continue;
×
4073
    }
4074
    
4075
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
4076
    if (pStatus->trigReaderNum != trigReaderNum) {
×
4077
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
4078
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4079
      continue;
×
4080
    }
4081

4082
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
4083
    if (pStatus->calcReaderNum != calcReaderNum) {
×
4084
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
4085
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4086
      continue;
×
4087
    }
4088

4089
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
4090
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
4091
      if (runnerNum != pStatus->runnerNum) {
×
4092
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
4093
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4094
        continue;
×
4095
      }
4096
    }
4097
  }
4098
}
4099

4100
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
368✔
4101
  int32_t code = TSDB_CODE_SUCCESS;
368✔
4102
  int32_t lino = 0;
368✔
4103
  int32_t minVal = watchError ? 0 : 1;
368✔
4104

4105
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
368✔
4106
    return code;
×
4107
  }
4108

4109
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
368✔
4110
    (void)sched_yield();
×
4111
  }
4112

4113
  if (watchError) {
368✔
4114
    taosHashClear(mStreamMgmt.vgroupMap);
×
4115
    taosHashClear(mStreamMgmt.snodeMap);
×
4116
    taosHashClear(mStreamMgmt.taskMap);
×
4117
    taosHashClear(mStreamMgmt.streamMap);
×
4118
    mstInfo("watch error happends, clear all maps");
×
4119
    goto _exit;
×
4120
  }
4121

4122
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
368✔
4123
    mstInfo("no stream tasks remain during watch state");
368✔
4124
    goto _exit;
368✔
4125
  }
4126

4127
  msmWatchCheckStreamMap(pCtx);
×
4128

4129
_exit:
368✔
4130

4131
  mStreamMgmt.lastTaskId += 100000;
368✔
4132

4133
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
368✔
4134

4135
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
368✔
4136

4137
  if (code) {
368✔
4138
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4139
  }
4140

4141
  return code;
368✔
4142
}
4143

4144

4145
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
12,232✔
4146
  int32_t code = TSDB_CODE_SUCCESS;
12,232✔
4147
  int32_t lino = 0;
12,232✔
4148
  SStreamHbMsg* pReq = pCtx->pReq;
12,232✔
4149

4150
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
12,232✔
4151
  
4152
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
12,232✔
4153
    goto _exit;
×
4154
  }
4155

4156
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
12,232✔
4157
  if (GOT_SNODE(pReq->snodeId)) {
12,232✔
4158
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,181✔
4159
  }
4160

4161
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
12,232✔
4162
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4163
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4164
  }
4165

4166
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
12,232✔
4167
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
368✔
4168
  }
4169

4170
_exit:
12,232✔
4171

4172
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
12,232✔
4173
  
4174
  if (code) {
12,232✔
4175
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4176

4177
    (void)msmWatchHandleEnding(pCtx, true);
×
4178
  }
4179

4180
  return code;
12,232✔
4181
}
4182

4183
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
7,972,595✔
4184
  int32_t listSize = taosArrayGetSize(pOReaders);
7,972,595✔
4185
  int32_t totalSize = 0;
7,971,796✔
4186
  
4187
  for (int32_t i = 0; i < listSize; ++i) {
8,569,017✔
4188
    SArray* pList = taosArrayGetP(pOReaders, i);
597,221✔
4189
    totalSize += taosArrayGetSize(pList);
597,221✔
4190
  }
4191

4192
  return totalSize;
7,971,796✔
4193
}
4194

4195
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
371,114✔
4196
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
371,114✔
4197
  if (NULL == pList) {
371,114✔
4198
    return NULL;
×
4199
  }
4200

4201
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
371,114✔
4202
}
4203

4204

4205
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
64,394✔
4206
  int32_t code = TSDB_CODE_SUCCESS;
64,394✔
4207
  int32_t lino = 0;
64,394✔
4208

4209
  if (NULL == pStatus->trigOReaders) {
64,394✔
4210
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
55,925✔
4211
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
55,925✔
4212
  }
4213

4214
  while (true) {
55,925✔
4215
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
120,319✔
4216

4217
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
120,319✔
4218
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
55,925✔
4219
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
55,925✔
4220

4221
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
111,850✔
4222
      continue;
55,925✔
4223
    }
4224

4225
    *ppRes = *ppOReaderList;
64,394✔
4226
    break;
64,394✔
4227
  }
4228

4229
_exit:
64,394✔
4230

4231
  if (code) {
64,394✔
4232
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4233
  }
4234

4235
  return code;
64,394✔
4236
}
4237

4238
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
75,890✔
4239
  int32_t code = TSDB_CODE_SUCCESS;
75,890✔
4240
  int32_t lino = 0;
75,890✔
4241
  bool    readerExists = false;
75,890✔
4242
  int64_t streamId = pTask->streamId;
75,890✔
4243

4244
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
75,890✔
4245
  for (int32_t i = 0; i < readerNum; ++i) {
157,391✔
4246
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
92,997✔
4247
    if (pReader->id.nodeId == vgId) {
92,997✔
4248
      readerExists = true;
11,496✔
4249
      break;
11,496✔
4250
    }
4251
  }
4252

4253
  if (!readerExists) {
75,890✔
4254
    SArray* pReaderList = NULL;
64,394✔
4255
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
64,394✔
4256
    
4257
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
64,394✔
4258
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
64,394✔
4259
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
64,394✔
4260
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
64,394✔
4261
  }
4262

4263
_exit:
11,496✔
4264

4265
  if (code) {
75,890✔
4266
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4267
  }
4268

4269
  return code;
75,890✔
4270
}
4271

4272
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
67,551✔
4273
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
67,551✔
4274
  int32_t lino = 0;
67,551✔
4275
  int32_t vgId = 0;
67,551✔
4276
  int64_t streamId = pTask->streamId;
67,551✔
4277
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
67,551✔
4278
  int32_t tbNum = taosArrayGetSize(pTbs);
67,551✔
4279
  SStreamDbTableName* pName = NULL;
67,551✔
4280
  SSHashObj* pDbVgroups = NULL;
67,551✔
4281
  SStreamMgmtRsp rsp = {0};
67,551✔
4282
  rsp.reqId = pTask->pMgmtReq->reqId;
67,551✔
4283
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
67,551✔
4284
  int32_t iter = 0;
67,551✔
4285
  void* p = NULL;
67,551✔
4286
  SSHashObj* pVgs = NULL;
67,551✔
4287
  SStreamMgmtReq* pMgmtReq = NULL;
67,551✔
4288
  int8_t stopped = 0;
67,551✔
4289

4290
  if (NULL == pCtx->pRsp->rsps.rspList) {
67,551✔
4291
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4292
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4293
  }
4294
  
4295
  TSWAP(pTask->pMgmtReq, pMgmtReq);
67,551✔
4296
  rsp.task = *(SStreamTask*)pTask;
67,551✔
4297

4298
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
67,551✔
4299
  if (NULL == pStatus) {
67,551✔
4300
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4301
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4302
  }
4303

4304
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
67,551✔
4305
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
799✔
4306
    goto _exit;
799✔
4307
  }
4308

4309
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
66,752✔
4310

4311
  stopped = atomic_load_8(&pStatus->stopped);
66,752✔
4312
  if (stopped) {
66,752✔
4313
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4314
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4315
  }
4316

4317
  if (tbNum <= 0) {
66,752✔
4318
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4319
    goto _exit;
×
4320
  }
4321

4322
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
66,752✔
4323
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
66,752✔
4324
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
66,752✔
4325

4326
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
66,752✔
4327
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
66,752✔
4328
  
4329
  for (int32_t i = 0; i < tbNum; ++i) {
262,155✔
4330
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
195,403✔
4331
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
195,403✔
4332
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
390,806✔
4333
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
195,403✔
4334
  }
4335

4336
  int32_t vgNum = tSimpleHashGetSize(pVgs);
66,752✔
4337
  while (true) {
4338
    p = tSimpleHashIterate(pVgs, p, &iter);
142,642✔
4339
    if (NULL == p) {
142,642✔
4340
      break;
66,752✔
4341
    }
4342
    
4343
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
75,890✔
4344
  }
4345
  
4346
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
66,752✔
4347
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
66,752✔
4348
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
66,752✔
4349

4350
  SStreamTaskAddr addr;
66,752✔
4351
  for (int32_t i = 0; i < vgNum; ++i) {
131,146✔
4352
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
64,394✔
4353
    addr.taskId = pOTask->id.taskId;
64,394✔
4354
    addr.nodeId = pOTask->id.nodeId;
64,394✔
4355
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
64,394✔
4356
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
128,788✔
4357
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
64,394✔
4358
  }
4359

4360
_exit:
67,551✔
4361

4362
  tFreeSStreamMgmtReq(pMgmtReq);
67,551✔
4363
  taosMemoryFree(pMgmtReq);
67,551✔
4364

4365
  tSimpleHashCleanup(pVgs);
67,551✔
4366
  mstDestroyDbVgroupsHash(pDbVgroups);
67,551✔
4367

4368
  if (code) {
67,551✔
4369
    rsp.code = code;
×
4370
    
4371
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4372

4373
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4374
  } else {
4375
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
135,102✔
4376
  }
4377

4378
_final:
67,551✔
4379

4380
  if (finalCode) {
67,551✔
4381
    tFreeSStreamMgmtRsp(&rsp);
×
4382
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4383
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4384
  }
4385

4386
  return finalCode;
67,551✔
4387
}
4388

4389
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,618✔
4390
  int32_t num = taosArrayGetSize(pList);
2,618✔
4391
  SStreamCalcScan* pScan = NULL;
2,618✔
4392
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,618✔
4393
  int64_t planUid = 0;
2,618✔
4394
  for (int32_t i = 0; i < num; ++i) {
2,618✔
4395
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,618✔
4396
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,618✔
4397
    if (0 != planUid && planUid == uid) {
2,618✔
4398
      *ppRes = pScan;
2,618✔
4399
      break;
2,618✔
4400
    }
4401
  }
4402

4403
_exit:
2,618✔
4404

4405
  if (code) {
2,618✔
4406
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4407
  }
4408

4409
  return code;
2,618✔
4410
}
4411

4412
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,618✔
4413
  int32_t code = TSDB_CODE_SUCCESS;
2,618✔
4414
  int32_t lino = 0;
2,618✔
4415
  bool    readerExists = false;
2,618✔
4416
  int64_t streamId = pTask->streamId;
2,618✔
4417
  SListNode* pNode = listHead(pStatus->calcReaders);
2,618✔
4418
  SStmTaskStatus* pReader = NULL;
2,618✔
4419
  int32_t taskIdx = 0;
2,618✔
4420

4421
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,618✔
4422
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
24,276✔
4423
    pReader = (SStmTaskStatus*)pNode->data;
21,658✔
4424
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
21,658✔
4425
      readerExists = true;
×
4426
      pAddr->taskId = pReader->id.taskId;
×
4427
      break;
×
4428
    }
4429
  }
4430

4431
  if (!readerExists) {
2,618✔
4432
    if (NULL == pStatus->calcReaders) {
2,618✔
4433
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4434
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4435
      taskIdx = 0;
×
4436
    } else {
4437
      pNode = listTail(pStatus->calcReaders);
2,618✔
4438
      pReader = (SStmTaskStatus*)pNode->data;
2,618✔
4439
      taskIdx = pReader->id.taskIdx + 1;
2,618✔
4440
    }
4441

4442
    SStreamCalcScan* pScan = NULL;
2,618✔
4443
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,618✔
4444
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,618✔
4445
    pReader = tdListReserve(pStatus->calcReaders);
2,618✔
4446
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,618✔
4447
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,618✔
4448
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,618✔
4449
    pAddr->taskId = pReader->id.taskId;
2,618✔
4450
  }
4451

4452
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,618✔
4453
  pAddr->nodeId = vgId;
2,618✔
4454

4455
_exit:
2,618✔
4456

4457
  if (code) {
2,618✔
4458
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4459
  }
4460

4461
  return code;
2,618✔
4462
}
4463

4464

4465
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,618✔
4466
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,618✔
4467
  int32_t lino = 0;
2,618✔
4468
  int32_t vgId = 0;
2,618✔
4469
  int64_t streamId = pTask->streamId;
2,618✔
4470
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,618✔
4471
  int32_t reqNum = taosArrayGetSize(pReqs);
2,618✔
4472
  SStreamOReaderDeployReq* pReq = NULL;
2,618✔
4473
  SStreamOReaderDeployRsp* pRsp = NULL;
2,618✔
4474
  SStreamMgmtRsp rsp = {0};
2,618✔
4475
  rsp.reqId = pTask->pMgmtReq->reqId;
2,618✔
4476
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,618✔
4477
  SStreamMgmtReq* pMgmtReq = NULL;
2,618✔
4478
  int8_t stopped = 0;
2,618✔
4479
  int32_t vgNum = 0;
2,618✔
4480
  SStreamTaskAddr* pAddr = NULL;
2,618✔
4481

4482
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,618✔
4483
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4484
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4485
  }
4486
  
4487
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,618✔
4488
  rsp.task = *(SStreamTask*)pTask;
2,618✔
4489

4490
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,618✔
4491
  if (NULL == pStatus) {
2,618✔
4492
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4493
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4494
  }
4495

4496
  stopped = atomic_load_8(&pStatus->stopped);
2,618✔
4497
  if (stopped) {
2,618✔
4498
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4499
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4500
  }
4501

4502
  if (reqNum <= 0) {
2,618✔
4503
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4504
    goto _exit;
×
4505
  }
4506

4507
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,618✔
4508
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,618✔
4509

4510
  for (int32_t i = 0; i < reqNum; ++i) {
5,236✔
4511
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,618✔
4512
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,618✔
4513
    pRsp->execId = pReq->execId;
2,618✔
4514
    vgNum = taosArrayGetSize(pReq->vgIds);
2,618✔
4515
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,618✔
4516
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,618✔
4517
    
4518
    for (int32_t n = 0; n < vgNum; ++n) {
5,236✔
4519
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,618✔
4520
      pAddr = taosArrayGet(pRsp->vgList, n);
2,618✔
4521
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,618✔
4522
    }
4523
  }
4524

4525
_exit:
2,618✔
4526

4527
  tFreeSStreamMgmtReq(pMgmtReq);
2,618✔
4528
  taosMemoryFree(pMgmtReq);
2,618✔
4529

4530
  if (code) {
2,618✔
4531
    rsp.code = code;
×
4532
    
4533
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4534
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4535
  } else {
4536
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
5,236✔
4537
  }
4538

4539

4540
_final:
2,618✔
4541

4542
  if (finalCode) {
2,618✔
4543
    tFreeSStreamMgmtRsp(&rsp);
×
4544
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4545
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4546
  }
4547

4548
  return finalCode;
2,618✔
4549
}
4550

4551

4552
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
70,169✔
4553
  int32_t code = TSDB_CODE_SUCCESS;
70,169✔
4554
  int32_t lino = 0;
70,169✔
4555

4556
  switch (pTask->pMgmtReq->type) {
70,169✔
4557
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
67,551✔
4558
      msmDeployTriggerOrigReader(pCtx, pTask);
67,551✔
4559
      break;
67,551✔
4560
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,618✔
4561
      msmDeployRunnerOrigReader(pCtx, pTask);
2,618✔
4562
      break;
2,618✔
4563
    default:
×
4564
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4565
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4566
      break;
×
4567
  }
4568

4569
_exit:
70,169✔
4570

4571
  if (code) {
70,169✔
4572
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4573
  }
4574

4575
  return code;
70,169✔
4576
}
4577

4578
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
24,988✔
4579
  int32_t code = TSDB_CODE_SUCCESS;
24,988✔
4580
  int32_t lino = 0;
24,988✔
4581
  SStreamHbMsg* pReq = pCtx->pReq;
24,988✔
4582
  SStmTaskStatusMsg* pTask = NULL;
24,988✔
4583
  
4584
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
24,988✔
4585
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
24,988✔
4586
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
24,988✔
4587
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
24,988✔
4588
  }
4589
  
4590
  for (int32_t i = 0; i < reqNum; ++i) {
95,157✔
4591
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
70,169✔
4592
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
70,169✔
4593
    if (NULL == pTask) {
70,169✔
4594
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4595
      continue;
×
4596
    }
4597

4598
    if (NULL == pTask->pMgmtReq) {
70,169✔
4599
      msttError("idx %d without mgmtReq", idx);
×
4600
      continue;
×
4601
    }
4602

4603
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
70,169✔
4604
  }
4605

4606
_exit:
24,988✔
4607

4608
  if (code) {
24,988✔
4609
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4610
  }
4611

4612
  return code;
24,988✔
4613
}
4614

4615
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
18,584,866✔
4616
  int32_t code = TSDB_CODE_SUCCESS;
18,584,866✔
4617
  int32_t lino = 0;
18,584,866✔
4618
  SStreamHbMsg* pReq = pCtx->pReq;
18,584,866✔
4619

4620
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
18,584,866✔
4621
  if (GOT_SNODE(pReq->snodeId)) {
18,425,871✔
4622
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,744,172✔
4623
  }
4624
  
4625
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
18,425,516✔
4626
    msmHandleStreamActions(pCtx);
73,933✔
4627
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
73,933✔
4628
  }
4629

4630
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
18,425,516✔
4631
    code = msmHandleStreamRequests(pCtx);
24,988✔
4632
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
24,988✔
4633
    TAOS_CHECK_EXIT(code);
24,988✔
4634
  }
4635

4636
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
18,425,516✔
4637
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
106,973✔
4638
  } else {
4639
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
18,318,543✔
4640
  }
4641

4642
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
18,425,516✔
4643
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
145,454✔
4644
  }
4645

4646
  if (taosHashGetSize(pCtx->deployStm) > 0) {
18,425,516✔
4647
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
99,678✔
4648
  }
4649

4650
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
18,425,516✔
4651
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,566,196✔
4652
  }
4653

4654
  if (taosHashGetSize(pCtx->actionStm) > 0) {
18,425,516✔
4655
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
160,624✔
4656
  }
4657

4658
_exit:
18,425,516✔
4659

4660
  if (code) {
18,584,866✔
4661
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
159,350✔
4662
  }
4663

4664
  return code;
18,584,866✔
4665
}
4666

4667
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
18,675,991✔
4668
  int32_t lino = 0;
18,675,991✔
4669
  int32_t tlen = 0;
18,675,991✔
4670
  void   *buf = NULL;
18,675,991✔
4671

4672
  if (TSDB_CODE_SUCCESS != code) {
18,675,991✔
4673
    goto _exit;
159,350✔
4674
  }
4675

4676
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
18,516,641✔
4677
  if (code < 0) {
18,516,641✔
4678
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4679
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4680
  }
4681

4682
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
18,516,641✔
4683
  if (buf == NULL) {
18,516,641✔
4684
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4685
    TAOS_CHECK_EXIT(terrno);    
×
4686
  }
4687

4688
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
18,516,641✔
4689
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
18,516,641✔
4690

4691
  SEncoder encoder;
18,513,744✔
4692
  tEncoderInit(&encoder, abuf, tlen);
18,516,641✔
4693
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
18,516,641✔
4694
    rpcFreeCont(buf);
×
4695
    buf = NULL;
×
4696
    tEncoderClear(&encoder);
×
4697
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4698
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4699
  }
4700
  tEncoderClear(&encoder);
18,516,641✔
4701

4702
_exit:
18,675,991✔
4703

4704
  pMsg->code = code;
18,675,991✔
4705
  pMsg->info = *pRpcInfo;
18,675,991✔
4706
  if (TSDB_CODE_SUCCESS == code) {
18,675,991✔
4707
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
18,516,641✔
4708
    pMsg->pCont = buf;
18,516,641✔
4709
  }
4710
}
18,675,991✔
4711

4712

4713
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
18,675,991✔
4714
  int32_t code = TSDB_CODE_SUCCESS;
18,675,991✔
4715
  SMStreamHbRspMsg rsp = {0};
18,675,991✔
4716
  rsp.streamGId = pHb->streamGId;
18,675,991✔
4717

4718
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
18,675,991✔
4719

4720
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
18,675,991✔
4721
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
78,893✔
4722
    goto _exit;
78,893✔
4723
  }
4724

4725
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
18,597,098✔
4726
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
18,597,098✔
4727

4728
  pCtx->tidx = tidx;
18,597,098✔
4729
  pCtx->pMnode = pMnode;
18,597,098✔
4730
  pCtx->currTs = currTs;
18,597,098✔
4731
  pCtx->pReq = pHb;
18,597,098✔
4732
  pCtx->pRsp = &rsp;
18,597,098✔
4733
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
18,597,098✔
4734
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
18,597,098✔
4735
  
4736
  switch (atomic_load_8(&mStreamMgmt.state)) {
18,597,098✔
4737
    case MND_STM_STATE_WATCH:
12,232✔
4738
      code = msmWatchHandleHbMsg(pCtx);
12,232✔
4739
      break;
12,232✔
4740
    case MND_STM_STATE_NORMAL:
18,584,866✔
4741
      code = msmNormalHandleHbMsg(pCtx);
18,584,866✔
4742
      break;
18,584,866✔
4743
    default:
×
4744
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4745
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4746
      break;
×
4747
  }
4748

4749
_exit:
18,675,991✔
4750

4751
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
18,675,991✔
4752

4753
  msmCleanStreamGrpCtx(pHb);
18,675,991✔
4754
  msmClearStreamToDeployMaps(pHb);
18,675,991✔
4755

4756
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
18,675,991✔
4757
  
4758
  tFreeSMStreamHbRspMsg(&rsp);
18,675,991✔
4759

4760
  return code;
18,675,991✔
4761
}
4762

4763
void msmHandleBecomeLeader(SMnode *pMnode) {
454,813✔
4764
  if (tsDisableStream) {
454,813✔
4765
    return;
×
4766
  }
4767

4768
  mstInfo("start to process mnode become leader");
454,813✔
4769

4770
  int32_t code = 0;
454,813✔
4771
  streamAddVnodeLeader(MNODE_HANDLE);
454,813✔
4772
  
4773
  taosWLockLatch(&mStreamMgmt.runtimeLock);
454,813✔
4774
  msmDestroyRuntimeInfo(pMnode);
454,813✔
4775
  code = msmInitRuntimeInfo(pMnode);
454,813✔
4776
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
454,813✔
4777

4778
  if (TSDB_CODE_SUCCESS == code) {
454,813✔
4779
    atomic_store_8(&mStreamMgmt.active, 1);
454,813✔
4780
  }
4781

4782
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
454,813✔
4783
}
4784

4785
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
555,027✔
4786
  if (tsDisableStream) {
555,027✔
4787
    return;
×
4788
  }
4789

4790
  mstInfo("start to process mnode become not leader");
555,027✔
4791

4792
  streamRemoveVnodeLeader(MNODE_HANDLE);
555,027✔
4793

4794
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
555,027✔
4795
    taosWLockLatch(&mStreamMgmt.runtimeLock);
454,813✔
4796
    msmDestroyRuntimeInfo(pMnode);
454,813✔
4797
    mStreamMgmt.stat.inactiveTimes++;
454,813✔
4798
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
454,813✔
4799
  }
4800
}
4801

4802

4803
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4804
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4805
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4806
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4807
  } else {
4808
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4809
  }
4810
}
×
4811

4812
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
8,601✔
4813
  int32_t code = TSDB_CODE_SUCCESS;
8,601✔
4814
  int32_t lino = 0;
8,601✔
4815
  SStreamObj* pStream = pObj;
8,601✔
4816
  SSnodeObj* pSnode = p1;
8,601✔
4817
  SArray** ppRes = p2;
8,601✔
4818

4819
  if (pStream->mainSnodeId == pSnode->id) {
8,601✔
4820
    if (NULL == *ppRes) {
5,650✔
4821
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
910✔
4822
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
910✔
4823
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
910✔
4824
    }
4825

4826
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
11,300✔
4827
  }
4828

4829
  return true;
8,601✔
4830

4831
_exit:
×
4832

4833
  if (code) {
×
4834
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4835
  }  
4836

4837
  *(int32_t*)p3 = code;
×
4838

4839
  return false;
×
4840
}
4841

4842

4843
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
42,655✔
4844
  int32_t code = TSDB_CODE_SUCCESS;
42,655✔
4845
  int32_t lino = 0;
42,655✔
4846
  
4847
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
42,655✔
4848
  TAOS_CHECK_EXIT(code);
42,655✔
4849

4850
  int32_t streamNum = taosArrayGetSize(*ppRes);
42,655✔
4851
  if (streamNum > 0 && 0 == pSnode->replicaId) {
42,655✔
4852
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
223✔
4853
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
223✔
4854
  }
4855

4856
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4857

4858
_exit:
42,655✔
4859

4860
  if (code) {
42,655✔
4861
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
223✔
4862
  }  
4863

4864
  return code;
42,655✔
4865
}
4866

4867
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
713,775✔
4868
  SStreamObj* pStream = pObj;
713,775✔
4869
  int64_t streamId = pStream->pCreate->streamId;
713,775✔
4870
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
713,775✔
4871
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
713,775✔
4872
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
713,775✔
4873
  
4874
  if ((userDropped || userStopped) && (NULL == pStatus)) {
713,775✔
4875
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
223✔
4876
    return true;
223✔
4877
  }
4878
  
4879
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
713,552✔
4880
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
554,066✔
4881
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4882
    return true;
554,066✔
4883
  }
4884

4885
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
159,486✔
4886
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
69,343✔
4887
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4888
    return true;
69,343✔
4889
  }  
4890

4891
  if (pStatus) {
90,143✔
4892
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
89,460✔
4893
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4894
    }
4895

4896
    return true;
89,460✔
4897
  }
4898

4899
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
683✔
4900

4901
  return true;
683✔
4902
}
4903

4904
void msmCheckLoopStreamMap(SMnode *pMnode) {
55,739✔
4905
  SStmStatus* pStatus = NULL;
55,739✔
4906
  void* pIter = NULL;
55,739✔
4907
  int8_t stopped = 0;
55,739✔
4908
  int64_t streamId = 0;
55,739✔
4909
  
4910
  while (true) {
4911
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
268,734✔
4912
    if (NULL == pIter) {
268,734✔
4913
      break;
55,739✔
4914
    }
4915

4916
    pStatus = (SStmStatus*)pIter;
212,995✔
4917

4918
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
212,995✔
4919
    stopped = atomic_load_8(&pStatus->stopped);
212,995✔
4920
    if (MST_IS_USER_STOPPED(stopped)) {
212,995✔
4921
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
473✔
4922
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
473✔
4923
      continue;
473✔
4924
    }
4925

4926
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
212,522✔
4927
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4928
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4929
      continue;
×
4930
    }
4931

4932
    if (MST_IS_ERROR_STOPPED(stopped)) {
212,522✔
4933
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
16,632✔
4934
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
11,088✔
4935
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4936
            
4937
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
11,088✔
4938
        continue;
11,088✔
4939
      }
4940

4941
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
5,544✔
4942
      continue;
5,544✔
4943
    }
4944

4945
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
195,890✔
4946
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4947
      continue;
×
4948
    }
4949
  }
4950
}
55,739✔
4951

4952
void msmCheckStreamsStatus(SMnode *pMnode) {
519,338✔
4953
  SStmCheckStatusCtx ctx = {0};
519,338✔
4954

4955
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
519,338✔
4956
  
4957
  if (MST_READY_FOR_SDB_LOOP()) {
519,338✔
4958
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
122,988✔
4959
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
122,988✔
4960
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
122,988✔
4961
  }
4962

4963
  if (MST_READY_FOR_MAP_LOOP()) {
519,338✔
4964
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
55,739✔
4965
    msmCheckLoopStreamMap(pMnode);
55,739✔
4966
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
55,739✔
4967
  }
4968
}
519,338✔
4969

4970
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
5,710,731✔
4971
  for (int32_t i = 0; i < taskNum; ++i) {
12,059,072✔
4972
    SStmTaskStatus* pTask = *(pList + i);
6,348,341✔
4973

4974
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
6,348,341✔
4975
      continue;
652,030✔
4976
    }
4977
    
4978
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
5,698,025✔
4979
      continue;
5,696,340✔
4980
    }
4981

4982
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,685✔
4983
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,685✔
4984
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4985
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4986
          
4987
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
4988
      break;
×
4989
    }
4990

4991
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
1,685✔
4992
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4993

4994
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,685✔
4995
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,685✔
4996

4997
    SStmTaskAction task = {0};
1,685✔
4998
    task.streamId = streamId;
1,685✔
4999
    task.id = pTask->id;
1,685✔
5000
    task.flag = pTask->flags;
1,685✔
5001
    task.type = pTask->type;
1,685✔
5002
    
5003
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,685✔
5004
  }
5005
}
5,710,731✔
5006

5007
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
200,950✔
5008
  void* pIter = NULL;
200,950✔
5009
  SStmVgStreamStatus* pVg = NULL;
200,950✔
5010
  int64_t streamId = 0;
200,950✔
5011
  
5012
  while (true) {
2,288,551✔
5013
    pIter = taosHashIterate(pStreams, pIter);
2,489,501✔
5014
    if (NULL == pIter) {
2,489,501✔
5015
      break;
200,950✔
5016
    }
5017

5018
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,288,551✔
5019
    pVg = (SStmVgStreamStatus*)pIter;
2,288,551✔
5020

5021
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
2,288,551✔
5022
    if (taskNum > 0) {
2,288,551✔
5023
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,351,642✔
5024
    }
5025

5026
    taskNum = taosArrayGetSize(pVg->calcReaders);
2,288,551✔
5027
    if (taskNum > 0) {
2,288,551✔
5028
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,513,703✔
5029
    }
5030
  }
5031
}
200,950✔
5032

5033
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
5034
  int64_t streamId = 0;
×
5035
  void* pIter = NULL;
×
5036
  SStmVgStreamStatus* pStream = NULL;
×
5037

5038
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
5039
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5040
    return;
×
5041
  }
5042

5043
  
5044
  while (true) {
5045
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
5046
    if (NULL == pIter) {
×
5047
      break;
×
5048
    }
5049

5050
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
5051
    
5052
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
5053
  }
5054

5055
  taosHashClear(pVg->streamTasks);
×
5056
}
5057

5058

5059
void msmCheckVgroupStatus(SMnode *pMnode) {
519,338✔
5060
  void* pIter = NULL;
519,338✔
5061
  int32_t code = 0;
519,338✔
5062
  
5063
  while (true) {
1,890,222✔
5064
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
2,409,560✔
5065
    if (NULL == pIter) {
2,409,560✔
5066
      break;
519,338✔
5067
    }
5068

5069
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,890,222✔
5070
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,890,222✔
5071
      continue;
1,684,570✔
5072
    }
5073
    
5074
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
205,652✔
5075

5076
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
205,652✔
5077
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
4,702✔
5078
      if (NULL == pVgroup) {
4,702✔
5079
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
4,702✔
5080
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
4,702✔
5081
        if (code) {
4,702✔
5082
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
5083
        }
5084
        continue;
4,702✔
5085
      }
5086
      mndReleaseVgroup(pMnode, pVgroup);
×
5087
      
5088
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5089
      
5090
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
5091
      continue;
×
5092
    }
5093

5094
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
200,950✔
5095

5096
    msmCheckVgroupStreamStatus(pVg->streamTasks);
200,950✔
5097
  }
5098
}
519,338✔
5099

5100
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
1,334✔
5101
  *deployNum = 0;
1,334✔
5102
  
5103
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
4,204✔
5104
    if (pStream->runners[i]) {
3,719✔
5105
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
1,334✔
5106
      for (int32_t t = 0; t < taskNum; ++t) {
1,819✔
5107
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
1,334✔
5108
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
1,334✔
5109
        if (stopped) {
1,334✔
5110
          mstsDebug("stream already stopped %d, ignore it", stopped);
849✔
5111
          *deployNum = 0;
849✔
5112
          return;
849✔
5113
        }
5114

5115
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
485✔
5116
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
485✔
5117
      }
5118
      
5119
      deployId[*deployNum] = i;
485✔
5120
      (*deployNum)++;
485✔
5121
    }
5122
  }
5123
}
5124

5125
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
3,395✔
5126
  pSnode->runnerThreadNum = -1;
3,395✔
5127

5128
  (void)msmSTAddSnodesToMap(pMnode);
3,395✔
5129

5130
  int64_t streamId = 0;
3,395✔
5131
  void* pIter = NULL;
3,395✔
5132
  SStmSnodeStreamStatus* pStream = NULL;
3,395✔
5133
  int32_t deployNum = 0;
3,395✔
5134
  SStmTaskAction task = {0};
3,395✔
5135
  
5136
  while (true) {
5137
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
6,184✔
5138
    if (NULL == pIter) {
6,184✔
5139
      break;
3,395✔
5140
    }
5141

5142
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,789✔
5143
    
5144
    task.streamId = streamId;
2,789✔
5145
    
5146
    pStream = (SStmSnodeStreamStatus*)pIter;
2,789✔
5147
    if (pStream->trigger) {
2,789✔
5148
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
1,455✔
5149
      if (stopped) {
1,455✔
5150
        mstsDebug("stream already stopped %d, ignore it", stopped);
1,051✔
5151
        continue;
1,051✔
5152
      }
5153

5154
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
404✔
5155
      
5156
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
404✔
5157
    } else {
5158
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
1,334✔
5159
      
5160
      if (task.deployNum > 0) {
1,334✔
5161
        //task.triggerStatus = pStream->trigger;
5162
        task.multiRunner = true;
485✔
5163
        task.type = STREAM_RUNNER_TASK;
485✔
5164
        
5165
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
485✔
5166
        
5167
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
485✔
5168
      }
5169
    }
5170
  }
5171

5172
  taosHashClear(pSnode->streamTasks);
3,395✔
5173
}
3,395✔
5174

5175

5176
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
58,055✔
5177
  void* pIter = NULL;
58,055✔
5178
  SStmSnodeStreamStatus* pSnode = NULL;
58,055✔
5179
  int64_t streamId = 0;
58,055✔
5180
  
5181
  while (true) {
5182
    pIter = taosHashIterate(pStreams, pIter);
805,094✔
5183
    if (NULL == pIter) {
805,094✔
5184
      break;
58,055✔
5185
    }
5186

5187
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
747,039✔
5188
    pSnode = (SStmSnodeStreamStatus*)pIter;
747,039✔
5189

5190
    if (NULL != pSnode->trigger) {
747,039✔
5191
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
730,086✔
5192
    }
5193

5194
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,988,156✔
5195
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
2,241,117✔
5196
      if (taskNum > 0) {
2,241,117✔
5197
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
2,115,300✔
5198
      }
5199
    }
5200
  }
5201
}
58,055✔
5202

5203

5204
void msmCheckSnodeStatus(SMnode *pMnode) {
519,338✔
5205
  void* pIter = NULL;
519,338✔
5206
  
5207
  while (true) {
629,910✔
5208
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
1,149,248✔
5209
    if (NULL == pIter) {
1,149,248✔
5210
      break;
519,338✔
5211
    }
5212

5213
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
629,910✔
5214
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
629,910✔
5215
      continue;
555,058✔
5216
    }
5217

5218
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
74,852✔
5219
    
5220
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
74,852✔
5221
    if (NULL == pSnode->streamTasks) {
74,852✔
5222
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
14,170✔
5223
      continue;
14,170✔
5224
    }
5225
    
5226
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
60,682✔
5227
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
2,627✔
5228
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5229
      
5230
      msmHandleSnodeLost(pMnode, pSnode);
2,627✔
5231
      continue;
2,627✔
5232
    }
5233
    
5234
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
58,055✔
5235

5236
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
58,055✔
5237
  }
5238
}
519,338✔
5239

5240

5241
void msmCheckTasksStatus(SMnode *pMnode) {
519,338✔
5242
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
519,338✔
5243

5244
  msmCheckVgroupStatus(pMnode);
519,338✔
5245
  msmCheckSnodeStatus(pMnode);
519,338✔
5246
}
519,338✔
5247

5248
void msmCheckSnodesState(SMnode *pMnode) {
519,338✔
5249
  if (!MST_READY_FOR_SNODE_LOOP()) {
519,338✔
5250
    return;
468,455✔
5251
  }
5252

5253
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
50,883✔
5254

5255
  void* pIter = NULL;
50,883✔
5256
  int32_t snodeId = 0;
50,883✔
5257
  while (true) {
56,390✔
5258
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
107,273✔
5259
    if (NULL == pIter) {
107,273✔
5260
      break;
50,883✔
5261
    }
5262

5263
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
56,390✔
5264
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
56,390✔
5265
      continue;
55,483✔
5266
    }
5267

5268
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
907✔
5269
    if (NULL == pSnode->streamTasks) {
907✔
5270
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
139✔
5271
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
139✔
5272
      continue;
139✔
5273
    }
5274
    
5275
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
768✔
5276
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5277
    
5278
    msmHandleSnodeLost(pMnode, pSnode);
768✔
5279
  }
5280

5281
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
50,883✔
5282
}
5283

5284
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
11,793,532✔
5285
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
11,793,532✔
5286
  if (0 == active || MND_STM_STATE_NORMAL != state) {
11,793,532✔
5287
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,549✔
5288
    return false;
2,549✔
5289
  }
5290

5291
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
11,790,983✔
5292
    mstTrace("ignore health check since no stream now");
10,752,307✔
5293
    return false;
10,752,307✔
5294
  }
5295

5296
  return true;
1,038,676✔
5297
}
5298

5299
void msmHealthCheck(SMnode *pMnode) {
11,274,194✔
5300
  if (!msmCheckNeedHealthCheck(pMnode)) {
11,274,194✔
5301
    return;
10,754,856✔
5302
  }
5303

5304
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
1,020,896✔
5305
  
5306
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
519,338✔
5307
  if (!msmCheckNeedHealthCheck(pMnode)) {
519,338✔
5308
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5309
    return;
×
5310
  }
5311
  
5312
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
519,338✔
5313
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
519,338✔
5314

5315
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
519,338✔
5316
  
5317
  msmCheckStreamsStatus(pMnode);
519,338✔
5318
  msmCheckTasksStatus(pMnode);
519,338✔
5319
  msmCheckSnodesState(pMnode);
519,338✔
5320

5321
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
519,338✔
5322

5323
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
519,338✔
5324
}
5325

5326
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
880✔
5327
  SStreamObj *pStream = pObj;
880✔
5328
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
880✔
5329
    return true;
×
5330
  }
5331
  
5332
  pStream->updateTime = *(int64_t*)p1;
880✔
5333
  
5334
  (*(int32_t*)p2)++;
880✔
5335
  
5336
  return true;
880✔
5337
}
5338

5339
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
23,179✔
5340
  int32_t code = 0;
23,179✔
5341
  int8_t  stopped = 0;
23,179✔
5342
  
5343
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
23,179✔
5344
  
5345
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
23,179✔
5346
  if (NULL == pStatus) {
23,179✔
5347
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,467✔
5348
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,467✔
5349
    goto _exit;
3,467✔
5350
  }
5351

5352
  stopped = atomic_load_8(&pStatus->stopped);
19,712✔
5353
  if (stopped) {
19,712✔
5354
    mstsError("stream already stopped, stopped:%d", stopped);
×
5355
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5356
    goto _exit;
×
5357
  }
5358

5359
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
19,712✔
5360
    pAddr->taskId = pStatus->triggerTask->id.taskId;
15,744✔
5361
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
15,744✔
5362
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
15,744✔
5363
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
15,744✔
5364
    goto _exit;
15,744✔
5365
  }
5366

5367
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
3,968✔
5368
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,968✔
5369

5370
_exit:
23,179✔
5371
  
5372
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
23,179✔
5373

5374
  return code;
23,179✔
5375
}
5376

5377
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
454,813✔
5378
  int32_t code = TSDB_CODE_SUCCESS;
454,813✔
5379
  int32_t lino = 0;
454,813✔
5380
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
454,813✔
5381
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
454,813✔
5382
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
454,813✔
5383

5384
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
908,633✔
5385

5386
  mStreamMgmt.stat.activeTimes++;
454,813✔
5387
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
454,813✔
5388
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
454,813✔
5389
  if (NULL == mStreamMgmt.tCtx) {
454,813✔
5390
    code = terrno;
×
5391
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5392
    goto _exit;
×
5393
  }
5394

5395
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
454,813✔
5396
  if (mStreamMgmt.actionQ == NULL) {
454,813✔
5397
    code = terrno;
×
5398
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5399
    goto _exit;
×
5400
  }
5401
  
5402
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
454,813✔
5403
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
454,813✔
5404
  
5405
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
454,813✔
5406
  
5407
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,726,255✔
5408
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,271,442✔
5409

5410
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
13,628,652✔
5411
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
11,357,210✔
5412
      if (pCtx->deployStm[m] == NULL) {
11,357,210✔
5413
        code = terrno;
×
5414
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5415
        goto _exit;
×
5416
      }
5417
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
11,357,210✔
5418
      
5419
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
11,357,210✔
5420
      if (pCtx->actionStm[m] == NULL) {
11,357,210✔
5421
        code = terrno;
×
5422
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5423
        goto _exit;
×
5424
      }
5425
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
11,357,210✔
5426
    }
5427
  }
5428
  
5429
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
454,813✔
5430
  if (mStreamMgmt.streamMap == NULL) {
454,813✔
5431
    code = terrno;
×
5432
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5433
    goto _exit;
×
5434
  }
5435
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
454,813✔
5436
  
5437
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
454,813✔
5438
  if (mStreamMgmt.taskMap == NULL) {
454,813✔
5439
    code = terrno;
×
5440
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5441
    goto _exit;
×
5442
  }
5443
  
5444
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
454,813✔
5445
  if (mStreamMgmt.vgroupMap == NULL) {
454,813✔
5446
    code = terrno;
×
5447
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5448
    goto _exit;
×
5449
  }
5450
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
454,813✔
5451

5452
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
454,813✔
5453
  if (mStreamMgmt.snodeMap == NULL) {
454,813✔
5454
    code = terrno;
×
5455
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5456
    goto _exit;
×
5457
  }
5458
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
454,813✔
5459
  
5460
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
454,813✔
5461
  if (mStreamMgmt.dnodeMap == NULL) {
454,813✔
5462
    code = terrno;
×
5463
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5464
    goto _exit;
×
5465
  }
5466

5467
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
454,813✔
5468
  if (mStreamMgmt.toDeployVgMap == NULL) {
454,813✔
5469
    code = terrno;
×
5470
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5471
    goto _exit;
×
5472
  }
5473
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
454,813✔
5474
  
5475
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
454,813✔
5476
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
454,813✔
5477
    code = terrno;
×
5478
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5479
    goto _exit;
×
5480
  }
5481
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
454,813✔
5482

5483
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
454,813✔
5484
  if (mStreamMgmt.toUpdateScanMap == NULL) {
454,813✔
5485
    code = terrno;
×
5486
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5487
    goto _exit;
×
5488
  }
5489
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
454,813✔
5490

5491
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
454,813✔
5492
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
454,813✔
5493

5494
  mStreamMgmt.lastTaskId = 1;
454,813✔
5495

5496
  int32_t activeStreamNum = 0;
454,813✔
5497
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
454,813✔
5498

5499
  if (activeStreamNum > 0) {
454,813✔
5500
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
718✔
5501
  } else {
5502
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
454,095✔
5503
  }
5504

5505
_exit:
454,813✔
5506

5507
  if (code) {
454,813✔
5508
    msmDestroyRuntimeInfo(pMnode);
×
5509
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5510
  } else {
5511
    mstInfo("mnode stream runtime init done");
454,813✔
5512
  }
5513

5514
  return code;
454,813✔
5515
}
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