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

taosdata / TDengine / #4878

11 Dec 2025 02:43AM UTC coverage: 64.569% (-0.02%) from 64.586%
#4878

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

307 of 617 new or added lines in 24 files covered. (49.76%)

3821 existing lines in 123 files now uncovered.

163630 of 253417 relevant lines covered (64.57%)

107598827.89 hits per line

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

76.35
/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() {
844,870✔
34
  SStmQNode* pQNode = NULL;
844,870✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
844,870✔
37
    return;
422,435✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,111,809✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,670,854✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,559,045✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,559,045✔
51
  }
52
}
2,111,809✔
53

54
void msmDestroyThreadCtxs() {
844,870✔
55
  if (NULL == mStreamMgmt.tCtx) {
844,870✔
56
    return;
422,435✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,534,244✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,111,809✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
422,435✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
844,870✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
844,870✔
94
}
844,870✔
95

96

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

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

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

111
    pStatus = pStream;
757✔
112
  }
113

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

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

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

136
_exit:
398✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
8,054✔
139

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

145

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

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

200
  pSnode = NULL;
457,307✔
201

202
_exit:
457,307✔
203

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

208
  return code;
457,307✔
209
}
210

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

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

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

237
_exit:
1,012,566✔
238

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

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

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
608,005✔
275
  int32_t code = TSDB_CODE_SUCCESS;
608,005✔
276
  int32_t lino = 0;
608,005✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
608,005✔
278
  if (NULL == pStream) {
608,005✔
279
    SStmVgStreamStatus stream = {0};
461,308✔
280
    if (trigReader) {
461,308✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
299,901✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
299,901✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
599,802✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
161,407✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
161,407✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
322,814✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
461,308✔
290
    goto _exit;
461,308✔
291
  }
292
  
293
  if (trigReader) {
146,697✔
294
    if (NULL == pStream->trigReaders) {
23,122✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
23,122✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
23,122✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
46,244✔
300
    goto _exit;
23,122✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
123,575✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
91,837✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
91,837✔
306
  }
307

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

310
_exit:
123,575✔
311

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

320
  return code;
608,005✔
321
}
322

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

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

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

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

350
  return code;
608,005✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
610,117✔
363
    if (NULL == pVg) {
610,117✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
136,301✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
136,301✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
272,602✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
136,301✔
368
      if (TSDB_CODE_SUCCESS == code) {
136,301✔
369
        goto _return;
136,301✔
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);
473,816✔
381
    if (NULL == pVg->taskList) {
473,816✔
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)) {
947,632✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
473,816✔
390
    
391
    taosHashRelease(pVgMap, pVg);
473,816✔
392
    break;
473,816✔
393
  }
394
  
395
_return:
610,117✔
396

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

404
  return code;
610,117✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
862,393✔
409
  int32_t code = TSDB_CODE_SUCCESS;
862,393✔
410
  int32_t lino = 0;
862,393✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
862,393✔
412
  if (NULL == pStream) {
862,393✔
413
    SStmSnodeStreamStatus stream = {0};
220,214✔
414
    if (deployId < 0) {
220,214✔
415
      stream.trigger = pStatus;
3,383✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
216,831✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
216,831✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
433,662✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
220,214✔
423
    goto _exit;
220,214✔
424
  }
425
  
426
  if (deployId < 0) {
642,179✔
427
    if (NULL != pStream->trigger) {
206,286✔
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;
206,286✔
433
    goto _exit;
206,286✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
435,893✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
402,731✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
402,731✔
439
  }
440

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

443
_exit:
435,893✔
444

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

453
  return code;
862,393✔
454
}
455

456

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

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

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

484
  return code;
862,393✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
209,669✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
58,176✔
568
      ext.deployed = false;
58,176✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
116,352✔
570

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

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

614
  return code;
652,724✔
615
}
616

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

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

632
_exit:
619,562✔
633

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

638
  return code;
619,562✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
15,845✔
676
    }
677

678
    break;
679
  }
680

UNCOV
681
_exit:
×
682

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

UNCOV
687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

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

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

719
  mstDebug("start to update vgroups upTs");
14,649,995✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
51,248,673✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
36,598,678✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
36,598,678✔
725
  }
726

727
_exit:
14,649,995✔
728

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

733
  return code;
14,649,995✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
397,103✔
739
  int32_t num = taosArrayGetSize(pList);
397,103✔
740
  for (int32_t i = 0; i < num; ++i) {
1,036,137✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
856,958✔
742
    if (pScan->readFromCache) {
856,958✔
743
      return pScan->scanPlan;
217,924✔
744
    }
745
  }
746

747
  return NULL;
179,179✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
610,117✔
775
}
776

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

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

790
    if (!STREAM_IS_TOP_RUNNER(pStatus->flags)) {
618,858✔
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;
608,085✔
797
    runner.addr.taskId = pStatus->id.taskId;
618,858✔
798
    runner.addr.nodeId = pStatus->id.nodeId;
618,858✔
799
    runner.addr.epset = mndGetDnodeEpsetById(pMnode, pStatus->id.nodeId);
618,858✔
800
    runner.execReplica = pInfo->runnerReplica; 
618,858✔
801
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &runner), code, lino, _exit, terrno);
1,237,716✔
802
    mstsDebug("the %dth runner target added to trigger's runnerList, TASK:%" PRIx64 , i, runner.addr.taskId);
618,858✔
803
  }
804

805
_exit:
206,286✔
806

807
  if (code) {
206,286✔
808
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
809
  }
810
  
811
  return TSDB_CODE_SUCCESS;
206,286✔
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) {
209,669✔
837
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
838
  int32_t lino = 0;
209,669✔
839
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
840
  SStreamTriggerDeployMsg* pMsg = &pDeploy->msg.trigger;
209,669✔
841
  
842
  pMsg->triggerType = pStream->pCreate->triggerType;
209,669✔
843
  pMsg->igDisorder = pStream->pCreate->igDisorder;
209,669✔
844
  pMsg->fillHistory = pStream->pCreate->fillHistory;
209,669✔
845
  pMsg->fillHistoryFirst = pStream->pCreate->fillHistoryFirst;
209,669✔
846
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
209,669✔
847
  pMsg->igNoDataTrigger = pStream->pCreate->igNoDataTrigger;
209,669✔
848
  pMsg->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags);
209,669✔
849
  pMsg->triggerHasPF = pStream->pCreate->triggerHasPF;
209,669✔
850
  pMsg->isTriggerTblStb = (pStream->pCreate->triggerTblType == TSDB_SUPER_TABLE);
209,669✔
851
  pMsg->precision = pStream->pCreate->triggerPrec;
209,669✔
852
  pMsg->partitionCols = pStream->pCreate->partitionCols;
209,669✔
853

854
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
209,669✔
855
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
209,669✔
856
  pMsg->addOptions = pStream->pCreate->addOptions;
209,669✔
857
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
209,669✔
858

859
  pMsg->maxDelay = pStream->pCreate->maxDelay;
209,669✔
860
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
209,669✔
861
  pMsg->watermark = pStream->pCreate->watermark;
209,669✔
862
  pMsg->expiredTime = pStream->pCreate->expiredTime;
209,669✔
863
  pMsg->trigger = pInfo->pCreate->trigger;
209,669✔
864

865
  pMsg->eventTypes = pStream->pCreate->eventTypes;
209,669✔
866
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
209,669✔
867
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
209,669✔
868
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
209,669✔
869
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
209,669✔
870
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
209,669✔
871
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
73,024✔
872
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
73,024✔
873
  }
874

875
  SStreamTaskAddr addr;
206,078✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
209,669✔
877
  if (triggerReaderNum > 0) {
209,669✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
208,523✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
208,523✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
468,397✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
258,728✔
884
    addr.taskId = pStatus->id.taskId;
258,728✔
885
    addr.nodeId = pStatus->id.nodeId;
258,728✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
258,728✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
517,456✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
258,728✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
209,669✔
892
  pMsg->streamName = pInfo->streamName;
209,669✔
893

894
  if (0 == pInfo->runnerNum) {
209,669✔
895
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
3,383✔
896
    return code;
3,383✔
897
  }
898

899
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
206,286✔
900

901
_exit:
206,286✔
902

903
  if (code) {
206,286✔
904
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
905
  } else {
906
    mstsDebug("trigger deploy info built, readerNum:%d, runnerNum:%d", (int32_t)taosArrayGetSize(pMsg->readerList), (int32_t)taosArrayGetSize(pMsg->runnerList));
206,286✔
907
  }
908
  
909
  return TSDB_CODE_SUCCESS;
206,286✔
910
}
911

912

913
int32_t msmBuildRunnerDeployInfo(SStmTaskDeploy* pDeploy, SSubplan *plan, SStreamObj* pStream, SStmStatus* pInfo, bool topPlan) {
652,724✔
914
  int32_t code = TSDB_CODE_SUCCESS;
652,724✔
915
  int32_t lino = 0;
652,724✔
916
  int64_t streamId = pStream->pCreate->streamId;
652,724✔
917
  SStreamRunnerDeployMsg* pMsg = &pDeploy->msg.runner;
652,724✔
918
  //TAOS_CHECK_EXIT(qSubPlanToString(plan, &pMsg->pPlan, NULL));
919

920
  pMsg->execReplica = pInfo->runnerReplica;
652,724✔
921
  pMsg->streamName = pInfo->streamName;
652,724✔
922
  //TAOS_CHECK_EXIT(nodesCloneNode((SNode*)plan, (SNode**)&pMsg->pPlan));
923
  pMsg->pPlan = plan;
652,724✔
924
  pMsg->outDBFName = pInfo->pCreate->outDB;
652,724✔
925
  pMsg->outTblName = pInfo->pCreate->outTblName;
652,724✔
926
  pMsg->outTblType = pStream->pCreate->outTblType;
652,724✔
927
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
652,724✔
928
  pMsg->calcNotifyOnly = pStream->pCreate->calcNotifyOnly;
652,724✔
929
  pMsg->topPlan = topPlan;
652,724✔
930
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
652,724✔
931
  pMsg->addOptions = pStream->pCreate->addOptions;
652,724✔
932
  if(pStream->pCreate->trigger.sliding.overlap) {
652,724✔
933
    pMsg->addOptions |= CALC_SLIDING_OVERLAP;
13,458✔
934
  }
935
  pMsg->outCols = pInfo->pCreate->outCols;
652,724✔
936
  pMsg->outTags = pInfo->pCreate->outTags;
652,724✔
937
  pMsg->outStbUid = pStream->pCreate->outStbUid;
652,724✔
938
  pMsg->outStbSversion = pStream->pCreate->outStbSversion;
652,724✔
939
  
940
  pMsg->subTblNameExpr = pInfo->pCreate->subTblNameExpr;
652,724✔
941
  pMsg->tagValueExpr = pInfo->pCreate->tagValueExpr;
652,724✔
942
  pMsg->forceOutCols = pInfo->pCreate->forceOutCols;
652,724✔
943

944
_exit:
652,724✔
945

946
  if (code) {
652,724✔
947
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
948
  }
949
  
950
  return code;
652,724✔
951
}
952

953

954
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
487,912✔
955
  int32_t code = TSDB_CODE_SUCCESS;
487,912✔
956
  int32_t lino = 0;
487,912✔
957
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
487,912✔
958
  SListNode* pNode = pList ? listHead(pList) : NULL;
487,912✔
959
  
960
  for (int32_t i = 0; i < taskNum; ++i) {
1,095,917✔
961
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
608,005✔
962
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
608,005✔
963
    if (pNode) {
608,005✔
964
      pNode = TD_DLIST_NODE_NEXT(pNode);
280,703✔
965
    }
966
  }
967
  
968
_exit:
487,912✔
969

970
  if (code) {
487,912✔
971
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
972
  }
973
  
974
  return code;
487,912✔
975
}
976

977

978
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
829,231✔
979
  int32_t code = TSDB_CODE_SUCCESS;
829,231✔
980
  int32_t lino = 0;
829,231✔
981
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
829,231✔
982
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
829,231✔
983
  
984
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,691,624✔
985
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
862,393✔
986
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
862,393✔
987
  }
988
  
989
_exit:
829,231✔
990

991
  if (code) {
829,231✔
992
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
993
  }
994

995
  return code;
829,231✔
996
}
997

998
int64_t msmAssignTaskId(void) {
1,469,694✔
999
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,469,694✔
1000
}
1001

1002
int64_t msmAssignTaskSeriousId(void) {
1,469,694✔
1003
  return taosGetTimestampNs();
1,469,694✔
1004
}
1005

1006

1007
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
1,088,099✔
1008
  int32_t code = TSDB_CODE_SUCCESS;
1,088,099✔
1009
  int32_t lino = 0;
1,088,099✔
1010
  bool     noExists = false;
1,088,099✔
1011
  SStmSnodeStatus* pStatus = NULL;
1,088,099✔
1012

1013
  while (true) {
1014
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
1,101,316✔
1015
    if (NULL == pStatus) {
1,101,316✔
1016
      if (noExists) {
13,217✔
1017
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1018
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1019
      }
1020

1021
      noExists = true;
13,217✔
1022
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
13,217✔
1023
      
1024
      continue;
13,217✔
1025
    }
1026

1027
    *alive = (pStatus->runnerThreadNum >= 0);
1,088,099✔
1028
    break;
1,088,099✔
1029
  }
1030

1031
_exit:
1,088,099✔
1032

1033
  if (code) {
1,088,099✔
1034
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1035
  }
1036

1037
  return code;
1,088,099✔
1038
}
1039

1040
int32_t msmRetrieveStaticSnodeId(SMnode* pMnode, SStreamObj* pStream) {
416,659✔
1041
  int32_t code = TSDB_CODE_SUCCESS;
416,659✔
1042
  int32_t lino = 0;
416,659✔
1043
  bool alive = false;
416,659✔
1044
  int32_t mainSnodeId = atomic_load_32(&pStream->mainSnodeId);
416,659✔
1045
  int32_t snodeId = mainSnodeId;
416,659✔
1046
  int64_t streamId = pStream->pCreate->streamId;
416,659✔
1047
  
1048
  while (true) {
1049
    TAOS_CHECK_EXIT(msmIsSnodeAlive(pMnode, snodeId, streamId, &alive));
417,469✔
1050

1051
    if (alive) {
417,469✔
1052
      return snodeId;
416,659✔
1053
    }
1054
    
1055
    if (snodeId == mainSnodeId) {
810✔
1056
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
810✔
1057
      if (NULL == pSnode) {
810✔
1058
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1059
        return 0;
×
1060
      }
1061
      
1062
      if (pSnode->replicaId <= 0) {
810✔
1063
        mstsError("no available snode now, mainSnodeId:%d, replicaId:%d", mainSnodeId, pSnode->replicaId);
×
1064
        mndReleaseSnode(pMnode, pSnode);
×
1065
        return 0;
×
1066
      }
1067

1068
      snodeId = pSnode->replicaId;
810✔
1069
      mndReleaseSnode(pMnode, pSnode);
810✔
1070
      
1071
      continue;
810✔
1072
    }
1073

1074
    mstsError("no available snode now, mainSnodeId:%d, followerSnodeId:%d", mainSnodeId, snodeId);
×
1075
    return 0;
×
1076
  }
1077

1078
_exit:
×
1079

1080
  if (code) {
×
1081
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1082
  }
1083

1084
  return 0;
×
1085
}
1086

1087
int32_t msmAssignRandomSnodeId(SMnode* pMnode, int64_t streamId) {
640,082✔
1088
  int32_t code = TSDB_CODE_SUCCESS;
640,082✔
1089
  int32_t lino = 0;
640,082✔
1090
  int32_t snodeIdx = 0;
640,082✔
1091
  int32_t snodeId = 0;
640,082✔
1092
  void      *pIter = NULL;
640,082✔
1093
  SSnodeObj *pObj = NULL;
640,082✔
1094
  bool alive = false;
640,082✔
1095
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
640,082✔
1096
  if (snodeNum <= 0) {
640,082✔
1097
    mstsInfo("no available snode now, num:%d", snodeNum);
4,240✔
1098
    goto _exit;
4,240✔
1099
  }
1100

1101
  int32_t snodeTarget = taosRand() % snodeNum;
635,842✔
1102

1103
  while (1) {
1104
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
671,035✔
1105
    if (pIter == NULL) {
671,035✔
1106
      if (0 == snodeId) {
405✔
1107
        mstsError("no alive snode now, snodeNum:%d", snodeNum);
×
1108
        break;
×
1109
      }
1110
      
1111
      snodeId = 0;
405✔
1112
      continue;
405✔
1113
    }
1114

1115
    code = msmIsSnodeAlive(pMnode, pObj->id, streamId, &alive);
670,630✔
1116
    if (code) {
670,630✔
1117
      sdbRelease(pMnode->pSdb, pObj);
×
1118
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1119
      pObj = NULL;
×
1120
      TAOS_CHECK_EXIT(code);
×
1121
    }
1122
    
1123
    if (!alive) {
670,630✔
1124
      sdbRelease(pMnode->pSdb, pObj);
405✔
1125
      continue;
405✔
1126
    }
1127

1128
    snodeId = pObj->id;
670,225✔
1129
    if (snodeIdx == snodeTarget) {
670,225✔
1130
      sdbRelease(pMnode->pSdb, pObj);
635,842✔
1131
      sdbCancelFetch(pMnode->pSdb, pIter);
635,842✔
1132
      pObj = NULL;
635,842✔
1133
      goto _exit;
635,842✔
1134
    }
1135

1136
    sdbRelease(pMnode->pSdb, pObj);
34,383✔
1137
    snodeIdx++;
34,383✔
1138
  }
1139

1140
_exit:
640,082✔
1141

1142
  if (code) {
640,082✔
1143
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1144
  }
1145

1146
  if (0 == snodeId) {
640,082✔
1147
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
4,240✔
1148
  }
1149

1150
  return snodeId;
640,082✔
1151
}
1152

1153
int32_t msmAssignTaskSnodeId(SMnode* pMnode, SStreamObj* pStream, bool isStatic) {
829,231✔
1154
  int64_t streamId = pStream->pCreate->streamId;
829,231✔
1155
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
829,231✔
1156
  int32_t snodeId = 0;
829,231✔
1157
  if (snodeNum <= 0) {
829,231✔
1158
    mstsInfo("no available snode now, num:%d", snodeNum);
×
1159
    goto _exit;
×
1160
  }
1161

1162
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
829,231✔
1163

1164
_exit:
829,231✔
1165

1166
  if (0 == snodeId) {
829,231✔
1167
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1168
  }
1169

1170
  return snodeId;
829,231✔
1171
}
1172

1173

1174
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
209,669✔
1175
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
1176
  int32_t lino = 0;
209,669✔
1177
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
1178

1179
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
209,669✔
1180
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
209,669✔
1181

1182
  pInfo->triggerTask->id.taskId = pCtx->triggerTaskId;
209,669✔
1183
  pInfo->triggerTask->id.deployId = 0;
209,669✔
1184
  pInfo->triggerTask->id.seriousId = msmAssignTaskSeriousId();
209,669✔
1185
  pInfo->triggerTask->id.nodeId = pCtx->triggerNodeId;
209,669✔
1186
  pInfo->triggerTask->id.taskIdx = 0;
209,669✔
1187
  pInfo->triggerTask->type = STREAM_TRIGGER_TASK;
209,669✔
1188
  pInfo->triggerTask->lastUpTs = pCtx->currTs;
209,669✔
1189
  pInfo->triggerTask->pStream = pInfo;
209,669✔
1190

1191
  SStmTaskDeploy info = {0};
209,669✔
1192
  info.task.type = pInfo->triggerTask->type;
209,669✔
1193
  info.task.streamId = streamId;
209,669✔
1194
  info.task.taskId =  pInfo->triggerTask->id.taskId;
209,669✔
1195
  info.task.seriousId = pInfo->triggerTask->id.seriousId;
209,669✔
1196
  info.task.nodeId =  pInfo->triggerTask->id.nodeId;
209,669✔
1197
  info.task.taskIdx =  pInfo->triggerTask->id.taskIdx;
209,669✔
1198
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pInfo, &info, pStream));
209,669✔
1199
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
209,669✔
1200
  
1201
  (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
209,669✔
1202

1203
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
209,669✔
1204
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
209,669✔
1205

1206
_exit:
209,669✔
1207

1208
  if (code) {
209,669✔
1209
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1210
  }
1211

1212
  return code;
209,669✔
1213
}
1214

1215
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
323,023✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
323,023✔
1217
  int32_t lino = 0;
323,023✔
1218

1219
  pState->id.taskId = msmAssignTaskId();
323,023✔
1220
  pState->id.deployId = 0;
323,023✔
1221
  pState->id.seriousId = msmAssignTaskSeriousId();
323,023✔
1222
  pState->id.nodeId = nodeId;
323,023✔
1223
  pState->id.taskIdx = 0;
323,023✔
1224
  pState->type = STREAM_READER_TASK;
323,023✔
1225
  pState->flags = STREAM_FLAG_TRIGGER_READER;
323,023✔
1226
  pState->status = STREAM_STATUS_UNDEPLOYED;
323,023✔
1227
  pState->lastUpTs = pCtx->currTs;
323,023✔
1228
  pState->pStream = pInfo;
323,023✔
1229
  
1230
  SStmTaskDeploy info = {0};
323,023✔
1231
  info.task.type = pState->type;
323,023✔
1232
  info.task.streamId = streamId;
323,023✔
1233
  info.task.taskId = pState->id.taskId;
323,023✔
1234
  info.task.flags = pState->flags;
323,023✔
1235
  info.task.seriousId = pState->id.seriousId;
323,023✔
1236
  info.task.nodeId = pState->id.nodeId;
323,023✔
1237
  info.task.taskIdx = pState->id.taskIdx;
323,023✔
1238
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, NULL, pInfo, true));
323,023✔
1239
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
323,023✔
1240

1241
_exit:
323,023✔
1242

1243
  if (code) {
323,023✔
1244
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1245
  }
1246

1247
  return code;
323,023✔
1248
}
1249

1250
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
209,669✔
1251
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
1252
  int32_t lino = 0;
209,669✔
1253
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
1254
  SSdb   *pSdb = pCtx->pMnode->pSdb;
209,669✔
1255
  SStmTaskStatus* pState = NULL;
209,669✔
1256
  SVgObj *pVgroup = NULL;
209,669✔
1257
  SDbObj* pDb = NULL;
209,669✔
1258
  
1259
  switch (pStream->pCreate->triggerTblType) {
209,669✔
1260
    case TSDB_NORMAL_TABLE:
96,942✔
1261
    case TSDB_CHILD_TABLE:
1262
    case TSDB_VIRTUAL_CHILD_TABLE:
1263
    case TSDB_VIRTUAL_NORMAL_TABLE: {
1264
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
96,942✔
1265
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
96,942✔
1266
      pState = taosArrayGet(pInfo->trigReaders, 0);
96,942✔
1267
      
1268
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
96,942✔
1269
      break;
96,942✔
1270
    }
1271
    case TSDB_SUPER_TABLE: {
111,581✔
1272
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
111,581✔
1273
      if (NULL == pDb) {
111,581✔
1274
        code = terrno;
×
1275
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
1276
        goto _exit;
×
1277
      }
1278

1279
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
111,581✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
111,581✔
1281
      
1282
      void *pIter = NULL;
111,581✔
1283
      while (1) {
543,799✔
1284
        SStmTaskDeploy info = {0};
655,380✔
1285
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
655,380✔
1286
        if (pIter == NULL) {
655,380✔
1287
          break;
111,581✔
1288
        }
1289
      
1290
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
543,799✔
1291
          pState = taosArrayReserve(pInfo->trigReaders, 1);
161,786✔
1292

1293
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
161,786✔
1294
          if (code) {
161,786✔
1295
            sdbRelease(pSdb, pVgroup);
×
1296
            sdbCancelFetch(pSdb, pIter);
×
1297
            pVgroup = NULL;
×
1298
            TAOS_CHECK_EXIT(code);
×
1299
          }
1300
        }
1301

1302
        sdbRelease(pSdb, pVgroup);
543,799✔
1303
      }
1304
      break;
111,581✔
1305
    }
1306
    default:
1,146✔
1307
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,146✔
1308
      break;
1,146✔
1309
  }
1310

1311
_exit:
209,669✔
1312

1313
  mndReleaseDb(pCtx->pMnode, pDb);
209,669✔
1314

1315
  if (code) {
209,669✔
1316
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1317
  }
1318

1319
  return code;
209,669✔
1320
}
1321

1322
int32_t msmUPAddScanTask(SStmGrpCtx* pCtx, SStreamObj* pStream, char* scanPlan, int32_t vgId, int64_t taskId) {
281,407✔
1323
  int32_t code = TSDB_CODE_SUCCESS;
281,407✔
1324
  int32_t lino = 0;
281,407✔
1325
  SSubplan* pSubplan = NULL;
281,407✔
1326
  int64_t streamId = pStream->pCreate->streamId;
281,407✔
1327
  int64_t key[2] = {streamId, 0};
281,407✔
1328
  SStmTaskSrcAddr addr;
277,816✔
1329
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
281,407✔
1330
  addr.isFromCache = false;
281,407✔
1331
  
1332
  if (MNODE_HANDLE == vgId) {
281,407✔
1333
    mndGetMnodeEpSet(pCtx->pMnode, &addr.epset);
×
1334
  } else if (vgId > MNODE_HANDLE) {
281,407✔
1335
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
281,407✔
1336
  } else {
1337
    mstsError("invalid vgId %d in scanPlan", vgId);
×
1338
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1339
  }
1340
  
1341
  addr.taskId = taskId;
281,407✔
1342
  addr.vgId = vgId;
281,407✔
1343
  addr.groupId = pSubplan->id.groupId;
281,407✔
1344

1345
  key[1] = pSubplan->id.subplanId;
281,407✔
1346

1347
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
281,407✔
1348
  if (NULL == ppRes) {
281,407✔
1349
    SArray* pRes = taosArrayInit(1, sizeof(addr));
281,407✔
1350
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
281,407✔
1351
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
562,814✔
1352
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
281,407✔
1353
  } else {
1354
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1355
  }
1356

1357
  mstsDebug("calcReader %" PRIx64 " added to toUpdateScan, vgId:%d, groupId:%d, subplanId:%d", taskId, vgId, pSubplan->id.groupId, pSubplan->id.subplanId);
281,407✔
1358
  
1359
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
281,407✔
1360
  
1361
_exit:
281,407✔
1362

1363
  nodesDestroyNode((SNode*)pSubplan);
281,407✔
1364

1365
  if (code) {
281,407✔
1366
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1367
  }
1368

1369
  return code;
281,407✔
1370
}
1371

1372
int32_t msmUPAddCacheTask(SStmGrpCtx* pCtx, SStreamCalcScan* pScan, SStreamObj* pStream) {
99,603✔
1373
  int32_t code = TSDB_CODE_SUCCESS;
99,603✔
1374
  int32_t lino = 0;
99,603✔
1375
  SSubplan* pSubplan = NULL;
99,603✔
1376
  int64_t streamId = pStream->pCreate->streamId;
99,603✔
1377
  int64_t key[2] = {streamId, 0};
99,603✔
1378
  TAOS_CHECK_EXIT(nodesStringToNode(pScan->scanPlan, (SNode**)&pSubplan));
99,603✔
1379

1380
  SStmTaskSrcAddr addr;
99,603✔
1381
  addr.isFromCache = true;
99,603✔
1382
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
99,603✔
1383
  addr.taskId = pCtx->triggerTaskId;
99,603✔
1384
  addr.vgId = pCtx->triggerNodeId;
99,603✔
1385
  addr.groupId = pSubplan->id.groupId;
99,603✔
1386

1387
  key[1] = pSubplan->id.subplanId;
99,603✔
1388
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
99,603✔
1389
  if (NULL == ppRes) {
99,603✔
1390
    SArray* pRes = taosArrayInit(1, sizeof(addr));
99,603✔
1391
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
99,603✔
1392
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
199,206✔
1393
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
99,603✔
1394
  } else {
1395
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1396
  }
1397
  
1398
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
99,603✔
1399
  
1400
_exit:
99,603✔
1401

1402
  nodesDestroyNode((SNode*)pSubplan);
99,603✔
1403
  
1404
  if (code) {
99,603✔
1405
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1406
  }
1407

1408
  return code;
99,603✔
1409
}
1410

1411

1412
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
284,982✔
1413
  int32_t code = TSDB_CODE_SUCCESS;
284,982✔
1414
  int32_t lino = 0;
284,982✔
1415

1416
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
284,982✔
1417

1418
  pState->id.taskId = msmAssignTaskId();
284,982✔
1419
  pState->id.deployId = 0;
284,982✔
1420
  pState->id.seriousId = msmAssignTaskSeriousId();
284,982✔
1421
  pState->id.nodeId = nodeId;
284,982✔
1422
  pState->id.taskIdx = taskIdx;
284,982✔
1423
  pState->type = STREAM_READER_TASK;
284,982✔
1424
  pState->flags = 0;
284,982✔
1425
  pState->status = STREAM_STATUS_UNDEPLOYED;
284,982✔
1426
  pState->lastUpTs = pCtx->currTs;
284,982✔
1427
  pState->pStream = pInfo;
284,982✔
1428
  
1429
  SStmTaskDeploy info = {0};
284,982✔
1430
  info.task.type = pState->type;
284,982✔
1431
  info.task.streamId = streamId;
284,982✔
1432
  info.task.taskId = pState->id.taskId;
284,982✔
1433
  info.task.flags = pState->flags;
284,982✔
1434
  info.task.seriousId = pState->id.seriousId;
284,982✔
1435
  info.task.nodeId = pState->id.nodeId;
284,982✔
1436
  info.task.taskIdx = pState->id.taskIdx;
284,982✔
1437
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, calcScanPlan, pInfo, false));
284,982✔
1438
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
284,982✔
1439

1440
_exit:
284,982✔
1441

1442
  if (code) {
284,982✔
1443
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1444
  }
1445

1446
  return code;
284,982✔
1447
}
1448

1449

1450
static int32_t msmTDAddCalcReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
209,669✔
1451
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
1452
  int32_t lino = 0;
209,669✔
1453
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
209,669✔
1454
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
1455
  SStmTaskStatus* pState = NULL;
209,669✔
1456
  pInfo->calcReaders = tdListNew(sizeof(SStmTaskStatus));
209,669✔
1457
  TSDB_CHECK_NULL(pInfo->calcReaders, code, lino, _exit, terrno);
209,669✔
1458

1459
  
1460
  for (int32_t i = 0; i < calcTasksNum; ++i) {
589,975✔
1461
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
380,306✔
1462
    if (pScan->readFromCache) {
380,306✔
1463
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
99,603✔
1464
      continue;
99,603✔
1465
    }
1466
    
1467
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
280,703✔
1468
    for (int32_t m = 0; m < vgNum; ++m) {
561,406✔
1469
      pState = tdListReserve(pInfo->calcReaders);
280,703✔
1470
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
280,703✔
1471

1472
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
280,703✔
1473
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
280,703✔
1474
    }
1475
  }
1476

1477
_exit:
209,669✔
1478

1479
  if (code) {
209,669✔
1480
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1481
  }
1482

1483
  return code;
209,669✔
1484
}
1485

1486

1487

1488
static int32_t msmUPPrepareReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
704✔
1489
  int32_t code = TSDB_CODE_SUCCESS;
704✔
1490
  int32_t lino = 0;
704✔
1491
  int64_t streamId = pStream->pCreate->streamId;
704✔
1492
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
704✔
1493
  if (calcTasksNum <= 0) {
704✔
1494
    mstsDebug("no calc scan plan, ignore parepare reader tasks, readerNum:%d", (int32_t)MST_LIST_SIZE(pInfo->calcReaders));
×
1495
    return code;    
×
1496
  }
1497
  
1498
  SListNode* pNode = listHead(pInfo->calcReaders);
704✔
1499
  
1500
  for (int32_t i = 0; i < calcTasksNum; ++i) {
1,408✔
1501
    SStreamCalcScan* pScan = taosArrayGet(pStream->pCreate->calcScanPlanList, i);
704✔
1502
    if (pScan->readFromCache) {
704✔
1503
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
×
1504
      continue;
×
1505
    }
1506
    
1507
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
704✔
1508
    for (int32_t m = 0; m < vgNum; ++m) {
1,408✔
1509
      SStmTaskStatus* pReader = (SStmTaskStatus*)pNode->data;
704✔
1510
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pReader->id.nodeId, pReader->id.taskId));
704✔
1511
      pNode = TD_DLIST_NODE_NEXT(pNode);
704✔
1512
    }
1513
  }
1514

1515
_exit:
704✔
1516

1517
  if (code) {
704✔
1518
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1519
  }
1520

1521
  return code;
704✔
1522
}
1523

1524
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
209,669✔
1525
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
1526
  int32_t lino = 0;
209,669✔
1527
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
1528
  
1529
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
209,669✔
1530
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
209,669✔
1531

1532
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
209,669✔
1533
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
209,669✔
1534
  
1535
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
209,669✔
1536
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
209,669✔
1537
  
1538
_exit:
209,669✔
1539

1540
  if (code) {
209,669✔
1541
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1542
  }
1543
  
1544
  return code;
209,669✔
1545
}
1546

1547
int32_t msmUpdatePlanSourceAddr(SStreamTask* pTask, int64_t streamId, SSubplan* plan, int64_t clientId, SStmTaskSrcAddr* pSrc, int32_t msgType, int64_t srcSubplanId) {
1,174,784✔
1548
  SDownstreamSourceNode source = {
1,174,784✔
1549
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1550
      .clientId = clientId,
1551
      .taskId = pSrc->taskId,
1,174,784✔
1552
      .sId = 0,
1553
      .execId = 0,
1554
      .fetchMsgType = msgType,
1555
      .localExec = false,
1556
  };
1557

1558
  source.addr.epSet = pSrc->epset;
1,174,784✔
1559
  source.addr.nodeId = pSrc->vgId;
1,174,784✔
1560

1561
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
1,174,784✔
1562
      plan->id.subplanId, pSrc->groupId, srcSubplanId, source.clientId, source.taskId, source.addr.nodeId, TMSG_INFO(source.fetchMsgType));
1563
  
1564
  return qSetSubplanExecutionNode(plan, pSrc->groupId, &source);
1,174,784✔
1565
}
1566

1567
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
33,162✔
1568
  int64_t streamId = pStream->pCreate->streamId;
33,162✔
1569
  int32_t runnerNum = taosArrayGetSize(pRunners);
33,162✔
1570
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
82,905✔
1571
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
82,905✔
1572
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
82,905✔
1573
    if (pPlan->id.subplanId == subplanId) {
82,905✔
1574
      *taskId = pDeploy->task.taskId;
33,162✔
1575
      *ppParent = &pDeploy->task;
33,162✔
1576
      return TSDB_CODE_SUCCESS;
33,162✔
1577
    }
1578
  }
1579

1580
  mstsError("subplanId %d not found in runner list", subplanId);
×
1581

1582
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1583
}
1584

1585
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
652,724✔
1586
  int32_t code = TSDB_CODE_SUCCESS;
652,724✔
1587
  int32_t lino = 0;
652,724✔
1588
  int64_t key[2] = {streamId, -1};
652,724✔
1589
  SNode* pNode = NULL;
652,724✔
1590
  SStreamTask* pTask = &pDeploy->task;
652,724✔
1591
  FOREACH(pNode, pPlan->pChildren) {
1,827,508✔
1592
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,174,784✔
1593
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
33,162✔
1594
      continue;
33,162✔
1595
    }
1596
    
1597
    SValueNode* pVal = (SValueNode*)pNode;
1,141,622✔
1598
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,141,622✔
1599
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1600
      continue;
×
1601
    }
1602

1603
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,141,622✔
1604

1605
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
1,141,622✔
1606
    if (NULL == ppRes) {
1,141,622✔
1607
      msttError("lowest runner subplan ID:%d,%d can't get its child ID:%" PRId64 " addr", pPlan->id.groupId, pPlan->id.subplanId, key[1]);
×
1608
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1609
    }
1610

1611
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,141,622✔
1612
    for (int32_t i = 0; i < childrenNum; ++i) {
2,283,244✔
1613
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,141,622✔
1614
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,141,622✔
1615
    }
1616
  }
1617

1618
_exit:
652,724✔
1619

1620
  if (code) {
652,724✔
1621
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1622
  }
1623

1624
  return code;
652,724✔
1625
}
1626

1627
int32_t msmUpdateRunnerPlan(SStmGrpCtx* pCtx, SArray* pRunners, int32_t beginIdx, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
652,724✔
1628
  int32_t code = TSDB_CODE_SUCCESS;
652,724✔
1629
  int32_t lino = 0;
652,724✔
1630
  SSubplan* pPlan = pDeploy->msg.runner.pPlan;
652,724✔
1631
  SStreamTask* pTask = &pDeploy->task;
652,724✔
1632
  SStreamTask* parentTask = NULL;
652,724✔
1633
  int64_t streamId = pStream->pCreate->streamId;
652,724✔
1634

1635
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
652,724✔
1636

1637
  SNode* pTmp = NULL;
652,724✔
1638
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,827,508✔
1639
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,174,784✔
1640
      ERASE_NODE(pPlan->pChildren);
1,141,622✔
1641
      continue;
1,141,622✔
1642
    }
1643
    WHERE_NEXT;
33,162✔
1644
  }
1645
  nodesClearList(pPlan->pChildren);
652,724✔
1646
  pPlan->pChildren = NULL;
652,724✔
1647

1648
  if (NULL == pPlan->pParents) {
652,724✔
1649
    goto _exit;
619,562✔
1650
  }
1651

1652
  SNode* pNode = NULL;
33,162✔
1653
  int64_t parentTaskId = 0;
33,162✔
1654
  SStmTaskSrcAddr addr = {0};
33,162✔
1655
  addr.taskId = pDeploy->task.taskId;
33,162✔
1656
  addr.vgId = pDeploy->task.nodeId;
33,162✔
1657
  addr.groupId = pPlan->id.groupId;
33,162✔
1658
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
33,162✔
1659
  FOREACH(pNode, pPlan->pParents) {
66,324✔
1660
    SSubplan* pSubplan = (SSubplan*)pNode;
33,162✔
1661
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
33,162✔
1662
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
33,162✔
1663
  }
1664
  
1665
_exit:
641,951✔
1666

1667
  if (code) {
652,724✔
1668
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1669
  }
1670

1671
  return code;
652,724✔
1672
}
1673

1674
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
619,562✔
1675
  int32_t code = TSDB_CODE_SUCCESS;
619,562✔
1676
  int32_t lino = 0;
619,562✔
1677
  int64_t streamId = pStream->pCreate->streamId;
619,562✔
1678
  int32_t runnerNum = taosArrayGetSize(pRunners);
619,562✔
1679
  
1680
  for (int32_t i = 0; i < runnerNum; ++i) {
1,272,286✔
1681
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
652,724✔
1682
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
652,724✔
1683
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
652,724✔
1684

1685
    SStreamTask* pTask = &pDeploy->task;
652,724✔
1686
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
652,724✔
1687
  }
1688

1689
_exit:
619,562✔
1690

1691
  if (code) {
619,562✔
1692
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1693
  }
1694

1695
  return code;
619,562✔
1696
}
1697

1698
int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream) {
206,286✔
1699
  int32_t code = 0;
206,286✔
1700
  int32_t lino = 0;
206,286✔
1701
  int64_t streamId = pStream->pCreate->streamId;
206,286✔
1702
  SArray* deployTaskList = NULL;
206,286✔
1703
  SArray* deployList = NULL;
206,286✔
1704
  int32_t deployNodeId = 0;
206,286✔
1705
  SStmTaskStatus* pState = NULL;
206,286✔
1706
  int32_t taskIdx = 0;
206,286✔
1707
  SNodeListNode *plans = NULL;
206,286✔
1708
  int32_t        taskNum = 0;
206,286✔
1709
  int32_t        totalTaskNum = 0;
206,286✔
1710

1711
  if (pDag->numOfSubplans <= 0) {
206,286✔
1712
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1713
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1714
  }
1715

1716
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
206,286✔
1717
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1718
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1719
  }
1720

1721
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
206,286✔
1722
  if (levelNum <= 0) {
206,286✔
1723
    mstsError("invalid level num:%d", levelNum);
×
1724
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1725
  }
1726

1727
  int32_t        lowestLevelIdx = levelNum - 1;
206,286✔
1728
  
1729
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
206,286✔
1730
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
206,286✔
1731
    mstsError("invalid level plan, level:0, planNodeType:%d", nodeType(plans));
×
1732
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1733
  }
1734
  
1735
  taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
206,286✔
1736
  if (taskNum != 1) {
206,286✔
1737
    mstsError("invalid level plan number:%d, level:0", taskNum);
×
1738
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1739
  }
1740

1741
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
206,286✔
1742
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
206,286✔
1743
  
1744
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
825,144✔
1745
    totalTaskNum = 0;
618,858✔
1746

1747
    deployList = pInfo->runners[deployId];
618,858✔
1748
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
618,858✔
1749
    if (!GOT_SNODE(deployNodeId)) {
618,858✔
1750
      TAOS_CHECK_EXIT(terrno);
×
1751
    }
1752

1753
    taskIdx = 0;
618,858✔
1754
    
1755
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,254,297✔
1756
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
635,439✔
1757
      if (NULL == plans) {
635,439✔
1758
        mstsError("empty level plan, level:%d", i);
×
1759
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1760
      }
1761

1762
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
635,439✔
1763
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1764
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1765
      }
1766

1767
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
635,439✔
1768
      if (taskNum <= 0) {
635,439✔
1769
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1770
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1771
      }
1772

1773
      totalTaskNum += taskNum;
635,439✔
1774
      if (totalTaskNum > pDag->numOfSubplans) {
635,439✔
1775
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1776
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1777
      }
1778

1779
      for (int32_t n = 0; n < taskNum; ++n) {
1,287,459✔
1780
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
652,020✔
1781
        pState = taosArrayReserve(deployList, 1);
652,020✔
1782

1783
        pState->id.taskId = msmAssignTaskId();
652,020✔
1784
        pState->id.deployId = deployId;
652,020✔
1785
        pState->id.seriousId = msmAssignTaskSeriousId();
652,020✔
1786
        pState->id.nodeId = deployNodeId;
652,020✔
1787
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
652,020✔
1788
        pState->type = STREAM_RUNNER_TASK;
652,020✔
1789
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
652,020✔
1790
        pState->status = STREAM_STATUS_UNDEPLOYED;
652,020✔
1791
        pState->lastUpTs = pCtx->currTs;
652,020✔
1792
        pState->pStream = pInfo;
652,020✔
1793

1794
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
652,020✔
1795
        pDeploy->task.type = pState->type;
652,020✔
1796
        pDeploy->task.streamId = streamId;
652,020✔
1797
        pDeploy->task.taskId = pState->id.taskId;
652,020✔
1798
        pDeploy->task.flags = pState->flags;
652,020✔
1799
        pDeploy->task.seriousId = pState->id.seriousId;
652,020✔
1800
        pDeploy->task.deployId = pState->id.deployId;
652,020✔
1801
        pDeploy->task.nodeId = pState->id.nodeId;
652,020✔
1802
        pDeploy->task.taskIdx = pState->id.taskIdx;
652,020✔
1803
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
652,020✔
1804

1805
        SStreamTask* pTask = &pDeploy->task;
652,020✔
1806
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
652,020✔
1807
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1808
      }
1809

1810
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
635,439✔
1811
    }
1812

1813
    if (totalTaskNum != pDag->numOfSubplans) {
618,858✔
1814
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1815
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1816
    }
1817

1818
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
618,858✔
1819

1820
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
618,858✔
1821

1822
    nodesDestroyNode((SNode *)pDag);
618,858✔
1823
    pDag = NULL;
618,858✔
1824
    
1825
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
618,858✔
1826

1827
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
618,858✔
1828
  }
1829

1830
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
825,144✔
1831
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
618,858✔
1832
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
618,858✔
1833
  }
1834
  
1835
  pInfo->runnerNum = totalTaskNum;
206,286✔
1836
  
1837
_exit:
206,286✔
1838

1839
  if (code) {
206,286✔
1840
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1841
  }
1842

1843
  taosArrayDestroy(deployTaskList);
206,286✔
1844
  nodesDestroyNode((SNode *)pDag);
206,286✔
1845

1846
  return code;
206,286✔
1847
}
1848

1849
int32_t msmReBuildRunnerTasks(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream, SStmTaskAction* pAction) {
704✔
1850
  int32_t code = 0;
704✔
1851
  int32_t lino = 0;
704✔
1852
  int64_t streamId = pStream->pCreate->streamId;
704✔
1853
  int32_t newNodeId = 0;
704✔
1854
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
704✔
1855
  int32_t        lowestLevelIdx = levelNum - 1;
704✔
1856
  SNodeListNode *plans = NULL;
704✔
1857
  int32_t        taskNum = 0;
704✔
1858
  int32_t        totalTaskNum = 0;
704✔
1859
  int32_t        deployId = 0;
704✔
1860
  SStmTaskStatus* pRunner = NULL;
704✔
1861
  SStmTaskStatus* pStartRunner = NULL;
704✔
1862
  int32_t taskIdx = 0;
704✔
1863
  SArray* deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
704✔
1864
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
704✔
1865

1866
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
1,408✔
1867
    deployId = pAction->deployId[r];
704✔
1868

1869
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
704✔
1870

1871
    pStartRunner = pRunner;
704✔
1872
    totalTaskNum = 0;
704✔
1873

1874
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
704✔
1875
    if (!GOT_SNODE(newNodeId)) {
704✔
1876
      TAOS_CHECK_EXIT(terrno);
×
1877
    }
1878

1879
    taskIdx = 0;
704✔
1880
    
1881
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,408✔
1882
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
704✔
1883
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
704✔
1884
      totalTaskNum += taskNum;
704✔
1885

1886
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
704✔
1887
      
1888
      for (int32_t n = 0; n < taskNum; ++n) {
1,408✔
1889
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
704✔
1890

1891
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
704✔
1892
        if (pRunner->id.taskIdx != newTaskIdx) {
704✔
1893
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1894
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1895
        }
1896

1897
        pRunner->id.nodeId = newNodeId;
704✔
1898

1899
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
704✔
1900
        pDeploy->task.type = pRunner->type;
704✔
1901
        pDeploy->task.streamId = streamId;
704✔
1902
        pDeploy->task.taskId = pRunner->id.taskId;
704✔
1903
        pDeploy->task.flags = pRunner->flags;
704✔
1904
        pDeploy->task.seriousId = pRunner->id.seriousId;
704✔
1905
        pDeploy->task.nodeId = pRunner->id.nodeId;
704✔
1906
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
704✔
1907
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
704✔
1908

1909
        pRunner++;
704✔
1910
      }
1911

1912
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
704✔
1913
    }
1914

1915
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
704✔
1916

1917
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
704✔
1918

1919
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
704✔
1920

1921
    nodesDestroyNode((SNode *)pDag);
704✔
1922
    pDag = NULL;
704✔
1923

1924
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
704✔
1925
  }
1926

1927
_exit:
704✔
1928

1929
  if (code) {
704✔
1930
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1931
  }
1932

1933
  nodesDestroyNode((SNode *)pDag);
704✔
1934
  taosArrayDestroy(deployTaskList);
704✔
1935

1936
  return code;
704✔
1937
}
1938

1939

1940
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
196,345✔
1941
  int32_t code = TSDB_CODE_SUCCESS;
196,345✔
1942
  int32_t lino = 0;
196,345✔
1943
  //STREAMTODO 
1944
  
1945
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
196,345✔
1946
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
196,345✔
1947

1948
_exit:
196,345✔
1949

1950
  if (code) {
196,345✔
1951
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1952
  }
1953

1954
  return code;
196,345✔
1955
}
1956

1957

1958
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
209,669✔
1959
  if (NULL == pStream->pCreate->calcPlan) {
209,669✔
1960
    return TSDB_CODE_SUCCESS;
3,383✔
1961
  }
1962
  
1963
  int32_t code = TSDB_CODE_SUCCESS;
206,286✔
1964
  int32_t lino = 0;
206,286✔
1965
  int64_t streamId = pStream->pCreate->streamId;
206,286✔
1966
  SQueryPlan* pPlan = NULL;
206,286✔
1967

1968
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
206,286✔
1969

1970
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
825,144✔
1971
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
618,858✔
1972
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
618,858✔
1973
  }
1974

1975
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
206,286✔
1976
  pPlan = NULL;
206,286✔
1977
  
1978
  TAOS_CHECK_EXIT(code);
206,286✔
1979

1980
  taosHashClear(mStreamMgmt.toUpdateScanMap);
206,286✔
1981
  mStreamMgmt.toUpdateScanNum = 0;
206,286✔
1982

1983
_exit:
206,286✔
1984

1985
  nodesDestroyNode((SNode *)pPlan);
206,286✔
1986

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

1991
  return code;
206,286✔
1992
}
1993

1994

1995
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
209,669✔
1996
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
1997
  int32_t lino = 0;
209,669✔
1998
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
1999

2000
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
209,669✔
2001

2002
  pCtx->triggerTaskId = msmAssignTaskId();
209,669✔
2003
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
209,669✔
2004
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
209,669✔
2005
    TAOS_CHECK_EXIT(terrno);
×
2006
  }
2007

2008
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
209,669✔
2009
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
209,669✔
2010
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
209,669✔
2011
  
2012
_exit:
209,669✔
2013

2014
  if (code) {
209,669✔
2015
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2016
  }
2017

2018
  return code;
209,669✔
2019
}
2020

2021
static int32_t msmInitTrigReaderList(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
×
2022
  int32_t code = TSDB_CODE_SUCCESS;
×
2023
  int32_t lino = 0;
×
2024
  int64_t streamId = pStream->pCreate->streamId;
×
2025
  SSdb   *pSdb = pCtx->pMnode->pSdb;
×
2026
  SStmTaskStatus* pState = NULL;
×
2027
  SDbObj* pDb = NULL;
×
2028
  
2029
  switch (pStream->pCreate->triggerTblType) {
×
2030
    case TSDB_NORMAL_TABLE:
×
2031
    case TSDB_CHILD_TABLE:
2032
    case TSDB_VIRTUAL_CHILD_TABLE:
2033
    case TSDB_VIRTUAL_NORMAL_TABLE: {
2034
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
×
2035
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2036
      pInfo->trigReaderNum = 1;
×
2037
      break;
×
2038
    }
2039
    case TSDB_SUPER_TABLE: {
×
2040
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
×
2041
      if (NULL == pDb) {
×
2042
        code = terrno;
×
2043
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
2044
        goto _exit;
×
2045
      }
2046

2047
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
2048
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2049
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2050
      mndReleaseDb(pCtx->pMnode, pDb);
×
2051
      pDb = NULL;
×
2052
      break;
×
2053
    }
2054
    default:
×
2055
      pInfo->trigReaderNum = 0;
×
2056
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2057
      break;
×
2058
  }
2059

2060
_exit:
×
2061

2062
  if (code) {
×
2063
    mndReleaseDb(pCtx->pMnode, pDb);
×
2064
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2065
  }
2066

2067
  return code;
×
2068
}
2069

2070

2071
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
199,728✔
2072
  int32_t code = TSDB_CODE_SUCCESS;
199,728✔
2073
  int32_t lino = 0;
199,728✔
2074
  int64_t streamId = pStream->pCreate->streamId;
199,728✔
2075

2076
  pStatus->lastActionTs = INT64_MIN;
199,728✔
2077

2078
  if (NULL == pStatus->streamName) {
199,728✔
2079
    pStatus->streamName = taosStrdup(pStream->name);
199,728✔
2080
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
199,728✔
2081
  }
2082

2083
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
199,728✔
2084
  
2085
  if (pStream->pCreate->numOfCalcSubplan > 0) {
199,728✔
2086
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
196,345✔
2087
    
2088
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
196,345✔
2089
  }
2090

2091
  if (initList) {
199,728✔
2092
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2093

2094
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
2095
    if (subPlanNum > 0) {
×
2096
      pStatus->calcReaderNum = subPlanNum;
×
2097
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2098
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2099
    }
2100

2101
    if (pStatus->runnerNum > 0) {
×
2102
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2103
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2104
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2105
      }
2106
    }
2107
  }
2108
  
2109
_exit:
199,728✔
2110

2111
  if (code) {
199,728✔
2112
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2113
  }
2114

2115
  return code;
199,728✔
2116
}
2117

2118
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
209,669✔
2119
  int32_t code = TSDB_CODE_SUCCESS;
209,669✔
2120
  int32_t lino = 0;
209,669✔
2121
  int64_t streamId = pStream->pCreate->streamId;
209,669✔
2122
  SStmStatus info = {0};
209,669✔
2123

2124
  if (NULL == pStatus) {
209,669✔
2125
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
199,728✔
2126

2127
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
199,728✔
2128

2129
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
199,728✔
2130
  }
2131
  
2132
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
209,669✔
2133

2134
  mstLogSStmStatus("stream deployed", streamId, pStatus);
209,669✔
2135

2136
_exit:
209,669✔
2137

2138
  if (code) {
209,669✔
2139
    if (NULL != pStatus) {
×
2140
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2141
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2142
    }
2143
    
2144
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2145
  }
2146

2147
  return code;
209,669✔
2148
}
2149

2150

2151
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
13,135✔
2152
  int32_t code = TSDB_CODE_SUCCESS;
13,135✔
2153
  void* pIter = NULL;
13,135✔
2154

2155
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
18,607✔
2156
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
5,472✔
2157
    (void)mstWaitLock(&pVg->lock, true);
5,472✔
2158

2159
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
5,472✔
2160
    if (atomic_load_32(&pVg->deployed) == taskNum) {
5,472✔
2161
      taosRUnLockLatch(&pVg->lock);
×
2162
      continue;
×
2163
    }
2164

2165
    for (int32_t i = 0; i < taskNum; ++i) {
33,536✔
2166
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
28,064✔
2167
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
28,064✔
2168
        continue;
25,952✔
2169
      }
2170

2171
      mstDestroySStmTaskToDeployExt(pExt);
2,112✔
2172
      pExt->deployed = true;
2,112✔
2173
    }
2174
    
2175
    taosRUnLockLatch(&pVg->lock);
5,472✔
2176
  }
2177

2178
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
24,962✔
2179
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
11,827✔
2180
    (void)mstWaitLock(&pSnode->lock, true);
11,827✔
2181

2182
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
11,827✔
2183
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
11,827✔
2184
      for (int32_t i = 0; i < taskNum; ++i) {
27,638✔
2185
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
16,867✔
2186
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
16,867✔
2187
          continue;
15,406✔
2188
        }
2189
        
2190
        mstDestroySStmTaskToDeployExt(pExt);
1,461✔
2191
        pExt->deployed = true;
1,461✔
2192
      }
2193
    }
2194

2195
    taskNum = taosArrayGetSize(pSnode->runnerList);
11,827✔
2196
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
11,827✔
2197
      for (int32_t i = 0; i < taskNum; ++i) {
56,758✔
2198
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
44,931✔
2199
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
44,931✔
2200
          continue;
41,358✔
2201
        }
2202
        
2203
        mstDestroySStmTaskToDeployExt(pExt);
3,573✔
2204
        pExt->deployed = true;
3,573✔
2205
      }
2206
    }
2207

2208
    taosRUnLockLatch(&pSnode->lock);
11,827✔
2209
  }
2210

2211
  
2212
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
52,061✔
2213
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
38,926✔
2214
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
38,926✔
2215
    if (TSDB_CODE_SUCCESS == code) {
38,926✔
2216
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
17,518✔
2217
    }
2218
  }
2219

2220
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
48,349✔
2221
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
35,214✔
2222
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
35,214✔
2223
    if (TSDB_CODE_SUCCESS == code) {
35,214✔
2224
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
17,848✔
2225
    }
2226
  }
2227

2228
  size_t keyLen = 0;
13,135✔
2229
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
747,424✔
2230
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
734,289✔
2231
    if (*pStreamId == streamId) {
734,289✔
2232
      int64_t taskId = *(pStreamId + 1);
82,025✔
2233
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
82,025✔
2234
      if (code) {
82,025✔
2235
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2236
      } else {
2237
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
82,025✔
2238
      }
2239
    }
2240
  }
2241

2242
  if (fromStreamMap) {
13,135✔
2243
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,194✔
2244
    if (code) {
3,194✔
2245
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2246
    } else {
2247
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,194✔
2248
    }
2249
  }
2250
  
2251
  return code;
13,135✔
2252
}
2253

2254
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
9,941✔
2255
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
9,941✔
2256
  
2257
  (void)msmSTRemoveStream(streamId, false);  
9,941✔
2258

2259
  mstResetSStmStatus(pStatus);
9,941✔
2260

2261
  pStatus->deployTimes++;
9,941✔
2262
}
9,941✔
2263

2264
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
211,086✔
2265
  int32_t code = TSDB_CODE_SUCCESS;
211,086✔
2266
  int32_t lino = 0;
211,086✔
2267
  int64_t streamId = pAction->streamId;
211,086✔
2268
  char* streamName = pAction->streamName;
211,086✔
2269
  SStreamObj* pStream = NULL;
211,086✔
2270
  int8_t stopped = 0;
211,086✔
2271

2272
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
211,086✔
2273
  if (pStatus) {
211,086✔
2274
    stopped = atomic_load_8(&pStatus->stopped);
9,941✔
2275
    if (0 == stopped) {
9,941✔
2276
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
1,056✔
2277
      msmResetStreamForRedeploy(streamId, pStatus);
1,056✔
2278
    } else {
2279
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
8,885✔
2280
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2281
        return code;
×
2282
      }
2283
      
2284
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
8,885✔
2285
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
8,885✔
2286
        msmResetStreamForRedeploy(streamId, pStatus);
8,885✔
2287
      }
2288
    }
2289
  }
2290

2291
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
211,086✔
2292
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
211,086✔
2293
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
1,417✔
2294
    return TSDB_CODE_SUCCESS;
1,417✔
2295
  }
2296

2297
  TAOS_CHECK_EXIT(code);
209,669✔
2298

2299
  if (pStatus && pStream->pCreate->streamId != streamId) {
209,669✔
2300
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2301
    atomic_store_8(&pStatus->stopped, 2);
×
2302
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2303
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2304
  }
2305

2306
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
209,669✔
2307
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
209,669✔
2308
  if (userStopped || userDropped) {
209,669✔
2309
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
×
2310
    goto _exit;
×
2311
  }
2312
  
2313
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
209,669✔
2314

2315
_exit:
209,669✔
2316

2317
  mndReleaseStream(pCtx->pMnode, pStream);
209,669✔
2318

2319
  if (code) {
209,669✔
2320
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2321
  }
2322

2323
  return code;
209,669✔
2324
}
2325

2326
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2,112✔
2327
  int32_t code = TSDB_CODE_SUCCESS;
2,112✔
2328
  int32_t lino = 0;
2,112✔
2329
  int64_t streamId = pAction->streamId;
2,112✔
2330
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2,112✔
2331
  if (NULL == ppTask) {
2,112✔
2332
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
×
2333
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2334
  }
2335
  
2336
  SStmTaskDeploy info = {0};
2,112✔
2337
  info.task.type = pAction->type;
2,112✔
2338
  info.task.streamId = pAction->streamId;
2,112✔
2339
  info.task.taskId = pAction->id.taskId;
2,112✔
2340
  info.task.seriousId = (*ppTask)->id.seriousId;
2,112✔
2341
  info.task.nodeId = pAction->id.nodeId;
2,112✔
2342
  info.task.taskIdx = pAction->id.taskIdx;
2,112✔
2343
  
2344
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
2,112✔
2345
  SStreamCalcScan* scanPlan = NULL;
2,112✔
2346
  if (!isTriggerReader) {
2,112✔
2347
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
1,056✔
2348
    if (NULL == scanPlan) {
1,056✔
2349
      mstsError("fail to get TASK:%" PRId64 " scanPlan, taskIdx:%d, scanPlanNum:%zu", 
×
2350
          pAction->id.taskId, pAction->id.taskIdx, taosArrayGetSize(pStatus->pCreate->calcScanPlanList));
2351
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2352
    }
2353
  }
2354
  
2355
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, scanPlan ? scanPlan->scanPlan : NULL, pStatus, isTriggerReader));
2,112✔
2356
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
2,112✔
2357

2358
_exit:
2,112✔
2359

2360
  if (code) {
2,112✔
2361
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2362
  }
2363

2364
  return code;
2,112✔
2365
}
2366

2367
/*
2368
static int32_t msmReLaunchTriggerTask(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2369
  int32_t code = TSDB_CODE_SUCCESS;
2370
  int32_t lino = 0;
2371
  int64_t streamId = pAction->streamId;
2372
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2373
  if (NULL == ppTask) {
2374
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
2375
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
2376
  }
2377
  
2378
  (*ppTask)->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2379
  if (!GOT_SNODE((*ppTask)->id.nodeId)) {
2380
    mstsError("no avaible snode for deploying trigger task, seriousId: %" PRId64, (*ppTask)->id.seriousId);
2381
    return TSDB_CODE_SUCCESS;
2382
  }
2383
  
2384
  SStmTaskDeploy info = {0};
2385
  info.task.type = pAction->type;
2386
  info.task.streamId = streamId;
2387
  info.task.taskId = pAction->id.taskId;
2388
  info.task.seriousId = (*ppTask)->id.seriousId;
2389
  info.task.nodeId = (*ppTask)->id.nodeId;
2390
  info.task.taskIdx = pAction->id.taskIdx;
2391
  
2392
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2393
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2394
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, *ppTask, 1, -1));
2395
  
2396
  atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2397

2398
_exit:
2399

2400
  if (code) {
2401
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2402
  }
2403

2404
  return code;
2405
}
2406
*/
2407

2408
static int32_t msmReLaunchRunnerDeploy(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
704✔
2409
  int32_t code = TSDB_CODE_SUCCESS;
704✔
2410
  int32_t lino = 0;
704✔
2411
  int64_t streamId = pAction->streamId;
704✔
2412
  
2413
/*
2414
  if (pAction->triggerStatus) {
2415
    pCtx->triggerTaskId = pAction->triggerStatus->id.taskId;
2416
    pAction->triggerStatus->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2417
    if (!GOT_SNODE(pAction->triggerStatus->id.nodeId)) {
2418
      mstsError("no avaible snode for deploying trigger task, seriousId:%" PRId64, pAction->triggerStatus->id.seriousId);
2419
      return TSDB_CODE_SUCCESS;
2420
    }
2421
  
2422
    pCtx->triggerNodeId = pAction->triggerStatus->id.nodeId;
2423
  } else {
2424
*/
2425
  pCtx->triggerTaskId = pStatus->triggerTask->id.taskId;
704✔
2426
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
704✔
2427
//  }
2428
  
2429
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
704✔
2430
  
2431
  SQueryPlan* pPlan = NULL;
704✔
2432
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
704✔
2433
  
2434
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
704✔
2435
  
2436
  taosHashClear(mStreamMgmt.toUpdateScanMap);
704✔
2437
  mStreamMgmt.toUpdateScanNum = 0;
704✔
2438
  
2439
/*
2440
  if (pAction->triggerStatus) {
2441
    SStmTaskDeploy info = {0};
2442
    info.task.type = STREAM_TRIGGER_TASK;
2443
    info.task.streamId = streamId;
2444
    info.task.taskId = pCtx->triggerTaskId;
2445
    info.task.seriousId = pAction->triggerStatus->id.seriousId;
2446
    info.task.nodeId = pCtx->triggerNodeId;
2447
    info.task.taskIdx = 0;
2448
  
2449
    TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2450
    TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2451
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pAction->triggerStatus, 1, -1));
2452
    
2453
    atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2454
  }
2455
*/
2456

2457
_exit:
704✔
2458

2459
  if (code) {
704✔
2460
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2461
  }
2462

2463
  return code;
704✔
2464
}
2465

2466

2467
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
3,520✔
2468
  int32_t code = TSDB_CODE_SUCCESS;
3,520✔
2469
  int32_t lino = 0;
3,520✔
2470
  int64_t streamId = pAction->streamId;
3,520✔
2471
  int64_t taskId = pAction->id.taskId;
3,520✔
2472
  SStreamObj* pStream = NULL;
3,520✔
2473

2474
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
3,520✔
2475

2476
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
3,520✔
2477
  if (NULL == pStatus) {
3,520✔
2478
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2479
    return TSDB_CODE_SUCCESS;
×
2480
  }
2481

2482
  int8_t stopped = atomic_load_8(&pStatus->stopped);
3,520✔
2483
  if (stopped) {
3,520✔
2484
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
704✔
2485
    return TSDB_CODE_SUCCESS;
704✔
2486
  }
2487

2488
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
2,816✔
2489
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
2,816✔
2490
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2491
    return TSDB_CODE_SUCCESS;
×
2492
  }
2493

2494
  TAOS_CHECK_EXIT(code);
2,816✔
2495

2496
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
2,816✔
2497
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
2,816✔
2498
  if (userStopped || userDropped) {
2,816✔
2499
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2500
    goto _exit;
×
2501
  }
2502

2503
  switch (pAction->type) {
2,816✔
2504
    case STREAM_READER_TASK:
2,112✔
2505
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
2,112✔
2506
      break;
2,112✔
2507
/*
2508
    case STREAM_TRIGGER_TASK:
2509
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2510
      break;
2511
*/
2512
    case STREAM_RUNNER_TASK:
704✔
2513
      if (pAction->multiRunner) {
704✔
2514
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
704✔
2515
      } else {
2516
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
2517
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2518
      }
2519
      break;
704✔
2520
    default:
×
2521
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2522
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2523
      break;
×
2524
  }
2525

2526
_exit:
2,816✔
2527

2528
  if (pStream) {
2,816✔
2529
    mndReleaseStream(pCtx->pMnode, pStream);
2,816✔
2530
  }
2531

2532
  if (code) {
2,816✔
2533
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2534
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2535
  }
2536

2537
  return code;
2,816✔
2538
}
2539

2540
static int32_t msmTDRemoveStream(int64_t streamId) {
×
2541
  void* pIter = NULL;
×
2542
  
2543
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
×
2544
    while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
×
2545
      SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
×
2546
      int32_t taskNum = taosArrayGetSize(pVg->taskList);
×
2547
      if (atomic_load_32(&pVg->deployed) == taskNum) {
×
2548
        continue;
×
2549
      }
2550
      
2551
      for (int32_t i = 0; i < taskNum; ++i) {
×
2552
        SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
×
2553
        if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2554
          pExt->deployed = true;
×
2555
        }
2556
      }
2557
    }
2558
  }
2559

2560
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
2561
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2562
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2563
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2564
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2565
        for (int32_t i = 0; i < taskNum; ++i) {
×
2566
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2567
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2568
            pExt->deployed = true;
×
2569
          }
2570
        }
2571
      }
2572

2573
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
2574
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2575
        for (int32_t i = 0; i < taskNum; ++i) {
×
2576
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2577
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2578
            pExt->deployed = true;
×
2579
          }
2580
        }
2581
      }
2582
    }
2583
  }
2584

2585
  return TSDB_CODE_SUCCESS;
×
2586
}
2587

2588
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
3,194✔
2589
  int32_t code = TSDB_CODE_SUCCESS;
3,194✔
2590
  int32_t lino = 0;
3,194✔
2591

2592
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,194✔
2593

2594
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
3,194✔
2595

2596
_exit:
3,194✔
2597

2598
  if (code) {
3,194✔
2599
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2600
  } else {
2601
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,194✔
2602
  }
2603

2604
  return code;
3,194✔
2605
}
2606

2607
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
16,115✔
2608
  int32_t code = TSDB_CODE_SUCCESS;
16,115✔
2609
  int32_t lino = 0;
16,115✔
2610

2611
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
16,115✔
2612
  if (0 == active || MND_STM_STATE_NORMAL != state) {
16,115✔
2613
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2614
    return;
×
2615
  }
2616

2617
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
16,115✔
2618
  if (NULL == pStream) {
16,115✔
2619
    mstsInfo("stream %s already not in streamMap", streamName);
366✔
2620
    goto _exit;
366✔
2621
  }
2622

2623
  atomic_store_8(&pStream->stopped, 2);
15,749✔
2624

2625
  mstsInfo("set stream %s stopped by user", streamName);
15,749✔
2626

2627
_exit:
×
2628

2629
  taosHashRelease(mStreamMgmt.streamMap, pStream);
16,115✔
2630

2631
  if (code) {
16,115✔
2632
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2633
  }
2634

2635
  return;
16,115✔
2636
}
2637

2638
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
6,539✔
2639
  int32_t code = TSDB_CODE_SUCCESS;
6,539✔
2640
  int32_t lino = 0;
6,539✔
2641

2642
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
6,539✔
2643
  if (0 == active || MND_STM_STATE_NORMAL != state) {
6,539✔
2644
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2645
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2646
  }
2647

2648
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
6,539✔
2649
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
6,539✔
2650
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2651
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2652
    goto _exit;
×
2653
  }
2654

2655
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
6,539✔
2656

2657
_exit:
6,539✔
2658

2659
  taosHashRelease(mStreamMgmt.streamMap, pStream);
6,539✔
2660

2661
  if (code) {
6,539✔
2662
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2663
  }
2664

2665
  return code;
6,539✔
2666
}
2667

2668
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
53,496✔
2669
  int32_t code = TSDB_CODE_SUCCESS;
53,496✔
2670
  int32_t lino = 0;
53,496✔
2671
  SStmQNode* pQNode = NULL;
53,496✔
2672

2673
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
268,102✔
2674
    switch (pQNode->type) {
214,606✔
2675
      case STREAM_ACT_DEPLOY:
214,606✔
2676
        if (pQNode->streamAct) {
214,606✔
2677
          mstDebug("start to handle stream deploy action");
211,086✔
2678
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
211,086✔
2679
        } else {
2680
          mstDebug("start to handle task deploy action");
3,520✔
2681
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
3,520✔
2682
        }
2683
        break;
214,606✔
2684
      default:
×
2685
        break;
×
2686
    }
2687
  }
2688

2689
_exit:
53,496✔
2690

2691
  if (code) {
53,496✔
2692
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2693
  }
2694
}
53,496✔
2695

2696
void msmStopAllStreamsByGrant(int32_t errCode) {
×
2697
  SStmStatus* pStatus = NULL;
×
2698
  void* pIter = NULL;
×
2699
  int64_t streamId = 0;
×
2700
  
2701
  while (true) {
2702
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
2703
    if (NULL == pIter) {
×
2704
      break;
×
2705
    }
2706

2707
    pStatus = (SStmStatus*)pIter;
×
2708

2709
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2710
    atomic_store_8(&pStatus->stopped, 4);
×
2711

2712
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2713
  }
2714
}
×
2715

2716
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2717
  mstInfo("stream grant expired");
×
2718

2719
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2720
    mstWarn("mnode stream is NOT active, ignore handling");
×
2721
    return errCode;
×
2722
  }
2723

2724
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2725

2726
  msmStopAllStreamsByGrant(errCode);
×
2727

2728
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2729
  
2730
  return errCode;
×
2731
}
2732

2733
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,465,364✔
2734
  int32_t code = TSDB_CODE_SUCCESS;
1,465,364✔
2735
  int32_t lino = 0;
1,465,364✔
2736
  int64_t streamId = pDeploy->task.streamId;
1,465,364✔
2737
  
2738
  switch (pDeploy->task.type) {
1,465,364✔
2739
    case STREAM_READER_TASK:
608,005✔
2740
      if (NULL == pStream->readerTasks) {
608,005✔
2741
        pStream->streamId = streamId;
270,006✔
2742
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
270,006✔
2743
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
270,006✔
2744
      }
2745
      
2746
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,216,010✔
2747
      break;
608,005✔
2748
    case STREAM_TRIGGER_TASK:
208,208✔
2749
      pStream->streamId = streamId;
208,208✔
2750
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
208,208✔
2751
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
208,208✔
2752
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
208,208✔
2753
      break;
208,208✔
2754
    case STREAM_RUNNER_TASK:
649,151✔
2755
      if (NULL == pStream->runnerTasks) {
649,151✔
2756
        pStream->streamId = streamId;
215,370✔
2757
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
215,370✔
2758
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
215,370✔
2759
      }      
2760
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,298,302✔
2761
      break;
649,151✔
2762
    default:
×
2763
      break;
×
2764
  }
2765

2766
_exit:
1,465,364✔
2767

2768
  if (code) {
1,465,364✔
2769
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2770
  }
2771

2772
  return code;
1,465,364✔
2773
}
2774

2775
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,465,364✔
2776
  int32_t code = TSDB_CODE_SUCCESS;
1,465,364✔
2777
  int32_t lino = 0;
1,465,364✔
2778
  int64_t streamId = pDeploy->task.streamId;
1,465,364✔
2779
  SStreamTask* pTask = &pDeploy->task;
1,465,364✔
2780
  SStmStreamDeploy streamDeploy = {0};
1,465,364✔
2781
  SStmStreamDeploy* pStream = NULL;
1,465,364✔
2782
   
2783
  while (true) {
2784
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,465,364✔
2785
    if (NULL == pStream) {
1,465,364✔
2786
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
284,283✔
2787
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
284,283✔
2788
      if (TSDB_CODE_SUCCESS == code) {
284,283✔
2789
        goto _exit;
284,283✔
2790
      }
2791

2792
      if (TSDB_CODE_DUP_KEY != code) {
×
2793
        goto _exit;
×
2794
      }    
2795

2796
      tFreeSStmStreamDeploy(&streamDeploy);
×
2797
      continue;
×
2798
    }
2799

2800
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,181,081✔
2801
    
2802
    break;
1,181,081✔
2803
  }
2804
  
2805
_exit:
1,465,364✔
2806

2807
  taosHashRelease(pHash, pStream);
1,465,364✔
2808

2809
  if (code) {
1,465,364✔
2810
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2811
  } else {
2812
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,465,364✔
2813
  }
2814

2815
  return code;
1,465,364✔
2816
}
2817

2818

2819
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
248,803✔
2820
  int32_t code = TSDB_CODE_SUCCESS;
248,803✔
2821
  int32_t lino = 0;
248,803✔
2822
  int32_t taskNum = taosArrayGetSize(pTasks);
248,803✔
2823

2824
  for (int32_t i = 0; i < taskNum; ++i) {
1,720,503✔
2825
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,471,700✔
2826
    if (pExt->deployed) {
1,471,700✔
2827
      continue;
6,336✔
2828
    }
2829

2830
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,465,364✔
2831
    pExt->deployed = true;
1,465,364✔
2832

2833
    (void)atomic_add_fetch_32(deployed, 1);
1,465,364✔
2834
  }
2835

2836
_exit:
248,803✔
2837

2838
  if (code) {
248,803✔
2839
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2840
  }
2841

2842
  return code;
248,803✔
2843
}
2844

2845
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
70,660✔
2846
  int32_t code = TSDB_CODE_SUCCESS;
70,660✔
2847
  int32_t lino = 0;
70,660✔
2848
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
70,660✔
2849
  SStmVgTasksToDeploy* pVg = NULL;
70,660✔
2850
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2851

2852
  mstDebug("start to add stream vgroup tasks deploy");
70,660✔
2853
  
2854
  for (int32_t i = 0; i < vgNum; ++i) {
453,353✔
2855
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
382,693✔
2856

2857
    msmUpdateVgroupUpTs(pCtx, *vgId);
382,693✔
2858

2859
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
382,693✔
2860
    if (NULL == pVg) {
382,693✔
2861
      continue;
246,392✔
2862
    }
2863

2864
    if (taosRTryLockLatch(&pVg->lock)) {
136,301✔
2865
      continue;
×
2866
    }
2867
    
2868
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
136,301✔
2869
      taosRUnLockLatch(&pVg->lock);
×
2870
      continue;
×
2871
    }
2872
    
2873
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
136,301✔
2874
    taosRUnLockLatch(&pVg->lock);
136,301✔
2875
  }
2876

2877
_exit:
70,660✔
2878

2879
  if (code) {
70,660✔
2880
    if (pVg) {
×
2881
      taosRUnLockLatch(&pVg->lock);
×
2882
    }
2883

2884
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2885
  }
2886

2887
  return code;
70,660✔
2888
}
2889

2890
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
103,863✔
2891
  int32_t code = TSDB_CODE_SUCCESS;
103,863✔
2892
  int32_t lino = 0;
103,863✔
2893
  SStmSnodeTasksDeploy* pSnode = NULL;
103,863✔
2894
  SStreamHbMsg* pReq = pCtx->pReq;
103,863✔
2895

2896
  mstDebug("start to add stream snode tasks deploy");
103,863✔
2897
  
2898
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
103,863✔
2899
  if (NULL == pSnode) {
103,863✔
2900
    return TSDB_CODE_SUCCESS;
45,693✔
2901
  }
2902

2903
  (void)mstWaitLock(&pSnode->lock, false);
58,170✔
2904
  
2905
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
58,170✔
2906
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
54,332✔
2907
  }
2908

2909
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
58,170✔
2910
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
58,170✔
2911
  }
2912
  
2913
  taosWUnLockLatch(&pSnode->lock);
58,170✔
2914

2915
_exit:
58,170✔
2916

2917
  if (code) {
58,170✔
2918
    if (pSnode) {
×
2919
      taosWUnLockLatch(&pSnode->lock);
×
2920
    }
2921

2922
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2923
  }
2924

2925
  return code;
58,170✔
2926
}
2927

2928
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
647,986✔
2929
  int32_t code = TSDB_CODE_SUCCESS;
647,986✔
2930
  int32_t lino = 0;
647,986✔
2931
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
647,986✔
2932
  if (NULL == pStatus) {
647,986✔
2933
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2934
    return TSDB_CODE_SUCCESS;
×
2935
  }
2936
  
2937
  pStatus->lastActionTs = currTs;
647,986✔
2938

2939
_exit:
647,986✔
2940

2941
  if (code) {
647,986✔
2942
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2943
  }
2944

2945
  return code;
647,986✔
2946
}
2947

2948
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
73,582✔
2949
  int32_t code = TSDB_CODE_SUCCESS;
73,582✔
2950
  int32_t lino = 0;
73,582✔
2951
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
73,582✔
2952
  void* pIter = NULL;
73,582✔
2953

2954
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
73,582✔
2955
  
2956
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
73,582✔
2957
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
73,582✔
2958

2959
  while (1) {
284,283✔
2960
    pIter = taosHashIterate(pCtx->deployStm, pIter);
357,865✔
2961
    if (pIter == NULL) {
357,865✔
2962
      break;
73,582✔
2963
    }
2964
    
2965
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
284,283✔
2966
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
568,566✔
2967

2968
    int64_t streamId = pDeploy->streamId;
284,283✔
2969
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
284,283✔
2970
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
2971

2972
    mstClearSStmStreamDeploy(pDeploy);
284,283✔
2973
    
2974
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
284,283✔
2975
  }
2976
  
2977
_exit:
73,582✔
2978

2979
  if (pIter) {
73,582✔
2980
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2981
  }
2982

2983
  if (code) {
73,582✔
2984
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2985
  }
2986
  
2987
  return code;
73,582✔
2988
}
2989

2990
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
70,660✔
2991
  int32_t code = TSDB_CODE_SUCCESS;
70,660✔
2992
  int32_t lino = 0;
70,660✔
2993
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
70,660✔
2994
  SStmVgTasksToDeploy* pVg = NULL;
70,660✔
2995
  
2996
  for (int32_t i = 0; i < vgNum; ++i) {
453,353✔
2997
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
382,693✔
2998
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
382,693✔
2999
    if (NULL == pVg) {
382,693✔
3000
      continue;
246,392✔
3001
    }
3002

3003
    if (taosWTryLockLatch(&pVg->lock)) {
136,301✔
3004
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3005
      continue;
×
3006
    }
3007
    
3008
    if (atomic_load_32(&pVg->deployed) <= 0) {
136,301✔
3009
      taosWUnLockLatch(&pVg->lock);
×
3010
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3011
      continue;
×
3012
    }
3013

3014
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
136,301✔
3015
    if (atomic_load_32(&pVg->deployed) == taskNum) {
136,301✔
3016
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
135,949✔
3017
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
135,949✔
3018
      pVg->taskList = NULL;
135,949✔
3019
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
135,949✔
3020
      taosWUnLockLatch(&pVg->lock);
135,949✔
3021
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
135,949✔
3022
      continue;
135,949✔
3023
    }
3024

3025
    for (int32_t m = taskNum - 1; m >= 0; --m) {
4,576✔
3026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
4,224✔
3027
      if (!pExt->deployed) {
4,224✔
UNCOV
3028
        continue;
×
3029
      }
3030

3031
      mstDestroySStmTaskToDeployExt(pExt);
4,224✔
3032

3033
      taosArrayRemove(pVg->taskList, m);
4,224✔
3034
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
4,224✔
3035
    }
3036
    atomic_store_32(&pVg->deployed, 0);
352✔
3037
    taosWUnLockLatch(&pVg->lock);
352✔
3038
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
352✔
3039
  }
3040

3041
_exit:
70,660✔
3042

3043
  if (code) {
70,660✔
3044
    if (pVg) {
×
3045
      taosWUnLockLatch(&pVg->lock);
×
3046
    }
3047

3048
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3049
  }
3050
}
70,660✔
3051

3052
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
133,057✔
3053
  if (!GOT_SNODE(snodeId)) {
133,057✔
3054
    return;
29,194✔
3055
  }
3056
  
3057
  int32_t code = TSDB_CODE_SUCCESS;
103,863✔
3058
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
103,863✔
3059
  if (NULL == pSnode) {
103,863✔
3060
    return;
45,693✔
3061
  }
3062

3063
  if (taosWTryLockLatch(&pSnode->lock)) {
58,170✔
3064
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3065
    return;
×
3066
  }
3067

3068
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
58,170✔
3069
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
58,170✔
3070
  
3071
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
58,170✔
3072
    taosWUnLockLatch(&pSnode->lock);
×
3073
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3074
    return;
×
3075
  }
3076

3077
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
58,170✔
3078
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
57,466✔
3079
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
57,466✔
3080
    pSnode->triggerList = NULL;
57,466✔
3081
  }
3082

3083
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
58,170✔
3084
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
57,466✔
3085
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
57,466✔
3086
    pSnode->runnerList = NULL;
57,466✔
3087
  }
3088

3089
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
58,170✔
3090
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
57,466✔
3091
    taosWUnLockLatch(&pSnode->lock);
57,466✔
3092
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
57,466✔
3093
    return;
57,466✔
3094
  }
3095

3096
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
704✔
3097
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
2,816✔
3098
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
2,112✔
3099
      if (!pExt->deployed) {
2,112✔
UNCOV
3100
        continue;
×
3101
      }
3102

3103
      mstDestroySStmTaskToDeployExt(pExt);
2,112✔
3104
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2,112✔
3105
      taosArrayRemove(pSnode->triggerList, m);
2,112✔
3106
    }
3107
    
3108
    pSnode->triggerDeployed = 0;
704✔
3109
  }
3110

3111
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
704✔
3112
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
7,040✔
3113
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
6,336✔
3114
      if (!pExt->deployed) {
6,336✔
UNCOV
3115
        continue;
×
3116
      }
3117

3118
      mstDestroySStmTaskToDeployExt(pExt);
6,336✔
3119
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
6,336✔
3120
      taosArrayRemove(pSnode->runnerList, m);
6,336✔
3121
    }
3122
    
3123
    pSnode->runnerDeployed = 0;
704✔
3124
  }
3125
  
3126
  taosWUnLockLatch(&pSnode->lock);
704✔
3127
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
704✔
3128
}
3129

3130
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
15,117,096✔
3131
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
15,117,096✔
3132
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
70,660✔
3133
  }
3134

3135
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
15,117,448✔
3136
    msmCleanDeployedSnodeTasks(pHb->snodeId);
133,057✔
3137
  }
3138
}
15,117,448✔
3139

3140
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
15,117,096✔
3141
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
15,117,096✔
3142
  if (mStreamMgmt.tCtx) {
15,117,096✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
14,949,465✔
3144
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
14,949,465✔
3145
  }
3146
}
15,117,448✔
3147

3148
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
279,004✔
3149
  int32_t code = TSDB_CODE_SUCCESS;
279,004✔
3150
  int32_t lino = 0;
279,004✔
3151
  int32_t action = STREAM_ACT_START;
279,004✔
3152
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
279,004✔
3153
  if (pAction) {
279,004✔
3154
    pAction->actions |= action;
×
3155
    pAction->start.triggerId = *pId;
×
3156
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3157
  } else {
3158
    SStmAction newAction = {0};
279,004✔
3159
    newAction.actions = action;
279,004✔
3160
    newAction.start.triggerId = *pId;
279,004✔
3161
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
279,004✔
3162
    mstsDebug("stream add START action, actions:%x", newAction.actions);
279,004✔
3163
  }
3164

3165
_exit:
279,004✔
3166

3167
  if (code) {
279,004✔
3168
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3169
  }
3170

3171
  return code;
279,004✔
3172
}
3173

3174
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
3175
  int32_t code = TSDB_CODE_SUCCESS;
×
3176
  int32_t lino = 0;
×
3177
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3178
  
3179
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
3180
  if (pAction) {
×
3181
    pAction->actions |= action;
×
3182
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3183
  } else {
3184
    SStmAction newAction = {0};
×
3185
    newAction.actions = action;
×
3186
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3187
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3188
  }
3189

3190
_exit:
×
3191

3192
  if (code) {
×
3193
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3194
  }
3195

3196
  return code;
×
3197
}
3198

3199

3200

3201
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
84,699✔
3202
  int32_t code = TSDB_CODE_SUCCESS;
84,699✔
3203
  int32_t lino = 0;
84,699✔
3204
  int32_t action = STREAM_ACT_UNDEPLOY;
84,699✔
3205
  bool    dropped = false;
84,699✔
3206

3207
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
84,699✔
3208
  mstsDebug("stream dropped: %d", dropped);
84,699✔
3209
  
3210
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
84,699✔
3211
  if (pAction) {
84,699✔
3212
    pAction->actions |= action;
55,785✔
3213
    if (NULL == pAction->undeploy.taskList) {
55,785✔
3214
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3215
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3216
    }
3217

3218
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
111,570✔
3219
    if (pAction->undeploy.doCheckpoint) {
55,785✔
3220
      pAction->undeploy.doCheckpoint = dropped ? false : true;
37,321✔
3221
    }
3222
    if (!pAction->undeploy.doCleanup) {
55,785✔
3223
      pAction->undeploy.doCleanup = dropped ? true : false;
37,321✔
3224
    }
3225
    
3226
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
55,785✔
3227
  } else {
3228
    SStmAction newAction = {0};
28,914✔
3229
    newAction.actions = action;
28,914✔
3230
    newAction.undeploy.doCheckpoint = dropped ? false : true;
28,914✔
3231
    newAction.undeploy.doCleanup = dropped ? true : false;
28,914✔
3232
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
28,914✔
3233
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
28,914✔
3234
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
57,828✔
3235
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
28,914✔
3236
    
3237
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
28,914✔
3238
  }
3239

3240
_exit:
84,699✔
3241

3242
  if (code) {
84,699✔
3243
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3244
  }
3245

3246
  return code;
84,699✔
3247
}
3248

3249
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
6,112✔
3250
  int32_t code = TSDB_CODE_SUCCESS;
6,112✔
3251
  int32_t lino = 0;
6,112✔
3252
  int32_t action = STREAM_ACT_RECALC;
6,112✔
3253
  SStmAction newAction = {0};
6,112✔
3254
  
3255
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
6,112✔
3256
  if (pAction) {
6,112✔
3257
    pAction->actions |= action;
×
3258
    pAction->recalc.recalcList = recalcList;
×
3259

3260
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3261
  } else {
3262
    newAction.actions = action;
6,112✔
3263
    newAction.recalc.recalcList = recalcList;
6,112✔
3264
    
3265
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
6,112✔
3266
    
3267
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
6,112✔
3268
  }
3269

3270
_exit:
6,112✔
3271

3272
  if (code) {
6,112✔
3273
    mstDestroySStmAction(&newAction);
×
3274
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3275
  }
3276

3277
  return code;
6,112✔
3278
}
3279

3280
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
366,498✔
3281
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
366,498✔
3282
  if (NULL == pStream) {
366,498✔
3283
    return false;
×
3284
  }
3285

3286
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
366,498✔
3287
    return false;
×
3288
  }
3289

3290
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
366,498✔
3291
  for (int32_t i = 0; i < readerNum; ++i) {
806,272✔
3292
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
439,774✔
3293
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
439,774✔
3294
      return false;
×
3295
    }
3296
  }
3297

3298
  readerNum = taosArrayGetSize(pStream->trigOReaders);
366,498✔
3299
  for (int32_t i = 0; i < readerNum; ++i) {
430,793✔
3300
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
64,295✔
3301
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
64,295✔
3302
      return false;
×
3303
    }
3304
  }
3305

3306
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
366,498✔
3307
  SListNode* pNode = listHead(pStream->calcReaders);
366,498✔
3308
  for (int32_t i = 0; i < readerNum; ++i) {
994,583✔
3309
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
628,085✔
3310
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
628,085✔
3311
      return false;
×
3312
    }
3313
    pNode = TD_DLIST_NODE_NEXT(pNode);
628,085✔
3314
  }
3315

3316
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,195,673✔
3317
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
916,669✔
3318
    for (int32_t m = 0; m < runnerNum; ++m) {
1,779,006✔
3319
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
949,831✔
3320
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
949,831✔
3321
        return false;
87,494✔
3322
      }
3323
    }
3324
  }
3325
  
3326
  return true;
279,004✔
3327
}
3328

3329

3330
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
16,693,343✔
3331
  int32_t code = TSDB_CODE_SUCCESS;
16,693,343✔
3332
  int32_t lino = 0;
16,693,343✔
3333
  int32_t action = 0;
16,693,343✔
3334
  int64_t streamId = pMsg->streamId;
16,693,343✔
3335
  SStreamTask* pTask = (SStreamTask*)pMsg;
16,693,343✔
3336
  int8_t  stopped = 0;
16,693,343✔
3337

3338
  msttDebug("start to handle task abnormal status %d", pTask->status);
16,693,343✔
3339
  
3340
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
16,693,343✔
3341
  if (NULL == pStatus) {
16,693,343✔
3342
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3343
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3344
    return;
93,412✔
3345
  }
3346

3347
  stopped = atomic_load_8(&pStatus->stopped);
16,693,343✔
3348
  if (stopped) {
16,693,343✔
3349
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3350
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3351
    return;
×
3352
  }
3353
  
3354
  switch (pMsg->status) {
16,693,343✔
3355
    case STREAM_STATUS_INIT:      
16,684,554✔
3356
      if (STREAM_TRIGGER_TASK != pMsg->type) {
16,684,554✔
3357
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
16,138,141✔
3358
        return;
16,138,141✔
3359
      }
3360
      
3361
      if (INT64_MIN == pStatus->lastActionTs) {
546,413✔
3362
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3363
        return;
×
3364
      }
3365
      
3366
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
546,413✔
3367
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
179,915✔
3368
        return;
179,915✔
3369
      }
3370

3371
      if (STREAM_IS_RUNNING(pStatus)) {
366,498✔
3372
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3373
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
366,498✔
3374
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
279,004✔
3375
      }
3376
      break;
366,498✔
3377
    case STREAM_STATUS_FAILED:
8,789✔
3378
      //STREAMTODO ADD ERRCODE HANDLE
3379
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
8,789✔
3380
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
7,297✔
3381
                 pMsg->taskIdx);
3382
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
7,297✔
3383
      } else {
3384
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
1,492✔
3385
                 pMsg->taskIdx);
3386
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
1,492✔
3387
      }
3388
      break;
8,789✔
3389
    default:
×
3390
      break;
×
3391
  }
3392

3393
_exit:
375,287✔
3394

3395
  if (code) {
375,287✔
3396
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3397
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3398
  }
3399
}
3400

3401
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
83,207✔
3402
  int32_t code = TSDB_CODE_SUCCESS;
83,207✔
3403
  int32_t lino = 0;
83,207✔
3404
  SStreamTask* pTask = (SStreamTask*)pStatus;
83,207✔
3405
  int64_t streamId = pStatus->streamId;
83,207✔
3406

3407
  msttInfo("start to handle task error, type: %d", err);
83,207✔
3408

3409
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
83,207✔
3410

3411
_exit:
83,207✔
3412

3413
  if (code) {
83,207✔
3414
    // IGNORE STOP STREAM BY ERROR  
3415
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3416
  }
3417
}
83,207✔
3418

3419
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
6,977,874✔
3420
  int32_t code = TSDB_CODE_SUCCESS;
6,977,874✔
3421
  int32_t lino = 0;
6,977,874✔
3422
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
6,977,874✔
3423

3424
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
6,977,874✔
3425
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3426
  }
3427
  
3428
  SArray* userRecalcList = NULL;
6,977,874✔
3429
  if (atomic_load_ptr(&pStream->userRecalcList)) {
6,977,874✔
3430
    taosWLockLatch(&pStream->userRecalcLock);
6,112✔
3431
    if (pStream->userRecalcList) {
6,112✔
3432
      userRecalcList = pStream->userRecalcList;
6,112✔
3433
      pStream->userRecalcList = NULL;
6,112✔
3434
    }
3435
    taosWUnLockLatch(&pStream->userRecalcLock);
6,112✔
3436
    
3437
    if (userRecalcList) {
6,112✔
3438
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
6,112✔
3439
    }
3440
  }
3441

3442
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
6,977,874✔
3443
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
3,499,245✔
3444
    if (NULL == pStatus->detailStatus) {
3,499,245✔
3445
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
206,379✔
3446
      if (NULL == pStatus->detailStatus) {
206,379✔
3447
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3448
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3449
      }
3450
    }
3451
    
3452
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
3,499,245✔
3453
    taosWUnLockLatch(&pStatus->detailStatusLock);
3,499,245✔
3454
  }
3455

3456
_exit:
3,478,629✔
3457

3458
  if (code) {
6,977,874✔
3459
    // IGNORE STOP STREAM BY ERROR
3460
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3461
  }
3462
}
6,977,874✔
3463

3464
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,571,397✔
3465
  int32_t code = TSDB_CODE_SUCCESS;
1,571,397✔
3466
  int32_t lino = 0;
1,571,397✔
3467
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,571,397✔
3468

3469
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,571,397✔
3470

3471
  for (int32_t i = 0; i < num; ++i) {
57,618,219✔
3472
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
56,046,822✔
3473
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
56,046,822✔
3474
    
3475
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
56,046,822✔
3476
    if (NULL == ppStatus) {
56,046,822✔
3477
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
10,592✔
3478
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
10,592✔
3479
      continue;
10,592✔
3480
    }
3481

3482
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
56,036,230✔
3483
    int8_t stopped = atomic_load_8(&pStream->stopped);
56,036,230✔
3484
    if (stopped) {
56,036,230✔
3485
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
72,615✔
3486
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
72,615✔
3487
      continue;
72,615✔
3488
    }
3489

3490
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
55,963,615✔
3491
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3492
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3493
          
3494
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3495
      continue;
×
3496
    }
3497

3498
    if ((*ppStatus)->status != pTask->status) {
55,963,615✔
3499
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,529,669✔
3500
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,068,305✔
3501
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,461,364✔
3502
        if (pStream->triggerTask) {
×
3503
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3504
        }
3505
        
3506
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3507
      }
3508
    }
3509
    
3510
    (*ppStatus)->errCode = pTask->errorCode;
55,963,615✔
3511
    (*ppStatus)->status = pTask->status;
55,963,615✔
3512
    (*ppStatus)->lastUpTs = pCtx->currTs;
55,963,615✔
3513
    
3514
    if (STREAM_STATUS_RUNNING != pTask->status) {
55,963,615✔
3515
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
16,693,343✔
3516
    }
3517
    
3518
    if (STREAM_TRIGGER_TASK == pTask->type) {
55,963,615✔
3519
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
6,977,874✔
3520
    }
3521
  }
3522

3523
_exit:
1,571,397✔
3524

3525
  if (code) {
1,571,397✔
3526
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3527
  }
3528

3529
  return code;
1,571,397✔
3530
}
3531

3532
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3533
  int32_t code = TSDB_CODE_SUCCESS;
×
3534
  int32_t lino = 0;
×
3535
  int64_t streamId = pTask->streamId;
×
3536
  SStreamObj* pStream = NULL;
×
3537

3538
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3539
  if (NULL == pStatus) {
×
3540
    SStmStatus status = {0};
×
3541
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3542
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3543
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
3544
      mndReleaseStream(pCtx->pMnode, pStream);
×
3545
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3546
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3547
      return code;
×
3548
    }
3549

3550
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3551
    mndReleaseStream(pCtx->pMnode, pStream);
×
3552

3553
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3554
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3555
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3556
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3557
  }
3558

3559
  SStmTaskStatus* pNewTask = NULL;
×
3560
  switch (pTask->type) {
×
3561
    case STREAM_READER_TASK: {
×
3562
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3563
      if (NULL == pList) {
×
3564
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
3565
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3566
      }
3567
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3568
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3569
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3570
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3571
            (int32_t)taosArrayGetSize(pList), readerSize);
3572
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3573
      }
3574
      
3575
      SStmTaskStatus taskStatus = {0};
×
3576
      taskStatus.pStream = pStatus;
×
3577
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3578
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3579
        pNewTask = taosArrayPush(pList, &taskStatus);
×
3580
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3581
      } else {
3582
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3583
      }
3584
      
3585
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3586
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3587
      break;
×
3588
    }
3589
    case STREAM_TRIGGER_TASK: {
×
3590
      taosMemoryFreeClear(pStatus->triggerTask);
×
3591
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3592
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3593
      pStatus->triggerTask->pStream = pStatus;
×
3594
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
3595
      pNewTask = pStatus->triggerTask;
×
3596

3597
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3598
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3599
      break;
×
3600
    }
3601
    case STREAM_RUNNER_TASK:{
×
3602
      if (NULL == pStatus->runners[pTask->deployId]) {
×
3603
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3604
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3605
      }
3606
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
3607
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3608
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3609
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3610
      }    
3611
      
3612
      SStmTaskStatus taskStatus = {0};
×
3613
      taskStatus.pStream = pStatus;
×
3614
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3615
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3616
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3617

3618
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3619
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3620
      break;
×
3621
    }
3622
    default: {
×
3623
      msttError("invalid task type:%d in task status", pTask->type);
×
3624
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3625
      break;
×
3626
    }
3627
  }
3628

3629
_exit:
×
3630

3631
  if (code) {
×
3632
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3633
  } else {
3634
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3635
  }
3636

3637
  return code;
×
3638
}
3639

3640
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3641
  int32_t code = TSDB_CODE_SUCCESS;
×
3642
  int32_t lino = 0;
×
3643
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3644

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

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

3651
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3652
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3653
    }
3654
    
3655
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3656
    if (NULL == ppStatus) {
×
3657
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3658
      
3659
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3660
      
3661
      continue;
×
3662
    }
3663
    
3664
    (*ppStatus)->status = pTask->status;
×
3665
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3666
  }
3667

3668
_exit:
×
3669

3670
  if (code) {
×
3671
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3672
  }
3673

3674
  return code;
×
3675
}
3676

3677
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
279,004✔
3678
  int32_t code = TSDB_CODE_SUCCESS;
279,004✔
3679
  int32_t lino = 0;
279,004✔
3680
  if (NULL == pCtx->pRsp->start.taskList) {
279,004✔
3681
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
116,439✔
3682
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
116,439✔
3683
  }
3684

3685
  SStmTaskId* pId = &pAction->start.triggerId;
279,004✔
3686
  SStreamTaskStart start = {0};
279,004✔
3687
  start.task.type = STREAM_TRIGGER_TASK;
279,004✔
3688
  start.task.streamId = streamId;
279,004✔
3689
  start.task.taskId = pId->taskId;
279,004✔
3690
  start.task.seriousId = pId->seriousId;
279,004✔
3691
  start.task.nodeId = pId->nodeId;
279,004✔
3692
  start.task.taskIdx = pId->taskIdx;
279,004✔
3693

3694
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
558,008✔
3695
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
279,004✔
3696

3697
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
279,004✔
3698

3699
  return;
279,004✔
3700

3701
_exit:
×
3702

3703
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3704
}
3705

3706

3707
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
28,914✔
3708
  int32_t code = TSDB_CODE_SUCCESS;
28,914✔
3709
  int32_t lino = 0;
28,914✔
3710
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
28,914✔
3711
  if (NULL == pCtx->pRsp->undeploy.taskList) {
28,914✔
3712
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
25,700✔
3713
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
25,700✔
3714
  }
3715

3716
  SStreamTaskUndeploy undeploy;
28,914✔
3717
  for (int32_t i = 0; i < dropNum; ++i) {
113,613✔
3718
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
84,699✔
3719
    undeploy.task = *pTask;
84,699✔
3720
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
84,699✔
3721
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
84,699✔
3722

3723
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
169,398✔
3724
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
84,699✔
3725

3726
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
84,699✔
3727
  }
3728

3729
  return;
28,914✔
3730

3731
_exit:
×
3732

3733
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3734
}
3735

3736
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3737
  int32_t code = TSDB_CODE_SUCCESS;
×
3738
  int32_t lino = 0;
×
3739

3740
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3741
  if (NULL == pStream) {
×
3742
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3743
    return;
×
3744
  }
3745

3746
  if (NULL == pStream->triggerTask) {
×
3747
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3748
    return;
×
3749
  }
3750

3751
  SStreamMgmtRsp rsp = {0};
×
3752
  rsp.reqId = INT64_MIN;
×
3753
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3754
  rsp.task.streamId = streamId;
×
3755
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3756

3757
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3758

3759
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3760
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3761
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3762
  }
3763

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

3766
_exit:
×
3767

3768
  if (code) {
×
3769
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3770
  } else {
3771
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3772
  }
3773
}
3774

3775
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
6,112✔
3776
  int32_t code = TSDB_CODE_SUCCESS;
6,112✔
3777
  int32_t lino = 0;
6,112✔
3778

3779
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
6,112✔
3780
  if (NULL == pStream) {
6,112✔
3781
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3782
    return;
×
3783
  }
3784

3785
  if (NULL == pStream->triggerTask) {
6,112✔
3786
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3787
    return;
×
3788
  }
3789

3790
  SStreamMgmtRsp rsp = {0};
6,112✔
3791
  rsp.reqId = INT64_MIN;
6,112✔
3792
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
6,112✔
3793
  rsp.task.streamId = streamId;
6,112✔
3794
  rsp.task.taskId = pStream->triggerTask->id.taskId;
6,112✔
3795
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
6,112✔
3796

3797
  if (NULL == pCtx->pRsp->rsps.rspList) {
6,112✔
3798
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
5,380✔
3799
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
5,380✔
3800
  }
3801

3802
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
12,224✔
3803

3804
_exit:
6,112✔
3805

3806
  if (code) {
6,112✔
3807
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3808
  } else {
3809
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
6,112✔
3810
  }
3811
}
3812

3813

3814
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
147,187✔
3815
  int32_t code = TSDB_CODE_SUCCESS;
147,187✔
3816
  int32_t lino = 0;
147,187✔
3817
  void* pIter = NULL;
147,187✔
3818
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
147,187✔
3819

3820
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
147,187✔
3821

3822
  while (1) {
314,030✔
3823
    pIter = taosHashIterate(pCtx->actionStm, pIter);
461,217✔
3824
    if (pIter == NULL) {
461,217✔
3825
      break;
147,187✔
3826
    }
3827

3828
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
314,030✔
3829
    SStmAction *pAction = (SStmAction *)pIter;
314,030✔
3830
    
3831
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
314,030✔
3832
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
28,914✔
3833
      continue;
28,914✔
3834
    }
3835

3836
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
285,116✔
3837
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3838
    }
3839

3840
    if (STREAM_ACT_RECALC & pAction->actions) {
285,116✔
3841
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
6,112✔
3842
    }
3843

3844
    if (STREAM_ACT_START & pAction->actions) {
285,116✔
3845
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
279,004✔
3846
    }
3847
  }
3848
  
3849
_exit:
147,187✔
3850

3851
  if (pIter) {
147,187✔
3852
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3853
  }
3854

3855
  if (code) {
147,187✔
3856
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3857
  }
3858

3859
  return code;
147,187✔
3860
}
3861

3862
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
14,949,465✔
3863
  int32_t  code = TSDB_CODE_SUCCESS;
14,949,465✔
3864
  int32_t  lino = 0;
14,949,465✔
3865
  int64_t* lastTs = NULL;
14,949,465✔
3866
  bool     noExists = false;
14,949,465✔
3867

3868
  while (true) {
3869
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
15,539,596✔
3870
    if (NULL == lastTs) {
15,539,596✔
3871
      if (noExists) {
817,933✔
3872
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
227,802✔
3873
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
227,802✔
3874
      }
3875

3876
      noExists = true;
590,131✔
3877
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
590,131✔
3878
      
3879
      continue;
590,131✔
3880
    }
3881

3882
    while (true) {
×
3883
      int64_t lastTsValue = atomic_load_64(lastTs);
14,721,663✔
3884
      if (pCtx->currTs > lastTsValue) {
14,721,663✔
3885
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
14,721,185✔
3886
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
14,721,185✔
3887
          return code;
14,721,185✔
3888
        }
3889

3890
        continue;
×
3891
      }
3892

3893
      return code;
478✔
3894
    }
3895

3896
    break;
3897
  }
3898

3899
_exit:
227,802✔
3900

3901
  if (code) {
227,802✔
3902
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
227,802✔
3903
  }
3904

3905
  return code;  
227,802✔
3906
}
3907

3908
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
3909
  SStmStatus* pStatus = NULL;
×
3910
  int32_t trigReaderNum = 0;
×
3911
  int32_t calcReaderNum = 0;
×
3912
  int32_t runnerNum = 0;
×
3913
  int64_t streamId = 0;
×
3914
  void* pIter = NULL;
×
3915
  while (true) {
3916
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
3917
    if (NULL == pIter) {
×
3918
      return;
×
3919
    }
3920

3921
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3922
    pStatus = (SStmStatus*)pIter;
×
3923

3924
    if (NULL == pStatus->triggerTask) {
×
3925
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
3926
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3927
      continue;
×
3928
    }
3929
    
3930
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
3931
    if (pStatus->trigReaderNum != trigReaderNum) {
×
3932
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
3933
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3934
      continue;
×
3935
    }
3936

3937
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
3938
    if (pStatus->calcReaderNum != calcReaderNum) {
×
3939
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
3940
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3941
      continue;
×
3942
    }
3943

3944
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
3945
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
3946
      if (runnerNum != pStatus->runnerNum) {
×
3947
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
3948
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3949
        continue;
×
3950
      }
3951
    }
3952
  }
3953
}
3954

3955
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
252✔
3956
  int32_t code = TSDB_CODE_SUCCESS;
252✔
3957
  int32_t lino = 0;
252✔
3958
  int32_t minVal = watchError ? 0 : 1;
252✔
3959

3960
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
252✔
3961
    return code;
×
3962
  }
3963

3964
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
252✔
3965
    (void)sched_yield();
×
3966
  }
3967

3968
  if (watchError) {
252✔
3969
    taosHashClear(mStreamMgmt.vgroupMap);
×
3970
    taosHashClear(mStreamMgmt.snodeMap);
×
3971
    taosHashClear(mStreamMgmt.taskMap);
×
3972
    taosHashClear(mStreamMgmt.streamMap);
×
3973
    mstInfo("watch error happends, clear all maps");
×
3974
    goto _exit;
×
3975
  }
3976

3977
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
252✔
3978
    mstInfo("no stream tasks remain during watch state");
252✔
3979
    goto _exit;
252✔
3980
  }
3981

3982
  msmWatchCheckStreamMap(pCtx);
×
3983

3984
_exit:
252✔
3985

3986
  mStreamMgmt.lastTaskId += 100000;
252✔
3987

3988
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
252✔
3989

3990
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
252✔
3991

3992
  if (code) {
252✔
3993
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3994
  }
3995

3996
  return code;
252✔
3997
}
3998

3999

4000
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
1,008✔
4001
  int32_t code = TSDB_CODE_SUCCESS;
1,008✔
4002
  int32_t lino = 0;
1,008✔
4003
  SStreamHbMsg* pReq = pCtx->pReq;
1,008✔
4004

4005
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
1,008✔
4006
  
4007
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
1,008✔
4008
    goto _exit;
×
4009
  }
4010

4011
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
1,008✔
4012
  if (GOT_SNODE(pReq->snodeId)) {
1,008✔
4013
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,008✔
4014
  }
4015

4016
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
1,008✔
4017
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4018
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4019
  }
4020

4021
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
1,008✔
4022
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
252✔
4023
  }
4024

4025
_exit:
1,008✔
4026

4027
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
1,008✔
4028
  
4029
  if (code) {
1,008✔
4030
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4031

4032
    (void)msmWatchHandleEnding(pCtx, true);
×
4033
  }
4034

4035
  return code;
1,008✔
4036
}
4037

4038
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
81,594✔
4039
  int32_t code = TSDB_CODE_SUCCESS;
81,594✔
4040
  int32_t lino = 0;
81,594✔
4041
  bool    readerExists = false;
81,594✔
4042
  int64_t streamId = pTask->streamId;
81,594✔
4043

4044
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
81,594✔
4045
  for (int32_t i = 0; i < readerNum; ++i) {
162,297✔
4046
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
98,002✔
4047
    if (pReader->id.nodeId == vgId) {
98,002✔
4048
      readerExists = true;
17,299✔
4049
      break;
17,299✔
4050
    }
4051
  }
4052

4053
  if (!readerExists) {
81,594✔
4054
    if (NULL == pStatus->trigOReaders) {
64,295✔
4055
      pStatus->trigOReaders = taosArrayInit(vgNum, sizeof(SStmTaskStatus));
56,058✔
4056
      TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
56,058✔
4057
    }
4058
    
4059
    SStmTaskStatus* pState = taosArrayReserve(pStatus->trigOReaders, 1);
64,295✔
4060
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
64,295✔
4061
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
64,295✔
4062
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
64,295✔
4063
  }
4064

4065
_exit:
81,594✔
4066

4067
  if (code) {
81,594✔
4068
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4069
  }
4070

4071
  return code;
81,594✔
4072
}
4073

4074
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
72,625✔
4075
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
72,625✔
4076
  int32_t lino = 0;
72,625✔
4077
  int32_t vgId = 0;
72,625✔
4078
  int64_t streamId = pTask->streamId;
72,625✔
4079
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
72,625✔
4080
  int32_t tbNum = taosArrayGetSize(pTbs);
72,625✔
4081
  SStreamDbTableName* pName = NULL;
72,625✔
4082
  SSHashObj* pDbVgroups = NULL;
72,625✔
4083
  SStreamMgmtRsp rsp = {0};
72,625✔
4084
  rsp.reqId = pTask->pMgmtReq->reqId;
72,625✔
4085
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
72,625✔
4086
  int32_t iter = 0;
72,625✔
4087
  void* p = NULL;
72,625✔
4088
  SSHashObj* pVgs = NULL;
72,625✔
4089
  SStreamMgmtReq* pMgmtReq = NULL;
72,625✔
4090
  int8_t stopped = 0;
72,625✔
4091

4092
  if (NULL == pCtx->pRsp->rsps.rspList) {
72,625✔
4093
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4094
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4095
  }
4096
  
4097
  TSWAP(pTask->pMgmtReq, pMgmtReq);
72,625✔
4098
  rsp.task = *(SStreamTask*)pTask;
72,625✔
4099

4100
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
72,625✔
4101
  if (NULL == pStatus) {
72,625✔
4102
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4103
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4104
  }
4105

4106
  stopped = atomic_load_8(&pStatus->stopped);
72,625✔
4107
  if (stopped) {
72,625✔
4108
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4109
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4110
  }
4111

4112
  if (tbNum <= 0) {
72,625✔
4113
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4114
    goto _exit;
×
4115
  }
4116

4117
  int32_t oReaderNum = taosArrayGetSize(pStatus->trigOReaders);
72,625✔
4118
  if (oReaderNum > 0) {
72,625✔
4119
    mstsWarn("origReaders already exits, num:%d", oReaderNum);
×
4120
    goto _exit;
×
4121
  }
4122

4123
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
72,625✔
4124
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
72,625✔
4125
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
72,625✔
4126

4127
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
72,625✔
4128
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
72,625✔
4129
  
4130
  for (int32_t i = 0; i < tbNum; ++i) {
274,553✔
4131
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
201,928✔
4132
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
201,928✔
4133
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
403,856✔
4134
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
201,928✔
4135
  }
4136

4137
  int32_t vgNum = tSimpleHashGetSize(pVgs);
72,625✔
4138
  while (true) {
4139
    p = tSimpleHashIterate(pVgs, p, &iter);
154,219✔
4140
    if (NULL == p) {
154,219✔
4141
      break;
72,625✔
4142
    }
4143
    
4144
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
81,594✔
4145
  }
4146
  
4147
  vgNum = taosArrayGetSize(pStatus->trigOReaders);
72,625✔
4148
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
72,625✔
4149
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
72,625✔
4150

4151
  SStreamTaskAddr addr;
72,625✔
4152
  for (int32_t i = 0; i < vgNum; ++i) {
136,920✔
4153
    SStmTaskStatus* pOTask = taosArrayGet(pStatus->trigOReaders, i);
64,295✔
4154
    addr.taskId = pOTask->id.taskId;
64,295✔
4155
    addr.nodeId = pOTask->id.nodeId;
64,295✔
4156
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
64,295✔
4157
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
128,590✔
4158
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
64,295✔
4159
  }
4160

4161
_exit:
72,625✔
4162

4163
  tFreeSStreamMgmtReq(pMgmtReq);
72,625✔
4164
  taosMemoryFree(pMgmtReq);
72,625✔
4165

4166
  tSimpleHashCleanup(pVgs);
72,625✔
4167
  mstDestroyDbVgroupsHash(pDbVgroups);
72,625✔
4168

4169
  if (code) {
72,625✔
4170
    rsp.code = code;
×
4171
    
4172
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4173

4174
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4175
  } else {
4176
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
145,250✔
4177
  }
4178

4179
_final:
72,625✔
4180

4181
  if (finalCode) {
72,625✔
4182
    tFreeSStreamMgmtRsp(&rsp);
×
4183
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4184
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4185
  }
4186

4187
  return finalCode;
72,625✔
4188
}
4189

4190
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
4,279✔
4191
  int32_t num = taosArrayGetSize(pList);
4,279✔
4192
  SStreamCalcScan* pScan = NULL;
4,279✔
4193
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
4,279✔
4194
  int64_t planUid = 0;
4,279✔
4195
  for (int32_t i = 0; i < num; ++i) {
4,279✔
4196
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
4,279✔
4197
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
4,279✔
4198
    if (0 != planUid && planUid == uid) {
4,279✔
4199
      *ppRes = pScan;
4,279✔
4200
      break;
4,279✔
4201
    }
4202
  }
4203

4204
_exit:
4,279✔
4205

4206
  if (code) {
4,279✔
4207
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4208
  }
4209

4210
  return code;
4,279✔
4211
}
4212

4213
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
4,279✔
4214
  int32_t code = TSDB_CODE_SUCCESS;
4,279✔
4215
  int32_t lino = 0;
4,279✔
4216
  bool    readerExists = false;
4,279✔
4217
  int64_t streamId = pTask->streamId;
4,279✔
4218
  SListNode* pNode = listHead(pStatus->calcReaders);
4,279✔
4219
  SStmTaskStatus* pReader = NULL;
4,279✔
4220
  int32_t taskIdx = 0;
4,279✔
4221

4222
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
4,279✔
4223
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
39,678✔
4224
    pReader = (SStmTaskStatus*)pNode->data;
35,399✔
4225
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
35,399✔
4226
      readerExists = true;
×
4227
      pAddr->taskId = pReader->id.taskId;
×
4228
      break;
×
4229
    }
4230
  }
4231

4232
  if (!readerExists) {
4,279✔
4233
    if (NULL == pStatus->calcReaders) {
4,279✔
4234
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4235
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4236
      taskIdx = 0;
×
4237
    } else {
4238
      pNode = listTail(pStatus->calcReaders);
4,279✔
4239
      pReader = (SStmTaskStatus*)pNode->data;
4,279✔
4240
      taskIdx = pReader->id.taskIdx + 1;
4,279✔
4241
    }
4242

4243
    SStreamCalcScan* pScan = NULL;
4,279✔
4244
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
4,279✔
4245
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
4,279✔
4246
    pReader = tdListReserve(pStatus->calcReaders);
4,279✔
4247
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
4,279✔
4248
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
4,279✔
4249
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
4,279✔
4250
    pAddr->taskId = pReader->id.taskId;
4,279✔
4251
  }
4252

4253
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
4,279✔
4254
  pAddr->nodeId = vgId;
4,279✔
4255

4256
_exit:
4,279✔
4257

4258
  if (code) {
4,279✔
4259
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4260
  }
4261

4262
  return code;
4,279✔
4263
}
4264

4265

4266
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
4,279✔
4267
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
4,279✔
4268
  int32_t lino = 0;
4,279✔
4269
  int32_t vgId = 0;
4,279✔
4270
  int64_t streamId = pTask->streamId;
4,279✔
4271
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
4,279✔
4272
  int32_t reqNum = taosArrayGetSize(pReqs);
4,279✔
4273
  SStreamOReaderDeployReq* pReq = NULL;
4,279✔
4274
  SStreamOReaderDeployRsp* pRsp = NULL;
4,279✔
4275
  SStreamMgmtRsp rsp = {0};
4,279✔
4276
  rsp.reqId = pTask->pMgmtReq->reqId;
4,279✔
4277
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
4,279✔
4278
  SStreamMgmtReq* pMgmtReq = NULL;
4,279✔
4279
  int8_t stopped = 0;
4,279✔
4280
  int32_t vgNum = 0;
4,279✔
4281
  SStreamTaskAddr* pAddr = NULL;
4,279✔
4282

4283
  if (NULL == pCtx->pRsp->rsps.rspList) {
4,279✔
4284
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4285
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4286
  }
4287
  
4288
  TSWAP(pTask->pMgmtReq, pMgmtReq);
4,279✔
4289
  rsp.task = *(SStreamTask*)pTask;
4,279✔
4290

4291
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
4,279✔
4292
  if (NULL == pStatus) {
4,279✔
4293
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4294
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4295
  }
4296

4297
  stopped = atomic_load_8(&pStatus->stopped);
4,279✔
4298
  if (stopped) {
4,279✔
4299
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4300
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4301
  }
4302

4303
  if (reqNum <= 0) {
4,279✔
4304
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4305
    goto _exit;
×
4306
  }
4307

4308
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
4,279✔
4309
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
4,279✔
4310

4311
  for (int32_t i = 0; i < reqNum; ++i) {
8,558✔
4312
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
4,279✔
4313
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
4,279✔
4314
    pRsp->execId = pReq->execId;
4,279✔
4315
    vgNum = taosArrayGetSize(pReq->vgIds);
4,279✔
4316
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
4,279✔
4317
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
4,279✔
4318
    
4319
    for (int32_t n = 0; n < vgNum; ++n) {
8,558✔
4320
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
4,279✔
4321
      pAddr = taosArrayGet(pRsp->vgList, n);
4,279✔
4322
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
4,279✔
4323
    }
4324
  }
4325

4326
_exit:
4,279✔
4327

4328
  tFreeSStreamMgmtReq(pMgmtReq);
4,279✔
4329
  taosMemoryFree(pMgmtReq);
4,279✔
4330

4331
  if (code) {
4,279✔
4332
    rsp.code = code;
×
4333
    
4334
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4335
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4336
  } else {
4337
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
8,558✔
4338
  }
4339

4340

4341
_final:
4,279✔
4342

4343
  if (finalCode) {
4,279✔
4344
    tFreeSStreamMgmtRsp(&rsp);
×
4345
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4346
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4347
  }
4348

4349
  return finalCode;
4,279✔
4350
}
4351

4352

4353
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
76,904✔
4354
  int32_t code = TSDB_CODE_SUCCESS;
76,904✔
4355
  int32_t lino = 0;
76,904✔
4356

4357
  switch (pTask->pMgmtReq->type) {
76,904✔
4358
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
72,625✔
4359
      msmDeployTriggerOrigReader(pCtx, pTask);
72,625✔
4360
      break;
72,625✔
4361
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
4,279✔
4362
      msmDeployRunnerOrigReader(pCtx, pTask);
4,279✔
4363
      break;
4,279✔
4364
    default:
×
4365
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4366
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4367
      break;
×
4368
  }
4369

4370
_exit:
76,904✔
4371

4372
  if (code) {
76,904✔
4373
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4374
  }
4375

4376
  return code;
76,904✔
4377
}
4378

4379
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
22,671✔
4380
  int32_t code = TSDB_CODE_SUCCESS;
22,671✔
4381
  int32_t lino = 0;
22,671✔
4382
  SStreamHbMsg* pReq = pCtx->pReq;
22,671✔
4383
  SStmTaskStatusMsg* pTask = NULL;
22,671✔
4384
  
4385
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
22,671✔
4386
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
22,671✔
4387
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
22,671✔
4388
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
22,671✔
4389
  }
4390
  
4391
  for (int32_t i = 0; i < reqNum; ++i) {
99,575✔
4392
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
76,904✔
4393
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
76,904✔
4394
    if (NULL == pTask) {
76,904✔
4395
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4396
      continue;
×
4397
    }
4398

4399
    if (NULL == pTask->pMgmtReq) {
76,904✔
4400
      msttError("idx %d without mgmtReq", idx);
×
4401
      continue;
×
4402
    }
4403

4404
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
76,904✔
4405
  }
4406

4407
_exit:
22,671✔
4408

4409
  if (code) {
22,671✔
4410
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4411
  }
4412

4413
  return code;
22,671✔
4414
}
4415

4416
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
14,948,457✔
4417
  int32_t code = TSDB_CODE_SUCCESS;
14,948,457✔
4418
  int32_t lino = 0;
14,948,457✔
4419
  SStreamHbMsg* pReq = pCtx->pReq;
14,948,457✔
4420

4421
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
14,948,457✔
4422
  if (GOT_SNODE(pReq->snodeId)) {
14,720,655✔
4423
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,675,403✔
4424
  }
4425
  
4426
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
14,720,655✔
4427
    msmHandleStreamActions(pCtx);
53,496✔
4428
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
53,496✔
4429
  }
4430

4431
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
14,720,655✔
4432
    code = msmHandleStreamRequests(pCtx);
22,671✔
4433
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
22,671✔
4434
    TAOS_CHECK_EXIT(code);
22,671✔
4435
  }
4436

4437
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,720,655✔
4438
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
70,660✔
4439
  } else {
4440
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
14,649,995✔
4441
  }
4442

4443
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
14,720,655✔
4444
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
103,863✔
4445
  }
4446

4447
  if (taosHashGetSize(pCtx->deployStm) > 0) {
14,720,655✔
4448
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
73,582✔
4449
  }
4450

4451
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
14,720,655✔
4452
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,571,397✔
4453
  }
4454

4455
  if (taosHashGetSize(pCtx->actionStm) > 0) {
14,720,655✔
4456
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
147,187✔
4457
  }
4458

4459
_exit:
14,720,655✔
4460

4461
  if (code) {
14,948,457✔
4462
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
227,802✔
4463
  }
4464

4465
  return code;
14,948,457✔
4466
}
4467

4468
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
15,117,448✔
4469
  int32_t lino = 0;
15,117,448✔
4470
  int32_t tlen = 0;
15,117,448✔
4471
  void   *buf = NULL;
15,117,448✔
4472

4473
  if (TSDB_CODE_SUCCESS != code) {
15,117,448✔
4474
    goto _exit;
227,802✔
4475
  }
4476

4477
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
14,889,646✔
4478
  if (code < 0) {
14,889,646✔
4479
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4480
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4481
  }
4482

4483
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
14,889,646✔
4484
  if (buf == NULL) {
14,889,294✔
4485
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4486
    TAOS_CHECK_EXIT(terrno);    
×
4487
  }
4488

4489
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
14,889,294✔
4490
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
14,889,294✔
4491

4492
  SEncoder encoder;
14,870,126✔
4493
  tEncoderInit(&encoder, abuf, tlen);
14,889,646✔
4494
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
14,889,646✔
4495
    rpcFreeCont(buf);
×
4496
    buf = NULL;
×
4497
    tEncoderClear(&encoder);
×
4498
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4499
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4500
  }
4501
  tEncoderClear(&encoder);
14,889,646✔
4502

4503
_exit:
15,117,096✔
4504

4505
  pMsg->code = code;
15,117,096✔
4506
  pMsg->info = *pRpcInfo;
15,117,448✔
4507
  if (TSDB_CODE_SUCCESS == code) {
15,117,448✔
4508
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
14,889,294✔
4509
    pMsg->pCont = buf;
14,889,646✔
4510
  }
4511
}
15,117,096✔
4512

4513

4514
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
15,117,448✔
4515
  int32_t code = TSDB_CODE_SUCCESS;
15,117,448✔
4516
  SMStreamHbRspMsg rsp = {0};
15,117,448✔
4517
  rsp.streamGId = pHb->streamGId;
15,117,448✔
4518

4519
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
15,117,448✔
4520

4521
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
15,117,448✔
4522
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
167,983✔
4523
    goto _exit;
167,983✔
4524
  }
4525

4526
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,949,465✔
4527
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
14,949,465✔
4528

4529
  pCtx->tidx = tidx;
14,949,465✔
4530
  pCtx->pMnode = pMnode;
14,949,465✔
4531
  pCtx->currTs = currTs;
14,949,465✔
4532
  pCtx->pReq = pHb;
14,949,465✔
4533
  pCtx->pRsp = &rsp;
14,949,465✔
4534
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
14,949,465✔
4535
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
14,949,465✔
4536
  
4537
  switch (atomic_load_8(&mStreamMgmt.state)) {
14,949,465✔
4538
    case MND_STM_STATE_WATCH:
1,008✔
4539
      code = msmWatchHandleHbMsg(pCtx);
1,008✔
4540
      break;
1,008✔
4541
    case MND_STM_STATE_NORMAL:
14,948,457✔
4542
      code = msmNormalHandleHbMsg(pCtx);
14,948,457✔
4543
      break;
14,948,457✔
4544
    default:
×
4545
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4546
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4547
      break;
×
4548
  }
4549

4550
_exit:
15,117,448✔
4551

4552
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
15,117,448✔
4553

4554
  msmCleanStreamGrpCtx(pHb);
15,117,448✔
4555
  msmClearStreamToDeployMaps(pHb);
15,117,448✔
4556

4557
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
15,117,448✔
4558
  
4559
  tFreeSMStreamHbRspMsg(&rsp);
15,117,448✔
4560

4561
  return code;
15,117,096✔
4562
}
4563

4564
void msmHandleBecomeLeader(SMnode *pMnode) {
422,435✔
4565
  if (tsDisableStream) {
422,435✔
4566
    return;
×
4567
  }
4568

4569
  mstInfo("start to process mnode become leader");
422,435✔
4570

4571
  int32_t code = 0;
422,435✔
4572
  streamAddVnodeLeader(MNODE_HANDLE);
422,435✔
4573
  
4574
  taosWLockLatch(&mStreamMgmt.runtimeLock);
422,435✔
4575
  msmDestroyRuntimeInfo(pMnode);
422,435✔
4576
  code = msmInitRuntimeInfo(pMnode);
422,435✔
4577
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
422,435✔
4578

4579
  if (TSDB_CODE_SUCCESS == code) {
422,435✔
4580
    atomic_store_8(&mStreamMgmt.active, 1);
422,435✔
4581
  }
4582

4583
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
422,435✔
4584
}
4585

4586
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
613,499✔
4587
  if (tsDisableStream) {
613,499✔
4588
    return;
×
4589
  }
4590

4591
  mstInfo("start to process mnode become not leader");
613,499✔
4592

4593
  streamRemoveVnodeLeader(MNODE_HANDLE);
613,499✔
4594

4595
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
613,499✔
4596
    taosWLockLatch(&mStreamMgmt.runtimeLock);
422,435✔
4597
    msmDestroyRuntimeInfo(pMnode);
422,435✔
4598
    mStreamMgmt.stat.inactiveTimes++;
422,435✔
4599
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
422,435✔
4600
  }
4601
}
4602

4603

4604
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4605
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4606
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4607
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4608
  } else {
4609
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4610
  }
4611
}
×
4612

4613
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
13,546✔
4614
  int32_t code = TSDB_CODE_SUCCESS;
13,546✔
4615
  int32_t lino = 0;
13,546✔
4616
  SStreamObj* pStream = pObj;
13,546✔
4617
  SSnodeObj* pSnode = p1;
13,546✔
4618
  SArray** ppRes = p2;
13,546✔
4619

4620
  if (pStream->mainSnodeId == pSnode->id) {
13,546✔
4621
    if (NULL == *ppRes) {
9,708✔
4622
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,880✔
4623
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
1,880✔
4624
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
1,880✔
4625
    }
4626

4627
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
19,416✔
4628
  }
4629

4630
  return true;
13,546✔
4631

4632
_exit:
×
4633

4634
  if (code) {
×
4635
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4636
  }  
4637

4638
  *(int32_t*)p3 = code;
×
4639

4640
  return false;
×
4641
}
4642

4643

4644
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
66,061✔
4645
  int32_t code = TSDB_CODE_SUCCESS;
66,061✔
4646
  int32_t lino = 0;
66,061✔
4647
  
4648
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
66,061✔
4649
  TAOS_CHECK_EXIT(code);
66,061✔
4650

4651
  int32_t streamNum = taosArrayGetSize(*ppRes);
66,061✔
4652
  if (streamNum > 0 && 0 == pSnode->replicaId) {
66,061✔
4653
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
366✔
4654
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
366✔
4655
  }
4656

4657
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4658

4659
_exit:
66,061✔
4660

4661
  if (code) {
66,061✔
4662
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
366✔
4663
  }  
4664

4665
  return code;
66,061✔
4666
}
4667

4668
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
580,727✔
4669
  SStreamObj* pStream = pObj;
580,727✔
4670
  int64_t streamId = pStream->pCreate->streamId;
580,727✔
4671
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
580,727✔
4672
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
580,727✔
4673
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
580,727✔
4674
  
4675
  if ((userDropped || userStopped) && (NULL == pStatus)) {
580,727✔
4676
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
732✔
4677
    return true;
732✔
4678
  }
4679
  
4680
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
579,995✔
4681
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
424,641✔
4682
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4683
    return true;
424,641✔
4684
  }
4685

4686
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
155,354✔
4687
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
27,488✔
4688
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4689
    return true;
27,488✔
4690
  }  
4691

4692
  if (pStatus) {
127,866✔
4693
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
125,051✔
4694
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4695
    }
4696

4697
    return true;
125,051✔
4698
  }
4699

4700
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
2,815✔
4701

4702
  return true;
2,815✔
4703
}
4704

4705
void msmCheckLoopStreamMap(SMnode *pMnode) {
60,847✔
4706
  SStmStatus* pStatus = NULL;
60,847✔
4707
  void* pIter = NULL;
60,847✔
4708
  int8_t stopped = 0;
60,847✔
4709
  int64_t streamId = 0;
60,847✔
4710
  
4711
  while (true) {
4712
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
400,358✔
4713
    if (NULL == pIter) {
400,358✔
4714
      break;
60,847✔
4715
    }
4716

4717
    pStatus = (SStmStatus*)pIter;
339,511✔
4718

4719
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
339,511✔
4720
    stopped = atomic_load_8(&pStatus->stopped);
339,511✔
4721
    if (MST_IS_USER_STOPPED(stopped)) {
339,511✔
4722
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
3,194✔
4723
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
3,194✔
4724
      continue;
3,194✔
4725
    }
4726

4727
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
336,317✔
4728
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4729
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4730
      continue;
×
4731
    }
4732

4733
    if (MST_IS_ERROR_STOPPED(stopped)) {
336,317✔
4734
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
18,793✔
4735
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
12,788✔
4736
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4737
            
4738
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
12,788✔
4739
        continue;
12,788✔
4740
      }
4741

4742
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
6,005✔
4743
      continue;
6,005✔
4744
    }
4745

4746
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
317,524✔
4747
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4748
      continue;
×
4749
    }
4750
  }
4751
}
60,847✔
4752

4753
void msmCheckStreamsStatus(SMnode *pMnode) {
470,808✔
4754
  SStmCheckStatusCtx ctx = {0};
470,808✔
4755

4756
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
470,808✔
4757
  
4758
  if (MST_READY_FOR_SDB_LOOP()) {
470,808✔
4759
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
84,167✔
4760
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
84,167✔
4761
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
84,167✔
4762
  }
4763

4764
  if (MST_READY_FOR_MAP_LOOP()) {
470,808✔
4765
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
60,847✔
4766
    msmCheckLoopStreamMap(pMnode);
60,847✔
4767
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
60,847✔
4768
  }
4769
}
470,808✔
4770

4771
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
5,760,292✔
4772
  for (int32_t i = 0; i < taskNum; ++i) {
11,799,267✔
4773
    SStmTaskStatus* pTask = *(pList + i);
6,038,975✔
4774

4775
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
6,038,975✔
4776
      continue;
106,430✔
4777
    }
4778
    
4779
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
5,947,035✔
4780
      continue;
5,944,219✔
4781
    }
4782

4783
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
2,816✔
4784
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
2,816✔
UNCOV
4785
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4786
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4787
          
UNCOV
4788
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
UNCOV
4789
      break;
×
4790
    }
4791

4792
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
2,816✔
4793
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4794

4795
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
2,816✔
4796
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
2,816✔
4797

4798
    SStmTaskAction task = {0};
2,816✔
4799
    task.streamId = streamId;
2,816✔
4800
    task.id = pTask->id;
2,816✔
4801
    task.flag = pTask->flags;
2,816✔
4802
    task.type = pTask->type;
2,816✔
4803
    
4804
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
2,816✔
4805
  }
4806
}
5,760,292✔
4807

4808
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
202,323✔
4809
  void* pIter = NULL;
202,323✔
4810
  SStmVgStreamStatus* pVg = NULL;
202,323✔
4811
  int64_t streamId = 0;
202,323✔
4812
  
4813
  while (true) {
2,414,743✔
4814
    pIter = taosHashIterate(pStreams, pIter);
2,617,066✔
4815
    if (NULL == pIter) {
2,617,066✔
4816
      break;
202,323✔
4817
    }
4818

4819
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,414,743✔
4820
    pVg = (SStmVgStreamStatus*)pIter;
2,414,743✔
4821

4822
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
2,414,743✔
4823
    if (taskNum > 0) {
2,414,743✔
4824
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,426,962✔
4825
    }
4826

4827
    taskNum = taosArrayGetSize(pVg->calcReaders);
2,414,743✔
4828
    if (taskNum > 0) {
2,414,743✔
4829
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,529,868✔
4830
    }
4831
  }
4832
}
202,323✔
4833

4834
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4835
  int64_t streamId = 0;
×
4836
  void* pIter = NULL;
×
4837
  SStmVgStreamStatus* pStream = NULL;
×
4838

4839
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4840
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4841
    return;
×
4842
  }
4843

4844
  
4845
  while (true) {
4846
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4847
    if (NULL == pIter) {
×
4848
      break;
×
4849
    }
4850

4851
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4852
    
4853
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4854
  }
4855

4856
  taosHashClear(pVg->streamTasks);
×
4857
}
4858

4859

4860
void msmCheckVgroupStatus(SMnode *pMnode) {
470,808✔
4861
  void* pIter = NULL;
470,808✔
4862
  int32_t code = 0;
470,808✔
4863
  
4864
  while (true) {
1,889,897✔
4865
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
2,360,705✔
4866
    if (NULL == pIter) {
2,360,705✔
4867
      break;
470,808✔
4868
    }
4869

4870
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,889,897✔
4871
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,889,897✔
4872
      continue;
1,684,998✔
4873
    }
4874
    
4875
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
204,899✔
4876

4877
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
204,899✔
4878
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
2,576✔
4879
      if (NULL == pVgroup) {
2,576✔
4880
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
2,576✔
4881
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
2,576✔
4882
        if (code) {
2,576✔
4883
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4884
        }
4885
        continue;
2,576✔
4886
      }
4887
      mndReleaseVgroup(pMnode, pVgroup);
×
4888
      
4889
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4890
      
4891
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
4892
      continue;
×
4893
    }
4894

4895
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
202,323✔
4896

4897
    msmCheckVgroupStreamStatus(pVg->streamTasks);
202,323✔
4898
  }
4899
}
470,808✔
4900

4901
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
1,919✔
4902
  *deployNum = 0;
1,919✔
4903
  
4904
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
5,246✔
4905
    if (pStream->runners[i]) {
4,542✔
4906
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
1,919✔
4907
      for (int32_t t = 0; t < taskNum; ++t) {
2,623✔
4908
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
1,919✔
4909
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
1,919✔
4910
        if (stopped) {
1,919✔
4911
          mstsDebug("stream already stopped %d, ignore it", stopped);
1,215✔
4912
          *deployNum = 0;
1,215✔
4913
          return;
1,215✔
4914
        }
4915

4916
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
704✔
4917
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
704✔
4918
      }
4919
      
4920
      deployId[*deployNum] = i;
704✔
4921
      (*deployNum)++;
704✔
4922
    }
4923
  }
4924
}
4925

4926
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
5,810✔
4927
  pSnode->runnerThreadNum = -1;
5,810✔
4928

4929
  (void)msmSTAddSnodesToMap(pMnode);
5,810✔
4930

4931
  int64_t streamId = 0;
5,810✔
4932
  void* pIter = NULL;
5,810✔
4933
  SStmSnodeStreamStatus* pStream = NULL;
5,810✔
4934
  int32_t deployNum = 0;
5,810✔
4935
  SStmTaskAction task = {0};
5,810✔
4936
  
4937
  while (true) {
4938
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
10,106✔
4939
    if (NULL == pIter) {
10,106✔
4940
      break;
5,810✔
4941
    }
4942

4943
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
4,296✔
4944
    
4945
    task.streamId = streamId;
4,296✔
4946
    
4947
    pStream = (SStmSnodeStreamStatus*)pIter;
4,296✔
4948
    if (pStream->trigger) {
4,296✔
4949
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
2,377✔
4950
      if (stopped) {
2,377✔
4951
        mstsDebug("stream already stopped %d, ignore it", stopped);
1,620✔
4952
        continue;
1,620✔
4953
      }
4954

4955
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
757✔
4956
      
4957
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
757✔
4958
    } else {
4959
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
1,919✔
4960
      
4961
      if (task.deployNum > 0) {
1,919✔
4962
        //task.triggerStatus = pStream->trigger;
4963
        task.multiRunner = true;
704✔
4964
        task.type = STREAM_RUNNER_TASK;
704✔
4965
        
4966
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
704✔
4967
        
4968
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
704✔
4969
      }
4970
    }
4971
  }
4972

4973
  taosHashClear(pSnode->streamTasks);
5,810✔
4974
}
5,810✔
4975

4976

4977
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
64,279✔
4978
  void* pIter = NULL;
64,279✔
4979
  SStmSnodeStreamStatus* pSnode = NULL;
64,279✔
4980
  int64_t streamId = 0;
64,279✔
4981
  
4982
  while (true) {
4983
    pIter = taosHashIterate(pStreams, pIter);
806,651✔
4984
    if (NULL == pIter) {
806,651✔
4985
      break;
64,279✔
4986
    }
4987

4988
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
742,372✔
4989
    pSnode = (SStmSnodeStreamStatus*)pIter;
742,372✔
4990

4991
    if (NULL != pSnode->trigger) {
742,372✔
4992
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
719,995✔
4993
    }
4994

4995
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,969,488✔
4996
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
2,227,116✔
4997
      if (taskNum > 0) {
2,227,116✔
4998
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
2,083,467✔
4999
      }
5000
    }
5001
  }
5002
}
64,279✔
5003

5004

5005
void msmCheckSnodeStatus(SMnode *pMnode) {
470,808✔
5006
  void* pIter = NULL;
470,808✔
5007
  
5008
  while (true) {
638,499✔
5009
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
1,109,307✔
5010
    if (NULL == pIter) {
1,109,307✔
5011
      break;
470,808✔
5012
    }
5013

5014
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
638,499✔
5015
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
638,499✔
5016
      continue;
562,985✔
5017
    }
5018

5019
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
75,514✔
5020
    
5021
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
75,514✔
5022
    if (NULL == pSnode->streamTasks) {
75,514✔
5023
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
6,587✔
5024
      continue;
6,587✔
5025
    }
5026
    
5027
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
68,927✔
5028
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
4,648✔
5029
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5030
      
5031
      msmHandleSnodeLost(pMnode, pSnode);
4,648✔
5032
      continue;
4,648✔
5033
    }
5034
    
5035
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
64,279✔
5036

5037
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
64,279✔
5038
  }
5039
}
470,808✔
5040

5041

5042
void msmCheckTasksStatus(SMnode *pMnode) {
470,808✔
5043
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
470,808✔
5044

5045
  msmCheckVgroupStatus(pMnode);
470,808✔
5046
  msmCheckSnodeStatus(pMnode);
470,808✔
5047
}
470,808✔
5048

5049
void msmCheckSnodesState(SMnode *pMnode) {
470,808✔
5050
  if (!MST_READY_FOR_SNODE_LOOP()) {
470,808✔
5051
    return;
421,191✔
5052
  }
5053

5054
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
49,617✔
5055

5056
  void* pIter = NULL;
49,617✔
5057
  int32_t snodeId = 0;
49,617✔
5058
  while (true) {
56,835✔
5059
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
106,452✔
5060
    if (NULL == pIter) {
106,452✔
5061
      break;
49,617✔
5062
    }
5063

5064
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
56,835✔
5065
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
56,835✔
5066
      continue;
55,673✔
5067
    }
5068

5069
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
1,162✔
5070
    if (NULL == pSnode->streamTasks) {
1,162✔
5071
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5072
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5073
      continue;
×
5074
    }
5075
    
5076
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,162✔
5077
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5078
    
5079
    msmHandleSnodeLost(pMnode, pSnode);
1,162✔
5080
  }
5081

5082
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
49,617✔
5083
}
5084

5085
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
7,512,516✔
5086
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
7,512,516✔
5087
  if (0 == active || MND_STM_STATE_NORMAL != state) {
7,512,516✔
5088
    mstTrace("ignore health check since active:%d state:%d", active, state);
756✔
5089
    return false;
756✔
5090
  }
5091

5092
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,511,760✔
5093
    mstTrace("ignore health check since no stream now");
6,570,144✔
5094
    return false;
6,570,144✔
5095
  }
5096

5097
  return true;
941,616✔
5098
}
5099

5100
void msmHealthCheck(SMnode *pMnode) {
7,041,708✔
5101
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,041,708✔
5102
    return;
6,570,900✔
5103
  }
5104

5105
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
896,916✔
5106
  
5107
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
470,808✔
5108
  if (!msmCheckNeedHealthCheck(pMnode)) {
470,808✔
5109
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5110
    return;
×
5111
  }
5112
  
5113
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
470,808✔
5114
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
470,808✔
5115

5116
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
470,808✔
5117
  
5118
  msmCheckStreamsStatus(pMnode);
470,808✔
5119
  msmCheckTasksStatus(pMnode);
470,808✔
5120
  msmCheckSnodesState(pMnode);
470,808✔
5121

5122
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
470,808✔
5123

5124
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
470,808✔
5125
}
5126

5127
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
1,764✔
5128
  SStreamObj *pStream = pObj;
1,764✔
5129
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
1,764✔
5130
    return true;
×
5131
  }
5132
  
5133
  pStream->updateTime = *(int64_t*)p1;
1,764✔
5134
  
5135
  (*(int32_t*)p2)++;
1,764✔
5136
  
5137
  return true;
1,764✔
5138
}
5139

5140
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5141
  int32_t code = 0;
×
5142
  int8_t  stopped = 0;
×
5143
  
5144
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5145
  
5146
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5147
  if (NULL == pStatus) {
×
5148
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5149
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5150
    goto _exit;
×
5151
  }
5152

5153
  stopped = atomic_load_8(&pStatus->stopped);
×
5154
  if (stopped) {
×
5155
    mstsError("stream already stopped, stopped:%d", stopped);
×
5156
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5157
    goto _exit;
×
5158
  }
5159

5160
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5161
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5162
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5163
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5164
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5165
    goto _exit;
×
5166
  }
5167

5168
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
×
5169
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5170

5171
_exit:
×
5172
  
5173
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5174

5175
  return code;
×
5176
}
5177

5178
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
422,435✔
5179
  int32_t code = TSDB_CODE_SUCCESS;
422,435✔
5180
  int32_t lino = 0;
422,435✔
5181
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
422,435✔
5182
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
422,435✔
5183
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
422,435✔
5184

5185
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
843,763✔
5186

5187
  mStreamMgmt.stat.activeTimes++;
422,435✔
5188
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
422,435✔
5189
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
422,435✔
5190
  if (NULL == mStreamMgmt.tCtx) {
422,435✔
5191
    code = terrno;
×
5192
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5193
    goto _exit;
×
5194
  }
5195

5196
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
422,435✔
5197
  if (mStreamMgmt.actionQ == NULL) {
422,435✔
5198
    code = terrno;
×
5199
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5200
    goto _exit;
×
5201
  }
5202
  
5203
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
422,435✔
5204
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
422,435✔
5205
  
5206
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
422,435✔
5207
  
5208
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,534,244✔
5209
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,111,809✔
5210

5211
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,670,854✔
5212
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,559,045✔
5213
      if (pCtx->deployStm[m] == NULL) {
10,559,045✔
5214
        code = terrno;
×
5215
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5216
        goto _exit;
×
5217
      }
5218
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
10,559,045✔
5219
      
5220
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,559,045✔
5221
      if (pCtx->actionStm[m] == NULL) {
10,559,045✔
5222
        code = terrno;
×
5223
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5224
        goto _exit;
×
5225
      }
5226
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
10,559,045✔
5227
    }
5228
  }
5229
  
5230
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
422,435✔
5231
  if (mStreamMgmt.streamMap == NULL) {
422,435✔
5232
    code = terrno;
×
5233
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5234
    goto _exit;
×
5235
  }
5236
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
422,435✔
5237
  
5238
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
422,435✔
5239
  if (mStreamMgmt.taskMap == NULL) {
422,435✔
5240
    code = terrno;
×
5241
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5242
    goto _exit;
×
5243
  }
5244
  
5245
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
422,435✔
5246
  if (mStreamMgmt.vgroupMap == NULL) {
422,435✔
5247
    code = terrno;
×
5248
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5249
    goto _exit;
×
5250
  }
5251
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
422,435✔
5252

5253
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
422,435✔
5254
  if (mStreamMgmt.snodeMap == NULL) {
422,435✔
5255
    code = terrno;
×
5256
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5257
    goto _exit;
×
5258
  }
5259
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
422,435✔
5260
  
5261
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
422,435✔
5262
  if (mStreamMgmt.dnodeMap == NULL) {
422,435✔
5263
    code = terrno;
×
5264
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5265
    goto _exit;
×
5266
  }
5267

5268
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
422,435✔
5269
  if (mStreamMgmt.toDeployVgMap == NULL) {
422,435✔
5270
    code = terrno;
×
5271
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5272
    goto _exit;
×
5273
  }
5274
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
422,435✔
5275
  
5276
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
422,435✔
5277
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
422,435✔
5278
    code = terrno;
×
5279
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5280
    goto _exit;
×
5281
  }
5282
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
422,435✔
5283

5284
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
422,435✔
5285
  if (mStreamMgmt.toUpdateScanMap == NULL) {
422,435✔
5286
    code = terrno;
×
5287
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5288
    goto _exit;
×
5289
  }
5290
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
422,435✔
5291

5292
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
422,435✔
5293
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
422,435✔
5294

5295
  mStreamMgmt.lastTaskId = 1;
422,435✔
5296

5297
  int32_t activeStreamNum = 0;
422,435✔
5298
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
422,435✔
5299

5300
  if (activeStreamNum > 0) {
422,435✔
5301
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
252✔
5302
  } else {
5303
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
422,183✔
5304
  }
5305

5306
_exit:
422,435✔
5307

5308
  if (code) {
422,435✔
5309
    msmDestroyRuntimeInfo(pMnode);
×
5310
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5311
  } else {
5312
    mstInfo("mnode stream runtime init done");
422,435✔
5313
  }
5314

5315
  return code;
422,435✔
5316
}
5317

5318

5319

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