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

taosdata / TDengine / #4877

11 Dec 2025 02:43AM UTC coverage: 64.586% (-0.05%) from 64.632%
#4877

push

travis-ci

guanshengliang
feat(TS-7270): internal dependence

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

673 existing lines in 130 files now uncovered.

163673 of 253417 relevant lines covered (64.59%)

105540806.95 hits per line

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

76.44
/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() {
839,712✔
34
  SStmQNode* pQNode = NULL;
839,712✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
839,712✔
37
    return;
419,856✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,098,910✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,593,460✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,494,550✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,494,550✔
51
  }
52
}
2,098,910✔
53

54
void msmDestroyThreadCtxs() {
839,712✔
55
  if (NULL == mStreamMgmt.tCtx) {
839,712✔
56
    return;
419,856✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,518,766✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,098,910✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
419,856✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
839,712✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
839,712✔
94
}
839,712✔
95

96

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

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

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

111
    pStatus = pStream;
6,479✔
112
  }
113

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

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

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

136
_exit:
398✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
13,904✔
139

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

145

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

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

200
  pSnode = NULL;
455,864✔
201

202
_exit:
455,864✔
203

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

208
  return code;
455,864✔
209
}
210

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

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

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

235
  pDnode = NULL;
1,150,945✔
236

237
_exit:
1,150,945✔
238

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

243
  return code;
1,150,945✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
606,359✔
275
  int32_t code = TSDB_CODE_SUCCESS;
606,359✔
276
  int32_t lino = 0;
606,359✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
606,359✔
278
  if (NULL == pStream) {
606,359✔
279
    SStmVgStreamStatus stream = {0};
462,028✔
280
    if (trigReader) {
462,028✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
305,112✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
305,112✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
610,224✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
156,916✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
156,916✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
313,832✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
462,028✔
290
    goto _exit;
462,028✔
291
  }
292
  
293
  if (trigReader) {
144,331✔
294
    if (NULL == pStream->trigReaders) {
22,229✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
22,229✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
22,229✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
44,458✔
300
    goto _exit;
22,229✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
122,102✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
91,257✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
91,257✔
306
  }
307

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

310
_exit:
122,102✔
311

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

320
  return code;
606,359✔
321
}
322

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

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

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

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

350
  return code;
606,359✔
351
}
352

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

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

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

404
  return code;
609,191✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
877,903✔
409
  int32_t code = TSDB_CODE_SUCCESS;
877,903✔
410
  int32_t lino = 0;
877,903✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
877,903✔
412
  if (NULL == pStream) {
877,903✔
413
    SStmSnodeStreamStatus stream = {0};
225,226✔
414
    if (deployId < 0) {
225,226✔
415
      stream.trigger = pStatus;
3,259✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
221,967✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
221,967✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
443,934✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
225,226✔
423
    goto _exit;
225,226✔
424
  }
425
  
426
  if (deployId < 0) {
652,677✔
427
    if (NULL != pStream->trigger) {
210,228✔
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;
210,228✔
433
    goto _exit;
210,228✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
442,449✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
409,425✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
409,425✔
439
  }
440

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

443
_exit:
442,449✔
444

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

453
  return code;
877,903✔
454
}
455

456

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

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

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

484
  return code;
877,903✔
485
}
486

487

488

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

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

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

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

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

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

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

550
  return code;
213,487✔
551
}
552

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

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

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

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

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

614
  return code;
664,416✔
615
}
616

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

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

632
_exit:
631,392✔
633

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

638
  return code;
631,392✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
18,635✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
762✔
682

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

687
  return code;  
762✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
98,910✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
15,948,944✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
56,688,974✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
40,740,030✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
40,740,030✔
725
  }
726

727
_exit:
15,948,944✔
728

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

733
  return code;
15,948,944✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
404,953✔
739
  int32_t num = taosArrayGetSize(pList);
404,953✔
740
  for (int32_t i = 0; i < num; ++i) {
1,026,478✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
849,660✔
742
    if (pScan->readFromCache) {
849,660✔
743
      return pScan->scanPlan;
228,135✔
744
    }
745
  }
746

747
  return NULL;
176,818✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
609,191✔
775
}
776

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

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

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

805
_exit:
210,228✔
806

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

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

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

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

875
  SStreamTaskAddr addr;
209,826✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
213,487✔
877
  if (triggerReaderNum > 0) {
213,487✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
212,372✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
212,372✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
478,700✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
265,213✔
884
    addr.taskId = pStatus->id.taskId;
265,213✔
885
    addr.nodeId = pStatus->id.nodeId;
265,213✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
265,213✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
530,426✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
265,213✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
213,487✔
892
  pMsg->streamName = pInfo->streamName;
213,487✔
893

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

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

901
_exit:
210,228✔
902

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

912

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

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

944
_exit:
664,416✔
945

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

953

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

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

977

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

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

995
  return code;
844,879✔
996
}
997

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

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

1006

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

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

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

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

1031
_exit:
1,096,758✔
1032

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

1037
  return code;
1,096,758✔
1038
}
1039

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

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

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

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

1078
_exit:
×
1079

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

1084
  return 0;
×
1085
}
1086

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

1101
  int32_t snodeTarget = taosRand() % snodeNum;
640,709✔
1102

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

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

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

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

1140
_exit:
644,949✔
1141

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

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

1150
  return snodeId;
644,949✔
1151
}
1152

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

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

1164
_exit:
844,879✔
1165

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

1170
  return snodeId;
844,879✔
1171
}
1172

1173

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

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

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

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

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

1206
_exit:
213,487✔
1207

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

1212
  return code;
213,487✔
1213
}
1214

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

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

1241
_exit:
327,341✔
1242

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

1247
  return code;
327,341✔
1248
}
1249

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

1279
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
114,592✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
114,592✔
1281
      
1282
      void *pIter = NULL;
114,592✔
1283
      while (1) {
602,204✔
1284
        SStmTaskDeploy info = {0};
716,796✔
1285
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
716,796✔
1286
        if (pIter == NULL) {
716,796✔
1287
          break;
114,592✔
1288
        }
1289
      
1290
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
602,204✔
1291
          pState = taosArrayReserve(pInfo->trigReaders, 1);
167,433✔
1292

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

1302
        sdbRelease(pSdb, pVgroup);
602,204✔
1303
      }
1304
      break;
114,592✔
1305
    }
1306
    default:
1,115✔
1307
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,115✔
1308
      break;
1,115✔
1309
  }
1310

1311
_exit:
213,487✔
1312

1313
  mndReleaseDb(pCtx->pMnode, pDb);
213,487✔
1314

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

1319
  return code;
213,487✔
1320
}
1321

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

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

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

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

1363
  nodesDestroyNode((SNode*)pSubplan);
275,447✔
1364

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

1369
  return code;
275,447✔
1370
}
1371

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

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

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

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

1408
  return code;
104,341✔
1409
}
1410

1411

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

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

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

1440
_exit:
279,018✔
1441

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

1446
  return code;
279,018✔
1447
}
1448

1449

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

1459
  
1460
  for (int32_t i = 0; i < calcTasksNum; ++i) {
592,567✔
1461
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
379,080✔
1462
    if (pScan->readFromCache) {
379,080✔
1463
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
104,341✔
1464
      continue;
104,341✔
1465
    }
1466
    
1467
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
274,739✔
1468
    for (int32_t m = 0; m < vgNum; ++m) {
549,478✔
1469
      pState = tdListReserve(pInfo->calcReaders);
274,739✔
1470
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
274,739✔
1471

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

1477
_exit:
213,487✔
1478

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

1483
  return code;
213,487✔
1484
}
1485

1486

1487

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

1515
_exit:
708✔
1516

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

1521
  return code;
708✔
1522
}
1523

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

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

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

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

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

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

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

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

1582
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1583
}
1584

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

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

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

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

1618
_exit:
664,416✔
1619

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

1624
  return code;
664,416✔
1625
}
1626

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

1635
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
664,416✔
1636

1637
  SNode* pTmp = NULL;
664,416✔
1638
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,835,388✔
1639
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,170,972✔
1640
      ERASE_NODE(pPlan->pChildren);
1,137,948✔
1641
      continue;
1,137,948✔
1642
    }
1643
    WHERE_NEXT;
33,024✔
1644
  }
1645
  nodesClearList(pPlan->pChildren);
664,416✔
1646
  pPlan->pChildren = NULL;
664,416✔
1647

1648
  if (NULL == pPlan->pParents) {
664,416✔
1649
    goto _exit;
631,392✔
1650
  }
1651

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

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

1671
  return code;
664,416✔
1672
}
1673

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

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

1689
_exit:
631,392✔
1690

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

1695
  return code;
631,392✔
1696
}
1697

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

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

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

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

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

1741
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
210,228✔
1742
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
210,228✔
1743
  
1744
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
840,912✔
1745
    totalTaskNum = 0;
630,684✔
1746

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

1753
    taskIdx = 0;
630,684✔
1754
    
1755
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,277,880✔
1756
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
647,196✔
1757
      if (NULL == plans) {
647,196✔
1758
        mstsError("empty level plan, level:%d", i);
×
1759
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1760
      }
1761

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

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

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

1779
      for (int32_t n = 0; n < taskNum; ++n) {
1,310,904✔
1780
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
663,708✔
1781
        pState = taosArrayReserve(deployList, 1);
663,708✔
1782

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

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

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

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

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

1818
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
630,684✔
1819

1820
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
630,684✔
1821

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

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

1830
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
840,912✔
1831
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
630,684✔
1832
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
630,684✔
1833
  }
1834
  
1835
  pInfo->runnerNum = totalTaskNum;
210,228✔
1836
  
1837
_exit:
210,228✔
1838

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

1843
  taosArrayDestroy(deployTaskList);
210,228✔
1844
  nodesDestroyNode((SNode *)pDag);
210,228✔
1845

1846
  return code;
210,228✔
1847
}
1848

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

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

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

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

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

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

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

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

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

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

1909
        pRunner++;
708✔
1910
      }
1911

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

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

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

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

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

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

1927
_exit:
708✔
1928

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

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

1936
  return code;
708✔
1937
}
1938

1939

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

1948
_exit:
194,460✔
1949

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

1954
  return code;
194,460✔
1955
}
1956

1957

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

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

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

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

1980
  taosHashClear(mStreamMgmt.toUpdateScanMap);
210,228✔
1981
  mStreamMgmt.toUpdateScanNum = 0;
210,228✔
1982

1983
_exit:
210,228✔
1984

1985
  nodesDestroyNode((SNode *)pPlan);
210,228✔
1986

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

1991
  return code;
210,228✔
1992
}
1993

1994

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

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

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

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

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

2018
  return code;
213,487✔
2019
}
2020

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

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

2060
_exit:
×
2061

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

2067
  return code;
×
2068
}
2069

2070

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

2076
  pStatus->lastActionTs = INT64_MIN;
197,719✔
2077

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

2083
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
197,719✔
2084
  
2085
  if (pStream->pCreate->numOfCalcSubplan > 0) {
197,719✔
2086
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
194,460✔
2087
    
2088
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
194,460✔
2089
  }
2090

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

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

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

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

2115
  return code;
197,719✔
2116
}
2117

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

2124
  if (NULL == pStatus) {
213,487✔
2125
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
197,719✔
2126

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

2129
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
197,719✔
2130
  }
2131
  
2132
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
213,487✔
2133

2134
  mstLogSStmStatus("stream deployed", streamId, pStatus);
213,487✔
2135

2136
_exit:
213,487✔
2137

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

2147
  return code;
213,487✔
2148
}
2149

2150

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

2155
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
34,174✔
2156
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
15,208✔
2157
    (void)mstWaitLock(&pVg->lock, true);
15,208✔
2158

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

2165
    for (int32_t i = 0; i < taskNum; ++i) {
68,042✔
2166
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
52,834✔
2167
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
52,834✔
2168
        continue;
52,126✔
2169
      }
2170

2171
      mstDestroySStmTaskToDeployExt(pExt);
708✔
2172
      pExt->deployed = true;
708✔
2173
    }
2174
    
2175
    taosRUnLockLatch(&pVg->lock);
15,208✔
2176
  }
2177

2178
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
35,544✔
2179
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
16,578✔
2180
    (void)mstWaitLock(&pSnode->lock, true);
16,578✔
2181

2182
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
16,578✔
2183
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
16,578✔
2184
      for (int32_t i = 0; i < taskNum; ++i) {
52,605✔
2185
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
37,089✔
2186
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
37,089✔
2187
          continue;
35,622✔
2188
        }
2189
        
2190
        mstDestroySStmTaskToDeployExt(pExt);
1,467✔
2191
        pExt->deployed = true;
1,467✔
2192
      }
2193
    }
2194

2195
    taskNum = taosArrayGetSize(pSnode->runnerList);
16,578✔
2196
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
16,578✔
2197
      for (int32_t i = 0; i < taskNum; ++i) {
122,175✔
2198
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
105,597✔
2199
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
105,597✔
2200
          continue;
102,006✔
2201
        }
2202
        
2203
        mstDestroySStmTaskToDeployExt(pExt);
3,591✔
2204
        pExt->deployed = true;
3,591✔
2205
      }
2206
    }
2207

2208
    taosRUnLockLatch(&pSnode->lock);
16,578✔
2209
  }
2210

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

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

2228
  size_t keyLen = 0;
18,966✔
2229
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
4,144,978✔
2230
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
4,126,012✔
2231
    if (*pStreamId == streamId) {
4,126,012✔
2232
      int64_t taskId = *(pStreamId + 1);
114,148✔
2233
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
114,148✔
2234
      if (code) {
114,148✔
2235
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2236
      } else {
2237
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
114,148✔
2238
      }
2239
    }
2240
  }
2241

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

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

2259
  mstResetSStmStatus(pStatus);
15,768✔
2260

2261
  pStatus->deployTimes++;
15,768✔
2262
}
15,768✔
2263

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

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

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

2297
  TAOS_CHECK_EXIT(code);
213,487✔
2298

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

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

2315
_exit:
213,487✔
2316

2317
  mndReleaseStream(pCtx->pMnode, pStream);
213,487✔
2318

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

2323
  return code;
213,487✔
2324
}
2325

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

2358
_exit:
2,832✔
2359

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

2364
  return code;
2,832✔
2365
}
2366

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

2398
_exit:
2399

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

2404
  return code;
2405
}
2406
*/
2407

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

2457
_exit:
708✔
2458

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

2463
  return code;
708✔
2464
}
2465

2466

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

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

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

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

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

2494
  TAOS_CHECK_EXIT(code);
3,540✔
2495

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

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

2526
_exit:
3,540✔
2527

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

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

2537
  return code;
3,540✔
2538
}
2539

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

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

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

2585
  return TSDB_CODE_SUCCESS;
×
2586
}
2587

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

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

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

2596
_exit:
3,198✔
2597

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

2604
  return code;
3,198✔
2605
}
2606

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

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

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

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

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

2627
_exit:
×
2628

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

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

2635
  return;
16,325✔
2636
}
2637

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

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

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

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

2657
_exit:
6,477✔
2658

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

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

2665
  return code;
6,477✔
2666
}
2667

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

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

2689
_exit:
50,748✔
2690

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

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

2707
    pStatus = (SStmStatus*)pIter;
×
2708

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

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

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

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

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

2726
  msmStopAllStreamsByGrant(errCode);
×
2727

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

2733
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,462,044✔
2734
  int32_t code = TSDB_CODE_SUCCESS;
1,462,044✔
2735
  int32_t lino = 0;
1,462,044✔
2736
  int64_t streamId = pDeploy->task.streamId;
1,462,044✔
2737
  
2738
  switch (pDeploy->task.type) {
1,462,044✔
2739
    case STREAM_READER_TASK:
608,483✔
2740
      if (NULL == pStream->readerTasks) {
608,483✔
2741
        pStream->streamId = streamId;
273,746✔
2742
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
273,746✔
2743
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
273,746✔
2744
      }
2745
      
2746
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,216,966✔
2747
      break;
608,483✔
2748
    case STREAM_TRIGGER_TASK:
211,312✔
2749
      pStream->streamId = streamId;
211,312✔
2750
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
211,312✔
2751
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
211,312✔
2752
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
211,312✔
2753
      break;
211,312✔
2754
    case STREAM_RUNNER_TASK:
642,249✔
2755
      if (NULL == pStream->runnerTasks) {
642,249✔
2756
        pStream->streamId = streamId;
214,433✔
2757
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
214,433✔
2758
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
214,433✔
2759
      }      
2760
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,284,498✔
2761
      break;
642,249✔
2762
    default:
×
2763
      break;
×
2764
  }
2765

2766
_exit:
1,462,044✔
2767

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

2772
  return code;
1,462,044✔
2773
}
2774

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

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

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

2800
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,162,716✔
2801
    
2802
    break;
1,162,716✔
2803
  }
2804
  
2805
_exit:
1,462,044✔
2806

2807
  taosHashRelease(pHash, pStream);
1,462,044✔
2808

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

2815
  return code;
1,462,044✔
2816
}
2817

2818

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

2824
  for (int32_t i = 0; i < taskNum; ++i) {
1,740,726✔
2825
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,464,168✔
2826
    if (pExt->deployed) {
1,464,168✔
2827
      continue;
2,124✔
2828
    }
2829

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

2833
    (void)atomic_add_fetch_32(deployed, 1);
1,462,044✔
2834
  }
2835

2836
_exit:
276,558✔
2837

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

2842
  return code;
276,558✔
2843
}
2844

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

2852
  mstDebug("start to add stream vgroup tasks deploy");
76,703✔
2853
  
2854
  for (int32_t i = 0; i < vgNum; ++i) {
509,467✔
2855
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
432,764✔
2856

2857
    msmUpdateVgroupUpTs(pCtx, *vgId);
432,764✔
2858

2859
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
432,764✔
2860
    if (NULL == pVg) {
432,764✔
2861
      continue;
266,704✔
2862
    }
2863

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

2877
_exit:
76,703✔
2878

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

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

2887
  return code;
76,703✔
2888
}
2889

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

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

2903
  (void)mstWaitLock(&pSnode->lock, false);
57,865✔
2904
  
2905
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
57,865✔
2906
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
53,348✔
2907
  }
2908

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

2915
_exit:
57,865✔
2916

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

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

2925
  return code;
57,865✔
2926
}
2927

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

2939
_exit:
679,028✔
2940

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

2945
  return code;
679,028✔
2946
}
2947

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

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

2959
  while (1) {
299,328✔
2960
    pIter = taosHashIterate(pCtx->deployStm, pIter);
377,852✔
2961
    if (pIter == NULL) {
377,852✔
2962
      break;
78,524✔
2963
    }
2964
    
2965
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
299,328✔
2966
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
598,656✔
2967

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

2972
    mstClearSStmStreamDeploy(pDeploy);
299,328✔
2973
    
2974
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
299,328✔
2975
  }
2976
  
2977
_exit:
78,524✔
2978

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

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

2990
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
76,703✔
2991
  int32_t code = TSDB_CODE_SUCCESS;
76,703✔
2992
  int32_t lino = 0;
76,703✔
2993
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
76,703✔
2994
  SStmVgTasksToDeploy* pVg = NULL;
76,703✔
2995
  
2996
  for (int32_t i = 0; i < vgNum; ++i) {
509,467✔
2997
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
432,764✔
2998
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
432,764✔
2999
    if (NULL == pVg) {
432,764✔
3000
      continue;
266,704✔
3001
    }
3002

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

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

3025
    for (int32_t m = taskNum - 1; m >= 0; --m) {
15,341✔
3026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
13,557✔
3027
      if (!pExt->deployed) {
13,557✔
3028
        continue;
1,430✔
3029
      }
3030

3031
      mstDestroySStmTaskToDeployExt(pExt);
12,127✔
3032

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

3041
_exit:
76,703✔
3042

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

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

3052
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
128,482✔
3053
  if (!GOT_SNODE(snodeId)) {
128,482✔
3054
    return;
27,336✔
3055
  }
3056
  
3057
  int32_t code = TSDB_CODE_SUCCESS;
101,146✔
3058
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
101,146✔
3059
  if (NULL == pSnode) {
101,146✔
3060
    return;
43,281✔
3061
  }
3062

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

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

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

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

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

3096
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
3,214✔
3097
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
11,787✔
3098
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
10,003✔
3099
      if (!pExt->deployed) {
10,003✔
3100
        continue;
9,295✔
3101
      }
3102

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

3111
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
3,214✔
3112
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
47,516✔
3113
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
45,732✔
3114
      if (!pExt->deployed) {
45,732✔
3115
        continue;
715✔
3116
      }
3117

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

3130
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,573,584✔
3131
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,573,584✔
3132
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
76,703✔
3133
  }
3134

3135
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,573,584✔
3136
    msmCleanDeployedSnodeTasks(pHb->snodeId);
128,482✔
3137
  }
3138
}
16,573,584✔
3139

3140
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
16,573,584✔
3141
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,573,584✔
3142
  if (mStreamMgmt.tCtx) {
16,573,584✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
16,399,221✔
3144
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
16,399,221✔
3145
  }
3146
}
16,573,584✔
3147

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

3165
_exit:
275,677✔
3166

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

3171
  return code;
275,677✔
3172
}
3173

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

3190
_exit:
×
3191

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

3196
  return code;
×
3197
}
3198

3199

3200

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

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

3218
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
134,146✔
3219
    if (pAction->undeploy.doCheckpoint) {
67,073✔
3220
      pAction->undeploy.doCheckpoint = dropped ? false : true;
48,221✔
3221
    }
3222
    if (!pAction->undeploy.doCleanup) {
67,073✔
3223
      pAction->undeploy.doCleanup = dropped ? true : false;
48,221✔
3224
    }
3225
    
3226
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
67,073✔
3227
  } else {
3228
    SStmAction newAction = {0};
36,950✔
3229
    newAction.actions = action;
36,950✔
3230
    newAction.undeploy.doCheckpoint = dropped ? false : true;
36,950✔
3231
    newAction.undeploy.doCleanup = dropped ? true : false;
36,950✔
3232
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
36,950✔
3233
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
36,950✔
3234
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
73,900✔
3235
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
36,950✔
3236
    
3237
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
36,950✔
3238
  }
3239

3240
_exit:
104,023✔
3241

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

3246
  return code;
104,023✔
3247
}
3248

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

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

3270
_exit:
6,052✔
3271

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

3277
  return code;
6,052✔
3278
}
3279

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

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

3290
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
424,349✔
3291
  for (int32_t i = 0; i < readerNum; ++i) {
962,976✔
3292
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
538,627✔
3293
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
538,627✔
3294
      return false;
×
3295
    }
3296
  }
3297

3298
  readerNum = taosArrayGetSize(pStream->trigOReaders);
424,349✔
3299
  for (int32_t i = 0; i < readerNum; ++i) {
486,477✔
3300
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
62,128✔
3301
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
62,128✔
UNCOV
3302
      return false;
×
3303
    }
3304
  }
3305

3306
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
424,349✔
3307
  SListNode* pNode = listHead(pStream->calcReaders);
424,349✔
3308
  for (int32_t i = 0; i < readerNum; ++i) {
951,738✔
3309
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
527,389✔
3310
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
527,389✔
3311
      return false;
×
3312
    }
3313
    pNode = TD_DLIST_NODE_NEXT(pNode);
527,389✔
3314
  }
3315

3316
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,233,023✔
3317
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
957,346✔
3318
    for (int32_t m = 0; m < runnerNum; ++m) {
1,799,044✔
3319
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
990,370✔
3320
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
990,370✔
3321
        return false;
148,672✔
3322
      }
3323
    }
3324
  }
3325
  
3326
  return true;
275,677✔
3327
}
3328

3329

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

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

3347
  stopped = atomic_load_8(&pStatus->stopped);
30,322,656✔
3348
  if (stopped) {
30,322,656✔
3349
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3350
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3351
    return;
×
3352
  }
3353
  
3354
  switch (pMsg->status) {
30,322,656✔
3355
    case STREAM_STATUS_INIT:      
30,315,231✔
3356
      if (STREAM_TRIGGER_TASK != pMsg->type) {
30,315,231✔
3357
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
29,677,862✔
3358
        return;
29,677,862✔
3359
      }
3360
      
3361
      if (INT64_MIN == pStatus->lastActionTs) {
637,369✔
3362
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3363
        return;
×
3364
      }
3365
      
3366
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
637,369✔
3367
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
213,020✔
3368
        return;
213,020✔
3369
      }
3370

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

3393
_exit:
431,774✔
3394

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

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

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

3409
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
104,023✔
3410

3411
_exit:
104,023✔
3412

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

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

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

3442
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
11,972,276✔
3443
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
5,991,828✔
3444
    if (NULL == pStatus->detailStatus) {
5,991,828✔
3445
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
210,604✔
3446
      if (NULL == pStatus->detailStatus) {
210,604✔
3447
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3448
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3449
      }
3450
    }
3451
    
3452
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
5,991,828✔
3453
    taosWUnLockLatch(&pStatus->detailStatusLock);
5,991,828✔
3454
  }
3455

3456
_exit:
5,980,448✔
3457

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

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

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

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

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

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

3498
    if ((*ppStatus)->status != pTask->status) {
101,430,882✔
3499
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,508,634✔
3500
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,051,586✔
3501
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,457,048✔
3502
        if (pStream->triggerTask) {
×
3503
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3504
        }
3505
        
3506
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3507
      }
3508
    }
3509
    
3510
    (*ppStatus)->errCode = pTask->errorCode;
101,430,882✔
3511
    (*ppStatus)->status = pTask->status;
101,430,882✔
3512
    (*ppStatus)->lastUpTs = pCtx->currTs;
101,430,882✔
3513
    
3514
    if (STREAM_STATUS_RUNNING != pTask->status) {
101,430,882✔
3515
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
30,322,656✔
3516
    }
3517
    
3518
    if (STREAM_TRIGGER_TASK == pTask->type) {
101,430,882✔
3519
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
11,972,276✔
3520
    }
3521
  }
3522

3523
_exit:
1,936,074✔
3524

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

3529
  return code;
1,936,074✔
3530
}
3531

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

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

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

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

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

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

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

3629
_exit:
×
3630

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

3637
  return code;
×
3638
}
3639

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

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

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

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

3668
_exit:
×
3669

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

3674
  return code;
×
3675
}
3676

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

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

3694
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
551,354✔
3695
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
275,677✔
3696

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

3699
  return;
275,677✔
3700

3701
_exit:
×
3702

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

3706

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

3716
  SStreamTaskUndeploy undeploy;
36,950✔
3717
  for (int32_t i = 0; i < dropNum; ++i) {
140,973✔
3718
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
104,023✔
3719
    undeploy.task = *pTask;
104,023✔
3720
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
104,023✔
3721
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
104,023✔
3722

3723
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
208,046✔
3724
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
104,023✔
3725

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

3729
  return;
36,950✔
3730

3731
_exit:
×
3732

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

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

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

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

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

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

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

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

3766
_exit:
×
3767

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

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

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

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

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

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

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

3804
_exit:
6,052✔
3805

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

3813

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

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

3822
  while (1) {
318,679✔
3823
    pIter = taosHashIterate(pCtx->actionStm, pIter);
471,007✔
3824
    if (pIter == NULL) {
471,007✔
3825
      break;
152,328✔
3826
    }
3827

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

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

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

3844
    if (STREAM_ACT_START & pAction->actions) {
281,729✔
3845
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
275,677✔
3846
    }
3847
  }
3848
  
3849
_exit:
152,328✔
3850

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

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

3859
  return code;
152,328✔
3860
}
3861

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

3868
  while (true) {
3869
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
17,130,310✔
3870
    if (NULL == lastTs) {
17,130,310✔
3871
      if (noExists) {
1,102,889✔
3872
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
371,800✔
3873
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
371,800✔
3874
      }
3875

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

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

3890
        continue;
×
3891
      }
3892

3893
      return code;
483✔
3894
    }
3895

3896
    break;
3897
  }
3898

3899
_exit:
371,800✔
3900

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

3905
  return code;  
371,800✔
3906
}
3907

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

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

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

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

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

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

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

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

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

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

3982
  msmWatchCheckStreamMap(pCtx);
×
3983

3984
_exit:
253✔
3985

3986
  mStreamMgmt.lastTaskId += 100000;
253✔
3987

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

3990
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
253✔
3991

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

3996
  return code;
253✔
3997
}
3998

3999

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

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

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

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

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

4025
_exit:
1,012✔
4026

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

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

4035
  return code;
1,012✔
4036
}
4037

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

4044
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
79,458✔
4045
  for (int32_t i = 0; i < readerNum; ++i) {
157,380✔
4046
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
95,252✔
4047
    if (pReader->id.nodeId == vgId) {
95,252✔
4048
      readerExists = true;
17,330✔
4049
      break;
17,330✔
4050
    }
4051
  }
4052

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

4065
_exit:
79,458✔
4066

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

4071
  return code;
79,458✔
4072
}
4073

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

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

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

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

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

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

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

4127
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
70,793✔
4128
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
70,793✔
4129
  
4130
  for (int32_t i = 0; i < tbNum; ++i) {
266,585✔
4131
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
195,792✔
4132
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
195,792✔
4133
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
391,584✔
4134
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
195,792✔
4135
  }
4136

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

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

4161
_exit:
70,793✔
4162

4163
  tFreeSStreamMgmtReq(pMgmtReq);
70,793✔
4164
  taosMemoryFree(pMgmtReq);
70,793✔
4165

4166
  tSimpleHashCleanup(pVgs);
70,793✔
4167
  mstDestroyDbVgroupsHash(pDbVgroups);
70,793✔
4168

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

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

4179
_final:
70,793✔
4180

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

4187
  return finalCode;
70,793✔
4188
}
4189

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

4204
_exit:
4,279✔
4205

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

4210
  return code;
4,279✔
4211
}
4212

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

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

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

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

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

4256
_exit:
4,279✔
4257

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

4262
  return code;
4,279✔
4263
}
4264

4265

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

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

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

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

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

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

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

4326
_exit:
4,279✔
4327

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

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

4340

4341
_final:
4,279✔
4342

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

4349
  return finalCode;
4,279✔
4350
}
4351

4352

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

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

4370
_exit:
75,072✔
4371

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

4376
  return code;
75,072✔
4377
}
4378

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

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

4404
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
75,072✔
4405
  }
4406

4407
_exit:
27,540✔
4408

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

4413
  return code;
27,540✔
4414
}
4415

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

4421
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,398,209✔
4422
  if (GOT_SNODE(pReq->snodeId)) {
16,026,409✔
4423
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,065,369✔
4424
  }
4425
  
4426
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,025,647✔
4427
    msmHandleStreamActions(pCtx);
50,748✔
4428
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
50,748✔
4429
  }
4430

4431
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
16,025,647✔
4432
    code = msmHandleStreamRequests(pCtx);
27,540✔
4433
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
27,540✔
4434
    TAOS_CHECK_EXIT(code);
27,540✔
4435
  }
4436

4437
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,025,647✔
4438
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
76,703✔
4439
  } else {
4440
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
15,948,944✔
4441
  }
4442

4443
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,025,647✔
4444
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
101,146✔
4445
  }
4446

4447
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,025,647✔
4448
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
78,524✔
4449
  }
4450

4451
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,025,647✔
4452
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,936,074✔
4453
  }
4454

4455
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,025,647✔
4456
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
152,328✔
4457
  }
4458

4459
_exit:
16,025,647✔
4460

4461
  if (code) {
16,398,209✔
4462
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
372,562✔
4463
  }
4464

4465
  return code;
16,398,209✔
4466
}
4467

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

4473
  if (TSDB_CODE_SUCCESS != code) {
16,573,584✔
4474
    goto _exit;
372,562✔
4475
  }
4476

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

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

4489
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
16,201,022✔
4490
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
16,201,022✔
4491

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

4503
_exit:
16,573,584✔
4504

4505
  pMsg->code = code;
16,573,584✔
4506
  pMsg->info = *pRpcInfo;
16,573,584✔
4507
  if (TSDB_CODE_SUCCESS == code) {
16,573,584✔
4508
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
16,201,022✔
4509
    pMsg->pCont = buf;
16,201,022✔
4510
  }
4511
}
16,573,584✔
4512

4513

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

4519
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,573,584✔
4520

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

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

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

4550
_exit:
16,573,584✔
4551

4552
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
16,573,584✔
4553

4554
  msmCleanStreamGrpCtx(pHb);
16,573,584✔
4555
  msmClearStreamToDeployMaps(pHb);
16,573,584✔
4556

4557
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
16,573,584✔
4558
  
4559
  tFreeSMStreamHbRspMsg(&rsp);
16,573,584✔
4560

4561
  return code;
16,573,584✔
4562
}
4563

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

4569
  mstInfo("start to process mnode become leader");
419,856✔
4570

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

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

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

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

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

4593
  streamRemoveVnodeLeader(MNODE_HANDLE);
610,315✔
4594

4595
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
610,315✔
4596
    taosWLockLatch(&mStreamMgmt.runtimeLock);
419,856✔
4597
    msmDestroyRuntimeInfo(pMnode);
419,856✔
4598
    mStreamMgmt.stat.inactiveTimes++;
419,856✔
4599
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
419,856✔
4600
  }
4601
}
4602

4603

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

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

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

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

4630
  return true;
13,628✔
4631

4632
_exit:
×
4633

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

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

4640
  return false;
×
4641
}
4642

4643

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

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

4657
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4658

4659
_exit:
70,024✔
4660

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

4665
  return code;
70,024✔
4666
}
4667

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

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

4692
  if (pStatus) {
120,191✔
4693
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
117,328✔
4694
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4695
    }
4696

4697
    return true;
117,328✔
4698
  }
4699

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

4702
  return true;
2,863✔
4703
}
4704

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

4717
    pStatus = (SStmStatus*)pIter;
490,112✔
4718

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

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

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

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

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

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

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

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

4771
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
10,410,695✔
4772
  for (int32_t i = 0; i < taskNum; ++i) {
21,311,493✔
4773
    SStmTaskStatus* pTask = *(pList + i);
10,906,518✔
4774

4775
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
10,906,518✔
4776
      continue;
143,682✔
4777
    }
4778
    
4779
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
10,777,718✔
4780
      continue;
10,769,166✔
4781
    }
4782

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

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

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

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

4808
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
264,226✔
4809
  void* pIter = NULL;
264,226✔
4810
  SStmVgStreamStatus* pVg = NULL;
264,226✔
4811
  int64_t streamId = 0;
264,226✔
4812
  
4813
  while (true) {
4,528,044✔
4814
    pIter = taosHashIterate(pStreams, pIter);
4,792,270✔
4815
    if (NULL == pIter) {
4,792,270✔
4816
      break;
264,226✔
4817
    }
4818

4819
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
4,528,044✔
4820
    pVg = (SStmVgStreamStatus*)pIter;
4,528,044✔
4821

4822
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
4,528,044✔
4823
    if (taskNum > 0) {
4,528,044✔
4824
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
2,608,174✔
4825
    }
4826

4827
    taskNum = taosArrayGetSize(pVg->calcReaders);
4,528,044✔
4828
    if (taskNum > 0) {
4,528,044✔
4829
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
2,943,180✔
4830
    }
4831
  }
4832
}
264,226✔
4833

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

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

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

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

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

4859

4860
void msmCheckVgroupStatus(SMnode *pMnode) {
575,209✔
4861
  void* pIter = NULL;
575,209✔
4862
  int32_t code = 0;
575,209✔
4863
  
4864
  while (true) {
2,526,032✔
4865
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
3,101,241✔
4866
    if (NULL == pIter) {
3,101,241✔
4867
      break;
575,209✔
4868
    }
4869

4870
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
2,526,032✔
4871
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
2,526,032✔
4872
      continue;
2,259,504✔
4873
    }
4874
    
4875
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
266,528✔
4876

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

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

4897
    msmCheckVgroupStreamStatus(pVg->streamTasks);
264,226✔
4898
  }
4899
}
575,209✔
4900

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

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

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

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

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

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

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

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

4976

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

4988
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,283,830✔
4989
    pSnode = (SStmSnodeStreamStatus*)pIter;
1,283,830✔
4990

4991
    if (NULL != pSnode->trigger) {
1,283,830✔
4992
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
1,261,105✔
4993
    }
4994

4995
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
5,135,320✔
4996
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
3,851,490✔
4997
      if (taskNum > 0) {
3,851,490✔
4998
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
3,598,236✔
4999
      }
5000
    }
5001
  }
5002
}
73,220✔
5003

5004

5005
void msmCheckSnodeStatus(SMnode *pMnode) {
575,209✔
5006
  void* pIter = NULL;
575,209✔
5007
  
5008
  while (true) {
744,328✔
5009
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
1,319,537✔
5010
    if (NULL == pIter) {
1,319,537✔
5011
      break;
575,209✔
5012
    }
5013

5014
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
744,328✔
5015
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
744,328✔
5016
      continue;
658,966✔
5017
    }
5018

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

5037
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
73,220✔
5038
  }
5039
}
575,209✔
5040

5041

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

5045
  msmCheckVgroupStatus(pMnode);
575,209✔
5046
  msmCheckSnodeStatus(pMnode);
575,209✔
5047
}
575,209✔
5048

5049
void msmCheckSnodesState(SMnode *pMnode) {
575,209✔
5050
  if (!MST_READY_FOR_SNODE_LOOP()) {
575,209✔
5051
    return;
525,190✔
5052
  }
5053

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

5056
  void* pIter = NULL;
50,019✔
5057
  int32_t snodeId = 0;
50,019✔
5058
  while (true) {
57,255✔
5059
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
107,274✔
5060
    if (NULL == pIter) {
107,274✔
5061
      break;
50,019✔
5062
    }
5063

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

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

5082
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
50,019✔
5083
}
5084

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

5092
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
8,169,840✔
5093
    mstTrace("ignore health check since no stream now");
7,019,422✔
5094
    return false;
7,019,422✔
5095
  }
5096

5097
  return true;
1,150,418✔
5098
}
5099

5100
void msmHealthCheck(SMnode *pMnode) {
7,595,390✔
5101
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,595,390✔
5102
    return;
7,020,181✔
5103
  }
5104

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

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

5122
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
575,209✔
5123

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

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

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

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

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

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

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

5175
  return code;
×
5176
}
5177

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

5185
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
838,629✔
5186

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

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

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

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

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

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

5292
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
419,856✔
5293
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
419,856✔
5294

5295
  mStreamMgmt.lastTaskId = 1;
419,856✔
5296

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

5300
  if (activeStreamNum > 0) {
419,856✔
5301
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
253✔
5302
  } else {
5303
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
419,603✔
5304
  }
5305

5306
_exit:
419,856✔
5307

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

5315
  return code;
419,856✔
5316
}
5317

5318

5319

STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc