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

taosdata / TDengine / #4940

27 Jan 2026 10:23AM UTC coverage: 66.832% (-0.1%) from 66.931%
#4940

push

travis-ci

web-flow
fix: asan invalid write issue (#34400)

7 of 8 new or added lines in 2 files covered. (87.5%)

822 existing lines in 141 files now uncovered.

204293 of 305680 relevant lines covered (66.83%)

124534723.83 hits per line

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

76.67
/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() {
727,758✔
34
  SStmQNode* pQNode = NULL;
727,758✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
727,758✔
37
    return;
363,879✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,819,205✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,915,230✔
49
    taosHashCleanup(pCtx->deployStm[m]);
9,096,025✔
50
    taosHashCleanup(pCtx->actionStm[m]);
9,096,025✔
51
  }
52
}
1,819,205✔
53

54
void msmDestroyThreadCtxs() {
727,758✔
55
  if (NULL == mStreamMgmt.tCtx) {
727,758✔
56
    return;
363,879✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,183,084✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,819,205✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
363,879✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
727,758✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
727,758✔
94
}
727,758✔
95

96

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

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

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

111
    pStatus = pStream;
1,057✔
112
  }
113

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

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

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

136
_exit:
428✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
5,327✔
139

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

145

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

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

200
  pSnode = NULL;
384,737✔
201

202
_exit:
384,737✔
203

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

208
  return code;
384,737✔
209
}
210

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

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

235
  pDnode = NULL;
827,624✔
236

237
_exit:
827,624✔
238

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

243
  return code;
827,624✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
487,489✔
275
  int32_t code = TSDB_CODE_SUCCESS;
487,489✔
276
  int32_t lino = 0;
487,489✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
487,489✔
278
  if (NULL == pStream) {
487,489✔
279
    SStmVgStreamStatus stream = {0};
364,771✔
280
    if (trigReader) {
364,771✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
223,124✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
223,124✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
446,248✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
141,647✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
141,647✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
283,294✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
364,771✔
290
    goto _exit;
364,771✔
291
  }
292
  
293
  if (trigReader) {
122,718✔
294
    if (NULL == pStream->trigReaders) {
21,553✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
21,553✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
21,553✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
43,106✔
300
    goto _exit;
21,553✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
101,165✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
72,825✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
72,825✔
306
  }
307

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

310
_exit:
101,165✔
311

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

320
  return code;
487,489✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
58,570✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
58,570✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
58,570✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
428,919✔
339
  }
340
  
341
_exit:
487,383✔
342

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

350
  return code;
487,489✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
488,509✔
363
    if (NULL == pVg) {
488,509✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
125,217✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
125,217✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
250,434✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
125,217✔
368
      if (TSDB_CODE_SUCCESS == code) {
125,217✔
369
        goto _return;
125,217✔
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);
363,292✔
381
    if (NULL == pVg->taskList) {
363,292✔
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)) {
726,584✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
363,292✔
390
    
391
    taosHashRelease(pVgMap, pVg);
363,292✔
392
    break;
363,292✔
393
  }
394
  
395
_return:
488,509✔
396

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

404
  return code;
488,509✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
638,008✔
409
  int32_t code = TSDB_CODE_SUCCESS;
638,008✔
410
  int32_t lino = 0;
638,008✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
638,008✔
412
  if (NULL == pStream) {
638,008✔
413
    SStmSnodeStreamStatus stream = {0};
162,269✔
414
    if (deployId < 0) {
162,269✔
415
      stream.trigger = pStatus;
3,082✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
159,187✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
159,187✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
318,374✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
162,269✔
423
    goto _exit;
162,269✔
424
  }
425
  
426
  if (deployId < 0) {
475,739✔
427
    if (NULL != pStream->trigger) {
153,842✔
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;
153,842✔
433
    goto _exit;
153,842✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
321,897✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
303,084✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
303,084✔
439
  }
440

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

443
_exit:
321,897✔
444

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

453
  return code;
638,008✔
454
}
455

456

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

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

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

484
  return code;
638,008✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
156,924✔
551
}
552

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

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

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

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

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

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

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

614
  return code;
481,084✔
615
}
616

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

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

632
_exit:
462,271✔
633

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

638
  return code;
462,271✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
12,028✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
216✔
682

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

687
  return code;  
216✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
69,900✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
17,625,299✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
70,547,701✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
52,922,402✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
52,922,402✔
725
  }
726

727
_exit:
17,625,299✔
728

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

733
  return code;
17,625,299✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
304,322✔
739
  int32_t num = taosArrayGetSize(pList);
304,322✔
740
  for (int32_t i = 0; i < num; ++i) {
877,221✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
730,212✔
742
    if (pScan->readFromCache) {
730,212✔
743
      return pScan->scanPlan;
157,313✔
744
    }
745
  }
746

747
  return NULL;
147,009✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
488,509✔
775
}
776

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

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

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

805
_exit:
153,842✔
806

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

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

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

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

875
  SStreamTaskAddr addr;
156,553✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
156,924✔
877
  if (triggerReaderNum > 0) {
156,924✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
155,993✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
155,993✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
345,259✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
188,335✔
884
    addr.taskId = pStatus->id.taskId;
188,335✔
885
    addr.nodeId = pStatus->id.nodeId;
188,335✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
188,335✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
376,670✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
188,335✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
156,924✔
892
  pMsg->streamName = pInfo->streamName;
156,924✔
893

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

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

901
_exit:
153,842✔
902

903
  if (code) {
153,842✔
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));
153,842✔
907
  }
908
  
909
  return TSDB_CODE_SUCCESS;
153,842✔
910
}
911

912

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

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

946
  pMsg->colCids = pInfo->pCreate->colCids;
481,084✔
947
  pMsg->tagCids = pInfo->pCreate->tagCids;
481,084✔
948
_exit:
481,084✔
949

950
  if (code) {
481,084✔
951
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
952
  }
953
  
954
  return code;
481,084✔
955
}
956

957

958
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
372,489✔
959
  int32_t code = TSDB_CODE_SUCCESS;
372,489✔
960
  int32_t lino = 0;
372,489✔
961
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
372,489✔
962
  SListNode* pNode = pList ? listHead(pList) : NULL;
372,489✔
963
  
964
  for (int32_t i = 0; i < taskNum; ++i) {
859,978✔
965
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
487,489✔
966
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
487,489✔
967
    if (pNode) {
487,489✔
968
      pNode = TD_DLIST_NODE_NEXT(pNode);
240,513✔
969
    }
970
  }
971
  
972
_exit:
372,489✔
973

974
  if (code) {
372,489✔
975
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
976
  }
977
  
978
  return code;
372,489✔
979
}
980

981

982
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
619,195✔
983
  int32_t code = TSDB_CODE_SUCCESS;
619,195✔
984
  int32_t lino = 0;
619,195✔
985
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
619,195✔
986
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
619,195✔
987
  
988
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,257,203✔
989
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
638,008✔
990
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
638,008✔
991
  }
992
  
993
_exit:
619,195✔
994

995
  if (code) {
619,195✔
996
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
997
  }
998

999
  return code;
619,195✔
1000
}
1001

1002
int64_t msmAssignTaskId(void) {
1,124,752✔
1003
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,124,752✔
1004
}
1005

1006
int64_t msmAssignTaskSeriousId(void) {
1,124,752✔
1007
  return taosGetTimestampNs();
1,124,752✔
1008
}
1009

1010

1011
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
796,515✔
1012
  int32_t code = TSDB_CODE_SUCCESS;
796,515✔
1013
  int32_t lino = 0;
796,515✔
1014
  bool     noExists = false;
796,515✔
1015
  SStmSnodeStatus* pStatus = NULL;
796,515✔
1016

1017
  while (true) {
1018
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
803,339✔
1019
    if (NULL == pStatus) {
803,339✔
1020
      if (noExists) {
6,824✔
1021
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1022
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1023
      }
1024

1025
      noExists = true;
6,824✔
1026
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
6,824✔
1027
      
1028
      continue;
6,824✔
1029
    }
1030

1031
    *alive = (pStatus->runnerThreadNum >= 0);
796,515✔
1032
    break;
796,515✔
1033
  }
1034

1035
_exit:
796,515✔
1036

1037
  if (code) {
796,515✔
1038
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1039
  }
1040

1041
  return code;
796,515✔
1042
}
1043

1044
int32_t msmRetrieveStaticSnodeId(SMnode* pMnode, SStreamObj* pStream) {
311,511✔
1045
  int32_t code = TSDB_CODE_SUCCESS;
311,511✔
1046
  int32_t lino = 0;
311,511✔
1047
  bool alive = false;
311,511✔
1048
  int32_t mainSnodeId = atomic_load_32(&pStream->mainSnodeId);
311,511✔
1049
  int32_t snodeId = mainSnodeId;
311,511✔
1050
  int64_t streamId = pStream->pCreate->streamId;
311,511✔
1051
  
1052
  while (true) {
1053
    TAOS_CHECK_EXIT(msmIsSnodeAlive(pMnode, snodeId, streamId, &alive));
311,511✔
1054

1055
    if (alive) {
311,511✔
1056
      return snodeId;
311,511✔
1057
    }
1058
    
UNCOV
1059
    if (snodeId == mainSnodeId) {
×
UNCOV
1060
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
UNCOV
1061
      if (NULL == pSnode) {
×
1062
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1063
        return 0;
×
1064
      }
1065
      
UNCOV
1066
      if (pSnode->replicaId <= 0) {
×
1067
        mstsError("no available snode now, mainSnodeId:%d, replicaId:%d", mainSnodeId, pSnode->replicaId);
×
1068
        mndReleaseSnode(pMnode, pSnode);
×
1069
        return 0;
×
1070
      }
1071

UNCOV
1072
      snodeId = pSnode->replicaId;
×
UNCOV
1073
      mndReleaseSnode(pMnode, pSnode);
×
1074
      
UNCOV
1075
      continue;
×
1076
    }
1077

1078
    mstsError("no available snode now, mainSnodeId:%d, followerSnodeId:%d", mainSnodeId, snodeId);
×
1079
    return 0;
×
1080
  }
1081

1082
_exit:
×
1083

1084
  if (code) {
×
1085
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1086
  }
1087

1088
  return 0;
×
1089
}
1090

1091
int32_t msmAssignRandomSnodeId(SMnode* pMnode, int64_t streamId) {
466,174✔
1092
  int32_t code = TSDB_CODE_SUCCESS;
466,174✔
1093
  int32_t lino = 0;
466,174✔
1094
  int32_t snodeIdx = 0;
466,174✔
1095
  int32_t snodeId = 0;
466,174✔
1096
  void      *pIter = NULL;
466,174✔
1097
  SSnodeObj *pObj = NULL;
466,174✔
1098
  bool alive = false;
466,174✔
1099
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
466,174✔
1100
  if (snodeNum <= 0) {
466,174✔
1101
    mstsInfo("no available snode now, num:%d", snodeNum);
2,410✔
1102
    goto _exit;
2,410✔
1103
  }
1104

1105
  int32_t snodeTarget = taosRand() % snodeNum;
463,764✔
1106

1107
  while (1) {
1108
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
485,004✔
1109
    if (pIter == NULL) {
485,004✔
UNCOV
1110
      if (0 == snodeId) {
×
1111
        mstsError("no alive snode now, snodeNum:%d", snodeNum);
×
1112
        break;
×
1113
      }
1114
      
UNCOV
1115
      snodeId = 0;
×
UNCOV
1116
      continue;
×
1117
    }
1118

1119
    code = msmIsSnodeAlive(pMnode, pObj->id, streamId, &alive);
485,004✔
1120
    if (code) {
485,004✔
1121
      sdbRelease(pMnode->pSdb, pObj);
×
1122
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1123
      pObj = NULL;
×
1124
      TAOS_CHECK_EXIT(code);
×
1125
    }
1126
    
1127
    if (!alive) {
485,004✔
UNCOV
1128
      sdbRelease(pMnode->pSdb, pObj);
×
UNCOV
1129
      continue;
×
1130
    }
1131

1132
    snodeId = pObj->id;
485,004✔
1133
    if (snodeIdx == snodeTarget) {
485,004✔
1134
      sdbRelease(pMnode->pSdb, pObj);
463,764✔
1135
      sdbCancelFetch(pMnode->pSdb, pIter);
463,764✔
1136
      pObj = NULL;
463,764✔
1137
      goto _exit;
463,764✔
1138
    }
1139

1140
    sdbRelease(pMnode->pSdb, pObj);
21,240✔
1141
    snodeIdx++;
21,240✔
1142
  }
1143

1144
_exit:
466,174✔
1145

1146
  if (code) {
466,174✔
1147
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1148
  }
1149

1150
  if (0 == snodeId) {
466,174✔
1151
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,410✔
1152
  }
1153

1154
  return snodeId;
466,174✔
1155
}
1156

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

1166
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
619,195✔
1167

1168
_exit:
619,195✔
1169

1170
  if (0 == snodeId) {
619,195✔
1171
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1172
  }
1173

1174
  return snodeId;
619,195✔
1175
}
1176

1177

1178
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
156,924✔
1179
  int32_t code = TSDB_CODE_SUCCESS;
156,924✔
1180
  int32_t lino = 0;
156,924✔
1181
  int64_t streamId = pStream->pCreate->streamId;
156,924✔
1182

1183
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
156,924✔
1184
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
156,924✔
1185

1186
  pInfo->triggerTask->id.taskId = pCtx->triggerTaskId;
156,924✔
1187
  pInfo->triggerTask->id.deployId = 0;
156,924✔
1188
  pInfo->triggerTask->id.seriousId = msmAssignTaskSeriousId();
156,924✔
1189
  pInfo->triggerTask->id.nodeId = pCtx->triggerNodeId;
156,924✔
1190
  pInfo->triggerTask->id.taskIdx = 0;
156,924✔
1191
  pInfo->triggerTask->type = STREAM_TRIGGER_TASK;
156,924✔
1192
  pInfo->triggerTask->lastUpTs = pCtx->currTs;
156,924✔
1193
  pInfo->triggerTask->pStream = pInfo;
156,924✔
1194

1195
  SStmTaskDeploy info = {0};
156,924✔
1196
  info.task.type = pInfo->triggerTask->type;
156,924✔
1197
  info.task.streamId = streamId;
156,924✔
1198
  info.task.taskId =  pInfo->triggerTask->id.taskId;
156,924✔
1199
  info.task.seriousId = pInfo->triggerTask->id.seriousId;
156,924✔
1200
  info.task.nodeId =  pInfo->triggerTask->id.nodeId;
156,924✔
1201
  info.task.taskIdx =  pInfo->triggerTask->id.taskIdx;
156,924✔
1202
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pInfo, &info, pStream));
156,924✔
1203
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
156,924✔
1204
  
1205
  (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
156,924✔
1206

1207
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
156,924✔
1208
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
156,924✔
1209

1210
_exit:
156,924✔
1211

1212
  if (code) {
156,924✔
1213
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1214
  }
1215

1216
  return code;
156,924✔
1217
}
1218

1219
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
244,677✔
1220
  int32_t code = TSDB_CODE_SUCCESS;
244,677✔
1221
  int32_t lino = 0;
244,677✔
1222

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

1245
_exit:
244,677✔
1246

1247
  if (code) {
244,677✔
1248
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1249
  }
1250

1251
  return code;
244,677✔
1252
}
1253

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

1283
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
66,162✔
1284
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
66,162✔
1285
      
1286
      void *pIter = NULL;
66,162✔
1287
      while (1) {
367,140✔
1288
        SStmTaskDeploy info = {0};
433,302✔
1289
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
433,302✔
1290
        if (pIter == NULL) {
433,302✔
1291
          break;
66,162✔
1292
        }
1293
      
1294
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
367,140✔
1295
          pState = taosArrayReserve(pInfo->trigReaders, 1);
98,504✔
1296

1297
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
98,504✔
1298
          if (code) {
98,504✔
1299
            sdbRelease(pSdb, pVgroup);
×
1300
            sdbCancelFetch(pSdb, pIter);
×
1301
            pVgroup = NULL;
×
1302
            TAOS_CHECK_EXIT(code);
×
1303
          }
1304
        }
1305

1306
        sdbRelease(pSdb, pVgroup);
367,140✔
1307
      }
1308
      break;
66,162✔
1309
    }
1310
    default:
931✔
1311
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
931✔
1312
      break;
931✔
1313
  }
1314

1315
_exit:
156,924✔
1316

1317
  mndReleaseDb(pCtx->pMnode, pDb);
156,924✔
1318

1319
  if (code) {
156,924✔
1320
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1321
  }
1322

1323
  return code;
156,924✔
1324
}
1325

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

1349
  key[1] = pSubplan->id.subplanId;
241,258✔
1350

1351
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
241,258✔
1352
  if (NULL == ppRes) {
241,258✔
1353
    SArray* pRes = taosArrayInit(1, sizeof(addr));
241,258✔
1354
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
241,258✔
1355
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
482,516✔
1356
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
241,258✔
1357
  } else {
1358
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1359
  }
1360

1361
  mstsDebug("calcReader %" PRIx64 " added to toUpdateScan, vgId:%d, groupId:%d, subplanId:%d", taskId, vgId, pSubplan->id.groupId, pSubplan->id.subplanId);
241,258✔
1362
  
1363
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
241,258✔
1364
  
1365
_exit:
241,258✔
1366

1367
  nodesDestroyNode((SNode*)pSubplan);
241,258✔
1368

1369
  if (code) {
241,258✔
1370
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1371
  }
1372

1373
  return code;
241,258✔
1374
}
1375

1376
int32_t msmUPAddCacheTask(SStmGrpCtx* pCtx, SStreamCalcScan* pScan, SStreamObj* pStream) {
71,105✔
1377
  int32_t code = TSDB_CODE_SUCCESS;
71,105✔
1378
  int32_t lino = 0;
71,105✔
1379
  SSubplan* pSubplan = NULL;
71,105✔
1380
  int64_t streamId = pStream->pCreate->streamId;
71,105✔
1381
  int64_t key[2] = {streamId, 0};
71,105✔
1382
  TAOS_CHECK_EXIT(nodesStringToNode(pScan->scanPlan, (SNode**)&pSubplan));
71,105✔
1383

1384
  SStmTaskSrcAddr addr;
71,105✔
1385
  addr.isFromCache = true;
71,105✔
1386
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
71,105✔
1387
  addr.taskId = pCtx->triggerTaskId;
71,105✔
1388
  addr.vgId = pCtx->triggerNodeId;
71,105✔
1389
  addr.groupId = pSubplan->id.groupId;
71,105✔
1390

1391
  key[1] = pSubplan->id.subplanId;
71,105✔
1392
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
71,105✔
1393
  if (NULL == ppRes) {
71,105✔
1394
    SArray* pRes = taosArrayInit(1, sizeof(addr));
71,105✔
1395
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
71,105✔
1396
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
142,210✔
1397
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
71,105✔
1398
  } else {
1399
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1400
  }
1401
  
1402
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
71,105✔
1403
  
1404
_exit:
71,105✔
1405

1406
  nodesDestroyNode((SNode*)pSubplan);
71,105✔
1407
  
1408
  if (code) {
71,105✔
1409
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1410
  }
1411

1412
  return code;
71,105✔
1413
}
1414

1415

1416
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
242,812✔
1417
  int32_t code = TSDB_CODE_SUCCESS;
242,812✔
1418
  int32_t lino = 0;
242,812✔
1419

1420
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
242,812✔
1421

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

1444
_exit:
242,812✔
1445

1446
  if (code) {
242,812✔
1447
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1448
  }
1449

1450
  return code;
242,812✔
1451
}
1452

1453

1454
static int32_t msmTDAddCalcReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
156,924✔
1455
  int32_t code = TSDB_CODE_SUCCESS;
156,924✔
1456
  int32_t lino = 0;
156,924✔
1457
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
156,924✔
1458
  int64_t streamId = pStream->pCreate->streamId;
156,924✔
1459
  SStmTaskStatus* pState = NULL;
156,924✔
1460
  pInfo->calcReaders = tdListNew(sizeof(SStmTaskStatus));
156,924✔
1461
  TSDB_CHECK_NULL(pInfo->calcReaders, code, lino, _exit, terrno);
156,924✔
1462

1463
  
1464
  for (int32_t i = 0; i < calcTasksNum; ++i) {
468,542✔
1465
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
311,618✔
1466
    if (pScan->readFromCache) {
311,618✔
1467
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
71,105✔
1468
      continue;
71,105✔
1469
    }
1470
    
1471
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
240,513✔
1472
    for (int32_t m = 0; m < vgNum; ++m) {
481,026✔
1473
      pState = tdListReserve(pInfo->calcReaders);
240,513✔
1474
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
240,513✔
1475

1476
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
240,513✔
1477
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
240,513✔
1478
    }
1479
  }
1480

1481
_exit:
156,924✔
1482

1483
  if (code) {
156,924✔
1484
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1485
  }
1486

1487
  return code;
156,924✔
1488
}
1489

1490

1491

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

1519
_exit:
745✔
1520

1521
  if (code) {
745✔
1522
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1523
  }
1524

1525
  return code;
745✔
1526
}
1527

1528
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
156,924✔
1529
  int32_t code = TSDB_CODE_SUCCESS;
156,924✔
1530
  int32_t lino = 0;
156,924✔
1531
  int64_t streamId = pStream->pCreate->streamId;
156,924✔
1532
  
1533
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
156,924✔
1534
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
156,924✔
1535

1536
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
156,924✔
1537
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
156,924✔
1538
  
1539
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
156,924✔
1540
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
156,924✔
1541
  
1542
_exit:
156,924✔
1543

1544
  if (code) {
156,924✔
1545
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1546
  }
1547
  
1548
  return code;
156,924✔
1549
}
1550

1551
int32_t msmUpdatePlanSourceAddr(SStreamTask* pTask, int64_t streamId, SSubplan* plan, int64_t clientId, SStmTaskSrcAddr* pSrc, int32_t msgType, int64_t srcSubplanId) {
954,412✔
1552
  SDownstreamSourceNode source = {
954,412✔
1553
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1554
      .clientId = clientId,
1555
      .taskId = pSrc->taskId,
954,412✔
1556
      .sId = 0,
1557
      .execId = 0,
1558
      .fetchMsgType = msgType,
1559
      .localExec = false,
1560
  };
1561

1562
  source.addr.epSet = pSrc->epset;
954,412✔
1563
  source.addr.nodeId = pSrc->vgId;
954,412✔
1564

1565
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
954,412✔
1566
      plan->id.subplanId, pSrc->groupId, srcSubplanId, source.clientId, source.taskId, source.addr.nodeId, TMSG_INFO(source.fetchMsgType));
1567
  
1568
  return qSetSubplanExecutionNode(plan, pSrc->groupId, &source);
954,412✔
1569
}
1570

1571
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
18,813✔
1572
  int64_t streamId = pStream->pCreate->streamId;
18,813✔
1573
  int32_t runnerNum = taosArrayGetSize(pRunners);
18,813✔
1574
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
47,955✔
1575
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
47,955✔
1576
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
47,955✔
1577
    if (pPlan->id.subplanId == subplanId) {
47,955✔
1578
      *taskId = pDeploy->task.taskId;
18,813✔
1579
      *ppParent = &pDeploy->task;
18,813✔
1580
      return TSDB_CODE_SUCCESS;
18,813✔
1581
    }
1582
  }
1583

1584
  mstsError("subplanId %d not found in runner list", subplanId);
×
1585

1586
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1587
}
1588

1589
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
481,084✔
1590
  int32_t code = TSDB_CODE_SUCCESS;
481,084✔
1591
  int32_t lino = 0;
481,084✔
1592
  int64_t key[2] = {streamId, -1};
481,084✔
1593
  SNode* pNode = NULL;
481,084✔
1594
  SStreamTask* pTask = &pDeploy->task;
481,084✔
1595
  FOREACH(pNode, pPlan->pChildren) {
1,435,496✔
1596
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
954,412✔
1597
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
18,813✔
1598
      continue;
18,813✔
1599
    }
1600
    
1601
    SValueNode* pVal = (SValueNode*)pNode;
935,599✔
1602
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
935,599✔
1603
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1604
      continue;
×
1605
    }
1606

1607
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
935,599✔
1608

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

1615
    int32_t childrenNum = taosArrayGetSize(*ppRes);
935,599✔
1616
    for (int32_t i = 0; i < childrenNum; ++i) {
1,871,198✔
1617
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
935,599✔
1618
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
935,599✔
1619
    }
1620
  }
1621

1622
_exit:
481,084✔
1623

1624
  if (code) {
481,084✔
1625
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1626
  }
1627

1628
  return code;
481,084✔
1629
}
1630

1631
int32_t msmUpdateRunnerPlan(SStmGrpCtx* pCtx, SArray* pRunners, int32_t beginIdx, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
481,084✔
1632
  int32_t code = TSDB_CODE_SUCCESS;
481,084✔
1633
  int32_t lino = 0;
481,084✔
1634
  SSubplan* pPlan = pDeploy->msg.runner.pPlan;
481,084✔
1635
  SStreamTask* pTask = &pDeploy->task;
481,084✔
1636
  SStreamTask* parentTask = NULL;
481,084✔
1637
  int64_t streamId = pStream->pCreate->streamId;
481,084✔
1638

1639
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
481,084✔
1640

1641
  SNode* pTmp = NULL;
481,084✔
1642
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,435,496✔
1643
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
954,412✔
1644
      ERASE_NODE(pPlan->pChildren);
935,599✔
1645
      continue;
935,599✔
1646
    }
1647
    WHERE_NEXT;
18,813✔
1648
  }
1649
  nodesClearList(pPlan->pChildren);
481,084✔
1650
  pPlan->pChildren = NULL;
481,084✔
1651

1652
  if (NULL == pPlan->pParents) {
481,084✔
1653
    goto _exit;
462,271✔
1654
  }
1655

1656
  SNode* pNode = NULL;
18,813✔
1657
  int64_t parentTaskId = 0;
18,813✔
1658
  SStmTaskSrcAddr addr = {0};
18,813✔
1659
  addr.taskId = pDeploy->task.taskId;
18,813✔
1660
  addr.vgId = pDeploy->task.nodeId;
18,813✔
1661
  addr.groupId = pPlan->id.groupId;
18,813✔
1662
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
18,813✔
1663
  FOREACH(pNode, pPlan->pParents) {
37,626✔
1664
    SSubplan* pSubplan = (SSubplan*)pNode;
18,813✔
1665
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
18,813✔
1666
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
18,813✔
1667
  }
1668
  
1669
_exit:
479,971✔
1670

1671
  if (code) {
481,084✔
1672
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1673
  }
1674

1675
  return code;
481,084✔
1676
}
1677

1678
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
462,271✔
1679
  int32_t code = TSDB_CODE_SUCCESS;
462,271✔
1680
  int32_t lino = 0;
462,271✔
1681
  int64_t streamId = pStream->pCreate->streamId;
462,271✔
1682
  int32_t runnerNum = taosArrayGetSize(pRunners);
462,271✔
1683
  
1684
  for (int32_t i = 0; i < runnerNum; ++i) {
943,355✔
1685
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
481,084✔
1686
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
481,084✔
1687
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
481,084✔
1688

1689
    SStreamTask* pTask = &pDeploy->task;
481,084✔
1690
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
481,084✔
1691
  }
1692

1693
_exit:
462,271✔
1694

1695
  if (code) {
462,271✔
1696
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1697
  }
1698

1699
  return code;
462,271✔
1700
}
1701

1702
int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream) {
153,842✔
1703
  int32_t code = 0;
153,842✔
1704
  int32_t lino = 0;
153,842✔
1705
  int64_t streamId = pStream->pCreate->streamId;
153,842✔
1706
  SArray* deployTaskList = NULL;
153,842✔
1707
  SArray* deployList = NULL;
153,842✔
1708
  int32_t deployNodeId = 0;
153,842✔
1709
  SStmTaskStatus* pState = NULL;
153,842✔
1710
  int32_t taskIdx = 0;
153,842✔
1711
  SNodeListNode *plans = NULL;
153,842✔
1712
  int32_t        taskNum = 0;
153,842✔
1713
  int32_t        totalTaskNum = 0;
153,842✔
1714

1715
  if (pDag->numOfSubplans <= 0) {
153,842✔
1716
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1717
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1718
  }
1719

1720
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
153,842✔
1721
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1722
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1723
  }
1724

1725
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
153,842✔
1726
  if (levelNum <= 0) {
153,842✔
1727
    mstsError("invalid level num:%d", levelNum);
×
1728
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1729
  }
1730

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

1745
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
153,842✔
1746
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
153,842✔
1747
  
1748
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
615,368✔
1749
    totalTaskNum = 0;
461,526✔
1750

1751
    deployList = pInfo->runners[deployId];
461,526✔
1752
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
461,526✔
1753
    if (!GOT_SNODE(deployNodeId)) {
461,526✔
1754
      TAOS_CHECK_EXIT(terrno);
×
1755
    }
1756

1757
    taskIdx = 0;
461,526✔
1758
    
1759
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
932,151✔
1760
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
470,625✔
1761
      if (NULL == plans) {
470,625✔
1762
        mstsError("empty level plan, level:%d", i);
×
1763
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1764
      }
1765

1766
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
470,625✔
1767
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1768
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1769
      }
1770

1771
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
470,625✔
1772
      if (taskNum <= 0) {
470,625✔
1773
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1774
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1775
      }
1776

1777
      totalTaskNum += taskNum;
470,625✔
1778
      if (totalTaskNum > pDag->numOfSubplans) {
470,625✔
1779
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1780
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1781
      }
1782

1783
      for (int32_t n = 0; n < taskNum; ++n) {
950,964✔
1784
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
480,339✔
1785
        pState = taosArrayReserve(deployList, 1);
480,339✔
1786

1787
        pState->id.taskId = msmAssignTaskId();
480,339✔
1788
        pState->id.deployId = deployId;
480,339✔
1789
        pState->id.seriousId = msmAssignTaskSeriousId();
480,339✔
1790
        pState->id.nodeId = deployNodeId;
480,339✔
1791
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
480,339✔
1792
        pState->type = STREAM_RUNNER_TASK;
480,339✔
1793
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
480,339✔
1794
        pState->status = STREAM_STATUS_UNDEPLOYED;
480,339✔
1795
        pState->lastUpTs = pCtx->currTs;
480,339✔
1796
        pState->pStream = pInfo;
480,339✔
1797

1798
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
480,339✔
1799
        pDeploy->task.type = pState->type;
480,339✔
1800
        pDeploy->task.streamId = streamId;
480,339✔
1801
        pDeploy->task.taskId = pState->id.taskId;
480,339✔
1802
        pDeploy->task.flags = pState->flags;
480,339✔
1803
        pDeploy->task.seriousId = pState->id.seriousId;
480,339✔
1804
        pDeploy->task.deployId = pState->id.deployId;
480,339✔
1805
        pDeploy->task.nodeId = pState->id.nodeId;
480,339✔
1806
        pDeploy->task.taskIdx = pState->id.taskIdx;
480,339✔
1807
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
480,339✔
1808

1809
        SStreamTask* pTask = &pDeploy->task;
480,339✔
1810
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
480,339✔
1811
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1812
      }
1813

1814
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
470,625✔
1815
    }
1816

1817
    if (totalTaskNum != pDag->numOfSubplans) {
461,526✔
1818
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1819
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1820
    }
1821

1822
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
461,526✔
1823

1824
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
461,526✔
1825

1826
    nodesDestroyNode((SNode *)pDag);
461,526✔
1827
    pDag = NULL;
461,526✔
1828
    
1829
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
461,526✔
1830

1831
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
461,526✔
1832
  }
1833

1834
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
615,368✔
1835
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
461,526✔
1836
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
461,526✔
1837
  }
1838
  
1839
  pInfo->runnerNum = totalTaskNum;
153,842✔
1840
  
1841
_exit:
153,842✔
1842

1843
  if (code) {
153,842✔
1844
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1845
  }
1846

1847
  taosArrayDestroy(deployTaskList);
153,842✔
1848
  nodesDestroyNode((SNode *)pDag);
153,842✔
1849

1850
  return code;
153,842✔
1851
}
1852

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

1870
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
1,490✔
1871
    deployId = pAction->deployId[r];
745✔
1872

1873
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
745✔
1874

1875
    pStartRunner = pRunner;
745✔
1876
    totalTaskNum = 0;
745✔
1877

1878
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
745✔
1879
    if (!GOT_SNODE(newNodeId)) {
745✔
1880
      TAOS_CHECK_EXIT(terrno);
×
1881
    }
1882

1883
    taskIdx = 0;
745✔
1884
    
1885
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,490✔
1886
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
745✔
1887
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
745✔
1888
      totalTaskNum += taskNum;
745✔
1889

1890
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
745✔
1891
      
1892
      for (int32_t n = 0; n < taskNum; ++n) {
1,490✔
1893
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
745✔
1894

1895
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
745✔
1896
        if (pRunner->id.taskIdx != newTaskIdx) {
745✔
1897
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1898
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1899
        }
1900

1901
        pRunner->id.nodeId = newNodeId;
745✔
1902

1903
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
745✔
1904
        pDeploy->task.type = pRunner->type;
745✔
1905
        pDeploy->task.streamId = streamId;
745✔
1906
        pDeploy->task.taskId = pRunner->id.taskId;
745✔
1907
        pDeploy->task.flags = pRunner->flags;
745✔
1908
        pDeploy->task.seriousId = pRunner->id.seriousId;
745✔
1909
        pDeploy->task.nodeId = pRunner->id.nodeId;
745✔
1910
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
745✔
1911
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
745✔
1912

1913
        pRunner++;
745✔
1914
      }
1915

1916
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
745✔
1917
    }
1918

1919
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
745✔
1920

1921
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
745✔
1922

1923
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
745✔
1924

1925
    nodesDestroyNode((SNode *)pDag);
745✔
1926
    pDag = NULL;
745✔
1927

1928
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
745✔
1929
  }
1930

1931
_exit:
745✔
1932

1933
  if (code) {
745✔
1934
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1935
  }
1936

1937
  nodesDestroyNode((SNode *)pDag);
745✔
1938
  taosArrayDestroy(deployTaskList);
745✔
1939

1940
  return code;
745✔
1941
}
1942

1943

1944
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
147,376✔
1945
  int32_t code = TSDB_CODE_SUCCESS;
147,376✔
1946
  int32_t lino = 0;
147,376✔
1947
  //STREAMTODO 
1948
  
1949
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
147,376✔
1950
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
147,376✔
1951

1952
_exit:
147,376✔
1953

1954
  if (code) {
147,376✔
1955
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1956
  }
1957

1958
  return code;
147,376✔
1959
}
1960

1961

1962
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
156,924✔
1963
  if (NULL == pStream->pCreate->calcPlan) {
156,924✔
1964
    return TSDB_CODE_SUCCESS;
3,082✔
1965
  }
1966
  
1967
  int32_t code = TSDB_CODE_SUCCESS;
153,842✔
1968
  int32_t lino = 0;
153,842✔
1969
  int64_t streamId = pStream->pCreate->streamId;
153,842✔
1970
  SQueryPlan* pPlan = NULL;
153,842✔
1971

1972
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
153,842✔
1973

1974
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
615,368✔
1975
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
461,526✔
1976
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
461,526✔
1977
  }
1978

1979
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
153,842✔
1980
  pPlan = NULL;
153,842✔
1981
  
1982
  TAOS_CHECK_EXIT(code);
153,842✔
1983

1984
  taosHashClear(mStreamMgmt.toUpdateScanMap);
153,842✔
1985
  mStreamMgmt.toUpdateScanNum = 0;
153,842✔
1986

1987
_exit:
153,842✔
1988

1989
  nodesDestroyNode((SNode *)pPlan);
153,842✔
1990

1991
  if (code) {
153,842✔
1992
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1993
  }
1994

1995
  return code;
153,842✔
1996
}
1997

1998

1999
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
156,924✔
2000
  int32_t code = TSDB_CODE_SUCCESS;
156,924✔
2001
  int32_t lino = 0;
156,924✔
2002
  int64_t streamId = pStream->pCreate->streamId;
156,924✔
2003

2004
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
156,924✔
2005

2006
  pCtx->triggerTaskId = msmAssignTaskId();
156,924✔
2007
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
156,924✔
2008
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
156,924✔
2009
    TAOS_CHECK_EXIT(terrno);
×
2010
  }
2011

2012
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
156,924✔
2013
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
156,924✔
2014
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
156,924✔
2015
  
2016
_exit:
156,924✔
2017

2018
  if (code) {
156,924✔
2019
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2020
  }
2021

2022
  return code;
156,924✔
2023
}
2024

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

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

2064
_exit:
×
2065

2066
  if (code) {
×
2067
    mndReleaseDb(pCtx->pMnode, pDb);
×
2068
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2069
  }
2070

2071
  return code;
×
2072
}
2073

2074

2075
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
150,458✔
2076
  int32_t code = TSDB_CODE_SUCCESS;
150,458✔
2077
  int32_t lino = 0;
150,458✔
2078
  int64_t streamId = pStream->pCreate->streamId;
150,458✔
2079

2080
  pStatus->lastActionTs = INT64_MIN;
150,458✔
2081

2082
  if (NULL == pStatus->streamName) {
150,458✔
2083
    pStatus->streamName = taosStrdup(pStream->name);
150,458✔
2084
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
150,458✔
2085
  }
2086

2087
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
150,458✔
2088
  
2089
  if (pStream->pCreate->numOfCalcSubplan > 0) {
150,458✔
2090
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
147,376✔
2091
    
2092
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
147,376✔
2093
  }
2094

2095
  if (initList) {
150,458✔
2096
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2097

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

2105
    if (pStatus->runnerNum > 0) {
×
2106
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2107
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2108
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2109
      }
2110
    }
2111
  }
2112
  
2113
_exit:
150,458✔
2114

2115
  if (code) {
150,458✔
2116
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2117
  }
2118

2119
  return code;
150,458✔
2120
}
2121

2122
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
156,924✔
2123
  int32_t code = TSDB_CODE_SUCCESS;
156,924✔
2124
  int32_t lino = 0;
156,924✔
2125
  int64_t streamId = pStream->pCreate->streamId;
156,924✔
2126
  SStmStatus info = {0};
156,924✔
2127

2128
  if (NULL == pStatus) {
156,924✔
2129
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
150,458✔
2130

2131
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
150,458✔
2132

2133
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
150,458✔
2134
  }
2135
  
2136
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
156,924✔
2137

2138
  mstLogSStmStatus("stream deployed", streamId, pStatus);
156,924✔
2139

2140
_exit:
156,924✔
2141

2142
  if (code) {
156,924✔
2143
    if (NULL != pStatus) {
×
2144
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2145
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2146
    }
2147
    
2148
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2149
  }
2150

2151
  return code;
156,924✔
2152
}
2153

2154

2155
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
8,280✔
2156
  int32_t code = TSDB_CODE_SUCCESS;
8,280✔
2157
  void* pIter = NULL;
8,280✔
2158

2159
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
12,117✔
2160
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
3,837✔
2161
    (void)mstWaitLock(&pVg->lock, true);
3,837✔
2162

2163
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
3,837✔
2164
    if (atomic_load_32(&pVg->deployed) == taskNum) {
3,837✔
2165
      taosRUnLockLatch(&pVg->lock);
×
2166
      continue;
×
2167
    }
2168

2169
    for (int32_t i = 0; i < taskNum; ++i) {
19,249✔
2170
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
15,412✔
2171
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
15,412✔
2172
        continue;
13,954✔
2173
      }
2174

2175
      mstDestroySStmTaskToDeployExt(pExt);
1,458✔
2176
      pExt->deployed = true;
1,458✔
2177
    }
2178
    
2179
    taosRUnLockLatch(&pVg->lock);
3,837✔
2180
  }
2181

2182
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
15,207✔
2183
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
6,927✔
2184
    (void)mstWaitLock(&pSnode->lock, true);
6,927✔
2185

2186
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
6,927✔
2187
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
6,927✔
2188
      for (int32_t i = 0; i < taskNum; ++i) {
12,500✔
2189
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
8,003✔
2190
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
8,003✔
2191
          continue;
7,274✔
2192
        }
2193
        
2194
        mstDestroySStmTaskToDeployExt(pExt);
729✔
2195
        pExt->deployed = true;
729✔
2196
      }
2197
    }
2198

2199
    taskNum = taosArrayGetSize(pSnode->runnerList);
6,927✔
2200
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
6,927✔
2201
      for (int32_t i = 0; i < taskNum; ++i) {
32,346✔
2202
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
25,419✔
2203
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
25,419✔
2204
          continue;
23,232✔
2205
        }
2206
        
2207
        mstDestroySStmTaskToDeployExt(pExt);
2,187✔
2208
        pExt->deployed = true;
2,187✔
2209
      }
2210
    }
2211

2212
    taosRUnLockLatch(&pSnode->lock);
6,927✔
2213
  }
2214

2215
  
2216
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
28,320✔
2217
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
20,040✔
2218
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
20,040✔
2219
    if (TSDB_CODE_SUCCESS == code) {
20,040✔
2220
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
11,090✔
2221
    }
2222
  }
2223

2224
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
34,264✔
2225
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
25,984✔
2226
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
25,984✔
2227
    if (TSDB_CODE_SUCCESS == code) {
25,984✔
2228
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
14,575✔
2229
    }
2230
  }
2231

2232
  size_t keyLen = 0;
8,280✔
2233
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
871,004✔
2234
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
862,724✔
2235
    if (*pStreamId == streamId) {
862,724✔
2236
      int64_t taskId = *(pStreamId + 1);
55,314✔
2237
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
55,314✔
2238
      if (code) {
55,314✔
2239
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2240
      } else {
2241
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
55,314✔
2242
      }
2243
    }
2244
  }
2245

2246
  if (fromStreamMap) {
8,280✔
2247
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
1,814✔
2248
    if (code) {
1,814✔
2249
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2250
    } else {
2251
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,814✔
2252
    }
2253
  }
2254
  
2255
  return code;
8,280✔
2256
}
2257

2258
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
6,466✔
2259
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
6,466✔
2260
  
2261
  (void)msmSTRemoveStream(streamId, false);  
6,466✔
2262

2263
  mstResetSStmStatus(pStatus);
6,466✔
2264

2265
  pStatus->deployTimes++;
6,466✔
2266
}
6,466✔
2267

2268
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
158,018✔
2269
  int32_t code = TSDB_CODE_SUCCESS;
158,018✔
2270
  int32_t lino = 0;
158,018✔
2271
  int64_t streamId = pAction->streamId;
158,018✔
2272
  char* streamName = pAction->streamName;
158,018✔
2273
  SStreamObj* pStream = NULL;
158,018✔
2274
  int8_t stopped = 0;
158,018✔
2275

2276
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
158,018✔
2277
  if (pStatus) {
158,018✔
2278
    stopped = atomic_load_8(&pStatus->stopped);
6,466✔
2279
    if (0 == stopped) {
6,466✔
2280
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
729✔
2281
      msmResetStreamForRedeploy(streamId, pStatus);
729✔
2282
    } else {
2283
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
5,737✔
2284
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2285
        return code;
×
2286
      }
2287
      
2288
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
5,737✔
2289
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
5,737✔
2290
        msmResetStreamForRedeploy(streamId, pStatus);
5,737✔
2291
      }
2292
    }
2293
  }
2294

2295
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
158,018✔
2296
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
158,018✔
2297
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
1,094✔
2298
    return TSDB_CODE_SUCCESS;
1,094✔
2299
  }
2300

2301
  TAOS_CHECK_EXIT(code);
156,924✔
2302

2303
  if (pStatus && pStream->pCreate->streamId != streamId) {
156,924✔
2304
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2305
    atomic_store_8(&pStatus->stopped, 2);
×
2306
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2307
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2308
  }
2309

2310
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
156,924✔
2311
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
156,924✔
2312
  if (userStopped || userDropped) {
156,924✔
2313
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
×
2314
    goto _exit;
×
2315
  }
2316
  
2317
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
156,924✔
2318

2319
_exit:
156,924✔
2320

2321
  mndReleaseStream(pCtx->pMnode, pStream);
156,924✔
2322

2323
  if (code) {
156,924✔
2324
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2325
  }
2326

2327
  return code;
156,924✔
2328
}
2329

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

2362
_exit:
1,020✔
2363

2364
  if (code) {
1,020✔
2365
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2366
  }
2367

2368
  return code;
1,020✔
2369
}
2370

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

2402
_exit:
2403

2404
  if (code) {
2405
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2406
  }
2407

2408
  return code;
2409
}
2410
*/
2411

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

2461
_exit:
745✔
2462

2463
  if (code) {
745✔
2464
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2465
  }
2466

2467
  return code;
745✔
2468
}
2469

2470

2471
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
2,105✔
2472
  int32_t code = TSDB_CODE_SUCCESS;
2,105✔
2473
  int32_t lino = 0;
2,105✔
2474
  int64_t streamId = pAction->streamId;
2,105✔
2475
  int64_t taskId = pAction->id.taskId;
2,105✔
2476
  SStreamObj* pStream = NULL;
2,105✔
2477

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

2480
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
2,105✔
2481
  if (NULL == pStatus) {
2,105✔
2482
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2483
    return TSDB_CODE_SUCCESS;
×
2484
  }
2485

2486
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,105✔
2487
  if (stopped) {
2,105✔
2488
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
340✔
2489
    return TSDB_CODE_SUCCESS;
340✔
2490
  }
2491

2492
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,765✔
2493
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,765✔
2494
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2495
    return TSDB_CODE_SUCCESS;
×
2496
  }
2497

2498
  TAOS_CHECK_EXIT(code);
1,765✔
2499

2500
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,765✔
2501
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,765✔
2502
  if (userStopped || userDropped) {
1,765✔
2503
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2504
    goto _exit;
×
2505
  }
2506

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

2530
_exit:
1,765✔
2531

2532
  if (pStream) {
1,765✔
2533
    mndReleaseStream(pCtx->pMnode, pStream);
1,765✔
2534
  }
2535

2536
  if (code) {
1,765✔
2537
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2538
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2539
  }
2540

2541
  return code;
1,765✔
2542
}
2543

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

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

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

2589
  return TSDB_CODE_SUCCESS;
×
2590
}
2591

2592
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
1,814✔
2593
  int32_t code = TSDB_CODE_SUCCESS;
1,814✔
2594
  int32_t lino = 0;
1,814✔
2595

2596
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,814✔
2597

2598
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
1,814✔
2599

2600
_exit:
1,814✔
2601

2602
  if (code) {
1,814✔
2603
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2604
  } else {
2605
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,814✔
2606
  }
2607

2608
  return code;
1,814✔
2609
}
2610

2611
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
23,613✔
2612
  int32_t code = TSDB_CODE_SUCCESS;
23,613✔
2613
  int32_t lino = 0;
23,613✔
2614

2615
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
23,613✔
2616
  if (0 == active || MND_STM_STATE_NORMAL != state) {
23,613✔
2617
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
311✔
2618
    return;
311✔
2619
  }
2620

2621
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
23,302✔
2622
  if (NULL == pStream) {
23,302✔
2623
    mstsInfo("stream %s already not in streamMap", streamName);
403✔
2624
    goto _exit;
403✔
2625
  }
2626

2627
  atomic_store_8(&pStream->stopped, 2);
22,899✔
2628

2629
  mstsInfo("set stream %s stopped by user", streamName);
22,899✔
2630

2631
_exit:
×
2632

2633
  taosHashRelease(mStreamMgmt.streamMap, pStream);
23,302✔
2634

2635
  if (code) {
23,302✔
2636
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2637
  }
2638

2639
  return;
23,302✔
2640
}
2641

2642
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,096✔
2643
  int32_t code = TSDB_CODE_SUCCESS;
3,096✔
2644
  int32_t lino = 0;
3,096✔
2645

2646
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,096✔
2647
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,096✔
2648
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2649
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2650
  }
2651

2652
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,096✔
2653
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,096✔
2654
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2655
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2656
    goto _exit;
×
2657
  }
2658

2659
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,096✔
2660

2661
_exit:
3,096✔
2662

2663
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,096✔
2664

2665
  if (code) {
3,096✔
2666
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2667
  }
2668

2669
  return code;
3,096✔
2670
}
2671

2672
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
34,529✔
2673
  int32_t code = TSDB_CODE_SUCCESS;
34,529✔
2674
  int32_t lino = 0;
34,529✔
2675
  SStmQNode* pQNode = NULL;
34,529✔
2676

2677
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
194,652✔
2678
    switch (pQNode->type) {
160,123✔
2679
      case STREAM_ACT_DEPLOY:
160,123✔
2680
        if (pQNode->streamAct) {
160,123✔
2681
          mstDebug("start to handle stream deploy action");
158,018✔
2682
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
158,018✔
2683
        } else {
2684
          mstDebug("start to handle task deploy action");
2,105✔
2685
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
2,105✔
2686
        }
2687
        break;
160,123✔
2688
      default:
×
2689
        break;
×
2690
    }
2691
  }
2692

2693
_exit:
34,529✔
2694

2695
  if (code) {
34,529✔
2696
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2697
  }
2698
}
34,529✔
2699

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

2711
    pStatus = (SStmStatus*)pIter;
×
2712

2713
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2714
    atomic_store_8(&pStatus->stopped, 4);
×
2715

2716
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2717
  }
2718
}
×
2719

2720
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2721
  mstInfo("stream grant expired");
×
2722

2723
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2724
    mstWarn("mnode stream is NOT active, ignore handling");
×
2725
    return errCode;
×
2726
  }
2727

2728
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2729

2730
  msmStopAllStreamsByGrant(errCode);
×
2731

2732
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2733
  
2734
  return errCode;
×
2735
}
2736

2737
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,121,908✔
2738
  int32_t code = TSDB_CODE_SUCCESS;
1,121,908✔
2739
  int32_t lino = 0;
1,121,908✔
2740
  int64_t streamId = pDeploy->task.streamId;
1,121,908✔
2741
  
2742
  switch (pDeploy->task.type) {
1,121,908✔
2743
    case STREAM_READER_TASK:
487,051✔
2744
      if (NULL == pStream->readerTasks) {
487,051✔
2745
        pStream->streamId = streamId;
208,489✔
2746
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
208,489✔
2747
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
208,489✔
2748
      }
2749
      
2750
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
974,102✔
2751
      break;
487,051✔
2752
    case STREAM_TRIGGER_TASK:
156,195✔
2753
      pStream->streamId = streamId;
156,195✔
2754
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
156,195✔
2755
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
156,195✔
2756
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
156,195✔
2757
      break;
156,195✔
2758
    case STREAM_RUNNER_TASK:
478,662✔
2759
      if (NULL == pStream->runnerTasks) {
478,662✔
2760
        pStream->streamId = streamId;
159,175✔
2761
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
159,175✔
2762
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
159,175✔
2763
      }      
2764
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
957,324✔
2765
      break;
478,662✔
2766
    default:
×
2767
      break;
×
2768
  }
2769

2770
_exit:
1,121,908✔
2771

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

2776
  return code;
1,121,908✔
2777
}
2778

2779
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,121,908✔
2780
  int32_t code = TSDB_CODE_SUCCESS;
1,121,908✔
2781
  int32_t lino = 0;
1,121,908✔
2782
  int64_t streamId = pDeploy->task.streamId;
1,121,908✔
2783
  SStreamTask* pTask = &pDeploy->task;
1,121,908✔
2784
  SStmStreamDeploy streamDeploy = {0};
1,121,908✔
2785
  SStmStreamDeploy* pStream = NULL;
1,121,908✔
2786
   
2787
  while (true) {
2788
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,121,908✔
2789
    if (NULL == pStream) {
1,121,908✔
2790
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
221,028✔
2791
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
221,028✔
2792
      if (TSDB_CODE_SUCCESS == code) {
221,028✔
2793
        goto _exit;
221,028✔
2794
      }
2795

2796
      if (TSDB_CODE_DUP_KEY != code) {
×
2797
        goto _exit;
×
2798
      }    
2799

2800
      tFreeSStmStreamDeploy(&streamDeploy);
×
2801
      continue;
×
2802
    }
2803

2804
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
900,880✔
2805
    
2806
    break;
900,880✔
2807
  }
2808
  
2809
_exit:
1,121,908✔
2810

2811
  taosHashRelease(pHash, pStream);
1,121,908✔
2812

2813
  if (code) {
1,121,908✔
2814
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2815
  } else {
2816
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,121,908✔
2817
  }
2818

2819
  return code;
1,121,908✔
2820
}
2821

2822

2823
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
204,462✔
2824
  int32_t code = TSDB_CODE_SUCCESS;
204,462✔
2825
  int32_t lino = 0;
204,462✔
2826
  int32_t taskNum = taosArrayGetSize(pTasks);
204,462✔
2827

2828
  for (int32_t i = 0; i < taskNum; ++i) {
1,330,744✔
2829
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,126,282✔
2830
    if (pExt->deployed) {
1,126,282✔
2831
      continue;
4,374✔
2832
    }
2833

2834
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,121,908✔
2835
    pExt->deployed = true;
1,121,908✔
2836

2837
    (void)atomic_add_fetch_32(deployed, 1);
1,121,908✔
2838
  }
2839

2840
_exit:
204,462✔
2841

2842
  if (code) {
204,462✔
2843
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2844
  }
2845

2846
  return code;
204,462✔
2847
}
2848

2849
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
55,474✔
2850
  int32_t code = TSDB_CODE_SUCCESS;
55,474✔
2851
  int32_t lino = 0;
55,474✔
2852
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
55,474✔
2853
  SStmVgTasksToDeploy* pVg = NULL;
55,474✔
2854
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2855

2856
  mstDebug("start to add stream vgroup tasks deploy");
55,474✔
2857
  
2858
  for (int32_t i = 0; i < vgNum; ++i) {
387,362✔
2859
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
331,888✔
2860

2861
    msmUpdateVgroupUpTs(pCtx, *vgId);
331,888✔
2862

2863
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
331,888✔
2864
    if (NULL == pVg) {
331,888✔
2865
      continue;
203,086✔
2866
    }
2867

2868
    if (taosRTryLockLatch(&pVg->lock)) {
128,802✔
2869
      continue;
×
2870
    }
2871
    
2872
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
128,802✔
2873
      taosRUnLockLatch(&pVg->lock);
1,434✔
2874
      continue;
1,434✔
2875
    }
2876
    
2877
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
127,368✔
2878
    taosRUnLockLatch(&pVg->lock);
127,368✔
2879
  }
2880

2881
_exit:
55,474✔
2882

2883
  if (code) {
55,474✔
2884
    if (pVg) {
×
2885
      taosRUnLockLatch(&pVg->lock);
×
2886
    }
2887

2888
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2889
  }
2890

2891
  return code;
55,474✔
2892
}
2893

2894
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
57,777✔
2895
  int32_t code = TSDB_CODE_SUCCESS;
57,777✔
2896
  int32_t lino = 0;
57,777✔
2897
  SStmSnodeTasksDeploy* pSnode = NULL;
57,777✔
2898
  SStreamHbMsg* pReq = pCtx->pReq;
57,777✔
2899

2900
  mstDebug("start to add stream snode tasks deploy");
57,777✔
2901
  
2902
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
57,777✔
2903
  if (NULL == pSnode) {
57,777✔
2904
    return TSDB_CODE_SUCCESS;
16,660✔
2905
  }
2906

2907
  (void)mstWaitLock(&pSnode->lock, false);
41,117✔
2908
  
2909
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
41,117✔
2910
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
38,128✔
2911
  }
2912

2913
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
41,117✔
2914
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
38,966✔
2915
  }
2916
  
2917
  taosWUnLockLatch(&pSnode->lock);
41,117✔
2918

2919
_exit:
41,117✔
2920

2921
  if (code) {
41,117✔
2922
    if (pSnode) {
×
2923
      taosWUnLockLatch(&pSnode->lock);
×
2924
    }
2925

2926
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2927
  }
2928

2929
  return code;
41,117✔
2930
}
2931

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

2943
_exit:
551,687✔
2944

2945
  if (code) {
551,687✔
2946
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2947
  }
2948

2949
  return code;
551,687✔
2950
}
2951

2952
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
57,130✔
2953
  int32_t code = TSDB_CODE_SUCCESS;
57,130✔
2954
  int32_t lino = 0;
57,130✔
2955
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
57,130✔
2956
  void* pIter = NULL;
57,130✔
2957

2958
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
57,130✔
2959
  
2960
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
57,130✔
2961
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
57,130✔
2962

2963
  while (1) {
221,028✔
2964
    pIter = taosHashIterate(pCtx->deployStm, pIter);
278,158✔
2965
    if (pIter == NULL) {
278,158✔
2966
      break;
57,130✔
2967
    }
2968
    
2969
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
221,028✔
2970
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
442,056✔
2971

2972
    int64_t streamId = pDeploy->streamId;
221,028✔
2973
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
221,028✔
2974
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
2975

2976
    mstClearSStmStreamDeploy(pDeploy);
221,028✔
2977
    
2978
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
221,028✔
2979
  }
2980
  
2981
_exit:
57,130✔
2982

2983
  if (pIter) {
57,130✔
2984
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2985
  }
2986

2987
  if (code) {
57,130✔
2988
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2989
  }
2990
  
2991
  return code;
57,130✔
2992
}
2993

2994
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
54,757✔
2995
  int32_t code = TSDB_CODE_SUCCESS;
54,757✔
2996
  int32_t lino = 0;
54,757✔
2997
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
54,757✔
2998
  SStmVgTasksToDeploy* pVg = NULL;
54,757✔
2999
  
3000
  for (int32_t i = 0; i < vgNum; ++i) {
381,626✔
3001
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
326,869✔
3002
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
326,869✔
3003
    if (NULL == pVg) {
326,869✔
3004
      continue;
199,501✔
3005
    }
3006

3007
    if (taosWTryLockLatch(&pVg->lock)) {
127,368✔
3008
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3009
      continue;
×
3010
    }
3011
    
3012
    if (atomic_load_32(&pVg->deployed) <= 0) {
127,368✔
3013
      taosWUnLockLatch(&pVg->lock);
×
3014
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3015
      continue;
×
3016
    }
3017

3018
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
127,368✔
3019
    if (atomic_load_32(&pVg->deployed) == taskNum) {
127,368✔
3020
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
124,828✔
3021
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
124,828✔
3022
      pVg->taskList = NULL;
124,828✔
3023
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
124,828✔
3024
      taosWUnLockLatch(&pVg->lock);
124,828✔
3025
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
124,828✔
3026
      continue;
124,828✔
3027
    }
3028

3029
    for (int32_t m = taskNum - 1; m >= 0; --m) {
26,249✔
3030
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
23,709✔
3031
      if (!pExt->deployed) {
23,709✔
3032
        continue;
2,151✔
3033
      }
3034

3035
      mstDestroySStmTaskToDeployExt(pExt);
21,558✔
3036

3037
      taosArrayRemove(pVg->taskList, m);
21,558✔
3038
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
21,558✔
3039
    }
3040
    atomic_store_32(&pVg->deployed, 0);
2,540✔
3041
    taosWUnLockLatch(&pVg->lock);
2,540✔
3042
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
2,540✔
3043
  }
3044

3045
_exit:
54,757✔
3046

3047
  if (code) {
54,757✔
3048
    if (pVg) {
×
3049
      taosWUnLockLatch(&pVg->lock);
×
3050
    }
3051

3052
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3053
  }
3054
}
54,757✔
3055

3056
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
69,465✔
3057
  if (!GOT_SNODE(snodeId)) {
69,465✔
3058
    return;
11,688✔
3059
  }
3060
  
3061
  int32_t code = TSDB_CODE_SUCCESS;
57,777✔
3062
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
57,777✔
3063
  if (NULL == pSnode) {
57,777✔
3064
    return;
16,660✔
3065
  }
3066

3067
  if (taosWTryLockLatch(&pSnode->lock)) {
41,117✔
3068
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3069
    return;
×
3070
  }
3071

3072
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
41,117✔
3073
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
41,117✔
3074
  
3075
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
41,117✔
3076
    taosWUnLockLatch(&pSnode->lock);
×
3077
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3078
    return;
×
3079
  }
3080

3081
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
41,117✔
3082
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
37,690✔
3083
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
37,690✔
3084
    pSnode->triggerList = NULL;
37,690✔
3085
  }
3086

3087
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
41,117✔
3088
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
38,407✔
3089
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
38,407✔
3090
    pSnode->runnerList = NULL;
38,407✔
3091
  }
3092

3093
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
41,117✔
3094
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
36,973✔
3095
    taosWUnLockLatch(&pSnode->lock);
36,973✔
3096
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
36,973✔
3097
    return;
36,973✔
3098
  }
3099

3100
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
4,144✔
3101
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
20,659✔
3102
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
18,666✔
3103
      if (!pExt->deployed) {
18,666✔
3104
        continue;
717✔
3105
      }
3106

3107
      mstDestroySStmTaskToDeployExt(pExt);
17,949✔
3108
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
17,949✔
3109
      taosArrayRemove(pSnode->triggerList, m);
17,949✔
3110
    }
3111
    
3112
    pSnode->triggerDeployed = 0;
1,993✔
3113
  }
3114

3115
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
4,144✔
3116
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
10,669✔
3117
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
8,676✔
3118
      if (!pExt->deployed) {
8,676✔
3119
        continue;
4,302✔
3120
      }
3121

3122
      mstDestroySStmTaskToDeployExt(pExt);
4,374✔
3123
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
4,374✔
3124
      taosArrayRemove(pSnode->runnerList, m);
4,374✔
3125
    }
3126
    
3127
    pSnode->runnerDeployed = 0;
1,993✔
3128
  }
3129
  
3130
  taosWUnLockLatch(&pSnode->lock);
4,144✔
3131
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
4,144✔
3132
}
3133

3134
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
17,916,652✔
3135
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
17,916,652✔
3136
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
54,757✔
3137
  }
3138

3139
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
17,916,652✔
3140
    msmCleanDeployedSnodeTasks(pHb->snodeId);
69,465✔
3141
  }
3142
}
17,916,652✔
3143

3144
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
17,916,652✔
3145
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
17,916,652✔
3146
  if (mStreamMgmt.tCtx) {
17,916,652✔
3147
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
17,835,454✔
3148
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
17,835,454✔
3149
  }
3150
}
17,916,652✔
3151

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

3169
_exit:
212,209✔
3170

3171
  if (code) {
212,209✔
3172
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3173
  }
3174

3175
  return code;
212,209✔
3176
}
3177

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

3194
_exit:
×
3195

3196
  if (code) {
×
3197
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3198
  }
3199

3200
  return code;
×
3201
}
3202

3203

3204

3205
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
118,450✔
3206
  int32_t code = TSDB_CODE_SUCCESS;
118,450✔
3207
  int32_t lino = 0;
118,450✔
3208
  int32_t action = STREAM_ACT_UNDEPLOY;
118,450✔
3209
  bool    dropped = false;
118,450✔
3210

3211
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
118,450✔
3212
  mstsDebug("stream dropped: %d", dropped);
118,450✔
3213
  
3214
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
118,450✔
3215
  if (pAction) {
118,450✔
3216
    pAction->actions |= action;
84,932✔
3217
    if (NULL == pAction->undeploy.taskList) {
84,932✔
3218
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3219
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3220
    }
3221

3222
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
169,864✔
3223
    if (pAction->undeploy.doCheckpoint) {
84,932✔
3224
      pAction->undeploy.doCheckpoint = dropped ? false : true;
25,627✔
3225
    }
3226
    if (!pAction->undeploy.doCleanup) {
84,932✔
3227
      pAction->undeploy.doCleanup = dropped ? true : false;
25,627✔
3228
    }
3229
    
3230
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
84,932✔
3231
  } else {
3232
    SStmAction newAction = {0};
33,518✔
3233
    newAction.actions = action;
33,518✔
3234
    newAction.undeploy.doCheckpoint = dropped ? false : true;
33,518✔
3235
    newAction.undeploy.doCleanup = dropped ? true : false;
33,518✔
3236
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
33,518✔
3237
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
33,518✔
3238
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
67,036✔
3239
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
33,518✔
3240
    
3241
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
33,518✔
3242
  }
3243

3244
_exit:
118,450✔
3245

3246
  if (code) {
118,450✔
3247
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3248
  }
3249

3250
  return code;
118,450✔
3251
}
3252

3253
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,096✔
3254
  int32_t code = TSDB_CODE_SUCCESS;
3,096✔
3255
  int32_t lino = 0;
3,096✔
3256
  int32_t action = STREAM_ACT_RECALC;
3,096✔
3257
  SStmAction newAction = {0};
3,096✔
3258
  
3259
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,096✔
3260
  if (pAction) {
3,096✔
3261
    pAction->actions |= action;
×
3262
    pAction->recalc.recalcList = recalcList;
×
3263

3264
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3265
  } else {
3266
    newAction.actions = action;
3,096✔
3267
    newAction.recalc.recalcList = recalcList;
3,096✔
3268
    
3269
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,096✔
3270
    
3271
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,096✔
3272
  }
3273

3274
_exit:
3,096✔
3275

3276
  if (code) {
3,096✔
3277
    mstDestroySStmAction(&newAction);
×
3278
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3279
  }
3280

3281
  return code;
3,096✔
3282
}
3283

3284
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
270,086✔
3285
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
270,086✔
3286
  if (NULL == pStream) {
270,086✔
3287
    return false;
×
3288
  }
3289

3290
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
270,086✔
3291
    return false;
×
3292
  }
3293

3294
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
270,086✔
3295
  for (int32_t i = 0; i < readerNum; ++i) {
590,187✔
3296
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
320,101✔
3297
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
320,101✔
3298
      return false;
×
3299
    }
3300
  }
3301

3302
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
270,086✔
3303
  for (int32_t i = 0; i < readerNum; ++i) {
326,428✔
3304
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
56,342✔
3305
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
56,342✔
3306
      return false;
×
3307
    }
3308
  }
3309

3310
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
270,086✔
3311
  SListNode* pNode = listHead(pStream->calcReaders);
270,086✔
3312
  for (int32_t i = 0; i < readerNum; ++i) {
781,799✔
3313
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
511,713✔
3314
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
511,713✔
3315
      return false;
×
3316
    }
3317
    pNode = TD_DLIST_NODE_NEXT(pNode);
511,713✔
3318
  }
3319

3320
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
895,578✔
3321
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
683,369✔
3322
    for (int32_t m = 0; m < runnerNum; ++m) {
1,327,674✔
3323
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
702,182✔
3324
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
702,182✔
3325
        return false;
57,877✔
3326
      }
3327
    }
3328
  }
3329
  
3330
  return true;
212,209✔
3331
}
3332

3333

3334
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
17,252,466✔
3335
  int32_t code = TSDB_CODE_SUCCESS;
17,252,466✔
3336
  int32_t lino = 0;
17,252,466✔
3337
  int32_t action = 0;
17,252,466✔
3338
  int64_t streamId = pMsg->streamId;
17,252,466✔
3339
  SStreamTask* pTask = (SStreamTask*)pMsg;
17,252,466✔
3340
  int8_t  stopped = 0;
17,252,466✔
3341

3342
  msttDebug("start to handle task abnormal status %d", pTask->status);
17,252,466✔
3343
  
3344
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
17,252,466✔
3345
  if (NULL == pStatus) {
17,252,466✔
3346
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3347
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3348
    return;
10,072✔
3349
  }
3350

3351
  stopped = atomic_load_8(&pStatus->stopped);
17,252,466✔
3352
  if (stopped) {
17,252,466✔
3353
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3354
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3355
    return;
×
3356
  }
3357
  
3358
  switch (pMsg->status) {
17,252,466✔
3359
    case STREAM_STATUS_INIT:      
17,246,762✔
3360
      if (STREAM_TRIGGER_TASK != pMsg->type) {
17,246,762✔
3361
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
16,825,953✔
3362
        return;
16,825,953✔
3363
      }
3364
      
3365
      if (INT64_MIN == pStatus->lastActionTs) {
420,809✔
3366
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3367
        return;
×
3368
      }
3369
      
3370
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
420,809✔
3371
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
150,723✔
3372
        return;
150,723✔
3373
      }
3374

3375
      if (STREAM_IS_RUNNING(pStatus)) {
270,086✔
3376
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3377
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
270,086✔
3378
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
212,209✔
3379
      }
3380
      break;
270,086✔
3381
    case STREAM_STATUS_FAILED:
5,704✔
3382
      //STREAMTODO ADD ERRCODE HANDLE
3383
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
5,704✔
3384
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
4,270✔
3385
                 pMsg->taskIdx);
3386
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
4,270✔
3387
      } else {
3388
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
1,434✔
3389
                 pMsg->taskIdx);
3390
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
1,434✔
3391
      }
3392
      break;
5,704✔
3393
    default:
×
3394
      break;
×
3395
  }
3396

3397
_exit:
275,790✔
3398

3399
  if (code) {
275,790✔
3400
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3401
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3402
  }
3403
}
3404

3405
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
117,016✔
3406
  int32_t code = TSDB_CODE_SUCCESS;
117,016✔
3407
  int32_t lino = 0;
117,016✔
3408
  SStreamTask* pTask = (SStreamTask*)pStatus;
117,016✔
3409
  int64_t streamId = pStatus->streamId;
117,016✔
3410

3411
  msttInfo("start to handle task error, type: %d", err);
117,016✔
3412

3413
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
117,016✔
3414

3415
_exit:
117,016✔
3416

3417
  if (code) {
117,016✔
3418
    // IGNORE STOP STREAM BY ERROR  
3419
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3420
  }
3421
}
117,016✔
3422

3423
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
4,833,571✔
3424
  int32_t code = TSDB_CODE_SUCCESS;
4,833,571✔
3425
  int32_t lino = 0;
4,833,571✔
3426
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
4,833,571✔
3427

3428
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
4,833,571✔
3429
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3430
  }
3431
  
3432
  SArray* userRecalcList = NULL;
4,833,571✔
3433
  if (atomic_load_ptr(&pStream->userRecalcList)) {
4,833,571✔
3434
    taosWLockLatch(&pStream->userRecalcLock);
3,096✔
3435
    if (pStream->userRecalcList) {
3,096✔
3436
      userRecalcList = pStream->userRecalcList;
3,096✔
3437
      pStream->userRecalcList = NULL;
3,096✔
3438
    }
3439
    taosWUnLockLatch(&pStream->userRecalcLock);
3,096✔
3440
    
3441
    if (userRecalcList) {
3,096✔
3442
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,096✔
3443
    }
3444
  }
3445

3446
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
4,833,571✔
3447
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,421,854✔
3448
    if (NULL == pStatus->detailStatus) {
2,421,854✔
3449
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
154,749✔
3450
      if (NULL == pStatus->detailStatus) {
154,749✔
3451
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3452
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3453
      }
3454
    }
3455
    
3456
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,421,854✔
3457
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,421,854✔
3458
  }
3459

3460
_exit:
2,411,717✔
3461

3462
  if (code) {
4,833,571✔
3463
    // IGNORE STOP STREAM BY ERROR
3464
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3465
  }
3466
}
4,833,571✔
3467

3468
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,017,245✔
3469
  int32_t code = TSDB_CODE_SUCCESS;
1,017,245✔
3470
  int32_t lino = 0;
1,017,245✔
3471
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,017,245✔
3472

3473
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,017,245✔
3474

3475
  for (int32_t i = 0; i < num; ++i) {
40,059,870✔
3476
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
39,042,625✔
3477
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
39,042,625✔
3478
    
3479
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
39,042,625✔
3480
    if (NULL == ppStatus) {
39,042,625✔
3481
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
5,204✔
3482
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
5,204✔
3483
      continue;
5,204✔
3484
    }
3485

3486
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
39,037,421✔
3487
    int8_t stopped = atomic_load_8(&pStream->stopped);
39,037,421✔
3488
    if (stopped) {
39,037,421✔
3489
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
111,812✔
3490
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
111,812✔
3491
      continue;
111,812✔
3492
    }
3493

3494
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
38,925,609✔
3495
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3496
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3497
          
3498
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3499
      continue;
×
3500
    }
3501

3502
    if ((*ppStatus)->status != pTask->status) {
38,925,609✔
3503
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,941,685✔
3504
        (*ppStatus)->runningStartTs = pCtx->currTs;
822,424✔
3505
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,119,261✔
3506
        if (pStream->triggerTask) {
×
3507
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3508
        }
3509
        
3510
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3511
      }
3512
    }
3513
    
3514
    (*ppStatus)->errCode = pTask->errorCode;
38,925,609✔
3515
    (*ppStatus)->status = pTask->status;
38,925,609✔
3516
    (*ppStatus)->lastUpTs = pCtx->currTs;
38,925,609✔
3517
    
3518
    if (STREAM_STATUS_RUNNING != pTask->status) {
38,925,609✔
3519
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
17,252,466✔
3520
    }
3521
    
3522
    if (STREAM_TRIGGER_TASK == pTask->type) {
38,925,609✔
3523
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
4,833,571✔
3524
    }
3525
  }
3526

3527
_exit:
1,017,245✔
3528

3529
  if (code) {
1,017,245✔
3530
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3531
  }
3532

3533
  return code;
1,017,245✔
3534
}
3535

3536
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3537
  int32_t code = TSDB_CODE_SUCCESS;
×
3538
  int32_t lino = 0;
×
3539
  int64_t streamId = pTask->streamId;
×
3540
  SStreamObj* pStream = NULL;
×
3541

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

3554
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3555
    mndReleaseStream(pCtx->pMnode, pStream);
×
3556

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

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

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

3625
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3626
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3627
      break;
×
3628
    }
3629
    default: {
×
3630
      msttError("invalid task type:%d in task status", pTask->type);
×
3631
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3632
      break;
×
3633
    }
3634
  }
3635

3636
_exit:
×
3637

3638
  if (code) {
×
3639
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3640
  } else {
3641
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3642
  }
3643

3644
  return code;
×
3645
}
3646

3647
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3648
  int32_t code = TSDB_CODE_SUCCESS;
×
3649
  int32_t lino = 0;
×
3650
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3651

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

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

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

3675
_exit:
×
3676

3677
  if (code) {
×
3678
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3679
  }
3680

3681
  return code;
×
3682
}
3683

3684
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
212,209✔
3685
  int32_t code = TSDB_CODE_SUCCESS;
212,209✔
3686
  int32_t lino = 0;
212,209✔
3687
  if (NULL == pCtx->pRsp->start.taskList) {
212,209✔
3688
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
86,457✔
3689
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
86,457✔
3690
  }
3691

3692
  SStmTaskId* pId = &pAction->start.triggerId;
212,209✔
3693
  SStreamTaskStart start = {0};
212,209✔
3694
  start.task.type = STREAM_TRIGGER_TASK;
212,209✔
3695
  start.task.streamId = streamId;
212,209✔
3696
  start.task.taskId = pId->taskId;
212,209✔
3697
  start.task.seriousId = pId->seriousId;
212,209✔
3698
  start.task.nodeId = pId->nodeId;
212,209✔
3699
  start.task.taskIdx = pId->taskIdx;
212,209✔
3700

3701
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
424,418✔
3702
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
212,209✔
3703

3704
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
212,209✔
3705

3706
  return;
212,209✔
3707

3708
_exit:
×
3709

3710
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3711
}
3712

3713

3714
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
33,518✔
3715
  int32_t code = TSDB_CODE_SUCCESS;
33,518✔
3716
  int32_t lino = 0;
33,518✔
3717
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
33,518✔
3718
  if (NULL == pCtx->pRsp->undeploy.taskList) {
33,518✔
3719
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
21,626✔
3720
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
21,626✔
3721
  }
3722

3723
  SStreamTaskUndeploy undeploy;
33,518✔
3724
  for (int32_t i = 0; i < dropNum; ++i) {
151,968✔
3725
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
118,450✔
3726
    undeploy.task = *pTask;
118,450✔
3727
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
118,450✔
3728
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
118,450✔
3729

3730
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
236,900✔
3731
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
118,450✔
3732

3733
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
118,450✔
3734
  }
3735

3736
  return;
33,518✔
3737

3738
_exit:
×
3739

3740
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3741
}
3742

3743
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3744
  int32_t code = TSDB_CODE_SUCCESS;
×
3745
  int32_t lino = 0;
×
3746

3747
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3748
  if (NULL == pStream) {
×
3749
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3750
    return;
×
3751
  }
3752

3753
  if (NULL == pStream->triggerTask) {
×
3754
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3755
    return;
×
3756
  }
3757

3758
  SStreamMgmtRsp rsp = {0};
×
3759
  rsp.reqId = INT64_MIN;
×
3760
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3761
  rsp.task.streamId = streamId;
×
3762
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3763

3764
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3765

3766
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3767
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3768
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3769
  }
3770

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

3773
_exit:
×
3774

3775
  if (code) {
×
3776
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3777
  } else {
3778
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3779
  }
3780
}
3781

3782
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,096✔
3783
  int32_t code = TSDB_CODE_SUCCESS;
3,096✔
3784
  int32_t lino = 0;
3,096✔
3785

3786
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,096✔
3787
  if (NULL == pStream) {
3,096✔
3788
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3789
    return;
×
3790
  }
3791

3792
  if (NULL == pStream->triggerTask) {
3,096✔
3793
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3794
    return;
×
3795
  }
3796

3797
  SStreamMgmtRsp rsp = {0};
3,096✔
3798
  rsp.reqId = INT64_MIN;
3,096✔
3799
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,096✔
3800
  rsp.task.streamId = streamId;
3,096✔
3801
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,096✔
3802
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,096✔
3803

3804
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,096✔
3805
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
2,906✔
3806
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
2,906✔
3807
  }
3808

3809
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,192✔
3810

3811
_exit:
3,096✔
3812

3813
  if (code) {
3,096✔
3814
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3815
  } else {
3816
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,096✔
3817
  }
3818
}
3819

3820

3821
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
110,473✔
3822
  int32_t code = TSDB_CODE_SUCCESS;
110,473✔
3823
  int32_t lino = 0;
110,473✔
3824
  void* pIter = NULL;
110,473✔
3825
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
110,473✔
3826

3827
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
110,473✔
3828

3829
  while (1) {
248,823✔
3830
    pIter = taosHashIterate(pCtx->actionStm, pIter);
359,296✔
3831
    if (pIter == NULL) {
359,296✔
3832
      break;
110,473✔
3833
    }
3834

3835
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
248,823✔
3836
    SStmAction *pAction = (SStmAction *)pIter;
248,823✔
3837
    
3838
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
248,823✔
3839
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
33,518✔
3840
      continue;
33,518✔
3841
    }
3842

3843
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
215,305✔
3844
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3845
    }
3846

3847
    if (STREAM_ACT_RECALC & pAction->actions) {
215,305✔
3848
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,096✔
3849
    }
3850

3851
    if (STREAM_ACT_START & pAction->actions) {
215,305✔
3852
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
212,209✔
3853
    }
3854
  }
3855
  
3856
_exit:
110,473✔
3857

3858
  if (pIter) {
110,473✔
3859
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3860
  }
3861

3862
  if (code) {
110,473✔
3863
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3864
  }
3865

3866
  return code;
110,473✔
3867
}
3868

3869
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
17,835,454✔
3870
  int32_t  code = TSDB_CODE_SUCCESS;
17,835,454✔
3871
  int32_t  lino = 0;
17,835,454✔
3872
  int64_t* lastTs = NULL;
17,835,454✔
3873
  bool     noExists = false;
17,835,454✔
3874

3875
  while (true) {
3876
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
18,299,199✔
3877
    if (NULL == lastTs) {
18,299,199✔
3878
      if (noExists) {
604,042✔
3879
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
140,297✔
3880
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
140,297✔
3881
      }
3882

3883
      noExists = true;
463,745✔
3884
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
463,745✔
3885
      
3886
      continue;
463,745✔
3887
    }
3888

3889
    while (true) {
×
3890
      int64_t lastTsValue = atomic_load_64(lastTs);
17,695,157✔
3891
      if (pCtx->currTs > lastTsValue) {
17,695,157✔
3892
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
17,694,911✔
3893
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
17,694,911✔
3894
          return code;
17,694,911✔
3895
        }
3896

3897
        continue;
×
3898
      }
3899

3900
      return code;
246✔
3901
    }
3902

3903
    break;
3904
  }
3905

3906
_exit:
140,297✔
3907

3908
  if (code) {
140,297✔
3909
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
140,297✔
3910
  }
3911

3912
  return code;  
140,297✔
3913
}
3914

3915
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
3916
  SStmStatus* pStatus = NULL;
×
3917
  int32_t trigReaderNum = 0;
×
3918
  int32_t calcReaderNum = 0;
×
3919
  int32_t runnerNum = 0;
×
3920
  int64_t streamId = 0;
×
3921
  void* pIter = NULL;
×
3922
  while (true) {
3923
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
3924
    if (NULL == pIter) {
×
3925
      return;
×
3926
    }
3927

3928
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3929
    pStatus = (SStmStatus*)pIter;
×
3930

3931
    if (NULL == pStatus->triggerTask) {
×
3932
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
3933
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3934
      continue;
×
3935
    }
3936
    
3937
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
3938
    if (pStatus->trigReaderNum != trigReaderNum) {
×
3939
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
3940
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3941
      continue;
×
3942
    }
3943

3944
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
3945
    if (pStatus->calcReaderNum != calcReaderNum) {
×
3946
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
3947
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3948
      continue;
×
3949
    }
3950

3951
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
3952
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
3953
      if (runnerNum != pStatus->runnerNum) {
×
3954
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
3955
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3956
        continue;
×
3957
      }
3958
    }
3959
  }
3960
}
3961

3962
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
337✔
3963
  int32_t code = TSDB_CODE_SUCCESS;
337✔
3964
  int32_t lino = 0;
337✔
3965
  int32_t minVal = watchError ? 0 : 1;
337✔
3966

3967
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
337✔
3968
    return code;
×
3969
  }
3970

3971
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
337✔
3972
    (void)sched_yield();
×
3973
  }
3974

3975
  if (watchError) {
337✔
3976
    taosHashClear(mStreamMgmt.vgroupMap);
×
3977
    taosHashClear(mStreamMgmt.snodeMap);
×
3978
    taosHashClear(mStreamMgmt.taskMap);
×
3979
    taosHashClear(mStreamMgmt.streamMap);
×
3980
    mstInfo("watch error happends, clear all maps");
×
3981
    goto _exit;
×
3982
  }
3983

3984
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
337✔
3985
    mstInfo("no stream tasks remain during watch state");
337✔
3986
    goto _exit;
337✔
3987
  }
3988

3989
  msmWatchCheckStreamMap(pCtx);
×
3990

3991
_exit:
337✔
3992

3993
  mStreamMgmt.lastTaskId += 100000;
337✔
3994

3995
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
337✔
3996

3997
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
337✔
3998

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

4003
  return code;
337✔
4004
}
4005

4006

4007
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
14,168✔
4008
  int32_t code = TSDB_CODE_SUCCESS;
14,168✔
4009
  int32_t lino = 0;
14,168✔
4010
  SStreamHbMsg* pReq = pCtx->pReq;
14,168✔
4011

4012
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
14,168✔
4013
  
4014
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
14,168✔
4015
    goto _exit;
×
4016
  }
4017

4018
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
14,168✔
4019
  if (GOT_SNODE(pReq->snodeId)) {
14,168✔
4020
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,290✔
4021
  }
4022

4023
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
14,168✔
4024
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4025
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4026
  }
4027

4028
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
14,168✔
4029
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
337✔
4030
  }
4031

4032
_exit:
14,168✔
4033

4034
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
14,168✔
4035
  
4036
  if (code) {
14,168✔
4037
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4038

4039
    (void)msmWatchHandleEnding(pCtx, true);
×
4040
  }
4041

4042
  return code;
14,168✔
4043
}
4044

4045
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
12,417,808✔
4046
  int32_t listSize = taosArrayGetSize(pOReaders);
12,417,808✔
4047
  int32_t totalSize = 0;
12,417,808✔
4048
  
4049
  for (int32_t i = 0; i < listSize; ++i) {
20,483,334✔
4050
    SArray* pList = taosArrayGetP(pOReaders, i);
8,065,526✔
4051
    totalSize += taosArrayGetSize(pList);
8,065,526✔
4052
  }
4053

4054
  return totalSize;
12,417,808✔
4055
}
4056

4057
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
4,758,556✔
4058
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
4,758,556✔
4059
  if (NULL == pList) {
4,758,556✔
4060
    return NULL;
×
4061
  }
4062

4063
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
4,758,556✔
4064
}
4065

4066

4067
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
56,342✔
4068
  int32_t code = TSDB_CODE_SUCCESS;
56,342✔
4069
  int32_t lino = 0;
56,342✔
4070

4071
  if (NULL == pStatus->trigOReaders) {
56,342✔
4072
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
48,768✔
4073
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
48,768✔
4074
  }
4075

4076
  while (true) {
48,768✔
4077
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
105,110✔
4078

4079
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
105,110✔
4080
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
48,768✔
4081
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
48,768✔
4082

4083
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
97,536✔
4084
      continue;
48,768✔
4085
    }
4086

4087
    *ppRes = *ppOReaderList;
56,342✔
4088
    break;
56,342✔
4089
  }
4090

4091
_exit:
56,342✔
4092

4093
  if (code) {
56,342✔
4094
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4095
  }
4096

4097
  return code;
56,342✔
4098
}
4099

4100
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
65,944✔
4101
  int32_t code = TSDB_CODE_SUCCESS;
65,944✔
4102
  int32_t lino = 0;
65,944✔
4103
  bool    readerExists = false;
65,944✔
4104
  int64_t streamId = pTask->streamId;
65,944✔
4105

4106
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
65,944✔
4107
  for (int32_t i = 0; i < readerNum; ++i) {
137,576✔
4108
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
81,234✔
4109
    if (pReader->id.nodeId == vgId) {
81,234✔
4110
      readerExists = true;
9,602✔
4111
      break;
9,602✔
4112
    }
4113
  }
4114

4115
  if (!readerExists) {
65,944✔
4116
    SArray* pReaderList = NULL;
56,342✔
4117
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
56,342✔
4118
    
4119
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
56,342✔
4120
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
56,342✔
4121
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
56,342✔
4122
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
56,342✔
4123
  }
4124

4125
_exit:
9,602✔
4126

4127
  if (code) {
65,944✔
4128
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4129
  }
4130

4131
  return code;
65,944✔
4132
}
4133

4134
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
57,800✔
4135
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
57,800✔
4136
  int32_t lino = 0;
57,800✔
4137
  int32_t vgId = 0;
57,800✔
4138
  int64_t streamId = pTask->streamId;
57,800✔
4139
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
57,800✔
4140
  int32_t tbNum = taosArrayGetSize(pTbs);
57,800✔
4141
  SStreamDbTableName* pName = NULL;
57,800✔
4142
  SSHashObj* pDbVgroups = NULL;
57,800✔
4143
  SStreamMgmtRsp rsp = {0};
57,800✔
4144
  rsp.reqId = pTask->pMgmtReq->reqId;
57,800✔
4145
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
57,800✔
4146
  int32_t iter = 0;
57,800✔
4147
  void* p = NULL;
57,800✔
4148
  SSHashObj* pVgs = NULL;
57,800✔
4149
  SStreamMgmtReq* pMgmtReq = NULL;
57,800✔
4150
  int8_t stopped = 0;
57,800✔
4151

4152
  if (NULL == pCtx->pRsp->rsps.rspList) {
57,800✔
4153
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4154
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4155
  }
4156
  
4157
  TSWAP(pTask->pMgmtReq, pMgmtReq);
57,800✔
4158
  rsp.task = *(SStreamTask*)pTask;
57,800✔
4159

4160
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
57,800✔
4161
  if (NULL == pStatus) {
57,800✔
4162
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4163
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4164
  }
4165

4166
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
57,800✔
4167
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4168
    goto _exit;
×
4169
  }
4170

4171
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
57,800✔
4172

4173
  stopped = atomic_load_8(&pStatus->stopped);
57,800✔
4174
  if (stopped) {
57,800✔
4175
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4176
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4177
  }
4178

4179
  if (tbNum <= 0) {
57,800✔
4180
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4181
    goto _exit;
×
4182
  }
4183

4184
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
57,800✔
4185
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
57,800✔
4186
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
57,800✔
4187

4188
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
57,800✔
4189
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
57,800✔
4190
  
4191
  for (int32_t i = 0; i < tbNum; ++i) {
230,218✔
4192
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
172,418✔
4193
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
172,418✔
4194
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
344,836✔
4195
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
172,418✔
4196
  }
4197

4198
  int32_t vgNum = tSimpleHashGetSize(pVgs);
57,800✔
4199
  while (true) {
4200
    p = tSimpleHashIterate(pVgs, p, &iter);
123,744✔
4201
    if (NULL == p) {
123,744✔
4202
      break;
57,800✔
4203
    }
4204
    
4205
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
65,944✔
4206
  }
4207
  
4208
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
57,800✔
4209
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
57,800✔
4210
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
57,800✔
4211

4212
  SStreamTaskAddr addr;
57,800✔
4213
  for (int32_t i = 0; i < vgNum; ++i) {
114,142✔
4214
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
56,342✔
4215
    addr.taskId = pOTask->id.taskId;
56,342✔
4216
    addr.nodeId = pOTask->id.nodeId;
56,342✔
4217
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
56,342✔
4218
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
112,684✔
4219
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
56,342✔
4220
  }
4221

4222
_exit:
57,800✔
4223

4224
  tFreeSStreamMgmtReq(pMgmtReq);
57,800✔
4225
  taosMemoryFree(pMgmtReq);
57,800✔
4226

4227
  tSimpleHashCleanup(pVgs);
57,800✔
4228
  mstDestroyDbVgroupsHash(pDbVgroups);
57,800✔
4229

4230
  if (code) {
57,800✔
4231
    rsp.code = code;
×
4232
    
4233
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4234

4235
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4236
  } else {
4237
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
115,600✔
4238
  }
4239

4240
_final:
57,800✔
4241

4242
  if (finalCode) {
57,800✔
4243
    tFreeSStreamMgmtRsp(&rsp);
×
4244
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4245
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4246
  }
4247

4248
  return finalCode;
57,800✔
4249
}
4250

4251
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,299✔
4252
  int32_t num = taosArrayGetSize(pList);
2,299✔
4253
  SStreamCalcScan* pScan = NULL;
2,299✔
4254
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,299✔
4255
  int64_t planUid = 0;
2,299✔
4256
  for (int32_t i = 0; i < num; ++i) {
2,299✔
4257
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,299✔
4258
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,299✔
4259
    if (0 != planUid && planUid == uid) {
2,299✔
4260
      *ppRes = pScan;
2,299✔
4261
      break;
2,299✔
4262
    }
4263
  }
4264

4265
_exit:
2,299✔
4266

4267
  if (code) {
2,299✔
4268
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4269
  }
4270

4271
  return code;
2,299✔
4272
}
4273

4274
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,299✔
4275
  int32_t code = TSDB_CODE_SUCCESS;
2,299✔
4276
  int32_t lino = 0;
2,299✔
4277
  bool    readerExists = false;
2,299✔
4278
  int64_t streamId = pTask->streamId;
2,299✔
4279
  SListNode* pNode = listHead(pStatus->calcReaders);
2,299✔
4280
  SStmTaskStatus* pReader = NULL;
2,299✔
4281
  int32_t taskIdx = 0;
2,299✔
4282

4283
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,299✔
4284
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
21,318✔
4285
    pReader = (SStmTaskStatus*)pNode->data;
19,019✔
4286
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
19,019✔
4287
      readerExists = true;
×
4288
      pAddr->taskId = pReader->id.taskId;
×
4289
      break;
×
4290
    }
4291
  }
4292

4293
  if (!readerExists) {
2,299✔
4294
    if (NULL == pStatus->calcReaders) {
2,299✔
4295
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4296
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4297
      taskIdx = 0;
×
4298
    } else {
4299
      pNode = listTail(pStatus->calcReaders);
2,299✔
4300
      pReader = (SStmTaskStatus*)pNode->data;
2,299✔
4301
      taskIdx = pReader->id.taskIdx + 1;
2,299✔
4302
    }
4303

4304
    SStreamCalcScan* pScan = NULL;
2,299✔
4305
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,299✔
4306
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,299✔
4307
    pReader = tdListReserve(pStatus->calcReaders);
2,299✔
4308
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,299✔
4309
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,299✔
4310
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,299✔
4311
    pAddr->taskId = pReader->id.taskId;
2,299✔
4312
  }
4313

4314
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,299✔
4315
  pAddr->nodeId = vgId;
2,299✔
4316

4317
_exit:
2,299✔
4318

4319
  if (code) {
2,299✔
4320
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4321
  }
4322

4323
  return code;
2,299✔
4324
}
4325

4326

4327
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,299✔
4328
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,299✔
4329
  int32_t lino = 0;
2,299✔
4330
  int32_t vgId = 0;
2,299✔
4331
  int64_t streamId = pTask->streamId;
2,299✔
4332
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,299✔
4333
  int32_t reqNum = taosArrayGetSize(pReqs);
2,299✔
4334
  SStreamOReaderDeployReq* pReq = NULL;
2,299✔
4335
  SStreamOReaderDeployRsp* pRsp = NULL;
2,299✔
4336
  SStreamMgmtRsp rsp = {0};
2,299✔
4337
  rsp.reqId = pTask->pMgmtReq->reqId;
2,299✔
4338
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,299✔
4339
  SStreamMgmtReq* pMgmtReq = NULL;
2,299✔
4340
  int8_t stopped = 0;
2,299✔
4341
  int32_t vgNum = 0;
2,299✔
4342
  SStreamTaskAddr* pAddr = NULL;
2,299✔
4343

4344
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,299✔
4345
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4346
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4347
  }
4348
  
4349
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,299✔
4350
  rsp.task = *(SStreamTask*)pTask;
2,299✔
4351

4352
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,299✔
4353
  if (NULL == pStatus) {
2,299✔
4354
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4355
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4356
  }
4357

4358
  stopped = atomic_load_8(&pStatus->stopped);
2,299✔
4359
  if (stopped) {
2,299✔
4360
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4361
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4362
  }
4363

4364
  if (reqNum <= 0) {
2,299✔
4365
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4366
    goto _exit;
×
4367
  }
4368

4369
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,299✔
4370
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,299✔
4371

4372
  for (int32_t i = 0; i < reqNum; ++i) {
4,598✔
4373
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,299✔
4374
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,299✔
4375
    pRsp->execId = pReq->execId;
2,299✔
4376
    vgNum = taosArrayGetSize(pReq->vgIds);
2,299✔
4377
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,299✔
4378
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,299✔
4379
    
4380
    for (int32_t n = 0; n < vgNum; ++n) {
4,598✔
4381
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,299✔
4382
      pAddr = taosArrayGet(pRsp->vgList, n);
2,299✔
4383
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,299✔
4384
    }
4385
  }
4386

4387
_exit:
2,299✔
4388

4389
  tFreeSStreamMgmtReq(pMgmtReq);
2,299✔
4390
  taosMemoryFree(pMgmtReq);
2,299✔
4391

4392
  if (code) {
2,299✔
4393
    rsp.code = code;
×
4394
    
4395
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4396
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4397
  } else {
4398
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,598✔
4399
  }
4400

4401

4402
_final:
2,299✔
4403

4404
  if (finalCode) {
2,299✔
4405
    tFreeSStreamMgmtRsp(&rsp);
×
4406
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4407
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4408
  }
4409

4410
  return finalCode;
2,299✔
4411
}
4412

4413

4414
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
60,099✔
4415
  int32_t code = TSDB_CODE_SUCCESS;
60,099✔
4416
  int32_t lino = 0;
60,099✔
4417

4418
  switch (pTask->pMgmtReq->type) {
60,099✔
4419
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
57,800✔
4420
      msmDeployTriggerOrigReader(pCtx, pTask);
57,800✔
4421
      break;
57,800✔
4422
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,299✔
4423
      msmDeployRunnerOrigReader(pCtx, pTask);
2,299✔
4424
      break;
2,299✔
4425
    default:
×
4426
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4427
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4428
      break;
×
4429
  }
4430

4431
_exit:
60,099✔
4432

4433
  if (code) {
60,099✔
4434
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4435
  }
4436

4437
  return code;
60,099✔
4438
}
4439

4440
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
20,920✔
4441
  int32_t code = TSDB_CODE_SUCCESS;
20,920✔
4442
  int32_t lino = 0;
20,920✔
4443
  SStreamHbMsg* pReq = pCtx->pReq;
20,920✔
4444
  SStmTaskStatusMsg* pTask = NULL;
20,920✔
4445
  
4446
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
20,920✔
4447
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
20,920✔
4448
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
20,920✔
4449
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
20,920✔
4450
  }
4451
  
4452
  for (int32_t i = 0; i < reqNum; ++i) {
81,019✔
4453
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
60,099✔
4454
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
60,099✔
4455
    if (NULL == pTask) {
60,099✔
4456
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4457
      continue;
×
4458
    }
4459

4460
    if (NULL == pTask->pMgmtReq) {
60,099✔
4461
      msttError("idx %d without mgmtReq", idx);
×
4462
      continue;
×
4463
    }
4464

4465
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
60,099✔
4466
  }
4467

4468
_exit:
20,920✔
4469

4470
  if (code) {
20,920✔
4471
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4472
  }
4473

4474
  return code;
20,920✔
4475
}
4476

4477
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
17,821,286✔
4478
  int32_t code = TSDB_CODE_SUCCESS;
17,821,286✔
4479
  int32_t lino = 0;
17,821,286✔
4480
  SStreamHbMsg* pReq = pCtx->pReq;
17,821,286✔
4481

4482
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
17,821,286✔
4483
  if (GOT_SNODE(pReq->snodeId)) {
17,680,989✔
4484
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,176,549✔
4485
  }
4486
  
4487
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
17,680,773✔
4488
    msmHandleStreamActions(pCtx);
34,529✔
4489
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
34,529✔
4490
  }
4491

4492
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
17,680,773✔
4493
    code = msmHandleStreamRequests(pCtx);
20,920✔
4494
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
20,920✔
4495
    TAOS_CHECK_EXIT(code);
20,920✔
4496
  }
4497

4498
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
17,680,773✔
4499
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
55,474✔
4500
  } else {
4501
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
17,625,299✔
4502
  }
4503

4504
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
17,680,773✔
4505
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
57,777✔
4506
  }
4507

4508
  if (taosHashGetSize(pCtx->deployStm) > 0) {
17,680,773✔
4509
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
57,130✔
4510
  }
4511

4512
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
17,680,773✔
4513
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,017,245✔
4514
  }
4515

4516
  if (taosHashGetSize(pCtx->actionStm) > 0) {
17,680,773✔
4517
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
110,473✔
4518
  }
4519

4520
_exit:
17,680,773✔
4521

4522
  if (code) {
17,821,286✔
4523
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
140,513✔
4524
  }
4525

4526
  return code;
17,821,286✔
4527
}
4528

4529
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
17,916,652✔
4530
  int32_t lino = 0;
17,916,652✔
4531
  int32_t tlen = 0;
17,916,652✔
4532
  void   *buf = NULL;
17,916,652✔
4533

4534
  if (TSDB_CODE_SUCCESS != code) {
17,916,652✔
4535
    goto _exit;
140,513✔
4536
  }
4537

4538
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
17,776,139✔
4539
  if (code < 0) {
17,776,139✔
4540
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4541
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4542
  }
4543

4544
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
17,776,139✔
4545
  if (buf == NULL) {
17,776,139✔
4546
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4547
    TAOS_CHECK_EXIT(terrno);    
×
4548
  }
4549

4550
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
17,776,139✔
4551
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
17,776,139✔
4552

4553
  SEncoder encoder;
17,525,836✔
4554
  tEncoderInit(&encoder, abuf, tlen);
17,776,139✔
4555
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
17,776,139✔
4556
    rpcFreeCont(buf);
×
4557
    buf = NULL;
×
4558
    tEncoderClear(&encoder);
×
4559
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4560
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4561
  }
4562
  tEncoderClear(&encoder);
17,776,139✔
4563

4564
_exit:
17,916,652✔
4565

4566
  pMsg->code = code;
17,916,652✔
4567
  pMsg->info = *pRpcInfo;
17,916,652✔
4568
  if (TSDB_CODE_SUCCESS == code) {
17,916,652✔
4569
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
17,776,139✔
4570
    pMsg->pCont = buf;
17,776,139✔
4571
  }
4572
}
17,916,652✔
4573

4574

4575
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
17,916,652✔
4576
  int32_t code = TSDB_CODE_SUCCESS;
17,916,652✔
4577
  SMStreamHbRspMsg rsp = {0};
17,916,652✔
4578
  rsp.streamGId = pHb->streamGId;
17,916,652✔
4579

4580
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
17,916,652✔
4581

4582
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
17,916,652✔
4583
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
81,198✔
4584
    goto _exit;
81,198✔
4585
  }
4586

4587
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
17,835,454✔
4588
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
17,835,454✔
4589

4590
  pCtx->tidx = tidx;
17,835,454✔
4591
  pCtx->pMnode = pMnode;
17,835,454✔
4592
  pCtx->currTs = currTs;
17,835,454✔
4593
  pCtx->pReq = pHb;
17,835,454✔
4594
  pCtx->pRsp = &rsp;
17,835,454✔
4595
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
17,835,454✔
4596
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
17,835,454✔
4597
  
4598
  switch (atomic_load_8(&mStreamMgmt.state)) {
17,835,454✔
4599
    case MND_STM_STATE_WATCH:
14,168✔
4600
      code = msmWatchHandleHbMsg(pCtx);
14,168✔
4601
      break;
14,168✔
4602
    case MND_STM_STATE_NORMAL:
17,821,286✔
4603
      code = msmNormalHandleHbMsg(pCtx);
17,821,286✔
4604
      break;
17,821,286✔
4605
    default:
×
4606
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4607
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4608
      break;
×
4609
  }
4610

4611
_exit:
17,916,652✔
4612

4613
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
17,916,652✔
4614

4615
  msmCleanStreamGrpCtx(pHb);
17,916,652✔
4616
  msmClearStreamToDeployMaps(pHb);
17,916,652✔
4617

4618
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
17,916,652✔
4619
  
4620
  tFreeSMStreamHbRspMsg(&rsp);
17,916,652✔
4621

4622
  return code;
17,916,652✔
4623
}
4624

4625
void msmHandleBecomeLeader(SMnode *pMnode) {
363,879✔
4626
  if (tsDisableStream) {
363,879✔
4627
    return;
×
4628
  }
4629

4630
  mstInfo("start to process mnode become leader");
363,879✔
4631

4632
  int32_t code = 0;
363,879✔
4633
  streamAddVnodeLeader(MNODE_HANDLE);
363,879✔
4634
  
4635
  taosWLockLatch(&mStreamMgmt.runtimeLock);
363,879✔
4636
  msmDestroyRuntimeInfo(pMnode);
363,879✔
4637
  code = msmInitRuntimeInfo(pMnode);
363,879✔
4638
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
363,879✔
4639

4640
  if (TSDB_CODE_SUCCESS == code) {
363,879✔
4641
    atomic_store_8(&mStreamMgmt.active, 1);
363,879✔
4642
  }
4643

4644
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
363,879✔
4645
}
4646

4647
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
450,708✔
4648
  if (tsDisableStream) {
450,708✔
4649
    return;
×
4650
  }
4651

4652
  mstInfo("start to process mnode become not leader");
450,708✔
4653

4654
  streamRemoveVnodeLeader(MNODE_HANDLE);
450,708✔
4655

4656
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
450,708✔
4657
    taosWLockLatch(&mStreamMgmt.runtimeLock);
363,879✔
4658
    msmDestroyRuntimeInfo(pMnode);
363,879✔
4659
    mStreamMgmt.stat.inactiveTimes++;
363,879✔
4660
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
363,879✔
4661
  }
4662
}
4663

4664

4665
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4666
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4667
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4668
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4669
  } else {
4670
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4671
  }
4672
}
×
4673

4674
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
7,240✔
4675
  int32_t code = TSDB_CODE_SUCCESS;
7,240✔
4676
  int32_t lino = 0;
7,240✔
4677
  SStreamObj* pStream = pObj;
7,240✔
4678
  SSnodeObj* pSnode = p1;
7,240✔
4679
  SArray** ppRes = p2;
7,240✔
4680

4681
  if (pStream->mainSnodeId == pSnode->id) {
7,240✔
4682
    if (NULL == *ppRes) {
4,275✔
4683
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
765✔
4684
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
765✔
4685
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
765✔
4686
    }
4687

4688
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
8,550✔
4689
  }
4690

4691
  return true;
7,240✔
4692

4693
_exit:
×
4694

4695
  if (code) {
×
4696
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4697
  }  
4698

4699
  *(int32_t*)p3 = code;
×
4700

4701
  return false;
×
4702
}
4703

4704

4705
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
33,128✔
4706
  int32_t code = TSDB_CODE_SUCCESS;
33,128✔
4707
  int32_t lino = 0;
33,128✔
4708
  
4709
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
33,128✔
4710
  TAOS_CHECK_EXIT(code);
33,128✔
4711

4712
  int32_t streamNum = taosArrayGetSize(*ppRes);
33,128✔
4713
  if (streamNum > 0 && 0 == pSnode->replicaId) {
33,128✔
4714
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
190✔
4715
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
190✔
4716
  }
4717

4718
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4719

4720
_exit:
33,128✔
4721

4722
  if (code) {
33,128✔
4723
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
190✔
4724
  }  
4725

4726
  return code;
33,128✔
4727
}
4728

4729
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
380,503✔
4730
  SStreamObj* pStream = pObj;
380,503✔
4731
  int64_t streamId = pStream->pCreate->streamId;
380,503✔
4732
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
380,503✔
4733
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
380,503✔
4734
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
380,503✔
4735
  
4736
  if ((userDropped || userStopped) && (NULL == pStatus)) {
380,503✔
4737
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
380✔
4738
    return true;
380✔
4739
  }
4740
  
4741
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
380,123✔
4742
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
260,707✔
4743
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4744
    return true;
260,707✔
4745
  }
4746

4747
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
119,416✔
4748
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
35,250✔
4749
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4750
    return true;
35,250✔
4751
  }  
4752

4753
  if (pStatus) {
84,166✔
4754
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
83,074✔
4755
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4756
    }
4757

4758
    return true;
83,074✔
4759
  }
4760

4761
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
1,092✔
4762

4763
  return true;
1,092✔
4764
}
4765

4766
void msmCheckLoopStreamMap(SMnode *pMnode) {
37,955✔
4767
  SStmStatus* pStatus = NULL;
37,955✔
4768
  void* pIter = NULL;
37,955✔
4769
  int8_t stopped = 0;
37,955✔
4770
  int64_t streamId = 0;
37,955✔
4771
  
4772
  while (true) {
4773
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
371,722✔
4774
    if (NULL == pIter) {
371,722✔
4775
      break;
37,955✔
4776
    }
4777

4778
    pStatus = (SStmStatus*)pIter;
333,767✔
4779

4780
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
333,767✔
4781
    stopped = atomic_load_8(&pStatus->stopped);
333,767✔
4782
    if (MST_IS_USER_STOPPED(stopped)) {
333,767✔
4783
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
1,814✔
4784
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
1,814✔
4785
      continue;
1,814✔
4786
    }
4787

4788
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
331,953✔
4789
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4790
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4791
      continue;
×
4792
    }
4793

4794
    if (MST_IS_ERROR_STOPPED(stopped)) {
331,953✔
4795
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
12,909✔
4796
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
8,606✔
4797
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4798
            
4799
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
8,606✔
4800
        continue;
8,606✔
4801
      }
4802

4803
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
4,303✔
4804
      continue;
4,303✔
4805
    }
4806

4807
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
319,044✔
4808
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4809
      continue;
×
4810
    }
4811
  }
4812
}
37,955✔
4813

4814
void msmCheckStreamsStatus(SMnode *pMnode) {
312,575✔
4815
  SStmCheckStatusCtx ctx = {0};
312,575✔
4816

4817
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
312,575✔
4818
  
4819
  if (MST_READY_FOR_SDB_LOOP()) {
312,575✔
4820
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
51,863✔
4821
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
51,863✔
4822
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
51,863✔
4823
  }
4824

4825
  if (MST_READY_FOR_MAP_LOOP()) {
312,575✔
4826
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
37,955✔
4827
    msmCheckLoopStreamMap(pMnode);
37,955✔
4828
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
37,955✔
4829
  }
4830
}
312,575✔
4831

4832
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
4,147,202✔
4833
  for (int32_t i = 0; i < taskNum; ++i) {
8,495,736✔
4834
    SStmTaskStatus* pTask = *(pList + i);
4,349,421✔
4835

4836
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
4,349,421✔
4837
      continue;
65,913✔
4838
    }
4839
    
4840
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
4,285,006✔
4841
      continue;
4,282,759✔
4842
    }
4843

4844
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
2,247✔
4845
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
2,247✔
4846
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
887✔
4847
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4848
          
4849
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
887✔
4850
      break;
887✔
4851
    }
4852

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

4856
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,360✔
4857
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,360✔
4858

4859
    SStmTaskAction task = {0};
1,360✔
4860
    task.streamId = streamId;
1,360✔
4861
    task.id = pTask->id;
1,360✔
4862
    task.flag = pTask->flags;
1,360✔
4863
    task.type = pTask->type;
1,360✔
4864
    
4865
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,360✔
4866
  }
4867
}
4,147,202✔
4868

4869
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
128,196✔
4870
  void* pIter = NULL;
128,196✔
4871
  SStmVgStreamStatus* pVg = NULL;
128,196✔
4872
  int64_t streamId = 0;
128,196✔
4873
  
4874
  while (true) {
1,727,476✔
4875
    pIter = taosHashIterate(pStreams, pIter);
1,855,672✔
4876
    if (NULL == pIter) {
1,855,672✔
4877
      break;
128,196✔
4878
    }
4879

4880
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,727,476✔
4881
    pVg = (SStmVgStreamStatus*)pIter;
1,727,476✔
4882

4883
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,727,476✔
4884
    if (taskNum > 0) {
1,727,476✔
4885
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,002,769✔
4886
    }
4887

4888
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,727,476✔
4889
    if (taskNum > 0) {
1,727,476✔
4890
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,129,549✔
4891
    }
4892
  }
4893
}
128,196✔
4894

4895
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4896
  int64_t streamId = 0;
×
4897
  void* pIter = NULL;
×
4898
  SStmVgStreamStatus* pStream = NULL;
×
4899

4900
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4901
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4902
    return;
×
4903
  }
4904

4905
  
4906
  while (true) {
4907
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4908
    if (NULL == pIter) {
×
4909
      break;
×
4910
    }
4911

4912
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4913
    
4914
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4915
  }
4916

4917
  taosHashClear(pVg->streamTasks);
×
4918
}
4919

4920

4921
void msmCheckVgroupStatus(SMnode *pMnode) {
312,575✔
4922
  void* pIter = NULL;
312,575✔
4923
  int32_t code = 0;
312,575✔
4924
  
4925
  while (true) {
1,189,123✔
4926
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,501,698✔
4927
    if (NULL == pIter) {
1,501,698✔
4928
      break;
312,575✔
4929
    }
4930

4931
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,189,123✔
4932
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,189,123✔
4933
      continue;
1,058,895✔
4934
    }
4935
    
4936
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
130,228✔
4937

4938
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
130,228✔
4939
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
2,032✔
4940
      if (NULL == pVgroup) {
2,032✔
4941
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
2,032✔
4942
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
2,032✔
4943
        if (code) {
2,032✔
4944
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4945
        }
4946
        continue;
2,032✔
4947
      }
4948
      mndReleaseVgroup(pMnode, pVgroup);
×
4949
      
4950
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4951
      
4952
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
4953
      continue;
×
4954
    }
4955

4956
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
128,196✔
4957

4958
    msmCheckVgroupStreamStatus(pVg->streamTasks);
128,196✔
4959
  }
4960
}
312,575✔
4961

4962
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
980✔
4963
  *deployNum = 0;
980✔
4964
  
4965
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
3,450✔
4966
    if (pStream->runners[i]) {
2,705✔
4967
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
980✔
4968
      for (int32_t t = 0; t < taskNum; ++t) {
1,725✔
4969
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
980✔
4970
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
980✔
4971
        if (stopped) {
980✔
4972
          mstsDebug("stream already stopped %d, ignore it", stopped);
235✔
4973
          *deployNum = 0;
235✔
4974
          return;
235✔
4975
        }
4976

4977
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
745✔
4978
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
745✔
4979
      }
4980
      
4981
      deployId[*deployNum] = i;
745✔
4982
      (*deployNum)++;
745✔
4983
    }
4984
  }
4985
}
4986

4987
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
1,790✔
4988
  pSnode->runnerThreadNum = -1;
1,790✔
4989

4990
  (void)msmSTAddSnodesToMap(pMnode);
1,790✔
4991

4992
  int64_t streamId = 0;
1,790✔
4993
  void* pIter = NULL;
1,790✔
4994
  SStmSnodeStreamStatus* pStream = NULL;
1,790✔
4995
  int32_t deployNum = 0;
1,790✔
4996
  SStmTaskAction task = {0};
1,790✔
4997
  
4998
  while (true) {
4999
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
3,645✔
5000
    if (NULL == pIter) {
3,645✔
5001
      break;
1,790✔
5002
    }
5003

5004
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,855✔
5005
    
5006
    task.streamId = streamId;
1,855✔
5007
    
5008
    pStream = (SStmSnodeStreamStatus*)pIter;
1,855✔
5009
    if (pStream->trigger) {
1,855✔
5010
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
875✔
5011
      if (stopped) {
875✔
5012
        mstsDebug("stream already stopped %d, ignore it", stopped);
705✔
5013
        continue;
705✔
5014
      }
5015

5016
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
170✔
5017
      
5018
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
170✔
5019
    } else {
5020
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
980✔
5021
      
5022
      if (task.deployNum > 0) {
980✔
5023
        //task.triggerStatus = pStream->trigger;
5024
        task.multiRunner = true;
745✔
5025
        task.type = STREAM_RUNNER_TASK;
745✔
5026
        
5027
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
745✔
5028
        
5029
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
745✔
5030
      }
5031
    }
5032
  }
5033

5034
  taosHashClear(pSnode->streamTasks);
1,790✔
5035
}
1,790✔
5036

5037

5038
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
35,442✔
5039
  void* pIter = NULL;
35,442✔
5040
  SStmSnodeStreamStatus* pSnode = NULL;
35,442✔
5041
  int64_t streamId = 0;
35,442✔
5042
  
5043
  while (true) {
5044
    pIter = taosHashIterate(pStreams, pIter);
565,425✔
5045
    if (NULL == pIter) {
565,425✔
5046
      break;
35,442✔
5047
    }
5048

5049
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
529,983✔
5050
    pSnode = (SStmSnodeStreamStatus*)pIter;
529,983✔
5051

5052
    if (NULL != pSnode->trigger) {
529,983✔
5053
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
520,173✔
5054
    }
5055

5056
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,119,932✔
5057
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,589,949✔
5058
      if (taskNum > 0) {
1,589,949✔
5059
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,494,711✔
5060
      }
5061
    }
5062
  }
5063
}
35,442✔
5064

5065

5066
void msmCheckSnodeStatus(SMnode *pMnode) {
312,575✔
5067
  void* pIter = NULL;
312,575✔
5068
  
5069
  while (true) {
386,415✔
5070
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
698,990✔
5071
    if (NULL == pIter) {
698,990✔
5072
      break;
312,575✔
5073
    }
5074

5075
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
386,415✔
5076
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
386,415✔
5077
      continue;
340,810✔
5078
    }
5079

5080
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
45,605✔
5081
    
5082
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
45,605✔
5083
    if (NULL == pSnode->streamTasks) {
45,605✔
5084
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
8,778✔
5085
      continue;
8,778✔
5086
    }
5087
    
5088
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
36,827✔
5089
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,385✔
5090
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5091
      
5092
      msmHandleSnodeLost(pMnode, pSnode);
1,385✔
5093
      continue;
1,385✔
5094
    }
5095
    
5096
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
35,442✔
5097

5098
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
35,442✔
5099
  }
5100
}
312,575✔
5101

5102

5103
void msmCheckTasksStatus(SMnode *pMnode) {
312,575✔
5104
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
312,575✔
5105

5106
  msmCheckVgroupStatus(pMnode);
312,575✔
5107
  msmCheckSnodeStatus(pMnode);
312,575✔
5108
}
312,575✔
5109

5110
void msmCheckSnodesState(SMnode *pMnode) {
312,575✔
5111
  if (!MST_READY_FOR_SNODE_LOOP()) {
312,575✔
5112
    return;
281,560✔
5113
  }
5114

5115
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
31,015✔
5116

5117
  void* pIter = NULL;
31,015✔
5118
  int32_t snodeId = 0;
31,015✔
5119
  while (true) {
34,895✔
5120
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
65,910✔
5121
    if (NULL == pIter) {
65,910✔
5122
      break;
31,015✔
5123
    }
5124

5125
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
34,895✔
5126
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
34,895✔
5127
      continue;
34,255✔
5128
    }
5129

5130
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
640✔
5131
    if (NULL == pSnode->streamTasks) {
640✔
5132
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
235✔
5133
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
235✔
5134
      continue;
235✔
5135
    }
5136
    
5137
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
405✔
5138
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5139
    
5140
    msmHandleSnodeLost(pMnode, pSnode);
405✔
5141
  }
5142

5143
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
31,015✔
5144
}
5145

5146
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
11,481,258✔
5147
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
11,481,258✔
5148
  if (0 == active || MND_STM_STATE_NORMAL != state) {
11,481,258✔
5149
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,604✔
5150
    return false;
2,604✔
5151
  }
5152

5153
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
11,478,654✔
5154
    mstTrace("ignore health check since no stream now");
10,853,504✔
5155
    return false;
10,853,504✔
5156
  }
5157

5158
  return true;
625,150✔
5159
}
5160

5161
void msmHealthCheck(SMnode *pMnode) {
11,168,683✔
5162
  if (!msmCheckNeedHealthCheck(pMnode)) {
11,168,683✔
5163
    return;
10,856,108✔
5164
  }
5165

5166
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
599,646✔
5167
  
5168
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
312,575✔
5169
  if (!msmCheckNeedHealthCheck(pMnode)) {
312,575✔
5170
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5171
    return;
×
5172
  }
5173
  
5174
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
312,575✔
5175
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
312,575✔
5176

5177
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
312,575✔
5178
  
5179
  msmCheckStreamsStatus(pMnode);
312,575✔
5180
  msmCheckTasksStatus(pMnode);
312,575✔
5181
  msmCheckSnodesState(pMnode);
312,575✔
5182

5183
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
312,575✔
5184

5185
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
312,575✔
5186
}
5187

5188
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
807✔
5189
  SStreamObj *pStream = pObj;
807✔
5190
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
807✔
5191
    return true;
×
5192
  }
5193
  
5194
  pStream->updateTime = *(int64_t*)p1;
807✔
5195
  
5196
  (*(int32_t*)p2)++;
807✔
5197
  
5198
  return true;
807✔
5199
}
5200

5201
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5202
  int32_t code = 0;
×
5203
  int8_t  stopped = 0;
×
5204
  
5205
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5206
  
5207
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5208
  if (NULL == pStatus) {
×
5209
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5210
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5211
    goto _exit;
×
5212
  }
5213

5214
  stopped = atomic_load_8(&pStatus->stopped);
×
5215
  if (stopped) {
×
5216
    mstsError("stream already stopped, stopped:%d", stopped);
×
5217
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5218
    goto _exit;
×
5219
  }
5220

5221
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5222
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5223
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5224
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5225
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5226
    goto _exit;
×
5227
  }
5228

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

5232
_exit:
×
5233
  
5234
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5235

5236
  return code;
×
5237
}
5238

5239
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
363,879✔
5240
  int32_t code = TSDB_CODE_SUCCESS;
363,879✔
5241
  int32_t lino = 0;
363,879✔
5242
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
363,879✔
5243
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
363,879✔
5244
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
363,879✔
5245

5246
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
727,703✔
5247

5248
  mStreamMgmt.stat.activeTimes++;
363,879✔
5249
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
363,879✔
5250
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
363,879✔
5251
  if (NULL == mStreamMgmt.tCtx) {
363,879✔
5252
    code = terrno;
×
5253
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5254
    goto _exit;
×
5255
  }
5256

5257
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
363,879✔
5258
  if (mStreamMgmt.actionQ == NULL) {
363,879✔
5259
    code = terrno;
×
5260
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5261
    goto _exit;
×
5262
  }
5263
  
5264
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
363,879✔
5265
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
363,879✔
5266
  
5267
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
363,879✔
5268
  
5269
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,183,084✔
5270
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,819,205✔
5271

5272
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,915,230✔
5273
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,096,025✔
5274
      if (pCtx->deployStm[m] == NULL) {
9,096,025✔
5275
        code = terrno;
×
5276
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5277
        goto _exit;
×
5278
      }
5279
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
9,096,025✔
5280
      
5281
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,096,025✔
5282
      if (pCtx->actionStm[m] == NULL) {
9,096,025✔
5283
        code = terrno;
×
5284
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5285
        goto _exit;
×
5286
      }
5287
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
9,096,025✔
5288
    }
5289
  }
5290
  
5291
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
363,879✔
5292
  if (mStreamMgmt.streamMap == NULL) {
363,879✔
5293
    code = terrno;
×
5294
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5295
    goto _exit;
×
5296
  }
5297
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
363,879✔
5298
  
5299
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
363,879✔
5300
  if (mStreamMgmt.taskMap == NULL) {
363,879✔
5301
    code = terrno;
×
5302
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5303
    goto _exit;
×
5304
  }
5305
  
5306
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
363,879✔
5307
  if (mStreamMgmt.vgroupMap == NULL) {
363,879✔
5308
    code = terrno;
×
5309
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5310
    goto _exit;
×
5311
  }
5312
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
363,879✔
5313

5314
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
363,879✔
5315
  if (mStreamMgmt.snodeMap == NULL) {
363,879✔
5316
    code = terrno;
×
5317
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5318
    goto _exit;
×
5319
  }
5320
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
363,879✔
5321
  
5322
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
363,879✔
5323
  if (mStreamMgmt.dnodeMap == NULL) {
363,879✔
5324
    code = terrno;
×
5325
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5326
    goto _exit;
×
5327
  }
5328

5329
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
363,879✔
5330
  if (mStreamMgmt.toDeployVgMap == NULL) {
363,879✔
5331
    code = terrno;
×
5332
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5333
    goto _exit;
×
5334
  }
5335
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
363,879✔
5336
  
5337
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
363,879✔
5338
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
363,879✔
5339
    code = terrno;
×
5340
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5341
    goto _exit;
×
5342
  }
5343
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
363,879✔
5344

5345
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
363,879✔
5346
  if (mStreamMgmt.toUpdateScanMap == NULL) {
363,879✔
5347
    code = terrno;
×
5348
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5349
    goto _exit;
×
5350
  }
5351
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
363,879✔
5352

5353
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
363,879✔
5354
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
363,879✔
5355

5356
  mStreamMgmt.lastTaskId = 1;
363,879✔
5357

5358
  int32_t activeStreamNum = 0;
363,879✔
5359
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
363,879✔
5360

5361
  if (activeStreamNum > 0) {
363,879✔
5362
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
651✔
5363
  } else {
5364
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
363,228✔
5365
  }
5366

5367
_exit:
363,879✔
5368

5369
  if (code) {
363,879✔
5370
    msmDestroyRuntimeInfo(pMnode);
×
5371
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5372
  } else {
5373
    mstInfo("mnode stream runtime init done");
363,879✔
5374
  }
5375

5376
  return code;
363,879✔
5377
}
5378

5379

5380

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