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

taosdata / TDengine / #4808

16 Oct 2025 11:40AM UTC coverage: 57.938% (-0.6%) from 58.524%
#4808

push

travis-ci

web-flow
fix(tref): increase TSDB_REF_OBJECTS from 100 to 2000 for improved reference handling (#33281)

137662 of 303532 branches covered (45.35%)

Branch coverage included in aggregate %.

209234 of 295200 relevant lines covered (70.88%)

4035326.15 hits per line

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

60.37
/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() {
2,242✔
34
  SStmQNode* pQNode = NULL;
2,242✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
2,242✔
37
    return;
1,121✔
38
  }
39

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

43
  taosMemoryFreeClear(mStreamMgmt.actionQ->head);
1,121!
44
  taosMemoryFreeClear(mStreamMgmt.actionQ);
1,121!
45
}
46

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
5,532✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
33,192✔
49
    taosHashCleanup(pCtx->deployStm[m]);
27,660✔
50
    taosHashCleanup(pCtx->actionStm[m]);
27,660✔
51
  }
52
}
5,532✔
53

54
void msmDestroyThreadCtxs() {
2,242✔
55
  if (NULL == mStreamMgmt.tCtx) {
2,242✔
56
    return;
1,121✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
6,653✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
5,532✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
1,121!
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
2,242✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
2,242!
94
}
2,242✔
95

96

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

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

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

111
    pStatus = pStream;
7✔
112
  }
113

114
  int8_t stopped = atomic_load_8(&pStatus->stopped);
7✔
115
  if (stopped) {
7!
116
    mstsDebug("stream already stopped %d, ignore stop", stopped);
×
117
    goto _exit;
×
118
  }
119

120
  if (pStatus->triggerTask && pStatus->triggerTask->runningStartTs && (currTs - pStatus->triggerTask->runningStartTs) > 2 * MST_ISOLATION_DURATION) {
7!
121
    pStatus->fatalRetryTimes = 0;
2✔
122
    mstsDebug("reset stream retryTimes, running duation:%" PRId64 "ms", currTs - pStatus->triggerTask->runningStartTs);
2!
123
  }
124

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

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

136
_exit:
1✔
137

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

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

145

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

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

200
  pSnode = NULL;
1,192✔
201

202
_exit:
1,192✔
203

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

208
  return code;
1,192✔
209
}
210

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

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

235
  pDnode = NULL;
2,374✔
236

237
_exit:
2,374✔
238

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

243
  return code;
2,374✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
1,109✔
275
  int32_t code = TSDB_CODE_SUCCESS;
1,109✔
276
  int32_t lino = 0;
1,109✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
1,109✔
278
  if (NULL == pStream) {
1,109✔
279
    SStmVgStreamStatus stream = {0};
868✔
280
    if (trigReader) {
868✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
586✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
586!
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
1,172!
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
282✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
282!
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
564!
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
868!
290
    goto _exit;
868✔
291
  }
292
  
293
  if (trigReader) {
241✔
294
    if (NULL == pStream->trigReaders) {
33!
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
33✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
33!
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
66!
300
    goto _exit;
33✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
208✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
153✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
153!
306
  }
307

308
  TSDB_CHECK_NULL(taosArrayPush(pStream->calcReaders, &pStatus), code, lino, _exit, terrno);
416!
309

310
_exit:
208✔
311

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

320
  return code;
1,109✔
321
}
322

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

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

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

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

350
  return code;
1,109✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
1,117✔
363
    if (NULL == pVg) {
1,117✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
291✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
291!
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
582!
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
291✔
368
      if (TSDB_CODE_SUCCESS == code) {
291!
369
        goto _return;
291✔
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);
826✔
381
    if (NULL == pVg->taskList) {
826!
382
      pVg->taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
×
383
      TSDB_CHECK_NULL(pVg->taskList, code, lino, _return, terrno);
×
384
    }
385
    if (NULL == taosArrayPush(pVg->taskList, &ext)) {
1,652!
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
826✔
390
    
391
    taosHashRelease(pVgMap, pVg);
826✔
392
    break;
826✔
393
  }
394
  
395
_return:
1,117✔
396

397
  if (code) {
1,117!
398
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
399
  } else {
400
    int32_t num = atomic_add_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
1,117✔
401
    msttDebug("task added to toDeployVgTaskNum, vgToDeployTaskNum:%d", num);
1,117✔
402
  }
403

404
  return code;
1,117✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
1,683✔
409
  int32_t code = TSDB_CODE_SUCCESS;
1,683✔
410
  int32_t lino = 0;
1,683✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
1,683✔
412
  if (NULL == pStream) {
1,683✔
413
    SStmSnodeStreamStatus stream = {0};
435✔
414
    if (deployId < 0) {
435✔
415
      stream.trigger = pStatus;
5✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
430✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
430!
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
860!
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
435!
423
    goto _exit;
435✔
424
  }
425
  
426
  if (deployId < 0) {
1,248✔
427
    if (NULL != pStream->trigger) {
403!
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;
403✔
433
    goto _exit;
403✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
845✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
779✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
779!
439
  }
440

441
  TSDB_CHECK_NULL(taosArrayPush(pStream->runners[deployId], &pStatus), code, lino, _exit, terrno);
1,690!
442

443
_exit:
845✔
444

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

453
  return code;
1,683✔
454
}
455

456

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

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

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

484
  return code;
1,683✔
485
}
486

487

488

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

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

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

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

544
  if (code) {
408!
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);
408✔
548
  }
549

550
  return code;
408✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
102✔
568
      ext.deployed = false;
102✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
204!
570

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

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

614
  return code;
1,275✔
615
}
616

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

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

632
_exit:
1,209✔
633

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

638
  return code;
1,209✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
27✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
×
682

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

687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
219✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
29,257✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
103,801✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
74,544✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
74,544✔
725
  }
726

727
_exit:
29,257✔
728

729
  if (code) {
29,257!
730
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
731
  }
732

733
  return code;
29,257✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
752✔
739
  int32_t num = taosArrayGetSize(pList);
752✔
740
  for (int32_t i = 0; i < num; ++i) {
1,771✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
1,455✔
742
    if (pScan->readFromCache) {
1,455✔
743
      return pScan->scanPlan;
436✔
744
    }
745
  }
746

747
  return NULL;
316✔
748
}
749

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

773
  return TSDB_CODE_SUCCESS;
1,117✔
774
}
775

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

780
  if (pInfo->runnerDeploys > 0) {
403!
781
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
403✔
782
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
403!
783
  }
784
  
785
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
1,612✔
786
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
1,209✔
787
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
1,209!
788

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

804
_exit:
403✔
805

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

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

825
  mndReleaseSnode(pMnode, pSnode);
×
826

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

832
  return TSDB_CODE_SUCCESS;
×
833
}
834

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

852
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
408✔
853
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
408✔
854
  pMsg->addOptions = pStream->pCreate->addOptions;
408✔
855
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
408✔
856

857
  pMsg->maxDelay = pStream->pCreate->maxDelay;
408✔
858
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
408✔
859
  pMsg->watermark = pStream->pCreate->watermark;
408✔
860
  pMsg->expiredTime = pStream->pCreate->expiredTime;
408✔
861
  pMsg->trigger = pInfo->pCreate->trigger;
408✔
862

863
  pMsg->eventTypes = pStream->pCreate->eventTypes;
408✔
864
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
408✔
865
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
408✔
866
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
408✔
867
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
408✔
868
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
408✔
869
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
129✔
870
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
129✔
871
  }
872

873
  SStreamTaskAddr addr;
874
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
408✔
875
  if (triggerReaderNum > 0) {
408✔
876
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
406✔
877
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
406!
878
  }
879
  
880
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
927✔
881
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
519✔
882
    addr.taskId = pStatus->id.taskId;
519✔
883
    addr.nodeId = pStatus->id.nodeId;
519✔
884
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
519✔
885
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
1,038!
886
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
519✔
887
  }
888

889
  pMsg->leaderSnodeId = pStream->mainSnodeId;
408✔
890
  pMsg->streamName = pInfo->streamName;
408✔
891

892
  if (0 == pInfo->runnerNum) {
408✔
893
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
5!
894
    return code;
5✔
895
  }
896

897
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
403!
898

899
_exit:
403✔
900

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

910

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

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

942
_exit:
1,275✔
943

944
  if (code) {
1,275!
945
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
946
  }
947
  
948
  return code;
1,275✔
949
}
950

951

952
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
927✔
953
  int32_t code = TSDB_CODE_SUCCESS;
927✔
954
  int32_t lino = 0;
927✔
955
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
927!
956
  SListNode* pNode = pList ? listHead(pList) : NULL;
927✔
957
  
958
  for (int32_t i = 0; i < taskNum; ++i) {
2,036✔
959
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
1,109✔
960
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
1,109!
961
    if (pNode) {
1,109✔
962
      pNode = TD_DLIST_NODE_NEXT(pNode);
479✔
963
    }
964
  }
965
  
966
_exit:
927✔
967

968
  if (code) {
927!
969
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
970
  }
971
  
972
  return code;
927✔
973
}
974

975

976
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
1,617✔
977
  int32_t code = TSDB_CODE_SUCCESS;
1,617✔
978
  int32_t lino = 0;
1,617✔
979
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
1,617✔
980
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
1,617✔
981
  
982
  for (int32_t i = 0; i < rtaskNum; ++i) {
3,300✔
983
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
1,683✔
984
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
1,683!
985
  }
986
  
987
_exit:
1,617✔
988

989
  if (code) {
1,617!
990
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
991
  }
992

993
  return code;
1,617✔
994
}
995

996
int64_t msmAssignTaskId(void) {
2,792✔
997
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
2,792✔
998
}
999

1000
int64_t msmAssignTaskSeriousId(void) {
2,792✔
1001
  return taosGetTimestampNs();
2,792✔
1002
}
1003

1004

1005
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
2,163✔
1006
  int32_t code = TSDB_CODE_SUCCESS;
2,163✔
1007
  int32_t lino = 0;
2,163✔
1008
  bool     noExists = false;
2,163✔
1009
  SStmSnodeStatus* pStatus = NULL;
2,163✔
1010

1011
  while (true) {
1012
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
2,201✔
1013
    if (NULL == pStatus) {
2,201✔
1014
      if (noExists) {
38!
1015
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1016
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1017
      }
1018

1019
      noExists = true;
38✔
1020
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
38!
1021
      
1022
      continue;
38✔
1023
    }
1024

1025
    *alive = (pStatus->runnerThreadNum >= 0);
2,163✔
1026
    break;
2,163✔
1027
  }
1028

1029
_exit:
2,163✔
1030

1031
  if (code) {
2,163!
1032
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1033
  }
1034

1035
  return code;
2,163✔
1036
}
1037

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

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

1066
      snodeId = pSnode->replicaId;
×
1067
      mndReleaseSnode(pMnode, pSnode);
×
1068
      
1069
      continue;
×
1070
    }
1071

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

1076
_exit:
×
1077

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

1082
  return 0;
×
1083
}
1084

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

1099
  int32_t snodeTarget = taosRand() % snodeNum;
1,252✔
1100

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

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

1126
    snodeId = pObj->id;
1,352✔
1127
    if (snodeIdx == snodeTarget) {
1,352✔
1128
      sdbRelease(pMnode->pSdb, pObj);
1,252✔
1129
      sdbCancelFetch(pMnode->pSdb, pIter);
1,252✔
1130
      pObj = NULL;
1,252✔
1131
      goto _exit;
1,252✔
1132
    }
1133

1134
    sdbRelease(pMnode->pSdb, pObj);
100✔
1135
    snodeIdx++;
100✔
1136
  }
1137

1138
_exit:
1,262✔
1139

1140
  if (code) {
1,262!
1141
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1142
  }
1143

1144
  if (0 == snodeId) {
1,262✔
1145
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
10✔
1146
  }
1147

1148
  return snodeId;
1,262✔
1149
}
1150

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

1160
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
1,617✔
1161

1162
_exit:
1,617✔
1163

1164
  if (0 == snodeId) {
1,617!
1165
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1166
  }
1167

1168
  return snodeId;
1,617✔
1169
}
1170

1171

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

1177
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
408!
1178
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
408!
1179

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

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

1201
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
408!
1202
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
408!
1203

1204
_exit:
408✔
1205

1206
  if (code) {
408!
1207
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1208
  }
1209

1210
  return code;
408✔
1211
}
1212

1213
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
619✔
1214
  int32_t code = TSDB_CODE_SUCCESS;
619✔
1215
  int32_t lino = 0;
619✔
1216

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

1239
_exit:
619✔
1240

1241
  if (code) {
619!
1242
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1243
  }
1244

1245
  return code;
619✔
1246
}
1247

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

1277
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
230✔
1278
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
230!
1279
      
1280
      void *pIter = NULL;
230✔
1281
      while (1) {
1,302✔
1282
        SStmTaskDeploy info = {0};
1,532✔
1283
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
1,532✔
1284
        if (pIter == NULL) {
1,532✔
1285
          break;
230✔
1286
        }
1287
      
1288
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
1,302!
1289
          pState = taosArrayReserve(pInfo->trigReaders, 1);
343✔
1290

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

1300
        sdbRelease(pSdb, pVgroup);
1,302✔
1301
      }
1302
      break;
230✔
1303
    }
1304
    default:
2✔
1305
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
2!
1306
      break;
2✔
1307
  }
1308

1309
_exit:
408✔
1310

1311
  mndReleaseDb(pCtx->pMnode, pDb);
408✔
1312

1313
  if (code) {
408!
1314
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1315
  }
1316

1317
  return code;
408✔
1318
}
1319

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

1343
  key[1] = pSubplan->id.subplanId;
479✔
1344

1345
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
479✔
1346
  if (NULL == ppRes) {
479!
1347
    SArray* pRes = taosArrayInit(1, sizeof(addr));
479✔
1348
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
479!
1349
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
958!
1350
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
479!
1351
  } else {
1352
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1353
  }
1354

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

1361
  nodesDestroyNode((SNode*)pSubplan);
479✔
1362

1363
  if (code) {
479!
1364
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1365
  }
1366

1367
  return code;
479✔
1368
}
1369

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

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

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

1400
  nodesDestroyNode((SNode*)pSubplan);
203✔
1401
  
1402
  if (code) {
203!
1403
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1404
  }
1405

1406
  return code;
203✔
1407
}
1408

1409

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

1414
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
490!
1415

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

1438
_exit:
490✔
1439

1440
  if (code) {
490!
1441
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1442
  }
1443

1444
  return code;
490✔
1445
}
1446

1447

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

1457
  
1458
  for (int32_t i = 0; i < calcTasksNum; ++i) {
1,090✔
1459
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
682✔
1460
    if (pScan->readFromCache) {
682✔
1461
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
203!
1462
      continue;
203✔
1463
    }
1464
    
1465
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
479✔
1466
    for (int32_t m = 0; m < vgNum; ++m) {
958✔
1467
      pState = tdListReserve(pInfo->calcReaders);
479✔
1468
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
479!
1469

1470
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
479!
1471
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
479!
1472
    }
1473
  }
1474

1475
_exit:
408✔
1476

1477
  if (code) {
408!
1478
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1479
  }
1480

1481
  return code;
408✔
1482
}
1483

1484

1485

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

1513
_exit:
×
1514

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

1519
  return code;
×
1520
}
1521

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

1530
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
408!
1531
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
408!
1532
  
1533
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
408!
1534
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
408!
1535
  
1536
_exit:
408✔
1537

1538
  if (code) {
408!
1539
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1540
  }
1541
  
1542
  return code;
408✔
1543
}
1544

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

1556
  source.addr.epSet = pSrc->epset;
2,112✔
1557
  source.addr.nodeId = pSrc->vgId;
2,112✔
1558

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

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

1578
  mstsError("subplanId %d not found in runner list", subplanId);
×
1579

1580
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1581
}
1582

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

1601
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
2,046✔
1602

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

1609
    int32_t childrenNum = taosArrayGetSize(*ppRes);
2,046✔
1610
    for (int32_t i = 0; i < childrenNum; ++i) {
4,092✔
1611
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
2,046✔
1612
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
2,046!
1613
    }
1614
  }
1615

1616
_exit:
1,275✔
1617

1618
  if (code) {
1,275!
1619
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1620
  }
1621

1622
  return code;
1,275✔
1623
}
1624

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

1633
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
1,275!
1634

1635
  SNode* pTmp = NULL;
1,275✔
1636
  WHERE_EACH(pTmp, pPlan->pChildren) {
3,387!
1637
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
2,112✔
1638
      ERASE_NODE(pPlan->pChildren);
2,046✔
1639
      continue;
2,046✔
1640
    }
1641
    WHERE_NEXT;
66✔
1642
  }
1643
  nodesClearList(pPlan->pChildren);
1,275✔
1644
  pPlan->pChildren = NULL;
1,275✔
1645

1646
  if (NULL == pPlan->pParents) {
1,275✔
1647
    goto _exit;
1,209✔
1648
  }
1649

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

1665
  if (code) {
1,275!
1666
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1667
  }
1668

1669
  return code;
1,275✔
1670
}
1671

1672
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
1,209✔
1673
  int32_t code = TSDB_CODE_SUCCESS;
1,209✔
1674
  int32_t lino = 0;
1,209✔
1675
  int64_t streamId = pStream->pCreate->streamId;
1,209✔
1676
  int32_t runnerNum = taosArrayGetSize(pRunners);
1,209✔
1677
  
1678
  for (int32_t i = 0; i < runnerNum; ++i) {
2,484✔
1679
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
1,275✔
1680
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
1,275!
1681
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
1,275!
1682

1683
    SStreamTask* pTask = &pDeploy->task;
1,275✔
1684
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
1,275✔
1685
  }
1686

1687
_exit:
1,209✔
1688

1689
  if (code) {
1,209!
1690
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1691
  }
1692

1693
  return code;
1,209✔
1694
}
1695

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

1709
  if (pDag->numOfSubplans <= 0) {
403!
1710
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1711
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1712
  }
1713

1714
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
403!
1715
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1716
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1717
  }
1718

1719
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
403!
1720
  if (levelNum <= 0) {
403!
1721
    mstsError("invalid level num:%d", levelNum);
×
1722
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1723
  }
1724

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

1739
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
403✔
1740
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
403!
1741
  
1742
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
1,612✔
1743
    totalTaskNum = 0;
1,209✔
1744

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

1751
    taskIdx = 0;
1,209✔
1752
    
1753
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
2,451✔
1754
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
1,242✔
1755
      if (NULL == plans) {
1,242!
1756
        mstsError("empty level plan, level:%d", i);
×
1757
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1758
      }
1759

1760
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
1,242!
1761
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1762
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1763
      }
1764

1765
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
1,242!
1766
      if (taskNum <= 0) {
1,242!
1767
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1768
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1769
      }
1770

1771
      totalTaskNum += taskNum;
1,242✔
1772
      if (totalTaskNum > pDag->numOfSubplans) {
1,242!
1773
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1774
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1775
      }
1776

1777
      for (int32_t n = 0; n < taskNum; ++n) {
2,517✔
1778
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
1,275✔
1779
        pState = taosArrayReserve(deployList, 1);
1,275✔
1780

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

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

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

1808
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
1,242✔
1809
    }
1810

1811
    if (totalTaskNum != pDag->numOfSubplans) {
1,209!
1812
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1813
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1814
    }
1815

1816
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
1,209!
1817

1818
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
1,209!
1819

1820
    nodesDestroyNode((SNode *)pDag);
1,209✔
1821
    pDag = NULL;
1,209✔
1822
    
1823
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
1,209!
1824

1825
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
1,209✔
1826
  }
1827

1828
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
1,612✔
1829
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
1,209!
1830
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
1,209!
1831
  }
1832
  
1833
  pInfo->runnerNum = totalTaskNum;
403✔
1834
  
1835
_exit:
403✔
1836

1837
  if (code) {
403!
1838
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1839
  }
1840

1841
  taosArrayDestroy(deployTaskList);
403✔
1842
  nodesDestroyNode((SNode *)pDag);
403✔
1843

1844
  return code;
403✔
1845
}
1846

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

1864
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
×
1865
    deployId = pAction->deployId[r];
×
1866

1867
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
×
1868

1869
    pStartRunner = pRunner;
×
1870
    totalTaskNum = 0;
×
1871

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

1877
    taskIdx = 0;
×
1878
    
1879
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
×
1880
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
×
1881
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
×
1882
      totalTaskNum += taskNum;
×
1883

1884
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
×
1885
      
1886
      for (int32_t n = 0; n < taskNum; ++n) {
×
1887
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
×
1888

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

1895
        pRunner->id.nodeId = newNodeId;
×
1896

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

1907
        pRunner++;
×
1908
      }
1909

1910
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
×
1911
    }
1912

1913
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
×
1914

1915
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
×
1916

1917
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
×
1918

1919
    nodesDestroyNode((SNode *)pDag);
×
1920
    pDag = NULL;
×
1921

1922
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
×
1923
  }
1924

1925
_exit:
×
1926

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

1931
  nodesDestroyNode((SNode *)pDag);
×
1932
  taosArrayDestroy(deployTaskList);
×
1933

1934
  return code;
×
1935
}
1936

1937

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

1946
_exit:
385✔
1947

1948
  if (code) {
385!
1949
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1950
  }
1951

1952
  return code;
385✔
1953
}
1954

1955

1956
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
408✔
1957
  if (NULL == pStream->pCreate->calcPlan) {
408✔
1958
    return TSDB_CODE_SUCCESS;
5✔
1959
  }
1960
  
1961
  int32_t code = TSDB_CODE_SUCCESS;
403✔
1962
  int32_t lino = 0;
403✔
1963
  int64_t streamId = pStream->pCreate->streamId;
403✔
1964
  SQueryPlan* pPlan = NULL;
403✔
1965

1966
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
403!
1967

1968
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
1,612✔
1969
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
1,209✔
1970
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
1,209!
1971
  }
1972

1973
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
403✔
1974
  pPlan = NULL;
403✔
1975
  
1976
  TAOS_CHECK_EXIT(code);
403!
1977

1978
  taosHashClear(mStreamMgmt.toUpdateScanMap);
403✔
1979
  mStreamMgmt.toUpdateScanNum = 0;
403✔
1980

1981
_exit:
403✔
1982

1983
  nodesDestroyNode((SNode *)pPlan);
403✔
1984

1985
  if (code) {
403!
1986
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1987
  }
1988

1989
  return code;
403✔
1990
}
1991

1992

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

1998
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
408!
1999

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

2006
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
408!
2007
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
408!
2008
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
408!
2009
  
2010
_exit:
408✔
2011

2012
  if (code) {
408!
2013
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2014
  }
2015

2016
  return code;
408✔
2017
}
2018

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

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

2058
_exit:
×
2059

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

2065
  return code;
×
2066
}
2067

2068

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

2074
  pStatus->lastActionTs = INT64_MIN;
390✔
2075

2076
  if (NULL == pStatus->streamName) {
390!
2077
    pStatus->streamName = taosStrdup(pStream->name);
390!
2078
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
390!
2079
  }
2080

2081
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
390!
2082
  
2083
  if (pStream->pCreate->numOfCalcSubplan > 0) {
390✔
2084
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
385✔
2085
    
2086
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
385!
2087
  }
2088

2089
  if (initList) {
390!
2090
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2091

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

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

2109
  if (code) {
390!
2110
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2111
  }
2112

2113
  return code;
390✔
2114
}
2115

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

2122
  if (NULL == pStatus) {
408✔
2123
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
390!
2124

2125
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
390!
2126

2127
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
390✔
2128
  }
2129
  
2130
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
408!
2131

2132
  mstLogSStmStatus("stream deployed", streamId, pStatus);
408✔
2133

2134
_exit:
408✔
2135

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

2145
  return code;
408✔
2146
}
2147

2148

2149
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
20✔
2150
  int32_t code = TSDB_CODE_SUCCESS;
20✔
2151
  void* pIter = NULL;
20✔
2152

2153
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
30✔
2154
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
10✔
2155
    (void)mstWaitLock(&pVg->lock, true);
10✔
2156

2157
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
10✔
2158
    if (atomic_load_32(&pVg->deployed) == taskNum) {
10!
2159
      taosRUnLockLatch(&pVg->lock);
×
2160
      continue;
×
2161
    }
2162

2163
    for (int32_t i = 0; i < taskNum; ++i) {
55✔
2164
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
45✔
2165
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
45✔
2166
        continue;
39✔
2167
      }
2168

2169
      mstDestroySStmTaskToDeployExt(pExt);
6✔
2170
      pExt->deployed = true;
6✔
2171
    }
2172
    
2173
    taosRUnLockLatch(&pVg->lock);
10✔
2174
  }
2175

2176
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
43✔
2177
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
23✔
2178
    (void)mstWaitLock(&pSnode->lock, true);
23✔
2179

2180
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
23✔
2181
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
23✔
2182
      for (int32_t i = 0; i < taskNum; ++i) {
39✔
2183
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
23✔
2184
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
23✔
2185
          continue;
20✔
2186
        }
2187
        
2188
        mstDestroySStmTaskToDeployExt(pExt);
3✔
2189
        pExt->deployed = true;
3✔
2190
      }
2191
    }
2192

2193
    taskNum = taosArrayGetSize(pSnode->runnerList);
23✔
2194
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
23!
2195
      for (int32_t i = 0; i < taskNum; ++i) {
92✔
2196
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
69✔
2197
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
69✔
2198
          continue;
60✔
2199
        }
2200
        
2201
        mstDestroySStmTaskToDeployExt(pExt);
9✔
2202
        pExt->deployed = true;
9✔
2203
      }
2204
    }
2205

2206
    taosRUnLockLatch(&pSnode->lock);
23✔
2207
  }
2208

2209
  
2210
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
76✔
2211
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
56✔
2212
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
56✔
2213
    if (TSDB_CODE_SUCCESS == code) {
56✔
2214
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
31✔
2215
    }
2216
  }
2217

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

2226
  size_t keyLen = 0;
20✔
2227
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
1,020✔
2228
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
1,000✔
2229
    if (*pStreamId == streamId) {
1,000✔
2230
      int64_t taskId = *(pStreamId + 1);
129✔
2231
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
129✔
2232
      if (code) {
129!
2233
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2234
      } else {
2235
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
129✔
2236
      }
2237
    }
2238
  }
2239

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

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

2257
  mstResetSStmStatus(pStatus);
18✔
2258

2259
  pStatus->deployTimes++;
18✔
2260
}
18✔
2261

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

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

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

2295
  TAOS_CHECK_EXIT(code);
408!
2296

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

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

2313
_exit:
408✔
2314

2315
  mndReleaseStream(pCtx->pMnode, pStream);
408✔
2316

2317
  if (code) {
408!
2318
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2319
  }
2320

2321
  return code;
408✔
2322
}
2323

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

2356
_exit:
8✔
2357

2358
  if (code) {
8!
2359
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2360
  }
2361

2362
  return code;
8✔
2363
}
2364

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

2396
_exit:
2397

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

2402
  return code;
2403
}
2404
*/
2405

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

2455
_exit:
×
2456

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

2461
  return code;
×
2462
}
2463

2464

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

2472
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
8!
2473

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

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

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

2492
  TAOS_CHECK_EXIT(code);
8!
2493

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

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

2524
_exit:
8✔
2525

2526
  if (pStream) {
8!
2527
    mndReleaseStream(pCtx->pMnode, pStream);
8✔
2528
  }
2529

2530
  if (code) {
8!
2531
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2532
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2533
  }
2534

2535
  return code;
8✔
2536
}
2537

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

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

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

2583
  return TSDB_CODE_SUCCESS;
×
2584
}
2585

2586
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
2✔
2587
  int32_t code = TSDB_CODE_SUCCESS;
2✔
2588
  int32_t lino = 0;
2✔
2589

2590
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
2!
2591

2592
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
2!
2593

2594
_exit:
2✔
2595

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

2602
  return code;
2✔
2603
}
2604

2605
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
41✔
2606
  int32_t code = TSDB_CODE_SUCCESS;
41✔
2607
  int32_t lino = 0;
41✔
2608

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

2615
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
41✔
2616
  if (NULL == pStream) {
41✔
2617
    mstsInfo("stream %s already not in streamMap", streamName);
2!
2618
    goto _exit;
2✔
2619
  }
2620

2621
  atomic_store_8(&pStream->stopped, 2);
39✔
2622

2623
  mstsInfo("set stream %s stopped by user", streamName);
39!
2624

2625
_exit:
×
2626

2627
  taosHashRelease(mStreamMgmt.streamMap, pStream);
41✔
2628

2629
  if (code) {
41!
2630
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2631
  }
2632

2633
  return;
41✔
2634
}
2635

2636
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
17✔
2637
  int32_t code = TSDB_CODE_SUCCESS;
17✔
2638
  int32_t lino = 0;
17✔
2639

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

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

2653
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
17!
2654

2655
_exit:
17✔
2656

2657
  taosHashRelease(mStreamMgmt.streamMap, pStream);
17✔
2658

2659
  if (code) {
17!
2660
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2661
  }
2662

2663
  return code;
17✔
2664
}
2665

2666
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
90✔
2667
  int32_t code = TSDB_CODE_SUCCESS;
90✔
2668
  int32_t lino = 0;
90✔
2669
  SStmQNode* pQNode = NULL;
90✔
2670

2671
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
507✔
2672
    switch (pQNode->type) {
417!
2673
      case STREAM_ACT_DEPLOY:
417✔
2674
        if (pQNode->streamAct) {
417✔
2675
          mstDebug("start to handle stream deploy action");
409✔
2676
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
409!
2677
        } else {
2678
          mstDebug("start to handle task deploy action");
8!
2679
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
8!
2680
        }
2681
        break;
417✔
2682
      default:
×
2683
        break;
×
2684
    }
2685
  }
2686

2687
_exit:
90✔
2688

2689
  if (code) {
90!
2690
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2691
  }
2692
}
90✔
2693

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

2705
    pStatus = (SStmStatus*)pIter;
×
2706

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

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

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

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

2722
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2723

2724
  msmStopAllStreamsByGrant(errCode);
×
2725

2726
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2727
  
2728
  return errCode;
×
2729
}
2730

2731
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
2,782✔
2732
  int32_t code = TSDB_CODE_SUCCESS;
2,782✔
2733
  int32_t lino = 0;
2,782✔
2734
  int64_t streamId = pDeploy->task.streamId;
2,782✔
2735
  
2736
  switch (pDeploy->task.type) {
2,782!
2737
    case STREAM_READER_TASK:
1,111✔
2738
      if (NULL == pStream->readerTasks) {
1,111✔
2739
        pStream->streamId = streamId;
509✔
2740
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
509✔
2741
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
509!
2742
      }
2743
      
2744
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
2,222!
2745
      break;
1,111✔
2746
    case STREAM_TRIGGER_TASK:
405✔
2747
      pStream->streamId = streamId;
405✔
2748
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
405!
2749
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
405!
2750
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
405✔
2751
      break;
405✔
2752
    case STREAM_RUNNER_TASK:
1,266✔
2753
      if (NULL == pStream->runnerTasks) {
1,266✔
2754
        pStream->streamId = streamId;
425✔
2755
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
425✔
2756
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
425!
2757
      }      
2758
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
2,532!
2759
      break;
1,266✔
2760
    default:
×
2761
      break;
×
2762
  }
2763

2764
_exit:
2,782✔
2765

2766
  if (code) {
2,782!
2767
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2768
  }
2769

2770
  return code;
2,782✔
2771
}
2772

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

2790
      if (TSDB_CODE_DUP_KEY != code) {
×
2791
        goto _exit;
×
2792
      }    
2793

2794
      tFreeSStmStreamDeploy(&streamDeploy);
×
2795
      continue;
×
2796
    }
2797

2798
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
2,236!
2799
    
2800
    break;
2,236✔
2801
  }
2802
  
2803
_exit:
2,782✔
2804

2805
  taosHashRelease(pHash, pStream);
2,782✔
2806

2807
  if (code) {
2,782!
2808
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2809
  } else {
2810
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
2,782✔
2811
  }
2812

2813
  return code;
2,782✔
2814
}
2815

2816

2817
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
490✔
2818
  int32_t code = TSDB_CODE_SUCCESS;
490✔
2819
  int32_t lino = 0;
490✔
2820
  int32_t taskNum = taosArrayGetSize(pTasks);
490✔
2821

2822
  for (int32_t i = 0; i < taskNum; ++i) {
3,290✔
2823
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
2,800✔
2824
    if (pExt->deployed) {
2,800✔
2825
      continue;
18✔
2826
    }
2827

2828
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
2,782!
2829
    pExt->deployed = true;
2,782✔
2830

2831
    (void)atomic_add_fetch_32(deployed, 1);
2,782✔
2832
  }
2833

2834
_exit:
490✔
2835

2836
  if (code) {
490!
2837
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2838
  }
2839

2840
  return code;
490✔
2841
}
2842

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

2850
  mstDebug("start to add stream vgroup tasks deploy");
125✔
2851
  
2852
  for (int32_t i = 0; i < vgNum; ++i) {
826✔
2853
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
701✔
2854

2855
    msmUpdateVgroupUpTs(pCtx, *vgId);
701✔
2856

2857
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
701✔
2858
    if (NULL == pVg) {
701✔
2859
      continue;
410✔
2860
    }
2861

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

2875
_exit:
125✔
2876

2877
  if (code) {
125!
2878
    if (pVg) {
×
2879
      taosRUnLockLatch(&pVg->lock);
×
2880
    }
2881

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

2885
  return code;
125✔
2886
}
2887

2888
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
108✔
2889
  int32_t code = TSDB_CODE_SUCCESS;
108✔
2890
  int32_t lino = 0;
108✔
2891
  SStmSnodeTasksDeploy* pSnode = NULL;
108✔
2892
  SStreamHbMsg* pReq = pCtx->pReq;
108✔
2893

2894
  mstDebug("start to add stream snode tasks deploy");
108✔
2895
  
2896
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
108✔
2897
  if (NULL == pSnode) {
108✔
2898
    return TSDB_CODE_SUCCESS;
4✔
2899
  }
2900

2901
  (void)mstWaitLock(&pSnode->lock, false);
104✔
2902
  
2903
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
104✔
2904
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
96!
2905
  }
2906

2907
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
104✔
2908
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
103!
2909
  }
2910
  
2911
  taosWUnLockLatch(&pSnode->lock);
104✔
2912

2913
_exit:
104✔
2914

2915
  if (code) {
104!
2916
    if (pSnode) {
×
2917
      taosWUnLockLatch(&pSnode->lock);
×
2918
    }
2919

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

2923
  return code;
104✔
2924
}
2925

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

2937
_exit:
1,212✔
2938

2939
  if (code) {
1,212!
2940
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2941
  }
2942

2943
  return code;
1,212✔
2944
}
2945

2946
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
130✔
2947
  int32_t code = TSDB_CODE_SUCCESS;
130✔
2948
  int32_t lino = 0;
130✔
2949
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
130✔
2950
  void* pIter = NULL;
130✔
2951

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

2957
  while (1) {
546✔
2958
    pIter = taosHashIterate(pCtx->deployStm, pIter);
676✔
2959
    if (pIter == NULL) {
676✔
2960
      break;
130✔
2961
    }
2962
    
2963
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
546✔
2964
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
1,092!
2965

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

2970
    mstClearSStmStreamDeploy(pDeploy);
546✔
2971
    
2972
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
546!
2973
  }
2974
  
2975
_exit:
130✔
2976

2977
  if (pIter) {
130!
2978
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2979
  }
2980

2981
  if (code) {
130!
2982
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2983
  }
2984
  
2985
  return code;
130✔
2986
}
2987

2988
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
125✔
2989
  int32_t code = TSDB_CODE_SUCCESS;
125✔
2990
  int32_t lino = 0;
125✔
2991
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
125✔
2992
  SStmVgTasksToDeploy* pVg = NULL;
125✔
2993
  
2994
  for (int32_t i = 0; i < vgNum; ++i) {
826✔
2995
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
701✔
2996
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
701✔
2997
    if (NULL == pVg) {
701✔
2998
      continue;
410✔
2999
    }
3000

3001
    if (taosWTryLockLatch(&pVg->lock)) {
291!
3002
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3003
      continue;
×
3004
    }
3005
    
3006
    if (atomic_load_32(&pVg->deployed) <= 0) {
291!
3007
      taosWUnLockLatch(&pVg->lock);
×
3008
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3009
      continue;
×
3010
    }
3011

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

3023
    for (int32_t m = taskNum - 1; m >= 0; --m) {
13✔
3024
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
12✔
3025
      if (!pExt->deployed) {
12!
3026
        continue;
×
3027
      }
3028

3029
      mstDestroySStmTaskToDeployExt(pExt);
12✔
3030

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

3039
_exit:
125✔
3040

3041
  if (code) {
125!
3042
    if (pVg) {
×
3043
      taosWUnLockLatch(&pVg->lock);
×
3044
    }
3045

3046
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3047
  }
3048
}
125✔
3049

3050
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
114✔
3051
  if (!GOT_SNODE(snodeId)) {
114✔
3052
    return;
6✔
3053
  }
3054
  
3055
  int32_t code = TSDB_CODE_SUCCESS;
108✔
3056
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
108✔
3057
  if (NULL == pSnode) {
108✔
3058
    return;
4✔
3059
  }
3060

3061
  if (taosWTryLockLatch(&pSnode->lock)) {
104!
3062
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3063
    return;
×
3064
  }
3065

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

3075
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
103✔
3076
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
101✔
3077
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
101✔
3078
    pSnode->triggerList = NULL;
101✔
3079
  }
3080

3081
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
103✔
3082
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
101✔
3083
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
101✔
3084
    pSnode->runnerList = NULL;
101✔
3085
  }
3086

3087
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
103!
3088
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
101✔
3089
    taosWUnLockLatch(&pSnode->lock);
101✔
3090
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
101✔
3091
    return;
101✔
3092
  }
3093

3094
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
2!
3095
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
8✔
3096
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
6✔
3097
      if (!pExt->deployed) {
6!
3098
        continue;
×
3099
      }
3100

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

3109
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
2!
3110
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
20✔
3111
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
18✔
3112
      if (!pExt->deployed) {
18!
3113
        continue;
×
3114
      }
3115

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

3128
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
30,429✔
3129
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
30,429✔
3130
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
125✔
3131
  }
3132

3133
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
30,429✔
3134
    msmCleanDeployedSnodeTasks(pHb->snodeId);
114✔
3135
  }
3136
}
30,429✔
3137

3138
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
30,429✔
3139
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
30,429✔
3140
  if (mStreamMgmt.tCtx) {
30,429✔
3141
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
29,728✔
3142
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
29,728✔
3143
  }
3144
}
30,429✔
3145

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

3163
_exit:
528✔
3164

3165
  if (code) {
528!
3166
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3167
  }
3168

3169
  return code;
528✔
3170
}
3171

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

3188
_exit:
×
3189

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

3194
  return code;
×
3195
}
3196

3197

3198

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

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

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

3238
_exit:
138✔
3239

3240
  if (code) {
138!
3241
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3242
  }
3243

3244
  return code;
138✔
3245
}
3246

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

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

3268
_exit:
×
3269

3270
  if (code) {
16!
3271
    mstDestroySStmAction(&newAction);
×
3272
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3273
  }
3274

3275
  return code;
16✔
3276
}
3277

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

3284
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
626!
3285
    return false;
×
3286
  }
3287

3288
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
626✔
3289
  for (int32_t i = 0; i < readerNum; ++i) {
1,395✔
3290
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
769✔
3291
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
769!
3292
      return false;
×
3293
    }
3294
  }
3295

3296
  readerNum = taosArrayGetSize(pStream->trigOReaders);
626✔
3297
  for (int32_t i = 0; i < readerNum; ++i) {
725✔
3298
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
99✔
3299
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
99!
3300
      return false;
×
3301
    }
3302
  }
3303

3304
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
626!
3305
  SListNode* pNode = listHead(pStream->calcReaders);
626✔
3306
  for (int32_t i = 0; i < readerNum; ++i) {
1,478✔
3307
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
852✔
3308
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
852!
3309
      return false;
×
3310
    }
3311
    pNode = TD_DLIST_NODE_NEXT(pNode);
852✔
3312
  }
3313

3314
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
2,183✔
3315
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
1,655✔
3316
    for (int32_t m = 0; m < runnerNum; ++m) {
3,278✔
3317
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
1,721✔
3318
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
1,721!
3319
        return false;
98✔
3320
      }
3321
    }
3322
  }
3323
  
3324
  return true;
528✔
3325
}
3326

3327

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

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

3345
  stopped = atomic_load_8(&pStatus->stopped);
18,770✔
3346
  if (stopped) {
18,770!
3347
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3348
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3349
    return;
×
3350
  }
3351
  
3352
  switch (pMsg->status) {
18,770!
3353
    case STREAM_STATUS_INIT:      
18,763✔
3354
      if (STREAM_TRIGGER_TASK != pMsg->type) {
18,763✔
3355
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
17,744!
3356
        return;
17,744✔
3357
      }
3358
      
3359
      if (INT64_MIN == pStatus->lastActionTs) {
1,019!
3360
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3361
        return;
×
3362
      }
3363
      
3364
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
1,019✔
3365
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
393✔
3366
        return;
393✔
3367
      }
3368

3369
      if (STREAM_IS_RUNNING(pStatus)) {
626!
3370
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3371
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
626!
3372
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
528!
3373
      }
3374
      break;
626✔
3375
    case STREAM_STATUS_FAILED:
7✔
3376
      //STREAMTODO ADD ERRCODE HANDLE
3377
      msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode), pMsg->taskIdx);
7!
3378
      TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
7!
3379
      break;
7✔
3380
    default:
×
3381
      break;
×
3382
  }
3383

3384
_exit:
633✔
3385

3386
  if (code) {
633!
3387
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3388
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3389
  }
3390
}
3391

3392
void msmHandleStatusUpdateErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
131✔
3393
  int32_t code = TSDB_CODE_SUCCESS;
131✔
3394
  int32_t lino = 0;
131✔
3395
  SStreamTask* pTask = (SStreamTask*)pStatus;
131✔
3396
  int64_t streamId = pStatus->streamId;
131✔
3397

3398
  msttInfo("start to handle task status update exception, type: %d", err);
131!
3399
  
3400
  // STREAMTODO
3401

3402
  if (STM_ERR_TASK_NOT_EXISTS == err || STM_ERR_STREAM_STOPPED == err) {
131!
3403
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
131!
3404
  }
3405

3406
_exit:
131✔
3407

3408
  if (code) {
131!
3409
    // IGNORE STOP STREAM BY ERROR  
3410
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3411
  }
3412
}
131✔
3413

3414
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
7,131✔
3415
  int32_t code = TSDB_CODE_SUCCESS;
7,131✔
3416
  int32_t lino = 0;
7,131✔
3417
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
7,131✔
3418

3419
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
7,131!
3420
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3421
  }
3422
  
3423
  SArray* userRecalcList = NULL;
7,131✔
3424
  if (atomic_load_ptr(&pStream->userRecalcList)) {
7,131✔
3425
    taosWLockLatch(&pStream->userRecalcLock);
16✔
3426
    if (pStream->userRecalcList) {
16!
3427
      userRecalcList = pStream->userRecalcList;
16✔
3428
      pStream->userRecalcList = NULL;
16✔
3429
    }
3430
    taosWUnLockLatch(&pStream->userRecalcLock);
16✔
3431
    
3432
    if (userRecalcList) {
16!
3433
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
16!
3434
    }
3435
  }
3436

3437
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
7,131!
3438
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
3,511✔
3439
    if (NULL == pStatus->detailStatus) {
3,511✔
3440
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
401!
3441
      if (NULL == pStatus->detailStatus) {
401!
3442
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3443
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3444
      }
3445
    }
3446
    
3447
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
3,511✔
3448
    taosWUnLockLatch(&pStatus->detailStatusLock);
3,511✔
3449
  }
3450

3451
_exit:
3,620✔
3452

3453
  if (code) {
7,131!
3454
    // IGNORE STOP STREAM BY ERROR
3455
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3456
  }
3457
}
7,131✔
3458

3459
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
2,980✔
3460
  int32_t code = TSDB_CODE_SUCCESS;
2,980✔
3461
  int32_t lino = 0;
2,980✔
3462
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
2,980✔
3463

3464
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
2,980✔
3465

3466
  for (int32_t i = 0; i < num; ++i) {
50,743✔
3467
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
47,763✔
3468
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
47,763✔
3469
    
3470
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
47,763✔
3471
    if (NULL == ppStatus) {
47,763✔
3472
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
31!
3473
      msmHandleStatusUpdateErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
31✔
3474
      continue;
31✔
3475
    }
3476

3477
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
47,732✔
3478
    int8_t stopped = atomic_load_8(&pStream->stopped);
47,732✔
3479
    if (stopped) {
47,732✔
3480
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
100!
3481
      msmHandleStatusUpdateErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
100✔
3482
      continue;
100✔
3483
    }
3484

3485
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
47,632!
3486
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3487
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3488
          
3489
      msmHandleStatusUpdateErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3490
      continue;
×
3491
    }
3492

3493
    if ((*ppStatus)->status != pTask->status) {
47,632✔
3494
      if (STREAM_STATUS_RUNNING == pTask->status) {
4,842✔
3495
        (*ppStatus)->runningStartTs = pCtx->currTs;
2,085✔
3496
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
2,757!
3497
        if (pStream->triggerTask) {
×
3498
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3499
        }
3500
        
3501
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3502
      }
3503
    }
3504
    
3505
    (*ppStatus)->errCode = pTask->errorCode;
47,632✔
3506
    (*ppStatus)->status = pTask->status;
47,632✔
3507
    (*ppStatus)->lastUpTs = pCtx->currTs;
47,632✔
3508
    
3509
    if (STREAM_STATUS_RUNNING != pTask->status) {
47,632✔
3510
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
18,770✔
3511
    }
3512
    
3513
    if (STREAM_TRIGGER_TASK == pTask->type) {
47,632✔
3514
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
7,131✔
3515
    }
3516
  }
3517

3518
_exit:
2,980✔
3519

3520
  if (code) {
2,980!
3521
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3522
  }
3523

3524
  return code;
2,980✔
3525
}
3526

3527
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3528
  int32_t code = TSDB_CODE_SUCCESS;
×
3529
  int32_t lino = 0;
×
3530
  int64_t streamId = pTask->streamId;
×
3531
  SStreamObj* pStream = NULL;
×
3532

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

3545
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3546
    mndReleaseStream(pCtx->pMnode, pStream);
×
3547

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

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

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

3613
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3614
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3615
      break;
×
3616
    }
3617
    default: {
×
3618
      msttError("invalid task type:%d in task status", pTask->type);
×
3619
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3620
      break;
×
3621
    }
3622
  }
3623

3624
_exit:
×
3625

3626
  if (code) {
×
3627
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3628
  } else {
3629
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3630
  }
3631

3632
  return code;
×
3633
}
3634

3635
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3636
  int32_t code = TSDB_CODE_SUCCESS;
×
3637
  int32_t lino = 0;
×
3638
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3639

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

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

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

3663
_exit:
×
3664

3665
  if (code) {
×
3666
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3667
  }
3668

3669
  return code;
×
3670
}
3671

3672
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
528✔
3673
  int32_t code = TSDB_CODE_SUCCESS;
528✔
3674
  int32_t lino = 0;
528✔
3675
  if (NULL == pCtx->pRsp->start.taskList) {
528✔
3676
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
214✔
3677
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
214!
3678
  }
3679

3680
  SStmTaskId* pId = &pAction->start.triggerId;
528✔
3681
  SStreamTaskStart start = {0};
528✔
3682
  start.task.type = STREAM_TRIGGER_TASK;
528✔
3683
  start.task.streamId = streamId;
528✔
3684
  start.task.taskId = pId->taskId;
528✔
3685
  start.task.seriousId = pId->seriousId;
528✔
3686
  start.task.nodeId = pId->nodeId;
528✔
3687
  start.task.taskIdx = pId->taskIdx;
528✔
3688

3689
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
1,056!
3690
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
528!
3691

3692
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
528✔
3693

3694
  return;
528✔
3695

3696
_exit:
×
3697

3698
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3699
}
3700

3701

3702
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
50✔
3703
  int32_t code = TSDB_CODE_SUCCESS;
50✔
3704
  int32_t lino = 0;
50✔
3705
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
50✔
3706
  if (NULL == pCtx->pRsp->undeploy.taskList) {
50✔
3707
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
46✔
3708
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
46!
3709
  }
3710

3711
  SStreamTaskUndeploy undeploy;
3712
  for (int32_t i = 0; i < dropNum; ++i) {
188✔
3713
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
138✔
3714
    undeploy.task = *pTask;
138✔
3715
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
138✔
3716
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
138✔
3717

3718
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
276!
3719
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
138!
3720

3721
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
138✔
3722
  }
3723

3724
  return;
50✔
3725

3726
_exit:
×
3727

3728
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3729
}
3730

3731
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3732
  int32_t code = TSDB_CODE_SUCCESS;
×
3733
  int32_t lino = 0;
×
3734

3735
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3736
  if (NULL == pStream) {
×
3737
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3738
    return;
×
3739
  }
3740

3741
  if (NULL == pStream->triggerTask) {
×
3742
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3743
    return;
×
3744
  }
3745

3746
  SStreamMgmtRsp rsp = {0};
×
3747
  rsp.reqId = INT64_MIN;
×
3748
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3749
  rsp.task.streamId = streamId;
×
3750
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3751

3752
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3753

3754
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3755
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3756
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3757
  }
3758

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

3761
_exit:
×
3762

3763
  if (code) {
×
3764
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3765
  } else {
3766
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3767
  }
3768
}
3769

3770
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
16✔
3771
  int32_t code = TSDB_CODE_SUCCESS;
16✔
3772
  int32_t lino = 0;
16✔
3773

3774
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
16✔
3775
  if (NULL == pStream) {
16!
3776
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3777
    return;
×
3778
  }
3779

3780
  if (NULL == pStream->triggerTask) {
16!
3781
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3782
    return;
×
3783
  }
3784

3785
  SStreamMgmtRsp rsp = {0};
16✔
3786
  rsp.reqId = INT64_MIN;
16✔
3787
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
16✔
3788
  rsp.task.streamId = streamId;
16✔
3789
  rsp.task.taskId = pStream->triggerTask->id.taskId;
16✔
3790
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
16✔
3791

3792
  if (NULL == pCtx->pRsp->rsps.rspList) {
16✔
3793
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
14✔
3794
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
14!
3795
  }
3796

3797
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
32!
3798

3799
_exit:
16✔
3800

3801
  if (code) {
16!
3802
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3803
  } else {
3804
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
16!
3805
  }
3806
}
3807

3808

3809
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
274✔
3810
  int32_t code = TSDB_CODE_SUCCESS;
274✔
3811
  int32_t lino = 0;
274✔
3812
  void* pIter = NULL;
274✔
3813
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
274✔
3814

3815
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
274✔
3816

3817
  while (1) {
594✔
3818
    pIter = taosHashIterate(pCtx->actionStm, pIter);
868✔
3819
    if (pIter == NULL) {
868✔
3820
      break;
274✔
3821
    }
3822

3823
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
594✔
3824
    SStmAction *pAction = (SStmAction *)pIter;
594✔
3825
    
3826
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
594✔
3827
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
50✔
3828
      continue;
50✔
3829
    }
3830

3831
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
544!
3832
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3833
    }
3834

3835
    if (STREAM_ACT_RECALC & pAction->actions) {
544✔
3836
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
16✔
3837
    }
3838

3839
    if (STREAM_ACT_START & pAction->actions) {
544✔
3840
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
528✔
3841
    }
3842
  }
3843
  
3844
_exit:
274✔
3845

3846
  if (pIter) {
274!
3847
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3848
  }
3849

3850
  if (code) {
274!
3851
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3852
  }
3853

3854
  return code;
274✔
3855
}
3856

3857
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
29,728✔
3858
  int32_t  code = TSDB_CODE_SUCCESS;
29,728✔
3859
  int32_t  lino = 0;
29,728✔
3860
  int64_t* lastTs = NULL;
29,728✔
3861
  bool     noExists = false;
29,728✔
3862

3863
  while (true) {
3864
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
30,981✔
3865
    if (NULL == lastTs) {
30,981✔
3866
      if (noExists) {
1,599✔
3867
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
346!
3868
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
346!
3869
      }
3870

3871
      noExists = true;
1,253✔
3872
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
1,253!
3873
      
3874
      continue;
1,253✔
3875
    }
3876

3877
    while (true) {
×
3878
      int64_t lastTsValue = atomic_load_64(lastTs);
29,382✔
3879
      if (pCtx->currTs > lastTsValue) {
29,382✔
3880
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
29,379!
3881
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
29,379✔
3882
          return code;
29,379✔
3883
        }
3884

3885
        continue;
×
3886
      }
3887

3888
      return code;
3✔
3889
    }
3890

3891
    break;
3892
  }
3893

3894
_exit:
346✔
3895

3896
  if (code) {
346!
3897
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
346!
3898
  }
3899

3900
  return code;  
346✔
3901
}
3902

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

3916
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3917
    pStatus = (SStmStatus*)pIter;
×
3918

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

3932
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
3933
    if (pStatus->calcReaderNum != calcReaderNum) {
×
3934
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
3935
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3936
      continue;
×
3937
    }
3938

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

3950
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
×
3951
  int32_t code = TSDB_CODE_SUCCESS;
×
3952
  int32_t lino = 0;
×
3953
  int32_t minVal = watchError ? 0 : 1;
×
3954

3955
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
×
3956
    return code;
×
3957
  }
3958

3959
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
×
3960
    (void)sched_yield();
×
3961
  }
3962

3963
  if (watchError) {
×
3964
    taosHashClear(mStreamMgmt.vgroupMap);
×
3965
    taosHashClear(mStreamMgmt.snodeMap);
×
3966
    taosHashClear(mStreamMgmt.taskMap);
×
3967
    taosHashClear(mStreamMgmt.streamMap);
×
3968
    mstInfo("watch error happends, clear all maps");
×
3969
    goto _exit;
×
3970
  }
3971

3972
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
×
3973
    mstInfo("no stream tasks remain during watch state");
×
3974
    goto _exit;
×
3975
  }
3976

3977
  msmWatchCheckStreamMap(pCtx);
×
3978

3979
_exit:
×
3980

3981
  mStreamMgmt.lastTaskId += 100000;
×
3982

3983
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
×
3984

3985
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
×
3986

3987
  if (code) {
×
3988
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3989
  }
3990

3991
  return code;
×
3992
}
3993

3994

3995
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
×
3996
  int32_t code = TSDB_CODE_SUCCESS;
×
3997
  int32_t lino = 0;
×
3998
  SStreamHbMsg* pReq = pCtx->pReq;
×
3999

4000
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
×
4001
  
4002
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
×
4003
    goto _exit;
×
4004
  }
4005

4006
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
×
4007
  if (GOT_SNODE(pReq->snodeId)) {
×
4008
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
×
4009
  }
4010

4011
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
×
4012
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4013
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4014
  }
4015

4016
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
×
4017
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
×
4018
  }
4019

4020
_exit:
×
4021

4022
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
×
4023
  
4024
  if (code) {
×
4025
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4026

4027
    (void)msmWatchHandleEnding(pCtx, true);
×
4028
  }
4029

4030
  return code;
×
4031
}
4032

4033
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
142✔
4034
  int32_t code = TSDB_CODE_SUCCESS;
142✔
4035
  int32_t lino = 0;
142✔
4036
  bool    readerExists = false;
142✔
4037
  int64_t streamId = pTask->streamId;
142✔
4038

4039
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
142✔
4040
  for (int32_t i = 0; i < readerNum; ++i) {
266✔
4041
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
166✔
4042
    if (pReader->id.nodeId == vgId) {
166✔
4043
      readerExists = true;
42✔
4044
      break;
42✔
4045
    }
4046
  }
4047

4048
  if (!readerExists) {
142✔
4049
    if (NULL == pStatus->trigOReaders) {
100✔
4050
      pStatus->trigOReaders = taosArrayInit(vgNum, sizeof(SStmTaskStatus));
89✔
4051
      TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
89!
4052
    }
4053
    
4054
    SStmTaskStatus* pState = taosArrayReserve(pStatus->trigOReaders, 1);
100✔
4055
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
100!
4056
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
100!
4057
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
100!
4058
  }
4059

4060
_exit:
142✔
4061

4062
  if (code) {
142!
4063
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4064
  }
4065

4066
  return code;
142✔
4067
}
4068

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

4090
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
129✔
4091
  if (NULL == pStatus) {
129!
4092
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4093
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4094
  }
4095

4096
  stopped = atomic_load_8(&pStatus->stopped);
129✔
4097
  if (stopped) {
129!
4098
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4099
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4100
  }
4101

4102
  if (tbNum <= 0) {
129!
4103
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4104
    goto _exit;
×
4105
  }
4106

4107
  int32_t oReaderNum = taosArrayGetSize(pStatus->trigOReaders);
129✔
4108
  if (oReaderNum > 0) {
129!
4109
    mstsWarn("origReaders already exits, num:%d", oReaderNum);
×
4110
    goto _exit;
×
4111
  }
4112

4113
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
129!
4114
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
129✔
4115
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
129!
4116

4117
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
129✔
4118
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
129!
4119
  
4120
  for (int32_t i = 0; i < tbNum; ++i) {
460✔
4121
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
331✔
4122
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
331!
4123
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
662!
4124
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
331!
4125
  }
4126

4127
  int32_t vgNum = tSimpleHashGetSize(pVgs);
129✔
4128
  while (true) {
4129
    p = tSimpleHashIterate(pVgs, p, &iter);
271✔
4130
    if (NULL == p) {
271✔
4131
      break;
129✔
4132
    }
4133
    
4134
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
142!
4135
  }
4136
  
4137
  vgNum = taosArrayGetSize(pStatus->trigOReaders);
129✔
4138
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
129✔
4139
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
129!
4140

4141
  SStreamTaskAddr addr;
4142
  for (int32_t i = 0; i < vgNum; ++i) {
229✔
4143
    SStmTaskStatus* pOTask = taosArrayGet(pStatus->trigOReaders, i);
100✔
4144
    addr.taskId = pOTask->id.taskId;
100✔
4145
    addr.nodeId = pOTask->id.nodeId;
100✔
4146
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
100✔
4147
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
200!
4148
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
100!
4149
  }
4150

4151
  if (NULL == pCtx->pRsp->rsps.rspList) {
129!
4152
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4153
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
4154
  }
4155

4156
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
258!
4157

4158
_exit:
129✔
4159

4160
  tFreeSStreamMgmtReq(pMgmtReq);
129✔
4161
  taosMemoryFree(pMgmtReq);
129!
4162

4163
  tSimpleHashCleanup(pVgs);
129✔
4164

4165
  if (code) {
129!
4166
    tFreeSStreamMgmtRsp(&rsp);
×
4167
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4168
  }
4169

4170
  mstDestroyDbVgroupsHash(pDbVgroups);
129✔
4171

4172
  return code;
129✔
4173
}
4174

4175
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
11✔
4176
  int32_t num = taosArrayGetSize(pList);
11✔
4177
  SStreamCalcScan* pScan = NULL;
11✔
4178
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
11✔
4179
  int64_t planUid = 0;
11✔
4180
  for (int32_t i = 0; i < num; ++i) {
11!
4181
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
11✔
4182
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
11!
4183
    if (0 != planUid && planUid == uid) {
11!
4184
      *ppRes = pScan;
11✔
4185
      break;
11✔
4186
    }
4187
  }
4188

4189
_exit:
×
4190

4191
  if (code) {
11!
4192
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4193
  }
4194

4195
  return code;
11✔
4196
}
4197

4198
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
19✔
4199
  int32_t code = TSDB_CODE_SUCCESS;
19✔
4200
  int32_t lino = 0;
19✔
4201
  bool    readerExists = false;
19✔
4202
  int64_t streamId = pTask->streamId;
19✔
4203
  SListNode* pNode = listHead(pStatus->calcReaders);
19✔
4204
  SStmTaskStatus* pReader = NULL;
19✔
4205
  int32_t taskIdx = 0;
19✔
4206

4207
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
19!
4208
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
168✔
4209
    pReader = (SStmTaskStatus*)pNode->data;
157✔
4210
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
157✔
4211
      readerExists = true;
8✔
4212
      pAddr->taskId = pReader->id.taskId;
8✔
4213
      break;
8✔
4214
    }
4215
  }
4216

4217
  if (!readerExists) {
19✔
4218
    if (NULL == pStatus->calcReaders) {
11!
4219
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4220
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4221
      taskIdx = 0;
×
4222
    } else {
4223
      pNode = listTail(pStatus->calcReaders);
11✔
4224
      pReader = (SStmTaskStatus*)pNode->data;
11✔
4225
      taskIdx = pReader->id.taskIdx + 1;
11✔
4226
    }
4227

4228
    SStreamCalcScan* pScan = NULL;
11✔
4229
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
11!
4230
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
11!
4231
    pReader = tdListReserve(pStatus->calcReaders);
11✔
4232
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
11!
4233
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
11!
4234
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
11!
4235
    pAddr->taskId = pReader->id.taskId;
11✔
4236
  }
4237

4238
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
19✔
4239
  pAddr->nodeId = vgId;
19✔
4240

4241
_exit:
19✔
4242

4243
  if (code) {
19!
4244
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4245
  }
4246

4247
  return code;
19✔
4248
}
4249

4250

4251
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
17✔
4252
  int32_t code = TSDB_CODE_SUCCESS;
17✔
4253
  int32_t lino = 0;
17✔
4254
  int32_t vgId = 0;
17✔
4255
  int64_t streamId = pTask->streamId;
17✔
4256
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
17✔
4257
  int32_t reqNum = taosArrayGetSize(pReqs);
17✔
4258
  SStreamOReaderDeployReq* pReq = NULL;
17✔
4259
  SStreamOReaderDeployRsp* pRsp = NULL;
17✔
4260
  SStreamMgmtRsp rsp = {0};
17✔
4261
  rsp.reqId = pTask->pMgmtReq->reqId;
17✔
4262
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
17✔
4263
  SStreamMgmtReq* pMgmtReq = NULL;
17✔
4264
  int8_t stopped = 0;
17✔
4265
  int32_t vgNum = 0;
17✔
4266
  SStreamTaskAddr* pAddr = NULL;
17✔
4267
  
4268
  TSWAP(pTask->pMgmtReq, pMgmtReq);
17✔
4269
  rsp.task = *(SStreamTask*)pTask;
17✔
4270

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

4277
  stopped = atomic_load_8(&pStatus->stopped);
17✔
4278
  if (stopped) {
17!
4279
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4280
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4281
  }
4282

4283
  if (reqNum <= 0) {
17!
4284
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4285
    goto _exit;
×
4286
  }
4287

4288
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
17✔
4289
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
17!
4290

4291
  for (int32_t i = 0; i < reqNum; ++i) {
34✔
4292
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
17✔
4293
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
17✔
4294
    pRsp->execId = pReq->execId;
17✔
4295
    vgNum = taosArrayGetSize(pReq->vgIds);
17✔
4296
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
17✔
4297
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
17!
4298
    
4299
    for (int32_t n = 0; n < vgNum; ++n) {
36✔
4300
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
19✔
4301
      pAddr = taosArrayGet(pRsp->vgList, n);
19✔
4302
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
19!
4303
    }
4304
  }
4305

4306
  if (NULL == pCtx->pRsp->rsps.rspList) {
17!
4307
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4308
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
4309
  }
4310

4311
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
34!
4312

4313
_exit:
17✔
4314

4315
  tFreeSStreamMgmtReq(pMgmtReq);
17✔
4316
  taosMemoryFree(pMgmtReq);
17!
4317

4318
  if (code) {
17!
4319
    tFreeSStreamMgmtRsp(&rsp);
×
4320
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4321
  }
4322

4323
  return code;
17✔
4324
}
4325

4326

4327
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
146✔
4328
  int32_t code = TSDB_CODE_SUCCESS;
146✔
4329
  int32_t lino = 0;
146✔
4330

4331
  switch (pTask->pMgmtReq->type) {
146!
4332
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
129✔
4333
      TAOS_CHECK_EXIT(msmDeployTriggerOrigReader(pCtx, pTask));
129!
4334
      break;
129✔
4335
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
17✔
4336
      TAOS_CHECK_EXIT(msmDeployRunnerOrigReader(pCtx, pTask));
17!
4337
      break;
17✔
4338
    default:
×
4339
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4340
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4341
      break;
×
4342
  }
4343

4344
_exit:
146✔
4345

4346
  if (code) {
146!
4347
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4348
  }
4349

4350
  return code;
146✔
4351
}
4352

4353
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
48✔
4354
  int32_t code = TSDB_CODE_SUCCESS;
48✔
4355
  int32_t lino = 0;
48✔
4356
  SStreamHbMsg* pReq = pCtx->pReq;
48✔
4357
  SStmTaskStatusMsg* pTask = NULL;
48✔
4358
  
4359
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
48✔
4360
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
48!
4361
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
48✔
4362
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
48!
4363
  }
4364
  
4365
  for (int32_t i = 0; i < reqNum; ++i) {
194✔
4366
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
146✔
4367
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
146✔
4368
    if (NULL == pTask) {
146!
4369
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4370
      continue;
×
4371
    }
4372

4373
    if (NULL == pTask->pMgmtReq) {
146!
4374
      msttError("idx %d without mgmtReq", idx);
×
4375
      continue;
×
4376
    }
4377

4378
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
146!
4379
  }
4380

4381
_exit:
48✔
4382

4383
  if (code) {
48!
4384
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4385
  }
4386

4387
  return code;
48✔
4388
}
4389

4390
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
29,728✔
4391
  int32_t code = TSDB_CODE_SUCCESS;
29,728✔
4392
  int32_t lino = 0;
29,728✔
4393
  SStreamHbMsg* pReq = pCtx->pReq;
29,728✔
4394

4395
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
29,728✔
4396
  if (GOT_SNODE(pReq->snodeId)) {
29,382✔
4397
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
3,094!
4398
  }
4399
  
4400
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
29,382!
4401
    msmHandleStreamActions(pCtx);
90✔
4402
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
90✔
4403
  }
4404

4405
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
29,382!
4406
    code = msmHandleStreamRequests(pCtx);
48✔
4407
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
48✔
4408
    TAOS_CHECK_EXIT(code);
48!
4409
  }
4410

4411
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
29,382✔
4412
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
125!
4413
  } else {
4414
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
29,257!
4415
  }
4416

4417
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
29,382✔
4418
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
108!
4419
  }
4420

4421
  if (taosHashGetSize(pCtx->deployStm) > 0) {
29,382✔
4422
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
130!
4423
  }
4424

4425
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
29,382✔
4426
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
2,980!
4427
  }
4428

4429
  if (taosHashGetSize(pCtx->actionStm) > 0) {
29,382✔
4430
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
274!
4431
  }
4432

4433
_exit:
29,382✔
4434

4435
  if (code) {
29,728✔
4436
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
346!
4437
  }
4438

4439
  return code;
29,728✔
4440
}
4441

4442
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
30,429✔
4443
  int32_t lino = 0;
30,429✔
4444
  int32_t tlen = 0;
30,429✔
4445
  void   *buf = NULL;
30,429✔
4446

4447
  if (TSDB_CODE_SUCCESS != code) {
30,429✔
4448
    goto _exit;
346✔
4449
  }
4450

4451
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
30,083!
4452
  if (code < 0) {
30,083!
4453
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4454
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4455
  }
4456

4457
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
30,083✔
4458
  if (buf == NULL) {
30,083!
4459
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4460
    TAOS_CHECK_EXIT(terrno);    
×
4461
  }
4462

4463
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
30,083✔
4464
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
30,083✔
4465

4466
  SEncoder encoder;
4467
  tEncoderInit(&encoder, abuf, tlen);
30,083✔
4468
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
30,083!
4469
    rpcFreeCont(buf);
×
4470
    buf = NULL;
×
4471
    tEncoderClear(&encoder);
×
4472
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4473
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4474
  }
4475
  tEncoderClear(&encoder);
30,083✔
4476

4477
_exit:
30,429✔
4478

4479
  pMsg->code = code;
30,429✔
4480
  pMsg->info = *pRpcInfo;
30,429✔
4481
  if (TSDB_CODE_SUCCESS == code) {
30,429✔
4482
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
30,083✔
4483
    pMsg->pCont = buf;
30,083✔
4484
  }
4485
}
30,429✔
4486

4487

4488
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
30,429✔
4489
  int32_t code = TSDB_CODE_SUCCESS;
30,429✔
4490
  SMStreamHbRspMsg rsp = {0};
30,429✔
4491
  rsp.streamGId = pHb->streamGId;
30,429✔
4492

4493
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
30,429✔
4494

4495
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
30,429✔
4496
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
701!
4497
    goto _exit;
701✔
4498
  }
4499

4500
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
29,728✔
4501
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
29,728✔
4502

4503
  pCtx->tidx = tidx;
29,728✔
4504
  pCtx->pMnode = pMnode;
29,728✔
4505
  pCtx->currTs = currTs;
29,728✔
4506
  pCtx->pReq = pHb;
29,728✔
4507
  pCtx->pRsp = &rsp;
29,728✔
4508
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
29,728✔
4509
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
29,728✔
4510
  
4511
  switch (atomic_load_8(&mStreamMgmt.state)) {
29,728!
4512
    case MND_STM_STATE_WATCH:
×
4513
      code = msmWatchHandleHbMsg(pCtx);
×
4514
      break;
×
4515
    case MND_STM_STATE_NORMAL:
29,728✔
4516
      code = msmNormalHandleHbMsg(pCtx);
29,728✔
4517
      break;
29,728✔
4518
    default:
×
4519
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4520
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4521
      break;
×
4522
  }
4523

4524
_exit:
30,429✔
4525

4526
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
30,429✔
4527

4528
  msmCleanStreamGrpCtx(pHb);
30,429✔
4529
  msmClearStreamToDeployMaps(pHb);
30,429✔
4530

4531
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
30,429✔
4532
  
4533
  tFreeSMStreamHbRspMsg(&rsp);
30,429✔
4534

4535
  return code;
30,429✔
4536
}
4537

4538
void msmHandleBecomeLeader(SMnode *pMnode) {
1,121✔
4539
  if (tsDisableStream) {
1,121!
4540
    return;
×
4541
  }
4542

4543
  mstInfo("start to process mnode become leader");
1,121!
4544

4545
  int32_t code = 0;
1,121✔
4546
  streamAddVnodeLeader(MNODE_HANDLE);
1,121✔
4547
  
4548
  taosWLockLatch(&mStreamMgmt.runtimeLock);
1,121✔
4549
  msmDestroyRuntimeInfo(pMnode);
1,121✔
4550
  code = msmInitRuntimeInfo(pMnode);
1,121✔
4551
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
1,121✔
4552

4553
  if (TSDB_CODE_SUCCESS == code) {
1,121!
4554
    atomic_store_8(&mStreamMgmt.active, 1);
1,121✔
4555
  }
4556

4557
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
1,121!
4558
}
4559

4560
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
1,748✔
4561
  if (tsDisableStream) {
1,748!
4562
    return;
×
4563
  }
4564

4565
  mstInfo("start to process mnode become not leader");
1,748!
4566

4567
  streamRemoveVnodeLeader(MNODE_HANDLE);
1,748✔
4568

4569
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
1,748✔
4570
    taosWLockLatch(&mStreamMgmt.runtimeLock);
1,121✔
4571
    msmDestroyRuntimeInfo(pMnode);
1,121✔
4572
    mStreamMgmt.stat.inactiveTimes++;
1,121✔
4573
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
1,121✔
4574
  }
4575
}
4576

4577

4578
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4579
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4580
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4581
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4582
  } else {
4583
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4584
  }
4585
}
×
4586

4587
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
35✔
4588
  int32_t code = TSDB_CODE_SUCCESS;
35✔
4589
  int32_t lino = 0;
35✔
4590
  SStreamObj* pStream = pObj;
35✔
4591
  SSnodeObj* pSnode = p1;
35✔
4592
  SArray** ppRes = p2;
35✔
4593

4594
  if (pStream->mainSnodeId == pSnode->id) {
35✔
4595
    if (NULL == *ppRes) {
19✔
4596
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
3✔
4597
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
3✔
4598
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
3!
4599
    }
4600

4601
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
38!
4602
  }
4603

4604
  return true;
35✔
4605

4606
_exit:
×
4607

4608
  if (code) {
×
4609
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4610
  }  
4611

4612
  *(int32_t*)p3 = code;
×
4613

4614
  return false;
×
4615
}
4616

4617

4618
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
156✔
4619
  int32_t code = TSDB_CODE_SUCCESS;
156✔
4620
  int32_t lino = 0;
156✔
4621
  
4622
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
156✔
4623
  TAOS_CHECK_EXIT(code);
156!
4624

4625
  int32_t streamNum = taosArrayGetSize(*ppRes);
156✔
4626
  if (streamNum > 0 && 0 == pSnode->replicaId) {
156✔
4627
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
1!
4628
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
1!
4629
  }
4630

4631
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4632

4633
_exit:
155✔
4634

4635
  if (code) {
156✔
4636
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
1!
4637
  }  
4638

4639
  return code;
156✔
4640
}
4641

4642
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
1,290✔
4643
  SStreamObj* pStream = pObj;
1,290✔
4644
  int64_t streamId = pStream->pCreate->streamId;
1,290✔
4645
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
1,290✔
4646
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
1,290✔
4647
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
1,290✔
4648
  
4649
  if ((userDropped || userStopped) && (NULL == pStatus)) {
1,290!
4650
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
1!
4651
    return true;
1✔
4652
  }
4653
  
4654
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
1,289✔
4655
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
994✔
4656
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4657
    return true;
994✔
4658
  }
4659

4660
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
295!
4661
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
69!
4662
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4663
    return true;
69✔
4664
  }  
4665

4666
  if (pStatus) {
226!
4667
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
226!
4668
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
1✔
4669
    }
4670

4671
    return true;
226✔
4672
  }
4673

4674
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
×
4675

4676
  return true;
×
4677
}
4678

4679
void msmCheckLoopStreamMap(SMnode *pMnode) {
126✔
4680
  SStmStatus* pStatus = NULL;
126✔
4681
  void* pIter = NULL;
126✔
4682
  int8_t stopped = 0;
126✔
4683
  int64_t streamId = 0;
126✔
4684
  
4685
  while (true) {
4686
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
956✔
4687
    if (NULL == pIter) {
956✔
4688
      break;
126✔
4689
    }
4690

4691
    pStatus = (SStmStatus*)pIter;
830✔
4692

4693
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
830✔
4694
    stopped = atomic_load_8(&pStatus->stopped);
830✔
4695
    if (MST_IS_USER_STOPPED(stopped)) {
830!
4696
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
1!
4697
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
1✔
4698
      continue;
1✔
4699
    }
4700

4701
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
829!
4702
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4703
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4704
      continue;
×
4705
    }
4706

4707
    if (MST_IS_ERROR_STOPPED(stopped)) {
829✔
4708
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
21✔
4709
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
14✔
4710
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4711
            
4712
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
14✔
4713
        continue;
14✔
4714
      }
4715

4716
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
7✔
4717
      continue;
7✔
4718
    }
4719

4720
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
808!
4721
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4722
      continue;
×
4723
    }
4724
  }
4725
}
126✔
4726

4727
void msmCheckStreamsStatus(SMnode *pMnode) {
849✔
4728
  SStmCheckStatusCtx ctx = {0};
849✔
4729

4730
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
849✔
4731
  
4732
  if (MST_READY_FOR_SDB_LOOP()) {
849!
4733
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
181✔
4734
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
181✔
4735
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
181✔
4736
  }
4737

4738
  if (MST_READY_FOR_MAP_LOOP()) {
849!
4739
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
126✔
4740
    msmCheckLoopStreamMap(pMnode);
126✔
4741
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
126✔
4742
  }
4743
}
849✔
4744

4745
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
5,196✔
4746
  for (int32_t i = 0; i < taskNum; ++i) {
10,617✔
4747
    SStmTaskStatus* pTask = *(pList + i);
5,426✔
4748

4749
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
5,426✔
4750
      continue;
5,413✔
4751
    }
4752
    
4753
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
5,260✔
4754
      continue;
5,247✔
4755
    }
4756

4757
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
13✔
4758
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
13✔
4759
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
5!
4760
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4761
          
4762
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
5✔
4763
      break;
5✔
4764
    }
4765

4766
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
8!
4767
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4768

4769
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
8✔
4770
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
8!
4771

4772
    SStmTaskAction task = {0};
8✔
4773
    task.streamId = streamId;
8✔
4774
    task.id = pTask->id;
8✔
4775
    task.flag = pTask->flags;
8✔
4776
    task.type = pTask->type;
8✔
4777
    
4778
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
8✔
4779
  }
4780
}
5,196✔
4781

4782
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
364✔
4783
  void* pIter = NULL;
364✔
4784
  SStmVgStreamStatus* pVg = NULL;
364✔
4785
  int64_t streamId = 0;
364✔
4786
  
4787
  while (true) {
1,732✔
4788
    pIter = taosHashIterate(pStreams, pIter);
2,096✔
4789
    if (NULL == pIter) {
2,096✔
4790
      break;
364✔
4791
    }
4792

4793
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,732✔
4794
    pVg = (SStmVgStreamStatus*)pIter;
1,732✔
4795

4796
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,732✔
4797
    if (taskNum > 0) {
1,732✔
4798
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,155✔
4799
    }
4800

4801
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,732✔
4802
    if (taskNum > 0) {
1,732✔
4803
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
904✔
4804
    }
4805
  }
4806
}
364✔
4807

4808
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4809
  int64_t streamId = 0;
×
4810
  void* pIter = NULL;
×
4811
  SStmVgStreamStatus* pStream = NULL;
×
4812

4813
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4814
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4815
    return;
×
4816
  }
4817

4818
  
4819
  while (true) {
4820
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4821
    if (NULL == pIter) {
×
4822
      break;
×
4823
    }
4824

4825
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4826
    
4827
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4828
  }
4829

4830
  taosHashClear(pVg->streamTasks);
×
4831
}
4832

4833

4834
void msmCheckVgroupStatus(SMnode *pMnode) {
849✔
4835
  void* pIter = NULL;
849✔
4836
  int32_t code = 0;
849✔
4837
  
4838
  while (true) {
3,381✔
4839
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
4,230✔
4840
    if (NULL == pIter) {
4,230✔
4841
      break;
849✔
4842
    }
4843

4844
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
3,381✔
4845
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
3,381✔
4846
      continue;
3,017✔
4847
    }
4848
    
4849
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
370✔
4850

4851
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
370✔
4852
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
6✔
4853
      if (NULL == pVgroup) {
6!
4854
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
6✔
4855
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
6✔
4856
        if (code) {
6!
4857
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4858
        }
4859
        continue;
6✔
4860
      }
4861
      mndReleaseVgroup(pMnode, pVgroup);
×
4862
      
4863
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4864
      
4865
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
4866
      continue;
×
4867
    }
4868

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

4871
    msmCheckVgroupStreamStatus(pVg->streamTasks);
364✔
4872
  }
4873
}
849✔
4874

4875
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
1✔
4876
  *deployNum = 0;
1✔
4877
  
4878
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2!
4879
    if (pStream->runners[i]) {
2✔
4880
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
1✔
4881
      for (int32_t t = 0; t < taskNum; ++t) {
1!
4882
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
1✔
4883
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
1✔
4884
        if (stopped) {
1!
4885
          mstsDebug("stream already stopped %d, ignore it", stopped);
1!
4886
          *deployNum = 0;
1✔
4887
          return;
1✔
4888
        }
4889

4890
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
×
4891
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
×
4892
      }
4893
      
4894
      deployId[*deployNum] = i;
×
4895
      (*deployNum)++;
×
4896
    }
4897
  }
4898
}
4899

4900
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
6✔
4901
  pSnode->runnerThreadNum = -1;
6✔
4902

4903
  (void)msmSTAddSnodesToMap(pMnode);
6✔
4904

4905
  int64_t streamId = 0;
6✔
4906
  void* pIter = NULL;
6✔
4907
  SStmSnodeStreamStatus* pStream = NULL;
6✔
4908
  int32_t deployNum = 0;
6✔
4909
  SStmTaskAction task = {0};
6✔
4910
  
4911
  while (true) {
4912
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
9✔
4913
    if (NULL == pIter) {
9✔
4914
      break;
6✔
4915
    }
4916

4917
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
3✔
4918
    
4919
    task.streamId = streamId;
3✔
4920
    
4921
    pStream = (SStmSnodeStreamStatus*)pIter;
3✔
4922
    if (pStream->trigger) {
3✔
4923
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
2✔
4924
      if (stopped) {
2!
4925
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
4926
        continue;
×
4927
      }
4928

4929
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
2!
4930
      
4931
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
2✔
4932
    } else {
4933
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
1✔
4934
      
4935
      if (task.deployNum > 0) {
1!
4936
        //task.triggerStatus = pStream->trigger;
4937
        task.multiRunner = true;
×
4938
        task.type = STREAM_RUNNER_TASK;
×
4939
        
4940
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
×
4941
        
4942
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
×
4943
      }
4944
    }
4945
  }
4946

4947
  taosHashClear(pSnode->streamTasks);
6✔
4948
}
6✔
4949

4950

4951
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
119✔
4952
  void* pIter = NULL;
119✔
4953
  SStmSnodeStreamStatus* pSnode = NULL;
119✔
4954
  int64_t streamId = 0;
119✔
4955
  
4956
  while (true) {
4957
    pIter = taosHashIterate(pStreams, pIter);
962✔
4958
    if (NULL == pIter) {
962✔
4959
      break;
119✔
4960
    }
4961

4962
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
843✔
4963
    pSnode = (SStmSnodeStreamStatus*)pIter;
843✔
4964

4965
    if (NULL != pSnode->trigger) {
843✔
4966
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
792✔
4967
    }
4968

4969
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
3,372✔
4970
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
2,529✔
4971
      if (taskNum > 0) {
2,529✔
4972
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
2,345✔
4973
      }
4974
    }
4975
  }
4976
}
119✔
4977

4978

4979
void msmCheckSnodeStatus(SMnode *pMnode) {
849✔
4980
  void* pIter = NULL;
849✔
4981
  
4982
  while (true) {
1,144✔
4983
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
1,993✔
4984
    if (NULL == pIter) {
1,993✔
4985
      break;
849✔
4986
    }
4987

4988
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,144✔
4989
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,144✔
4990
      continue;
1,007✔
4991
    }
4992

4993
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
137✔
4994
    
4995
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
137✔
4996
    if (NULL == pSnode->streamTasks) {
137✔
4997
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
13!
4998
      continue;
13✔
4999
    }
5000
    
5001
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
124✔
5002
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
5!
5003
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5004
      
5005
      msmHandleSnodeLost(pMnode, pSnode);
5✔
5006
      continue;
5✔
5007
    }
5008
    
5009
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
119✔
5010

5011
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
119✔
5012
  }
5013
}
849✔
5014

5015

5016
void msmCheckTasksStatus(SMnode *pMnode) {
849✔
5017
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
849✔
5018

5019
  msmCheckVgroupStatus(pMnode);
849✔
5020
  msmCheckSnodeStatus(pMnode);
849✔
5021
}
849✔
5022

5023
void msmCheckSnodesState(SMnode *pMnode) {
849✔
5024
  if (!MST_READY_FOR_SNODE_LOOP()) {
849!
5025
    return;
739✔
5026
  }
5027

5028
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
110✔
5029

5030
  void* pIter = NULL;
110✔
5031
  int32_t snodeId = 0;
110✔
5032
  while (true) {
124✔
5033
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
234✔
5034
    if (NULL == pIter) {
234✔
5035
      break;
110✔
5036
    }
5037

5038
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
124✔
5039
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
124✔
5040
      continue;
123✔
5041
    }
5042

5043
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
1✔
5044
    if (NULL == pSnode->streamTasks) {
1!
5045
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5046
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5047
      continue;
×
5048
    }
5049
    
5050
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1!
5051
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5052
    
5053
    msmHandleSnodeLost(pMnode, pSnode);
1✔
5054
  }
5055

5056
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
110✔
5057
}
5058

5059
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
13,825✔
5060
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
13,825✔
5061
  if (0 == active || MND_STM_STATE_NORMAL != state) {
13,825!
5062
    mstTrace("ignore health check since active:%d state:%d", active, state);
×
5063
    return false;
×
5064
  }
5065

5066
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
13,825✔
5067
    mstTrace("ignore health check since no stream now");
12,127✔
5068
    return false;
12,127✔
5069
  }
5070

5071
  return true;
1,698✔
5072
}
5073

5074
void msmHealthCheck(SMnode *pMnode) {
12,976✔
5075
  if (!msmCheckNeedHealthCheck(pMnode)) {
12,976✔
5076
    return;
12,127✔
5077
  }
5078

5079
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
1,582✔
5080
  
5081
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
849✔
5082
  if (!msmCheckNeedHealthCheck(pMnode)) {
849!
5083
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5084
    return;
×
5085
  }
5086
  
5087
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
849✔
5088
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
849✔
5089

5090
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
849✔
5091
  
5092
  msmCheckStreamsStatus(pMnode);
849✔
5093
  msmCheckTasksStatus(pMnode);
849✔
5094
  msmCheckSnodesState(pMnode);
849✔
5095

5096
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
849✔
5097

5098
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
849✔
5099
}
5100

5101
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
×
5102
  SStreamObj *pStream = pObj;
×
5103
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
×
5104
    return true;
×
5105
  }
5106
  
5107
  pStream->updateTime = *(int64_t*)p1;
×
5108
  
5109
  (*(int32_t*)p2)++;
×
5110
  
5111
  return true;
×
5112
}
5113

5114
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5115
  int32_t code = 0;
×
5116
  int8_t  stopped = 0;
×
5117
  
5118
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5119
  
5120
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5121
  if (NULL == pStatus) {
×
5122
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5123
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5124
    goto _exit;
×
5125
  }
5126

5127
  stopped = atomic_load_8(&pStatus->stopped);
×
5128
  if (stopped) {
×
5129
    mstsError("stream already stopped, stopped:%d", stopped);
×
5130
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5131
    goto _exit;
×
5132
  }
5133

5134
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5135
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5136
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5137
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5138
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5139
    goto _exit;
×
5140
  }
5141

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

5145
_exit:
×
5146
  
5147
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5148

5149
  return code;
×
5150
}
5151

5152
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
1,121✔
5153
  int32_t code = TSDB_CODE_SUCCESS;
1,121✔
5154
  int32_t lino = 0;
1,121✔
5155
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
1,121✔
5156
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
1,121✔
5157
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
1,121✔
5158

5159
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
2,216✔
5160

5161
  mStreamMgmt.stat.activeTimes++;
1,121✔
5162
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
1,121✔
5163
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
1,121!
5164
  if (NULL == mStreamMgmt.tCtx) {
1,121!
5165
    code = terrno;
×
5166
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5167
    goto _exit;
×
5168
  }
5169

5170
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
1,121!
5171
  if (mStreamMgmt.actionQ == NULL) {
1,121!
5172
    code = terrno;
×
5173
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5174
    goto _exit;
×
5175
  }
5176
  
5177
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
1,121!
5178
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
1,121!
5179
  
5180
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
1,121✔
5181
  
5182
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
6,653✔
5183
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
5,532✔
5184

5185
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
33,192✔
5186
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
27,660✔
5187
      if (pCtx->deployStm[m] == NULL) {
27,660!
5188
        code = terrno;
×
5189
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5190
        goto _exit;
×
5191
      }
5192
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
27,660✔
5193
      
5194
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
27,660✔
5195
      if (pCtx->actionStm[m] == NULL) {
27,660!
5196
        code = terrno;
×
5197
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5198
        goto _exit;
×
5199
      }
5200
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
27,660✔
5201
    }
5202
  }
5203
  
5204
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
1,121✔
5205
  if (mStreamMgmt.streamMap == NULL) {
1,121!
5206
    code = terrno;
×
5207
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5208
    goto _exit;
×
5209
  }
5210
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
1,121✔
5211
  
5212
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
1,121✔
5213
  if (mStreamMgmt.taskMap == NULL) {
1,121!
5214
    code = terrno;
×
5215
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5216
    goto _exit;
×
5217
  }
5218
  
5219
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,121✔
5220
  if (mStreamMgmt.vgroupMap == NULL) {
1,121!
5221
    code = terrno;
×
5222
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5223
    goto _exit;
×
5224
  }
5225
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
1,121✔
5226

5227
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,121✔
5228
  if (mStreamMgmt.snodeMap == NULL) {
1,121!
5229
    code = terrno;
×
5230
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5231
    goto _exit;
×
5232
  }
5233
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
1,121✔
5234
  
5235
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,121✔
5236
  if (mStreamMgmt.dnodeMap == NULL) {
1,121!
5237
    code = terrno;
×
5238
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5239
    goto _exit;
×
5240
  }
5241

5242
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,121✔
5243
  if (mStreamMgmt.toDeployVgMap == NULL) {
1,121!
5244
    code = terrno;
×
5245
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5246
    goto _exit;
×
5247
  }
5248
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
1,121✔
5249
  
5250
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
1,121✔
5251
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
1,121!
5252
    code = terrno;
×
5253
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5254
    goto _exit;
×
5255
  }
5256
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
1,121✔
5257

5258
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
1,121✔
5259
  if (mStreamMgmt.toUpdateScanMap == NULL) {
1,121!
5260
    code = terrno;
×
5261
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5262
    goto _exit;
×
5263
  }
5264
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
1,121✔
5265

5266
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
1,121!
5267
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
1,121!
5268

5269
  mStreamMgmt.lastTaskId = 1;
1,121✔
5270

5271
  int32_t activeStreamNum = 0;
1,121✔
5272
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
1,121✔
5273

5274
  if (activeStreamNum > 0) {
1,121!
5275
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
×
5276
  } else {
5277
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
1,121✔
5278
  }
5279

5280
_exit:
1,121✔
5281

5282
  if (code) {
1,121!
5283
    msmDestroyRuntimeInfo(pMnode);
×
5284
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5285
  } else {
5286
    mstInfo("mnode stream runtime init done");
1,121!
5287
  }
5288

5289
  return code;
1,121✔
5290
}
5291

5292

5293

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