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

taosdata / TDengine / #5015

03 Apr 2026 03:59PM UTC coverage: 72.289% (+0.03%) from 72.256%
#5015

push

travis-ci

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

4055 of 5985 new or added lines in 68 files covered. (67.75%)

13044 existing lines in 149 files now uncovered.

257390 of 356056 relevant lines covered (72.29%)

130247228.09 hits per line

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

77.33
/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() {
848,824✔
34
  SStmQNode* pQNode = NULL;
848,824✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
848,824✔
37
    return;
424,412✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,117,182✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,703,092✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,585,910✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,585,910✔
51
  }
52
}
2,117,182✔
53

54
void msmDestroyThreadCtxs() {
848,824✔
55
  if (NULL == mStreamMgmt.tCtx) {
848,824✔
56
    return;
424,412✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,541,594✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,117,182✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
424,412✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
848,824✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
848,824✔
94
}
848,824✔
95

96

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

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

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

111
    pStatus = pStream;
552✔
112
  }
113

114
  int8_t stopped = atomic_load_8(&pStatus->stopped);
7,434✔
115
  if (stopped) {
7,434✔
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) {
7,434✔
121
    pStatus->fatalRetryTimes = 0;
792✔
122
    mstsDebug("reset stream retryTimes, running duation:%" PRId64 "ms", currTs - pStatus->triggerTask->runningStartTs);
792✔
123
  }
124

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

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

136
_exit:
×
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
7,434✔
139

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

145

146
static void msmSetInitRuntimeState(int8_t state) {
424,757✔
147
  switch (state) {
424,757✔
148
    case MND_STM_STATE_WATCH:
682✔
149
      mStreamMgmt.watch.ending = 0;
682✔
150
      mStreamMgmt.watch.taskRemains = 0;
682✔
151
      mStreamMgmt.watch.processing = 0;
682✔
152
      mstInfo("switch to WATCH state");
682✔
153
      break;
682✔
154
    case MND_STM_STATE_NORMAL:
424,075✔
155
      MND_STREAM_SET_LAST_TS(STM_EVENT_NORMAL_BEGIN, taosGetTimestampMs());
846,468✔
156
      mstInfo("switch to NORMAL state");
424,075✔
157
      break;
424,075✔
158
    default:
×
159
      return;
×
160
  }
161
  
162
  atomic_store_8(&mStreamMgmt.state, state);
424,757✔
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) {
454,372✔
175
  int32_t code = TSDB_CODE_SUCCESS;
454,372✔
176
  int32_t lino = 0;
454,372✔
177
  SStmSnodeStatus tasks = {0};
454,372✔
178
  SSnodeObj *pSnode = NULL;
454,372✔
179
  void *pIter = NULL;
454,372✔
180
  while (1) {
181
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pSnode);
502,700✔
182
    if (pIter == NULL) {
502,700✔
183
      break;
454,372✔
184
    }
185

186
    tasks.lastUpTs = taosGetTimestampMs();
48,328✔
187
    code = taosHashPut(mStreamMgmt.snodeMap, &pSnode->id, sizeof(pSnode->id), &tasks, sizeof(tasks));
48,328✔
188
    if (code && TSDB_CODE_DUP_KEY != code) {
48,328✔
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;
48,328✔
196
  
197
    sdbRelease(pMnode->pSdb, pSnode);
48,328✔
198
  }
199

200
  pSnode = NULL;
454,372✔
201

202
_exit:
454,372✔
203

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

208
  return code;
454,372✔
209
}
210

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

223
    code = taosHashPut(mStreamMgmt.dnodeMap, &pDnode->id, sizeof(pDnode->id), &lastUpTs, sizeof(lastUpTs));
1,028,328✔
224
    if (code && TSDB_CODE_DUP_KEY != code) {
1,028,328✔
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,028,328✔
232
    sdbRelease(pMnode->pSdb, pDnode);
1,028,328✔
233
  }
234

235
  pDnode = NULL;
941,343✔
236

237
_exit:
941,343✔
238

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

243
  return code;
941,343✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
673,153✔
275
  int32_t code = TSDB_CODE_SUCCESS;
673,153✔
276
  int32_t lino = 0;
673,153✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
673,153✔
278
  if (NULL == pStream) {
673,153✔
279
    SStmVgStreamStatus stream = {0};
478,573✔
280
    if (trigReader) {
478,573✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
282,211✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
282,211✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
564,422✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
196,362✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
196,362✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
392,724✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
478,573✔
290
    goto _exit;
478,573✔
291
  }
292
  
293
  if (trigReader) {
194,580✔
294
    if (NULL == pStream->trigReaders) {
23,577✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
23,577✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
23,577✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
47,154✔
300
    goto _exit;
23,577✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
171,003✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
112,833✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
112,833✔
306
  }
307

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

310
_exit:
171,003✔
311

312
  if (code) {
673,153✔
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", 
673,153✔
317
        trigReader ? "trigReader" : "calcReader", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId);
318
  }
319

320
  return code;
673,153✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
85,776✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
85,776✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
85,776✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
587,377✔
339
  }
340
  
341
_exit:
673,007✔
342

343
  if (code) {
673,153✔
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);
673,153✔
348
  }
349

350
  return code;
673,153✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
674,455✔
363
    if (NULL == pVg) {
674,455✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
170,264✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
170,264✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
340,528✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
170,264✔
368
      if (TSDB_CODE_SUCCESS == code) {
170,264✔
369
        goto _return;
170,264✔
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);
504,191✔
381
    if (NULL == pVg->taskList) {
504,191✔
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,008,382✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
504,191✔
390
    
391
    taosHashRelease(pVgMap, pVg);
504,191✔
392
    break;
504,191✔
393
  }
394
  
395
_return:
674,455✔
396

397
  if (code) {
674,455✔
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);
674,455✔
401
    msttDebug("task added to toDeployVgTaskNum, vgToDeployTaskNum:%d", num);
674,455✔
402
  }
403

404
  return code;
674,455✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
889,917✔
409
  int32_t code = TSDB_CODE_SUCCESS;
889,917✔
410
  int32_t lino = 0;
889,917✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
889,917✔
412
  if (NULL == pStream) {
889,917✔
413
    SStmSnodeStreamStatus stream = {0};
206,848✔
414
    if (deployId < 0) {
206,848✔
415
      stream.trigger = pStatus;
3,318✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
203,530✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
203,530✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
407,060✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
206,848✔
423
    goto _exit;
206,848✔
424
  }
425
  
426
  if (deployId < 0) {
683,069✔
427
    if (NULL != pStream->trigger) {
195,623✔
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;
195,623✔
433
    goto _exit;
195,623✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
487,446✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
384,822✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
384,822✔
439
  }
440

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

443
_exit:
487,446✔
444

445
  if (code) {
889,917✔
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", 
889,917✔
450
        (deployId < 0) ? "trigger" : "runner", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId, deployId);
451
  }
452

453
  return code;
889,917✔
454
}
455

456

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

461
  SStmSnodeStatus* pSnode = taosHashGet(mStreamMgmt.snodeMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId));
889,917✔
462
  if (NULL == pSnode) {
889,917✔
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) {
889,917✔
467
      pSnode->streamTasks = taosHashInit(2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
25,424✔
468
      TSDB_CHECK_NULL(pSnode->streamTasks, code, lino, _exit, terrno);
25,424✔
469
      taosHashSetFreeFp(pSnode->streamTasks, mstDestroySStmSnodeStreamStatus);
25,424✔
470
    }
471
    
472
    TAOS_CHECK_EXIT(msmSTAddToSnodeStreamHash(pSnode->streamTasks, streamId, pStatus, deployId));
889,917✔
473
  }
474
  
475
_exit:
889,917✔
476

477
  if (code) {
889,917✔
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", 
889,917✔
481
        pStatus->id.taskId, pStatus->id.taskIdx, pStatus->id.nodeId);
482
  }
483

484
  return code;
889,917✔
485
}
486

487

488

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

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

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

507
      code = taosHashPut(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &snode, sizeof(snode));
234✔
508
      if (TSDB_CODE_SUCCESS == code) {
234✔
509
        goto _return;
234✔
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);
198,707✔
521
    if (NULL == pSnode->triggerList) {
198,707✔
522
      pSnode->triggerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
53,055✔
523
      if (NULL == pSnode->triggerList) {
53,055✔
524
        taosWUnLockLatch(&pSnode->lock);
×
525
        TSDB_CHECK_NULL(pSnode->triggerList, code, lino, _return, terrno);
×
526
      }
527
    }
528
    
529
    ext.deploy = *pDeploy;
198,707✔
530
    ext.deployed = false;
198,707✔
531
    
532
    if (NULL == taosArrayPush(pSnode->triggerList, &ext)) {
397,414✔
533
      taosWUnLockLatch(&pSnode->lock);
×
534
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
535
    }
536
    taosWUnLockLatch(&pSnode->lock);
198,707✔
537
    
538
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
198,707✔
539
    break;
198,707✔
540
  }
541
  
542
_return:
198,941✔
543

544
  if (code) {
198,941✔
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);
198,941✔
548
  }
549

550
  return code;
198,941✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
55,185✔
568
      ext.deployed = false;
55,185✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
110,370✔
570

571
      code = taosHashPut(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &snode, sizeof(snode));
55,185✔
572
      if (TSDB_CODE_SUCCESS == code) {
55,185✔
573
        goto _return;
55,185✔
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);
635,791✔
585
    if (NULL == pSnode->runnerList) {
635,791✔
586
      pSnode->runnerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
1,776✔
587
      if (NULL == pSnode->runnerList) {
1,776✔
588
        taosWUnLockLatch(&pSnode->lock);
×
589
        TSDB_CHECK_NULL(pSnode->runnerList, code, lino, _return, terrno);
×
590
      }
591
    }
592
    
593
    ext.deploy = *pDeploy;
635,791✔
594
    ext.deployed = false;
635,791✔
595
    
596
    if (NULL == taosArrayPush(pSnode->runnerList, &ext)) {
1,271,582✔
597
      taosWUnLockLatch(&pSnode->lock);
×
598
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
599
    }
600
    taosWUnLockLatch(&pSnode->lock);
635,791✔
601
    
602
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
635,791✔
603
    break;
635,791✔
604
  }
605
  
606
_return:
690,976✔
607

608
  if (code) {
690,976✔
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);
690,976✔
612
  }
613

614
  return code;
690,976✔
615
}
616

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

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

632
_exit:
628,888✔
633

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

638
  return code;
628,888✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
16,115✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
718✔
682

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

687
  return code;  
718✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
96,639✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,530,515✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
68,276,485✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
51,745,970✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
51,745,970✔
725
  }
726

727
_exit:
16,530,515✔
728

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

733
  return code;
16,530,515✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
370,807✔
739
  int32_t num = taosArrayGetSize(pList);
370,807✔
740
  for (int32_t i = 0; i < num; ++i) {
1,149,277✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
955,261✔
742
    if (pScan->readFromCache) {
955,261✔
743
      return pScan->scanPlan;
176,791✔
744
    }
745
  }
746

747
  return NULL;
194,016✔
748
}
749

750
int32_t msmBuildReaderDeployInfo(SStmTaskDeploy* pDeploy, void* calcScanPlan, SStmStatus* pInfo, bool triggerReader) {
674,455✔
751
  SStreamReaderDeployMsg* pMsg = &pDeploy->msg.reader;
674,455✔
752
  pMsg->triggerReader = triggerReader;
674,455✔
753
  
754
  if (triggerReader) {
674,455✔
755
    SStreamReaderDeployFromTrigger* pTrigger = &pMsg->msg.trigger;
306,538✔
756
    pTrigger->triggerTblName = pInfo->pCreate->triggerTblName;
306,538✔
757
    pTrigger->triggerTblUid = pInfo->pCreate->triggerTblUid;
306,538✔
758
    pTrigger->triggerTblSuid = pInfo->pCreate->triggerTblSuid;
306,538✔
759
    pTrigger->triggerTblType = pInfo->pCreate->triggerTblType;
306,538✔
760
    pTrigger->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pInfo->pCreate->triggerTblType, pInfo->pCreate->flags);
306,538✔
761
    pTrigger->deleteReCalc = pInfo->pCreate->deleteReCalc;
306,538✔
762
    pTrigger->deleteOutTbl = pInfo->pCreate->deleteOutTbl;
306,538✔
763
    pTrigger->partitionCols = pInfo->pCreate->partitionCols;
306,538✔
764
    pTrigger->triggerCols = pInfo->pCreate->triggerCols;
306,538✔
765
    //pTrigger->triggerPrevFilter = pStream->pCreate->triggerPrevFilter;
766
    pTrigger->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
306,538✔
767
    pTrigger->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
306,538✔
768
  } else {
769
    SStreamReaderDeployFromCalc* pCalc = &pMsg->msg.calc;
367,917✔
770
    pCalc->execReplica = pInfo->runnerDeploys * pInfo->runnerReplica;
367,917✔
771
    pCalc->calcScanPlan = calcScanPlan;
367,917✔
772
  }
773

774
  return TSDB_CODE_SUCCESS;
674,455✔
775
}
776

777
int32_t msmBuildTriggerRunnerTargets(SMnode* pMnode, SStmStatus* pInfo, int64_t streamId, SArray** ppRes) {
195,623✔
778
  int32_t code = TSDB_CODE_SUCCESS;
195,623✔
779
  int32_t lino = 0;
195,623✔
780

781
  if (pInfo->runnerDeploys > 0) {
195,623✔
782
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
195,623✔
783
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
195,623✔
784
  }
785
  
786
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
782,492✔
787
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
586,869✔
788
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
586,869✔
789

790
    if (!STREAM_IS_TOP_RUNNER(pStatus->flags)) {
586,869✔
791
      mstsError("the last runner task %" PRIx64 " SID:%" PRId64 " tidx:%d in deploy %d is not top runner", 
×
792
          pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.taskIdx, i);
793
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
794
    }
795
    
796
    SStreamRunnerTarget runner;
585,678✔
797
    runner.addr.taskId = pStatus->id.taskId;
586,869✔
798
    runner.addr.nodeId = pStatus->id.nodeId;
586,869✔
799
    runner.addr.epset = mndGetDnodeEpsetById(pMnode, pStatus->id.nodeId);
586,869✔
800
    runner.execReplica = pInfo->runnerReplica; 
586,869✔
801
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &runner), code, lino, _exit, terrno);
1,173,738✔
802
    mstsDebug("the %dth runner target added to trigger's runnerList, TASK:%" PRIx64 , i, runner.addr.taskId);
586,869✔
803
  }
804

805
_exit:
195,623✔
806

807
  if (code) {
195,623✔
808
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
809
  }
810
  
811
  return TSDB_CODE_SUCCESS;
195,623✔
812
}
813

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

826
  mndReleaseSnode(pMnode, pSnode);
×
827

828
  pInfo->leaderEpSet = mndGetDnodeEpsetById(pMnode, pInfo->leaderSnodeId);
×
829
  if (GOT_SNODE(pInfo->replicaSnodeId)) {
×
830
    pInfo->replicaEpSet = mndGetDnodeEpsetById(pMnode, pInfo->replicaSnodeId);
×
831
  }
832

833
  return TSDB_CODE_SUCCESS;
×
834
}
835

836
int32_t msmBuildTriggerDeployInfo(SMnode* pMnode, SStmStatus* pInfo, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
198,941✔
837
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
838
  int32_t lino = 0;
198,941✔
839
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
840
  SStreamTriggerDeployMsg* pMsg = &pDeploy->msg.trigger;
198,941✔
841
  
842
  pMsg->triggerType = pStream->pCreate->triggerType;
198,941✔
843
  pMsg->igDisorder = pStream->pCreate->igDisorder;
198,941✔
844
  pMsg->fillHistory = pStream->pCreate->fillHistory;
198,941✔
845
  pMsg->fillHistoryFirst = pStream->pCreate->fillHistoryFirst;
198,941✔
846
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
198,941✔
847
  pMsg->igNoDataTrigger = pStream->pCreate->igNoDataTrigger;
198,941✔
848
  pMsg->enableMultiGroupCalc = pStream->pCreate->enableMultiGroupCalc;
198,941✔
849
  pMsg->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags);
198,941✔
850
  pMsg->triggerHasPF = pStream->pCreate->triggerHasPF;
198,941✔
851
  pMsg->isTriggerTblStb = (pStream->pCreate->triggerTblType == TSDB_SUPER_TABLE);
198,941✔
852
  pMsg->precision = pStream->pCreate->triggerPrec;
198,941✔
853
  pMsg->partitionCols = pInfo->pCreate->partitionCols;
198,941✔
854

855
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
198,941✔
856
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
198,941✔
857
  pMsg->addOptions = pStream->pCreate->addOptions;
198,941✔
858
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
198,941✔
859

860
  pMsg->maxDelay = pStream->pCreate->maxDelay;
198,941✔
861
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
198,941✔
862
  pMsg->watermark = pStream->pCreate->watermark;
198,941✔
863
  pMsg->expiredTime = pStream->pCreate->expiredTime;
198,941✔
864
  pMsg->idleTimeoutMs = pStream->pCreate->idleTimeoutMs;
198,941✔
865
  pMsg->trigger = pInfo->pCreate->trigger;
198,941✔
866

867
  pMsg->eventTypes = pStream->pCreate->eventTypes;
198,941✔
868
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
198,941✔
869
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
198,941✔
870
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
198,941✔
871
  pMsg->calcPkSlotId = pStream->pCreate->calcPkSlotId;
198,941✔
872
  pMsg->triPkSlotId = pStream->pCreate->triPkSlotId;
198,941✔
873
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
198,941✔
874
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
198,941✔
875
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
64,269✔
876
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
64,269✔
877
  }
878

879
  SStreamTaskAddr addr;
198,544✔
880
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
198,941✔
881
  if (triggerReaderNum > 0) {
198,941✔
882
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
197,933✔
883
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
197,933✔
884
  }
885
  
886
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
443,004✔
887
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
244,063✔
888
    addr.taskId = pStatus->id.taskId;
244,063✔
889
    addr.nodeId = pStatus->id.nodeId;
244,063✔
890
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
244,063✔
891
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
488,126✔
892
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
244,063✔
893
  }
894

895
  pMsg->leaderSnodeId = pStream->mainSnodeId;
198,941✔
896
  pMsg->streamName = pInfo->streamName;
198,941✔
897
  pMsg->nodelayCreateSubtable = pStream->pCreate->nodelayCreateSubtable;
198,941✔
898

899
  if (0 == pInfo->runnerNum) {
198,941✔
900
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
3,318✔
901
    return code;
3,318✔
902
  }
903

904
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
195,623✔
905

906
_exit:
195,623✔
907

908
  if (code) {
195,623✔
UNCOV
909
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
910
  } else {
911
    mstsDebug("trigger deploy info built, readerNum:%d, runnerNum:%d", (int32_t)taosArrayGetSize(pMsg->readerList), (int32_t)taosArrayGetSize(pMsg->runnerList));
195,623✔
912
  }
913
  
914
  return TSDB_CODE_SUCCESS;
195,623✔
915
}
916

917

918
int32_t msmBuildRunnerDeployInfo(SStmTaskDeploy* pDeploy, SSubplan *plan, SStreamObj* pStream, SStmStatus* pInfo, bool topPlan) {
690,976✔
919
  int32_t code = TSDB_CODE_SUCCESS;
690,976✔
920
  int32_t lino = 0;
690,976✔
921
  int64_t streamId = pStream->pCreate->streamId;
690,976✔
922
  SStreamRunnerDeployMsg* pMsg = &pDeploy->msg.runner;
690,976✔
923
  //TAOS_CHECK_EXIT(qSubPlanToString(plan, &pMsg->pPlan, NULL));
924

925
  pMsg->execReplica = pInfo->runnerReplica;
690,976✔
926
  pMsg->streamName = pInfo->streamName;
690,976✔
927
  //TAOS_CHECK_EXIT(nodesCloneNode((SNode*)plan, (SNode**)&pMsg->pPlan));
928
  pMsg->pPlan = plan;
690,976✔
929
  pMsg->outDBFName = pInfo->pCreate->outDB;
690,976✔
930
  pMsg->outTblName = pInfo->pCreate->outTblName;
690,976✔
931
  pMsg->outTblType = pStream->pCreate->outTblType;
690,976✔
932
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
690,976✔
933
  pMsg->calcNotifyOnly = pStream->pCreate->calcNotifyOnly;
690,976✔
934
  pMsg->topPlan = topPlan;
690,976✔
935
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
690,976✔
936
  pMsg->addOptions = pStream->pCreate->addOptions;
690,976✔
937
  if ((WINDOW_TYPE_INTERVAL == pStream->pCreate->triggerType && pStream->pCreate->trigger.sliding.overlap) ||
690,976✔
938
      (WINDOW_TYPE_COUNT == pStream->pCreate->triggerType &&
678,133✔
939
       pStream->pCreate->trigger.count.sliding < pStream->pCreate->trigger.count.countVal)) {
97,023✔
940
    pMsg->addOptions |= CALC_SLIDING_OVERLAP;
71,592✔
941
  }
942
  pMsg->outCols = pInfo->pCreate->outCols;
690,976✔
943
  pMsg->outTags = pInfo->pCreate->outTags;
690,976✔
944
  pMsg->outStbUid = pStream->pCreate->outStbUid;
690,976✔
945
  pMsg->outStbSversion = pStream->pCreate->outStbSversion;
690,976✔
946
  
947
  pMsg->subTblNameExpr = pInfo->pCreate->subTblNameExpr;
690,976✔
948
  pMsg->tagValueExpr = pInfo->pCreate->tagValueExpr;
690,976✔
949
  pMsg->forceOutCols = pInfo->pCreate->forceOutCols;
690,976✔
950

951
  pMsg->colCids = pInfo->pCreate->colCids;
690,976✔
952
  pMsg->tagCids = pInfo->pCreate->tagCids;
690,976✔
953
_exit:
690,976✔
954

955
  if (code) {
690,976✔
UNCOV
956
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
957
  }
958
  
959
  return code;
690,976✔
960
}
961

962

963
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
462,104✔
964
  int32_t code = TSDB_CODE_SUCCESS;
462,104✔
965
  int32_t lino = 0;
462,104✔
966
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
462,104✔
967
  SListNode* pNode = pList ? listHead(pList) : NULL;
462,104✔
968
  
969
  for (int32_t i = 0; i < taskNum; ++i) {
1,135,257✔
970
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
673,153✔
971
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
673,153✔
972
    if (pNode) {
673,153✔
973
      pNode = TD_DLIST_NODE_NEXT(pNode);
364,868✔
974
    }
975
  }
976
  
977
_exit:
462,104✔
978

979
  if (code) {
462,104✔
UNCOV
980
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
981
  }
982
  
983
  return code;
462,104✔
984
}
985

986

987
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
786,063✔
988
  int32_t code = TSDB_CODE_SUCCESS;
786,063✔
989
  int32_t lino = 0;
786,063✔
990
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
786,063✔
991
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
786,063✔
992
  
993
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,675,980✔
994
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
889,917✔
995
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
889,917✔
996
  }
997
  
998
_exit:
786,063✔
999

1000
  if (code) {
786,063✔
UNCOV
1001
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1002
  }
1003

1004
  return code;
786,063✔
1005
}
1006

1007
int64_t msmAssignTaskId(void) {
1,562,817✔
1008
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,562,817✔
1009
}
1010

1011
int64_t msmAssignTaskSeriousId(void) {
1,562,817✔
1012
  return taosGetTimestampNs();
1,562,817✔
1013
}
1014

1015

1016
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
1,057,338✔
1017
  int32_t code = TSDB_CODE_SUCCESS;
1,057,338✔
1018
  int32_t lino = 0;
1,057,338✔
1019
  bool     noExists = false;
1,057,338✔
1020
  SStmSnodeStatus* pStatus = NULL;
1,057,338✔
1021

1022
  while (true) {
1023
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
1,068,177✔
1024
    if (NULL == pStatus) {
1,068,177✔
1025
      if (noExists) {
10,839✔
UNCOV
1026
        mstsError("snode %d not exists in snodeMap", snodeId);
×
UNCOV
1027
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1028
      }
1029

1030
      noExists = true;
10,839✔
1031
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
10,839✔
1032
      
1033
      continue;
10,839✔
1034
    }
1035

1036
    *alive = (pStatus->runnerThreadNum >= 0);
1,057,338✔
1037
    break;
1,057,338✔
1038
  }
1039

1040
_exit:
1,057,338✔
1041

1042
  if (code) {
1,057,338✔
UNCOV
1043
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1044
  }
1045

1046
  return code;
1,057,338✔
1047
}
1048

1049
int32_t msmRetrieveStaticSnodeId(SMnode* pMnode, SStreamObj* pStream) {
408,739✔
1050
  int32_t code = TSDB_CODE_SUCCESS;
408,739✔
1051
  int32_t lino = 0;
408,739✔
1052
  bool alive = false;
408,739✔
1053
  int32_t mainSnodeId = atomic_load_32(&pStream->mainSnodeId);
408,739✔
1054
  int32_t snodeId = mainSnodeId;
408,739✔
1055
  int64_t streamId = pStream->pCreate->streamId;
408,739✔
1056
  
1057
  while (true) {
1058
    TAOS_CHECK_EXIT(msmIsSnodeAlive(pMnode, snodeId, streamId, &alive));
408,739✔
1059

1060
    if (alive) {
408,739✔
1061
      return snodeId;
408,739✔
1062
    }
1063
    
UNCOV
1064
    if (snodeId == mainSnodeId) {
×
UNCOV
1065
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
1066
      if (NULL == pSnode) {
×
1067
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1068
        return 0;
×
1069
      }
1070
      
UNCOV
1071
      if (pSnode->replicaId <= 0) {
×
UNCOV
1072
        mstsError("no available snode now, mainSnodeId:%d, replicaId:%d", mainSnodeId, pSnode->replicaId);
×
1073
        mndReleaseSnode(pMnode, pSnode);
×
1074
        return 0;
×
1075
      }
1076

UNCOV
1077
      snodeId = pSnode->replicaId;
×
UNCOV
1078
      mndReleaseSnode(pMnode, pSnode);
×
1079
      
1080
      continue;
×
1081
    }
1082

UNCOV
1083
    mstsError("no available snode now, mainSnodeId:%d, followerSnodeId:%d", mainSnodeId, snodeId);
×
UNCOV
1084
    return 0;
×
1085
  }
1086

UNCOV
1087
_exit:
×
1088

1089
  if (code) {
×
UNCOV
1090
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1091
  }
1092

UNCOV
1093
  return 0;
×
1094
}
1095

1096
int32_t msmAssignRandomSnodeId(SMnode* pMnode, int64_t streamId) {
624,301✔
1097
  int32_t code = TSDB_CODE_SUCCESS;
624,301✔
1098
  int32_t lino = 0;
624,301✔
1099
  int32_t snodeIdx = 0;
624,301✔
1100
  int32_t snodeId = 0;
624,301✔
1101
  void      *pIter = NULL;
624,301✔
1102
  SSnodeObj *pObj = NULL;
624,301✔
1103
  bool alive = false;
624,301✔
1104
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
624,301✔
1105
  if (snodeNum <= 0) {
624,301✔
1106
    mstsInfo("no available snode now, num:%d", snodeNum);
2,720✔
1107
    goto _exit;
2,720✔
1108
  }
1109

1110
  int32_t snodeTarget = taosRand() % snodeNum;
621,581✔
1111

1112
  while (1) {
1113
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
648,599✔
1114
    if (pIter == NULL) {
648,599✔
UNCOV
1115
      if (0 == snodeId) {
×
UNCOV
1116
        mstsError("no alive snode now, snodeNum:%d", snodeNum);
×
1117
        break;
×
1118
      }
1119
      
UNCOV
1120
      snodeId = 0;
×
UNCOV
1121
      continue;
×
1122
    }
1123

1124
    code = msmIsSnodeAlive(pMnode, pObj->id, streamId, &alive);
648,599✔
1125
    if (code) {
648,599✔
UNCOV
1126
      sdbRelease(pMnode->pSdb, pObj);
×
UNCOV
1127
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1128
      pObj = NULL;
×
1129
      TAOS_CHECK_EXIT(code);
×
1130
    }
1131
    
1132
    if (!alive) {
648,599✔
UNCOV
1133
      sdbRelease(pMnode->pSdb, pObj);
×
UNCOV
1134
      continue;
×
1135
    }
1136

1137
    snodeId = pObj->id;
648,599✔
1138
    if (snodeIdx == snodeTarget) {
648,599✔
1139
      sdbRelease(pMnode->pSdb, pObj);
621,581✔
1140
      sdbCancelFetch(pMnode->pSdb, pIter);
621,581✔
1141
      pObj = NULL;
621,581✔
1142
      goto _exit;
621,581✔
1143
    }
1144

1145
    sdbRelease(pMnode->pSdb, pObj);
27,018✔
1146
    snodeIdx++;
27,018✔
1147
  }
1148

1149
_exit:
624,301✔
1150

1151
  if (code) {
624,301✔
UNCOV
1152
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1153
  }
1154

1155
  if (0 == snodeId) {
624,301✔
1156
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,720✔
1157
  }
1158

1159
  return snodeId;
624,301✔
1160
}
1161

1162
int32_t msmAssignTaskSnodeId(SMnode* pMnode, SStreamObj* pStream, bool isStatic) {
827,829✔
1163
  int64_t streamId = pStream->pCreate->streamId;
827,829✔
1164
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
827,829✔
1165
  int32_t snodeId = 0;
827,829✔
1166
  if (snodeNum <= 0) {
827,829✔
UNCOV
1167
    mstsInfo("no available snode now, num:%d", snodeNum);
×
UNCOV
1168
    goto _exit;
×
1169
  }
1170

1171
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
827,829✔
1172

1173
_exit:
827,829✔
1174

1175
  if (0 == snodeId) {
827,829✔
UNCOV
1176
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1177
  }
1178

1179
  return snodeId;
827,829✔
1180
}
1181

1182

1183
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,941✔
1184
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
1185
  int32_t lino = 0;
198,941✔
1186
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
1187

1188
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
198,941✔
1189
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
198,941✔
1190

1191
  pInfo->triggerTask->id.taskId = pCtx->triggerTaskId;
198,941✔
1192
  pInfo->triggerTask->id.deployId = 0;
198,941✔
1193
  pInfo->triggerTask->id.seriousId = msmAssignTaskSeriousId();
198,941✔
1194
  pInfo->triggerTask->id.nodeId = pCtx->triggerNodeId;
198,941✔
1195
  pInfo->triggerTask->id.taskIdx = 0;
198,941✔
1196
  pInfo->triggerTask->type = STREAM_TRIGGER_TASK;
198,941✔
1197
  pInfo->triggerTask->lastUpTs = pCtx->currTs;
198,941✔
1198
  pInfo->triggerTask->pStream = pInfo;
198,941✔
1199

1200
  SStmTaskDeploy info = {0};
198,941✔
1201
  info.task.type = pInfo->triggerTask->type;
198,941✔
1202
  info.task.streamId = streamId;
198,941✔
1203
  info.task.taskId =  pInfo->triggerTask->id.taskId;
198,941✔
1204
  info.task.seriousId = pInfo->triggerTask->id.seriousId;
198,941✔
1205
  info.task.nodeId =  pInfo->triggerTask->id.nodeId;
198,941✔
1206
  info.task.taskIdx =  pInfo->triggerTask->id.taskIdx;
198,941✔
1207
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pInfo, &info, pStream));
198,941✔
1208
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
198,941✔
1209
  
1210
  (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
198,941✔
1211

1212
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
198,941✔
1213
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
198,941✔
1214

1215
_exit:
198,941✔
1216

1217
  if (code) {
198,941✔
UNCOV
1218
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1219
  }
1220

1221
  return code;
198,941✔
1222
}
1223

1224
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
305,788✔
1225
  int32_t code = TSDB_CODE_SUCCESS;
305,788✔
1226
  int32_t lino = 0;
305,788✔
1227

1228
  pState->id.taskId = msmAssignTaskId();
305,788✔
1229
  pState->id.deployId = 0;
305,788✔
1230
  pState->id.seriousId = msmAssignTaskSeriousId();
305,788✔
1231
  pState->id.nodeId = nodeId;
305,788✔
1232
  pState->id.taskIdx = 0;
305,788✔
1233
  pState->type = STREAM_READER_TASK;
305,788✔
1234
  pState->flags = STREAM_FLAG_TRIGGER_READER;
305,788✔
1235
  pState->status = STREAM_STATUS_UNDEPLOYED;
305,788✔
1236
  pState->lastUpTs = pCtx->currTs;
305,788✔
1237
  pState->pStream = pInfo;
305,788✔
1238
  
1239
  SStmTaskDeploy info = {0};
305,788✔
1240
  info.task.type = pState->type;
305,788✔
1241
  info.task.streamId = streamId;
305,788✔
1242
  info.task.taskId = pState->id.taskId;
305,788✔
1243
  info.task.flags = pState->flags;
305,788✔
1244
  info.task.seriousId = pState->id.seriousId;
305,788✔
1245
  info.task.nodeId = pState->id.nodeId;
305,788✔
1246
  info.task.taskIdx = pState->id.taskIdx;
305,788✔
1247
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, NULL, pInfo, true));
305,788✔
1248
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
305,788✔
1249

1250
_exit:
305,788✔
1251

1252
  if (code) {
305,788✔
UNCOV
1253
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1254
  }
1255

1256
  return code;
305,788✔
1257
}
1258

1259
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,941✔
1260
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
1261
  int32_t lino = 0;
198,941✔
1262
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
1263
  SSdb   *pSdb = pCtx->pMnode->pSdb;
198,941✔
1264
  SStmTaskStatus* pState = NULL;
198,941✔
1265
  SVgObj *pVgroup = NULL;
198,941✔
1266
  SDbObj* pDb = NULL;
198,941✔
1267
  
1268
  switch (pStream->pCreate->triggerTblType) {
198,941✔
1269
    case TSDB_NORMAL_TABLE:
116,066✔
1270
    case TSDB_CHILD_TABLE:
1271
    case TSDB_VIRTUAL_CHILD_TABLE:
1272
    case TSDB_VIRTUAL_NORMAL_TABLE: {
1273
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
116,066✔
1274
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
116,066✔
1275
      pState = taosArrayGet(pInfo->trigReaders, 0);
116,066✔
1276
      
1277
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
116,066✔
1278
      break;
116,066✔
1279
    }
1280
    case TSDB_SUPER_TABLE: {
81,867✔
1281
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
81,867✔
1282
      if (NULL == pDb) {
81,867✔
UNCOV
1283
        code = terrno;
×
UNCOV
1284
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
1285
        goto _exit;
×
1286
      }
1287

1288
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
81,867✔
1289
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
81,867✔
1290
      
1291
      void *pIter = NULL;
81,867✔
1292
      while (1) {
411,347✔
1293
        SStmTaskDeploy info = {0};
493,214✔
1294
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
493,214✔
1295
        if (pIter == NULL) {
493,214✔
1296
          break;
81,867✔
1297
        }
1298
      
1299
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
411,347✔
1300
          pState = taosArrayReserve(pInfo->trigReaders, 1);
127,997✔
1301

1302
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
127,997✔
1303
          if (code) {
127,997✔
UNCOV
1304
            sdbRelease(pSdb, pVgroup);
×
UNCOV
1305
            sdbCancelFetch(pSdb, pIter);
×
1306
            pVgroup = NULL;
×
1307
            TAOS_CHECK_EXIT(code);
×
1308
          }
1309
        }
1310

1311
        sdbRelease(pSdb, pVgroup);
411,347✔
1312
      }
1313
      break;
81,867✔
1314
    }
1315
    default:
1,008✔
1316
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,008✔
1317
      break;
1,008✔
1318
  }
1319

1320
_exit:
198,941✔
1321

1322
  mndReleaseDb(pCtx->pMnode, pDb);
198,941✔
1323

1324
  if (code) {
198,941✔
UNCOV
1325
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1326
  }
1327

1328
  return code;
198,941✔
1329
}
1330

1331
int32_t msmUPAddScanTask(SStmGrpCtx* pCtx, SStreamObj* pStream, char* scanPlan, int32_t vgId, int64_t taskId) {
365,121✔
1332
  int32_t code = TSDB_CODE_SUCCESS;
365,121✔
1333
  int32_t lino = 0;
365,121✔
1334
  SSubplan* pSubplan = NULL;
365,121✔
1335
  int64_t streamId = pStream->pCreate->streamId;
365,121✔
1336
  int64_t key[2] = {streamId, 0};
365,121✔
1337
  SStmTaskSrcAddr addr;
364,705✔
1338
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
365,121✔
1339
  addr.isFromCache = false;
365,121✔
1340
  
1341
  if (MNODE_HANDLE == vgId) {
365,121✔
UNCOV
1342
    mndGetMnodeEpSet(pCtx->pMnode, &addr.epset);
×
1343
  } else if (vgId > MNODE_HANDLE) {
365,121✔
1344
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
365,121✔
1345
  } else {
UNCOV
1346
    mstsError("invalid vgId %d in scanPlan", vgId);
×
UNCOV
1347
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1348
  }
1349
  
1350
  addr.taskId = taskId;
365,121✔
1351
  addr.vgId = vgId;
365,121✔
1352
  addr.groupId = pSubplan->id.groupId;
365,121✔
1353

1354
  key[1] = pSubplan->id.subplanId;
365,121✔
1355

1356
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
365,121✔
1357
  if (NULL == ppRes) {
365,121✔
1358
    SArray* pRes = taosArrayInit(1, sizeof(addr));
346,557✔
1359
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
346,557✔
1360
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
693,114✔
1361
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
346,557✔
1362
  } else {
1363
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
37,128✔
1364
  }
1365

1366
  mstsDebug("calcReader %" PRIx64 " added to toUpdateScan, vgId:%d, groupId:%d, subplanId:%d", taskId, vgId, pSubplan->id.groupId, pSubplan->id.subplanId);
365,121✔
1367
  
1368
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
365,121✔
1369
  
1370
_exit:
365,121✔
1371

1372
  nodesDestroyNode((SNode*)pSubplan);
365,121✔
1373

1374
  if (code) {
365,121✔
UNCOV
1375
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1376
  }
1377

1378
  return code;
365,121✔
1379
}
1380

1381
int32_t msmUPAddCacheTask(SStmGrpCtx* pCtx, SStreamCalcScan* pScan, SStreamObj* pStream) {
81,895✔
1382
  int32_t code = TSDB_CODE_SUCCESS;
81,895✔
1383
  int32_t lino = 0;
81,895✔
1384
  SSubplan* pSubplan = NULL;
81,895✔
1385
  int64_t streamId = pStream->pCreate->streamId;
81,895✔
1386
  int64_t key[2] = {streamId, 0};
81,895✔
1387
  TAOS_CHECK_EXIT(nodesStringToNode(pScan->scanPlan, (SNode**)&pSubplan));
81,895✔
1388

1389
  SStmTaskSrcAddr addr;
81,895✔
1390
  addr.isFromCache = true;
81,895✔
1391
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
81,895✔
1392
  addr.taskId = pCtx->triggerTaskId;
81,895✔
1393
  addr.vgId = pCtx->triggerNodeId;
81,895✔
1394
  addr.groupId = pSubplan->id.groupId;
81,895✔
1395

1396
  key[1] = pSubplan->id.subplanId;
81,895✔
1397
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
81,895✔
1398
  if (NULL == ppRes) {
81,895✔
1399
    SArray* pRes = taosArrayInit(1, sizeof(addr));
81,895✔
1400
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
81,895✔
1401
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
163,790✔
1402
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
81,895✔
1403
  } else {
UNCOV
1404
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1405
  }
1406
  
1407
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
81,895✔
1408
  
1409
_exit:
81,895✔
1410

1411
  nodesDestroyNode((SNode*)pSubplan);
81,895✔
1412
  
1413
  if (code) {
81,895✔
UNCOV
1414
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1415
  }
1416

1417
  return code;
81,895✔
1418
}
1419

1420

1421
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
367,365✔
1422
  int32_t code = TSDB_CODE_SUCCESS;
367,365✔
1423
  int32_t lino = 0;
367,365✔
1424

1425
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
367,365✔
1426

1427
  pState->id.taskId = msmAssignTaskId();
367,365✔
1428
  pState->id.deployId = 0;
367,365✔
1429
  pState->id.seriousId = msmAssignTaskSeriousId();
367,365✔
1430
  pState->id.nodeId = nodeId;
367,365✔
1431
  pState->id.taskIdx = taskIdx;
367,365✔
1432
  pState->type = STREAM_READER_TASK;
367,365✔
1433
  pState->flags = 0;
367,365✔
1434
  pState->status = STREAM_STATUS_UNDEPLOYED;
367,365✔
1435
  pState->lastUpTs = pCtx->currTs;
367,365✔
1436
  pState->pStream = pInfo;
367,365✔
1437
  
1438
  SStmTaskDeploy info = {0};
367,365✔
1439
  info.task.type = pState->type;
367,365✔
1440
  info.task.streamId = streamId;
367,365✔
1441
  info.task.taskId = pState->id.taskId;
367,365✔
1442
  info.task.flags = pState->flags;
367,365✔
1443
  info.task.seriousId = pState->id.seriousId;
367,365✔
1444
  info.task.nodeId = pState->id.nodeId;
367,365✔
1445
  info.task.taskIdx = pState->id.taskIdx;
367,365✔
1446
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, calcScanPlan, pInfo, false));
367,365✔
1447
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
367,365✔
1448

1449
_exit:
367,365✔
1450

1451
  if (code) {
367,365✔
UNCOV
1452
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1453
  }
1454

1455
  return code;
367,365✔
1456
}
1457

1458

1459
static int32_t msmTDAddCalcReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,941✔
1460
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
1461
  int32_t lino = 0;
198,941✔
1462
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
198,941✔
1463
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
1464
  SStmTaskStatus* pState = NULL;
198,941✔
1465
  pInfo->calcReaders = tdListNew(sizeof(SStmTaskStatus));
198,941✔
1466
  TSDB_CHECK_NULL(pInfo->calcReaders, code, lino, _exit, terrno);
198,941✔
1467

1468
  
1469
  for (int32_t i = 0; i < calcTasksNum; ++i) {
645,704✔
1470
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
446,763✔
1471
    if (pScan->readFromCache) {
446,763✔
1472
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
81,895✔
1473
      continue;
81,895✔
1474
    }
1475
    
1476
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
364,868✔
1477
    for (int32_t m = 0; m < vgNum; ++m) {
729,736✔
1478
      pState = tdListReserve(pInfo->calcReaders);
364,868✔
1479
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
364,868✔
1480

1481
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
364,868✔
1482
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
364,868✔
1483
    }
1484
  }
1485

1486
_exit:
198,941✔
1487

1488
  if (code) {
198,941✔
UNCOV
1489
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1490
  }
1491

1492
  return code;
198,941✔
1493
}
1494

1495

1496

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

1524
_exit:
253✔
1525

1526
  if (code) {
253✔
UNCOV
1527
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1528
  }
1529

1530
  return code;
253✔
1531
}
1532

1533
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,941✔
1534
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
1535
  int32_t lino = 0;
198,941✔
1536
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
1537
  
1538
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
198,941✔
1539
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
198,941✔
1540

1541
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
198,941✔
1542
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
198,941✔
1543
  
1544
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
198,941✔
1545
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
198,941✔
1546
  
1547
_exit:
198,941✔
1548

1549
  if (code) {
198,941✔
UNCOV
1550
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1551
  }
1552
  
1553
  return code;
198,941✔
1554
}
1555

1556
int32_t msmUpdatePlanSourceAddr(SStreamTask* pTask, int64_t streamId, SSubplan* plan, int64_t clientId, SStmTaskSrcAddr* pSrc, int32_t msgType, int64_t srcSubplanId) {
1,521,298✔
1557
  SDownstreamSourceNode source = {
1,521,298✔
1558
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1559
      .clientId = clientId,
1560
      .taskId = pSrc->taskId,
1,521,298✔
1561
      .sId = 0,
1562
      .execId = 0,
1563
      .fetchMsgType = msgType,
1564
      .localExec = false,
1565
  };
1566

1567
  source.addr.epSet = pSrc->epset;
1,521,298✔
1568
  source.addr.nodeId = pSrc->vgId;
1,521,298✔
1569

1570
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
1,521,298✔
1571
      plan->id.subplanId, pSrc->groupId, srcSubplanId, source.clientId, source.taskId, source.addr.nodeId, TMSG_INFO(source.fetchMsgType));
1572
  
1573
  return qSetSubplanExecutionNode(plan, pSrc->groupId, &source);
1,521,298✔
1574
}
1575

1576
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
62,088✔
1577
  int64_t streamId = pStream->pCreate->streamId;
62,088✔
1578
  int32_t runnerNum = taosArrayGetSize(pRunners);
62,088✔
1579
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
190,932✔
1580
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
190,932✔
1581
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
190,932✔
1582
    if (pPlan->id.subplanId == subplanId) {
190,932✔
1583
      *taskId = pDeploy->task.taskId;
62,088✔
1584
      *ppParent = &pDeploy->task;
62,088✔
1585
      return TSDB_CODE_SUCCESS;
62,088✔
1586
    }
1587
  }
1588

UNCOV
1589
  mstsError("subplanId %d not found in runner list", subplanId);
×
1590

1591
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1592
}
1593

1594
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
690,976✔
1595
  int32_t code = TSDB_CODE_SUCCESS;
690,976✔
1596
  int32_t lino = 0;
690,976✔
1597
  int64_t key[2] = {streamId, -1};
690,976✔
1598
  SNode* pNode = NULL;
690,976✔
1599
  SStreamTask* pTask = &pDeploy->task;
690,976✔
1600
  FOREACH(pNode, pPlan->pChildren) {
2,093,606✔
1601
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,402,630✔
1602
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
62,088✔
1603
      continue;
62,088✔
1604
    }
1605
    
1606
    SValueNode* pVal = (SValueNode*)pNode;
1,340,542✔
1607
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,340,542✔
UNCOV
1608
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
UNCOV
1609
      continue;
×
1610
    }
1611

1612
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,340,542✔
1613

1614
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
1,340,542✔
1615
    if (NULL == ppRes) {
1,340,542✔
UNCOV
1616
      msttError("lowest runner subplan ID:%d,%d can't get its child ID:%" PRId64 " addr", pPlan->id.groupId, pPlan->id.subplanId, key[1]);
×
UNCOV
1617
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1618
    }
1619

1620
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,340,542✔
1621
    for (int32_t i = 0; i < childrenNum; ++i) {
2,799,752✔
1622
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,459,210✔
1623
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,459,210✔
1624
    }
1625
  }
1626

1627
_exit:
690,976✔
1628

1629
  if (code) {
690,976✔
UNCOV
1630
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1631
  }
1632

1633
  return code;
690,976✔
1634
}
1635

1636
int32_t msmUpdateRunnerPlan(SStmGrpCtx* pCtx, SArray* pRunners, int32_t beginIdx, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
690,976✔
1637
  int32_t code = TSDB_CODE_SUCCESS;
690,976✔
1638
  int32_t lino = 0;
690,976✔
1639
  SSubplan* pPlan = pDeploy->msg.runner.pPlan;
690,976✔
1640
  SStreamTask* pTask = &pDeploy->task;
690,976✔
1641
  SStreamTask* parentTask = NULL;
690,976✔
1642
  int64_t streamId = pStream->pCreate->streamId;
690,976✔
1643

1644
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
690,976✔
1645

1646
  SNode* pTmp = NULL;
690,976✔
1647
  WHERE_EACH(pTmp, pPlan->pChildren) {
2,093,606✔
1648
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,402,630✔
1649
      ERASE_NODE(pPlan->pChildren);
1,340,542✔
1650
      continue;
1,340,542✔
1651
    }
1652
    WHERE_NEXT;
62,088✔
1653
  }
1654
  nodesClearList(pPlan->pChildren);
690,976✔
1655
  pPlan->pChildren = NULL;
690,976✔
1656

1657
  if (NULL == pPlan->pParents) {
690,976✔
1658
    goto _exit;
628,888✔
1659
  }
1660

1661
  SNode* pNode = NULL;
62,088✔
1662
  int64_t parentTaskId = 0;
62,088✔
1663
  SStmTaskSrcAddr addr = {0};
62,088✔
1664
  addr.taskId = pDeploy->task.taskId;
62,088✔
1665
  addr.vgId = pDeploy->task.nodeId;
62,088✔
1666
  addr.groupId = pPlan->id.groupId;
62,088✔
1667
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
62,088✔
1668
  FOREACH(pNode, pPlan->pParents) {
124,176✔
1669
    SSubplan* pSubplan = (SSubplan*)pNode;
62,088✔
1670
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
62,088✔
1671
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
62,088✔
1672
  }
1673
  
1674
_exit:
689,785✔
1675

1676
  if (code) {
690,976✔
UNCOV
1677
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1678
  }
1679

1680
  return code;
690,976✔
1681
}
1682

1683
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
628,888✔
1684
  int32_t code = TSDB_CODE_SUCCESS;
628,888✔
1685
  int32_t lino = 0;
628,888✔
1686
  int64_t streamId = pStream->pCreate->streamId;
628,888✔
1687
  int32_t runnerNum = taosArrayGetSize(pRunners);
628,888✔
1688
  
1689
  for (int32_t i = 0; i < runnerNum; ++i) {
1,319,864✔
1690
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
690,976✔
1691
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
690,976✔
1692
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
690,976✔
1693

1694
    SStreamTask* pTask = &pDeploy->task;
690,976✔
1695
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
690,976✔
1696
  }
1697

1698
_exit:
628,888✔
1699

1700
  if (code) {
628,888✔
UNCOV
1701
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1702
  }
1703

1704
  return code;
628,888✔
1705
}
1706

1707
static int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, int32_t dagIdx, SStmStatus* pInfo, SStreamObj* pStream, SQueryPlan** pRoot, SNodeList** subEP) {
209,545✔
1708
  int32_t code = 0;
209,545✔
1709
  int32_t lino = 0;
209,545✔
1710
  int64_t streamId = pStream->pCreate->streamId;
209,545✔
1711
  SArray* deployTaskList = NULL;
209,545✔
1712
  SArray* deployList = NULL;
209,545✔
1713
  int32_t deployNodeId = 0;
209,545✔
1714
  SStmTaskStatus* pState = NULL;
209,545✔
1715
  int32_t taskIdx = 0;
209,545✔
1716
  SNodeListNode *plans = NULL;
209,545✔
1717
  SQueryPlan    *pDag = NULL;
209,545✔
1718
  int32_t        taskNum = 0;
209,545✔
1719
  int32_t        totalTaskNum = 0;
209,545✔
1720
  bool           subQ = false;
209,545✔
1721

1722
  if (dagIdx >= 0) {
209,545✔
1723
    subQ = true;
13,922✔
1724
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
13,922✔
1725
  } else {
1726
    pDag = *pRoot;
195,623✔
1727
  }
1728

1729
  if (pDag->numOfSubplans <= 0) {
209,545✔
UNCOV
1730
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
UNCOV
1731
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1732
  }
1733

1734
  if ((*pRoot)->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
209,545✔
UNCOV
1735
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
UNCOV
1736
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1737
  }
1738

1739
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
209,545✔
1740
  if (levelNum <= 0) {
209,545✔
UNCOV
1741
    mstsError("invalid level num:%d", levelNum);
×
UNCOV
1742
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1743
  }
1744

1745
  int32_t        lowestLevelIdx = levelNum - 1;
209,545✔
1746
  
1747
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
209,545✔
1748
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
209,545✔
UNCOV
1749
    mstsError("invalid level plan, level:0, planNodeType:%d", nodeType(plans));
×
UNCOV
1750
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1751
  }
1752
  
1753
  taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
209,545✔
1754
  if (taskNum != 1) {
209,545✔
UNCOV
1755
    mstsError("invalid level plan number:%d, level:0", taskNum);
×
UNCOV
1756
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1757
  }
1758

1759
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
209,545✔
1760
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
209,545✔
1761
  
1762
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
838,180✔
1763
    totalTaskNum = 0;
628,635✔
1764

1765
    deployList = pInfo->runners[deployId];
628,635✔
1766
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
628,635✔
1767
    if (!GOT_SNODE(deployNodeId)) {
628,635✔
UNCOV
1768
      TAOS_CHECK_EXIT(terrno);
×
1769
    }
1770

1771
    taskIdx = 0;
628,635✔
1772

1773
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,279,299✔
1774
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
650,664✔
1775
      if (NULL == plans) {
650,664✔
UNCOV
1776
        mstsError("empty level plan, level:%d", i);
×
UNCOV
1777
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1778
      }
1779

1780
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
650,664✔
UNCOV
1781
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
UNCOV
1782
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1783
      }
1784

1785
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
650,664✔
1786
      if (taskNum <= 0) {
650,664✔
UNCOV
1787
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
UNCOV
1788
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1789
      }
1790

1791
      totalTaskNum += taskNum;
650,664✔
1792
      if (totalTaskNum > pDag->numOfSubplans) {
650,664✔
UNCOV
1793
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
UNCOV
1794
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1795
      }
1796

1797
      for (int32_t n = 0; n < taskNum; ++n) {
1,341,387✔
1798
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
690,723✔
1799
        pState = taosArrayReserve(deployList, 1);
690,723✔
1800

1801
        pState->id.taskId = msmAssignTaskId();
690,723✔
1802
        pState->id.deployId = deployId;
690,723✔
1803
        pState->id.seriousId = msmAssignTaskSeriousId();
690,723✔
1804
        pState->id.nodeId = deployNodeId;
690,723✔
1805
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
690,723✔
1806
        pState->type = STREAM_RUNNER_TASK;
690,723✔
1807
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
690,723✔
1808
        pState->status = STREAM_STATUS_UNDEPLOYED;
690,723✔
1809
        pState->lastUpTs = pCtx->currTs;
690,723✔
1810
        pState->pStream = pInfo;
690,723✔
1811

1812
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
690,723✔
1813
        pDeploy->task.type = pState->type;
690,723✔
1814
        pDeploy->task.streamId = streamId;
690,723✔
1815
        pDeploy->task.taskId = pState->id.taskId;
690,723✔
1816
        pDeploy->task.flags = pState->flags;
690,723✔
1817
        pDeploy->task.seriousId = pState->id.seriousId;
690,723✔
1818
        pDeploy->task.deployId = pState->id.deployId;
690,723✔
1819
        pDeploy->task.nodeId = pState->id.nodeId;
690,723✔
1820
        pDeploy->task.taskIdx = pState->id.taskIdx;
690,723✔
1821
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i && !subQ));
690,723✔
1822

1823
        SStreamTask* pTask = &pDeploy->task;
690,723✔
1824
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
690,723✔
1825
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1826

1827
        if (subQ) {
690,723✔
1828
          if (i == 0) {
59,586✔
1829
            SStmTaskSrcAddr addr = {0};
41,766✔
1830
            SDownstreamSourceNode* pSource = NULL;
41,766✔
1831

1832
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
41,766✔
1833

1834
            addr.taskId = pDeploy->task.taskId;
41,766✔
1835
            addr.vgId = pDeploy->task.nodeId;
41,766✔
1836
            addr.groupId = plan->id.groupId;
41,766✔
1837
            addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
41,766✔
1838

1839
            pSource->addr.epSet = addr.epset;
41,766✔
1840
            pSource->addr.nodeId = addr.vgId;
41,766✔
1841

1842
            pSource->clientId = streamId;
41,766✔
1843
            pSource->taskId = pDeploy->task.taskId;
41,766✔
1844
            pSource->sId = 0;
41,766✔
1845
            pSource->execId = 0;
41,766✔
1846
            pSource->fetchMsgType = TDMT_STREAM_FETCH_FROM_RUNNER;
41,766✔
1847
            pSource->localExec = false;
41,766✔
1848

1849
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
41,766✔
1850
          }
1851
        } else {
1852
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
631,137✔
1853
        }
1854
      }
1855

1856
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
650,664✔
1857
    }
1858

1859
    if (totalTaskNum != pDag->numOfSubplans) {
628,635✔
UNCOV
1860
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
UNCOV
1861
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1862
    }
1863

1864
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
628,635✔
1865

1866
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
628,635✔
1867

1868
    nodesDestroyNode((SNode *)(*pRoot));
628,635✔
1869
    *pRoot = NULL;
628,635✔
1870
    
1871
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
628,635✔
1872
    if (subQ) {
628,635✔
1873
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
41,766✔
1874
    } else {
1875
      pDag = *pRoot;
586,869✔
1876
    }
1877

1878
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
628,635✔
1879
  }
1880

1881
  if (!subQ) {
209,545✔
1882
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
782,492✔
1883
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
586,869✔
1884
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
586,869✔
1885
    }
1886
  }
1887
  
1888
  pInfo->runnerNum += totalTaskNum;
209,545✔
1889
  
1890
_exit:
209,545✔
1891

1892
  if (code) {
209,545✔
UNCOV
1893
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1894
  }
1895

1896
  taosArrayDestroy(deployTaskList);
209,545✔
1897

1898
  return code;
209,545✔
1899
}
1900

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

1918
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
506✔
1919
    deployId = pAction->deployId[r];
253✔
1920

1921
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
253✔
1922

1923
    pStartRunner = pRunner;
253✔
1924
    totalTaskNum = 0;
253✔
1925

1926
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
253✔
1927
    if (!GOT_SNODE(newNodeId)) {
253✔
UNCOV
1928
      TAOS_CHECK_EXIT(terrno);
×
1929
    }
1930

1931
    taskIdx = 0;
253✔
1932
    
1933
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
506✔
1934
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
253✔
1935
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
253✔
1936
      totalTaskNum += taskNum;
253✔
1937

1938
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
253✔
1939
      
1940
      for (int32_t n = 0; n < taskNum; ++n) {
506✔
1941
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
253✔
1942

1943
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
253✔
1944
        if (pRunner->id.taskIdx != newTaskIdx) {
253✔
UNCOV
1945
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
UNCOV
1946
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1947
        }
1948

1949
        pRunner->id.nodeId = newNodeId;
253✔
1950

1951
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
253✔
1952
        pDeploy->task.type = pRunner->type;
253✔
1953
        pDeploy->task.streamId = streamId;
253✔
1954
        pDeploy->task.taskId = pRunner->id.taskId;
253✔
1955
        pDeploy->task.flags = pRunner->flags;
253✔
1956
        pDeploy->task.seriousId = pRunner->id.seriousId;
253✔
1957
        pDeploy->task.nodeId = pRunner->id.nodeId;
253✔
1958
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
253✔
1959
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
253✔
1960

1961
        pRunner++;
253✔
1962
      }
1963

1964
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
253✔
1965
    }
1966

1967
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
253✔
1968

1969
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
253✔
1970

1971
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
253✔
1972

1973
    nodesDestroyNode((SNode *)pDag);
253✔
1974
    pDag = NULL;
253✔
1975

1976
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
253✔
1977
  }
1978

1979
_exit:
253✔
1980

1981
  if (code) {
253✔
UNCOV
1982
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1983
  }
1984

1985
  nodesDestroyNode((SNode *)pDag);
253✔
1986
  taosArrayDestroy(deployTaskList);
253✔
1987

1988
  return code;
253✔
1989
}
1990

1991

1992
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
187,138✔
1993
  int32_t code = TSDB_CODE_SUCCESS;
187,138✔
1994
  int32_t lino = 0;
187,138✔
1995
  //STREAMTODO 
1996
  
1997
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
187,138✔
1998
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
187,138✔
1999

2000
_exit:
187,138✔
2001

2002
  if (code) {
187,138✔
UNCOV
2003
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2004
  }
2005

2006
  return code;
187,138✔
2007
}
2008

2009

2010
static int32_t msmUpdateCalcReaderTasks(SStreamObj* pStream, SNodeList* pSubEP) {
195,623✔
2011
  int32_t   code = TSDB_CODE_SUCCESS;
195,623✔
2012
  int32_t   lino = 0;
195,623✔
2013
  int64_t   streamId = pStream->pCreate->streamId;
195,623✔
2014
  void*     pIter = NULL;
195,623✔
2015
  SSubplan* pSubplan = NULL;
195,623✔
2016

2017
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
804,586✔
2018
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
608,963✔
2019
    (void)mstWaitLock(&pVg->lock, true);
608,963✔
2020

2021
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
608,963✔
2022
    if (atomic_load_32(&pVg->deployed) == taskNum) {
608,963✔
2023
      taosRUnLockLatch(&pVg->lock);
1,542✔
2024
      continue;
1,542✔
2025
    }
2026

2027
    for (int32_t i = 0; i < taskNum; ++i) {
3,493,753✔
2028
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
2,886,332✔
2029
      if (pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
2,886,332✔
2030
        continue;
2,296,139✔
2031
      }
2032

2033
      if (!pExt->deploy.msg.reader.triggerReader) {
590,193✔
2034
        SStreamReaderDeployFromCalc* pCalcReaderDeploy = &pExt->deploy.msg.reader.msg.calc;
352,532✔
2035
        TAOS_CHECK_EXIT(nodesStringToNode(pCalcReaderDeploy->calcScanPlan, (SNode**)&pSubplan));
352,532✔
2036
        TAOS_CHECK_EXIT(nodesCloneList(pSubEP, &pSubplan->pSubQ));
352,532✔
2037
        TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pSubplan, false, (char**)&pCalcReaderDeploy->calcScanPlan, NULL));
352,532✔
2038
        pCalcReaderDeploy->freeScanPlan = true;
352,532✔
2039
        nodesDestroyNode((SNode *)pSubplan);
352,532✔
2040
      }
2041
    }
2042

2043
    taosRUnLockLatch(&pVg->lock);
607,421✔
2044
  }
2045

2046
_exit:
195,623✔
2047
  if (code) {
195,623✔
UNCOV
2048
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2049
  }
2050

2051
  return code;
195,623✔
2052
}
2053

2054
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,941✔
2055
  if (NULL == pStream->pCreate->calcPlan) {
198,941✔
2056
    return TSDB_CODE_SUCCESS;
3,318✔
2057
  }
2058

2059
  int32_t code = TSDB_CODE_SUCCESS;
195,623✔
2060
  int32_t lino = 0;
195,623✔
2061
  int64_t streamId = pStream->pCreate->streamId;
195,623✔
2062
  SQueryPlan* pPlan = NULL;
195,623✔
2063
  SNodeList*  pSubEP = NULL;
195,623✔
2064
  SNode*      pNode = NULL;
195,623✔
2065
  int32_t     subQueryPlans = 0;
195,623✔
2066

2067
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
195,623✔
2068

2069
  FOREACH(pNode, pPlan->pChildren) {
209,545✔
2070
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
13,922✔
2071

2072
    subQueryPlans += calcSubQPlan->numOfSubplans;
13,922✔
2073
  }
2074

2075
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
782,492✔
2076
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans + subQueryPlans, sizeof(SStmTaskStatus));
586,869✔
2077
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
586,869✔
2078
  }
2079

2080
  for (int32_t i = 0; i < LIST_LENGTH(pPlan->pChildren); ++i) {
209,545✔
2081
    code = msmBuildRunnerTasksImpl(pCtx, i, pInfo, pStream, &pPlan, &pSubEP);
13,922✔
2082
    TAOS_CHECK_EXIT(code);
13,922✔
2083
  }
2084

2085
  code = msmBuildRunnerTasksImpl(pCtx, -1, pInfo, pStream, &pPlan, &pSubEP);
195,623✔
2086
  TAOS_CHECK_EXIT(code);
195,623✔
2087

2088
  taosHashClear(mStreamMgmt.toUpdateScanMap);
195,623✔
2089
  mStreamMgmt.toUpdateScanNum = 0;
195,623✔
2090

2091
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
195,623✔
2092

2093
_exit:
195,623✔
2094
  nodesDestroyNode((SNode *)pPlan);
195,623✔
2095
  if (pSubEP) {
195,623✔
2096
    nodesDestroyList(pSubEP);
12,213✔
2097
  }
2098

2099
  if (code) {
195,623✔
UNCOV
2100
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2101
  }
2102

2103
  return code;
195,623✔
2104
}
2105

2106
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,941✔
2107
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
2108
  int32_t lino = 0;
198,941✔
2109
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
2110

2111
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
198,941✔
2112

2113
  pCtx->triggerTaskId = msmAssignTaskId();
198,941✔
2114
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
198,941✔
2115
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
198,941✔
UNCOV
2116
    TAOS_CHECK_EXIT(terrno);
×
2117
  }
2118

2119
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
198,941✔
2120
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
198,941✔
2121
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
198,941✔
2122
  
2123
_exit:
198,941✔
2124

2125
  if (code) {
198,941✔
UNCOV
2126
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2127
  }
2128

2129
  return code;
198,941✔
2130
}
2131

UNCOV
2132
static int32_t msmInitTrigReaderList(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
×
UNCOV
2133
  int32_t code = TSDB_CODE_SUCCESS;
×
2134
  int32_t lino = 0;
×
2135
  int64_t streamId = pStream->pCreate->streamId;
×
2136
  SSdb   *pSdb = pCtx->pMnode->pSdb;
×
2137
  SStmTaskStatus* pState = NULL;
×
2138
  SDbObj* pDb = NULL;
×
2139
  
2140
  switch (pStream->pCreate->triggerTblType) {
×
UNCOV
2141
    case TSDB_NORMAL_TABLE:
×
2142
    case TSDB_CHILD_TABLE:
2143
    case TSDB_VIRTUAL_CHILD_TABLE:
2144
    case TSDB_VIRTUAL_NORMAL_TABLE: {
UNCOV
2145
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
×
UNCOV
2146
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2147
      pInfo->trigReaderNum = 1;
×
2148
      break;
×
2149
    }
2150
    case TSDB_SUPER_TABLE: {
×
UNCOV
2151
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
×
2152
      if (NULL == pDb) {
×
2153
        code = terrno;
×
2154
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
2155
        goto _exit;
×
2156
      }
2157

UNCOV
2158
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
UNCOV
2159
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2160
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2161
      mndReleaseDb(pCtx->pMnode, pDb);
×
2162
      pDb = NULL;
×
2163
      break;
×
2164
    }
2165
    default:
×
UNCOV
2166
      pInfo->trigReaderNum = 0;
×
2167
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2168
      break;
×
2169
  }
2170

UNCOV
2171
_exit:
×
2172

2173
  if (code) {
×
UNCOV
2174
    mndReleaseDb(pCtx->pMnode, pDb);
×
2175
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2176
  }
2177

UNCOV
2178
  return code;
×
2179
}
2180

2181

2182
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
190,456✔
2183
  int32_t code = TSDB_CODE_SUCCESS;
190,456✔
2184
  int32_t lino = 0;
190,456✔
2185
  int64_t streamId = pStream->pCreate->streamId;
190,456✔
2186

2187
  pStatus->lastActionTs = INT64_MIN;
190,456✔
2188

2189
  if (NULL == pStatus->streamName) {
190,456✔
2190
    pStatus->streamName = taosStrdup(pStream->name);
190,456✔
2191
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
190,456✔
2192
  }
2193

2194
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
190,456✔
2195
  
2196
  if (pStream->pCreate->numOfCalcSubplan > 0) {
190,456✔
2197
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
187,138✔
2198
    
2199
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
187,138✔
2200
  }
2201

2202
  if (initList) {
190,456✔
UNCOV
2203
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2204

2205
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
UNCOV
2206
    if (subPlanNum > 0) {
×
2207
      pStatus->calcReaderNum = subPlanNum;
×
2208
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2209
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2210
    }
2211

UNCOV
2212
    if (pStatus->runnerNum > 0) {
×
UNCOV
2213
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2214
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2215
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2216
      }
2217
    }
2218
  }
2219
  
2220
_exit:
190,456✔
2221

2222
  if (code) {
190,456✔
UNCOV
2223
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2224
  }
2225

2226
  return code;
190,456✔
2227
}
2228

2229
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
198,941✔
2230
  int32_t code = TSDB_CODE_SUCCESS;
198,941✔
2231
  int32_t lino = 0;
198,941✔
2232
  int64_t streamId = pStream->pCreate->streamId;
198,941✔
2233
  SStmStatus info = {0};
198,941✔
2234

2235
  if (NULL == pStatus) {
198,941✔
2236
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
190,456✔
2237

2238
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
190,456✔
2239

2240
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
190,456✔
2241
  }
2242
  
2243
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
198,941✔
2244

2245
  mstLogSStmStatus("stream deployed", streamId, pStatus);
198,941✔
2246

2247
_exit:
198,941✔
2248

2249
  if (code) {
198,941✔
UNCOV
2250
    if (NULL != pStatus) {
×
UNCOV
2251
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2252
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2253
    }
2254
    
UNCOV
2255
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2256
  }
2257

2258
  return code;
198,941✔
2259
}
2260

2261

2262
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
9,161✔
2263
  int32_t code = TSDB_CODE_SUCCESS;
9,161✔
2264
  void* pIter = NULL;
9,161✔
2265

2266
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
17,817✔
2267
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
8,656✔
2268
    (void)mstWaitLock(&pVg->lock, true);
8,656✔
2269

2270
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
8,656✔
2271
    if (atomic_load_32(&pVg->deployed) == taskNum) {
8,656✔
UNCOV
2272
      taosRUnLockLatch(&pVg->lock);
×
UNCOV
2273
      continue;
×
2274
    }
2275

2276
    for (int32_t i = 0; i < taskNum; ++i) {
36,962✔
2277
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
28,306✔
2278
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
28,306✔
2279
        continue;
28,306✔
2280
      }
2281

UNCOV
2282
      mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2283
      pExt->deployed = true;
×
2284
    }
2285
    
2286
    taosRUnLockLatch(&pVg->lock);
8,656✔
2287
  }
2288

2289
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
17,423✔
2290
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
8,262✔
2291
    (void)mstWaitLock(&pSnode->lock, true);
8,262✔
2292

2293
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
8,262✔
2294
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
8,262✔
2295
      for (int32_t i = 0; i < taskNum; ++i) {
19,432✔
2296
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
13,378✔
2297
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
13,378✔
2298
          continue;
13,378✔
2299
        }
2300
        
UNCOV
2301
        mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2302
        pExt->deployed = true;
×
2303
      }
2304
    }
2305

2306
    taskNum = taosArrayGetSize(pSnode->runnerList);
8,262✔
2307
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
8,262✔
2308
      for (int32_t i = 0; i < taskNum; ++i) {
48,396✔
2309
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
40,134✔
2310
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
40,134✔
2311
          continue;
40,134✔
2312
        }
2313
        
UNCOV
2314
        mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2315
        pExt->deployed = true;
×
2316
      }
2317
    }
2318

2319
    taosRUnLockLatch(&pSnode->lock);
8,262✔
2320
  }
2321

2322
  
2323
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
25,498✔
2324
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
16,337✔
2325
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
16,337✔
2326
    if (TSDB_CODE_SUCCESS == code) {
16,337✔
2327
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
11,553✔
2328
    }
2329
  }
2330

2331
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
41,561✔
2332
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
32,400✔
2333
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
32,400✔
2334
    if (TSDB_CODE_SUCCESS == code) {
32,400✔
2335
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
13,856✔
2336
    }
2337
  }
2338

2339
  size_t keyLen = 0;
9,161✔
2340
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
697,324✔
2341
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
688,163✔
2342
    if (*pStreamId == streamId) {
688,163✔
2343
      int64_t taskId = *(pStreamId + 1);
57,813✔
2344
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
57,813✔
2345
      if (code) {
57,813✔
UNCOV
2346
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2347
      } else {
2348
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
57,813✔
2349
      }
2350
    }
2351
  }
2352

2353
  if (fromStreamMap) {
9,161✔
2354
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
676✔
2355
    if (code) {
676✔
UNCOV
2356
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2357
    } else {
2358
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
676✔
2359
    }
2360
  }
2361
  
2362
  return code;
9,161✔
2363
}
2364

2365
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
8,485✔
2366
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
8,485✔
2367
  
2368
  (void)msmSTRemoveStream(streamId, false);  
8,485✔
2369

2370
  mstResetSStmStatus(pStatus);
8,485✔
2371

2372
  pStatus->deployTimes++;
8,485✔
2373
}
8,485✔
2374

2375
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
200,376✔
2376
  int32_t code = TSDB_CODE_SUCCESS;
200,376✔
2377
  int32_t lino = 0;
200,376✔
2378
  int64_t streamId = pAction->streamId;
200,376✔
2379
  char* streamName = pAction->streamName;
200,376✔
2380
  SStreamObj* pStream = NULL;
200,376✔
2381
  int8_t stopped = 0;
200,376✔
2382

2383
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
200,376✔
2384
  if (pStatus) {
200,376✔
2385
    stopped = atomic_load_8(&pStatus->stopped);
8,485✔
2386
    if (0 == stopped) {
8,485✔
UNCOV
2387
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
×
UNCOV
2388
      msmResetStreamForRedeploy(streamId, pStatus);
×
2389
    } else {
2390
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
8,485✔
UNCOV
2391
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
UNCOV
2392
        return code;
×
2393
      }
2394
      
2395
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
8,485✔
2396
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
8,485✔
2397
        msmResetStreamForRedeploy(streamId, pStatus);
8,485✔
2398
      }
2399
    }
2400
  }
2401

2402
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
200,376✔
2403
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
200,376✔
2404
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
1,435✔
2405
    return TSDB_CODE_SUCCESS;
1,435✔
2406
  }
2407

2408
  TAOS_CHECK_EXIT(code);
198,941✔
2409

2410
  if (pStatus && pStream->pCreate->streamId != streamId) {
198,941✔
UNCOV
2411
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
UNCOV
2412
    atomic_store_8(&pStatus->stopped, 2);
×
2413
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2414
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2415
  }
2416

2417
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
198,941✔
2418
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
198,941✔
2419
  if (userStopped || userDropped) {
198,941✔
UNCOV
2420
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
×
UNCOV
2421
    goto _exit;
×
2422
  }
2423
  
2424
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
198,941✔
2425

2426
_exit:
198,941✔
2427

2428
  mndReleaseStream(pCtx->pMnode, pStream);
198,941✔
2429

2430
  if (code) {
198,941✔
UNCOV
2431
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2432
  }
2433

2434
  return code;
198,941✔
2435
}
2436

2437
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
1,302✔
2438
  int32_t code = TSDB_CODE_SUCCESS;
1,302✔
2439
  int32_t lino = 0;
1,302✔
2440
  int64_t streamId = pAction->streamId;
1,302✔
2441
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
1,302✔
2442
  if (NULL == ppTask) {
1,302✔
UNCOV
2443
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
×
UNCOV
2444
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2445
  }
2446
  
2447
  SStmTaskDeploy info = {0};
1,302✔
2448
  info.task.type = pAction->type;
1,302✔
2449
  info.task.streamId = pAction->streamId;
1,302✔
2450
  info.task.taskId = pAction->id.taskId;
1,302✔
2451
  info.task.seriousId = (*ppTask)->id.seriousId;
1,302✔
2452
  info.task.nodeId = pAction->id.nodeId;
1,302✔
2453
  info.task.taskIdx = pAction->id.taskIdx;
1,302✔
2454
  
2455
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
1,302✔
2456
  SStreamCalcScan* scanPlan = NULL;
1,302✔
2457
  if (!isTriggerReader) {
1,302✔
2458
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
552✔
2459
    if (NULL == scanPlan) {
552✔
UNCOV
2460
      mstsError("fail to get TASK:%" PRId64 " scanPlan, taskIdx:%d, scanPlanNum:%zu", 
×
2461
          pAction->id.taskId, pAction->id.taskIdx, taosArrayGetSize(pStatus->pCreate->calcScanPlanList));
2462
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2463
    }
2464
  }
2465
  
2466
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, scanPlan ? scanPlan->scanPlan : NULL, pStatus, isTriggerReader));
1,302✔
2467
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
1,302✔
2468

2469
_exit:
1,302✔
2470

2471
  if (code) {
1,302✔
UNCOV
2472
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2473
  }
2474

2475
  return code;
1,302✔
2476
}
2477

2478
/*
2479
static int32_t msmReLaunchTriggerTask(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2480
  int32_t code = TSDB_CODE_SUCCESS;
2481
  int32_t lino = 0;
2482
  int64_t streamId = pAction->streamId;
2483
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2484
  if (NULL == ppTask) {
2485
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
2486
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
2487
  }
2488
  
2489
  (*ppTask)->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2490
  if (!GOT_SNODE((*ppTask)->id.nodeId)) {
2491
    mstsError("no avaible snode for deploying trigger task, seriousId: %" PRId64, (*ppTask)->id.seriousId);
2492
    return TSDB_CODE_SUCCESS;
2493
  }
2494
  
2495
  SStmTaskDeploy info = {0};
2496
  info.task.type = pAction->type;
2497
  info.task.streamId = streamId;
2498
  info.task.taskId = pAction->id.taskId;
2499
  info.task.seriousId = (*ppTask)->id.seriousId;
2500
  info.task.nodeId = (*ppTask)->id.nodeId;
2501
  info.task.taskIdx = pAction->id.taskIdx;
2502
  
2503
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2504
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2505
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, *ppTask, 1, -1));
2506
  
2507
  atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2508

2509
_exit:
2510

2511
  if (code) {
2512
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2513
  }
2514

2515
  return code;
2516
}
2517
*/
2518

2519
static int32_t msmReLaunchRunnerDeploy(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
253✔
2520
  int32_t code = TSDB_CODE_SUCCESS;
253✔
2521
  int32_t lino = 0;
253✔
2522
  int64_t streamId = pAction->streamId;
253✔
2523
  
2524
/*
2525
  if (pAction->triggerStatus) {
2526
    pCtx->triggerTaskId = pAction->triggerStatus->id.taskId;
2527
    pAction->triggerStatus->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2528
    if (!GOT_SNODE(pAction->triggerStatus->id.nodeId)) {
2529
      mstsError("no avaible snode for deploying trigger task, seriousId:%" PRId64, pAction->triggerStatus->id.seriousId);
2530
      return TSDB_CODE_SUCCESS;
2531
    }
2532
  
2533
    pCtx->triggerNodeId = pAction->triggerStatus->id.nodeId;
2534
  } else {
2535
*/
2536
  pCtx->triggerTaskId = pStatus->triggerTask->id.taskId;
253✔
2537
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
253✔
2538
//  }
2539
  
2540
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
253✔
2541
  
2542
  SQueryPlan* pPlan = NULL;
253✔
2543
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
253✔
2544
  
2545
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
253✔
2546
  
2547
  taosHashClear(mStreamMgmt.toUpdateScanMap);
253✔
2548
  mStreamMgmt.toUpdateScanNum = 0;
253✔
2549
  
2550
/*
2551
  if (pAction->triggerStatus) {
2552
    SStmTaskDeploy info = {0};
2553
    info.task.type = STREAM_TRIGGER_TASK;
2554
    info.task.streamId = streamId;
2555
    info.task.taskId = pCtx->triggerTaskId;
2556
    info.task.seriousId = pAction->triggerStatus->id.seriousId;
2557
    info.task.nodeId = pCtx->triggerNodeId;
2558
    info.task.taskIdx = 0;
2559
  
2560
    TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2561
    TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2562
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pAction->triggerStatus, 1, -1));
2563
    
2564
    atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2565
  }
2566
*/
2567

2568
_exit:
253✔
2569

2570
  if (code) {
253✔
UNCOV
2571
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2572
  }
2573

2574
  return code;
253✔
2575
}
2576

2577

2578
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
1,555✔
2579
  int32_t code = TSDB_CODE_SUCCESS;
1,555✔
2580
  int32_t lino = 0;
1,555✔
2581
  int64_t streamId = pAction->streamId;
1,555✔
2582
  int64_t taskId = pAction->id.taskId;
1,555✔
2583
  SStreamObj* pStream = NULL;
1,555✔
2584

2585
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
1,555✔
2586

2587
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,555✔
2588
  if (NULL == pStatus) {
1,555✔
UNCOV
2589
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
2590
    return TSDB_CODE_SUCCESS;
×
2591
  }
2592

2593
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,555✔
2594
  if (stopped) {
1,555✔
UNCOV
2595
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
UNCOV
2596
    return TSDB_CODE_SUCCESS;
×
2597
  }
2598

2599
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,555✔
2600
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,555✔
UNCOV
2601
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
UNCOV
2602
    return TSDB_CODE_SUCCESS;
×
2603
  }
2604

2605
  TAOS_CHECK_EXIT(code);
1,555✔
2606

2607
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,555✔
2608
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,555✔
2609
  if (userStopped || userDropped) {
1,555✔
UNCOV
2610
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
UNCOV
2611
    goto _exit;
×
2612
  }
2613

2614
  switch (pAction->type) {
1,555✔
2615
    case STREAM_READER_TASK:
1,302✔
2616
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,302✔
2617
      break;
1,302✔
2618
/*
2619
    case STREAM_TRIGGER_TASK:
2620
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2621
      break;
2622
*/
2623
    case STREAM_RUNNER_TASK:
253✔
2624
      if (pAction->multiRunner) {
253✔
2625
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
253✔
2626
      } else {
UNCOV
2627
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
UNCOV
2628
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2629
      }
2630
      break;
253✔
UNCOV
2631
    default:
×
UNCOV
2632
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2633
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2634
      break;
×
2635
  }
2636

2637
_exit:
1,555✔
2638

2639
  if (pStream) {
1,555✔
2640
    mndReleaseStream(pCtx->pMnode, pStream);
1,555✔
2641
  }
2642

2643
  if (code) {
1,555✔
UNCOV
2644
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
UNCOV
2645
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2646
  }
2647

2648
  return code;
1,555✔
2649
}
2650

UNCOV
2651
static int32_t msmTDRemoveStream(int64_t streamId) {
×
UNCOV
2652
  void* pIter = NULL;
×
2653
  
2654
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
×
UNCOV
2655
    while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
×
2656
      SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
×
2657
      int32_t taskNum = taosArrayGetSize(pVg->taskList);
×
2658
      if (atomic_load_32(&pVg->deployed) == taskNum) {
×
2659
        continue;
×
2660
      }
2661
      
UNCOV
2662
      for (int32_t i = 0; i < taskNum; ++i) {
×
UNCOV
2663
        SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
×
2664
        if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2665
          pExt->deployed = true;
×
2666
        }
2667
      }
2668
    }
2669
  }
2670

UNCOV
2671
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
UNCOV
2672
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2673
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2674
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2675
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2676
        for (int32_t i = 0; i < taskNum; ++i) {
×
2677
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2678
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2679
            pExt->deployed = true;
×
2680
          }
2681
        }
2682
      }
2683

UNCOV
2684
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
UNCOV
2685
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2686
        for (int32_t i = 0; i < taskNum; ++i) {
×
2687
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2688
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2689
            pExt->deployed = true;
×
2690
          }
2691
        }
2692
      }
2693
    }
2694
  }
2695

UNCOV
2696
  return TSDB_CODE_SUCCESS;
×
2697
}
2698

2699
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
676✔
2700
  int32_t code = TSDB_CODE_SUCCESS;
676✔
2701
  int32_t lino = 0;
676✔
2702

2703
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
676✔
2704

2705
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
676✔
2706

2707
_exit:
676✔
2708

2709
  if (code) {
676✔
UNCOV
2710
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2711
  } else {
2712
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
676✔
2713
  }
2714

2715
  return code;
676✔
2716
}
2717

2718
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
36,364✔
2719
  int32_t code = TSDB_CODE_SUCCESS;
36,364✔
2720
  int32_t lino = 0;
36,364✔
2721

2722
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
36,364✔
2723
  if (0 == active || MND_STM_STATE_NORMAL != state) {
36,364✔
2724
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
321✔
2725
    return;
321✔
2726
  }
2727

2728
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
36,043✔
2729
  if (NULL == pStream) {
36,043✔
2730
    mstsInfo("stream %s already not in streamMap", streamName);
1,612✔
2731
    goto _exit;
1,612✔
2732
  }
2733

2734
  atomic_store_8(&pStream->stopped, 2);
34,431✔
2735

2736
  mstsInfo("set stream %s stopped by user", streamName);
34,431✔
2737

UNCOV
2738
_exit:
×
2739

2740
  taosHashRelease(mStreamMgmt.streamMap, pStream);
36,043✔
2741

2742
  if (code) {
36,043✔
UNCOV
2743
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2744
  }
2745

2746
  return;
36,043✔
2747
}
2748

2749
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,392✔
2750
  int32_t code = TSDB_CODE_SUCCESS;
3,392✔
2751
  int32_t lino = 0;
3,392✔
2752

2753
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,392✔
2754
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,392✔
UNCOV
2755
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
UNCOV
2756
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2757
  }
2758

2759
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,392✔
2760
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,392✔
UNCOV
2761
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
UNCOV
2762
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2763
    goto _exit;
×
2764
  }
2765

2766
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,392✔
2767

2768
_exit:
3,392✔
2769

2770
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,392✔
2771

2772
  if (code) {
3,392✔
UNCOV
2773
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2774
  }
2775

2776
  return code;
3,392✔
2777
}
2778

2779
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
52,913✔
2780
  int32_t code = TSDB_CODE_SUCCESS;
52,913✔
2781
  int32_t lino = 0;
52,913✔
2782
  SStmQNode* pQNode = NULL;
52,913✔
2783

2784
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
254,844✔
2785
    switch (pQNode->type) {
201,931✔
2786
      case STREAM_ACT_DEPLOY:
201,931✔
2787
        if (pQNode->streamAct) {
201,931✔
2788
          mstDebug("start to handle stream deploy action");
200,376✔
2789
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
200,376✔
2790
        } else {
2791
          mstDebug("start to handle task deploy action");
1,555✔
2792
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,555✔
2793
        }
2794
        break;
201,931✔
UNCOV
2795
      default:
×
UNCOV
2796
        break;
×
2797
    }
2798
  }
2799

2800
_exit:
52,913✔
2801

2802
  if (code) {
52,913✔
UNCOV
2803
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2804
  }
2805
}
52,913✔
2806

UNCOV
2807
void msmStopAllStreamsByGrant(int32_t errCode) {
×
UNCOV
2808
  SStmStatus* pStatus = NULL;
×
2809
  void* pIter = NULL;
×
2810
  int64_t streamId = 0;
×
2811
  
2812
  while (true) {
UNCOV
2813
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
UNCOV
2814
    if (NULL == pIter) {
×
2815
      break;
×
2816
    }
2817

UNCOV
2818
    pStatus = (SStmStatus*)pIter;
×
2819

2820
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
UNCOV
2821
    atomic_store_8(&pStatus->stopped, 4);
×
2822

2823
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2824
  }
2825
}
×
2826

2827
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
UNCOV
2828
  mstInfo("stream grant expired");
×
2829

2830
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
UNCOV
2831
    mstWarn("mnode stream is NOT active, ignore handling");
×
2832
    return errCode;
×
2833
  }
2834

UNCOV
2835
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2836

2837
  msmStopAllStreamsByGrant(errCode);
×
2838

2839
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2840
  
2841
  return errCode;
×
2842
}
2843

2844
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,564,119✔
2845
  int32_t code = TSDB_CODE_SUCCESS;
1,564,119✔
2846
  int32_t lino = 0;
1,564,119✔
2847
  int64_t streamId = pDeploy->task.streamId;
1,564,119✔
2848
  
2849
  switch (pDeploy->task.type) {
1,564,119✔
2850
    case STREAM_READER_TASK:
674,455✔
2851
      if (NULL == pStream->readerTasks) {
674,455✔
2852
        pStream->streamId = streamId;
258,267✔
2853
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
258,267✔
2854
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
258,267✔
2855
      }
2856
      
2857
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,348,910✔
2858
      break;
674,455✔
2859
    case STREAM_TRIGGER_TASK:
198,941✔
2860
      pStream->streamId = streamId;
198,941✔
2861
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
198,941✔
2862
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
198,941✔
2863
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
198,941✔
2864
      break;
198,941✔
2865
    case STREAM_RUNNER_TASK:
690,723✔
2866
      if (NULL == pStream->runnerTasks) {
690,723✔
2867
        pStream->streamId = streamId;
205,072✔
2868
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
205,072✔
2869
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
205,072✔
2870
      }      
2871
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,381,446✔
2872
      break;
690,723✔
UNCOV
2873
    default:
×
UNCOV
2874
      break;
×
2875
  }
2876

2877
_exit:
1,564,119✔
2878

2879
  if (code) {
1,564,119✔
UNCOV
2880
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2881
  }
2882

2883
  return code;
1,564,119✔
2884
}
2885

2886
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,564,119✔
2887
  int32_t code = TSDB_CODE_SUCCESS;
1,564,119✔
2888
  int32_t lino = 0;
1,564,119✔
2889
  int64_t streamId = pDeploy->task.streamId;
1,564,119✔
2890
  SStreamTask* pTask = &pDeploy->task;
1,564,119✔
2891
  SStmStreamDeploy streamDeploy = {0};
1,564,119✔
2892
  SStmStreamDeploy* pStream = NULL;
1,564,119✔
2893
   
2894
  while (true) {
2895
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,564,119✔
2896
    if (NULL == pStream) {
1,564,119✔
2897
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
274,566✔
2898
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
274,566✔
2899
      if (TSDB_CODE_SUCCESS == code) {
274,566✔
2900
        goto _exit;
274,566✔
2901
      }
2902

UNCOV
2903
      if (TSDB_CODE_DUP_KEY != code) {
×
UNCOV
2904
        goto _exit;
×
2905
      }    
2906

UNCOV
2907
      tFreeSStmStreamDeploy(&streamDeploy);
×
UNCOV
2908
      continue;
×
2909
    }
2910

2911
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,289,553✔
2912
    
2913
    break;
1,289,553✔
2914
  }
2915
  
2916
_exit:
1,564,119✔
2917

2918
  taosHashRelease(pHash, pStream);
1,564,119✔
2919

2920
  if (code) {
1,564,119✔
UNCOV
2921
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2922
  } else {
2923
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,564,119✔
2924
  }
2925

2926
  return code;
1,564,119✔
2927
}
2928

2929

2930
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
287,200✔
2931
  int32_t code = TSDB_CODE_SUCCESS;
287,200✔
2932
  int32_t lino = 0;
287,200✔
2933
  int32_t taskNum = taosArrayGetSize(pTasks);
287,200✔
2934

2935
  for (int32_t i = 0; i < taskNum; ++i) {
1,859,800✔
2936
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,572,600✔
2937
    if (pExt->deployed) {
1,572,600✔
2938
      continue;
8,481✔
2939
    }
2940

2941
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,564,119✔
2942
    pExt->deployed = true;
1,564,119✔
2943

2944
    (void)atomic_add_fetch_32(deployed, 1);
1,564,119✔
2945
  }
2946

2947
_exit:
287,200✔
2948

2949
  if (code) {
287,200✔
UNCOV
2950
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2951
  }
2952

2953
  return code;
287,200✔
2954
}
2955

2956
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
79,907✔
2957
  int32_t code = TSDB_CODE_SUCCESS;
79,907✔
2958
  int32_t lino = 0;
79,907✔
2959
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
79,907✔
2960
  SStmVgTasksToDeploy* pVg = NULL;
79,907✔
2961
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2962

2963
  mstDebug("start to add stream vgroup tasks deploy");
79,907✔
2964
  
2965
  for (int32_t i = 0; i < vgNum; ++i) {
517,829✔
2966
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
437,922✔
2967

2968
    msmUpdateVgroupUpTs(pCtx, *vgId);
437,922✔
2969

2970
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
437,922✔
2971
    if (NULL == pVg) {
437,922✔
2972
      continue;
261,490✔
2973
    }
2974

2975
    if (taosRTryLockLatch(&pVg->lock)) {
176,432✔
UNCOV
2976
      continue;
×
2977
    }
2978
    
2979
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
176,432✔
2980
      taosRUnLockLatch(&pVg->lock);
3,084✔
2981
      continue;
3,084✔
2982
    }
2983
    
2984
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
173,348✔
2985
    taosRUnLockLatch(&pVg->lock);
173,348✔
2986
  }
2987

2988
_exit:
79,907✔
2989

2990
  if (code) {
79,907✔
UNCOV
2991
    if (pVg) {
×
UNCOV
2992
      taosRUnLockLatch(&pVg->lock);
×
2993
    }
2994

UNCOV
2995
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2996
  }
2997

2998
  return code;
79,907✔
2999
}
3000

3001
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
79,786✔
3002
  int32_t code = TSDB_CODE_SUCCESS;
79,786✔
3003
  int32_t lino = 0;
79,786✔
3004
  SStmSnodeTasksDeploy* pSnode = NULL;
79,786✔
3005
  SStreamHbMsg* pReq = pCtx->pReq;
79,786✔
3006

3007
  mstDebug("start to add stream snode tasks deploy");
79,786✔
3008
  
3009
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
79,786✔
3010
  if (NULL == pSnode) {
79,786✔
3011
    return TSDB_CODE_SUCCESS;
19,223✔
3012
  }
3013

3014
  (void)mstWaitLock(&pSnode->lock, false);
60,563✔
3015
  
3016
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
60,563✔
3017
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
54,831✔
3018
  }
3019

3020
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
60,563✔
3021
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
59,021✔
3022
  }
3023
  
3024
  taosWUnLockLatch(&pSnode->lock);
60,563✔
3025

3026
_exit:
60,563✔
3027

3028
  if (code) {
60,563✔
UNCOV
3029
    if (pSnode) {
×
UNCOV
3030
      taosWUnLockLatch(&pSnode->lock);
×
3031
    }
3032

UNCOV
3033
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3034
  }
3035

3036
  return code;
60,563✔
3037
}
3038

3039
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
723,760✔
3040
  int32_t code = TSDB_CODE_SUCCESS;
723,760✔
3041
  int32_t lino = 0;
723,760✔
3042
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
723,760✔
3043
  if (NULL == pStatus) {
723,760✔
UNCOV
3044
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
3045
    return TSDB_CODE_SUCCESS;
×
3046
  }
3047
  
3048
  pStatus->lastActionTs = currTs;
723,760✔
3049

3050
_exit:
723,760✔
3051

3052
  if (code) {
723,760✔
UNCOV
3053
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3054
  }
3055

3056
  return code;
723,760✔
3057
}
3058

3059
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
81,568✔
3060
  int32_t code = TSDB_CODE_SUCCESS;
81,568✔
3061
  int32_t lino = 0;
81,568✔
3062
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
81,568✔
3063
  void* pIter = NULL;
81,568✔
3064

3065
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
81,568✔
3066
  
3067
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
81,568✔
3068
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
81,568✔
3069

3070
  while (1) {
274,566✔
3071
    pIter = taosHashIterate(pCtx->deployStm, pIter);
356,134✔
3072
    if (pIter == NULL) {
356,134✔
3073
      break;
81,568✔
3074
    }
3075
    
3076
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
274,566✔
3077
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
549,132✔
3078

3079
    int64_t streamId = pDeploy->streamId;
274,566✔
3080
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
274,566✔
3081
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
3082

3083
    mstClearSStmStreamDeploy(pDeploy);
274,566✔
3084
    
3085
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
274,566✔
3086
  }
3087
  
3088
_exit:
81,568✔
3089

3090
  if (pIter) {
81,568✔
UNCOV
3091
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3092
  }
3093

3094
  if (code) {
81,568✔
UNCOV
3095
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3096
  }
3097
  
3098
  return code;
81,568✔
3099
}
3100

3101
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
79,136✔
3102
  int32_t code = TSDB_CODE_SUCCESS;
79,136✔
3103
  int32_t lino = 0;
79,136✔
3104
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
79,136✔
3105
  SStmVgTasksToDeploy* pVg = NULL;
79,136✔
3106
  
3107
  for (int32_t i = 0; i < vgNum; ++i) {
511,661✔
3108
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
432,525✔
3109
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
432,525✔
3110
    if (NULL == pVg) {
432,525✔
3111
      continue;
260,719✔
3112
    }
3113

3114
    if (taosWTryLockLatch(&pVg->lock)) {
171,806✔
UNCOV
3115
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
UNCOV
3116
      continue;
×
3117
    }
3118
    
3119
    if (atomic_load_32(&pVg->deployed) <= 0) {
171,806✔
UNCOV
3120
      taosWUnLockLatch(&pVg->lock);
×
UNCOV
3121
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3122
      continue;
×
3123
    }
3124

3125
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
171,806✔
3126
    if (atomic_load_32(&pVg->deployed) == taskNum) {
171,806✔
3127
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
170,264✔
3128
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
170,264✔
3129
      pVg->taskList = NULL;
170,264✔
3130
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
170,264✔
3131
      taosWUnLockLatch(&pVg->lock);
170,264✔
3132
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
170,264✔
3133
      continue;
170,264✔
3134
    }
3135

3136
    for (int32_t m = taskNum - 1; m >= 0; --m) {
23,130✔
3137
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
21,588✔
3138
      if (!pExt->deployed) {
21,588✔
3139
        continue;
1,542✔
3140
      }
3141

3142
      mstDestroySStmTaskToDeployExt(pExt);
20,046✔
3143

3144
      taosArrayRemove(pVg->taskList, m);
20,046✔
3145
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
20,046✔
3146
    }
3147
    atomic_store_32(&pVg->deployed, 0);
1,542✔
3148
    taosWUnLockLatch(&pVg->lock);
1,542✔
3149
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
1,542✔
3150
  }
3151

3152
_exit:
79,136✔
3153

3154
  if (code) {
79,136✔
UNCOV
3155
    if (pVg) {
×
UNCOV
3156
      taosWUnLockLatch(&pVg->lock);
×
3157
    }
3158

UNCOV
3159
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3160
  }
3161
}
79,136✔
3162

3163
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
89,460✔
3164
  if (!GOT_SNODE(snodeId)) {
89,460✔
3165
    return;
10,445✔
3166
  }
3167
  
3168
  int32_t code = TSDB_CODE_SUCCESS;
79,015✔
3169
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
79,015✔
3170
  if (NULL == pSnode) {
79,015✔
3171
    return;
19,223✔
3172
  }
3173

3174
  if (taosWTryLockLatch(&pSnode->lock)) {
59,792✔
UNCOV
3175
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
UNCOV
3176
    return;
×
3177
  }
3178

3179
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
59,792✔
3180
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
59,792✔
3181
  
3182
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
59,792✔
UNCOV
3183
    taosWUnLockLatch(&pSnode->lock);
×
UNCOV
3184
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3185
    return;
×
3186
  }
3187

3188
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
59,792✔
3189
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
56,708✔
3190
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
56,708✔
3191
    pSnode->triggerList = NULL;
56,708✔
3192
  }
3193

3194
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
59,792✔
3195
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
56,708✔
3196
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
56,708✔
3197
    pSnode->runnerList = NULL;
56,708✔
3198
  }
3199

3200
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
59,792✔
3201
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
55,166✔
3202
    taosWUnLockLatch(&pSnode->lock);
55,166✔
3203
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
55,166✔
3204
    return;
55,166✔
3205
  }
3206

3207
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
4,626✔
3208
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
31,611✔
3209
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
29,298✔
3210
      if (!pExt->deployed) {
29,298✔
3211
        continue;
1,542✔
3212
      }
3213

3214
      mstDestroySStmTaskToDeployExt(pExt);
27,756✔
3215
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
27,756✔
3216
      taosArrayRemove(pSnode->triggerList, m);
27,756✔
3217
    }
3218
    
3219
    pSnode->triggerDeployed = 0;
2,313✔
3220
  }
3221

3222
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
4,626✔
3223
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
60,138✔
3224
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
57,054✔
3225
      if (!pExt->deployed) {
57,054✔
3226
        continue;
3,855✔
3227
      }
3228

3229
      mstDestroySStmTaskToDeployExt(pExt);
53,199✔
3230
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
53,199✔
3231
      taosArrayRemove(pSnode->runnerList, m);
53,199✔
3232
    }
3233
    
3234
    pSnode->runnerDeployed = 0;
3,084✔
3235
  }
3236
  
3237
  taosWUnLockLatch(&pSnode->lock);
4,626✔
3238
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
4,626✔
3239
}
3240

3241
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,846,615✔
3242
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,846,615✔
3243
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
79,136✔
3244
  }
3245

3246
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,846,615✔
3247
    msmCleanDeployedSnodeTasks(pHb->snodeId);
89,460✔
3248
  }
3249
}
16,846,615✔
3250

3251
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
16,846,615✔
3252
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,846,615✔
3253
  if (mStreamMgmt.tCtx) {
16,846,615✔
3254
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
16,764,115✔
3255
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
16,764,115✔
3256
  }
3257
}
16,846,615✔
3258

3259
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
196,871✔
3260
  int32_t code = TSDB_CODE_SUCCESS;
196,871✔
3261
  int32_t lino = 0;
196,871✔
3262
  int32_t action = STREAM_ACT_START;
196,871✔
3263
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
196,871✔
3264
  if (pAction) {
196,871✔
UNCOV
3265
    pAction->actions |= action;
×
UNCOV
3266
    pAction->start.triggerId = *pId;
×
3267
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3268
  } else {
3269
    SStmAction newAction = {0};
196,871✔
3270
    newAction.actions = action;
196,871✔
3271
    newAction.start.triggerId = *pId;
196,871✔
3272
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
196,871✔
3273
    mstsDebug("stream add START action, actions:%x", newAction.actions);
196,871✔
3274
  }
3275

3276
_exit:
196,871✔
3277

3278
  if (code) {
196,871✔
UNCOV
3279
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3280
  }
3281

3282
  return code;
196,871✔
3283
}
3284

UNCOV
3285
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
UNCOV
3286
  int32_t code = TSDB_CODE_SUCCESS;
×
3287
  int32_t lino = 0;
×
3288
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3289
  
3290
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
UNCOV
3291
  if (pAction) {
×
3292
    pAction->actions |= action;
×
3293
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3294
  } else {
3295
    SStmAction newAction = {0};
×
UNCOV
3296
    newAction.actions = action;
×
3297
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3298
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3299
  }
3300

UNCOV
3301
_exit:
×
3302

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

UNCOV
3307
  return code;
×
3308
}
3309

3310

3311

3312
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
252,323✔
3313
  int32_t code = TSDB_CODE_SUCCESS;
252,323✔
3314
  int32_t lino = 0;
252,323✔
3315
  int32_t action = STREAM_ACT_UNDEPLOY;
252,323✔
3316
  bool    dropped = false;
252,323✔
3317

3318
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
252,323✔
3319
  mstsDebug("stream dropped: %d", dropped);
252,323✔
3320
  
3321
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
252,323✔
3322
  if (pAction) {
252,323✔
3323
    pAction->actions |= action;
201,136✔
3324
    if (NULL == pAction->undeploy.taskList) {
201,136✔
UNCOV
3325
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
UNCOV
3326
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3327
    }
3328

3329
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
402,272✔
3330
    if (pAction->undeploy.doCheckpoint) {
201,136✔
3331
      pAction->undeploy.doCheckpoint = dropped ? false : true;
36,792✔
3332
    }
3333
    if (!pAction->undeploy.doCleanup) {
201,136✔
3334
      pAction->undeploy.doCleanup = dropped ? true : false;
36,792✔
3335
    }
3336
    
3337
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
201,136✔
3338
  } else {
3339
    SStmAction newAction = {0};
51,187✔
3340
    newAction.actions = action;
51,187✔
3341
    newAction.undeploy.doCheckpoint = dropped ? false : true;
51,187✔
3342
    newAction.undeploy.doCleanup = dropped ? true : false;
51,187✔
3343
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
51,187✔
3344
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
51,187✔
3345
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
102,374✔
3346
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
51,187✔
3347
    
3348
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
51,187✔
3349
  }
3350

3351
_exit:
252,323✔
3352

3353
  if (code) {
252,323✔
UNCOV
3354
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3355
  }
3356

3357
  return code;
252,323✔
3358
}
3359

3360
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,392✔
3361
  int32_t code = TSDB_CODE_SUCCESS;
3,392✔
3362
  int32_t lino = 0;
3,392✔
3363
  int32_t action = STREAM_ACT_RECALC;
3,392✔
3364
  SStmAction newAction = {0};
3,392✔
3365
  
3366
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,392✔
3367
  if (pAction) {
3,392✔
UNCOV
3368
    pAction->actions |= action;
×
UNCOV
3369
    pAction->recalc.recalcList = recalcList;
×
3370

3371
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3372
  } else {
3373
    newAction.actions = action;
3,392✔
3374
    newAction.recalc.recalcList = recalcList;
3,392✔
3375
    
3376
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,392✔
3377
    
3378
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,392✔
3379
  }
3380

3381
_exit:
3,392✔
3382

3383
  if (code) {
3,392✔
UNCOV
3384
    mstDestroySStmAction(&newAction);
×
UNCOV
3385
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3386
  }
3387

3388
  return code;
3,392✔
3389
}
3390

3391
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
273,205✔
3392
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
273,205✔
3393
  if (NULL == pStream) {
273,205✔
UNCOV
3394
    return false;
×
3395
  }
3396

3397
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
273,205✔
UNCOV
3398
    return false;
×
3399
  }
3400

3401
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
273,205✔
3402
  for (int32_t i = 0; i < readerNum; ++i) {
602,657✔
3403
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
329,452✔
3404
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
329,452✔
UNCOV
3405
      return false;
×
3406
    }
3407
  }
3408

3409
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
273,205✔
3410
  for (int32_t i = 0; i < readerNum; ++i) {
273,205✔
UNCOV
3411
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
×
UNCOV
3412
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
×
3413
      return false;
×
3414
    }
3415
  }
3416

3417
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
273,205✔
3418
  SListNode* pNode = listHead(pStream->calcReaders);
273,205✔
3419
  for (int32_t i = 0; i < readerNum; ++i) {
831,998✔
3420
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
558,793✔
3421
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
558,793✔
UNCOV
3422
      return false;
×
3423
    }
3424
    pNode = TD_DLIST_NODE_NEXT(pNode);
558,793✔
3425
  }
3426

3427
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
857,659✔
3428
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
660,788✔
3429
    for (int32_t m = 0; m < runnerNum; ++m) {
1,346,792✔
3430
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
762,338✔
3431
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
762,338✔
3432
        return false;
76,334✔
3433
      }
3434
    }
3435
  }
3436
  
3437
  return true;
196,871✔
3438
}
3439

3440

3441
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
11,799,502✔
3442
  int32_t code = TSDB_CODE_SUCCESS;
11,799,502✔
3443
  int32_t lino = 0;
11,799,502✔
3444
  int32_t action = 0;
11,799,502✔
3445
  int64_t streamId = pMsg->streamId;
11,799,502✔
3446
  SStreamTask* pTask = (SStreamTask*)pMsg;
11,799,502✔
3447
  int8_t  stopped = 0;
11,799,502✔
3448

3449
  msttDebug("start to handle task abnormal status %d", pTask->status);
11,799,502✔
3450
  
3451
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
11,799,502✔
3452
  if (NULL == pStatus) {
11,799,502✔
UNCOV
3453
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
UNCOV
3454
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3455
    return;
11,998✔
3456
  }
3457

3458
  stopped = atomic_load_8(&pStatus->stopped);
11,799,502✔
3459
  if (stopped) {
11,799,502✔
UNCOV
3460
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
UNCOV
3461
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3462
    return;
×
3463
  }
3464
  
3465
  switch (pMsg->status) {
11,799,502✔
3466
    case STREAM_STATUS_INIT:      
11,792,224✔
3467
      if (STREAM_TRIGGER_TASK != pMsg->type) {
11,792,224✔
3468
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
11,395,955✔
3469
        return;
11,395,955✔
3470
      }
3471
      
3472
      if (INT64_MIN == pStatus->lastActionTs) {
396,269✔
UNCOV
3473
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
UNCOV
3474
        return;
×
3475
      }
3476
      
3477
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
396,269✔
3478
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
123,064✔
3479
        return;
123,064✔
3480
      }
3481

3482
      if (STREAM_IS_RUNNING(pStatus)) {
273,205✔
UNCOV
3483
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3484
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
273,205✔
3485
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
196,871✔
3486
      }
3487
      break;
273,205✔
3488
    case STREAM_STATUS_FAILED:
7,278✔
3489
      //STREAMTODO ADD ERRCODE HANDLE
3490
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
7,278✔
3491
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
6,882✔
3492
                 pMsg->taskIdx);
3493
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
6,882✔
3494
      } else {
3495
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
396✔
3496
                 pMsg->taskIdx);
3497
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
396✔
3498
      }
3499
      break;
7,278✔
UNCOV
3500
    default:
×
UNCOV
3501
      break;
×
3502
  }
3503

3504
_exit:
280,483✔
3505

3506
  if (code) {
280,483✔
UNCOV
3507
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
UNCOV
3508
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3509
  }
3510
}
3511

3512
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
251,927✔
3513
  int32_t code = TSDB_CODE_SUCCESS;
251,927✔
3514
  int32_t lino = 0;
251,927✔
3515
  SStreamTask* pTask = (SStreamTask*)pStatus;
251,927✔
3516
  int64_t streamId = pStatus->streamId;
251,927✔
3517

3518
  msttInfo("start to handle task error, type: %d", err);
251,927✔
3519

3520
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
251,927✔
3521

3522
_exit:
251,927✔
3523

3524
  if (code) {
251,927✔
3525
    // IGNORE STOP STREAM BY ERROR  
UNCOV
3526
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3527
  }
3528
}
251,927✔
3529

3530
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
4,283,417✔
3531
  int32_t code = TSDB_CODE_SUCCESS;
4,283,417✔
3532
  int32_t lino = 0;
4,283,417✔
3533
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
4,283,417✔
3534

3535
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
4,283,417✔
UNCOV
3536
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3537
  }
3538
  
3539
  SArray* userRecalcList = NULL;
4,283,417✔
3540
  if (atomic_load_ptr(&pStream->userRecalcList)) {
4,283,417✔
3541
    taosWLockLatch(&pStream->userRecalcLock);
3,392✔
3542
    if (pStream->userRecalcList) {
3,392✔
3543
      userRecalcList = pStream->userRecalcList;
3,392✔
3544
      pStream->userRecalcList = NULL;
3,392✔
3545
    }
3546
    taosWUnLockLatch(&pStream->userRecalcLock);
3,392✔
3547
    
3548
    if (userRecalcList) {
3,392✔
3549
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,392✔
3550
    }
3551
  }
3552

3553
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
4,283,417✔
3554
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,156,851✔
3555
    if (NULL == pStatus->detailStatus) {
2,156,851✔
3556
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
195,728✔
3557
      if (NULL == pStatus->detailStatus) {
195,728✔
UNCOV
3558
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
UNCOV
3559
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3560
      }
3561
    }
3562
    
3563
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,156,851✔
3564
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,156,851✔
3565
  }
3566

3567
_exit:
2,126,566✔
3568

3569
  if (code) {
4,283,417✔
3570
    // IGNORE STOP STREAM BY ERROR
UNCOV
3571
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3572
  }
3573
}
4,283,417✔
3574

3575
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,178,059✔
3576
  int32_t code = TSDB_CODE_SUCCESS;
1,178,059✔
3577
  int32_t lino = 0;
1,178,059✔
3578
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,178,059✔
3579

3580
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,178,059✔
3581

3582
  for (int32_t i = 0; i < num; ++i) {
36,292,138✔
3583
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
35,114,079✔
3584
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
35,114,079✔
3585
    
3586
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
35,114,079✔
3587
    if (NULL == ppStatus) {
35,114,079✔
3588
      msttWarn("task no longer exists in taskMap, streamId:0x%" PRIx64 " taskId:0x%" PRIx64 " type:%s status:%s taskIdx:%d",
1,626✔
3589
          (uint64_t)pTask->streamId, (uint64_t)pTask->taskId, gStreamTaskTypeStr[pTask->type], gStreamStatusStr[pTask->status], pTask->taskIdx);
3590
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
1,626✔
3591
      continue;
1,626✔
3592
    }
3593

3594
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
35,112,453✔
3595
    int8_t stopped = atomic_load_8(&pStream->stopped);
35,112,453✔
3596
    if (stopped) {
35,112,453✔
3597
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
250,301✔
3598
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
250,301✔
3599
      continue;
250,301✔
3600
    }
3601

3602
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
34,862,152✔
UNCOV
3603
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3604
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3605
          
3606
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
UNCOV
3607
      continue;
×
3608
    }
3609

3610
    if ((*ppStatus)->status != pTask->status) {
34,862,152✔
3611
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,596,418✔
3612
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,104,474✔
3613
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,491,944✔
UNCOV
3614
        if (pStream->triggerTask) {
×
UNCOV
3615
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3616
        }
3617
        
3618
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3619
      }
3620
    }
3621
    
3622
    (*ppStatus)->errCode = pTask->errorCode;
34,862,152✔
3623
    (*ppStatus)->status = pTask->status;
34,862,152✔
3624
    (*ppStatus)->lastUpTs = pCtx->currTs;
34,862,152✔
3625
    
3626
    if (STREAM_TRIGGER_TASK == pTask->type && STREAM_STATUS_RUNNING == pTask->status) {
34,862,152✔
3627
      mstInfo("streamId:0x%" PRIx64 " trigger task status updated to Running in streamMap", (uint64_t)pTask->streamId);
3,884,440✔
3628
    }
3629
    
3630
    if (STREAM_STATUS_RUNNING != pTask->status) {
34,862,152✔
3631
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
11,799,502✔
3632
    }
3633
    
3634
    if (STREAM_TRIGGER_TASK == pTask->type) {
34,862,152✔
3635
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
4,283,417✔
3636
    }
3637
  }
3638

3639
_exit:
1,178,059✔
3640

3641
  if (code) {
1,178,059✔
UNCOV
3642
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3643
  }
3644

3645
  return code;
1,178,059✔
3646
}
3647

UNCOV
3648
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3649
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
3650
  int32_t lino = 0;
×
UNCOV
3651
  int64_t streamId = pTask->streamId;
×
UNCOV
3652
  SStreamObj* pStream = NULL;
×
3653

UNCOV
3654
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3655
  if (NULL == pStatus) {
×
3656
    SStmStatus status = {0};
×
3657
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3658
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3659
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
UNCOV
3660
      mndReleaseStream(pCtx->pMnode, pStream);
×
3661
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3662
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3663
      return code;
×
3664
    }
3665

3666
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3667
    mndReleaseStream(pCtx->pMnode, pStream);
×
3668

UNCOV
3669
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3670
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
UNCOV
3671
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
UNCOV
3672
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3673
  }
3674

UNCOV
3675
  SStmTaskStatus* pNewTask = NULL;
×
3676
  switch (pTask->type) {
×
3677
    case STREAM_READER_TASK: {
×
3678
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3679
      if (NULL == pList) {
×
UNCOV
3680
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
UNCOV
3681
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3682
      }
3683
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3684
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3685
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3686
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3687
            (int32_t)taosArrayGetSize(pList), readerSize);
3688
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3689
      }
3690
      
3691
      SStmTaskStatus taskStatus = {0};
×
3692
      taskStatus.pStream = pStatus;
×
3693
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
UNCOV
3694
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3695
        pNewTask = taosArrayPush(pList, &taskStatus);
×
UNCOV
3696
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3697
      } else {
3698
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3699
        SListNode* pTailNode = tdListGetTail(pStatus->calcReaders);
×
3700
        QUERY_CHECK_NULL(pTailNode, code, lino, _exit, TSDB_CODE_INTERNAL_ERROR);
×
3701
        pNewTask = (SStmTaskStatus*)pTailNode->data;
×
3702
      }
3703
      
UNCOV
3704
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3705
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3706
      break;
×
3707
    }
3708
    case STREAM_TRIGGER_TASK: {
×
UNCOV
3709
      taosMemoryFreeClear(pStatus->triggerTask);
×
UNCOV
3710
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3711
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3712
      pStatus->triggerTask->pStream = pStatus;
×
3713
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
UNCOV
3714
      pNewTask = pStatus->triggerTask;
×
3715

3716
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3717
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3718
      break;
×
3719
    }
3720
    case STREAM_RUNNER_TASK:{
×
3721
      if (NULL == pStatus->runners[pTask->deployId]) {
×
UNCOV
3722
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3723
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3724
      }
3725
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
UNCOV
3726
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3727
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3728
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3729
      }    
3730
      
UNCOV
3731
      SStmTaskStatus taskStatus = {0};
×
3732
      taskStatus.pStream = pStatus;
×
3733
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
UNCOV
3734
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3735
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3736

UNCOV
3737
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3738
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3739
      break;
×
3740
    }
3741
    default: {
×
3742
      msttError("invalid task type:%d in task status", pTask->type);
×
UNCOV
3743
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3744
      break;
×
3745
    }
3746
  }
3747

3748
_exit:
×
3749

3750
  if (code) {
×
3751
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3752
  } else {
UNCOV
3753
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3754
  }
3755

UNCOV
3756
  return code;
×
3757
}
3758

UNCOV
3759
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3760
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
3761
  int32_t lino = 0;
×
UNCOV
3762
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3763

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

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

UNCOV
3770
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3771
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3772
    }
3773
    
3774
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3775
    if (NULL == ppStatus) {
×
UNCOV
3776
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3777
      
3778
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3779
      
UNCOV
3780
      continue;
×
3781
    }
3782
    
3783
    (*ppStatus)->status = pTask->status;
×
UNCOV
3784
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3785
  }
3786

3787
_exit:
×
3788

UNCOV
3789
  if (code) {
×
3790
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3791
  }
3792

UNCOV
3793
  return code;
×
3794
}
3795

3796
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
196,871✔
3797
  int32_t code = TSDB_CODE_SUCCESS;
196,871✔
3798
  int32_t lino = 0;
196,871✔
3799
  if (NULL == pCtx->pRsp->start.taskList) {
196,871✔
3800
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
99,569✔
3801
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
99,569✔
3802
  }
3803

3804
  SStmTaskId* pId = &pAction->start.triggerId;
196,871✔
3805
  SStreamTaskStart start = {0};
196,871✔
3806
  start.task.type = STREAM_TRIGGER_TASK;
196,871✔
3807
  start.task.streamId = streamId;
196,871✔
3808
  start.task.taskId = pId->taskId;
196,871✔
3809
  start.task.seriousId = pId->seriousId;
196,871✔
3810
  start.task.nodeId = pId->nodeId;
196,871✔
3811
  start.task.taskIdx = pId->taskIdx;
196,871✔
3812

3813
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
393,742✔
3814
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
196,871✔
3815

3816
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
196,871✔
3817

3818
  return;
196,871✔
3819

UNCOV
3820
_exit:
×
3821

UNCOV
3822
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3823
}
3824

3825

3826
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
51,187✔
3827
  int32_t code = TSDB_CODE_SUCCESS;
51,187✔
3828
  int32_t lino = 0;
51,187✔
3829
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
51,187✔
3830
  if (NULL == pCtx->pRsp->undeploy.taskList) {
51,187✔
3831
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
34,834✔
3832
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
34,834✔
3833
  }
3834

3835
  SStreamTaskUndeploy undeploy;
51,187✔
3836
  for (int32_t i = 0; i < dropNum; ++i) {
303,510✔
3837
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
252,323✔
3838
    undeploy.task = *pTask;
252,323✔
3839
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
252,323✔
3840
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
252,323✔
3841

3842
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
504,646✔
3843
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
252,323✔
3844

3845
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
252,323✔
3846
  }
3847

3848
  return;
51,187✔
3849

UNCOV
3850
_exit:
×
3851

UNCOV
3852
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3853
}
3854

UNCOV
3855
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
UNCOV
3856
  int32_t code = TSDB_CODE_SUCCESS;
×
3857
  int32_t lino = 0;
×
3858

3859
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
UNCOV
3860
  if (NULL == pStream) {
×
UNCOV
3861
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3862
    return;
×
3863
  }
3864

UNCOV
3865
  if (NULL == pStream->triggerTask) {
×
3866
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3867
    return;
×
3868
  }
3869

UNCOV
3870
  SStreamMgmtRsp rsp = {0};
×
UNCOV
3871
  rsp.reqId = INT64_MIN;
×
3872
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3873
  rsp.task.streamId = streamId;
×
3874
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3875

UNCOV
3876
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3877

3878
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3879
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3880
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3881
  }
3882

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

3885
_exit:
×
3886

3887
  if (code) {
×
UNCOV
3888
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3889
  } else {
3890
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3891
  }
3892
}
3893

3894
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,392✔
3895
  int32_t code = TSDB_CODE_SUCCESS;
3,392✔
3896
  int32_t lino = 0;
3,392✔
3897

3898
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,392✔
3899
  if (NULL == pStream) {
3,392✔
UNCOV
3900
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
3901
    return;
×
3902
  }
3903

3904
  if (NULL == pStream->triggerTask) {
3,392✔
UNCOV
3905
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
UNCOV
3906
    return;
×
3907
  }
3908

3909
  SStreamMgmtRsp rsp = {0};
3,392✔
3910
  rsp.reqId = INT64_MIN;
3,392✔
3911
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,392✔
3912
  rsp.task.streamId = streamId;
3,392✔
3913
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,392✔
3914
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,392✔
3915

3916
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,392✔
3917
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,194✔
3918
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,194✔
3919
  }
3920

3921
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,784✔
3922

3923
_exit:
3,392✔
3924

3925
  if (code) {
3,392✔
UNCOV
3926
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3927
  } else {
3928
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,392✔
3929
  }
3930
}
3931

3932

3933
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
137,399✔
3934
  int32_t code = TSDB_CODE_SUCCESS;
137,399✔
3935
  int32_t lino = 0;
137,399✔
3936
  void* pIter = NULL;
137,399✔
3937
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
137,399✔
3938

3939
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
137,399✔
3940

3941
  while (1) {
251,450✔
3942
    pIter = taosHashIterate(pCtx->actionStm, pIter);
388,849✔
3943
    if (pIter == NULL) {
388,849✔
3944
      break;
137,399✔
3945
    }
3946

3947
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
251,450✔
3948
    SStmAction *pAction = (SStmAction *)pIter;
251,450✔
3949
    
3950
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
251,450✔
3951
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
51,187✔
3952
      continue;
51,187✔
3953
    }
3954

3955
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
200,263✔
UNCOV
3956
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3957
    }
3958

3959
    if (STREAM_ACT_RECALC & pAction->actions) {
200,263✔
3960
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,392✔
3961
    }
3962

3963
    if (STREAM_ACT_START & pAction->actions) {
200,263✔
3964
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
196,871✔
3965
    }
3966
  }
3967
  
3968
_exit:
137,399✔
3969

3970
  if (pIter) {
137,399✔
UNCOV
3971
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3972
  }
3973

3974
  if (code) {
137,399✔
UNCOV
3975
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3976
  }
3977

3978
  return code;
137,399✔
3979
}
3980

3981
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
16,764,115✔
3982
  int32_t  code = TSDB_CODE_SUCCESS;
16,764,115✔
3983
  int32_t  lino = 0;
16,764,115✔
3984
  int64_t* lastTs = NULL;
16,764,115✔
3985
  bool     noExists = false;
16,764,115✔
3986

3987
  while (true) {
3988
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
17,281,046✔
3989
    if (NULL == lastTs) {
17,281,046✔
3990
      if (noExists) {
657,276✔
3991
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
140,345✔
3992
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
140,345✔
3993
      }
3994

3995
      noExists = true;
516,931✔
3996
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
516,931✔
3997
      
3998
      continue;
516,931✔
3999
    }
4000

UNCOV
4001
    while (true) {
×
4002
      int64_t lastTsValue = atomic_load_64(lastTs);
16,623,770✔
4003
      if (pCtx->currTs > lastTsValue) {
16,623,770✔
4004
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
16,620,069✔
4005
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
16,620,069✔
4006
          return code;
16,620,069✔
4007
        }
4008

UNCOV
4009
        continue;
×
4010
      }
4011

4012
      return code;
3,701✔
4013
    }
4014

4015
    break;
4016
  }
4017

4018
_exit:
140,345✔
4019

4020
  if (code) {
140,345✔
4021
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
140,345✔
4022
  }
4023

4024
  return code;  
140,345✔
4025
}
4026

UNCOV
4027
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
UNCOV
4028
  SStmStatus* pStatus = NULL;
×
UNCOV
4029
  int32_t trigReaderNum = 0;
×
UNCOV
4030
  int32_t calcReaderNum = 0;
×
UNCOV
4031
  int32_t runnerNum = 0;
×
UNCOV
4032
  int64_t streamId = 0;
×
UNCOV
4033
  void* pIter = NULL;
×
4034
  while (true) {
4035
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
4036
    if (NULL == pIter) {
×
4037
      return;
×
4038
    }
4039

4040
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
UNCOV
4041
    pStatus = (SStmStatus*)pIter;
×
4042

4043
    if (NULL == pStatus->triggerTask) {
×
4044
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4045
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
UNCOV
4046
      continue;
×
4047
    }
4048
    
UNCOV
4049
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
4050
    if (pStatus->trigReaderNum != trigReaderNum) {
×
4051
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
4052
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4053
      continue;
×
4054
    }
4055

4056
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
4057
    if (pStatus->calcReaderNum != calcReaderNum) {
×
4058
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
4059
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4060
      continue;
×
4061
    }
4062

4063
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
4064
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
4065
      if (runnerNum != pStatus->runnerNum) {
×
4066
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
4067
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
UNCOV
4068
        continue;
×
4069
      }
4070
    }
4071
  }
4072
}
4073

4074
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
345✔
4075
  int32_t code = TSDB_CODE_SUCCESS;
345✔
4076
  int32_t lino = 0;
345✔
4077
  int32_t minVal = watchError ? 0 : 1;
345✔
4078

4079
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
345✔
UNCOV
4080
    return code;
×
4081
  }
4082

4083
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
345✔
UNCOV
4084
    (void)sched_yield();
×
4085
  }
4086

4087
  if (watchError) {
345✔
UNCOV
4088
    taosHashClear(mStreamMgmt.vgroupMap);
×
UNCOV
4089
    taosHashClear(mStreamMgmt.snodeMap);
×
UNCOV
4090
    taosHashClear(mStreamMgmt.taskMap);
×
4091
    taosHashClear(mStreamMgmt.streamMap);
×
UNCOV
4092
    mstInfo("watch error happends, clear all maps");
×
UNCOV
4093
    goto _exit;
×
4094
  }
4095

4096
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
345✔
4097
    mstInfo("no stream tasks remain during watch state");
345✔
4098
    goto _exit;
345✔
4099
  }
4100

UNCOV
4101
  msmWatchCheckStreamMap(pCtx);
×
4102

4103
_exit:
345✔
4104

4105
  mStreamMgmt.lastTaskId += 100000;
345✔
4106

4107
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
345✔
4108

4109
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
345✔
4110

4111
  if (code) {
345✔
UNCOV
4112
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4113
  }
4114

4115
  return code;
345✔
4116
}
4117

4118

4119
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
12,630✔
4120
  int32_t code = TSDB_CODE_SUCCESS;
12,630✔
4121
  int32_t lino = 0;
12,630✔
4122
  SStreamHbMsg* pReq = pCtx->pReq;
12,630✔
4123

4124
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
12,630✔
4125
  
4126
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
12,630✔
UNCOV
4127
    goto _exit;
×
4128
  }
4129

4130
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
12,630✔
4131
  if (GOT_SNODE(pReq->snodeId)) {
12,630✔
4132
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,070✔
4133
  }
4134

4135
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
12,630✔
UNCOV
4136
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
UNCOV
4137
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4138
  }
4139

4140
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
12,630✔
4141
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
345✔
4142
  }
4143

4144
_exit:
12,630✔
4145

4146
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
12,630✔
4147
  
4148
  if (code) {
12,630✔
UNCOV
4149
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4150

UNCOV
4151
    (void)msmWatchHandleEnding(pCtx, true);
×
4152
  }
4153

4154
  return code;
12,630✔
4155
}
4156

4157
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,427,092✔
4158
  int32_t listSize = taosArrayGetSize(pOReaders);
5,427,092✔
4159
  int32_t totalSize = 0;
5,427,092✔
4160
  
4161
  for (int32_t i = 0; i < listSize; ++i) {
6,100,835✔
4162
    SArray* pList = taosArrayGetP(pOReaders, i);
673,743✔
4163
    totalSize += taosArrayGetSize(pList);
673,743✔
4164
  }
4165

4166
  return totalSize;
5,427,092✔
4167
}
4168

4169
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
405,390✔
4170
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
405,390✔
4171
  if (NULL == pList) {
405,390✔
UNCOV
4172
    return NULL;
×
4173
  }
4174

4175
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
405,390✔
4176
}
4177

4178

4179
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
61,725✔
4180
  int32_t code = TSDB_CODE_SUCCESS;
61,725✔
4181
  int32_t lino = 0;
61,725✔
4182

4183
  if (NULL == pStatus->trigOReaders) {
61,725✔
4184
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
53,571✔
4185
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
53,571✔
4186
  }
4187

4188
  while (true) {
53,571✔
4189
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
115,296✔
4190

4191
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
115,296✔
4192
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
53,571✔
4193
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
53,571✔
4194

4195
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
107,142✔
4196
      continue;
53,571✔
4197
    }
4198

4199
    *ppRes = *ppOReaderList;
61,725✔
4200
    break;
61,725✔
4201
  }
4202

4203
_exit:
61,725✔
4204

4205
  if (code) {
61,725✔
UNCOV
4206
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4207
  }
4208

4209
  return code;
61,725✔
4210
}
4211

4212
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
72,356✔
4213
  int32_t code = TSDB_CODE_SUCCESS;
72,356✔
4214
  int32_t lino = 0;
72,356✔
4215
  bool    readerExists = false;
72,356✔
4216
  int64_t streamId = pTask->streamId;
72,356✔
4217

4218
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
72,356✔
4219
  for (int32_t i = 0; i < readerNum; ++i) {
150,521✔
4220
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
88,796✔
4221
    if (pReader->id.nodeId == vgId) {
88,796✔
4222
      readerExists = true;
10,631✔
4223
      break;
10,631✔
4224
    }
4225
  }
4226

4227
  if (!readerExists) {
72,356✔
4228
    SArray* pReaderList = NULL;
61,725✔
4229
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
61,725✔
4230
    
4231
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
61,725✔
4232
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
61,725✔
4233
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
61,725✔
4234
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
61,725✔
4235
  }
4236

4237
_exit:
10,631✔
4238

4239
  if (code) {
72,356✔
UNCOV
4240
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4241
  }
4242

4243
  return code;
72,356✔
4244
}
4245

4246
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
64,379✔
4247
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
64,379✔
4248
  int32_t lino = 0;
64,379✔
4249
  int32_t vgId = 0;
64,379✔
4250
  int64_t streamId = pTask->streamId;
64,379✔
4251
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
64,379✔
4252
  int32_t tbNum = taosArrayGetSize(pTbs);
64,379✔
4253
  SStreamDbTableName* pName = NULL;
64,379✔
4254
  SSHashObj* pDbVgroups = NULL;
64,379✔
4255
  SStreamMgmtRsp rsp = {0};
64,379✔
4256
  rsp.reqId = pTask->pMgmtReq->reqId;
64,379✔
4257
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
64,379✔
4258
  int32_t iter = 0;
64,379✔
4259
  void* p = NULL;
64,379✔
4260
  SSHashObj* pVgs = NULL;
64,379✔
4261
  SStreamMgmtReq* pMgmtReq = NULL;
64,379✔
4262
  int8_t stopped = 0;
64,379✔
4263

4264
  if (NULL == pCtx->pRsp->rsps.rspList) {
64,379✔
UNCOV
4265
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
UNCOV
4266
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4267
  }
4268
  
4269
  TSWAP(pTask->pMgmtReq, pMgmtReq);
64,379✔
4270
  rsp.task = *(SStreamTask*)pTask;
64,379✔
4271

4272
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
64,379✔
4273
  if (NULL == pStatus) {
64,379✔
UNCOV
4274
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
4275
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4276
  }
4277

4278
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
64,379✔
4279
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
771✔
4280
    goto _exit;
771✔
4281
  }
4282

4283
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
63,608✔
4284

4285
  stopped = atomic_load_8(&pStatus->stopped);
63,608✔
4286
  if (stopped) {
63,608✔
4287
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
UNCOV
4288
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4289
  }
4290

4291
  if (tbNum <= 0) {
63,608✔
UNCOV
4292
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
UNCOV
4293
    goto _exit;
×
4294
  }
4295

4296
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
63,608✔
4297
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
63,608✔
4298
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
63,608✔
4299

4300
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
63,608✔
4301
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
63,608✔
4302
  
4303
  for (int32_t i = 0; i < tbNum; ++i) {
250,641✔
4304
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
187,033✔
4305
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
187,033✔
4306
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
374,066✔
4307
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
187,033✔
4308
  }
4309

4310
  int32_t vgNum = tSimpleHashGetSize(pVgs);
63,608✔
4311
  while (true) {
4312
    p = tSimpleHashIterate(pVgs, p, &iter);
135,964✔
4313
    if (NULL == p) {
135,964✔
4314
      break;
63,608✔
4315
    }
4316
    
4317
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
72,356✔
4318
  }
4319
  
4320
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
63,608✔
4321
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
63,608✔
4322
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
63,608✔
4323

4324
  SStreamTaskAddr addr;
63,608✔
4325
  for (int32_t i = 0; i < vgNum; ++i) {
125,333✔
4326
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
61,725✔
4327
    addr.taskId = pOTask->id.taskId;
61,725✔
4328
    addr.nodeId = pOTask->id.nodeId;
61,725✔
4329
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
61,725✔
4330
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
123,450✔
4331
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
61,725✔
4332
  }
4333

4334
_exit:
64,379✔
4335

4336
  tFreeSStreamMgmtReq(pMgmtReq);
64,379✔
4337
  taosMemoryFree(pMgmtReq);
64,379✔
4338

4339
  tSimpleHashCleanup(pVgs);
64,379✔
4340
  mstDestroyDbVgroupsHash(pDbVgroups);
64,379✔
4341

4342
  if (code) {
64,379✔
UNCOV
4343
    rsp.code = code;
×
4344
    
UNCOV
4345
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4346

UNCOV
4347
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4348
  } else {
4349
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
128,758✔
4350
  }
4351

4352
_final:
64,379✔
4353

4354
  if (finalCode) {
64,379✔
UNCOV
4355
    tFreeSStreamMgmtRsp(&rsp);
×
UNCOV
4356
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
UNCOV
4357
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4358
  }
4359

4360
  return finalCode;
64,379✔
4361
}
4362

4363
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,497✔
4364
  int32_t num = taosArrayGetSize(pList);
2,497✔
4365
  SStreamCalcScan* pScan = NULL;
2,497✔
4366
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,497✔
4367
  int64_t planUid = 0;
2,497✔
4368
  for (int32_t i = 0; i < num; ++i) {
2,497✔
4369
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,497✔
4370
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,497✔
4371
    if (0 != planUid && planUid == uid) {
2,497✔
4372
      *ppRes = pScan;
2,497✔
4373
      break;
2,497✔
4374
    }
4375
  }
4376

4377
_exit:
2,497✔
4378

4379
  if (code) {
2,497✔
UNCOV
4380
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4381
  }
4382

4383
  return code;
2,497✔
4384
}
4385

4386
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,497✔
4387
  int32_t code = TSDB_CODE_SUCCESS;
2,497✔
4388
  int32_t lino = 0;
2,497✔
4389
  bool    readerExists = false;
2,497✔
4390
  int64_t streamId = pTask->streamId;
2,497✔
4391
  SListNode* pNode = listHead(pStatus->calcReaders);
2,497✔
4392
  SStmTaskStatus* pReader = NULL;
2,497✔
4393
  int32_t taskIdx = 0;
2,497✔
4394

4395
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,497✔
4396
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
23,154✔
4397
    pReader = (SStmTaskStatus*)pNode->data;
20,657✔
4398
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
20,657✔
UNCOV
4399
      readerExists = true;
×
UNCOV
4400
      pAddr->taskId = pReader->id.taskId;
×
UNCOV
4401
      break;
×
4402
    }
4403
  }
4404

4405
  if (!readerExists) {
2,497✔
4406
    if (NULL == pStatus->calcReaders) {
2,497✔
4407
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4408
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
UNCOV
4409
      taskIdx = 0;
×
4410
    } else {
4411
      pNode = listTail(pStatus->calcReaders);
2,497✔
4412
      pReader = (SStmTaskStatus*)pNode->data;
2,497✔
4413
      taskIdx = pReader->id.taskIdx + 1;
2,497✔
4414
    }
4415

4416
    SStreamCalcScan* pScan = NULL;
2,497✔
4417
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,497✔
4418
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,497✔
4419
    pReader = tdListReserve(pStatus->calcReaders);
2,497✔
4420
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,497✔
4421
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,497✔
4422
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,497✔
4423
    pAddr->taskId = pReader->id.taskId;
2,497✔
4424
  }
4425

4426
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,497✔
4427
  pAddr->nodeId = vgId;
2,497✔
4428

4429
_exit:
2,497✔
4430

4431
  if (code) {
2,497✔
UNCOV
4432
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4433
  }
4434

4435
  return code;
2,497✔
4436
}
4437

4438

4439
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,497✔
4440
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,497✔
4441
  int32_t lino = 0;
2,497✔
4442
  int32_t vgId = 0;
2,497✔
4443
  int64_t streamId = pTask->streamId;
2,497✔
4444
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,497✔
4445
  int32_t reqNum = taosArrayGetSize(pReqs);
2,497✔
4446
  SStreamOReaderDeployReq* pReq = NULL;
2,497✔
4447
  SStreamOReaderDeployRsp* pRsp = NULL;
2,497✔
4448
  SStreamMgmtRsp rsp = {0};
2,497✔
4449
  rsp.reqId = pTask->pMgmtReq->reqId;
2,497✔
4450
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,497✔
4451
  SStreamMgmtReq* pMgmtReq = NULL;
2,497✔
4452
  int8_t stopped = 0;
2,497✔
4453
  int32_t vgNum = 0;
2,497✔
4454
  SStreamTaskAddr* pAddr = NULL;
2,497✔
4455

4456
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,497✔
UNCOV
4457
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
UNCOV
4458
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4459
  }
4460
  
4461
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,497✔
4462
  rsp.task = *(SStreamTask*)pTask;
2,497✔
4463

4464
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,497✔
4465
  if (NULL == pStatus) {
2,497✔
UNCOV
4466
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
4467
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4468
  }
4469

4470
  stopped = atomic_load_8(&pStatus->stopped);
2,497✔
4471
  if (stopped) {
2,497✔
UNCOV
4472
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4473
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4474
  }
4475

4476
  if (reqNum <= 0) {
2,497✔
UNCOV
4477
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
UNCOV
4478
    goto _exit;
×
4479
  }
4480

4481
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,497✔
4482
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,497✔
4483

4484
  for (int32_t i = 0; i < reqNum; ++i) {
4,994✔
4485
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,497✔
4486
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,497✔
4487
    pRsp->execId = pReq->execId;
2,497✔
4488
    vgNum = taosArrayGetSize(pReq->vgIds);
2,497✔
4489
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,497✔
4490
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,497✔
4491
    
4492
    for (int32_t n = 0; n < vgNum; ++n) {
4,994✔
4493
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,497✔
4494
      pAddr = taosArrayGet(pRsp->vgList, n);
2,497✔
4495
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,497✔
4496
    }
4497
  }
4498

4499
_exit:
2,497✔
4500

4501
  tFreeSStreamMgmtReq(pMgmtReq);
2,497✔
4502
  taosMemoryFree(pMgmtReq);
2,497✔
4503

4504
  if (code) {
2,497✔
UNCOV
4505
    rsp.code = code;
×
4506
    
UNCOV
4507
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
UNCOV
4508
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4509
  } else {
4510
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,994✔
4511
  }
4512

4513

4514
_final:
2,497✔
4515

4516
  if (finalCode) {
2,497✔
UNCOV
4517
    tFreeSStreamMgmtRsp(&rsp);
×
UNCOV
4518
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
UNCOV
4519
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4520
  }
4521

4522
  return finalCode;
2,497✔
4523
}
4524

4525

4526
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
66,876✔
4527
  int32_t code = TSDB_CODE_SUCCESS;
66,876✔
4528
  int32_t lino = 0;
66,876✔
4529

4530
  switch (pTask->pMgmtReq->type) {
66,876✔
4531
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
64,379✔
4532
      msmDeployTriggerOrigReader(pCtx, pTask);
64,379✔
4533
      break;
64,379✔
4534
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,497✔
4535
      msmDeployRunnerOrigReader(pCtx, pTask);
2,497✔
4536
      break;
2,497✔
UNCOV
4537
    default:
×
UNCOV
4538
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
UNCOV
4539
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
UNCOV
4540
      break;
×
4541
  }
4542

4543
_exit:
66,876✔
4544

4545
  if (code) {
66,876✔
4546
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4547
  }
4548

4549
  return code;
66,876✔
4550
}
4551

4552
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
26,151✔
4553
  int32_t code = TSDB_CODE_SUCCESS;
26,151✔
4554
  int32_t lino = 0;
26,151✔
4555
  SStreamHbMsg* pReq = pCtx->pReq;
26,151✔
4556
  SStmTaskStatusMsg* pTask = NULL;
26,151✔
4557
  
4558
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
26,151✔
4559
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
26,151✔
4560
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
26,151✔
4561
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
26,151✔
4562
  }
4563
  
4564
  for (int32_t i = 0; i < reqNum; ++i) {
93,027✔
4565
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
66,876✔
4566
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
66,876✔
4567
    if (NULL == pTask) {
66,876✔
UNCOV
4568
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
UNCOV
4569
      continue;
×
4570
    }
4571

4572
    if (NULL == pTask->pMgmtReq) {
66,876✔
UNCOV
4573
      msttError("idx %d without mgmtReq", idx);
×
UNCOV
4574
      continue;
×
4575
    }
4576

4577
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
66,876✔
4578
  }
4579

4580
_exit:
26,151✔
4581

4582
  if (code) {
26,151✔
UNCOV
4583
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4584
  }
4585

4586
  return code;
26,151✔
4587
}
4588

4589
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
16,751,485✔
4590
  int32_t code = TSDB_CODE_SUCCESS;
16,751,485✔
4591
  int32_t lino = 0;
16,751,485✔
4592
  SStreamHbMsg* pReq = pCtx->pReq;
16,751,485✔
4593

4594
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,751,485✔
4595
  if (GOT_SNODE(pReq->snodeId)) {
16,611,140✔
4596
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,347,371✔
4597
  }
4598
  
4599
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,610,422✔
4600
    msmHandleStreamActions(pCtx);
52,913✔
4601
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
52,913✔
4602
  }
4603

4604
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
16,610,422✔
4605
    code = msmHandleStreamRequests(pCtx);
26,151✔
4606
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
26,151✔
4607
    TAOS_CHECK_EXIT(code);
26,151✔
4608
  }
4609

4610
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,610,422✔
4611
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
79,907✔
4612
  } else {
4613
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,530,515✔
4614
  }
4615

4616
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,610,422✔
4617
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
79,786✔
4618
  }
4619

4620
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,610,422✔
4621
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
81,568✔
4622
  }
4623

4624
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,610,422✔
4625
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,178,059✔
4626
  }
4627

4628
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,610,422✔
4629
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
137,399✔
4630
  }
4631

4632
_exit:
16,610,422✔
4633

4634
  if (code) {
16,751,485✔
4635
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
141,063✔
4636
  }
4637

4638
  return code;
16,751,485✔
4639
}
4640

4641
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
16,846,615✔
4642
  int32_t lino = 0;
16,846,615✔
4643
  int32_t tlen = 0;
16,846,615✔
4644
  void   *buf = NULL;
16,846,615✔
4645

4646
  if (TSDB_CODE_SUCCESS != code) {
16,846,615✔
4647
    goto _exit;
141,063✔
4648
  }
4649

4650
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
16,705,552✔
4651
  if (code < 0) {
16,705,552✔
UNCOV
4652
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
UNCOV
4653
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4654
  }
4655

4656
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
16,705,552✔
4657
  if (buf == NULL) {
16,705,552✔
UNCOV
4658
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4659
    TAOS_CHECK_EXIT(terrno);    
×
4660
  }
4661

4662
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
16,705,552✔
4663
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
16,705,552✔
4664

4665
  SEncoder encoder;
16,702,715✔
4666
  tEncoderInit(&encoder, abuf, tlen);
16,705,552✔
4667
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
16,705,552✔
UNCOV
4668
    rpcFreeCont(buf);
×
UNCOV
4669
    buf = NULL;
×
UNCOV
4670
    tEncoderClear(&encoder);
×
UNCOV
4671
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
UNCOV
4672
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4673
  }
4674
  tEncoderClear(&encoder);
16,705,552✔
4675

4676
_exit:
16,846,615✔
4677

4678
  pMsg->code = code;
16,846,615✔
4679
  pMsg->info = *pRpcInfo;
16,846,615✔
4680
  if (TSDB_CODE_SUCCESS == code) {
16,846,615✔
4681
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
16,705,552✔
4682
    pMsg->pCont = buf;
16,705,552✔
4683
  }
4684
}
16,846,615✔
4685

4686

4687
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
16,846,615✔
4688
  int32_t code = TSDB_CODE_SUCCESS;
16,846,615✔
4689
  SMStreamHbRspMsg rsp = {0};
16,846,615✔
4690
  rsp.streamGId = pHb->streamGId;
16,846,615✔
4691

4692
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,846,615✔
4693

4694
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
16,846,615✔
4695
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
82,500✔
4696
    goto _exit;
82,500✔
4697
  }
4698

4699
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,764,115✔
4700
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
16,764,115✔
4701

4702
  pCtx->tidx = tidx;
16,764,115✔
4703
  pCtx->pMnode = pMnode;
16,764,115✔
4704
  pCtx->currTs = currTs;
16,764,115✔
4705
  pCtx->pReq = pHb;
16,764,115✔
4706
  pCtx->pRsp = &rsp;
16,764,115✔
4707
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
16,764,115✔
4708
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
16,764,115✔
4709
  
4710
  switch (atomic_load_8(&mStreamMgmt.state)) {
16,764,115✔
4711
    case MND_STM_STATE_WATCH:
12,630✔
4712
      code = msmWatchHandleHbMsg(pCtx);
12,630✔
4713
      break;
12,630✔
4714
    case MND_STM_STATE_NORMAL:
16,751,485✔
4715
      code = msmNormalHandleHbMsg(pCtx);
16,751,485✔
4716
      break;
16,751,485✔
UNCOV
4717
    default:
×
UNCOV
4718
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
UNCOV
4719
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
UNCOV
4720
      break;
×
4721
  }
4722

4723
_exit:
16,846,615✔
4724

4725
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
16,846,615✔
4726

4727
  msmCleanStreamGrpCtx(pHb);
16,846,615✔
4728
  msmClearStreamToDeployMaps(pHb);
16,846,615✔
4729

4730
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
16,846,615✔
4731
  
4732
  tFreeSMStreamHbRspMsg(&rsp);
16,846,615✔
4733

4734
  return code;
16,846,615✔
4735
}
4736

4737
void msmHandleBecomeLeader(SMnode *pMnode) {
424,412✔
4738
  if (tsDisableStream) {
424,412✔
UNCOV
4739
    return;
×
4740
  }
4741

4742
  mstInfo("start to process mnode become leader");
424,412✔
4743

4744
  int32_t code = 0;
424,412✔
4745
  streamAddVnodeLeader(MNODE_HANDLE);
424,412✔
4746
  
4747
  taosWLockLatch(&mStreamMgmt.runtimeLock);
424,412✔
4748
  msmDestroyRuntimeInfo(pMnode);
424,412✔
4749
  code = msmInitRuntimeInfo(pMnode);
424,412✔
4750
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
424,412✔
4751

4752
  if (TSDB_CODE_SUCCESS == code) {
424,412✔
4753
    atomic_store_8(&mStreamMgmt.active, 1);
424,412✔
4754
  }
4755

4756
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
424,412✔
4757
}
4758

4759
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
517,844✔
4760
  if (tsDisableStream) {
517,844✔
UNCOV
4761
    return;
×
4762
  }
4763

4764
  mstInfo("start to process mnode become not leader");
517,844✔
4765

4766
  streamRemoveVnodeLeader(MNODE_HANDLE);
517,844✔
4767

4768
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
517,844✔
4769
    taosWLockLatch(&mStreamMgmt.runtimeLock);
424,412✔
4770
    msmDestroyRuntimeInfo(pMnode);
424,412✔
4771
    mStreamMgmt.stat.inactiveTimes++;
424,412✔
4772
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
424,412✔
4773
  }
4774
}
4775

4776

UNCOV
4777
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
UNCOV
4778
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
UNCOV
4779
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4780
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4781
  } else {
UNCOV
4782
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4783
  }
4784
}
×
4785

4786
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
7,690✔
4787
  int32_t code = TSDB_CODE_SUCCESS;
7,690✔
4788
  int32_t lino = 0;
7,690✔
4789
  SStreamObj* pStream = pObj;
7,690✔
4790
  SSnodeObj* pSnode = p1;
7,690✔
4791
  SArray** ppRes = p2;
7,690✔
4792

4793
  if (pStream->mainSnodeId == pSnode->id) {
7,690✔
4794
    if (NULL == *ppRes) {
4,677✔
4795
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
635✔
4796
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
635✔
4797
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
635✔
4798
    }
4799

4800
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
9,354✔
4801
  }
4802

4803
  return true;
7,690✔
4804

UNCOV
4805
_exit:
×
4806

UNCOV
4807
  if (code) {
×
UNCOV
4808
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4809
  }  
4810

UNCOV
4811
  *(int32_t*)p3 = code;
×
4812

UNCOV
4813
  return false;
×
4814
}
4815

4816

4817
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
40,534✔
4818
  int32_t code = TSDB_CODE_SUCCESS;
40,534✔
4819
  int32_t lino = 0;
40,534✔
4820
  
4821
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
40,534✔
4822
  TAOS_CHECK_EXIT(code);
40,534✔
4823

4824
  int32_t streamNum = taosArrayGetSize(*ppRes);
40,534✔
4825
  if (streamNum > 0 && 0 == pSnode->replicaId) {
40,534✔
4826
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
198✔
4827
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
198✔
4828
  }
4829

4830
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4831

4832
_exit:
40,534✔
4833

4834
  if (code) {
40,534✔
4835
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
198✔
4836
  }  
4837

4838
  return code;
40,534✔
4839
}
4840

4841
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
581,525✔
4842
  SStreamObj* pStream = pObj;
581,525✔
4843
  int64_t streamId = pStream->pCreate->streamId;
581,525✔
4844
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
581,525✔
4845
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
581,525✔
4846
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
581,525✔
4847
  
4848
  if ((userDropped || userStopped) && (NULL == pStatus)) {
581,525✔
4849
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
198✔
4850
    return true;
198✔
4851
  }
4852
  
4853
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
581,327✔
4854
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
494,044✔
4855
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4856
    return true;
494,044✔
4857
  }
4858

4859
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
87,283✔
4860
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
59,268✔
4861
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4862
    return true;
59,268✔
4863
  }  
4864

4865
  if (pStatus) {
28,015✔
4866
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
27,847✔
UNCOV
4867
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4868
    }
4869

4870
    return true;
27,847✔
4871
  }
4872

4873
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
168✔
4874

4875
  return true;
168✔
4876
}
4877

4878
void msmCheckLoopStreamMap(SMnode *pMnode) {
50,757✔
4879
  SStmStatus* pStatus = NULL;
50,757✔
4880
  void* pIter = NULL;
50,757✔
4881
  int8_t stopped = 0;
50,757✔
4882
  int64_t streamId = 0;
50,757✔
4883
  
4884
  while (true) {
4885
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
282,334✔
4886
    if (NULL == pIter) {
282,334✔
4887
      break;
50,757✔
4888
    }
4889

4890
    pStatus = (SStmStatus*)pIter;
231,577✔
4891

4892
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
231,577✔
4893
    stopped = atomic_load_8(&pStatus->stopped);
231,577✔
4894
    if (MST_IS_USER_STOPPED(stopped)) {
231,577✔
4895
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
676✔
4896
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
676✔
4897
      continue;
676✔
4898
    }
4899

4900
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
230,901✔
UNCOV
4901
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4902
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
UNCOV
4903
      continue;
×
4904
    }
4905

4906
    if (MST_IS_ERROR_STOPPED(stopped)) {
230,901✔
4907
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
19,562✔
4908
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
13,188✔
4909
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4910
            
4911
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
13,188✔
4912
        continue;
13,188✔
4913
      }
4914

4915
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
6,374✔
4916
      continue;
6,374✔
4917
    }
4918

4919
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
211,339✔
UNCOV
4920
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
UNCOV
4921
      continue;
×
4922
    }
4923
  }
4924
}
50,757✔
4925

4926
void msmCheckStreamsStatus(SMnode *pMnode) {
403,603✔
4927
  SStmCheckStatusCtx ctx = {0};
403,603✔
4928

4929
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
403,603✔
4930
  
4931
  if (MST_READY_FOR_SDB_LOOP()) {
403,603✔
4932
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
102,120✔
4933
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
102,120✔
4934
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
102,120✔
4935
  }
4936

4937
  if (MST_READY_FOR_MAP_LOOP()) {
403,603✔
4938
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
50,757✔
4939
    msmCheckLoopStreamMap(pMnode);
50,757✔
4940
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
50,757✔
4941
  }
4942
}
403,603✔
4943

4944
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,757,029✔
4945
  for (int32_t i = 0; i < taskNum; ++i) {
8,041,612✔
4946
    SStmTaskStatus* pTask = *(pList + i);
4,284,583✔
4947

4948
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
4,284,583✔
4949
      continue;
605,211✔
4950
    }
4951
    
4952
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,681,424✔
4953
      continue;
3,680,122✔
4954
    }
4955

4956
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,302✔
4957
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,302✔
UNCOV
4958
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4959
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4960
          
UNCOV
4961
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
UNCOV
4962
      break;
×
4963
    }
4964

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

4968
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,302✔
4969
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,302✔
4970

4971
    SStmTaskAction task = {0};
1,302✔
4972
    task.streamId = streamId;
1,302✔
4973
    task.id = pTask->id;
1,302✔
4974
    task.flag = pTask->flags;
1,302✔
4975
    task.type = pTask->type;
1,302✔
4976
    
4977
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,302✔
4978
  }
4979
}
3,757,029✔
4980

4981
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
160,079✔
4982
  void* pIter = NULL;
160,079✔
4983
  SStmVgStreamStatus* pVg = NULL;
160,079✔
4984
  int64_t streamId = 0;
160,079✔
4985
  
4986
  while (true) {
1,492,328✔
4987
    pIter = taosHashIterate(pStreams, pIter);
1,652,407✔
4988
    if (NULL == pIter) {
1,652,407✔
4989
      break;
160,079✔
4990
    }
4991

4992
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,492,328✔
4993
    pVg = (SStmVgStreamStatus*)pIter;
1,492,328✔
4994

4995
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,492,328✔
4996
    if (taskNum > 0) {
1,492,328✔
4997
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
881,638✔
4998
    }
4999

5000
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,492,328✔
5001
    if (taskNum > 0) {
1,492,328✔
5002
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,020,613✔
5003
    }
5004
  }
5005
}
160,079✔
5006

UNCOV
5007
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
UNCOV
5008
  int64_t streamId = 0;
×
UNCOV
5009
  void* pIter = NULL;
×
UNCOV
5010
  SStmVgStreamStatus* pStream = NULL;
×
5011

UNCOV
5012
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
UNCOV
5013
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5014
    return;
×
5015
  }
5016

5017
  
5018
  while (true) {
5019
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
5020
    if (NULL == pIter) {
×
5021
      break;
×
5022
    }
5023

UNCOV
5024
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
5025
    
5026
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
5027
  }
5028

UNCOV
5029
  taosHashClear(pVg->streamTasks);
×
5030
}
5031

5032

5033
void msmCheckVgroupStatus(SMnode *pMnode) {
403,603✔
5034
  void* pIter = NULL;
403,603✔
5035
  int32_t code = 0;
403,603✔
5036
  
5037
  while (true) {
1,503,077✔
5038
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,906,680✔
5039
    if (NULL == pIter) {
1,906,680✔
5040
      break;
403,603✔
5041
    }
5042

5043
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,503,077✔
5044
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,503,077✔
5045
      continue;
1,337,620✔
5046
    }
5047
    
5048
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
165,457✔
5049

5050
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
165,457✔
5051
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
5,378✔
5052
      if (NULL == pVgroup) {
5,378✔
5053
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
5,378✔
5054
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
5,378✔
5055
        if (code) {
5,378✔
UNCOV
5056
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
5057
        }
5058
        continue;
5,378✔
5059
      }
UNCOV
5060
      mndReleaseVgroup(pMnode, pVgroup);
×
5061
      
UNCOV
5062
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5063
      
UNCOV
5064
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
UNCOV
5065
      continue;
×
5066
    }
5067

5068
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
160,079✔
5069

5070
    msmCheckVgroupStreamStatus(pVg->streamTasks);
160,079✔
5071
  }
5072
}
403,603✔
5073

5074
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
1,265✔
5075
  *deployNum = 0;
1,265✔
5076
  
5077
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
3,289✔
5078
    if (pStream->runners[i]) {
3,036✔
5079
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
1,265✔
5080
      for (int32_t t = 0; t < taskNum; ++t) {
1,518✔
5081
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
1,265✔
5082
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
1,265✔
5083
        if (stopped) {
1,265✔
5084
          mstsDebug("stream already stopped %d, ignore it", stopped);
1,012✔
5085
          *deployNum = 0;
1,012✔
5086
          return;
1,012✔
5087
        }
5088

5089
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
253✔
5090
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
253✔
5091
      }
5092
      
5093
      deployId[*deployNum] = i;
253✔
5094
      (*deployNum)++;
253✔
5095
    }
5096
  }
5097
}
5098

5099
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
3,059✔
5100
  pSnode->runnerThreadNum = -1;
3,059✔
5101

5102
  (void)msmSTAddSnodesToMap(pMnode);
3,059✔
5103

5104
  int64_t streamId = 0;
3,059✔
5105
  void* pIter = NULL;
3,059✔
5106
  SStmSnodeStreamStatus* pStream = NULL;
3,059✔
5107
  int32_t deployNum = 0;
3,059✔
5108
  SStmTaskAction task = {0};
3,059✔
5109
  
5110
  while (true) {
5111
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
5,635✔
5112
    if (NULL == pIter) {
5,635✔
5113
      break;
3,059✔
5114
    }
5115

5116
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,576✔
5117
    
5118
    task.streamId = streamId;
2,576✔
5119
    
5120
    pStream = (SStmSnodeStreamStatus*)pIter;
2,576✔
5121
    if (pStream->trigger) {
2,576✔
5122
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
1,311✔
5123
      if (stopped) {
1,311✔
5124
        mstsDebug("stream already stopped %d, ignore it", stopped);
759✔
5125
        continue;
759✔
5126
      }
5127

5128
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
552✔
5129
      
5130
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
552✔
5131
    } else {
5132
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
1,265✔
5133
      
5134
      if (task.deployNum > 0) {
1,265✔
5135
        //task.triggerStatus = pStream->trigger;
5136
        task.multiRunner = true;
253✔
5137
        task.type = STREAM_RUNNER_TASK;
253✔
5138
        
5139
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
253✔
5140
        
5141
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
253✔
5142
      }
5143
    }
5144
  }
5145

5146
  taosHashClear(pSnode->streamTasks);
3,059✔
5147
}
3,059✔
5148

5149

5150
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
45,821✔
5151
  void* pIter = NULL;
45,821✔
5152
  SStmSnodeStreamStatus* pSnode = NULL;
45,821✔
5153
  int64_t streamId = 0;
45,821✔
5154
  
5155
  while (true) {
5156
    pIter = taosHashIterate(pStreams, pIter);
538,696✔
5157
    if (NULL == pIter) {
538,696✔
5158
      break;
45,821✔
5159
    }
5160

5161
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
492,875✔
5162
    pSnode = (SStmSnodeStreamStatus*)pIter;
492,875✔
5163

5164
    if (NULL != pSnode->trigger) {
492,875✔
5165
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
473,778✔
5166
    }
5167

5168
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,971,500✔
5169
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,478,625✔
5170
      if (taskNum > 0) {
1,478,625✔
5171
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,381,000✔
5172
      }
5173
    }
5174
  }
5175
}
45,821✔
5176

5177

5178
void msmCheckSnodeStatus(SMnode *pMnode) {
403,603✔
5179
  void* pIter = NULL;
403,603✔
5180
  
5181
  while (true) {
522,530✔
5182
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
926,133✔
5183
    if (NULL == pIter) {
926,133✔
5184
      break;
403,603✔
5185
    }
5186

5187
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
522,530✔
5188
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
522,530✔
5189
      continue;
460,809✔
5190
    }
5191

5192
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
61,721✔
5193
    
5194
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
61,721✔
5195
    if (NULL == pSnode->streamTasks) {
61,721✔
5196
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
13,531✔
5197
      continue;
13,531✔
5198
    }
5199
    
5200
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
48,190✔
5201
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
2,369✔
5202
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5203
      
5204
      msmHandleSnodeLost(pMnode, pSnode);
2,369✔
5205
      continue;
2,369✔
5206
    }
5207
    
5208
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
45,821✔
5209

5210
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
45,821✔
5211
  }
5212
}
403,603✔
5213

5214

5215
void msmCheckTasksStatus(SMnode *pMnode) {
403,603✔
5216
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
403,603✔
5217

5218
  msmCheckVgroupStatus(pMnode);
403,603✔
5219
  msmCheckSnodeStatus(pMnode);
403,603✔
5220
}
403,603✔
5221

5222
void msmCheckSnodesState(SMnode *pMnode) {
403,603✔
5223
  if (!MST_READY_FOR_SNODE_LOOP()) {
403,603✔
5224
    return;
358,759✔
5225
  }
5226

5227
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
44,844✔
5228

5229
  void* pIter = NULL;
44,844✔
5230
  int32_t snodeId = 0;
44,844✔
5231
  while (true) {
51,254✔
5232
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
96,098✔
5233
    if (NULL == pIter) {
96,098✔
5234
      break;
44,844✔
5235
    }
5236

5237
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
51,254✔
5238
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
51,254✔
5239
      continue;
50,564✔
5240
    }
5241

5242
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
690✔
5243
    if (NULL == pSnode->streamTasks) {
690✔
UNCOV
5244
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
UNCOV
5245
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
UNCOV
5246
      continue;
×
5247
    }
5248
    
5249
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
690✔
5250
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5251
    
5252
    msmHandleSnodeLost(pMnode, pSnode);
690✔
5253
  }
5254

5255
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
44,844✔
5256
}
5257

5258
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
10,696,066✔
5259
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
10,696,066✔
5260
  if (0 == active || MND_STM_STATE_NORMAL != state) {
10,696,066✔
5261
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,728✔
5262
    return false;
2,728✔
5263
  }
5264

5265
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
10,693,338✔
5266
    mstTrace("ignore health check since no stream now");
9,886,132✔
5267
    return false;
9,886,132✔
5268
  }
5269

5270
  return true;
807,206✔
5271
}
5272

5273
void msmHealthCheck(SMnode *pMnode) {
10,292,463✔
5274
  if (!msmCheckNeedHealthCheck(pMnode)) {
10,292,463✔
5275
    return;
9,888,860✔
5276
  }
5277

5278
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
791,286✔
5279
  
5280
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
403,603✔
5281
  if (!msmCheckNeedHealthCheck(pMnode)) {
403,603✔
UNCOV
5282
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
UNCOV
5283
    return;
×
5284
  }
5285
  
5286
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
403,603✔
5287
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
403,603✔
5288

5289
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
403,603✔
5290
  
5291
  msmCheckStreamsStatus(pMnode);
403,603✔
5292
  msmCheckTasksStatus(pMnode);
403,603✔
5293
  msmCheckSnodesState(pMnode);
403,603✔
5294

5295
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
403,603✔
5296

5297
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
403,603✔
5298
}
5299

5300
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
826✔
5301
  SStreamObj *pStream = pObj;
826✔
5302
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
826✔
UNCOV
5303
    return true;
×
5304
  }
5305
  
5306
  pStream->updateTime = *(int64_t*)p1;
826✔
5307
  
5308
  (*(int32_t*)p2)++;
826✔
5309
  
5310
  return true;
826✔
5311
}
5312

5313
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
20,943✔
5314
  int32_t code = 0;
20,943✔
5315
  int8_t  stopped = 0;
20,943✔
5316
  
5317
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
20,943✔
5318
  
5319
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
20,943✔
5320
  if (NULL == pStatus) {
20,943✔
5321
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
2,808✔
5322
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
2,808✔
5323
    goto _exit;
2,808✔
5324
  }
5325

5326
  stopped = atomic_load_8(&pStatus->stopped);
18,135✔
5327
  if (stopped) {
18,135✔
UNCOV
5328
    mstsError("stream already stopped, stopped:%d", stopped);
×
UNCOV
5329
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
UNCOV
5330
    goto _exit;
×
5331
  }
5332

5333
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
18,135✔
5334
    pAddr->taskId = pStatus->triggerTask->id.taskId;
14,040✔
5335
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
14,040✔
5336
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
14,040✔
5337
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
14,040✔
5338
    goto _exit;
14,040✔
5339
  }
5340

5341
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
4,095✔
5342
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
4,095✔
5343

5344
_exit:
20,943✔
5345
  
5346
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
20,943✔
5347

5348
  return code;
20,943✔
5349
}
5350

5351
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
424,412✔
5352
  int32_t code = TSDB_CODE_SUCCESS;
424,412✔
5353
  int32_t lino = 0;
424,412✔
5354
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
424,412✔
5355
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
424,412✔
5356
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
424,412✔
5357

5358
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
847,142✔
5359

5360
  mStreamMgmt.stat.activeTimes++;
424,412✔
5361
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
424,412✔
5362
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
424,412✔
5363
  if (NULL == mStreamMgmt.tCtx) {
424,412✔
UNCOV
5364
    code = terrno;
×
UNCOV
5365
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
UNCOV
5366
    goto _exit;
×
5367
  }
5368

5369
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
424,412✔
5370
  if (mStreamMgmt.actionQ == NULL) {
424,412✔
5371
    code = terrno;
×
5372
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5373
    goto _exit;
×
5374
  }
5375
  
5376
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
424,412✔
5377
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
424,412✔
5378
  
5379
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
424,412✔
5380
  
5381
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,541,594✔
5382
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,117,182✔
5383

5384
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,703,092✔
5385
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,585,910✔
5386
      if (pCtx->deployStm[m] == NULL) {
10,585,910✔
UNCOV
5387
        code = terrno;
×
UNCOV
5388
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
UNCOV
5389
        goto _exit;
×
5390
      }
5391
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
10,585,910✔
5392
      
5393
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,585,910✔
5394
      if (pCtx->actionStm[m] == NULL) {
10,585,910✔
5395
        code = terrno;
×
5396
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
UNCOV
5397
        goto _exit;
×
5398
      }
5399
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
10,585,910✔
5400
    }
5401
  }
5402
  
5403
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
424,412✔
5404
  if (mStreamMgmt.streamMap == NULL) {
424,412✔
UNCOV
5405
    code = terrno;
×
UNCOV
5406
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
UNCOV
5407
    goto _exit;
×
5408
  }
5409
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
424,412✔
5410
  
5411
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
424,412✔
5412
  if (mStreamMgmt.taskMap == NULL) {
424,412✔
5413
    code = terrno;
×
5414
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
UNCOV
5415
    goto _exit;
×
5416
  }
5417
  
5418
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
424,412✔
5419
  if (mStreamMgmt.vgroupMap == NULL) {
424,412✔
5420
    code = terrno;
×
5421
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5422
    goto _exit;
×
5423
  }
5424
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
424,412✔
5425

5426
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
424,412✔
5427
  if (mStreamMgmt.snodeMap == NULL) {
424,412✔
5428
    code = terrno;
×
5429
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
UNCOV
5430
    goto _exit;
×
5431
  }
5432
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
424,412✔
5433
  
5434
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
424,412✔
5435
  if (mStreamMgmt.dnodeMap == NULL) {
424,412✔
5436
    code = terrno;
×
5437
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
UNCOV
5438
    goto _exit;
×
5439
  }
5440

5441
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
424,412✔
5442
  if (mStreamMgmt.toDeployVgMap == NULL) {
424,412✔
5443
    code = terrno;
×
5444
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5445
    goto _exit;
×
5446
  }
5447
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
424,412✔
5448
  
5449
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
424,412✔
5450
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
424,412✔
5451
    code = terrno;
×
5452
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
UNCOV
5453
    goto _exit;
×
5454
  }
5455
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
424,412✔
5456

5457
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
424,412✔
5458
  if (mStreamMgmt.toUpdateScanMap == NULL) {
424,412✔
5459
    code = terrno;
×
5460
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
UNCOV
5461
    goto _exit;
×
5462
  }
5463
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
424,412✔
5464

5465
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
424,412✔
5466
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
424,412✔
5467

5468
  mStreamMgmt.lastTaskId = 1;
424,412✔
5469

5470
  int32_t activeStreamNum = 0;
424,412✔
5471
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
424,412✔
5472

5473
  if (activeStreamNum > 0) {
424,412✔
5474
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
682✔
5475
  } else {
5476
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
423,730✔
5477
  }
5478

5479
_exit:
424,412✔
5480

5481
  if (code) {
424,412✔
UNCOV
5482
    msmDestroyRuntimeInfo(pMnode);
×
UNCOV
5483
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5484
  } else {
5485
    mstInfo("mnode stream runtime init done");
424,412✔
5486
  }
5487

5488
  return code;
424,412✔
5489
}
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