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

taosdata / TDengine / #4905

29 Dec 2025 02:08PM UTC coverage: 65.423% (-0.3%) from 65.734%
#4905

push

travis-ci

web-flow
enh: sign connect request (#34067)

23 of 29 new or added lines in 4 files covered. (79.31%)

11614 existing lines in 186 files now uncovered.

193476 of 295730 relevant lines covered (65.42%)

115752566.53 hits per line

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

76.26
/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() {
711,388✔
34
  SStmQNode* pQNode = NULL;
711,388✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
711,388✔
37
    return;
355,694✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,778,297✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,669,782✔
49
    taosHashCleanup(pCtx->deployStm[m]);
8,891,485✔
50
    taosHashCleanup(pCtx->actionStm[m]);
8,891,485✔
51
  }
52
}
1,778,297✔
53

54
void msmDestroyThreadCtxs() {
711,388✔
55
  if (NULL == mStreamMgmt.tCtx) {
711,388✔
56
    return;
355,694✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,133,991✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,778,297✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
355,694✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
711,388✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
711,388✔
94
}
711,388✔
95

96

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

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

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

111
    pStatus = pStream;
156✔
112
  }
113

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

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

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

136
_exit:
382✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,934✔
139

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

145

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

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

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

200
  pSnode = NULL;
374,308✔
201

202
_exit:
374,308✔
203

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

208
  return code;
374,308✔
209
}
210

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

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

235
  pDnode = NULL;
857,177✔
236

237
_exit:
857,177✔
238

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

243
  return code;
857,177✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
425,414✔
275
  int32_t code = TSDB_CODE_SUCCESS;
425,414✔
276
  int32_t lino = 0;
425,414✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
425,414✔
278
  if (NULL == pStream) {
425,414✔
279
    SStmVgStreamStatus stream = {0};
320,793✔
280
    if (trigReader) {
320,793✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
191,465✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
191,465✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
382,930✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
129,328✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
129,328✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
258,656✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
320,793✔
290
    goto _exit;
320,793✔
291
  }
292
  
293
  if (trigReader) {
104,621✔
294
    if (NULL == pStream->trigReaders) {
21,115✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
21,115✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
21,115✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
42,230✔
300
    goto _exit;
21,115✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
83,506✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
56,759✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
56,759✔
306
  }
307

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

310
_exit:
83,506✔
311

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

320
  return code;
425,414✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
51,086✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
51,086✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
51,086✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
374,328✔
339
  }
340
  
341
_exit:
425,300✔
342

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

350
  return code;
425,414✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
426,350✔
363
    if (NULL == pVg) {
426,350✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
75,229✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
75,229✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
150,458✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
75,229✔
368
      if (TSDB_CODE_SUCCESS == code) {
75,229✔
369
        goto _return;
75,229✔
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);
351,121✔
381
    if (NULL == pVg->taskList) {
351,121✔
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)) {
702,242✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
351,121✔
390
    
391
    taosHashRelease(pVgMap, pVg);
351,121✔
392
    break;
351,121✔
393
  }
394
  
395
_return:
426,350✔
396

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

404
  return code;
426,350✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
521,323✔
409
  int32_t code = TSDB_CODE_SUCCESS;
521,323✔
410
  int32_t lino = 0;
521,323✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
521,323✔
412
  if (NULL == pStream) {
521,323✔
413
    SStmSnodeStreamStatus stream = {0};
132,919✔
414
    if (deployId < 0) {
132,919✔
415
      stream.trigger = pStatus;
3,012✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
129,907✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
129,907✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
259,814✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
132,919✔
423
    goto _exit;
132,919✔
424
  }
425
  
426
  if (deployId < 0) {
388,404✔
427
    if (NULL != pStream->trigger) {
125,417✔
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;
125,417✔
433
    goto _exit;
125,417✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
262,987✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
246,559✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
246,559✔
439
  }
440

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

443
_exit:
262,987✔
444

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

453
  return code;
521,323✔
454
}
455

456

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

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

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

484
  return code;
521,323✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
128,429✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
26,083✔
568
      ext.deployed = false;
26,083✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
52,166✔
570

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

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

614
  return code;
392,894✔
615
}
616

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

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

632
_exit:
376,466✔
633

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

638
  return code;
376,466✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
10,543✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
189✔
682

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

687
  return code;  
189✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
49,772✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
12,992,702✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
49,726,520✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
36,733,818✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
36,733,818✔
725
  }
726

727
_exit:
12,992,702✔
728

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

733
  return code;
12,992,702✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
269,612✔
739
  int32_t num = taosArrayGetSize(pList);
269,612✔
740
  for (int32_t i = 0; i < num; ++i) {
795,550✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
671,494✔
742
    if (pScan->readFromCache) {
671,494✔
743
      return pScan->scanPlan;
145,556✔
744
    }
745
  }
746

747
  return NULL;
124,056✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
426,350✔
775
}
776

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

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

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

805
_exit:
125,417✔
806

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

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

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

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

875
  SStreamTaskAddr addr;
128,030✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
128,429✔
877
  if (triggerReaderNum > 0) {
128,429✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
127,524✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
127,524✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
285,795✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
157,366✔
884
    addr.taskId = pStatus->id.taskId;
157,366✔
885
    addr.nodeId = pStatus->id.nodeId;
157,366✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
157,366✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
314,732✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
157,366✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
128,429✔
892
  pMsg->streamName = pInfo->streamName;
128,429✔
893

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

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

901
_exit:
125,417✔
902

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

912

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

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

944
_exit:
392,894✔
945

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

953

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

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

977

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

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

995
  return code;
504,895✔
996
}
997

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

1002
int64_t msmAssignTaskSeriousId(void) {
946,522✔
1003
  return taosGetTimestampNs();
946,522✔
1004
}
1005

1006

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

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

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

1027
    *alive = (pStatus->runnerThreadNum >= 0);
648,899✔
1028
    break;
648,899✔
1029
  }
1030

1031
_exit:
648,899✔
1032

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

1037
  return code;
648,899✔
1038
}
1039

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

1051
    if (alive) {
254,061✔
1052
      return snodeId;
254,061✔
1053
    }
1054
    
1055
    if (snodeId == mainSnodeId) {
×
1056
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
1057
      if (NULL == pSnode) {
×
1058
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1059
        return 0;
×
1060
      }
1061
      
1062
      if (pSnode->replicaId <= 0) {
×
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;
×
1069
      mndReleaseSnode(pMnode, pSnode);
×
1070
      
1071
      continue;
×
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) {
382,012✔
1088
  int32_t code = TSDB_CODE_SUCCESS;
382,012✔
1089
  int32_t lino = 0;
382,012✔
1090
  int32_t snodeIdx = 0;
382,012✔
1091
  int32_t snodeId = 0;
382,012✔
1092
  void      *pIter = NULL;
382,012✔
1093
  SSnodeObj *pObj = NULL;
382,012✔
1094
  bool alive = false;
382,012✔
1095
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
382,012✔
1096
  if (snodeNum <= 0) {
382,012✔
1097
    mstsInfo("no available snode now, num:%d", snodeNum);
2,250✔
1098
    goto _exit;
2,250✔
1099
  }
1100

1101
  int32_t snodeTarget = taosRand() % snodeNum;
379,762✔
1102

1103
  while (1) {
1104
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
394,838✔
1105
    if (pIter == NULL) {
394,838✔
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);
394,838✔
1116
    if (code) {
394,838✔
1117
      sdbRelease(pMnode->pSdb, pObj);
×
1118
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1119
      pObj = NULL;
×
1120
      TAOS_CHECK_EXIT(code);
×
1121
    }
1122
    
1123
    if (!alive) {
394,838✔
1124
      sdbRelease(pMnode->pSdb, pObj);
×
1125
      continue;
×
1126
    }
1127

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

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

1140
_exit:
382,012✔
1141

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

1146
  if (0 == snodeId) {
382,012✔
1147
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,250✔
1148
  }
1149

1150
  return snodeId;
382,012✔
1151
}
1152

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

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

1164
_exit:
504,895✔
1165

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

1170
  return snodeId;
504,895✔
1171
}
1172

1173

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

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

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

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

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

1206
_exit:
128,429✔
1207

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

1212
  return code;
128,429✔
1213
}
1214

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

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

1241
_exit:
212,580✔
1242

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

1247
  return code;
212,580✔
1248
}
1249

1250
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
128,429✔
1251
  int32_t code = TSDB_CODE_SUCCESS;
128,429✔
1252
  int32_t lino = 0;
128,429✔
1253
  int64_t streamId = pStream->pCreate->streamId;
128,429✔
1254
  SSdb   *pSdb = pCtx->pMnode->pSdb;
128,429✔
1255
  SStmTaskStatus* pState = NULL;
128,429✔
1256
  SVgObj *pVgroup = NULL;
128,429✔
1257
  SDbObj* pDb = NULL;
128,429✔
1258
  
1259
  switch (pStream->pCreate->triggerTblType) {
128,429✔
1260
    case TSDB_NORMAL_TABLE:
68,359✔
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);
68,359✔
1265
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
68,359✔
1266
      pState = taosArrayGet(pInfo->trigReaders, 0);
68,359✔
1267
      
1268
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
68,359✔
1269
      break;
68,359✔
1270
    }
1271
    case TSDB_SUPER_TABLE: {
59,165✔
1272
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
59,165✔
1273
      if (NULL == pDb) {
59,165✔
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));
59,165✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
59,165✔
1281
      
1282
      void *pIter = NULL;
59,165✔
1283
      while (1) {
320,704✔
1284
        SStmTaskDeploy info = {0};
379,869✔
1285
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
379,869✔
1286
        if (pIter == NULL) {
379,869✔
1287
          break;
59,165✔
1288
        }
1289
      
1290
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
320,704✔
1291
          pState = taosArrayReserve(pInfo->trigReaders, 1);
89,007✔
1292

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

1302
        sdbRelease(pSdb, pVgroup);
320,704✔
1303
      }
1304
      break;
59,165✔
1305
    }
1306
    default:
905✔
1307
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
905✔
1308
      break;
905✔
1309
  }
1310

1311
_exit:
128,429✔
1312

1313
  mndReleaseDb(pCtx->pMnode, pDb);
128,429✔
1314

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

1319
  return code;
128,429✔
1320
}
1321

1322
int32_t msmUPAddScanTask(SStmGrpCtx* pCtx, SStreamObj* pStream, char* scanPlan, int32_t vgId, int64_t taskId) {
210,904✔
1323
  int32_t code = TSDB_CODE_SUCCESS;
210,904✔
1324
  int32_t lino = 0;
210,904✔
1325
  SSubplan* pSubplan = NULL;
210,904✔
1326
  int64_t streamId = pStream->pCreate->streamId;
210,904✔
1327
  int64_t key[2] = {streamId, 0};
210,904✔
1328
  SStmTaskSrcAddr addr;
210,505✔
1329
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
210,904✔
1330
  addr.isFromCache = false;
210,904✔
1331
  
1332
  if (MNODE_HANDLE == vgId) {
210,904✔
1333
    mndGetMnodeEpSet(pCtx->pMnode, &addr.epset);
×
1334
  } else if (vgId > MNODE_HANDLE) {
210,904✔
1335
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
210,904✔
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;
210,904✔
1342
  addr.vgId = vgId;
210,904✔
1343
  addr.groupId = pSubplan->id.groupId;
210,904✔
1344

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

1347
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
210,904✔
1348
  if (NULL == ppRes) {
210,904✔
1349
    SArray* pRes = taosArrayInit(1, sizeof(addr));
210,904✔
1350
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
210,904✔
1351
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
421,808✔
1352
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
210,904✔
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);
210,904✔
1358
  
1359
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
210,904✔
1360
  
1361
_exit:
210,904✔
1362

1363
  nodesDestroyNode((SNode*)pSubplan);
210,904✔
1364

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

1369
  return code;
210,904✔
1370
}
1371

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

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

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

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

1408
  return code;
62,014✔
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) {
212,834✔
1413
  int32_t code = TSDB_CODE_SUCCESS;
212,834✔
1414
  int32_t lino = 0;
212,834✔
1415

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

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

1440
_exit:
212,834✔
1441

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

1446
  return code;
212,834✔
1447
}
1448

1449

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

1459
  
1460
  for (int32_t i = 0; i < calcTasksNum; ++i) {
401,132✔
1461
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
272,703✔
1462
    if (pScan->readFromCache) {
272,703✔
1463
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
62,014✔
1464
      continue;
62,014✔
1465
    }
1466
    
1467
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
210,689✔
1468
    for (int32_t m = 0; m < vgNum; ++m) {
421,378✔
1469
      pState = tdListReserve(pInfo->calcReaders);
210,689✔
1470
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
210,689✔
1471

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

1477
_exit:
128,429✔
1478

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

1483
  return code;
128,429✔
1484
}
1485

1486

1487

1488
static int32_t msmUPPrepareReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
215✔
1489
  int32_t code = TSDB_CODE_SUCCESS;
215✔
1490
  int32_t lino = 0;
215✔
1491
  int64_t streamId = pStream->pCreate->streamId;
215✔
1492
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
215✔
1493
  if (calcTasksNum <= 0) {
215✔
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);
215✔
1499
  
1500
  for (int32_t i = 0; i < calcTasksNum; ++i) {
430✔
1501
    SStreamCalcScan* pScan = taosArrayGet(pStream->pCreate->calcScanPlanList, i);
215✔
1502
    if (pScan->readFromCache) {
215✔
1503
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
×
1504
      continue;
×
1505
    }
1506
    
1507
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
215✔
1508
    for (int32_t m = 0; m < vgNum; ++m) {
430✔
1509
      SStmTaskStatus* pReader = (SStmTaskStatus*)pNode->data;
215✔
1510
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pReader->id.nodeId, pReader->id.taskId));
215✔
1511
      pNode = TD_DLIST_NODE_NEXT(pNode);
215✔
1512
    }
1513
  }
1514

1515
_exit:
215✔
1516

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

1521
  return code;
215✔
1522
}
1523

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

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

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

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

1558
  source.addr.epSet = pSrc->epset;
834,752✔
1559
  source.addr.nodeId = pSrc->vgId;
834,752✔
1560

1561
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
834,752✔
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);
834,752✔
1565
}
1566

1567
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
16,428✔
1568
  int64_t streamId = pStream->pCreate->streamId;
16,428✔
1569
  int32_t runnerNum = taosArrayGetSize(pRunners);
16,428✔
1570
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
41,070✔
1571
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
41,070✔
1572
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
41,070✔
1573
    if (pPlan->id.subplanId == subplanId) {
41,070✔
1574
      *taskId = pDeploy->task.taskId;
16,428✔
1575
      *ppParent = &pDeploy->task;
16,428✔
1576
      return TSDB_CODE_SUCCESS;
16,428✔
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) {
392,894✔
1586
  int32_t code = TSDB_CODE_SUCCESS;
392,894✔
1587
  int32_t lino = 0;
392,894✔
1588
  int64_t key[2] = {streamId, -1};
392,894✔
1589
  SNode* pNode = NULL;
392,894✔
1590
  SStreamTask* pTask = &pDeploy->task;
392,894✔
1591
  FOREACH(pNode, pPlan->pChildren) {
1,227,646✔
1592
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
834,752✔
1593
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
16,428✔
1594
      continue;
16,428✔
1595
    }
1596
    
1597
    SValueNode* pVal = (SValueNode*)pNode;
818,324✔
1598
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
818,324✔
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);
818,324✔
1604

1605
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
818,324✔
1606
    if (NULL == ppRes) {
818,324✔
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);
818,324✔
1612
    for (int32_t i = 0; i < childrenNum; ++i) {
1,636,648✔
1613
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
818,324✔
1614
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
818,324✔
1615
    }
1616
  }
1617

1618
_exit:
392,894✔
1619

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

1624
  return code;
392,894✔
1625
}
1626

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

1635
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
392,894✔
1636

1637
  SNode* pTmp = NULL;
392,894✔
1638
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,227,646✔
1639
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
834,752✔
1640
      ERASE_NODE(pPlan->pChildren);
818,324✔
1641
      continue;
818,324✔
1642
    }
1643
    WHERE_NEXT;
16,428✔
1644
  }
1645
  nodesClearList(pPlan->pChildren);
392,894✔
1646
  pPlan->pChildren = NULL;
392,894✔
1647

1648
  if (NULL == pPlan->pParents) {
392,894✔
1649
    goto _exit;
376,466✔
1650
  }
1651

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

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

1671
  return code;
392,894✔
1672
}
1673

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

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

1689
_exit:
376,466✔
1690

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

1695
  return code;
376,466✔
1696
}
1697

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

1711
  if (pDag->numOfSubplans <= 0) {
125,417✔
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) {
125,417✔
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);
125,417✔
1722
  if (levelNum <= 0) {
125,417✔
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;
125,417✔
1728
  
1729
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
125,417✔
1730
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
125,417✔
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);
125,417✔
1736
  if (taskNum != 1) {
125,417✔
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);
125,417✔
1742
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
125,417✔
1743
  
1744
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
501,668✔
1745
    totalTaskNum = 0;
376,251✔
1746

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

1753
    taskIdx = 0;
376,251✔
1754
    
1755
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
760,716✔
1756
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
384,465✔
1757
      if (NULL == plans) {
384,465✔
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)) {
384,465✔
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);
384,465✔
1768
      if (taskNum <= 0) {
384,465✔
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;
384,465✔
1774
      if (totalTaskNum > pDag->numOfSubplans) {
384,465✔
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) {
777,144✔
1780
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
392,679✔
1781
        pState = taosArrayReserve(deployList, 1);
392,679✔
1782

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

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

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

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

1813
    if (totalTaskNum != pDag->numOfSubplans) {
376,251✔
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));
376,251✔
1819

1820
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
376,251✔
1821

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

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

1830
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
501,668✔
1831
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
376,251✔
1832
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
376,251✔
1833
  }
1834
  
1835
  pInfo->runnerNum = totalTaskNum;
125,417✔
1836
  
1837
_exit:
125,417✔
1838

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

1843
  taosArrayDestroy(deployTaskList);
125,417✔
1844
  nodesDestroyNode((SNode *)pDag);
125,417✔
1845

1846
  return code;
125,417✔
1847
}
1848

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

1866
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
430✔
1867
    deployId = pAction->deployId[r];
215✔
1868

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

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

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

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

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

1891
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
215✔
1892
        if (pRunner->id.taskIdx != newTaskIdx) {
215✔
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;
215✔
1898

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

1909
        pRunner++;
215✔
1910
      }
1911

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

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

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

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

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

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

1927
_exit:
215✔
1928

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

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

1936
  return code;
215✔
1937
}
1938

1939

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

1948
_exit:
120,823✔
1949

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

1954
  return code;
120,823✔
1955
}
1956

1957

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

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

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

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

1980
  taosHashClear(mStreamMgmt.toUpdateScanMap);
125,417✔
1981
  mStreamMgmt.toUpdateScanNum = 0;
125,417✔
1982

1983
_exit:
125,417✔
1984

1985
  nodesDestroyNode((SNode *)pPlan);
125,417✔
1986

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

1991
  return code;
125,417✔
1992
}
1993

1994

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

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

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

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

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

2018
  return code;
128,429✔
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) {
123,835✔
2072
  int32_t code = TSDB_CODE_SUCCESS;
123,835✔
2073
  int32_t lino = 0;
123,835✔
2074
  int64_t streamId = pStream->pCreate->streamId;
123,835✔
2075

2076
  pStatus->lastActionTs = INT64_MIN;
123,835✔
2077

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

2083
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
123,835✔
2084
  
2085
  if (pStream->pCreate->numOfCalcSubplan > 0) {
123,835✔
2086
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
120,823✔
2087
    
2088
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
120,823✔
2089
  }
2090

2091
  if (initList) {
123,835✔
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:
123,835✔
2110

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

2115
  return code;
123,835✔
2116
}
2117

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

2124
  if (NULL == pStatus) {
128,429✔
2125
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
123,835✔
2126

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

2129
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
123,835✔
2130
  }
2131
  
2132
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
128,429✔
2133

2134
  mstLogSStmStatus("stream deployed", streamId, pStatus);
128,429✔
2135

2136
_exit:
128,429✔
2137

2138
  if (code) {
128,429✔
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;
128,429✔
2148
}
2149

2150

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

2155
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
9,161✔
2156
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
2,913✔
2157
    (void)mstWaitLock(&pVg->lock, true);
2,913✔
2158

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

2165
    for (int32_t i = 0; i < taskNum; ++i) {
15,815✔
2166
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
12,902✔
2167
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
12,902✔
2168
        continue;
11,966✔
2169
      }
2170

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

2178
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
12,167✔
2179
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
5,919✔
2180
    (void)mstWaitLock(&pSnode->lock, true);
5,919✔
2181

2182
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
5,919✔
2183
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
5,919✔
2184
      for (int32_t i = 0; i < taskNum; ++i) {
10,655✔
2185
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
6,806✔
2186
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
6,806✔
2187
          continue;
6,338✔
2188
        }
2189
        
2190
        mstDestroySStmTaskToDeployExt(pExt);
468✔
2191
        pExt->deployed = true;
468✔
2192
      }
2193
    }
2194

2195
    taskNum = taosArrayGetSize(pSnode->runnerList);
5,919✔
2196
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
5,919✔
2197
      for (int32_t i = 0; i < taskNum; ++i) {
27,627✔
2198
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
21,708✔
2199
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
21,708✔
2200
          continue;
20,304✔
2201
        }
2202
        
2203
        mstDestroySStmTaskToDeployExt(pExt);
1,404✔
2204
        pExt->deployed = true;
1,404✔
2205
      }
2206
    }
2207

2208
    taosRUnLockLatch(&pSnode->lock);
5,919✔
2209
  }
2210

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

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

2228
  size_t keyLen = 0;
6,248✔
2229
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
384,879✔
2230
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
378,631✔
2231
    if (*pStreamId == streamId) {
378,631✔
2232
      int64_t taskId = *(pStreamId + 1);
38,664✔
2233
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
38,664✔
2234
      if (code) {
38,664✔
2235
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2236
      } else {
2237
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
38,664✔
2238
      }
2239
    }
2240
  }
2241

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

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

2259
  mstResetSStmStatus(pStatus);
4,594✔
2260

2261
  pStatus->deployTimes++;
4,594✔
2262
}
4,594✔
2263

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

2272
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
128,984✔
2273
  if (pStatus) {
128,984✔
2274
    stopped = atomic_load_8(&pStatus->stopped);
4,594✔
2275
    if (0 == stopped) {
4,594✔
2276
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
468✔
2277
      msmResetStreamForRedeploy(streamId, pStatus);
468✔
2278
    } else {
2279
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
4,126✔
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)) {
4,126✔
2285
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
4,126✔
2286
        msmResetStreamForRedeploy(streamId, pStatus);
4,126✔
2287
      }
2288
    }
2289
  }
2290

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

2297
  TAOS_CHECK_EXIT(code);
128,429✔
2298

2299
  if (pStatus && pStream->pCreate->streamId != streamId) {
128,429✔
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);
128,429✔
2307
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
128,429✔
2308
  if (userStopped || userDropped) {
128,429✔
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));
128,429✔
2314

2315
_exit:
128,429✔
2316

2317
  mndReleaseStream(pCtx->pMnode, pStream);
128,429✔
2318

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

2323
  return code;
128,429✔
2324
}
2325

2326
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
936✔
2327
  int32_t code = TSDB_CODE_SUCCESS;
936✔
2328
  int32_t lino = 0;
936✔
2329
  int64_t streamId = pAction->streamId;
936✔
2330
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
936✔
2331
  if (NULL == ppTask) {
936✔
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};
936✔
2337
  info.task.type = pAction->type;
936✔
2338
  info.task.streamId = pAction->streamId;
936✔
2339
  info.task.taskId = pAction->id.taskId;
936✔
2340
  info.task.seriousId = (*ppTask)->id.seriousId;
936✔
2341
  info.task.nodeId = pAction->id.nodeId;
936✔
2342
  info.task.taskIdx = pAction->id.taskIdx;
936✔
2343
  
2344
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
936✔
2345
  SStreamCalcScan* scanPlan = NULL;
936✔
2346
  if (!isTriggerReader) {
936✔
2347
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
468✔
2348
    if (NULL == scanPlan) {
468✔
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));
936✔
2356
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
936✔
2357

2358
_exit:
936✔
2359

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

2364
  return code;
936✔
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) {
215✔
2409
  int32_t code = TSDB_CODE_SUCCESS;
215✔
2410
  int32_t lino = 0;
215✔
2411
  int64_t streamId = pAction->streamId;
215✔
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;
215✔
2426
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
215✔
2427
//  }
2428
  
2429
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
215✔
2430
  
2431
  SQueryPlan* pPlan = NULL;
215✔
2432
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
215✔
2433
  
2434
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
215✔
2435
  
2436
  taosHashClear(mStreamMgmt.toUpdateScanMap);
215✔
2437
  mStreamMgmt.toUpdateScanNum = 0;
215✔
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:
215✔
2458

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

2463
  return code;
215✔
2464
}
2465

2466

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

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

2476
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,151✔
2477
  if (NULL == pStatus) {
1,151✔
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);
1,151✔
2483
  if (stopped) {
1,151✔
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);
1,151✔
2489
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,151✔
2490
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2491
    return TSDB_CODE_SUCCESS;
×
2492
  }
2493

2494
  TAOS_CHECK_EXIT(code);
1,151✔
2495

2496
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,151✔
2497
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,151✔
2498
  if (userStopped || userDropped) {
1,151✔
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) {
1,151✔
2504
    case STREAM_READER_TASK:
936✔
2505
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
936✔
2506
      break;
936✔
2507
/*
2508
    case STREAM_TRIGGER_TASK:
2509
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2510
      break;
2511
*/
2512
    case STREAM_RUNNER_TASK:
215✔
2513
      if (pAction->multiRunner) {
215✔
2514
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
215✔
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;
215✔
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:
1,151✔
2527

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

2532
  if (code) {
1,151✔
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;
1,151✔
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) {
1,654✔
2589
  int32_t code = TSDB_CODE_SUCCESS;
1,654✔
2590
  int32_t lino = 0;
1,654✔
2591

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

2594
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
1,654✔
2595

2596
_exit:
1,654✔
2597

2598
  if (code) {
1,654✔
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));
1,654✔
2602
  }
2603

2604
  return code;
1,654✔
2605
}
2606

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

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

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

2623
  atomic_store_8(&pStream->stopped, 2);
8,453✔
2624

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

2627
_exit:
×
2628

2629
  taosHashRelease(mStreamMgmt.streamMap, pStream);
8,622✔
2630

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

2635
  return;
8,622✔
2636
}
2637

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

2642
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
2,960✔
2643
  if (0 == active || MND_STM_STATE_NORMAL != state) {
2,960✔
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));
2,960✔
2649
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
2,960✔
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));
2,960✔
2656

2657
_exit:
2,960✔
2658

2659
  taosHashRelease(mStreamMgmt.streamMap, pStream);
2,960✔
2660

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

2665
  return code;
2,960✔
2666
}
2667

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

2673
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
154,205✔
2674
    switch (pQNode->type) {
130,135✔
2675
      case STREAM_ACT_DEPLOY:
130,135✔
2676
        if (pQNode->streamAct) {
130,135✔
2677
          mstDebug("start to handle stream deploy action");
128,984✔
2678
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
128,984✔
2679
        } else {
2680
          mstDebug("start to handle task deploy action");
1,151✔
2681
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,151✔
2682
        }
2683
        break;
130,135✔
2684
      default:
×
2685
        break;
×
2686
    }
2687
  }
2688

2689
_exit:
24,070✔
2690

2691
  if (code) {
24,070✔
2692
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2693
  }
2694
}
24,070✔
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) {
944,650✔
2734
  int32_t code = TSDB_CODE_SUCCESS;
944,650✔
2735
  int32_t lino = 0;
944,650✔
2736
  int64_t streamId = pDeploy->task.streamId;
944,650✔
2737
  
2738
  switch (pDeploy->task.type) {
944,650✔
2739
    case STREAM_READER_TASK:
425,414✔
2740
      if (NULL == pStream->readerTasks) {
425,414✔
2741
        pStream->streamId = streamId;
178,370✔
2742
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
178,370✔
2743
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
178,370✔
2744
      }
2745
      
2746
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
850,828✔
2747
      break;
425,414✔
2748
    case STREAM_TRIGGER_TASK:
127,961✔
2749
      pStream->streamId = streamId;
127,961✔
2750
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
127,961✔
2751
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
127,961✔
2752
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
127,961✔
2753
      break;
127,961✔
2754
    case STREAM_RUNNER_TASK:
391,275✔
2755
      if (NULL == pStream->runnerTasks) {
391,275✔
2756
        pStream->streamId = streamId;
128,971✔
2757
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
128,971✔
2758
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
128,971✔
2759
      }      
2760
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
782,550✔
2761
      break;
391,275✔
2762
    default:
×
2763
      break;
×
2764
  }
2765

2766
_exit:
944,650✔
2767

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

2772
  return code;
944,650✔
2773
}
2774

2775
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
944,650✔
2776
  int32_t code = TSDB_CODE_SUCCESS;
944,650✔
2777
  int32_t lino = 0;
944,650✔
2778
  int64_t streamId = pDeploy->task.streamId;
944,650✔
2779
  SStreamTask* pTask = &pDeploy->task;
944,650✔
2780
  SStmStreamDeploy streamDeploy = {0};
944,650✔
2781
  SStmStreamDeploy* pStream = NULL;
944,650✔
2782
   
2783
  while (true) {
2784
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
944,650✔
2785
    if (NULL == pStream) {
944,650✔
2786
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
186,159✔
2787
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
186,159✔
2788
      if (TSDB_CODE_SUCCESS == code) {
186,159✔
2789
        goto _exit;
186,159✔
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));
758,491✔
2801
    
2802
    break;
758,491✔
2803
  }
2804
  
2805
_exit:
944,650✔
2806

2807
  taosHashRelease(pHash, pStream);
944,650✔
2808

2809
  if (code) {
944,650✔
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);
944,650✔
2813
  }
2814

2815
  return code;
944,650✔
2816
}
2817

2818

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

2824
  for (int32_t i = 0; i < taskNum; ++i) {
1,074,237✔
2825
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
947,458✔
2826
    if (pExt->deployed) {
947,458✔
2827
      continue;
2,808✔
2828
    }
2829

2830
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
944,650✔
2831
    pExt->deployed = true;
944,650✔
2832

2833
    (void)atomic_add_fetch_32(deployed, 1);
944,650✔
2834
  }
2835

2836
_exit:
126,779✔
2837

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

2842
  return code;
126,779✔
2843
}
2844

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

2852
  mstDebug("start to add stream vgroup tasks deploy");
37,369✔
2853
  
2854
  for (int32_t i = 0; i < vgNum; ++i) {
254,988✔
2855
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
217,619✔
2856

2857
    msmUpdateVgroupUpTs(pCtx, *vgId);
217,619✔
2858

2859
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
217,619✔
2860
    if (NULL == pVg) {
217,619✔
2861
      continue;
142,390✔
2862
    }
2863

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

2877
_exit:
37,369✔
2878

2879
  if (code) {
37,369✔
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;
37,369✔
2888
}
2889

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

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

2903
  (void)mstWaitLock(&pSnode->lock, false);
26,068✔
2904
  
2905
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
26,068✔
2906
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
25,482✔
2907
  }
2908

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

2915
_exit:
26,068✔
2916

2917
  if (code) {
26,068✔
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;
26,068✔
2926
}
2927

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

2939
_exit:
414,882✔
2940

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

2945
  return code;
414,882✔
2946
}
2947

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

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

2959
  while (1) {
186,159✔
2960
    pIter = taosHashIterate(pCtx->deployStm, pIter);
223,950✔
2961
    if (pIter == NULL) {
223,950✔
2962
      break;
37,791✔
2963
    }
2964
    
2965
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
186,159✔
2966
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
372,318✔
2967

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

2972
    mstClearSStmStreamDeploy(pDeploy);
186,159✔
2973
    
2974
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
186,159✔
2975
  }
2976
  
2977
_exit:
37,791✔
2978

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

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

2990
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
37,369✔
2991
  int32_t code = TSDB_CODE_SUCCESS;
37,369✔
2992
  int32_t lino = 0;
37,369✔
2993
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
37,369✔
2994
  SStmVgTasksToDeploy* pVg = NULL;
37,369✔
2995
  
2996
  for (int32_t i = 0; i < vgNum; ++i) {
254,988✔
2997
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
217,619✔
2998
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
217,619✔
2999
    if (NULL == pVg) {
217,619✔
3000
      continue;
142,390✔
3001
    }
3002

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

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

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

3031
      mstDestroySStmTaskToDeployExt(pExt);
1,872✔
3032

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

3041
_exit:
37,369✔
3042

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

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

3052
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
50,915✔
3053
  if (!GOT_SNODE(snodeId)) {
50,915✔
3054
    return;
10,071✔
3055
  }
3056
  
3057
  int32_t code = TSDB_CODE_SUCCESS;
40,844✔
3058
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
40,844✔
3059
  if (NULL == pSnode) {
40,844✔
3060
    return;
14,776✔
3061
  }
3062

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

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

3077
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
26,068✔
3078
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
25,756✔
3079
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
25,756✔
3080
    pSnode->triggerList = NULL;
25,756✔
3081
  }
3082

3083
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
26,068✔
3084
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
25,756✔
3085
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
25,756✔
3086
    pSnode->runnerList = NULL;
25,756✔
3087
  }
3088

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

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

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

3111
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
312✔
3112
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
3,120✔
3113
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
2,808✔
3114
      if (!pExt->deployed) {
2,808✔
3115
        continue;
×
3116
      }
3117

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

3130
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
13,302,210✔
3131
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,302,210✔
3132
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
37,369✔
3133
  }
3134

3135
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
13,302,210✔
3136
    msmCleanDeployedSnodeTasks(pHb->snodeId);
50,915✔
3137
  }
3138
}
13,302,210✔
3139

3140
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
13,302,210✔
3141
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
13,302,210✔
3142
  if (mStreamMgmt.tCtx) {
13,302,210✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
13,236,898✔
3144
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
13,236,898✔
3145
  }
3146
}
13,302,210✔
3147

3148
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
182,897✔
3149
  int32_t code = TSDB_CODE_SUCCESS;
182,897✔
3150
  int32_t lino = 0;
182,897✔
3151
  int32_t action = STREAM_ACT_START;
182,897✔
3152
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
182,897✔
3153
  if (pAction) {
182,897✔
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};
182,897✔
3159
    newAction.actions = action;
182,897✔
3160
    newAction.start.triggerId = *pId;
182,897✔
3161
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
182,897✔
3162
    mstsDebug("stream add START action, actions:%x", newAction.actions);
182,897✔
3163
  }
3164

3165
_exit:
182,897✔
3166

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

3171
  return code;
182,897✔
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) {
45,826✔
3202
  int32_t code = TSDB_CODE_SUCCESS;
45,826✔
3203
  int32_t lino = 0;
45,826✔
3204
  int32_t action = STREAM_ACT_UNDEPLOY;
45,826✔
3205
  bool    dropped = false;
45,826✔
3206

3207
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
45,826✔
3208
  mstsDebug("stream dropped: %d", dropped);
45,826✔
3209
  
3210
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
45,826✔
3211
  if (pAction) {
45,826✔
3212
    pAction->actions |= action;
30,146✔
3213
    if (NULL == pAction->undeploy.taskList) {
30,146✔
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);
60,292✔
3219
    if (pAction->undeploy.doCheckpoint) {
30,146✔
3220
      pAction->undeploy.doCheckpoint = dropped ? false : true;
17,860✔
3221
    }
3222
    if (!pAction->undeploy.doCleanup) {
30,146✔
3223
      pAction->undeploy.doCleanup = dropped ? true : false;
17,860✔
3224
    }
3225
    
3226
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
30,146✔
3227
  } else {
3228
    SStmAction newAction = {0};
15,680✔
3229
    newAction.actions = action;
15,680✔
3230
    newAction.undeploy.doCheckpoint = dropped ? false : true;
15,680✔
3231
    newAction.undeploy.doCleanup = dropped ? true : false;
15,680✔
3232
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
15,680✔
3233
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
15,680✔
3234
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
31,360✔
3235
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
15,680✔
3236
    
3237
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
15,680✔
3238
  }
3239

3240
_exit:
45,826✔
3241

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

3246
  return code;
45,826✔
3247
}
3248

3249
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
2,960✔
3250
  int32_t code = TSDB_CODE_SUCCESS;
2,960✔
3251
  int32_t lino = 0;
2,960✔
3252
  int32_t action = STREAM_ACT_RECALC;
2,960✔
3253
  SStmAction newAction = {0};
2,960✔
3254
  
3255
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
2,960✔
3256
  if (pAction) {
2,960✔
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;
2,960✔
3263
    newAction.recalc.recalcList = recalcList;
2,960✔
3264
    
3265
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
2,960✔
3266
    
3267
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
2,960✔
3268
  }
3269

3270
_exit:
2,960✔
3271

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

3277
  return code;
2,960✔
3278
}
3279

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

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

3290
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
246,823✔
3291
  for (int32_t i = 0; i < readerNum; ++i) {
540,556✔
3292
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
293,733✔
3293
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
293,733✔
3294
      return false;
×
3295
    }
3296
  }
3297

3298
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
246,823✔
3299
  for (int32_t i = 0; i < readerNum; ++i) {
302,037✔
3300
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
55,214✔
3301
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
55,214✔
3302
      return false;
×
3303
    }
3304
  }
3305

3306
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
246,823✔
3307
  SListNode* pNode = listHead(pStream->calcReaders);
246,823✔
3308
  for (int32_t i = 0; i < readerNum; ++i) {
763,730✔
3309
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
516,907✔
3310
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
516,907✔
3311
      return false;
×
3312
    }
3313
    pNode = TD_DLIST_NODE_NEXT(pNode);
516,907✔
3314
  }
3315

3316
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
781,697✔
3317
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
598,800✔
3318
    for (int32_t m = 0; m < runnerNum; ++m) {
1,150,102✔
3319
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
615,228✔
3320
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
615,228✔
3321
        return false;
63,926✔
3322
      }
3323
    }
3324
  }
3325
  
3326
  return true;
182,897✔
3327
}
3328

3329

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

3338
  msttDebug("start to handle task abnormal status %d", pTask->status);
7,969,507✔
3339
  
3340
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
7,969,507✔
3341
  if (NULL == pStatus) {
7,969,507✔
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;
10,395✔
3345
  }
3346

3347
  stopped = atomic_load_8(&pStatus->stopped);
7,969,507✔
3348
  if (stopped) {
7,969,507✔
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) {
7,969,507✔
3355
    case STREAM_STATUS_INIT:      
7,965,729✔
3356
      if (STREAM_TRIGGER_TASK != pMsg->type) {
7,965,729✔
3357
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
7,606,360✔
3358
        return;
7,606,360✔
3359
      }
3360
      
3361
      if (INT64_MIN == pStatus->lastActionTs) {
359,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) {
359,369✔
3367
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
112,546✔
3368
        return;
112,546✔
3369
      }
3370

3371
      if (STREAM_IS_RUNNING(pStatus)) {
246,823✔
3372
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3373
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
246,823✔
3374
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
182,897✔
3375
      }
3376
      break;
246,823✔
3377
    case STREAM_STATUS_FAILED:
3,778✔
3378
      //STREAMTODO ADD ERRCODE HANDLE
3379
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
3,778✔
3380
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
3,778✔
3381
                 pMsg->taskIdx);
3382
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
3,778✔
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;
3,778✔
3389
    default:
×
3390
      break;
×
3391
  }
3392

3393
_exit:
250,601✔
3394

3395
  if (code) {
250,601✔
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) {
45,826✔
3402
  int32_t code = TSDB_CODE_SUCCESS;
45,826✔
3403
  int32_t lino = 0;
45,826✔
3404
  SStreamTask* pTask = (SStreamTask*)pStatus;
45,826✔
3405
  int64_t streamId = pStatus->streamId;
45,826✔
3406

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

3409
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
45,826✔
3410

3411
_exit:
45,826✔
3412

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

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

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

3442
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
2,614,346✔
3443
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,322,158✔
3444
    if (NULL == pStatus->detailStatus) {
1,322,158✔
3445
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
127,206✔
3446
      if (NULL == pStatus->detailStatus) {
127,206✔
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));
1,322,158✔
3453
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,322,158✔
3454
  }
3455

3456
_exit:
1,292,188✔
3457

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

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

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

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

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

3490
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
20,088,406✔
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) {
20,088,406✔
3499
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,647,134✔
3500
        (*ppStatus)->runningStartTs = pCtx->currTs;
706,092✔
3501
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
941,042✔
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;
20,088,406✔
3511
    (*ppStatus)->status = pTask->status;
20,088,406✔
3512
    (*ppStatus)->lastUpTs = pCtx->currTs;
20,088,406✔
3513
    
3514
    if (STREAM_STATUS_RUNNING != pTask->status) {
20,088,406✔
3515
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
7,969,507✔
3516
    }
3517
    
3518
    if (STREAM_TRIGGER_TASK == pTask->type) {
20,088,406✔
3519
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
2,614,346✔
3520
    }
3521
  }
3522

3523
_exit:
683,257✔
3524

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

3529
  return code;
683,257✔
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) {
182,897✔
3678
  int32_t code = TSDB_CODE_SUCCESS;
182,897✔
3679
  int32_t lino = 0;
182,897✔
3680
  if (NULL == pCtx->pRsp->start.taskList) {
182,897✔
3681
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
66,902✔
3682
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
66,902✔
3683
  }
3684

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

3694
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
365,794✔
3695
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
182,897✔
3696

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

3699
  return;
182,897✔
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) {
15,680✔
3708
  int32_t code = TSDB_CODE_SUCCESS;
15,680✔
3709
  int32_t lino = 0;
15,680✔
3710
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
15,680✔
3711
  if (NULL == pCtx->pRsp->undeploy.taskList) {
15,680✔
3712
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
11,859✔
3713
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
11,859✔
3714
  }
3715

3716
  SStreamTaskUndeploy undeploy;
15,680✔
3717
  for (int32_t i = 0; i < dropNum; ++i) {
61,506✔
3718
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
45,826✔
3719
    undeploy.task = *pTask;
45,826✔
3720
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
45,826✔
3721
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
45,826✔
3722

3723
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
91,652✔
3724
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
45,826✔
3725

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

3729
  return;
15,680✔
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) {
2,960✔
3776
  int32_t code = TSDB_CODE_SUCCESS;
2,960✔
3777
  int32_t lino = 0;
2,960✔
3778

3779
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,960✔
3780
  if (NULL == pStream) {
2,960✔
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) {
2,960✔
3786
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3787
    return;
×
3788
  }
3789

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

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

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

3804
_exit:
2,960✔
3805

3806
  if (code) {
2,960✔
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));
2,960✔
3810
  }
3811
}
3812

3813

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

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

3822
  while (1) {
201,537✔
3823
    pIter = taosHashIterate(pCtx->actionStm, pIter);
282,803✔
3824
    if (pIter == NULL) {
282,803✔
3825
      break;
81,266✔
3826
    }
3827

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

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

3840
    if (STREAM_ACT_RECALC & pAction->actions) {
185,857✔
3841
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
2,960✔
3842
    }
3843

3844
    if (STREAM_ACT_START & pAction->actions) {
185,857✔
3845
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
182,897✔
3846
    }
3847
  }
3848
  
3849
_exit:
81,266✔
3850

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

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

3859
  return code;
81,266✔
3860
}
3861

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

3868
  while (true) {
3869
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
13,738,381✔
3870
    if (NULL == lastTs) {
13,738,381✔
3871
      if (noExists) {
696,446✔
3872
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
194,963✔
3873
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
194,963✔
3874
      }
3875

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

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

3890
        continue;
×
3891
      }
3892

3893
      return code;
1,536✔
3894
    }
3895

3896
    break;
3897
  }
3898

3899
_exit:
194,963✔
3900

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

3905
  return code;  
194,963✔
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) {
620✔
3956
  int32_t code = TSDB_CODE_SUCCESS;
620✔
3957
  int32_t lino = 0;
620✔
3958
  int32_t minVal = watchError ? 0 : 1;
620✔
3959

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

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

3968
  if (watchError) {
620✔
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)) {
620✔
3978
    mstInfo("no stream tasks remain during watch state");
620✔
3979
    goto _exit;
620✔
3980
  }
3981

3982
  msmWatchCheckStreamMap(pCtx);
×
3983

3984
_exit:
620✔
3985

3986
  mStreamMgmt.lastTaskId += 100000;
620✔
3987

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

3990
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
620✔
3991

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

3996
  return code;
620✔
3997
}
3998

3999

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

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

4011
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
11,675✔
4012
  if (GOT_SNODE(pReq->snodeId)) {
11,675✔
4013
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,183✔
4014
  }
4015

4016
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
11,675✔
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) {
11,675✔
4022
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
620✔
4023
  }
4024

4025
_exit:
11,675✔
4026

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

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

4035
  return code;
11,675✔
4036
}
4037

4038
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
4,585,091✔
4039
  int32_t listSize = taosArrayGetSize(pOReaders);
4,585,091✔
4040
  int32_t totalSize = 0;
4,585,091✔
4041
  
4042
  for (int32_t i = 0; i < listSize; ++i) {
5,481,741✔
4043
    SArray* pList = taosArrayGetP(pOReaders, i);
896,650✔
4044
    totalSize += taosArrayGetSize(pList);
896,650✔
4045
  }
4046

4047
  return totalSize;
4,585,091✔
4048
}
4049

4050
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
567,319✔
4051
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
567,319✔
4052
  if (NULL == pList) {
567,319✔
4053
    return NULL;
×
4054
  }
4055

4056
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
567,319✔
4057
}
4058

4059

4060
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
55,214✔
4061
  int32_t code = TSDB_CODE_SUCCESS;
55,214✔
4062
  int32_t lino = 0;
55,214✔
4063

4064
  if (NULL == pStatus->trigOReaders) {
55,214✔
4065
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
47,796✔
4066
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
47,796✔
4067
  }
4068

4069
  while (true) {
47,796✔
4070
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
103,010✔
4071

4072
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
103,010✔
4073
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
47,796✔
4074
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
47,796✔
4075

4076
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
95,592✔
4077
      continue;
47,796✔
4078
    }
4079

4080
    *ppRes = *ppOReaderList;
55,214✔
4081
    break;
55,214✔
4082
  }
4083

4084
_exit:
55,214✔
4085

4086
  if (code) {
55,214✔
4087
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4088
  }
4089

4090
  return code;
55,214✔
4091
}
4092

4093
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
63,940✔
4094
  int32_t code = TSDB_CODE_SUCCESS;
63,940✔
4095
  int32_t lino = 0;
63,940✔
4096
  bool    readerExists = false;
63,940✔
4097
  int64_t streamId = pTask->streamId;
63,940✔
4098

4099
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
63,940✔
4100
  for (int32_t i = 0; i < readerNum; ++i) {
134,115✔
4101
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
78,901✔
4102
    if (pReader->id.nodeId == vgId) {
78,901✔
4103
      readerExists = true;
8,726✔
4104
      break;
8,726✔
4105
    }
4106
  }
4107

4108
  if (!readerExists) {
63,940✔
4109
    SArray* pReaderList = NULL;
55,214✔
4110
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
55,214✔
4111
    
4112
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
55,214✔
4113
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
55,214✔
4114
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
55,214✔
4115
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
55,214✔
4116
  }
4117

4118
_exit:
8,726✔
4119

4120
  if (code) {
63,940✔
4121
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4122
  }
4123

4124
  return code;
63,940✔
4125
}
4126

4127
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
56,003✔
4128
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
56,003✔
4129
  int32_t lino = 0;
56,003✔
4130
  int32_t vgId = 0;
56,003✔
4131
  int64_t streamId = pTask->streamId;
56,003✔
4132
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
56,003✔
4133
  int32_t tbNum = taosArrayGetSize(pTbs);
56,003✔
4134
  SStreamDbTableName* pName = NULL;
56,003✔
4135
  SSHashObj* pDbVgroups = NULL;
56,003✔
4136
  SStreamMgmtRsp rsp = {0};
56,003✔
4137
  rsp.reqId = pTask->pMgmtReq->reqId;
56,003✔
4138
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
56,003✔
4139
  int32_t iter = 0;
56,003✔
4140
  void* p = NULL;
56,003✔
4141
  SSHashObj* pVgs = NULL;
56,003✔
4142
  SStreamMgmtReq* pMgmtReq = NULL;
56,003✔
4143
  int8_t stopped = 0;
56,003✔
4144

4145
  if (NULL == pCtx->pRsp->rsps.rspList) {
56,003✔
4146
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4147
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4148
  }
4149
  
4150
  TSWAP(pTask->pMgmtReq, pMgmtReq);
56,003✔
4151
  rsp.task = *(SStreamTask*)pTask;
56,003✔
4152

4153
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
56,003✔
4154
  if (NULL == pStatus) {
56,003✔
4155
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4156
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4157
  }
4158

4159
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
56,003✔
4160
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4161
    goto _exit;
×
4162
  }
4163

4164
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
56,003✔
4165

4166
  stopped = atomic_load_8(&pStatus->stopped);
56,003✔
4167
  if (stopped) {
56,003✔
4168
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4169
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4170
  }
4171

4172
  if (tbNum <= 0) {
56,003✔
4173
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4174
    goto _exit;
×
4175
  }
4176

4177
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
56,003✔
4178
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
56,003✔
4179
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
56,003✔
4180

4181
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
56,003✔
4182
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
56,003✔
4183
  
4184
  for (int32_t i = 0; i < tbNum; ++i) {
223,604✔
4185
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
167,601✔
4186
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
167,601✔
4187
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
335,202✔
4188
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
167,601✔
4189
  }
4190

4191
  int32_t vgNum = tSimpleHashGetSize(pVgs);
56,003✔
4192
  while (true) {
4193
    p = tSimpleHashIterate(pVgs, p, &iter);
119,943✔
4194
    if (NULL == p) {
119,943✔
4195
      break;
56,003✔
4196
    }
4197
    
4198
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
63,940✔
4199
  }
4200
  
4201
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
56,003✔
4202
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
56,003✔
4203
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
56,003✔
4204

4205
  SStreamTaskAddr addr;
56,003✔
4206
  for (int32_t i = 0; i < vgNum; ++i) {
111,217✔
4207
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
55,214✔
4208
    addr.taskId = pOTask->id.taskId;
55,214✔
4209
    addr.nodeId = pOTask->id.nodeId;
55,214✔
4210
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
55,214✔
4211
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
110,428✔
4212
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
55,214✔
4213
  }
4214

4215
_exit:
56,003✔
4216

4217
  tFreeSStreamMgmtReq(pMgmtReq);
56,003✔
4218
  taosMemoryFree(pMgmtReq);
56,003✔
4219

4220
  tSimpleHashCleanup(pVgs);
56,003✔
4221
  mstDestroyDbVgroupsHash(pDbVgroups);
56,003✔
4222

4223
  if (code) {
56,003✔
4224
    rsp.code = code;
×
4225
    
4226
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4227

4228
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4229
  } else {
4230
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
112,006✔
4231
  }
4232

4233
_final:
56,003✔
4234

4235
  if (finalCode) {
56,003✔
4236
    tFreeSStreamMgmtRsp(&rsp);
×
4237
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4238
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4239
  }
4240

4241
  return finalCode;
56,003✔
4242
}
4243

4244
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,145✔
4245
  int32_t num = taosArrayGetSize(pList);
2,145✔
4246
  SStreamCalcScan* pScan = NULL;
2,145✔
4247
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,145✔
4248
  int64_t planUid = 0;
2,145✔
4249
  for (int32_t i = 0; i < num; ++i) {
2,145✔
4250
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,145✔
4251
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,145✔
4252
    if (0 != planUid && planUid == uid) {
2,145✔
4253
      *ppRes = pScan;
2,145✔
4254
      break;
2,145✔
4255
    }
4256
  }
4257

4258
_exit:
2,145✔
4259

4260
  if (code) {
2,145✔
4261
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4262
  }
4263

4264
  return code;
2,145✔
4265
}
4266

4267
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,145✔
4268
  int32_t code = TSDB_CODE_SUCCESS;
2,145✔
4269
  int32_t lino = 0;
2,145✔
4270
  bool    readerExists = false;
2,145✔
4271
  int64_t streamId = pTask->streamId;
2,145✔
4272
  SListNode* pNode = listHead(pStatus->calcReaders);
2,145✔
4273
  SStmTaskStatus* pReader = NULL;
2,145✔
4274
  int32_t taskIdx = 0;
2,145✔
4275

4276
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,145✔
4277
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
19,890✔
4278
    pReader = (SStmTaskStatus*)pNode->data;
17,745✔
4279
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
17,745✔
4280
      readerExists = true;
×
4281
      pAddr->taskId = pReader->id.taskId;
×
4282
      break;
×
4283
    }
4284
  }
4285

4286
  if (!readerExists) {
2,145✔
4287
    if (NULL == pStatus->calcReaders) {
2,145✔
4288
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4289
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4290
      taskIdx = 0;
×
4291
    } else {
4292
      pNode = listTail(pStatus->calcReaders);
2,145✔
4293
      pReader = (SStmTaskStatus*)pNode->data;
2,145✔
4294
      taskIdx = pReader->id.taskIdx + 1;
2,145✔
4295
    }
4296

4297
    SStreamCalcScan* pScan = NULL;
2,145✔
4298
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,145✔
4299
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,145✔
4300
    pReader = tdListReserve(pStatus->calcReaders);
2,145✔
4301
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,145✔
4302
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,145✔
4303
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,145✔
4304
    pAddr->taskId = pReader->id.taskId;
2,145✔
4305
  }
4306

4307
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,145✔
4308
  pAddr->nodeId = vgId;
2,145✔
4309

4310
_exit:
2,145✔
4311

4312
  if (code) {
2,145✔
4313
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4314
  }
4315

4316
  return code;
2,145✔
4317
}
4318

4319

4320
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,145✔
4321
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,145✔
4322
  int32_t lino = 0;
2,145✔
4323
  int32_t vgId = 0;
2,145✔
4324
  int64_t streamId = pTask->streamId;
2,145✔
4325
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,145✔
4326
  int32_t reqNum = taosArrayGetSize(pReqs);
2,145✔
4327
  SStreamOReaderDeployReq* pReq = NULL;
2,145✔
4328
  SStreamOReaderDeployRsp* pRsp = NULL;
2,145✔
4329
  SStreamMgmtRsp rsp = {0};
2,145✔
4330
  rsp.reqId = pTask->pMgmtReq->reqId;
2,145✔
4331
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,145✔
4332
  SStreamMgmtReq* pMgmtReq = NULL;
2,145✔
4333
  int8_t stopped = 0;
2,145✔
4334
  int32_t vgNum = 0;
2,145✔
4335
  SStreamTaskAddr* pAddr = NULL;
2,145✔
4336

4337
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,145✔
4338
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4339
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4340
  }
4341
  
4342
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,145✔
4343
  rsp.task = *(SStreamTask*)pTask;
2,145✔
4344

4345
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,145✔
4346
  if (NULL == pStatus) {
2,145✔
4347
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4348
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4349
  }
4350

4351
  stopped = atomic_load_8(&pStatus->stopped);
2,145✔
4352
  if (stopped) {
2,145✔
4353
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4354
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4355
  }
4356

4357
  if (reqNum <= 0) {
2,145✔
4358
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4359
    goto _exit;
×
4360
  }
4361

4362
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,145✔
4363
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,145✔
4364

4365
  for (int32_t i = 0; i < reqNum; ++i) {
4,290✔
4366
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,145✔
4367
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,145✔
4368
    pRsp->execId = pReq->execId;
2,145✔
4369
    vgNum = taosArrayGetSize(pReq->vgIds);
2,145✔
4370
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,145✔
4371
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,145✔
4372
    
4373
    for (int32_t n = 0; n < vgNum; ++n) {
4,290✔
4374
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,145✔
4375
      pAddr = taosArrayGet(pRsp->vgList, n);
2,145✔
4376
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,145✔
4377
    }
4378
  }
4379

4380
_exit:
2,145✔
4381

4382
  tFreeSStreamMgmtReq(pMgmtReq);
2,145✔
4383
  taosMemoryFree(pMgmtReq);
2,145✔
4384

4385
  if (code) {
2,145✔
4386
    rsp.code = code;
×
4387
    
4388
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4389
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4390
  } else {
4391
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,290✔
4392
  }
4393

4394

4395
_final:
2,145✔
4396

4397
  if (finalCode) {
2,145✔
4398
    tFreeSStreamMgmtRsp(&rsp);
×
4399
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4400
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4401
  }
4402

4403
  return finalCode;
2,145✔
4404
}
4405

4406

4407
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
58,148✔
4408
  int32_t code = TSDB_CODE_SUCCESS;
58,148✔
4409
  int32_t lino = 0;
58,148✔
4410

4411
  switch (pTask->pMgmtReq->type) {
58,148✔
4412
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
56,003✔
4413
      msmDeployTriggerOrigReader(pCtx, pTask);
56,003✔
4414
      break;
56,003✔
4415
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,145✔
4416
      msmDeployRunnerOrigReader(pCtx, pTask);
2,145✔
4417
      break;
2,145✔
4418
    default:
×
4419
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4420
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4421
      break;
×
4422
  }
4423

4424
_exit:
58,148✔
4425

4426
  if (code) {
58,148✔
4427
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4428
  }
4429

4430
  return code;
58,148✔
4431
}
4432

4433
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
15,983✔
4434
  int32_t code = TSDB_CODE_SUCCESS;
15,983✔
4435
  int32_t lino = 0;
15,983✔
4436
  SStreamHbMsg* pReq = pCtx->pReq;
15,983✔
4437
  SStmTaskStatusMsg* pTask = NULL;
15,983✔
4438
  
4439
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
15,983✔
4440
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
15,983✔
4441
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
15,983✔
4442
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
15,983✔
4443
  }
4444
  
4445
  for (int32_t i = 0; i < reqNum; ++i) {
74,131✔
4446
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
58,148✔
4447
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
58,148✔
4448
    if (NULL == pTask) {
58,148✔
4449
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4450
      continue;
×
4451
    }
4452

4453
    if (NULL == pTask->pMgmtReq) {
58,148✔
4454
      msttError("idx %d without mgmtReq", idx);
×
4455
      continue;
×
4456
    }
4457

4458
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
58,148✔
4459
  }
4460

4461
_exit:
15,983✔
4462

4463
  if (code) {
15,983✔
4464
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4465
  }
4466

4467
  return code;
15,983✔
4468
}
4469

4470
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
13,225,223✔
4471
  int32_t code = TSDB_CODE_SUCCESS;
13,225,223✔
4472
  int32_t lino = 0;
13,225,223✔
4473
  SStreamHbMsg* pReq = pCtx->pReq;
13,225,223✔
4474

4475
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
13,225,223✔
4476
  if (GOT_SNODE(pReq->snodeId)) {
13,030,260✔
4477
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
750,227✔
4478
  }
4479
  
4480
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
13,030,071✔
4481
    msmHandleStreamActions(pCtx);
24,070✔
4482
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
24,070✔
4483
  }
4484

4485
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
13,030,071✔
4486
    code = msmHandleStreamRequests(pCtx);
15,983✔
4487
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
15,983✔
4488
    TAOS_CHECK_EXIT(code);
15,983✔
4489
  }
4490

4491
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,030,071✔
4492
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
37,369✔
4493
  } else {
4494
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
12,992,702✔
4495
  }
4496

4497
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
13,030,071✔
4498
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
40,844✔
4499
  }
4500

4501
  if (taosHashGetSize(pCtx->deployStm) > 0) {
13,030,071✔
4502
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
37,791✔
4503
  }
4504

4505
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,030,071✔
4506
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
683,257✔
4507
  }
4508

4509
  if (taosHashGetSize(pCtx->actionStm) > 0) {
13,030,071✔
4510
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
81,266✔
4511
  }
4512

4513
_exit:
13,030,071✔
4514

4515
  if (code) {
13,225,223✔
4516
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
195,152✔
4517
  }
4518

4519
  return code;
13,225,223✔
4520
}
4521

4522
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
13,302,210✔
4523
  int32_t lino = 0;
13,302,210✔
4524
  int32_t tlen = 0;
13,302,210✔
4525
  void   *buf = NULL;
13,302,210✔
4526

4527
  if (TSDB_CODE_SUCCESS != code) {
13,302,210✔
4528
    goto _exit;
195,152✔
4529
  }
4530

4531
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
13,107,058✔
4532
  if (code < 0) {
13,107,058✔
4533
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4534
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4535
  }
4536

4537
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
13,107,058✔
4538
  if (buf == NULL) {
13,107,058✔
4539
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4540
    TAOS_CHECK_EXIT(terrno);    
×
4541
  }
4542

4543
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
13,107,058✔
4544
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
13,107,058✔
4545

4546
  SEncoder encoder;
13,104,931✔
4547
  tEncoderInit(&encoder, abuf, tlen);
13,107,058✔
4548
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
13,107,058✔
4549
    rpcFreeCont(buf);
×
4550
    buf = NULL;
×
4551
    tEncoderClear(&encoder);
×
4552
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4553
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4554
  }
4555
  tEncoderClear(&encoder);
13,107,058✔
4556

4557
_exit:
13,302,210✔
4558

4559
  pMsg->code = code;
13,302,210✔
4560
  pMsg->info = *pRpcInfo;
13,302,210✔
4561
  if (TSDB_CODE_SUCCESS == code) {
13,302,210✔
4562
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
13,107,058✔
4563
    pMsg->pCont = buf;
13,107,058✔
4564
  }
4565
}
13,302,210✔
4566

4567

4568
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
13,302,210✔
4569
  int32_t code = TSDB_CODE_SUCCESS;
13,302,210✔
4570
  SMStreamHbRspMsg rsp = {0};
13,302,210✔
4571
  rsp.streamGId = pHb->streamGId;
13,302,210✔
4572

4573
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
13,302,210✔
4574

4575
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
13,302,210✔
4576
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
65,312✔
4577
    goto _exit;
65,312✔
4578
  }
4579

4580
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
13,236,898✔
4581
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
13,236,898✔
4582

4583
  pCtx->tidx = tidx;
13,236,898✔
4584
  pCtx->pMnode = pMnode;
13,236,898✔
4585
  pCtx->currTs = currTs;
13,236,898✔
4586
  pCtx->pReq = pHb;
13,236,898✔
4587
  pCtx->pRsp = &rsp;
13,236,898✔
4588
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
13,236,898✔
4589
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
13,236,898✔
4590
  
4591
  switch (atomic_load_8(&mStreamMgmt.state)) {
13,236,898✔
4592
    case MND_STM_STATE_WATCH:
11,675✔
4593
      code = msmWatchHandleHbMsg(pCtx);
11,675✔
4594
      break;
11,675✔
4595
    case MND_STM_STATE_NORMAL:
13,225,223✔
4596
      code = msmNormalHandleHbMsg(pCtx);
13,225,223✔
4597
      break;
13,225,223✔
4598
    default:
×
4599
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4600
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4601
      break;
×
4602
  }
4603

4604
_exit:
13,302,210✔
4605

4606
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
13,302,210✔
4607

4608
  msmCleanStreamGrpCtx(pHb);
13,302,210✔
4609
  msmClearStreamToDeployMaps(pHb);
13,302,210✔
4610

4611
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
13,302,210✔
4612
  
4613
  tFreeSMStreamHbRspMsg(&rsp);
13,302,210✔
4614

4615
  return code;
13,302,210✔
4616
}
4617

4618
void msmHandleBecomeLeader(SMnode *pMnode) {
355,694✔
4619
  if (tsDisableStream) {
355,694✔
4620
    return;
×
4621
  }
4622

4623
  mstInfo("start to process mnode become leader");
355,694✔
4624

4625
  int32_t code = 0;
355,694✔
4626
  streamAddVnodeLeader(MNODE_HANDLE);
355,694✔
4627
  
4628
  taosWLockLatch(&mStreamMgmt.runtimeLock);
355,694✔
4629
  msmDestroyRuntimeInfo(pMnode);
355,694✔
4630
  code = msmInitRuntimeInfo(pMnode);
355,694✔
4631
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
355,694✔
4632

4633
  if (TSDB_CODE_SUCCESS == code) {
355,694✔
4634
    atomic_store_8(&mStreamMgmt.active, 1);
355,694✔
4635
  }
4636

4637
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
355,694✔
4638
}
4639

4640
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
434,545✔
4641
  if (tsDisableStream) {
434,545✔
4642
    return;
×
4643
  }
4644

4645
  mstInfo("start to process mnode become not leader");
434,545✔
4646

4647
  streamRemoveVnodeLeader(MNODE_HANDLE);
434,545✔
4648

4649
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
434,545✔
4650
    taosWLockLatch(&mStreamMgmt.runtimeLock);
355,694✔
4651
    msmDestroyRuntimeInfo(pMnode);
355,694✔
4652
    mStreamMgmt.stat.inactiveTimes++;
355,694✔
4653
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
355,694✔
4654
  }
4655
}
4656

4657

4658
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4659
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4660
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4661
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4662
  } else {
4663
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4664
  }
4665
}
×
4666

4667
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
6,613✔
4668
  int32_t code = TSDB_CODE_SUCCESS;
6,613✔
4669
  int32_t lino = 0;
6,613✔
4670
  SStreamObj* pStream = pObj;
6,613✔
4671
  SSnodeObj* pSnode = p1;
6,613✔
4672
  SArray** ppRes = p2;
6,613✔
4673

4674
  if (pStream->mainSnodeId == pSnode->id) {
6,613✔
4675
    if (NULL == *ppRes) {
3,898✔
4676
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
915✔
4677
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
915✔
4678
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
915✔
4679
    }
4680

4681
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
7,796✔
4682
  }
4683

4684
  return true;
6,613✔
4685

4686
_exit:
×
4687

4688
  if (code) {
×
4689
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4690
  }  
4691

4692
  *(int32_t*)p3 = code;
×
4693

4694
  return false;
×
4695
}
4696

4697

4698
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
35,061✔
4699
  int32_t code = TSDB_CODE_SUCCESS;
35,061✔
4700
  int32_t lino = 0;
35,061✔
4701
  
4702
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
35,061✔
4703
  TAOS_CHECK_EXIT(code);
35,061✔
4704

4705
  int32_t streamNum = taosArrayGetSize(*ppRes);
35,061✔
4706
  if (streamNum > 0 && 0 == pSnode->replicaId) {
35,061✔
4707
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
173✔
4708
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
173✔
4709
  }
4710

4711
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4712

4713
_exit:
35,061✔
4714

4715
  if (code) {
35,061✔
4716
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
173✔
4717
  }  
4718

4719
  return code;
35,061✔
4720
}
4721

4722
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
364,304✔
4723
  SStreamObj* pStream = pObj;
364,304✔
4724
  int64_t streamId = pStream->pCreate->streamId;
364,304✔
4725
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
364,304✔
4726
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
364,304✔
4727
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
364,304✔
4728
  
4729
  if ((userDropped || userStopped) && (NULL == pStatus)) {
364,304✔
4730
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
346✔
4731
    return true;
346✔
4732
  }
4733
  
4734
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
363,958✔
4735
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
261,470✔
4736
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4737
    return true;
261,470✔
4738
  }
4739

4740
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
102,488✔
4741
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
23,629✔
4742
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4743
    return true;
23,629✔
4744
  }  
4745

4746
  if (pStatus) {
78,859✔
4747
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
78,284✔
4748
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4749
    }
4750

4751
    return true;
78,284✔
4752
  }
4753

4754
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
575✔
4755

4756
  return true;
575✔
4757
}
4758

4759
void msmCheckLoopStreamMap(SMnode *pMnode) {
30,600✔
4760
  SStmStatus* pStatus = NULL;
30,600✔
4761
  void* pIter = NULL;
30,600✔
4762
  int8_t stopped = 0;
30,600✔
4763
  int64_t streamId = 0;
30,600✔
4764
  
4765
  while (true) {
4766
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
222,012✔
4767
    if (NULL == pIter) {
222,012✔
4768
      break;
30,600✔
4769
    }
4770

4771
    pStatus = (SStmStatus*)pIter;
191,412✔
4772

4773
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
191,412✔
4774
    stopped = atomic_load_8(&pStatus->stopped);
191,412✔
4775
    if (MST_IS_USER_STOPPED(stopped)) {
191,412✔
4776
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
1,654✔
4777
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
1,654✔
4778
      continue;
1,654✔
4779
    }
4780

4781
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
189,758✔
4782
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4783
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4784
      continue;
×
4785
    }
4786

4787
    if (MST_IS_ERROR_STOPPED(stopped)) {
189,758✔
4788
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
9,297✔
4789
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
6,263✔
4790
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4791
            
4792
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
6,263✔
4793
        continue;
6,263✔
4794
      }
4795

4796
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
3,034✔
4797
      continue;
3,034✔
4798
    }
4799

4800
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
180,461✔
4801
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4802
      continue;
×
4803
    }
4804
  }
4805
}
30,600✔
4806

4807
void msmCheckStreamsStatus(SMnode *pMnode) {
226,670✔
4808
  SStmCheckStatusCtx ctx = {0};
226,670✔
4809

4810
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
226,670✔
4811
  
4812
  if (MST_READY_FOR_SDB_LOOP()) {
226,670✔
4813
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
43,558✔
4814
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
43,558✔
4815
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
43,558✔
4816
  }
4817

4818
  if (MST_READY_FOR_MAP_LOOP()) {
226,670✔
4819
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
30,600✔
4820
    msmCheckLoopStreamMap(pMnode);
30,600✔
4821
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
30,600✔
4822
  }
4823
}
226,670✔
4824

4825
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
2,103,147✔
4826
  for (int32_t i = 0; i < taskNum; ++i) {
4,316,058✔
4827
    SStmTaskStatus* pTask = *(pList + i);
2,212,911✔
4828

4829
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
2,212,911✔
4830
      continue;
52,847✔
4831
    }
4832
    
4833
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
2,161,702✔
4834
      continue;
2,160,766✔
4835
    }
4836

4837
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
936✔
4838
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
936✔
4839
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4840
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4841
          
4842
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
4843
      break;
×
4844
    }
4845

4846
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
936✔
4847
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4848

4849
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
936✔
4850
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
936✔
4851

4852
    SStmTaskAction task = {0};
936✔
4853
    task.streamId = streamId;
936✔
4854
    task.id = pTask->id;
936✔
4855
    task.flag = pTask->flags;
936✔
4856
    task.type = pTask->type;
936✔
4857
    
4858
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
936✔
4859
  }
4860
}
2,103,147✔
4861

4862
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
95,096✔
4863
  void* pIter = NULL;
95,096✔
4864
  SStmVgStreamStatus* pVg = NULL;
95,096✔
4865
  int64_t streamId = 0;
95,096✔
4866
  
4867
  while (true) {
841,483✔
4868
    pIter = taosHashIterate(pStreams, pIter);
936,579✔
4869
    if (NULL == pIter) {
936,579✔
4870
      break;
95,096✔
4871
    }
4872

4873
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
841,483✔
4874
    pVg = (SStmVgStreamStatus*)pIter;
841,483✔
4875

4876
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
841,483✔
4877
    if (taskNum > 0) {
841,483✔
4878
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
477,173✔
4879
    }
4880

4881
    taskNum = taosArrayGetSize(pVg->calcReaders);
841,483✔
4882
    if (taskNum > 0) {
841,483✔
4883
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
545,014✔
4884
    }
4885
  }
4886
}
95,096✔
4887

4888
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4889
  int64_t streamId = 0;
×
4890
  void* pIter = NULL;
×
4891
  SStmVgStreamStatus* pStream = NULL;
×
4892

4893
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4894
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4895
    return;
×
4896
  }
4897

4898
  
4899
  while (true) {
4900
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4901
    if (NULL == pIter) {
×
4902
      break;
×
4903
    }
4904

4905
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4906
    
4907
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4908
  }
4909

4910
  taosHashClear(pVg->streamTasks);
×
4911
}
4912

4913

4914
void msmCheckVgroupStatus(SMnode *pMnode) {
226,670✔
4915
  void* pIter = NULL;
226,670✔
4916
  int32_t code = 0;
226,670✔
4917
  
4918
  while (true) {
896,048✔
4919
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,122,718✔
4920
    if (NULL == pIter) {
1,122,718✔
4921
      break;
226,670✔
4922
    }
4923

4924
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
896,048✔
4925
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
896,048✔
4926
      continue;
799,249✔
4927
    }
4928
    
4929
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
96,799✔
4930

4931
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
96,799✔
4932
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
1,703✔
4933
      if (NULL == pVgroup) {
1,703✔
4934
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
1,703✔
4935
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
1,703✔
4936
        if (code) {
1,703✔
4937
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4938
        }
4939
        continue;
1,703✔
4940
      }
4941
      mndReleaseVgroup(pMnode, pVgroup);
×
4942
      
4943
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4944
      
4945
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
4946
      continue;
×
4947
    }
4948

4949
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
95,096✔
4950

4951
    msmCheckVgroupStreamStatus(pVg->streamTasks);
95,096✔
4952
  }
4953
}
226,670✔
4954

4955
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
645✔
4956
  *deployNum = 0;
645✔
4957
  
4958
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,720✔
4959
    if (pStream->runners[i]) {
1,505✔
4960
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
645✔
4961
      for (int32_t t = 0; t < taskNum; ++t) {
860✔
4962
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
645✔
4963
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
645✔
4964
        if (stopped) {
645✔
4965
          mstsDebug("stream already stopped %d, ignore it", stopped);
430✔
4966
          *deployNum = 0;
430✔
4967
          return;
430✔
4968
        }
4969

4970
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
215✔
4971
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
215✔
4972
      }
4973
      
4974
      deployId[*deployNum] = i;
215✔
4975
      (*deployNum)++;
215✔
4976
    }
4977
  }
4978
}
4979

4980
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
2,285✔
4981
  pSnode->runnerThreadNum = -1;
2,285✔
4982

4983
  (void)msmSTAddSnodesToMap(pMnode);
2,285✔
4984

4985
  int64_t streamId = 0;
2,285✔
4986
  void* pIter = NULL;
2,285✔
4987
  SStmSnodeStreamStatus* pStream = NULL;
2,285✔
4988
  int32_t deployNum = 0;
2,285✔
4989
  SStmTaskAction task = {0};
2,285✔
4990
  
4991
  while (true) {
4992
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
3,731✔
4993
    if (NULL == pIter) {
3,731✔
4994
      break;
2,285✔
4995
    }
4996

4997
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,446✔
4998
    
4999
    task.streamId = streamId;
1,446✔
5000
    
5001
    pStream = (SStmSnodeStreamStatus*)pIter;
1,446✔
5002
    if (pStream->trigger) {
1,446✔
5003
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
801✔
5004
      if (stopped) {
801✔
5005
        mstsDebug("stream already stopped %d, ignore it", stopped);
645✔
5006
        continue;
645✔
5007
      }
5008

5009
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
156✔
5010
      
5011
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
156✔
5012
    } else {
5013
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
645✔
5014
      
5015
      if (task.deployNum > 0) {
645✔
5016
        //task.triggerStatus = pStream->trigger;
5017
        task.multiRunner = true;
215✔
5018
        task.type = STREAM_RUNNER_TASK;
215✔
5019
        
5020
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
215✔
5021
        
5022
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
215✔
5023
      }
5024
    }
5025
  }
5026

5027
  taosHashClear(pSnode->streamTasks);
2,285✔
5028
}
2,285✔
5029

5030

5031
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
30,648✔
5032
  void* pIter = NULL;
30,648✔
5033
  SStmSnodeStreamStatus* pSnode = NULL;
30,648✔
5034
  int64_t streamId = 0;
30,648✔
5035
  
5036
  while (true) {
5037
    pIter = taosHashIterate(pStreams, pIter);
319,060✔
5038
    if (NULL == pIter) {
319,060✔
5039
      break;
30,648✔
5040
    }
5041

5042
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
288,412✔
5043
    pSnode = (SStmSnodeStreamStatus*)pIter;
288,412✔
5044

5045
    if (NULL != pSnode->trigger) {
288,412✔
5046
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
276,346✔
5047
    }
5048

5049
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,153,648✔
5050
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
865,236✔
5051
      if (taskNum > 0) {
865,236✔
5052
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
804,614✔
5053
      }
5054
    }
5055
  }
5056
}
30,648✔
5057

5058

5059
void msmCheckSnodeStatus(SMnode *pMnode) {
226,670✔
5060
  void* pIter = NULL;
226,670✔
5061
  
5062
  while (true) {
297,143✔
5063
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
523,813✔
5064
    if (NULL == pIter) {
523,813✔
5065
      break;
226,670✔
5066
    }
5067

5068
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
297,143✔
5069
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
297,143✔
5070
      continue;
261,268✔
5071
    }
5072

5073
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
35,875✔
5074
    
5075
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
35,875✔
5076
    if (NULL == pSnode->streamTasks) {
35,875✔
5077
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
3,528✔
5078
      continue;
3,528✔
5079
    }
5080
    
5081
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
32,347✔
5082
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,699✔
5083
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5084
      
5085
      msmHandleSnodeLost(pMnode, pSnode);
1,699✔
5086
      continue;
1,699✔
5087
    }
5088
    
5089
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
30,648✔
5090

5091
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
30,648✔
5092
  }
5093
}
226,670✔
5094

5095

5096
void msmCheckTasksStatus(SMnode *pMnode) {
226,670✔
5097
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
226,670✔
5098

5099
  msmCheckVgroupStatus(pMnode);
226,670✔
5100
  msmCheckSnodeStatus(pMnode);
226,670✔
5101
}
226,670✔
5102

5103
void msmCheckSnodesState(SMnode *pMnode) {
226,670✔
5104
  if (!MST_READY_FOR_SNODE_LOOP()) {
226,670✔
5105
    return;
200,587✔
5106
  }
5107

5108
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
26,083✔
5109

5110
  void* pIter = NULL;
26,083✔
5111
  int32_t snodeId = 0;
26,083✔
5112
  while (true) {
29,637✔
5113
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
55,720✔
5114
    if (NULL == pIter) {
55,720✔
5115
      break;
26,083✔
5116
    }
5117

5118
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
29,637✔
5119
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
29,637✔
5120
      continue;
29,051✔
5121
    }
5122

5123
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
586✔
5124
    if (NULL == pSnode->streamTasks) {
586✔
5125
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5126
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5127
      continue;
×
5128
    }
5129
    
5130
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
586✔
5131
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5132
    
5133
    msmHandleSnodeLost(pMnode, pSnode);
586✔
5134
  }
5135

5136
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
26,083✔
5137
}
5138

5139
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
7,480,436✔
5140
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
7,480,436✔
5141
  if (0 == active || MND_STM_STATE_NORMAL != state) {
7,480,436✔
5142
    mstTrace("ignore health check since active:%d state:%d", active, state);
1,860✔
5143
    return false;
1,860✔
5144
  }
5145

5146
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,478,576✔
5147
    mstTrace("ignore health check since no stream now");
7,025,236✔
5148
    return false;
7,025,236✔
5149
  }
5150

5151
  return true;
453,340✔
5152
}
5153

5154
void msmHealthCheck(SMnode *pMnode) {
7,253,766✔
5155
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,253,766✔
5156
    return;
7,027,096✔
5157
  }
5158

5159
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
426,060✔
5160
  
5161
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
226,670✔
5162
  if (!msmCheckNeedHealthCheck(pMnode)) {
226,670✔
5163
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5164
    return;
×
5165
  }
5166
  
5167
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
226,670✔
5168
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
226,670✔
5169

5170
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
226,670✔
5171
  
5172
  msmCheckStreamsStatus(pMnode);
226,670✔
5173
  msmCheckTasksStatus(pMnode);
226,670✔
5174
  msmCheckSnodesState(pMnode);
226,670✔
5175

5176
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
226,670✔
5177

5178
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
226,670✔
5179
}
5180

5181
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
782✔
5182
  SStreamObj *pStream = pObj;
782✔
5183
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
782✔
5184
    return true;
×
5185
  }
5186
  
5187
  pStream->updateTime = *(int64_t*)p1;
782✔
5188
  
5189
  (*(int32_t*)p2)++;
782✔
5190
  
5191
  return true;
782✔
5192
}
5193

5194
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5195
  int32_t code = 0;
×
5196
  int8_t  stopped = 0;
×
5197
  
5198
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5199
  
5200
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5201
  if (NULL == pStatus) {
×
5202
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5203
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5204
    goto _exit;
×
5205
  }
5206

5207
  stopped = atomic_load_8(&pStatus->stopped);
×
5208
  if (stopped) {
×
5209
    mstsError("stream already stopped, stopped:%d", stopped);
×
5210
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5211
    goto _exit;
×
5212
  }
5213

5214
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5215
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5216
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5217
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5218
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5219
    goto _exit;
×
5220
  }
5221

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

5225
_exit:
×
5226
  
5227
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5228

5229
  return code;
×
5230
}
5231

5232
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
355,694✔
5233
  int32_t code = TSDB_CODE_SUCCESS;
355,694✔
5234
  int32_t lino = 0;
355,694✔
5235
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
355,694✔
5236
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
355,694✔
5237
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
355,694✔
5238

5239
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
711,322✔
5240

5241
  mStreamMgmt.stat.activeTimes++;
355,694✔
5242
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
355,694✔
5243
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
355,694✔
5244
  if (NULL == mStreamMgmt.tCtx) {
355,694✔
5245
    code = terrno;
×
5246
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5247
    goto _exit;
×
5248
  }
5249

5250
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
355,694✔
5251
  if (mStreamMgmt.actionQ == NULL) {
355,694✔
5252
    code = terrno;
×
5253
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5254
    goto _exit;
×
5255
  }
5256
  
5257
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
355,694✔
5258
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
355,694✔
5259
  
5260
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
355,694✔
5261
  
5262
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,133,991✔
5263
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,778,297✔
5264

5265
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,669,782✔
5266
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
8,891,485✔
5267
      if (pCtx->deployStm[m] == NULL) {
8,891,485✔
5268
        code = terrno;
×
5269
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5270
        goto _exit;
×
5271
      }
5272
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
8,891,485✔
5273
      
5274
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
8,891,485✔
5275
      if (pCtx->actionStm[m] == NULL) {
8,891,485✔
5276
        code = terrno;
×
5277
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5278
        goto _exit;
×
5279
      }
5280
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
8,891,485✔
5281
    }
5282
  }
5283
  
5284
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
355,694✔
5285
  if (mStreamMgmt.streamMap == NULL) {
355,694✔
5286
    code = terrno;
×
5287
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5288
    goto _exit;
×
5289
  }
5290
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
355,694✔
5291
  
5292
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
355,694✔
5293
  if (mStreamMgmt.taskMap == NULL) {
355,694✔
5294
    code = terrno;
×
5295
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5296
    goto _exit;
×
5297
  }
5298
  
5299
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
355,694✔
5300
  if (mStreamMgmt.vgroupMap == NULL) {
355,694✔
5301
    code = terrno;
×
5302
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5303
    goto _exit;
×
5304
  }
5305
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
355,694✔
5306

5307
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
355,694✔
5308
  if (mStreamMgmt.snodeMap == NULL) {
355,694✔
5309
    code = terrno;
×
5310
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5311
    goto _exit;
×
5312
  }
5313
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
355,694✔
5314
  
5315
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
355,694✔
5316
  if (mStreamMgmt.dnodeMap == NULL) {
355,694✔
5317
    code = terrno;
×
5318
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5319
    goto _exit;
×
5320
  }
5321

5322
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
355,694✔
5323
  if (mStreamMgmt.toDeployVgMap == NULL) {
355,694✔
5324
    code = terrno;
×
5325
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5326
    goto _exit;
×
5327
  }
5328
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
355,694✔
5329
  
5330
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
355,694✔
5331
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
355,694✔
5332
    code = terrno;
×
5333
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5334
    goto _exit;
×
5335
  }
5336
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
355,694✔
5337

5338
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
355,694✔
5339
  if (mStreamMgmt.toUpdateScanMap == NULL) {
355,694✔
5340
    code = terrno;
×
5341
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5342
    goto _exit;
×
5343
  }
5344
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
355,694✔
5345

5346
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
355,694✔
5347
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
355,694✔
5348

5349
  mStreamMgmt.lastTaskId = 1;
355,694✔
5350

5351
  int32_t activeStreamNum = 0;
355,694✔
5352
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
355,694✔
5353

5354
  if (activeStreamNum > 0) {
355,694✔
5355
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
620✔
5356
  } else {
5357
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
355,074✔
5358
  }
5359

5360
_exit:
355,694✔
5361

5362
  if (code) {
355,694✔
5363
    msmDestroyRuntimeInfo(pMnode);
×
5364
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5365
  } else {
5366
    mstInfo("mnode stream runtime init done");
355,694✔
5367
  }
5368

5369
  return code;
355,694✔
5370
}
5371

5372

5373

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