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

taosdata / TDengine / #5029

21 Apr 2026 10:00AM UTC coverage: 73.003% (+0.02%) from 72.986%
#5029

push

travis-ci

web-flow
fix(tmq): balance vgroup error (#35183)

273843 of 375111 relevant lines covered (73.0%)

134074995.07 hits per line

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

76.14
/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() {
878,952✔
34
  SStmQNode* pQNode = NULL;
878,952✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
878,952✔
37
    return;
439,476✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,192,392✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
13,154,352✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,961,960✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,961,960✔
51
  }
52
}
2,192,392✔
53

54
void msmDestroyThreadCtxs() {
878,952✔
55
  if (NULL == mStreamMgmt.tCtx) {
878,952✔
56
    return;
439,476✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,631,868✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,192,392✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
439,476✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
878,952✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
878,952✔
94
}
878,952✔
95

96

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

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

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

111
    pStatus = pStream;
191✔
112
  }
113

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

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

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

136
_exit:
×
137

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

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

145

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

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

200
  pSnode = NULL;
468,854✔
201

202
_exit:
468,854✔
203

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

208
  return code;
468,854✔
209
}
210

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

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

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

235
  pDnode = NULL;
973,904✔
236

237
_exit:
973,904✔
238

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

243
  return code;
973,904✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
699,664✔
275
  int32_t code = TSDB_CODE_SUCCESS;
699,664✔
276
  int32_t lino = 0;
699,664✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
699,664✔
278
  if (NULL == pStream) {
699,664✔
279
    SStmVgStreamStatus stream = {0};
497,392✔
280
    if (trigReader) {
497,392✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
289,752✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
289,752✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
579,504✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
207,640✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
207,640✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
415,280✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
497,392✔
290
    goto _exit;
497,392✔
291
  }
292
  
293
  if (trigReader) {
202,272✔
294
    if (NULL == pStream->trigReaders) {
23,954✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
23,954✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
23,954✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
47,908✔
300
    goto _exit;
23,954✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
178,318✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
117,823✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
117,823✔
306
  }
307

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

310
_exit:
178,318✔
311

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

320
  return code;
699,664✔
321
}
322

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

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

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

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

350
  return code;
699,664✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
701,022✔
363
    if (NULL == pVg) {
701,022✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
172,830✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
172,830✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
345,660✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
172,830✔
368
      if (TSDB_CODE_SUCCESS == code) {
172,830✔
369
        goto _return;
172,830✔
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);
528,192✔
381
    if (NULL == pVg->taskList) {
528,192✔
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,056,384✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
528,192✔
390
    
391
    taosHashRelease(pVgMap, pVg);
528,192✔
392
    break;
528,192✔
393
  }
394
  
395
_return:
701,022✔
396

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

404
  return code;
701,022✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
927,956✔
409
  int32_t code = TSDB_CODE_SUCCESS;
927,956✔
410
  int32_t lino = 0;
927,956✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
927,956✔
412
  if (NULL == pStream) {
927,956✔
413
    SStmSnodeStreamStatus stream = {0};
213,906✔
414
    if (deployId < 0) {
213,906✔
415
      stream.trigger = pStatus;
3,364✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
210,542✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
210,542✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
421,084✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
213,906✔
423
    goto _exit;
213,906✔
424
  }
425
  
426
  if (deployId < 0) {
714,050✔
427
    if (NULL != pStream->trigger) {
203,027✔
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;
203,027✔
433
    goto _exit;
203,027✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
511,023✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
400,230✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
400,230✔
439
  }
440

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

443
_exit:
511,023✔
444

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

453
  return code;
927,956✔
454
}
455

456

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

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

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

484
  return code;
927,956✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
206,391✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
57,173✔
568
      ext.deployed = false;
57,173✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
114,346✔
570

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

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

614
  return code;
721,565✔
615
}
616

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

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

632
_exit:
653,465✔
633

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

638
  return code;
653,465✔
639
}
640

641

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

648
  while (true) {
649
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &pCtx->pReq->snodeId, sizeof(pCtx->pReq->snodeId));
1,557,270✔
650
    if (NULL == pStatus) {
1,557,270✔
651
      if (noExists) {
16,474✔
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;
16,474✔
657
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pCtx->pMnode));
16,474✔
658
      
659
      continue;
16,474✔
660
    }
661

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

672
        continue;
×
673
      }
674

675
      return code;
18,820✔
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) {
57,486,953✔
691
  int32_t  code = TSDB_CODE_SUCCESS;
57,486,953✔
692
  int32_t  lino = 0;
57,486,953✔
693
  SStmVgroupStatus* pStatus = taosHashGet(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
57,486,953✔
694
  if (NULL == pStatus) {
57,486,953✔
695
    mstDebug("vgroup %d not exists in vgroupMap, ignore update upTs", vgId);
51,832,440✔
696
    return;
51,832,440✔
697
  }
698

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

707
      continue;
×
708
    }
709

710
    return;
103,601✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
17,984,158✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
75,037,917✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
57,053,759✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
57,053,759✔
725
  }
726

727
_exit:
17,984,158✔
728

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

733
  return code;
17,984,158✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
379,901✔
739
  int32_t num = taosArrayGetSize(pList);
379,901✔
740
  for (int32_t i = 0; i < num; ++i) {
1,186,026✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
985,680✔
742
    if (pScan->readFromCache) {
985,680✔
743
      return pScan->scanPlan;
179,555✔
744
    }
745
  }
746

747
  return NULL;
200,346✔
748
}
749

750
int32_t msmBuildReaderDeployInfo(SStmTaskDeploy* pDeploy, void* calcScanPlan, SStmStatus* pInfo, bool triggerReader) {
701,022✔
751
  SStreamReaderDeployMsg* pMsg = &pDeploy->msg.reader;
701,022✔
752
  pMsg->triggerReader = triggerReader;
701,022✔
753
  
754
  if (triggerReader) {
701,022✔
755
    SStreamReaderDeployFromTrigger* pTrigger = &pMsg->msg.trigger;
314,491✔
756
    pTrigger->triggerTblName = pInfo->pCreate->triggerTblName;
314,491✔
757
    pTrigger->triggerTblUid = pInfo->pCreate->triggerTblUid;
314,491✔
758
    pTrigger->triggerTblSuid = pInfo->pCreate->triggerTblSuid;
314,491✔
759
    pTrigger->triggerTblType = pInfo->pCreate->triggerTblType;
314,491✔
760
    pTrigger->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pInfo->pCreate->triggerTblType, pInfo->pCreate->flags);
314,491✔
761
    pTrigger->deleteReCalc = pInfo->pCreate->deleteReCalc;
314,491✔
762
    pTrigger->deleteOutTbl = pInfo->pCreate->deleteOutTbl;
314,491✔
763
    pTrigger->partitionCols = pInfo->pCreate->partitionCols;
314,491✔
764
    pTrigger->triggerCols = pInfo->pCreate->triggerCols;
314,491✔
765
    //pTrigger->triggerPrevFilter = pStream->pCreate->triggerPrevFilter;
766
    pTrigger->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
314,491✔
767
    pTrigger->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
314,491✔
768
  } else {
769
    SStreamReaderDeployFromCalc* pCalc = &pMsg->msg.calc;
386,531✔
770
    pCalc->execReplica = pInfo->runnerDeploys * pInfo->runnerReplica;
386,531✔
771
    if (calcScanPlan) {
386,531✔
772
      pCalc->calcScanPlan = taosStrdup(calcScanPlan);
386,531✔
773
      if (NULL == pCalc->calcScanPlan) {
386,531✔
774
        return terrno;
×
775
      }
776
      pCalc->freeScanPlan = true;
386,531✔
777
    }
778
  }
779

780
  return TSDB_CODE_SUCCESS;
701,022✔
781
}
782

783
int32_t msmBuildTriggerRunnerTargets(SMnode* pMnode, SStmStatus* pInfo, int64_t streamId, SArray** ppRes) {
203,027✔
784
  int32_t code = TSDB_CODE_SUCCESS;
203,027✔
785
  int32_t lino = 0;
203,027✔
786

787
  if (pInfo->runnerDeploys > 0) {
203,027✔
788
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
203,027✔
789
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
203,027✔
790
  }
791
  
792
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
812,108✔
793
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
609,081✔
794
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
609,081✔
795

796
    if (!STREAM_IS_TOP_RUNNER(pStatus->flags)) {
609,081✔
797
      mstsError("the last runner task %" PRIx64 " SID:%" PRId64 " tidx:%d in deploy %d is not top runner", 
×
798
          pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.taskIdx, i);
799
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
800
    }
801
    
802
    SStreamRunnerTarget runner;
607,743✔
803
    runner.addr.taskId = pStatus->id.taskId;
609,081✔
804
    runner.addr.nodeId = pStatus->id.nodeId;
609,081✔
805
    runner.addr.epset = mndGetDnodeEpsetById(pMnode, pStatus->id.nodeId);
609,081✔
806
    runner.execReplica = pInfo->runnerReplica; 
609,081✔
807
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &runner), code, lino, _exit, terrno);
1,218,162✔
808
    mstsDebug("the %dth runner target added to trigger's runnerList, TASK:%" PRIx64 , i, runner.addr.taskId);
609,081✔
809
  }
810

811
_exit:
203,027✔
812

813
  if (code) {
203,027✔
814
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
815
  }
816
  
817
  return TSDB_CODE_SUCCESS;
203,027✔
818
}
819

820
int32_t msmBuildStreamSnodeInfo(SMnode* pMnode, SStreamObj* pStream, SStreamSnodeInfo* pInfo) {
×
821
  int64_t streamId = pStream->pCreate->streamId;
×
822
  int32_t leaderSnodeId = atomic_load_32(&pStream->mainSnodeId);
×
823
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, leaderSnodeId);
×
824
  if (NULL == pSnode) {
×
825
    mstsError("snode %d not longer exists, ignore build stream snode info", leaderSnodeId);
×
826
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
827
  }
828
  
829
  pInfo->leaderSnodeId = leaderSnodeId;
×
830
  pInfo->replicaSnodeId = pSnode->replicaId;
×
831

832
  mndReleaseSnode(pMnode, pSnode);
×
833

834
  pInfo->leaderEpSet = mndGetDnodeEpsetById(pMnode, pInfo->leaderSnodeId);
×
835
  if (GOT_SNODE(pInfo->replicaSnodeId)) {
×
836
    pInfo->replicaEpSet = mndGetDnodeEpsetById(pMnode, pInfo->replicaSnodeId);
×
837
  }
838

839
  return TSDB_CODE_SUCCESS;
×
840
}
841

842
int32_t msmBuildTriggerDeployInfo(SMnode* pMnode, SStmStatus* pInfo, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
206,391✔
843
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
844
  int32_t lino = 0;
206,391✔
845
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
846
  SStreamTriggerDeployMsg* pMsg = &pDeploy->msg.trigger;
206,391✔
847
  
848
  pMsg->triggerType = pStream->pCreate->triggerType;
206,391✔
849
  pMsg->igDisorder = pStream->pCreate->igDisorder;
206,391✔
850
  pMsg->fillHistory = pStream->pCreate->fillHistory;
206,391✔
851
  pMsg->fillHistoryFirst = pStream->pCreate->fillHistoryFirst;
206,391✔
852
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
206,391✔
853
  pMsg->igNoDataTrigger = pStream->pCreate->igNoDataTrigger;
206,391✔
854
  pMsg->enableMultiGroupCalc = pStream->pCreate->enableMultiGroupCalc;
206,391✔
855
  pMsg->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags);
206,391✔
856
  pMsg->triggerHasPF = pStream->pCreate->triggerHasPF;
206,391✔
857
  pMsg->isTriggerTblStb = (pStream->pCreate->triggerTblType == TSDB_SUPER_TABLE);
206,391✔
858
  pMsg->precision = pStream->pCreate->triggerPrec;
206,391✔
859
  pMsg->partitionCols = pInfo->pCreate->partitionCols;
206,391✔
860

861
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
206,391✔
862
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
206,391✔
863
  pMsg->addOptions = pStream->pCreate->addOptions;
206,391✔
864
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
206,391✔
865

866
  pMsg->maxDelay = pStream->pCreate->maxDelay;
206,391✔
867
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
206,391✔
868
  pMsg->watermark = pStream->pCreate->watermark;
206,391✔
869
  pMsg->expiredTime = pStream->pCreate->expiredTime;
206,391✔
870
  pMsg->idleTimeoutMs = pStream->pCreate->idleTimeoutMs;
206,391✔
871
  pMsg->trigger = pInfo->pCreate->trigger;
206,391✔
872

873
  pMsg->eventTypes = pStream->pCreate->eventTypes;
206,391✔
874
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
206,391✔
875
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
206,391✔
876
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
206,391✔
877
  pMsg->calcPkSlotId = pStream->pCreate->calcPkSlotId;
206,391✔
878
  pMsg->triPkSlotId = pStream->pCreate->triPkSlotId;
206,391✔
879
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
206,391✔
880
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
206,391✔
881
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
65,410✔
882
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
65,410✔
883
  }
884

885
  SStreamTaskAddr addr;
205,945✔
886
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
206,391✔
887
  if (triggerReaderNum > 0) {
206,391✔
888
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
202,171✔
889
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
202,171✔
890
  }
891
  
892
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
457,385✔
893
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
250,994✔
894
    addr.taskId = pStatus->id.taskId;
250,994✔
895
    addr.nodeId = pStatus->id.nodeId;
250,994✔
896
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
250,994✔
897
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
501,988✔
898
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
250,994✔
899
  }
900

901
  pMsg->leaderSnodeId = pStream->mainSnodeId;
206,391✔
902
  pMsg->streamName = pInfo->streamName;
206,391✔
903
  pMsg->nodelayCreateSubtable = pStream->pCreate->nodelayCreateSubtable;
206,391✔
904

905
  if (0 == pInfo->runnerNum) {
206,391✔
906
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
3,364✔
907
    return code;
3,364✔
908
  }
909

910
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
203,027✔
911

912
_exit:
203,027✔
913

914
  if (code) {
203,027✔
915
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
916
  } else {
917
    mstsDebug("trigger deploy info built, readerNum:%d, runnerNum:%d", (int32_t)taosArrayGetSize(pMsg->readerList), (int32_t)taosArrayGetSize(pMsg->runnerList));
203,027✔
918
  }
919
  
920
  return TSDB_CODE_SUCCESS;
203,027✔
921
}
922

923

924
int32_t msmBuildRunnerDeployInfo(SStmTaskDeploy* pDeploy, SSubplan *plan, SStreamObj* pStream, SStmStatus* pInfo, bool topPlan) {
721,565✔
925
  int32_t code = TSDB_CODE_SUCCESS;
721,565✔
926
  int32_t lino = 0;
721,565✔
927
  int64_t streamId = pStream->pCreate->streamId;
721,565✔
928
  SStreamRunnerDeployMsg* pMsg = &pDeploy->msg.runner;
721,565✔
929
  //TAOS_CHECK_EXIT(qSubPlanToString(plan, &pMsg->pPlan, NULL));
930

931
  pMsg->execReplica = pInfo->runnerReplica;
721,565✔
932
  pMsg->streamName = pInfo->streamName;
721,565✔
933
  //TAOS_CHECK_EXIT(nodesCloneNode((SNode*)plan, (SNode**)&pMsg->pPlan));
934
  pMsg->pPlan = plan;
721,565✔
935
  pMsg->outDBFName = pInfo->pCreate->outDB;
721,565✔
936
  pMsg->outTblName = pInfo->pCreate->outTblName;
721,565✔
937
  pMsg->outTblType = pStream->pCreate->outTblType;
721,565✔
938
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
721,565✔
939
  pMsg->calcNotifyOnly = pStream->pCreate->calcNotifyOnly;
721,565✔
940
  pMsg->topPlan = topPlan;
721,565✔
941
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
721,565✔
942
  pMsg->addOptions = pStream->pCreate->addOptions;
721,565✔
943
  if ((WINDOW_TYPE_INTERVAL == pStream->pCreate->triggerType && pStream->pCreate->trigger.sliding.overlap) ||
721,565✔
944
      (WINDOW_TYPE_COUNT == pStream->pCreate->triggerType &&
708,551✔
945
       pStream->pCreate->trigger.count.sliding < pStream->pCreate->trigger.count.countVal)) {
100,194✔
946
    pMsg->addOptions |= CALC_SLIDING_OVERLAP;
73,395✔
947
  }
948
  pMsg->outCols = pInfo->pCreate->outCols;
721,565✔
949
  pMsg->outTags = pInfo->pCreate->outTags;
721,565✔
950
  pMsg->outStbUid = pStream->pCreate->outStbUid;
721,565✔
951
  pMsg->outStbSversion = pStream->pCreate->outStbSversion;
721,565✔
952
  
953
  pMsg->subTblNameExpr = pInfo->pCreate->subTblNameExpr;
721,565✔
954
  pMsg->tagValueExpr = pInfo->pCreate->tagValueExpr;
721,565✔
955
  pMsg->forceOutCols = pInfo->pCreate->forceOutCols;
721,565✔
956

957
  pMsg->colCids = pInfo->pCreate->colCids;
721,565✔
958
  pMsg->tagCids = pInfo->pCreate->tagCids;
721,565✔
959
_exit:
721,565✔
960

961
  if (code) {
721,565✔
962
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
963
  }
964
  
965
  return code;
721,565✔
966
}
967

968

969
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
478,002✔
970
  int32_t code = TSDB_CODE_SUCCESS;
478,002✔
971
  int32_t lino = 0;
478,002✔
972
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
478,002✔
973
  SListNode* pNode = pList ? listHead(pList) : NULL;
478,002✔
974
  
975
  for (int32_t i = 0; i < taskNum; ++i) {
1,177,666✔
976
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
699,664✔
977
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
699,664✔
978
    if (pNode) {
699,664✔
979
      pNode = TD_DLIST_NODE_NEXT(pNode);
383,450✔
980
    }
981
  }
982
  
983
_exit:
478,002✔
984

985
  if (code) {
478,002✔
986
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
987
  }
988
  
989
  return code;
478,002✔
990
}
991

992

993
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
815,663✔
994
  int32_t code = TSDB_CODE_SUCCESS;
815,663✔
995
  int32_t lino = 0;
815,663✔
996
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
815,663✔
997
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
815,663✔
998
  
999
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,743,619✔
1000
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
927,956✔
1001
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
927,956✔
1002
  }
1003
  
1004
_exit:
815,663✔
1005

1006
  if (code) {
815,663✔
1007
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1008
  }
1009

1010
  return code;
815,663✔
1011
}
1012

1013
int64_t msmAssignTaskId(void) {
1,627,429✔
1014
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,627,429✔
1015
}
1016

1017
int64_t msmAssignTaskSeriousId(void) {
1,627,429✔
1018
  return taosGetTimestampNs();
1,627,429✔
1019
}
1020

1021

1022
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
1,095,971✔
1023
  int32_t code = TSDB_CODE_SUCCESS;
1,095,971✔
1024
  int32_t lino = 0;
1,095,971✔
1025
  bool     noExists = false;
1,095,971✔
1026
  SStmSnodeStatus* pStatus = NULL;
1,095,971✔
1027

1028
  while (true) {
1029
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
1,107,538✔
1030
    if (NULL == pStatus) {
1,107,538✔
1031
      if (noExists) {
11,567✔
1032
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1033
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1034
      }
1035

1036
      noExists = true;
11,567✔
1037
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
11,567✔
1038
      
1039
      continue;
11,567✔
1040
    }
1041

1042
    *alive = (pStatus->runnerThreadNum >= 0);
1,095,971✔
1043
    break;
1,095,971✔
1044
  }
1045

1046
_exit:
1,095,971✔
1047

1048
  if (code) {
1,095,971✔
1049
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1050
  }
1051

1052
  return code;
1,095,971✔
1053
}
1054

1055
int32_t msmRetrieveStaticSnodeId(SMnode* pMnode, SStreamObj* pStream) {
424,340✔
1056
  int32_t code = TSDB_CODE_SUCCESS;
424,340✔
1057
  int32_t lino = 0;
424,340✔
1058
  bool alive = false;
424,340✔
1059
  int32_t mainSnodeId = atomic_load_32(&pStream->mainSnodeId);
424,340✔
1060
  int32_t snodeId = mainSnodeId;
424,340✔
1061
  int64_t streamId = pStream->pCreate->streamId;
424,340✔
1062
  
1063
  while (true) {
1064
    TAOS_CHECK_EXIT(msmIsSnodeAlive(pMnode, snodeId, streamId, &alive));
424,340✔
1065

1066
    if (alive) {
424,340✔
1067
      return snodeId;
424,340✔
1068
    }
1069
    
1070
    if (snodeId == mainSnodeId) {
×
1071
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
1072
      if (NULL == pSnode) {
×
1073
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1074
        return 0;
×
1075
      }
1076
      
1077
      if (pSnode->replicaId <= 0) {
×
1078
        mstsError("no available snode now, mainSnodeId:%d, replicaId:%d", mainSnodeId, pSnode->replicaId);
×
1079
        mndReleaseSnode(pMnode, pSnode);
×
1080
        return 0;
×
1081
      }
1082

1083
      snodeId = pSnode->replicaId;
×
1084
      mndReleaseSnode(pMnode, pSnode);
×
1085
      
1086
      continue;
×
1087
    }
1088

1089
    mstsError("no available snode now, mainSnodeId:%d, followerSnodeId:%d", mainSnodeId, snodeId);
×
1090
    return 0;
×
1091
  }
1092

1093
_exit:
×
1094

1095
  if (code) {
×
1096
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1097
  }
1098

1099
  return 0;
×
1100
}
1101

1102
int32_t msmAssignRandomSnodeId(SMnode* pMnode, int64_t streamId) {
647,090✔
1103
  int32_t code = TSDB_CODE_SUCCESS;
647,090✔
1104
  int32_t lino = 0;
647,090✔
1105
  int32_t snodeIdx = 0;
647,090✔
1106
  int32_t snodeId = 0;
647,090✔
1107
  void      *pIter = NULL;
647,090✔
1108
  SSnodeObj *pObj = NULL;
647,090✔
1109
  bool alive = false;
647,090✔
1110
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
647,090✔
1111
  if (snodeNum <= 0) {
647,090✔
1112
    mstsInfo("no available snode now, num:%d", snodeNum);
2,730✔
1113
    goto _exit;
2,730✔
1114
  }
1115

1116
  int32_t snodeTarget = taosRand() % snodeNum;
644,360✔
1117

1118
  while (1) {
1119
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
671,631✔
1120
    if (pIter == NULL) {
671,631✔
1121
      if (0 == snodeId) {
×
1122
        mstsError("no alive snode now, snodeNum:%d", snodeNum);
×
1123
        break;
×
1124
      }
1125
      
1126
      snodeId = 0;
×
1127
      continue;
×
1128
    }
1129

1130
    code = msmIsSnodeAlive(pMnode, pObj->id, streamId, &alive);
671,631✔
1131
    if (code) {
671,631✔
1132
      sdbRelease(pMnode->pSdb, pObj);
×
1133
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1134
      pObj = NULL;
×
1135
      TAOS_CHECK_EXIT(code);
×
1136
    }
1137
    
1138
    if (!alive) {
671,631✔
1139
      sdbRelease(pMnode->pSdb, pObj);
×
1140
      continue;
×
1141
    }
1142

1143
    snodeId = pObj->id;
671,631✔
1144
    if (snodeIdx == snodeTarget) {
671,631✔
1145
      sdbRelease(pMnode->pSdb, pObj);
644,360✔
1146
      sdbCancelFetch(pMnode->pSdb, pIter);
644,360✔
1147
      pObj = NULL;
644,360✔
1148
      goto _exit;
644,360✔
1149
    }
1150

1151
    sdbRelease(pMnode->pSdb, pObj);
27,271✔
1152
    snodeIdx++;
27,271✔
1153
  }
1154

1155
_exit:
647,090✔
1156

1157
  if (code) {
647,090✔
1158
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1159
  }
1160

1161
  if (0 == snodeId) {
647,090✔
1162
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,730✔
1163
  }
1164

1165
  return snodeId;
647,090✔
1166
}
1167

1168
int32_t msmAssignTaskSnodeId(SMnode* pMnode, SStreamObj* pStream, bool isStatic) {
859,856✔
1169
  int64_t streamId = pStream->pCreate->streamId;
859,856✔
1170
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
859,856✔
1171
  int32_t snodeId = 0;
859,856✔
1172
  if (snodeNum <= 0) {
859,856✔
1173
    mstsInfo("no available snode now, num:%d", snodeNum);
×
1174
    goto _exit;
×
1175
  }
1176

1177
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
859,856✔
1178

1179
_exit:
859,856✔
1180

1181
  if (0 == snodeId) {
859,856✔
1182
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1183
  }
1184

1185
  return snodeId;
859,856✔
1186
}
1187

1188

1189
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
206,391✔
1190
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
1191
  int32_t lino = 0;
206,391✔
1192
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
1193

1194
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
206,391✔
1195
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
206,391✔
1196

1197
  pInfo->triggerTask->id.taskId = pCtx->triggerTaskId;
206,391✔
1198
  pInfo->triggerTask->id.deployId = 0;
206,391✔
1199
  pInfo->triggerTask->id.seriousId = msmAssignTaskSeriousId();
206,391✔
1200
  pInfo->triggerTask->id.nodeId = pCtx->triggerNodeId;
206,391✔
1201
  pInfo->triggerTask->id.taskIdx = 0;
206,391✔
1202
  pInfo->triggerTask->type = STREAM_TRIGGER_TASK;
206,391✔
1203
  pInfo->triggerTask->lastUpTs = pCtx->currTs;
206,391✔
1204
  pInfo->triggerTask->pStream = pInfo;
206,391✔
1205

1206
  SStmTaskDeploy info = {0};
206,391✔
1207
  info.task.type = pInfo->triggerTask->type;
206,391✔
1208
  info.task.streamId = streamId;
206,391✔
1209
  info.task.taskId =  pInfo->triggerTask->id.taskId;
206,391✔
1210
  info.task.seriousId = pInfo->triggerTask->id.seriousId;
206,391✔
1211
  info.task.nodeId =  pInfo->triggerTask->id.nodeId;
206,391✔
1212
  info.task.taskIdx =  pInfo->triggerTask->id.taskIdx;
206,391✔
1213
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pInfo, &info, pStream));
206,391✔
1214
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
206,391✔
1215
  
1216
  (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
206,391✔
1217

1218
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
206,391✔
1219
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
206,391✔
1220

1221
_exit:
206,391✔
1222

1223
  if (code) {
206,391✔
1224
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1225
  }
1226

1227
  return code;
206,391✔
1228
}
1229

1230
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
313,706✔
1231
  int32_t code = TSDB_CODE_SUCCESS;
313,706✔
1232
  int32_t lino = 0;
313,706✔
1233

1234
  pState->id.taskId = msmAssignTaskId();
313,706✔
1235
  pState->id.deployId = 0;
313,706✔
1236
  pState->id.seriousId = msmAssignTaskSeriousId();
313,706✔
1237
  pState->id.nodeId = nodeId;
313,706✔
1238
  pState->id.taskIdx = 0;
313,706✔
1239
  pState->type = STREAM_READER_TASK;
313,706✔
1240
  pState->flags = STREAM_FLAG_TRIGGER_READER;
313,706✔
1241
  pState->status = STREAM_STATUS_UNDEPLOYED;
313,706✔
1242
  pState->lastUpTs = pCtx->currTs;
313,706✔
1243
  pState->pStream = pInfo;
313,706✔
1244
  
1245
  SStmTaskDeploy info = {0};
313,706✔
1246
  info.task.type = pState->type;
313,706✔
1247
  info.task.streamId = streamId;
313,706✔
1248
  info.task.taskId = pState->id.taskId;
313,706✔
1249
  info.task.flags = pState->flags;
313,706✔
1250
  info.task.seriousId = pState->id.seriousId;
313,706✔
1251
  info.task.nodeId = pState->id.nodeId;
313,706✔
1252
  info.task.taskIdx = pState->id.taskIdx;
313,706✔
1253
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, NULL, pInfo, true));
313,706✔
1254
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
313,706✔
1255

1256
_exit:
313,706✔
1257

1258
  if (code) {
313,706✔
1259
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1260
  }
1261

1262
  return code;
313,706✔
1263
}
1264

1265
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
206,391✔
1266
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
1267
  int32_t lino = 0;
206,391✔
1268
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
1269
  SSdb   *pSdb = pCtx->pMnode->pSdb;
206,391✔
1270
  SStmTaskStatus* pState = NULL;
206,391✔
1271
  SVgObj *pVgroup = NULL;
206,391✔
1272
  SDbObj* pDb = NULL;
206,391✔
1273
  
1274
  switch (pStream->pCreate->triggerTblType) {
206,391✔
1275
    case TSDB_NORMAL_TABLE:
119,356✔
1276
    case TSDB_CHILD_TABLE:
1277
    case TSDB_VIRTUAL_CHILD_TABLE:
1278
    case TSDB_VIRTUAL_NORMAL_TABLE: {
1279
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
119,356✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
119,356✔
1281
      pState = taosArrayGet(pInfo->trigReaders, 0);
119,356✔
1282
      
1283
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
119,356✔
1284
      break;
119,356✔
1285
    }
1286
    case TSDB_SUPER_TABLE: {
82,815✔
1287
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
82,815✔
1288
      if (NULL == pDb) {
82,815✔
1289
        code = terrno;
×
1290
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
1291
        goto _exit;
×
1292
      }
1293

1294
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
82,815✔
1295
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
82,815✔
1296
      
1297
      void *pIter = NULL;
82,815✔
1298
      while (1) {
426,008✔
1299
        SStmTaskDeploy info = {0};
508,823✔
1300
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
508,823✔
1301
        if (pIter == NULL) {
508,823✔
1302
          break;
82,815✔
1303
        }
1304
      
1305
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
426,008✔
1306
          pState = taosArrayReserve(pInfo->trigReaders, 1);
131,638✔
1307

1308
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
131,638✔
1309
          if (code) {
131,638✔
1310
            sdbRelease(pSdb, pVgroup);
×
1311
            sdbCancelFetch(pSdb, pIter);
×
1312
            pVgroup = NULL;
×
1313
            TAOS_CHECK_EXIT(code);
×
1314
          }
1315
        }
1316

1317
        sdbRelease(pSdb, pVgroup);
426,008✔
1318
      }
1319
      break;
82,815✔
1320
    }
1321
    default:
4,220✔
1322
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
4,220✔
1323
      break;
4,220✔
1324
  }
1325

1326
_exit:
206,391✔
1327

1328
  mndReleaseDb(pCtx->pMnode, pDb);
206,391✔
1329

1330
  if (code) {
206,391✔
1331
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1332
  }
1333

1334
  return code;
206,391✔
1335
}
1336

1337
int32_t msmUPAddScanTask(SStmGrpCtx* pCtx, SStreamObj* pStream, char* scanPlan, int32_t vgId, int64_t taskId) {
383,641✔
1338
  int32_t code = TSDB_CODE_SUCCESS;
383,641✔
1339
  int32_t lino = 0;
383,641✔
1340
  SSubplan* pSubplan = NULL;
383,641✔
1341
  int64_t streamId = pStream->pCreate->streamId;
383,641✔
1342
  int64_t key[2] = {streamId, 0};
383,641✔
1343
  SStmTaskSrcAddr addr;
383,176✔
1344
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
383,641✔
1345
  addr.isFromCache = false;
383,641✔
1346
  
1347
  if (MNODE_HANDLE == vgId) {
383,641✔
1348
    mndGetMnodeEpSet(pCtx->pMnode, &addr.epset);
×
1349
  } else if (vgId > MNODE_HANDLE) {
383,641✔
1350
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
383,641✔
1351
  } else {
1352
    mstsError("invalid vgId %d in scanPlan", vgId);
×
1353
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1354
  }
1355
  
1356
  addr.taskId = taskId;
383,641✔
1357
  addr.vgId = vgId;
383,641✔
1358
  addr.groupId = pSubplan->id.groupId;
383,641✔
1359

1360
  key[1] = pSubplan->id.subplanId;
383,641✔
1361

1362
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
383,641✔
1363
  if (NULL == ppRes) {
383,641✔
1364
    SArray* pRes = taosArrayInit(1, sizeof(addr));
363,761✔
1365
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
363,761✔
1366
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
727,522✔
1367
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
363,761✔
1368
  } else {
1369
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
39,760✔
1370
  }
1371

1372
  mstsDebug("calcReader %" PRIx64 " added to toUpdateScan, vgId:%d, groupId:%d, subplanId:%d", taskId, vgId, pSubplan->id.groupId, pSubplan->id.subplanId);
383,641✔
1373
  
1374
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
383,641✔
1375
  
1376
_exit:
383,641✔
1377

1378
  nodesDestroyNode((SNode*)pSubplan);
383,641✔
1379

1380
  if (code) {
383,641✔
1381
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1382
  }
1383

1384
  return code;
383,641✔
1385
}
1386

1387
int32_t msmUPAddCacheTask(SStmGrpCtx* pCtx, SStreamCalcScan* pScan, SStreamObj* pStream) {
82,908✔
1388
  int32_t code = TSDB_CODE_SUCCESS;
82,908✔
1389
  int32_t lino = 0;
82,908✔
1390
  SSubplan* pSubplan = NULL;
82,908✔
1391
  int64_t streamId = pStream->pCreate->streamId;
82,908✔
1392
  int64_t key[2] = {streamId, 0};
82,908✔
1393
  TAOS_CHECK_EXIT(nodesStringToNode(pScan->scanPlan, (SNode**)&pSubplan));
82,908✔
1394

1395
  SStmTaskSrcAddr addr;
82,908✔
1396
  addr.isFromCache = true;
82,908✔
1397
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
82,908✔
1398
  addr.taskId = pCtx->triggerTaskId;
82,908✔
1399
  addr.vgId = pCtx->triggerNodeId;
82,908✔
1400
  addr.groupId = pSubplan->id.groupId;
82,908✔
1401

1402
  key[1] = pSubplan->id.subplanId;
82,908✔
1403
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
82,908✔
1404
  if (NULL == ppRes) {
82,908✔
1405
    SArray* pRes = taosArrayInit(1, sizeof(addr));
82,908✔
1406
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
82,908✔
1407
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
165,816✔
1408
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
82,908✔
1409
  } else {
1410
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1411
  }
1412
  
1413
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
82,908✔
1414
  
1415
_exit:
82,908✔
1416

1417
  nodesDestroyNode((SNode*)pSubplan);
82,908✔
1418
  
1419
  if (code) {
82,908✔
1420
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1421
  }
1422

1423
  return code;
82,908✔
1424
}
1425

1426

1427
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
385,958✔
1428
  int32_t code = TSDB_CODE_SUCCESS;
385,958✔
1429
  int32_t lino = 0;
385,958✔
1430

1431
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
385,958✔
1432

1433
  pState->id.taskId = msmAssignTaskId();
385,958✔
1434
  pState->id.deployId = 0;
385,958✔
1435
  pState->id.seriousId = msmAssignTaskSeriousId();
385,958✔
1436
  pState->id.nodeId = nodeId;
385,958✔
1437
  pState->id.taskIdx = taskIdx;
385,958✔
1438
  pState->type = STREAM_READER_TASK;
385,958✔
1439
  pState->flags = 0;
385,958✔
1440
  pState->status = STREAM_STATUS_UNDEPLOYED;
385,958✔
1441
  pState->lastUpTs = pCtx->currTs;
385,958✔
1442
  pState->pStream = pInfo;
385,958✔
1443
  
1444
  SStmTaskDeploy info = {0};
385,958✔
1445
  info.task.type = pState->type;
385,958✔
1446
  info.task.streamId = streamId;
385,958✔
1447
  info.task.taskId = pState->id.taskId;
385,958✔
1448
  info.task.flags = pState->flags;
385,958✔
1449
  info.task.seriousId = pState->id.seriousId;
385,958✔
1450
  info.task.nodeId = pState->id.nodeId;
385,958✔
1451
  info.task.taskIdx = pState->id.taskIdx;
385,958✔
1452
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, calcScanPlan, pInfo, false));
385,958✔
1453
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
385,958✔
1454

1455
_exit:
385,958✔
1456

1457
  if (code) {
385,958✔
1458
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1459
  }
1460

1461
  return code;
385,958✔
1462
}
1463

1464

1465
static int32_t msmTDAddCalcReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
206,391✔
1466
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
1467
  int32_t lino = 0;
206,391✔
1468
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
206,391✔
1469
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
1470
  SStmTaskStatus* pState = NULL;
206,391✔
1471
  pInfo->calcReaders = tdListNew(sizeof(SStmTaskStatus));
206,391✔
1472
  TSDB_CHECK_NULL(pInfo->calcReaders, code, lino, _exit, terrno);
206,391✔
1473

1474
  
1475
  for (int32_t i = 0; i < calcTasksNum; ++i) {
672,749✔
1476
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
466,358✔
1477
    if (pScan->readFromCache) {
466,358✔
1478
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
82,908✔
1479
      continue;
82,908✔
1480
    }
1481
    
1482
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
383,450✔
1483
    for (int32_t m = 0; m < vgNum; ++m) {
766,900✔
1484
      pState = tdListReserve(pInfo->calcReaders);
383,450✔
1485
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
383,450✔
1486

1487
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
383,450✔
1488
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
383,450✔
1489
    }
1490
  }
1491

1492
_exit:
206,391✔
1493

1494
  if (code) {
206,391✔
1495
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1496
  }
1497

1498
  return code;
206,391✔
1499
}
1500

1501

1502

1503
static int32_t msmUPPrepareReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
191✔
1504
  int32_t code = TSDB_CODE_SUCCESS;
191✔
1505
  int32_t lino = 0;
191✔
1506
  int64_t streamId = pStream->pCreate->streamId;
191✔
1507
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
191✔
1508
  if (calcTasksNum <= 0) {
191✔
1509
    mstsDebug("no calc scan plan, ignore parepare reader tasks, readerNum:%d", (int32_t)MST_LIST_SIZE(pInfo->calcReaders));
×
1510
    return code;    
×
1511
  }
1512
  
1513
  SListNode* pNode = listHead(pInfo->calcReaders);
191✔
1514
  
1515
  for (int32_t i = 0; i < calcTasksNum; ++i) {
382✔
1516
    SStreamCalcScan* pScan = taosArrayGet(pStream->pCreate->calcScanPlanList, i);
191✔
1517
    if (pScan->readFromCache) {
191✔
1518
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
×
1519
      continue;
×
1520
    }
1521
    
1522
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
191✔
1523
    for (int32_t m = 0; m < vgNum; ++m) {
382✔
1524
      SStmTaskStatus* pReader = (SStmTaskStatus*)pNode->data;
191✔
1525
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pReader->id.nodeId, pReader->id.taskId));
191✔
1526
      pNode = TD_DLIST_NODE_NEXT(pNode);
191✔
1527
    }
1528
  }
1529

1530
_exit:
191✔
1531

1532
  if (code) {
191✔
1533
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1534
  }
1535

1536
  return code;
191✔
1537
}
1538

1539
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
206,391✔
1540
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
1541
  int32_t lino = 0;
206,391✔
1542
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
1543
  
1544
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
206,391✔
1545
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
206,391✔
1546

1547
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
206,391✔
1548
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
206,391✔
1549
  
1550
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
206,391✔
1551
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
206,391✔
1552
  
1553
_exit:
206,391✔
1554

1555
  if (code) {
206,391✔
1556
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1557
  }
1558
  
1559
  return code;
206,391✔
1560
}
1561

1562
int32_t msmUpdatePlanSourceAddr(SStreamTask* pTask, int64_t streamId, SSubplan* plan, int64_t clientId, SStmTaskSrcAddr* pSrc, int32_t msgType, int64_t srcSubplanId) {
1,594,367✔
1563
  SDownstreamSourceNode source = {
1,594,367✔
1564
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1565
      .clientId = clientId,
1566
      .taskId = pSrc->taskId,
1,594,367✔
1567
      .sId = 0,
1568
      .execId = 0,
1569
      .fetchMsgType = msgType,
1570
      .localExec = false,
1571
  };
1572

1573
  source.addr.epSet = pSrc->epset;
1,594,367✔
1574
  source.addr.nodeId = pSrc->vgId;
1,594,367✔
1575

1576
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
1,594,367✔
1577
      plan->id.subplanId, pSrc->groupId, srcSubplanId, source.clientId, source.taskId, source.addr.nodeId, TMSG_INFO(source.fetchMsgType));
1578
  
1579
  return qSetSubplanExecutionNode(plan, pSrc->groupId, &source);
1,594,367✔
1580
}
1581

1582
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
68,100✔
1583
  int64_t streamId = pStream->pCreate->streamId;
68,100✔
1584
  int32_t runnerNum = taosArrayGetSize(pRunners);
68,100✔
1585
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
209,535✔
1586
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
209,535✔
1587
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
209,535✔
1588
    if (pPlan->id.subplanId == subplanId) {
209,535✔
1589
      *taskId = pDeploy->task.taskId;
68,100✔
1590
      *ppParent = &pDeploy->task;
68,100✔
1591
      return TSDB_CODE_SUCCESS;
68,100✔
1592
    }
1593
  }
1594

1595
  mstsError("subplanId %d not found in runner list", subplanId);
×
1596

1597
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1598
}
1599

1600
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
721,565✔
1601
  int32_t code = TSDB_CODE_SUCCESS;
721,565✔
1602
  int32_t lino = 0;
721,565✔
1603
  int64_t key[2] = {streamId, -1};
721,565✔
1604
  SNode* pNode = NULL;
721,565✔
1605
  SStreamTask* pTask = &pDeploy->task;
721,565✔
1606
  FOREACH(pNode, pPlan->pChildren) {
2,188,930✔
1607
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,467,365✔
1608
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
68,100✔
1609
      continue;
68,100✔
1610
    }
1611
    
1612
    SValueNode* pVal = (SValueNode*)pNode;
1,399,265✔
1613
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,399,265✔
1614
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1615
      continue;
×
1616
    }
1617

1618
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,399,265✔
1619

1620
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
1,399,265✔
1621
    if (NULL == ppRes) {
1,399,265✔
1622
      msttError("lowest runner subplan ID:%d,%d can't get its child ID:%" PRId64 " addr", pPlan->id.groupId, pPlan->id.subplanId, key[1]);
×
1623
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1624
    }
1625

1626
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,399,265✔
1627
    for (int32_t i = 0; i < childrenNum; ++i) {
2,925,532✔
1628
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,526,267✔
1629
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,526,267✔
1630
    }
1631
  }
1632

1633
_exit:
721,565✔
1634

1635
  if (code) {
721,565✔
1636
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1637
  }
1638

1639
  return code;
721,565✔
1640
}
1641

1642
int32_t msmUpdateRunnerPlan(SStmGrpCtx* pCtx, SArray* pRunners, int32_t beginIdx, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
721,565✔
1643
  int32_t code = TSDB_CODE_SUCCESS;
721,565✔
1644
  int32_t lino = 0;
721,565✔
1645
  SSubplan* pPlan = pDeploy->msg.runner.pPlan;
721,565✔
1646
  SStreamTask* pTask = &pDeploy->task;
721,565✔
1647
  SStreamTask* parentTask = NULL;
721,565✔
1648
  int64_t streamId = pStream->pCreate->streamId;
721,565✔
1649

1650
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
721,565✔
1651

1652
  SNode* pTmp = NULL;
721,565✔
1653
  WHERE_EACH(pTmp, pPlan->pChildren) {
2,188,930✔
1654
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,467,365✔
1655
      ERASE_NODE(pPlan->pChildren);
1,399,265✔
1656
      continue;
1,399,265✔
1657
    }
1658
    WHERE_NEXT;
68,100✔
1659
  }
1660
  nodesClearList(pPlan->pChildren);
721,565✔
1661
  pPlan->pChildren = NULL;
721,565✔
1662

1663
  if (NULL == pPlan->pParents) {
721,565✔
1664
    goto _exit;
653,465✔
1665
  }
1666

1667
  SNode* pNode = NULL;
68,100✔
1668
  int64_t parentTaskId = 0;
68,100✔
1669
  SStmTaskSrcAddr addr = {0};
68,100✔
1670
  addr.taskId = pDeploy->task.taskId;
68,100✔
1671
  addr.vgId = pDeploy->task.nodeId;
68,100✔
1672
  addr.groupId = pPlan->id.groupId;
68,100✔
1673
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
68,100✔
1674
  FOREACH(pNode, pPlan->pParents) {
136,200✔
1675
    SSubplan* pSubplan = (SSubplan*)pNode;
68,100✔
1676
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
68,100✔
1677
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
68,100✔
1678
  }
1679
  
1680
_exit:
720,227✔
1681

1682
  if (code) {
721,565✔
1683
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1684
  }
1685

1686
  return code;
721,565✔
1687
}
1688

1689
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
653,465✔
1690
  int32_t code = TSDB_CODE_SUCCESS;
653,465✔
1691
  int32_t lino = 0;
653,465✔
1692
  int64_t streamId = pStream->pCreate->streamId;
653,465✔
1693
  int32_t runnerNum = taosArrayGetSize(pRunners);
653,465✔
1694
  
1695
  for (int32_t i = 0; i < runnerNum; ++i) {
1,375,030✔
1696
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
721,565✔
1697
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
721,565✔
1698
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
721,565✔
1699

1700
    SStreamTask* pTask = &pDeploy->task;
721,565✔
1701
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
721,565✔
1702
  }
1703

1704
_exit:
653,465✔
1705

1706
  if (code) {
653,465✔
1707
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1708
  }
1709

1710
  return code;
653,465✔
1711
}
1712

1713
static int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, int32_t dagIdx, SStmStatus* pInfo, SStreamObj* pStream, SQueryPlan** pRoot, SNodeList** subEP) {
217,758✔
1714
  int32_t code = 0;
217,758✔
1715
  int32_t lino = 0;
217,758✔
1716
  int64_t streamId = pStream->pCreate->streamId;
217,758✔
1717
  SArray* deployTaskList = NULL;
217,758✔
1718
  SArray* deployList = NULL;
217,758✔
1719
  int32_t deployNodeId = 0;
217,758✔
1720
  SStmTaskStatus* pState = NULL;
217,758✔
1721
  int32_t taskIdx = 0;
217,758✔
1722
  SNodeListNode *plans = NULL;
217,758✔
1723
  SQueryPlan    *pDag = NULL;
217,758✔
1724
  int32_t        taskNum = 0;
217,758✔
1725
  int32_t        totalTaskNum = 0;
217,758✔
1726
  bool           subQ = false;
217,758✔
1727

1728
  if (dagIdx >= 0) {
217,758✔
1729
    subQ = true;
14,731✔
1730
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
14,731✔
1731
  } else {
1732
    pDag = *pRoot;
203,027✔
1733
  }
1734

1735
  if (pDag->numOfSubplans <= 0) {
217,758✔
1736
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1737
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1738
  }
1739

1740
  if ((*pRoot)->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
217,758✔
1741
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1742
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1743
  }
1744

1745
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
217,758✔
1746
  if (levelNum <= 0) {
217,758✔
1747
    mstsError("invalid level num:%d", levelNum);
×
1748
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1749
  }
1750

1751
  int32_t        lowestLevelIdx = levelNum - 1;
217,758✔
1752
  
1753
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
217,758✔
1754
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
217,758✔
1755
    mstsError("invalid level plan, level:0, planNodeType:%d", nodeType(plans));
×
1756
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1757
  }
1758
  
1759
  taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
217,758✔
1760
  if (taskNum != 1) {
217,758✔
1761
    mstsError("invalid level plan number:%d, level:0", taskNum);
×
1762
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1763
  }
1764

1765
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
217,758✔
1766
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
217,758✔
1767
  
1768
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
871,032✔
1769
    totalTaskNum = 0;
653,274✔
1770

1771
    deployList = pInfo->runners[deployId];
653,274✔
1772
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
653,274✔
1773
    if (!GOT_SNODE(deployNodeId)) {
653,274✔
1774
      TAOS_CHECK_EXIT(terrno);
×
1775
    }
1776

1777
    taskIdx = 0;
653,274✔
1778

1779
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,330,686✔
1780
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
677,412✔
1781
      if (NULL == plans) {
677,412✔
1782
        mstsError("empty level plan, level:%d", i);
×
1783
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1784
      }
1785

1786
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
677,412✔
1787
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1788
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1789
      }
1790

1791
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
677,412✔
1792
      if (taskNum <= 0) {
677,412✔
1793
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1794
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1795
      }
1796

1797
      totalTaskNum += taskNum;
677,412✔
1798
      if (totalTaskNum > pDag->numOfSubplans) {
677,412✔
1799
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1800
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1801
      }
1802

1803
      for (int32_t n = 0; n < taskNum; ++n) {
1,398,786✔
1804
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
721,374✔
1805
        pState = taosArrayReserve(deployList, 1);
721,374✔
1806

1807
        pState->id.taskId = msmAssignTaskId();
721,374✔
1808
        pState->id.deployId = deployId;
721,374✔
1809
        pState->id.seriousId = msmAssignTaskSeriousId();
721,374✔
1810
        pState->id.nodeId = deployNodeId;
721,374✔
1811
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
721,374✔
1812
        pState->type = STREAM_RUNNER_TASK;
721,374✔
1813
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
721,374✔
1814
        pState->status = STREAM_STATUS_UNDEPLOYED;
721,374✔
1815
        pState->lastUpTs = pCtx->currTs;
721,374✔
1816
        pState->pStream = pInfo;
721,374✔
1817

1818
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
721,374✔
1819
        pDeploy->task.type = pState->type;
721,374✔
1820
        pDeploy->task.streamId = streamId;
721,374✔
1821
        pDeploy->task.taskId = pState->id.taskId;
721,374✔
1822
        pDeploy->task.flags = pState->flags;
721,374✔
1823
        pDeploy->task.seriousId = pState->id.seriousId;
721,374✔
1824
        pDeploy->task.deployId = pState->id.deployId;
721,374✔
1825
        pDeploy->task.nodeId = pState->id.nodeId;
721,374✔
1826
        pDeploy->task.taskIdx = pState->id.taskIdx;
721,374✔
1827
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i && !subQ));
721,374✔
1828

1829
        SStreamTask* pTask = &pDeploy->task;
721,374✔
1830
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
721,374✔
1831
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1832

1833
        if (subQ) {
721,374✔
1834
          if (i == 0) {
62,373✔
1835
            SStmTaskSrcAddr addr = {0};
44,193✔
1836
            SDownstreamSourceNode* pSource = NULL;
44,193✔
1837

1838
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
44,193✔
1839

1840
            addr.taskId = pDeploy->task.taskId;
44,193✔
1841
            addr.vgId = pDeploy->task.nodeId;
44,193✔
1842
            addr.groupId = plan->id.groupId;
44,193✔
1843
            addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
44,193✔
1844

1845
            pSource->addr.epSet = addr.epset;
44,193✔
1846
            pSource->addr.nodeId = addr.vgId;
44,193✔
1847

1848
            pSource->clientId = streamId;
44,193✔
1849
            pSource->taskId = pDeploy->task.taskId;
44,193✔
1850
            pSource->sId = 0;
44,193✔
1851
            pSource->execId = 0;
44,193✔
1852
            pSource->fetchMsgType = TDMT_STREAM_FETCH_FROM_RUNNER;
44,193✔
1853
            pSource->localExec = false;
44,193✔
1854

1855
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
44,193✔
1856
          }
1857
        } else {
1858
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
659,001✔
1859
        }
1860
      }
1861

1862
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
677,412✔
1863
    }
1864

1865
    if (totalTaskNum != pDag->numOfSubplans) {
653,274✔
1866
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1867
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1868
    }
1869

1870
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
653,274✔
1871

1872
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
653,274✔
1873

1874
    nodesDestroyNode((SNode *)(*pRoot));
653,274✔
1875
    *pRoot = NULL;
653,274✔
1876
    
1877
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
653,274✔
1878
    if (subQ) {
653,274✔
1879
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
44,193✔
1880
    } else {
1881
      pDag = *pRoot;
609,081✔
1882
    }
1883

1884
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
653,274✔
1885
  }
1886

1887
  if (!subQ) {
217,758✔
1888
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
812,108✔
1889
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
609,081✔
1890
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
609,081✔
1891
    }
1892
  }
1893
  
1894
  pInfo->runnerNum += totalTaskNum;
217,758✔
1895
  
1896
_exit:
217,758✔
1897

1898
  if (code) {
217,758✔
1899
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1900
  }
1901

1902
  taosArrayDestroy(deployTaskList);
217,758✔
1903

1904
  return code;
217,758✔
1905
}
1906

1907
int32_t msmReBuildRunnerTasks(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream, SStmTaskAction* pAction) {
191✔
1908
  int32_t code = 0;
191✔
1909
  int32_t lino = 0;
191✔
1910
  int64_t streamId = pStream->pCreate->streamId;
191✔
1911
  int32_t newNodeId = 0;
191✔
1912
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
191✔
1913
  int32_t        lowestLevelIdx = levelNum - 1;
191✔
1914
  SNodeListNode *plans = NULL;
191✔
1915
  int32_t        taskNum = 0;
191✔
1916
  int32_t        totalTaskNum = 0;
191✔
1917
  int32_t        deployId = 0;
191✔
1918
  SStmTaskStatus* pRunner = NULL;
191✔
1919
  SStmTaskStatus* pStartRunner = NULL;
191✔
1920
  int32_t taskIdx = 0;
191✔
1921
  SArray* deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
191✔
1922
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
191✔
1923

1924
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
382✔
1925
    deployId = pAction->deployId[r];
191✔
1926

1927
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
191✔
1928

1929
    pStartRunner = pRunner;
191✔
1930
    totalTaskNum = 0;
191✔
1931

1932
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
191✔
1933
    if (!GOT_SNODE(newNodeId)) {
191✔
1934
      TAOS_CHECK_EXIT(terrno);
×
1935
    }
1936

1937
    taskIdx = 0;
191✔
1938
    
1939
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
382✔
1940
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
191✔
1941
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
191✔
1942
      totalTaskNum += taskNum;
191✔
1943

1944
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
191✔
1945
      
1946
      for (int32_t n = 0; n < taskNum; ++n) {
382✔
1947
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
191✔
1948

1949
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
191✔
1950
        if (pRunner->id.taskIdx != newTaskIdx) {
191✔
1951
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1952
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1953
        }
1954

1955
        pRunner->id.nodeId = newNodeId;
191✔
1956

1957
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
191✔
1958
        pDeploy->task.type = pRunner->type;
191✔
1959
        pDeploy->task.streamId = streamId;
191✔
1960
        pDeploy->task.taskId = pRunner->id.taskId;
191✔
1961
        pDeploy->task.flags = pRunner->flags;
191✔
1962
        pDeploy->task.seriousId = pRunner->id.seriousId;
191✔
1963
        pDeploy->task.nodeId = pRunner->id.nodeId;
191✔
1964
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
191✔
1965
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
191✔
1966

1967
        pRunner++;
191✔
1968
      }
1969

1970
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
191✔
1971
    }
1972

1973
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
191✔
1974

1975
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
191✔
1976

1977
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
191✔
1978

1979
    nodesDestroyNode((SNode *)pDag);
191✔
1980
    pDag = NULL;
191✔
1981

1982
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
191✔
1983
  }
1984

1985
_exit:
191✔
1986

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

1991
  nodesDestroyNode((SNode *)pDag);
191✔
1992
  taosArrayDestroy(deployTaskList);
191✔
1993

1994
  return code;
191✔
1995
}
1996

1997

1998
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
196,329✔
1999
  int32_t code = TSDB_CODE_SUCCESS;
196,329✔
2000
  int32_t lino = 0;
196,329✔
2001
  //STREAMTODO 
2002
  
2003
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
196,329✔
2004
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
196,329✔
2005

2006
_exit:
196,329✔
2007

2008
  if (code) {
196,329✔
2009
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2010
  }
2011

2012
  return code;
196,329✔
2013
}
2014

2015

2016
static int32_t msmUpdateCalcReaderTasks(SStreamObj* pStream, SNodeList* pSubEP) {
203,027✔
2017
  int32_t   code = TSDB_CODE_SUCCESS;
203,027✔
2018
  int32_t   lino = 0;
203,027✔
2019
  int64_t   streamId = pStream->pCreate->streamId;
203,027✔
2020
  void*     pIter = NULL;
203,027✔
2021
  SSubplan* pSubplan = NULL;
203,027✔
2022

2023
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
858,748✔
2024
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
655,721✔
2025
    (void)mstWaitLock(&pVg->lock, true);
655,721✔
2026

2027
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
655,721✔
2028
    if (atomic_load_32(&pVg->deployed) == taskNum) {
655,721✔
2029
      taosRUnLockLatch(&pVg->lock);
×
2030
      continue;
×
2031
    }
2032

2033
    for (int32_t i = 0; i < taskNum; ++i) {
6,320,603✔
2034
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
5,664,882✔
2035
      if (pExt->deployed || pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
5,664,882✔
2036
        continue;
5,033,802✔
2037
      }
2038

2039
      if (!pExt->deploy.msg.reader.triggerReader) {
631,080✔
2040
        SStreamReaderDeployFromCalc* pCalcReaderDeploy = &pExt->deploy.msg.reader.msg.calc;
383,450✔
2041
        TAOS_CHECK_EXIT(nodesStringToNode(pCalcReaderDeploy->calcScanPlan, (SNode**)&pSubplan));
383,450✔
2042
        TAOS_CHECK_EXIT(nodesCloneList(pSubEP, &pSubplan->pSubQ));
383,450✔
2043
        
2044
        // Free old calcScanPlan before nodesNodeToString overwrites the pointer
2045
        if (pCalcReaderDeploy->freeScanPlan) {
383,450✔
2046
          taosMemoryFreeClear(pCalcReaderDeploy->calcScanPlan);
383,450✔
2047
        }
2048
        TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pSubplan, false, (char**)&pCalcReaderDeploy->calcScanPlan, NULL));
383,450✔
2049
        pCalcReaderDeploy->freeScanPlan = true;
383,450✔
2050
        nodesDestroyNode((SNode *)pSubplan);
383,450✔
2051
      }
2052
    }
2053

2054
    taosRUnLockLatch(&pVg->lock);
655,721✔
2055
  }
2056

2057
_exit:
203,027✔
2058
  if (code) {
203,027✔
2059
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2060
  }
2061

2062
  return code;
203,027✔
2063
}
2064

2065
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
206,391✔
2066
  if (NULL == pStream->pCreate->calcPlan) {
206,391✔
2067
    return TSDB_CODE_SUCCESS;
3,364✔
2068
  }
2069

2070
  int32_t code = TSDB_CODE_SUCCESS;
203,027✔
2071
  int32_t lino = 0;
203,027✔
2072
  int64_t streamId = pStream->pCreate->streamId;
203,027✔
2073
  SQueryPlan* pPlan = NULL;
203,027✔
2074
  SNodeList*  pSubEP = NULL;
203,027✔
2075
  SNode*      pNode = NULL;
203,027✔
2076
  int32_t     subQueryPlans = 0;
203,027✔
2077

2078
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
203,027✔
2079

2080
  FOREACH(pNode, pPlan->pChildren) {
217,758✔
2081
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
14,731✔
2082

2083
    subQueryPlans += calcSubQPlan->numOfSubplans;
14,731✔
2084
  }
2085

2086
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
812,108✔
2087
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans + subQueryPlans, sizeof(SStmTaskStatus));
609,081✔
2088
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
609,081✔
2089
  }
2090

2091
  for (int32_t i = 0; i < LIST_LENGTH(pPlan->pChildren); ++i) {
217,758✔
2092
    code = msmBuildRunnerTasksImpl(pCtx, i, pInfo, pStream, &pPlan, &pSubEP);
14,731✔
2093
    TAOS_CHECK_EXIT(code);
14,731✔
2094
  }
2095

2096
  code = msmBuildRunnerTasksImpl(pCtx, -1, pInfo, pStream, &pPlan, &pSubEP);
203,027✔
2097
  TAOS_CHECK_EXIT(code);
203,027✔
2098

2099
  taosHashClear(mStreamMgmt.toUpdateScanMap);
203,027✔
2100
  mStreamMgmt.toUpdateScanNum = 0;
203,027✔
2101

2102
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
203,027✔
2103

2104
_exit:
203,027✔
2105
  nodesDestroyNode((SNode *)pPlan);
203,027✔
2106
  if (pSubEP) {
203,027✔
2107
    nodesDestroyList(pSubEP);
12,939✔
2108
  }
2109

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

2114
  return code;
203,027✔
2115
}
2116

2117
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
206,391✔
2118
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
2119
  int32_t lino = 0;
206,391✔
2120
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
2121

2122
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
206,391✔
2123

2124
  pCtx->triggerTaskId = msmAssignTaskId();
206,391✔
2125
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
206,391✔
2126
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
206,391✔
2127
    TAOS_CHECK_EXIT(terrno);
×
2128
  }
2129

2130
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
206,391✔
2131
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
206,391✔
2132
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
206,391✔
2133
  
2134
_exit:
206,391✔
2135

2136
  if (code) {
206,391✔
2137
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2138
  }
2139

2140
  return code;
206,391✔
2141
}
2142

2143
static int32_t msmInitTrigReaderList(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
×
2144
  int32_t code = TSDB_CODE_SUCCESS;
×
2145
  int32_t lino = 0;
×
2146
  int64_t streamId = pStream->pCreate->streamId;
×
2147
  SSdb   *pSdb = pCtx->pMnode->pSdb;
×
2148
  SStmTaskStatus* pState = NULL;
×
2149
  SDbObj* pDb = NULL;
×
2150
  
2151
  switch (pStream->pCreate->triggerTblType) {
×
2152
    case TSDB_NORMAL_TABLE:
×
2153
    case TSDB_CHILD_TABLE:
2154
    case TSDB_VIRTUAL_CHILD_TABLE:
2155
    case TSDB_VIRTUAL_NORMAL_TABLE: {
2156
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
×
2157
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2158
      pInfo->trigReaderNum = 1;
×
2159
      break;
×
2160
    }
2161
    case TSDB_SUPER_TABLE: {
×
2162
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
×
2163
      if (NULL == pDb) {
×
2164
        code = terrno;
×
2165
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
2166
        goto _exit;
×
2167
      }
2168

2169
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
2170
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2171
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2172
      mndReleaseDb(pCtx->pMnode, pDb);
×
2173
      pDb = NULL;
×
2174
      break;
×
2175
    }
2176
    default:
×
2177
      pInfo->trigReaderNum = 0;
×
2178
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2179
      break;
×
2180
  }
2181

2182
_exit:
×
2183

2184
  if (code) {
×
2185
    mndReleaseDb(pCtx->pMnode, pDb);
×
2186
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2187
  }
2188

2189
  return code;
×
2190
}
2191

2192

2193
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
199,693✔
2194
  int32_t code = TSDB_CODE_SUCCESS;
199,693✔
2195
  int32_t lino = 0;
199,693✔
2196
  int64_t streamId = pStream->pCreate->streamId;
199,693✔
2197

2198
  pStatus->lastActionTs = INT64_MIN;
199,693✔
2199

2200
  if (NULL == pStatus->streamName) {
199,693✔
2201
    pStatus->streamName = taosStrdup(pStream->name);
199,693✔
2202
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
199,693✔
2203
  }
2204

2205
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
199,693✔
2206
  
2207
  if (pStream->pCreate->numOfCalcSubplan > 0) {
199,693✔
2208
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
196,329✔
2209
    
2210
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
196,329✔
2211
  }
2212

2213
  if (initList) {
199,693✔
2214
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2215

2216
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
2217
    if (subPlanNum > 0) {
×
2218
      pStatus->calcReaderNum = subPlanNum;
×
2219
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2220
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2221
    }
2222

2223
    if (pStatus->runnerNum > 0) {
×
2224
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2225
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2226
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2227
      }
2228
    }
2229
  }
2230
  
2231
_exit:
199,693✔
2232

2233
  if (code) {
199,693✔
2234
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2235
  }
2236

2237
  return code;
199,693✔
2238
}
2239

2240
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
206,391✔
2241
  int32_t code = TSDB_CODE_SUCCESS;
206,391✔
2242
  int32_t lino = 0;
206,391✔
2243
  int64_t streamId = pStream->pCreate->streamId;
206,391✔
2244
  SStmStatus info = {0};
206,391✔
2245

2246
  if (NULL == pStatus) {
206,391✔
2247
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
199,693✔
2248

2249
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
199,693✔
2250

2251
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
199,693✔
2252
  }
2253
  
2254
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
206,391✔
2255

2256
  mstLogSStmStatus("stream deployed", streamId, pStatus);
206,391✔
2257

2258
_exit:
206,391✔
2259

2260
  if (code) {
206,391✔
2261
    if (NULL != pStatus) {
×
2262
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2263
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2264
    }
2265
    
2266
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2267
  }
2268

2269
  return code;
206,391✔
2270
}
2271

2272

2273
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
7,721✔
2274
  int32_t code = TSDB_CODE_SUCCESS;
7,721✔
2275
  void* pIter = NULL;
7,721✔
2276

2277
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
15,246✔
2278
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
7,525✔
2279
    (void)mstWaitLock(&pVg->lock, true);
7,525✔
2280

2281
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
7,525✔
2282
    if (atomic_load_32(&pVg->deployed) == taskNum) {
7,525✔
2283
      taosRUnLockLatch(&pVg->lock);
×
2284
      continue;
×
2285
    }
2286

2287
    for (int32_t i = 0; i < taskNum; ++i) {
33,109✔
2288
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
25,584✔
2289
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
25,584✔
2290
        continue;
25,584✔
2291
      }
2292

2293
      mstDestroySStmTaskToDeployExt(pExt);
×
2294
      pExt->deployed = true;
×
2295
    }
2296
    
2297
    taosRUnLockLatch(&pVg->lock);
7,525✔
2298
  }
2299

2300
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
13,594✔
2301
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
5,873✔
2302
    (void)mstWaitLock(&pSnode->lock, true);
5,873✔
2303

2304
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
5,873✔
2305
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
5,873✔
2306
      for (int32_t i = 0; i < taskNum; ++i) {
15,412✔
2307
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
10,685✔
2308
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
10,685✔
2309
          continue;
10,685✔
2310
        }
2311
        
2312
        mstDestroySStmTaskToDeployExt(pExt);
×
2313
        pExt->deployed = true;
×
2314
      }
2315
    }
2316

2317
    taskNum = taosArrayGetSize(pSnode->runnerList);
5,873✔
2318
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
5,873✔
2319
      for (int32_t i = 0; i < taskNum; ++i) {
37,928✔
2320
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
32,055✔
2321
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
32,055✔
2322
          continue;
32,055✔
2323
        }
2324
        
2325
        mstDestroySStmTaskToDeployExt(pExt);
×
2326
        pExt->deployed = true;
×
2327
      }
2328
    }
2329

2330
    taosRUnLockLatch(&pSnode->lock);
5,873✔
2331
  }
2332

2333
  
2334
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
21,745✔
2335
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
14,024✔
2336
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
14,024✔
2337
    if (TSDB_CODE_SUCCESS == code) {
14,024✔
2338
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
8,867✔
2339
    }
2340
  }
2341

2342
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
34,998✔
2343
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
27,277✔
2344
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
27,277✔
2345
    if (TSDB_CODE_SUCCESS == code) {
27,277✔
2346
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
12,074✔
2347
    }
2348
  }
2349

2350
  size_t keyLen = 0;
7,721✔
2351
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
563,741✔
2352
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
556,020✔
2353
    if (*pStreamId == streamId) {
556,020✔
2354
      int64_t taskId = *(pStreamId + 1);
46,274✔
2355
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
46,274✔
2356
      if (code) {
46,274✔
2357
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2358
      } else {
2359
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
46,274✔
2360
      }
2361
    }
2362
  }
2363

2364
  if (fromStreamMap) {
7,721✔
2365
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
450✔
2366
    if (code) {
450✔
2367
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2368
    } else {
2369
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
450✔
2370
    }
2371
  }
2372
  
2373
  return code;
7,721✔
2374
}
2375

2376
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
7,271✔
2377
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
7,271✔
2378
  
2379
  (void)msmSTRemoveStream(streamId, false);  
7,271✔
2380

2381
  mstResetSStmStatus(pStatus);
7,271✔
2382

2383
  pStatus->deployTimes++;
7,271✔
2384
}
7,271✔
2385

2386
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
207,924✔
2387
  int32_t code = TSDB_CODE_SUCCESS;
207,924✔
2388
  int32_t lino = 0;
207,924✔
2389
  int64_t streamId = pAction->streamId;
207,924✔
2390
  char* streamName = pAction->streamName;
207,924✔
2391
  SStreamObj* pStream = NULL;
207,924✔
2392
  int8_t stopped = 0;
207,924✔
2393

2394
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
207,924✔
2395
  if (pStatus) {
207,924✔
2396
    stopped = atomic_load_8(&pStatus->stopped);
7,271✔
2397
    if (0 == stopped) {
7,271✔
2398
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
573✔
2399
      msmResetStreamForRedeploy(streamId, pStatus);
573✔
2400
    } else {
2401
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
6,698✔
2402
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2403
        return code;
×
2404
      }
2405
      
2406
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
6,698✔
2407
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
6,698✔
2408
        msmResetStreamForRedeploy(streamId, pStatus);
6,698✔
2409
      }
2410
    }
2411
  }
2412

2413
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
207,924✔
2414
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
207,924✔
2415
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
674✔
2416
    return TSDB_CODE_SUCCESS;
674✔
2417
  }
2418

2419
  TAOS_CHECK_EXIT(code);
207,250✔
2420

2421
  if (pStatus && pStream->pCreate->streamId != streamId) {
207,250✔
2422
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2423
    atomic_store_8(&pStatus->stopped, 2);
×
2424
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2425
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2426
  }
2427

2428
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
207,250✔
2429
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
207,250✔
2430
  if (userStopped || userDropped) {
207,250✔
2431
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
859✔
2432
    goto _exit;
859✔
2433
  }
2434
  
2435
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
206,391✔
2436

2437
_exit:
207,250✔
2438

2439
  mndReleaseStream(pCtx->pMnode, pStream);
207,250✔
2440

2441
  if (code) {
207,250✔
2442
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2443
  }
2444

2445
  return code;
207,250✔
2446
}
2447

2448
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
1,358✔
2449
  int32_t code = TSDB_CODE_SUCCESS;
1,358✔
2450
  int32_t lino = 0;
1,358✔
2451
  int64_t streamId = pAction->streamId;
1,358✔
2452
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
1,358✔
2453
  if (NULL == ppTask) {
1,358✔
2454
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
×
2455
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2456
  }
2457
  
2458
  SStmTaskDeploy info = {0};
1,358✔
2459
  info.task.type = pAction->type;
1,358✔
2460
  info.task.streamId = pAction->streamId;
1,358✔
2461
  info.task.taskId = pAction->id.taskId;
1,358✔
2462
  info.task.seriousId = (*ppTask)->id.seriousId;
1,358✔
2463
  info.task.nodeId = pAction->id.nodeId;
1,358✔
2464
  info.task.taskIdx = pAction->id.taskIdx;
1,358✔
2465
  
2466
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
1,358✔
2467
  SStreamCalcScan* scanPlan = NULL;
1,358✔
2468
  if (!isTriggerReader) {
1,358✔
2469
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
573✔
2470
    if (NULL == scanPlan) {
573✔
2471
      mstsError("fail to get TASK:%" PRId64 " scanPlan, taskIdx:%d, scanPlanNum:%zu", 
×
2472
          pAction->id.taskId, pAction->id.taskIdx, taosArrayGetSize(pStatus->pCreate->calcScanPlanList));
2473
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2474
    }
2475
  }
2476
  
2477
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, scanPlan ? scanPlan->scanPlan : NULL, pStatus, isTriggerReader));
1,358✔
2478
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
1,358✔
2479

2480
_exit:
1,358✔
2481

2482
  if (code) {
1,358✔
2483
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2484
  }
2485

2486
  return code;
1,358✔
2487
}
2488

2489
/*
2490
static int32_t msmReLaunchTriggerTask(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2491
  int32_t code = TSDB_CODE_SUCCESS;
2492
  int32_t lino = 0;
2493
  int64_t streamId = pAction->streamId;
2494
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2495
  if (NULL == ppTask) {
2496
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
2497
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
2498
  }
2499
  
2500
  (*ppTask)->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2501
  if (!GOT_SNODE((*ppTask)->id.nodeId)) {
2502
    mstsError("no avaible snode for deploying trigger task, seriousId: %" PRId64, (*ppTask)->id.seriousId);
2503
    return TSDB_CODE_SUCCESS;
2504
  }
2505
  
2506
  SStmTaskDeploy info = {0};
2507
  info.task.type = pAction->type;
2508
  info.task.streamId = streamId;
2509
  info.task.taskId = pAction->id.taskId;
2510
  info.task.seriousId = (*ppTask)->id.seriousId;
2511
  info.task.nodeId = (*ppTask)->id.nodeId;
2512
  info.task.taskIdx = pAction->id.taskIdx;
2513
  
2514
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2515
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2516
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, *ppTask, 1, -1));
2517
  
2518
  atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2519

2520
_exit:
2521

2522
  if (code) {
2523
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2524
  }
2525

2526
  return code;
2527
}
2528
*/
2529

2530
static int32_t msmReLaunchRunnerDeploy(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
191✔
2531
  int32_t code = TSDB_CODE_SUCCESS;
191✔
2532
  int32_t lino = 0;
191✔
2533
  int64_t streamId = pAction->streamId;
191✔
2534
  
2535
/*
2536
  if (pAction->triggerStatus) {
2537
    pCtx->triggerTaskId = pAction->triggerStatus->id.taskId;
2538
    pAction->triggerStatus->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2539
    if (!GOT_SNODE(pAction->triggerStatus->id.nodeId)) {
2540
      mstsError("no avaible snode for deploying trigger task, seriousId:%" PRId64, pAction->triggerStatus->id.seriousId);
2541
      return TSDB_CODE_SUCCESS;
2542
    }
2543
  
2544
    pCtx->triggerNodeId = pAction->triggerStatus->id.nodeId;
2545
  } else {
2546
*/
2547
  pCtx->triggerTaskId = pStatus->triggerTask->id.taskId;
191✔
2548
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
191✔
2549
//  }
2550
  
2551
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
191✔
2552
  
2553
  SQueryPlan* pPlan = NULL;
191✔
2554
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
191✔
2555
  
2556
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
191✔
2557
  
2558
  taosHashClear(mStreamMgmt.toUpdateScanMap);
191✔
2559
  mStreamMgmt.toUpdateScanNum = 0;
191✔
2560
  
2561
/*
2562
  if (pAction->triggerStatus) {
2563
    SStmTaskDeploy info = {0};
2564
    info.task.type = STREAM_TRIGGER_TASK;
2565
    info.task.streamId = streamId;
2566
    info.task.taskId = pCtx->triggerTaskId;
2567
    info.task.seriousId = pAction->triggerStatus->id.seriousId;
2568
    info.task.nodeId = pCtx->triggerNodeId;
2569
    info.task.taskIdx = 0;
2570
  
2571
    TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2572
    TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2573
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pAction->triggerStatus, 1, -1));
2574
    
2575
    atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2576
  }
2577
*/
2578

2579
_exit:
191✔
2580

2581
  if (code) {
191✔
2582
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2583
  }
2584

2585
  return code;
191✔
2586
}
2587

2588

2589
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
1,549✔
2590
  int32_t code = TSDB_CODE_SUCCESS;
1,549✔
2591
  int32_t lino = 0;
1,549✔
2592
  int64_t streamId = pAction->streamId;
1,549✔
2593
  int64_t taskId = pAction->id.taskId;
1,549✔
2594
  SStreamObj* pStream = NULL;
1,549✔
2595

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

2598
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,549✔
2599
  if (NULL == pStatus) {
1,549✔
2600
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2601
    return TSDB_CODE_SUCCESS;
×
2602
  }
2603

2604
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,549✔
2605
  if (stopped) {
1,549✔
2606
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
2607
    return TSDB_CODE_SUCCESS;
×
2608
  }
2609

2610
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,549✔
2611
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,549✔
2612
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2613
    return TSDB_CODE_SUCCESS;
×
2614
  }
2615

2616
  TAOS_CHECK_EXIT(code);
1,549✔
2617

2618
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,549✔
2619
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,549✔
2620
  if (userStopped || userDropped) {
1,549✔
2621
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2622
    goto _exit;
×
2623
  }
2624

2625
  switch (pAction->type) {
1,549✔
2626
    case STREAM_READER_TASK:
1,358✔
2627
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,358✔
2628
      break;
1,358✔
2629
/*
2630
    case STREAM_TRIGGER_TASK:
2631
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2632
      break;
2633
*/
2634
    case STREAM_RUNNER_TASK:
191✔
2635
      if (pAction->multiRunner) {
191✔
2636
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
191✔
2637
      } else {
2638
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
2639
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2640
      }
2641
      break;
191✔
2642
    default:
×
2643
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2644
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2645
      break;
×
2646
  }
2647

2648
_exit:
1,549✔
2649

2650
  if (pStream) {
1,549✔
2651
    mndReleaseStream(pCtx->pMnode, pStream);
1,549✔
2652
  }
2653

2654
  if (code) {
1,549✔
2655
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2656
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2657
  }
2658

2659
  return code;
1,549✔
2660
}
2661

2662
static int32_t msmTDRemoveStream(int64_t streamId) {
×
2663
  void* pIter = NULL;
×
2664
  
2665
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
×
2666
    while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
×
2667
      SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
×
2668
      int32_t taskNum = taosArrayGetSize(pVg->taskList);
×
2669
      if (atomic_load_32(&pVg->deployed) == taskNum) {
×
2670
        continue;
×
2671
      }
2672
      
2673
      for (int32_t i = 0; i < taskNum; ++i) {
×
2674
        SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
×
2675
        if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2676
          pExt->deployed = true;
×
2677
        }
2678
      }
2679
    }
2680
  }
2681

2682
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
2683
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2684
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2685
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2686
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2687
        for (int32_t i = 0; i < taskNum; ++i) {
×
2688
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2689
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2690
            pExt->deployed = true;
×
2691
          }
2692
        }
2693
      }
2694

2695
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
2696
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2697
        for (int32_t i = 0; i < taskNum; ++i) {
×
2698
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2699
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2700
            pExt->deployed = true;
×
2701
          }
2702
        }
2703
      }
2704
    }
2705
  }
2706

2707
  return TSDB_CODE_SUCCESS;
×
2708
}
2709

2710
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
450✔
2711
  int32_t code = TSDB_CODE_SUCCESS;
450✔
2712
  int32_t lino = 0;
450✔
2713

2714
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
450✔
2715

2716
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
450✔
2717

2718
_exit:
450✔
2719

2720
  if (code) {
450✔
2721
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2722
  } else {
2723
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
450✔
2724
  }
2725

2726
  return code;
450✔
2727
}
2728

2729
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
37,965✔
2730
  int32_t code = TSDB_CODE_SUCCESS;
37,965✔
2731
  int32_t lino = 0;
37,965✔
2732

2733
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
37,965✔
2734
  if (0 == active || MND_STM_STATE_NORMAL != state) {
37,965✔
2735
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
345✔
2736
    return;
345✔
2737
  }
2738

2739
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
37,620✔
2740
  if (NULL == pStream) {
37,620✔
2741
    mstsInfo("stream %s already not in streamMap", streamName);
1,390✔
2742
    goto _exit;
1,390✔
2743
  }
2744

2745
  atomic_store_8(&pStream->stopped, 2);
36,230✔
2746

2747
  mstsInfo("set stream %s stopped by user", streamName);
36,230✔
2748

2749
_exit:
×
2750

2751
  taosHashRelease(mStreamMgmt.streamMap, pStream);
37,620✔
2752

2753
  if (code) {
37,620✔
2754
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2755
  }
2756

2757
  return;
37,620✔
2758
}
2759

2760
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,524✔
2761
  int32_t code = TSDB_CODE_SUCCESS;
3,524✔
2762
  int32_t lino = 0;
3,524✔
2763

2764
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,524✔
2765
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,524✔
2766
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2767
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2768
  }
2769

2770
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,524✔
2771
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,524✔
2772
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2773
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2774
    goto _exit;
×
2775
  }
2776

2777
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,524✔
2778

2779
_exit:
3,524✔
2780

2781
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,524✔
2782

2783
  if (code) {
3,524✔
2784
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2785
  }
2786

2787
  return code;
3,524✔
2788
}
2789

2790
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
53,611✔
2791
  int32_t code = TSDB_CODE_SUCCESS;
53,611✔
2792
  int32_t lino = 0;
53,611✔
2793
  SStmQNode* pQNode = NULL;
53,611✔
2794

2795
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
263,084✔
2796
    switch (pQNode->type) {
209,473✔
2797
      case STREAM_ACT_DEPLOY:
209,473✔
2798
        if (pQNode->streamAct) {
209,473✔
2799
          mstDebug("start to handle stream deploy action");
207,924✔
2800
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
207,924✔
2801
        } else {
2802
          mstDebug("start to handle task deploy action");
1,549✔
2803
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,549✔
2804
        }
2805
        break;
209,473✔
2806
      default:
×
2807
        break;
×
2808
    }
2809
  }
2810

2811
_exit:
53,611✔
2812

2813
  if (code) {
53,611✔
2814
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2815
  }
2816
}
53,611✔
2817

2818
void msmStopAllStreamsByGrant(int32_t errCode) {
×
2819
  SStmStatus* pStatus = NULL;
×
2820
  void* pIter = NULL;
×
2821
  int64_t streamId = 0;
×
2822
  
2823
  while (true) {
2824
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
2825
    if (NULL == pIter) {
×
2826
      break;
×
2827
    }
2828

2829
    pStatus = (SStmStatus*)pIter;
×
2830

2831
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2832
    atomic_store_8(&pStatus->stopped, 4);
×
2833

2834
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2835
  }
2836
}
×
2837

2838
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2839
  mstInfo("stream grant expired");
×
2840

2841
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2842
    mstWarn("mnode stream is NOT active, ignore handling");
×
2843
    return errCode;
×
2844
  }
2845

2846
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2847

2848
  msmStopAllStreamsByGrant(errCode);
×
2849

2850
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2851
  
2852
  return errCode;
×
2853
}
2854

2855
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,628,406✔
2856
  int32_t code = TSDB_CODE_SUCCESS;
1,628,406✔
2857
  int32_t lino = 0;
1,628,406✔
2858
  int64_t streamId = pDeploy->task.streamId;
1,628,406✔
2859
  
2860
  switch (pDeploy->task.type) {
1,628,406✔
2861
    case STREAM_READER_TASK:
700,450✔
2862
      if (NULL == pStream->readerTasks) {
700,450✔
2863
        pStream->streamId = streamId;
266,281✔
2864
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
266,281✔
2865
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
266,281✔
2866
      }
2867
      
2868
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,400,900✔
2869
      break;
700,450✔
2870
    case STREAM_TRIGGER_TASK:
206,391✔
2871
      pStream->streamId = streamId;
206,391✔
2872
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
206,391✔
2873
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
206,391✔
2874
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
206,391✔
2875
      break;
206,391✔
2876
    case STREAM_RUNNER_TASK:
721,565✔
2877
      if (NULL == pStream->runnerTasks) {
721,565✔
2878
        pStream->streamId = streamId;
210,733✔
2879
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
210,733✔
2880
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
210,733✔
2881
      }      
2882
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,443,130✔
2883
      break;
721,565✔
2884
    default:
×
2885
      break;
×
2886
  }
2887

2888
_exit:
1,628,406✔
2889

2890
  if (code) {
1,628,406✔
2891
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2892
  }
2893

2894
  return code;
1,628,406✔
2895
}
2896

2897
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,628,406✔
2898
  int32_t code = TSDB_CODE_SUCCESS;
1,628,406✔
2899
  int32_t lino = 0;
1,628,406✔
2900
  int64_t streamId = pDeploy->task.streamId;
1,628,406✔
2901
  SStreamTask* pTask = &pDeploy->task;
1,628,406✔
2902
  SStmStreamDeploy streamDeploy = {0};
1,628,406✔
2903
  SStmStreamDeploy* pStream = NULL;
1,628,406✔
2904
   
2905
  while (true) {
2906
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,628,406✔
2907
    if (NULL == pStream) {
1,628,406✔
2908
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
279,165✔
2909
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
279,165✔
2910
      if (TSDB_CODE_SUCCESS == code) {
279,165✔
2911
        goto _exit;
279,165✔
2912
      }
2913

2914
      if (TSDB_CODE_DUP_KEY != code) {
×
2915
        goto _exit;
×
2916
      }    
2917

2918
      tFreeSStmStreamDeploy(&streamDeploy);
×
2919
      continue;
×
2920
    }
2921

2922
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,349,241✔
2923
    
2924
    break;
1,349,241✔
2925
  }
2926
  
2927
_exit:
1,628,406✔
2928

2929
  taosHashRelease(pHash, pStream);
1,628,406✔
2930

2931
  if (code) {
1,628,406✔
2932
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2933
  } else {
2934
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,628,406✔
2935
  }
2936

2937
  return code;
1,628,406✔
2938
}
2939

2940

2941
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
283,607✔
2942
  int32_t code = TSDB_CODE_SUCCESS;
283,607✔
2943
  int32_t lino = 0;
283,607✔
2944
  int32_t taskNum = taosArrayGetSize(pTasks);
283,607✔
2945

2946
  for (int32_t i = 0; i < taskNum; ++i) {
1,912,013✔
2947
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,628,406✔
2948
    if (pExt->deployed) {
1,628,406✔
2949
      continue;
×
2950
    }
2951

2952
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,628,406✔
2953
    pExt->deployed = true;
1,628,406✔
2954

2955
    (void)atomic_add_fetch_32(deployed, 1);
1,628,406✔
2956
  }
2957

2958
_exit:
283,607✔
2959

2960
  if (code) {
283,607✔
2961
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2962
  }
2963

2964
  return code;
283,607✔
2965
}
2966

2967
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
92,021✔
2968
  int32_t code = TSDB_CODE_SUCCESS;
92,021✔
2969
  int32_t lino = 0;
92,021✔
2970
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
92,021✔
2971
  SStmVgTasksToDeploy* pVg = NULL;
92,021✔
2972
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2973

2974
  mstDebug("start to add stream vgroup tasks deploy");
92,021✔
2975
  
2976
  for (int32_t i = 0; i < vgNum; ++i) {
525,215✔
2977
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
433,194✔
2978

2979
    msmUpdateVgroupUpTs(pCtx, *vgId);
433,194✔
2980

2981
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
433,194✔
2982
    if (NULL == pVg) {
433,194✔
2983
      continue;
260,507✔
2984
    }
2985

2986
    if (taosRTryLockLatch(&pVg->lock)) {
172,687✔
2987
      continue;
×
2988
    }
2989
    
2990
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
172,687✔
2991
      taosRUnLockLatch(&pVg->lock);
×
2992
      continue;
×
2993
    }
2994
    
2995
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
172,687✔
2996
    taosRUnLockLatch(&pVg->lock);
172,687✔
2997
  }
2998

2999
_exit:
92,021✔
3000

3001
  if (code) {
92,021✔
3002
    if (pVg) {
×
3003
      taosRUnLockLatch(&pVg->lock);
×
3004
    }
3005

3006
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3007
  }
3008

3009
  return code;
92,021✔
3010
}
3011

3012
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
62,077✔
3013
  int32_t code = TSDB_CODE_SUCCESS;
62,077✔
3014
  int32_t lino = 0;
62,077✔
3015
  SStmSnodeTasksDeploy* pSnode = NULL;
62,077✔
3016
  SStreamHbMsg* pReq = pCtx->pReq;
62,077✔
3017

3018
  mstDebug("start to add stream snode tasks deploy");
62,077✔
3019
  
3020
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
62,077✔
3021
  if (NULL == pSnode) {
62,077✔
3022
    return TSDB_CODE_SUCCESS;
4,668✔
3023
  }
3024

3025
  (void)mstWaitLock(&pSnode->lock, false);
57,409✔
3026
  
3027
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
57,409✔
3028
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
53,511✔
3029
  }
3030

3031
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
57,409✔
3032
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
57,409✔
3033
  }
3034
  
3035
  taosWUnLockLatch(&pSnode->lock);
57,409✔
3036

3037
_exit:
57,409✔
3038

3039
  if (code) {
57,409✔
3040
    if (pSnode) {
×
3041
      taosWUnLockLatch(&pSnode->lock);
×
3042
    }
3043

3044
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3045
  }
3046

3047
  return code;
57,409✔
3048
}
3049

3050
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
746,270✔
3051
  int32_t code = TSDB_CODE_SUCCESS;
746,270✔
3052
  int32_t lino = 0;
746,270✔
3053
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
746,270✔
3054
  if (NULL == pStatus) {
746,270✔
3055
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3056
    return TSDB_CODE_SUCCESS;
×
3057
  }
3058
  
3059
  pStatus->lastActionTs = currTs;
746,270✔
3060

3061
_exit:
746,270✔
3062

3063
  if (code) {
746,270✔
3064
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3065
  }
3066

3067
  return code;
746,270✔
3068
}
3069

3070
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
77,094✔
3071
  int32_t code = TSDB_CODE_SUCCESS;
77,094✔
3072
  int32_t lino = 0;
77,094✔
3073
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
77,094✔
3074
  void* pIter = NULL;
77,094✔
3075

3076
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
77,094✔
3077
  
3078
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
77,094✔
3079
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
77,094✔
3080

3081
  while (1) {
279,165✔
3082
    pIter = taosHashIterate(pCtx->deployStm, pIter);
356,259✔
3083
    if (pIter == NULL) {
356,259✔
3084
      break;
77,094✔
3085
    }
3086
    
3087
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
279,165✔
3088
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
558,330✔
3089

3090
    int64_t streamId = pDeploy->streamId;
279,165✔
3091
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
279,165✔
3092
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
3093

3094
    mstClearSStmStreamDeploy(pDeploy);
279,165✔
3095
    
3096
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
279,165✔
3097
  }
3098
  
3099
_exit:
77,094✔
3100

3101
  if (pIter) {
77,094✔
3102
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3103
  }
3104

3105
  if (code) {
77,094✔
3106
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3107
  }
3108
  
3109
  return code;
77,094✔
3110
}
3111

3112
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
92,021✔
3113
  int32_t code = TSDB_CODE_SUCCESS;
92,021✔
3114
  int32_t lino = 0;
92,021✔
3115
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
92,021✔
3116
  SStmVgTasksToDeploy* pVg = NULL;
92,021✔
3117
  
3118
  for (int32_t i = 0; i < vgNum; ++i) {
525,215✔
3119
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
433,194✔
3120
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
433,194✔
3121
    if (NULL == pVg) {
433,194✔
3122
      continue;
260,507✔
3123
    }
3124

3125
    if (taosWTryLockLatch(&pVg->lock)) {
172,687✔
3126
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3127
      continue;
×
3128
    }
3129
    
3130
    if (atomic_load_32(&pVg->deployed) <= 0) {
172,687✔
3131
      taosWUnLockLatch(&pVg->lock);
×
3132
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3133
      continue;
×
3134
    }
3135

3136
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
172,687✔
3137
    if (atomic_load_32(&pVg->deployed) == taskNum) {
172,687✔
3138
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
172,687✔
3139
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
172,687✔
3140
      pVg->taskList = NULL;
172,687✔
3141
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
172,687✔
3142
      taosWUnLockLatch(&pVg->lock);
172,687✔
3143
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
172,687✔
3144
      continue;
172,687✔
3145
    }
3146

3147
    for (int32_t m = taskNum - 1; m >= 0; --m) {
×
3148
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
×
3149
      if (!pExt->deployed) {
×
3150
        continue;
×
3151
      }
3152

3153
      mstDestroySStmTaskToDeployExt(pExt);
×
3154

3155
      taosArrayRemove(pVg->taskList, m);
×
3156
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
×
3157
    }
3158
    atomic_store_32(&pVg->deployed, 0);
×
3159
    taosWUnLockLatch(&pVg->lock);
×
3160
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3161
  }
3162

3163
_exit:
92,021✔
3164

3165
  if (code) {
92,021✔
3166
    if (pVg) {
×
3167
      taosWUnLockLatch(&pVg->lock);
×
3168
    }
3169

3170
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3171
  }
3172
}
92,021✔
3173

3174
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
67,650✔
3175
  if (!GOT_SNODE(snodeId)) {
67,650✔
3176
    return;
5,573✔
3177
  }
3178
  
3179
  int32_t code = TSDB_CODE_SUCCESS;
62,077✔
3180
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
62,077✔
3181
  if (NULL == pSnode) {
62,077✔
3182
    return;
4,668✔
3183
  }
3184

3185
  if (taosWTryLockLatch(&pSnode->lock)) {
57,409✔
3186
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3187
    return;
×
3188
  }
3189

3190
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
57,409✔
3191
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
57,409✔
3192
  
3193
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
57,409✔
3194
    taosWUnLockLatch(&pSnode->lock);
×
3195
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3196
    return;
×
3197
  }
3198

3199
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
57,409✔
3200
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
57,409✔
3201
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
57,409✔
3202
    pSnode->triggerList = NULL;
57,409✔
3203
  }
3204

3205
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
57,409✔
3206
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
57,409✔
3207
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
57,409✔
3208
    pSnode->runnerList = NULL;
57,409✔
3209
  }
3210

3211
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
57,409✔
3212
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
57,409✔
3213
    taosWUnLockLatch(&pSnode->lock);
57,409✔
3214
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
57,409✔
3215
    return;
57,409✔
3216
  }
3217

3218
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
×
3219
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
×
3220
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
×
3221
      if (!pExt->deployed) {
×
3222
        continue;
×
3223
      }
3224

3225
      mstDestroySStmTaskToDeployExt(pExt);
×
3226
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
3227
      taosArrayRemove(pSnode->triggerList, m);
×
3228
    }
3229
    
3230
    pSnode->triggerDeployed = 0;
×
3231
  }
3232

3233
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
×
3234
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
×
3235
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
×
3236
      if (!pExt->deployed) {
×
3237
        continue;
×
3238
      }
3239

3240
      mstDestroySStmTaskToDeployExt(pExt);
×
3241
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
3242
      taosArrayRemove(pSnode->runnerList, m);
×
3243
    }
3244
    
3245
    pSnode->runnerDeployed = 0;
×
3246
  }
3247
  
3248
  taosWUnLockLatch(&pSnode->lock);
×
3249
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3250
}
3251

3252
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
18,313,052✔
3253
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
18,313,052✔
3254
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
92,021✔
3255
  }
3256

3257
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
18,313,052✔
3258
    msmCleanDeployedSnodeTasks(pHb->snodeId);
67,650✔
3259
  }
3260
}
18,313,052✔
3261

3262
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
18,313,052✔
3263
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
18,313,052✔
3264
  if (mStreamMgmt.tCtx) {
18,313,052✔
3265
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
18,239,197✔
3266
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
18,239,197✔
3267
  }
3268
}
18,313,052✔
3269

3270
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
201,945✔
3271
  int32_t code = TSDB_CODE_SUCCESS;
201,945✔
3272
  int32_t lino = 0;
201,945✔
3273
  int32_t action = STREAM_ACT_START;
201,945✔
3274
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
201,945✔
3275
  if (pAction) {
201,945✔
3276
    pAction->actions |= action;
×
3277
    pAction->start.triggerId = *pId;
×
3278
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3279
  } else {
3280
    SStmAction newAction = {0};
201,945✔
3281
    newAction.actions = action;
201,945✔
3282
    newAction.start.triggerId = *pId;
201,945✔
3283
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
201,945✔
3284
    mstsDebug("stream add START action, actions:%x", newAction.actions);
201,945✔
3285
  }
3286

3287
_exit:
201,945✔
3288

3289
  if (code) {
201,945✔
3290
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3291
  }
3292

3293
  return code;
201,945✔
3294
}
3295

3296
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
3297
  int32_t code = TSDB_CODE_SUCCESS;
×
3298
  int32_t lino = 0;
×
3299
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3300
  
3301
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
3302
  if (pAction) {
×
3303
    pAction->actions |= action;
×
3304
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3305
  } else {
3306
    SStmAction newAction = {0};
×
3307
    newAction.actions = action;
×
3308
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3309
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3310
  }
3311

3312
_exit:
×
3313

3314
  if (code) {
×
3315
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3316
  }
3317

3318
  return code;
×
3319
}
3320

3321

3322

3323
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
265,160✔
3324
  int32_t code = TSDB_CODE_SUCCESS;
265,160✔
3325
  int32_t lino = 0;
265,160✔
3326
  int32_t action = STREAM_ACT_UNDEPLOY;
265,160✔
3327
  bool    dropped = false;
265,160✔
3328

3329
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
265,160✔
3330
  mstsDebug("stream dropped: %d", dropped);
265,160✔
3331
  
3332
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
265,160✔
3333
  if (pAction) {
265,160✔
3334
    pAction->actions |= action;
215,366✔
3335
    if (NULL == pAction->undeploy.taskList) {
215,366✔
3336
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3337
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3338
    }
3339

3340
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
430,732✔
3341
    if (pAction->undeploy.doCheckpoint) {
215,366✔
3342
      pAction->undeploy.doCheckpoint = dropped ? false : true;
30,374✔
3343
    }
3344
    if (!pAction->undeploy.doCleanup) {
215,366✔
3345
      pAction->undeploy.doCleanup = dropped ? true : false;
30,374✔
3346
    }
3347
    
3348
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
215,366✔
3349
  } else {
3350
    SStmAction newAction = {0};
49,794✔
3351
    newAction.actions = action;
49,794✔
3352
    newAction.undeploy.doCheckpoint = dropped ? false : true;
49,794✔
3353
    newAction.undeploy.doCleanup = dropped ? true : false;
49,794✔
3354
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
49,794✔
3355
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
49,794✔
3356
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
99,588✔
3357
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
49,794✔
3358
    
3359
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
49,794✔
3360
  }
3361

3362
_exit:
265,160✔
3363

3364
  if (code) {
265,160✔
3365
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3366
  }
3367

3368
  return code;
265,160✔
3369
}
3370

3371
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,524✔
3372
  int32_t code = TSDB_CODE_SUCCESS;
3,524✔
3373
  int32_t lino = 0;
3,524✔
3374
  int32_t action = STREAM_ACT_RECALC;
3,524✔
3375
  SStmAction newAction = {0};
3,524✔
3376
  
3377
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,524✔
3378
  if (pAction) {
3,524✔
3379
    pAction->actions |= action;
×
3380
    pAction->recalc.recalcList = recalcList;
×
3381

3382
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3383
  } else {
3384
    newAction.actions = action;
3,524✔
3385
    newAction.recalc.recalcList = recalcList;
3,524✔
3386
    
3387
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,524✔
3388
    
3389
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,524✔
3390
  }
3391

3392
_exit:
3,524✔
3393

3394
  if (code) {
3,524✔
3395
    mstDestroySStmAction(&newAction);
×
3396
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3397
  }
3398

3399
  return code;
3,524✔
3400
}
3401

3402
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
288,122✔
3403
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
288,122✔
3404
  if (NULL == pStream) {
288,122✔
3405
    return false;
×
3406
  }
3407

3408
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
288,122✔
3409
    return false;
×
3410
  }
3411

3412
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
288,122✔
3413
  for (int32_t i = 0; i < readerNum; ++i) {
636,397✔
3414
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
348,275✔
3415
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
348,275✔
3416
      return false;
×
3417
    }
3418
  }
3419

3420
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
288,122✔
3421
  for (int32_t i = 0; i < readerNum; ++i) {
288,122✔
3422
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
×
3423
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
×
3424
      return false;
×
3425
    }
3426
  }
3427

3428
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
288,122✔
3429
  SListNode* pNode = listHead(pStream->calcReaders);
288,122✔
3430
  for (int32_t i = 0; i < readerNum; ++i) {
889,159✔
3431
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
601,037✔
3432
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
601,037✔
3433
      return false;
×
3434
    }
3435
    pNode = TD_DLIST_NODE_NEXT(pNode);
601,037✔
3436
  }
3437

3438
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
884,193✔
3439
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
682,248✔
3440
    for (int32_t m = 0; m < runnerNum; ++m) {
1,387,648✔
3441
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
791,577✔
3442
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
791,577✔
3443
        return false;
86,177✔
3444
      }
3445
    }
3446
  }
3447
  
3448
  return true;
201,945✔
3449
}
3450

3451

3452
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
14,406,138✔
3453
  int32_t code = TSDB_CODE_SUCCESS;
14,406,138✔
3454
  int32_t lino = 0;
14,406,138✔
3455
  int32_t action = 0;
14,406,138✔
3456
  int64_t streamId = pMsg->streamId;
14,406,138✔
3457
  SStreamTask* pTask = (SStreamTask*)pMsg;
14,406,138✔
3458
  int8_t  stopped = 0;
14,406,138✔
3459

3460
  msttDebug("start to handle task abnormal status %d", pTask->status);
14,406,138✔
3461
  
3462
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
14,406,138✔
3463
  if (NULL == pStatus) {
14,406,138✔
3464
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3465
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3466
    return;
14,037✔
3467
  }
3468

3469
  stopped = atomic_load_8(&pStatus->stopped);
14,406,138✔
3470
  if (stopped) {
14,406,138✔
3471
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3472
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3473
    return;
×
3474
  }
3475
  
3476
  switch (pMsg->status) {
14,406,138✔
3477
    case STREAM_STATUS_INIT:      
14,399,896✔
3478
      if (STREAM_TRIGGER_TASK != pMsg->type) {
14,399,896✔
3479
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
13,993,135✔
3480
        return;
13,993,135✔
3481
      }
3482
      
3483
      if (INT64_MIN == pStatus->lastActionTs) {
406,761✔
3484
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3485
        return;
×
3486
      }
3487
      
3488
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
406,761✔
3489
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
118,639✔
3490
        return;
118,639✔
3491
      }
3492

3493
      if (STREAM_IS_RUNNING(pStatus)) {
288,122✔
3494
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3495
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
288,122✔
3496
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
201,945✔
3497
      }
3498
      break;
288,122✔
3499
    case STREAM_STATUS_FAILED:
6,242✔
3500
      //STREAMTODO ADD ERRCODE HANDLE
3501
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
6,242✔
3502
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
6,030✔
3503
                 pMsg->taskIdx);
3504
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
6,030✔
3505
      } else {
3506
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
212✔
3507
                 pMsg->taskIdx);
3508
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
212✔
3509
      }
3510
      break;
6,242✔
3511
    default:
×
3512
      break;
×
3513
  }
3514

3515
_exit:
294,364✔
3516

3517
  if (code) {
294,364✔
3518
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3519
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3520
  }
3521
}
3522

3523
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
264,948✔
3524
  int32_t code = TSDB_CODE_SUCCESS;
264,948✔
3525
  int32_t lino = 0;
264,948✔
3526
  SStreamTask* pTask = (SStreamTask*)pStatus;
264,948✔
3527
  int64_t streamId = pStatus->streamId;
264,948✔
3528

3529
  msttInfo("start to handle task error, type: %d", err);
264,948✔
3530

3531
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
264,948✔
3532

3533
_exit:
264,948✔
3534

3535
  if (code) {
264,948✔
3536
    // IGNORE STOP STREAM BY ERROR  
3537
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3538
  }
3539
}
264,948✔
3540

3541
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
5,689,683✔
3542
  int32_t code = TSDB_CODE_SUCCESS;
5,689,683✔
3543
  int32_t lino = 0;
5,689,683✔
3544
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
5,689,683✔
3545

3546
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
5,689,683✔
3547
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3548
  }
3549
  
3550
  SArray* userRecalcList = NULL;
5,689,683✔
3551
  if (atomic_load_ptr(&pStream->userRecalcList)) {
5,689,683✔
3552
    taosWLockLatch(&pStream->userRecalcLock);
3,524✔
3553
    if (pStream->userRecalcList) {
3,524✔
3554
      userRecalcList = pStream->userRecalcList;
3,524✔
3555
      pStream->userRecalcList = NULL;
3,524✔
3556
    }
3557
    taosWUnLockLatch(&pStream->userRecalcLock);
3,524✔
3558
    
3559
    if (userRecalcList) {
3,524✔
3560
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,524✔
3561
    }
3562
  }
3563

3564
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
5,689,683✔
3565
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,854,703✔
3566
    if (NULL == pStatus->detailStatus) {
2,854,703✔
3567
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
201,970✔
3568
      if (NULL == pStatus->detailStatus) {
201,970✔
3569
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3570
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3571
      }
3572
    }
3573
    
3574
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,854,703✔
3575
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,854,703✔
3576
  }
3577

3578
_exit:
2,834,980✔
3579

3580
  if (code) {
5,689,683✔
3581
    // IGNORE STOP STREAM BY ERROR
3582
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3583
  }
3584
}
5,689,683✔
3585

3586
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,380,069✔
3587
  int32_t code = TSDB_CODE_SUCCESS;
1,380,069✔
3588
  int32_t lino = 0;
1,380,069✔
3589
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,380,069✔
3590

3591
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,380,069✔
3592

3593
  for (int32_t i = 0; i < num; ++i) {
49,424,903✔
3594
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
48,044,834✔
3595
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
48,044,834✔
3596
    
3597
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
48,044,834✔
3598
    if (NULL == ppStatus) {
48,044,834✔
3599
      msttWarn("task no longer exists in taskMap, streamId:0x%" PRIx64 " taskId:0x%" PRIx64 " type:%s status:%s taskIdx:%d",
1,644✔
3600
          (uint64_t)pTask->streamId, (uint64_t)pTask->taskId, gStreamTaskTypeStr[pTask->type], gStreamStatusStr[pTask->status], pTask->taskIdx);
3601
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
1,644✔
3602
      continue;
1,644✔
3603
    }
3604

3605
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
48,043,190✔
3606
    int8_t stopped = atomic_load_8(&pStream->stopped);
48,043,190✔
3607
    if (stopped) {
48,043,190✔
3608
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
263,304✔
3609
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
263,304✔
3610
      continue;
263,304✔
3611
    }
3612

3613
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
47,779,886✔
3614
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3615
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3616
          
3617
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3618
      continue;
×
3619
    }
3620

3621
    if ((*ppStatus)->status != pTask->status) {
47,779,886✔
3622
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,706,498✔
3623
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,165,639✔
3624
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,540,859✔
3625
        if (pStream->triggerTask) {
×
3626
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3627
        }
3628
        
3629
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3630
      }
3631
    }
3632
    
3633
    (*ppStatus)->errCode = pTask->errorCode;
47,779,886✔
3634
    (*ppStatus)->status = pTask->status;
47,779,886✔
3635
    (*ppStatus)->lastUpTs = pCtx->currTs;
47,779,886✔
3636
    
3637
    if (STREAM_TRIGGER_TASK == pTask->type && STREAM_STATUS_RUNNING == pTask->status) {
47,779,886✔
3638
      mstInfo("streamId:0x%" PRIx64 " trigger task status updated to Running in streamMap", (uint64_t)pTask->streamId);
5,280,058✔
3639
    }
3640
    
3641
    if (STREAM_STATUS_RUNNING != pTask->status) {
47,779,886✔
3642
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
14,406,138✔
3643
    }
3644
    
3645
    if (STREAM_TRIGGER_TASK == pTask->type) {
47,779,886✔
3646
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
5,689,683✔
3647
    }
3648
  }
3649

3650
_exit:
1,380,069✔
3651

3652
  if (code) {
1,380,069✔
3653
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3654
  }
3655

3656
  return code;
1,380,069✔
3657
}
3658

3659
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3660
  int32_t code = TSDB_CODE_SUCCESS;
×
3661
  int32_t lino = 0;
×
3662
  int64_t streamId = pTask->streamId;
×
3663
  SStreamObj* pStream = NULL;
×
3664

3665
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3666
  if (NULL == pStatus) {
×
3667
    SStmStatus status = {0};
×
3668
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3669
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3670
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
3671
      mndReleaseStream(pCtx->pMnode, pStream);
×
3672
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3673
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3674
      return code;
×
3675
    }
3676

3677
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3678
    mndReleaseStream(pCtx->pMnode, pStream);
×
3679

3680
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3681
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3682
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3683
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3684
  }
3685

3686
  SStmTaskStatus* pNewTask = NULL;
×
3687
  switch (pTask->type) {
×
3688
    case STREAM_READER_TASK: {
×
3689
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3690
      if (NULL == pList) {
×
3691
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
3692
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3693
      }
3694
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3695
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3696
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3697
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3698
            (int32_t)taosArrayGetSize(pList), readerSize);
3699
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3700
      }
3701
      
3702
      SStmTaskStatus taskStatus = {0};
×
3703
      taskStatus.pStream = pStatus;
×
3704
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3705
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3706
        pNewTask = taosArrayPush(pList, &taskStatus);
×
3707
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3708
      } else {
3709
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3710
        SListNode* pTailNode = tdListGetTail(pStatus->calcReaders);
×
3711
        QUERY_CHECK_NULL(pTailNode, code, lino, _exit, TSDB_CODE_INTERNAL_ERROR);
×
3712
        pNewTask = (SStmTaskStatus*)pTailNode->data;
×
3713
      }
3714
      
3715
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3716
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3717
      break;
×
3718
    }
3719
    case STREAM_TRIGGER_TASK: {
×
3720
      taosMemoryFreeClear(pStatus->triggerTask);
×
3721
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3722
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3723
      pStatus->triggerTask->pStream = pStatus;
×
3724
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
3725
      pNewTask = pStatus->triggerTask;
×
3726

3727
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3728
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3729
      break;
×
3730
    }
3731
    case STREAM_RUNNER_TASK:{
×
3732
      if (NULL == pStatus->runners[pTask->deployId]) {
×
3733
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3734
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3735
      }
3736
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
3737
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3738
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3739
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3740
      }    
3741
      
3742
      SStmTaskStatus taskStatus = {0};
×
3743
      taskStatus.pStream = pStatus;
×
3744
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3745
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3746
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3747

3748
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3749
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3750
      break;
×
3751
    }
3752
    default: {
×
3753
      msttError("invalid task type:%d in task status", pTask->type);
×
3754
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3755
      break;
×
3756
    }
3757
  }
3758

3759
_exit:
×
3760

3761
  if (code) {
×
3762
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3763
  } else {
3764
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3765
  }
3766

3767
  return code;
×
3768
}
3769

3770
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3771
  int32_t code = TSDB_CODE_SUCCESS;
×
3772
  int32_t lino = 0;
×
3773
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3774

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

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

3781
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3782
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3783
    }
3784
    
3785
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3786
    if (NULL == ppStatus) {
×
3787
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3788
      
3789
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3790
      
3791
      continue;
×
3792
    }
3793
    
3794
    (*ppStatus)->status = pTask->status;
×
3795
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3796
  }
3797

3798
_exit:
×
3799

3800
  if (code) {
×
3801
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3802
  }
3803

3804
  return code;
×
3805
}
3806

3807
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
201,945✔
3808
  int32_t code = TSDB_CODE_SUCCESS;
201,945✔
3809
  int32_t lino = 0;
201,945✔
3810
  if (NULL == pCtx->pRsp->start.taskList) {
201,945✔
3811
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
102,599✔
3812
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
102,599✔
3813
  }
3814

3815
  SStmTaskId* pId = &pAction->start.triggerId;
201,945✔
3816
  SStreamTaskStart start = {0};
201,945✔
3817
  start.task.type = STREAM_TRIGGER_TASK;
201,945✔
3818
  start.task.streamId = streamId;
201,945✔
3819
  start.task.taskId = pId->taskId;
201,945✔
3820
  start.task.seriousId = pId->seriousId;
201,945✔
3821
  start.task.nodeId = pId->nodeId;
201,945✔
3822
  start.task.taskIdx = pId->taskIdx;
201,945✔
3823

3824
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
403,890✔
3825
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
201,945✔
3826

3827
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
201,945✔
3828

3829
  return;
201,945✔
3830

3831
_exit:
×
3832

3833
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3834
}
3835

3836

3837
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
49,794✔
3838
  int32_t code = TSDB_CODE_SUCCESS;
49,794✔
3839
  int32_t lino = 0;
49,794✔
3840
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
49,794✔
3841
  if (NULL == pCtx->pRsp->undeploy.taskList) {
49,794✔
3842
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
36,141✔
3843
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
36,141✔
3844
  }
3845

3846
  SStreamTaskUndeploy undeploy;
49,794✔
3847
  for (int32_t i = 0; i < dropNum; ++i) {
314,954✔
3848
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
265,160✔
3849
    undeploy.task = *pTask;
265,160✔
3850
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
265,160✔
3851
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
265,160✔
3852

3853
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
530,320✔
3854
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
265,160✔
3855

3856
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
265,160✔
3857
  }
3858

3859
  return;
49,794✔
3860

3861
_exit:
×
3862

3863
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3864
}
3865

3866
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3867
  int32_t code = TSDB_CODE_SUCCESS;
×
3868
  int32_t lino = 0;
×
3869

3870
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3871
  if (NULL == pStream) {
×
3872
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3873
    return;
×
3874
  }
3875

3876
  if (NULL == pStream->triggerTask) {
×
3877
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3878
    return;
×
3879
  }
3880

3881
  SStreamMgmtRsp rsp = {0};
×
3882
  rsp.reqId = INT64_MIN;
×
3883
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3884
  rsp.task.streamId = streamId;
×
3885
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3886

3887
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3888

3889
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3890
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3891
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3892
  }
3893

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

3896
_exit:
×
3897

3898
  if (code) {
×
3899
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3900
  } else {
3901
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3902
  }
3903
}
3904

3905
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,524✔
3906
  int32_t code = TSDB_CODE_SUCCESS;
3,524✔
3907
  int32_t lino = 0;
3,524✔
3908

3909
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,524✔
3910
  if (NULL == pStream) {
3,524✔
3911
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3912
    return;
×
3913
  }
3914

3915
  if (NULL == pStream->triggerTask) {
3,524✔
3916
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3917
    return;
×
3918
  }
3919

3920
  SStreamMgmtRsp rsp = {0};
3,524✔
3921
  rsp.reqId = INT64_MIN;
3,524✔
3922
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,524✔
3923
  rsp.task.streamId = streamId;
3,524✔
3924
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,524✔
3925
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,524✔
3926

3927
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,524✔
3928
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,312✔
3929
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,312✔
3930
  }
3931

3932
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
7,048✔
3933

3934
_exit:
3,524✔
3935

3936
  if (code) {
3,524✔
3937
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3938
  } else {
3939
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,524✔
3940
  }
3941
}
3942

3943

3944
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
141,419✔
3945
  int32_t code = TSDB_CODE_SUCCESS;
141,419✔
3946
  int32_t lino = 0;
141,419✔
3947
  void* pIter = NULL;
141,419✔
3948
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
141,419✔
3949

3950
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
141,419✔
3951

3952
  while (1) {
255,263✔
3953
    pIter = taosHashIterate(pCtx->actionStm, pIter);
396,682✔
3954
    if (pIter == NULL) {
396,682✔
3955
      break;
141,419✔
3956
    }
3957

3958
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
255,263✔
3959
    SStmAction *pAction = (SStmAction *)pIter;
255,263✔
3960
    
3961
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
255,263✔
3962
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
49,794✔
3963
      continue;
49,794✔
3964
    }
3965

3966
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
205,469✔
3967
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3968
    }
3969

3970
    if (STREAM_ACT_RECALC & pAction->actions) {
205,469✔
3971
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,524✔
3972
    }
3973

3974
    if (STREAM_ACT_START & pAction->actions) {
205,469✔
3975
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
201,945✔
3976
    }
3977
  }
3978
  
3979
_exit:
141,419✔
3980

3981
  if (pIter) {
141,419✔
3982
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3983
  }
3984

3985
  if (code) {
141,419✔
3986
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3987
  }
3988

3989
  return code;
141,419✔
3990
}
3991

3992
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
18,239,197✔
3993
  int32_t  code = TSDB_CODE_SUCCESS;
18,239,197✔
3994
  int32_t  lino = 0;
18,239,197✔
3995
  int64_t* lastTs = NULL;
18,239,197✔
3996
  bool     noExists = false;
18,239,197✔
3997

3998
  while (true) {
3999
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
18,773,625✔
4000
    if (NULL == lastTs) {
18,773,625✔
4001
      if (noExists) {
684,393✔
4002
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
149,965✔
4003
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
149,965✔
4004
      }
4005

4006
      noExists = true;
534,428✔
4007
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
534,428✔
4008
      
4009
      continue;
534,428✔
4010
    }
4011

4012
    while (true) {
×
4013
      int64_t lastTsValue = atomic_load_64(lastTs);
18,089,232✔
4014
      if (pCtx->currTs > lastTsValue) {
18,089,232✔
4015
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
18,084,784✔
4016
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
18,084,784✔
4017
          return code;
18,084,784✔
4018
        }
4019

4020
        continue;
×
4021
      }
4022

4023
      return code;
4,448✔
4024
    }
4025

4026
    break;
4027
  }
4028

4029
_exit:
149,965✔
4030

4031
  if (code) {
149,965✔
4032
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
149,965✔
4033
  }
4034

4035
  return code;  
149,965✔
4036
}
4037

4038
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
4039
  SStmStatus* pStatus = NULL;
×
4040
  int32_t trigReaderNum = 0;
×
4041
  int32_t calcReaderNum = 0;
×
4042
  int32_t runnerNum = 0;
×
4043
  int64_t streamId = 0;
×
4044
  void* pIter = NULL;
×
4045
  while (true) {
4046
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
4047
    if (NULL == pIter) {
×
4048
      return;
×
4049
    }
4050

4051
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4052
    pStatus = (SStmStatus*)pIter;
×
4053

4054
    if (NULL == pStatus->triggerTask) {
×
4055
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
4056
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4057
      continue;
×
4058
    }
4059
    
4060
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
4061
    if (pStatus->trigReaderNum != trigReaderNum) {
×
4062
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
4063
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4064
      continue;
×
4065
    }
4066

4067
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
4068
    if (pStatus->calcReaderNum != calcReaderNum) {
×
4069
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
4070
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4071
      continue;
×
4072
    }
4073

4074
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
4075
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
4076
      if (runnerNum != pStatus->runnerNum) {
×
4077
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
4078
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4079
        continue;
×
4080
      }
4081
    }
4082
  }
4083
}
4084

4085
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
375✔
4086
  int32_t code = TSDB_CODE_SUCCESS;
375✔
4087
  int32_t lino = 0;
375✔
4088
  int32_t minVal = watchError ? 0 : 1;
375✔
4089

4090
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
375✔
4091
    return code;
×
4092
  }
4093

4094
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
375✔
4095
    (void)sched_yield();
×
4096
  }
4097

4098
  if (watchError) {
375✔
4099
    taosHashClear(mStreamMgmt.vgroupMap);
×
4100
    taosHashClear(mStreamMgmt.snodeMap);
×
4101
    taosHashClear(mStreamMgmt.taskMap);
×
4102
    taosHashClear(mStreamMgmt.streamMap);
×
4103
    mstInfo("watch error happends, clear all maps");
×
4104
    goto _exit;
×
4105
  }
4106

4107
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
375✔
4108
    mstInfo("no stream tasks remain during watch state");
375✔
4109
    goto _exit;
375✔
4110
  }
4111

4112
  msmWatchCheckStreamMap(pCtx);
×
4113

4114
_exit:
375✔
4115

4116
  mStreamMgmt.lastTaskId += 100000;
375✔
4117

4118
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
375✔
4119

4120
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
375✔
4121

4122
  if (code) {
375✔
4123
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4124
  }
4125

4126
  return code;
375✔
4127
}
4128

4129

4130
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
13,053✔
4131
  int32_t code = TSDB_CODE_SUCCESS;
13,053✔
4132
  int32_t lino = 0;
13,053✔
4133
  SStreamHbMsg* pReq = pCtx->pReq;
13,053✔
4134

4135
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
13,053✔
4136
  
4137
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
13,053✔
4138
    goto _exit;
×
4139
  }
4140

4141
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
13,053✔
4142
  if (GOT_SNODE(pReq->snodeId)) {
13,053✔
4143
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,214✔
4144
  }
4145

4146
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,053✔
4147
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4148
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4149
  }
4150

4151
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
13,053✔
4152
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
375✔
4153
  }
4154

4155
_exit:
13,053✔
4156

4157
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
13,053✔
4158
  
4159
  if (code) {
13,053✔
4160
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4161

4162
    (void)msmWatchHandleEnding(pCtx, true);
×
4163
  }
4164

4165
  return code;
13,053✔
4166
}
4167

4168
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,109,883✔
4169
  int32_t listSize = taosArrayGetSize(pOReaders);
5,109,883✔
4170
  int32_t totalSize = 0;
5,109,883✔
4171
  
4172
  for (int32_t i = 0; i < listSize; ++i) {
5,651,461✔
4173
    SArray* pList = taosArrayGetP(pOReaders, i);
541,578✔
4174
    totalSize += taosArrayGetSize(pList);
541,578✔
4175
  }
4176

4177
  return totalSize;
5,109,883✔
4178
}
4179

4180
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
343,464✔
4181
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
343,464✔
4182
  if (NULL == pList) {
343,464✔
4183
    return NULL;
×
4184
  }
4185

4186
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
343,464✔
4187
}
4188

4189

4190
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
62,712✔
4191
  int32_t code = TSDB_CODE_SUCCESS;
62,712✔
4192
  int32_t lino = 0;
62,712✔
4193

4194
  if (NULL == pStatus->trigOReaders) {
62,712✔
4195
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
54,438✔
4196
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
54,438✔
4197
  }
4198

4199
  while (true) {
54,438✔
4200
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
117,150✔
4201

4202
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
117,150✔
4203
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
54,438✔
4204
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
54,438✔
4205

4206
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
108,876✔
4207
      continue;
54,438✔
4208
    }
4209

4210
    *ppRes = *ppOReaderList;
62,712✔
4211
    break;
62,712✔
4212
  }
4213

4214
_exit:
62,712✔
4215

4216
  if (code) {
62,712✔
4217
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4218
  }
4219

4220
  return code;
62,712✔
4221
}
4222

4223
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
73,630✔
4224
  int32_t code = TSDB_CODE_SUCCESS;
73,630✔
4225
  int32_t lino = 0;
73,630✔
4226
  bool    readerExists = false;
73,630✔
4227
  int64_t streamId = pTask->streamId;
73,630✔
4228

4229
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
73,630✔
4230
  for (int32_t i = 0; i < readerNum; ++i) {
153,046✔
4231
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
90,334✔
4232
    if (pReader->id.nodeId == vgId) {
90,334✔
4233
      readerExists = true;
10,918✔
4234
      break;
10,918✔
4235
    }
4236
  }
4237

4238
  if (!readerExists) {
73,630✔
4239
    SArray* pReaderList = NULL;
62,712✔
4240
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
62,712✔
4241
    
4242
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
62,712✔
4243
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
62,712✔
4244
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
62,712✔
4245
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
62,712✔
4246
  }
4247

4248
_exit:
10,918✔
4249

4250
  if (code) {
73,630✔
4251
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4252
  }
4253

4254
  return code;
73,630✔
4255
}
4256

4257
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
64,720✔
4258
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
64,720✔
4259
  int32_t lino = 0;
64,720✔
4260
  int32_t vgId = 0;
64,720✔
4261
  int64_t streamId = pTask->streamId;
64,720✔
4262
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
64,720✔
4263
  int32_t tbNum = taosArrayGetSize(pTbs);
64,720✔
4264
  SStreamDbTableName* pName = NULL;
64,720✔
4265
  SSHashObj* pDbVgroups = NULL;
64,720✔
4266
  SStreamMgmtRsp rsp = {0};
64,720✔
4267
  rsp.reqId = pTask->pMgmtReq->reqId;
64,720✔
4268
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
64,720✔
4269
  int32_t iter = 0;
64,720✔
4270
  void* p = NULL;
64,720✔
4271
  SSHashObj* pVgs = NULL;
64,720✔
4272
  SStreamMgmtReq* pMgmtReq = NULL;
64,720✔
4273
  int8_t stopped = 0;
64,720✔
4274

4275
  if (NULL == pCtx->pRsp->rsps.rspList) {
64,720✔
4276
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4277
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4278
  }
4279
  
4280
  TSWAP(pTask->pMgmtReq, pMgmtReq);
64,720✔
4281
  rsp.task = *(SStreamTask*)pTask;
64,720✔
4282

4283
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
64,720✔
4284
  if (NULL == pStatus) {
64,720✔
4285
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4286
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4287
  }
4288

4289
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
64,720✔
4290
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4291
    goto _exit;
×
4292
  }
4293

4294
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
64,720✔
4295

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

4302
  if (tbNum <= 0) {
64,720✔
4303
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4304
    goto _exit;
×
4305
  }
4306

4307
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
64,720✔
4308
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
64,720✔
4309
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
64,720✔
4310

4311
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
64,720✔
4312
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
64,720✔
4313
  
4314
  for (int32_t i = 0; i < tbNum; ++i) {
254,828✔
4315
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
190,108✔
4316
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
190,108✔
4317
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
380,216✔
4318
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
190,108✔
4319
  }
4320

4321
  int32_t vgNum = tSimpleHashGetSize(pVgs);
64,720✔
4322
  while (true) {
4323
    p = tSimpleHashIterate(pVgs, p, &iter);
138,350✔
4324
    if (NULL == p) {
138,350✔
4325
      break;
64,720✔
4326
    }
4327
    
4328
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
73,630✔
4329
  }
4330
  
4331
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
64,720✔
4332
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
64,720✔
4333
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
64,720✔
4334

4335
  SStreamTaskAddr addr;
64,720✔
4336
  for (int32_t i = 0; i < vgNum; ++i) {
127,432✔
4337
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
62,712✔
4338
    addr.taskId = pOTask->id.taskId;
62,712✔
4339
    addr.nodeId = pOTask->id.nodeId;
62,712✔
4340
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
62,712✔
4341
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
125,424✔
4342
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
62,712✔
4343
  }
4344

4345
_exit:
64,720✔
4346

4347
  tFreeSStreamMgmtReq(pMgmtReq);
64,720✔
4348
  taosMemoryFree(pMgmtReq);
64,720✔
4349

4350
  tSimpleHashCleanup(pVgs);
64,720✔
4351
  mstDestroyDbVgroupsHash(pDbVgroups);
64,720✔
4352

4353
  if (code) {
64,720✔
4354
    rsp.code = code;
×
4355
    
4356
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4357

4358
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4359
  } else {
4360
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
129,440✔
4361
  }
4362

4363
_final:
64,720✔
4364

4365
  if (finalCode) {
64,720✔
4366
    tFreeSStreamMgmtRsp(&rsp);
×
4367
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4368
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4369
  }
4370

4371
  return finalCode;
64,720✔
4372
}
4373

4374
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,508✔
4375
  int32_t num = taosArrayGetSize(pList);
2,508✔
4376
  SStreamCalcScan* pScan = NULL;
2,508✔
4377
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,508✔
4378
  int64_t planUid = 0;
2,508✔
4379
  for (int32_t i = 0; i < num; ++i) {
2,508✔
4380
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,508✔
4381
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,508✔
4382
    if (0 != planUid && planUid == uid) {
2,508✔
4383
      *ppRes = pScan;
2,508✔
4384
      break;
2,508✔
4385
    }
4386
  }
4387

4388
_exit:
2,508✔
4389

4390
  if (code) {
2,508✔
4391
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4392
  }
4393

4394
  return code;
2,508✔
4395
}
4396

4397
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,508✔
4398
  int32_t code = TSDB_CODE_SUCCESS;
2,508✔
4399
  int32_t lino = 0;
2,508✔
4400
  bool    readerExists = false;
2,508✔
4401
  int64_t streamId = pTask->streamId;
2,508✔
4402
  SListNode* pNode = listHead(pStatus->calcReaders);
2,508✔
4403
  SStmTaskStatus* pReader = NULL;
2,508✔
4404
  int32_t taskIdx = 0;
2,508✔
4405

4406
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,508✔
4407
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
23,256✔
4408
    pReader = (SStmTaskStatus*)pNode->data;
20,748✔
4409
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
20,748✔
4410
      readerExists = true;
×
4411
      pAddr->taskId = pReader->id.taskId;
×
4412
      break;
×
4413
    }
4414
  }
4415

4416
  if (!readerExists) {
2,508✔
4417
    if (NULL == pStatus->calcReaders) {
2,508✔
4418
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4419
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4420
      taskIdx = 0;
×
4421
    } else {
4422
      pNode = listTail(pStatus->calcReaders);
2,508✔
4423
      pReader = (SStmTaskStatus*)pNode->data;
2,508✔
4424
      taskIdx = pReader->id.taskIdx + 1;
2,508✔
4425
    }
4426

4427
    SStreamCalcScan* pScan = NULL;
2,508✔
4428
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,508✔
4429
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,508✔
4430
    pReader = tdListReserve(pStatus->calcReaders);
2,508✔
4431
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,508✔
4432
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,508✔
4433
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,508✔
4434
    pAddr->taskId = pReader->id.taskId;
2,508✔
4435
  }
4436

4437
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,508✔
4438
  pAddr->nodeId = vgId;
2,508✔
4439

4440
_exit:
2,508✔
4441

4442
  if (code) {
2,508✔
4443
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4444
  }
4445

4446
  return code;
2,508✔
4447
}
4448

4449

4450
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,508✔
4451
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,508✔
4452
  int32_t lino = 0;
2,508✔
4453
  int32_t vgId = 0;
2,508✔
4454
  int64_t streamId = pTask->streamId;
2,508✔
4455
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,508✔
4456
  int32_t reqNum = taosArrayGetSize(pReqs);
2,508✔
4457
  SStreamOReaderDeployReq* pReq = NULL;
2,508✔
4458
  SStreamOReaderDeployRsp* pRsp = NULL;
2,508✔
4459
  SStreamMgmtRsp rsp = {0};
2,508✔
4460
  rsp.reqId = pTask->pMgmtReq->reqId;
2,508✔
4461
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,508✔
4462
  SStreamMgmtReq* pMgmtReq = NULL;
2,508✔
4463
  int8_t stopped = 0;
2,508✔
4464
  int32_t vgNum = 0;
2,508✔
4465
  SStreamTaskAddr* pAddr = NULL;
2,508✔
4466

4467
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,508✔
4468
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4469
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4470
  }
4471
  
4472
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,508✔
4473
  rsp.task = *(SStreamTask*)pTask;
2,508✔
4474

4475
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,508✔
4476
  if (NULL == pStatus) {
2,508✔
4477
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4478
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4479
  }
4480

4481
  stopped = atomic_load_8(&pStatus->stopped);
2,508✔
4482
  if (stopped) {
2,508✔
4483
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4484
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4485
  }
4486

4487
  if (reqNum <= 0) {
2,508✔
4488
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4489
    goto _exit;
×
4490
  }
4491

4492
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,508✔
4493
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,508✔
4494

4495
  for (int32_t i = 0; i < reqNum; ++i) {
5,016✔
4496
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,508✔
4497
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,508✔
4498
    pRsp->execId = pReq->execId;
2,508✔
4499
    vgNum = taosArrayGetSize(pReq->vgIds);
2,508✔
4500
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,508✔
4501
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,508✔
4502
    
4503
    for (int32_t n = 0; n < vgNum; ++n) {
5,016✔
4504
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,508✔
4505
      pAddr = taosArrayGet(pRsp->vgList, n);
2,508✔
4506
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,508✔
4507
    }
4508
  }
4509

4510
_exit:
2,508✔
4511

4512
  tFreeSStreamMgmtReq(pMgmtReq);
2,508✔
4513
  taosMemoryFree(pMgmtReq);
2,508✔
4514

4515
  if (code) {
2,508✔
4516
    rsp.code = code;
×
4517
    
4518
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4519
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4520
  } else {
4521
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
5,016✔
4522
  }
4523

4524

4525
_final:
2,508✔
4526

4527
  if (finalCode) {
2,508✔
4528
    tFreeSStreamMgmtRsp(&rsp);
×
4529
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4530
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4531
  }
4532

4533
  return finalCode;
2,508✔
4534
}
4535

4536

4537
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
67,228✔
4538
  int32_t code = TSDB_CODE_SUCCESS;
67,228✔
4539
  int32_t lino = 0;
67,228✔
4540

4541
  switch (pTask->pMgmtReq->type) {
67,228✔
4542
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
64,720✔
4543
      msmDeployTriggerOrigReader(pCtx, pTask);
64,720✔
4544
      break;
64,720✔
4545
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,508✔
4546
      msmDeployRunnerOrigReader(pCtx, pTask);
2,508✔
4547
      break;
2,508✔
4548
    default:
×
4549
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4550
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4551
      break;
×
4552
  }
4553

4554
_exit:
67,228✔
4555

4556
  if (code) {
67,228✔
4557
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4558
  }
4559

4560
  return code;
67,228✔
4561
}
4562

4563
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
19,544✔
4564
  int32_t code = TSDB_CODE_SUCCESS;
19,544✔
4565
  int32_t lino = 0;
19,544✔
4566
  SStreamHbMsg* pReq = pCtx->pReq;
19,544✔
4567
  SStmTaskStatusMsg* pTask = NULL;
19,544✔
4568
  
4569
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
19,544✔
4570
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
19,544✔
4571
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
19,544✔
4572
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
19,544✔
4573
  }
4574
  
4575
  for (int32_t i = 0; i < reqNum; ++i) {
86,772✔
4576
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
67,228✔
4577
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
67,228✔
4578
    if (NULL == pTask) {
67,228✔
4579
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4580
      continue;
×
4581
    }
4582

4583
    if (NULL == pTask->pMgmtReq) {
67,228✔
4584
      msttError("idx %d without mgmtReq", idx);
×
4585
      continue;
×
4586
    }
4587

4588
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
67,228✔
4589
  }
4590

4591
_exit:
19,544✔
4592

4593
  if (code) {
19,544✔
4594
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4595
  }
4596

4597
  return code;
19,544✔
4598
}
4599

4600
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
18,226,144✔
4601
  int32_t code = TSDB_CODE_SUCCESS;
18,226,144✔
4602
  int32_t lino = 0;
18,226,144✔
4603
  SStreamHbMsg* pReq = pCtx->pReq;
18,226,144✔
4604

4605
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
18,226,144✔
4606
  if (GOT_SNODE(pReq->snodeId)) {
18,076,179✔
4607
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,538,582✔
4608
  }
4609
  
4610
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
18,076,179✔
4611
    msmHandleStreamActions(pCtx);
53,611✔
4612
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
53,611✔
4613
  }
4614

4615
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
18,076,179✔
4616
    code = msmHandleStreamRequests(pCtx);
19,544✔
4617
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
19,544✔
4618
    TAOS_CHECK_EXIT(code);
19,544✔
4619
  }
4620

4621
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
18,076,179✔
4622
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
92,021✔
4623
  } else {
4624
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
17,984,158✔
4625
  }
4626

4627
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
18,076,179✔
4628
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
62,077✔
4629
  }
4630

4631
  if (taosHashGetSize(pCtx->deployStm) > 0) {
18,076,179✔
4632
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
77,094✔
4633
  }
4634

4635
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
18,076,179✔
4636
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,380,069✔
4637
  }
4638

4639
  if (taosHashGetSize(pCtx->actionStm) > 0) {
18,076,179✔
4640
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
141,419✔
4641
  }
4642

4643
_exit:
18,076,179✔
4644

4645
  if (code) {
18,226,144✔
4646
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
149,965✔
4647
  }
4648

4649
  return code;
18,226,144✔
4650
}
4651

4652
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
18,313,052✔
4653
  int32_t lino = 0;
18,313,052✔
4654
  int32_t tlen = 0;
18,313,052✔
4655
  void   *buf = NULL;
18,313,052✔
4656

4657
  if (TSDB_CODE_SUCCESS != code) {
18,313,052✔
4658
    goto _exit;
149,965✔
4659
  }
4660

4661
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
18,163,087✔
4662
  if (code < 0) {
18,163,087✔
4663
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4664
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4665
  }
4666

4667
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
18,163,087✔
4668
  if (buf == NULL) {
18,163,087✔
4669
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4670
    TAOS_CHECK_EXIT(terrno);    
×
4671
  }
4672

4673
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
18,163,087✔
4674
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
18,163,087✔
4675

4676
  SEncoder encoder;
18,160,101✔
4677
  tEncoderInit(&encoder, abuf, tlen);
18,163,087✔
4678
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
18,163,087✔
4679
    rpcFreeCont(buf);
×
4680
    buf = NULL;
×
4681
    tEncoderClear(&encoder);
×
4682
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4683
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4684
  }
4685
  tEncoderClear(&encoder);
18,163,087✔
4686

4687
_exit:
18,313,052✔
4688

4689
  pMsg->code = code;
18,313,052✔
4690
  pMsg->info = *pRpcInfo;
18,313,052✔
4691
  if (TSDB_CODE_SUCCESS == code) {
18,313,052✔
4692
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
18,163,087✔
4693
    pMsg->pCont = buf;
18,163,087✔
4694
  }
4695
}
18,313,052✔
4696

4697

4698
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
18,313,052✔
4699
  int32_t code = TSDB_CODE_SUCCESS;
18,313,052✔
4700
  SMStreamHbRspMsg rsp = {0};
18,313,052✔
4701
  rsp.streamGId = pHb->streamGId;
18,313,052✔
4702

4703
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
18,313,052✔
4704

4705
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
18,313,052✔
4706
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
73,855✔
4707
    goto _exit;
73,855✔
4708
  }
4709

4710
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
18,239,197✔
4711
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
18,239,197✔
4712

4713
  pCtx->tidx = tidx;
18,239,197✔
4714
  pCtx->pMnode = pMnode;
18,239,197✔
4715
  pCtx->currTs = currTs;
18,239,197✔
4716
  pCtx->pReq = pHb;
18,239,197✔
4717
  pCtx->pRsp = &rsp;
18,239,197✔
4718
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
18,239,197✔
4719
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
18,239,197✔
4720
  
4721
  switch (atomic_load_8(&mStreamMgmt.state)) {
18,239,197✔
4722
    case MND_STM_STATE_WATCH:
13,053✔
4723
      code = msmWatchHandleHbMsg(pCtx);
13,053✔
4724
      break;
13,053✔
4725
    case MND_STM_STATE_NORMAL:
18,226,144✔
4726
      code = msmNormalHandleHbMsg(pCtx);
18,226,144✔
4727
      break;
18,226,144✔
4728
    default:
×
4729
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4730
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4731
      break;
×
4732
  }
4733

4734
_exit:
18,313,052✔
4735

4736
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
18,313,052✔
4737

4738
  msmCleanStreamGrpCtx(pHb);
18,313,052✔
4739
  msmClearStreamToDeployMaps(pHb);
18,313,052✔
4740

4741
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
18,313,052✔
4742
  
4743
  tFreeSMStreamHbRspMsg(&rsp);
18,313,052✔
4744

4745
  return code;
18,313,052✔
4746
}
4747

4748
void msmHandleBecomeLeader(SMnode *pMnode) {
439,476✔
4749
  if (tsDisableStream) {
439,476✔
4750
    return;
×
4751
  }
4752

4753
  mstInfo("start to process mnode become leader");
439,476✔
4754

4755
  int32_t code = 0;
439,476✔
4756
  streamAddVnodeLeader(MNODE_HANDLE);
439,476✔
4757
  
4758
  taosWLockLatch(&mStreamMgmt.runtimeLock);
439,476✔
4759
  msmDestroyRuntimeInfo(pMnode);
439,476✔
4760
  code = msmInitRuntimeInfo(pMnode);
439,476✔
4761
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
439,476✔
4762

4763
  if (TSDB_CODE_SUCCESS == code) {
439,476✔
4764
    atomic_store_8(&mStreamMgmt.active, 1);
439,476✔
4765
  }
4766

4767
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
439,476✔
4768
}
4769

4770
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
534,246✔
4771
  if (tsDisableStream) {
534,246✔
4772
    return;
×
4773
  }
4774

4775
  mstInfo("start to process mnode become not leader");
534,246✔
4776

4777
  streamRemoveVnodeLeader(MNODE_HANDLE);
534,246✔
4778

4779
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
534,246✔
4780
    taosWLockLatch(&mStreamMgmt.runtimeLock);
439,476✔
4781
    msmDestroyRuntimeInfo(pMnode);
439,476✔
4782
    mStreamMgmt.stat.inactiveTimes++;
439,476✔
4783
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
439,476✔
4784
  }
4785
}
4786

4787

4788
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4789
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4790
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4791
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4792
  } else {
4793
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4794
  }
4795
}
×
4796

4797
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
8,277✔
4798
  int32_t code = TSDB_CODE_SUCCESS;
8,277✔
4799
  int32_t lino = 0;
8,277✔
4800
  SStreamObj* pStream = pObj;
8,277✔
4801
  SSnodeObj* pSnode = p1;
8,277✔
4802
  SArray** ppRes = p2;
8,277✔
4803

4804
  if (pStream->mainSnodeId == pSnode->id) {
8,277✔
4805
    if (NULL == *ppRes) {
4,907✔
4806
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
959✔
4807
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
959✔
4808
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
959✔
4809
    }
4810

4811
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
9,814✔
4812
  }
4813

4814
  return true;
8,277✔
4815

4816
_exit:
×
4817

4818
  if (code) {
×
4819
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4820
  }  
4821

4822
  *(int32_t*)p3 = code;
×
4823

4824
  return false;
×
4825
}
4826

4827

4828
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
40,653✔
4829
  int32_t code = TSDB_CODE_SUCCESS;
40,653✔
4830
  int32_t lino = 0;
40,653✔
4831
  
4832
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
40,653✔
4833
  TAOS_CHECK_EXIT(code);
40,653✔
4834

4835
  int32_t streamNum = taosArrayGetSize(*ppRes);
40,653✔
4836
  if (streamNum > 0 && 0 == pSnode->replicaId) {
40,653✔
4837
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
212✔
4838
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
212✔
4839
  }
4840

4841
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4842

4843
_exit:
40,653✔
4844

4845
  if (code) {
40,653✔
4846
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
212✔
4847
  }  
4848

4849
  return code;
40,653✔
4850
}
4851

4852
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
663,188✔
4853
  SStreamObj* pStream = pObj;
663,188✔
4854
  int64_t streamId = pStream->pCreate->streamId;
663,188✔
4855
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
663,188✔
4856
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
663,188✔
4857
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
663,188✔
4858
  
4859
  if ((userDropped || userStopped) && (NULL == pStatus)) {
663,188✔
4860
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
212✔
4861
    return true;
212✔
4862
  }
4863
  
4864
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
662,976✔
4865
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
593,249✔
4866
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4867
    return true;
593,249✔
4868
  }
4869

4870
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
69,727✔
4871
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
39,660✔
4872
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4873
    return true;
39,660✔
4874
  }  
4875

4876
  if (pStatus) {
30,067✔
4877
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
29,857✔
4878
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4879
    }
4880

4881
    return true;
29,857✔
4882
  }
4883

4884
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
210✔
4885

4886
  return true;
210✔
4887
}
4888

4889
void msmCheckLoopStreamMap(SMnode *pMnode) {
50,961✔
4890
  SStmStatus* pStatus = NULL;
50,961✔
4891
  void* pIter = NULL;
50,961✔
4892
  int8_t stopped = 0;
50,961✔
4893
  int64_t streamId = 0;
50,961✔
4894
  
4895
  while (true) {
4896
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
324,872✔
4897
    if (NULL == pIter) {
324,872✔
4898
      break;
50,961✔
4899
    }
4900

4901
    pStatus = (SStmStatus*)pIter;
273,911✔
4902

4903
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
273,911✔
4904
    stopped = atomic_load_8(&pStatus->stopped);
273,911✔
4905
    if (MST_IS_USER_STOPPED(stopped)) {
273,911✔
4906
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
450✔
4907
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
450✔
4908
      continue;
450✔
4909
    }
4910

4911
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
273,461✔
4912
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4913
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4914
      continue;
×
4915
    }
4916

4917
    if (MST_IS_ERROR_STOPPED(stopped)) {
273,461✔
4918
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
15,485✔
4919
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
10,398✔
4920
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4921
            
4922
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
10,398✔
4923
        continue;
10,398✔
4924
      }
4925

4926
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
5,087✔
4927
      continue;
5,087✔
4928
    }
4929

4930
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
257,976✔
4931
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4932
      continue;
×
4933
    }
4934
  }
4935
}
50,961✔
4936

4937
void msmCheckStreamsStatus(SMnode *pMnode) {
438,649✔
4938
  SStmCheckStatusCtx ctx = {0};
438,649✔
4939

4940
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
438,649✔
4941
  
4942
  if (MST_READY_FOR_SDB_LOOP()) {
438,649✔
4943
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
106,917✔
4944
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
106,917✔
4945
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
106,917✔
4946
  }
4947

4948
  if (MST_READY_FOR_MAP_LOOP()) {
438,649✔
4949
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
50,961✔
4950
    msmCheckLoopStreamMap(pMnode);
50,961✔
4951
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
50,961✔
4952
  }
4953
}
438,649✔
4954

4955
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
5,077,513✔
4956
  for (int32_t i = 0; i < taskNum; ++i) {
10,767,241✔
4957
    SStmTaskStatus* pTask = *(pList + i);
5,689,728✔
4958

4959
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
5,689,728✔
4960
      continue;
626,782✔
4961
    }
4962
    
4963
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
5,065,124✔
4964
      continue;
5,063,766✔
4965
    }
4966

4967
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,358✔
4968
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,358✔
4969
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4970
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4971
          
4972
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
4973
      break;
×
4974
    }
4975

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

4979
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,358✔
4980
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,358✔
4981

4982
    SStmTaskAction task = {0};
1,358✔
4983
    task.streamId = streamId;
1,358✔
4984
    task.id = pTask->id;
1,358✔
4985
    task.flag = pTask->flags;
1,358✔
4986
    task.type = pTask->type;
1,358✔
4987
    
4988
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,358✔
4989
  }
4990
}
5,077,513✔
4991

4992
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
178,292✔
4993
  void* pIter = NULL;
178,292✔
4994
  SStmVgStreamStatus* pVg = NULL;
178,292✔
4995
  int64_t streamId = 0;
178,292✔
4996
  
4997
  while (true) {
2,063,602✔
4998
    pIter = taosHashIterate(pStreams, pIter);
2,241,894✔
4999
    if (NULL == pIter) {
2,241,894✔
5000
      break;
178,292✔
5001
    }
5002

5003
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,063,602✔
5004
    pVg = (SStmVgStreamStatus*)pIter;
2,063,602✔
5005

5006
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
2,063,602✔
5007
    if (taskNum > 0) {
2,063,602✔
5008
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,209,166✔
5009
    }
5010

5011
    taskNum = taosArrayGetSize(pVg->calcReaders);
2,063,602✔
5012
    if (taskNum > 0) {
2,063,602✔
5013
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,388,772✔
5014
    }
5015
  }
5016
}
178,292✔
5017

5018
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
5019
  int64_t streamId = 0;
×
5020
  void* pIter = NULL;
×
5021
  SStmVgStreamStatus* pStream = NULL;
×
5022

5023
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
5024
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5025
    return;
×
5026
  }
5027

5028
  
5029
  while (true) {
5030
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
5031
    if (NULL == pIter) {
×
5032
      break;
×
5033
    }
5034

5035
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
5036
    
5037
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
5038
  }
5039

5040
  taosHashClear(pVg->streamTasks);
×
5041
}
5042

5043

5044
void msmCheckVgroupStatus(SMnode *pMnode) {
438,649✔
5045
  void* pIter = NULL;
438,649✔
5046
  int32_t code = 0;
438,649✔
5047
  
5048
  while (true) {
1,689,602✔
5049
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
2,128,251✔
5050
    if (NULL == pIter) {
2,128,251✔
5051
      break;
438,649✔
5052
    }
5053

5054
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,689,602✔
5055
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,689,602✔
5056
      continue;
1,505,678✔
5057
    }
5058
    
5059
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
183,924✔
5060

5061
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
183,924✔
5062
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
5,632✔
5063
      if (NULL == pVgroup) {
5,632✔
5064
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
5,632✔
5065
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
5,632✔
5066
        if (code) {
5,632✔
5067
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
5068
        }
5069
        continue;
5,632✔
5070
      }
5071
      mndReleaseVgroup(pMnode, pVgroup);
×
5072
      
5073
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5074
      
5075
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
5076
      continue;
×
5077
    }
5078

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

5081
    msmCheckVgroupStreamStatus(pVg->streamTasks);
178,292✔
5082
  }
5083
}
438,649✔
5084

5085
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
382✔
5086
  *deployNum = 0;
382✔
5087
  
5088
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,146✔
5089
    if (pStream->runners[i]) {
955✔
5090
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
382✔
5091
      for (int32_t t = 0; t < taskNum; ++t) {
573✔
5092
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
382✔
5093
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
382✔
5094
        if (stopped) {
382✔
5095
          mstsDebug("stream already stopped %d, ignore it", stopped);
191✔
5096
          *deployNum = 0;
191✔
5097
          return;
191✔
5098
        }
5099

5100
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
191✔
5101
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
191✔
5102
      }
5103
      
5104
      deployId[*deployNum] = i;
191✔
5105
      (*deployNum)++;
191✔
5106
    }
5107
  }
5108
}
5109

5110
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
1,337✔
5111
  pSnode->runnerThreadNum = -1;
1,337✔
5112

5113
  (void)msmSTAddSnodesToMap(pMnode);
1,337✔
5114

5115
  int64_t streamId = 0;
1,337✔
5116
  void* pIter = NULL;
1,337✔
5117
  SStmSnodeStreamStatus* pStream = NULL;
1,337✔
5118
  int32_t deployNum = 0;
1,337✔
5119
  SStmTaskAction task = {0};
1,337✔
5120
  
5121
  while (true) {
5122
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
1,910✔
5123
    if (NULL == pIter) {
1,910✔
5124
      break;
1,337✔
5125
    }
5126

5127
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
573✔
5128
    
5129
    task.streamId = streamId;
573✔
5130
    
5131
    pStream = (SStmSnodeStreamStatus*)pIter;
573✔
5132
    if (pStream->trigger) {
573✔
5133
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
191✔
5134
      if (stopped) {
191✔
5135
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
5136
        continue;
×
5137
      }
5138

5139
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
191✔
5140
      
5141
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
191✔
5142
    } else {
5143
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
382✔
5144
      
5145
      if (task.deployNum > 0) {
382✔
5146
        //task.triggerStatus = pStream->trigger;
5147
        task.multiRunner = true;
191✔
5148
        task.type = STREAM_RUNNER_TASK;
191✔
5149
        
5150
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
191✔
5151
        
5152
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
191✔
5153
      }
5154
    }
5155
  }
5156

5157
  taosHashClear(pSnode->streamTasks);
1,337✔
5158
}
1,337✔
5159

5160

5161
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
50,662✔
5162
  void* pIter = NULL;
50,662✔
5163
  SStmSnodeStreamStatus* pSnode = NULL;
50,662✔
5164
  int64_t streamId = 0;
50,662✔
5165
  
5166
  while (true) {
5167
    pIter = taosHashIterate(pStreams, pIter);
702,969✔
5168
    if (NULL == pIter) {
702,969✔
5169
      break;
50,662✔
5170
    }
5171

5172
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
652,307✔
5173
    pSnode = (SStmSnodeStreamStatus*)pIter;
652,307✔
5174

5175
    if (NULL != pSnode->trigger) {
652,307✔
5176
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
637,309✔
5177
    }
5178

5179
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,609,228✔
5180
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,956,921✔
5181
      if (taskNum > 0) {
1,956,921✔
5182
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,842,266✔
5183
      }
5184
    }
5185
  }
5186
}
50,662✔
5187

5188

5189
void msmCheckSnodeStatus(SMnode *pMnode) {
438,649✔
5190
  void* pIter = NULL;
438,649✔
5191
  
5192
  while (true) {
544,410✔
5193
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
983,059✔
5194
    if (NULL == pIter) {
983,059✔
5195
      break;
438,649✔
5196
    }
5197

5198
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
544,410✔
5199
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
544,410✔
5200
      continue;
480,442✔
5201
    }
5202

5203
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
63,968✔
5204
    
5205
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
63,968✔
5206
    if (NULL == pSnode->streamTasks) {
63,968✔
5207
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
12,160✔
5208
      continue;
12,160✔
5209
    }
5210
    
5211
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
51,808✔
5212
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,146✔
5213
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5214
      
5215
      msmHandleSnodeLost(pMnode, pSnode);
1,146✔
5216
      continue;
1,146✔
5217
    }
5218
    
5219
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
50,662✔
5220

5221
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
50,662✔
5222
  }
5223
}
438,649✔
5224

5225

5226
void msmCheckTasksStatus(SMnode *pMnode) {
438,649✔
5227
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
438,649✔
5228

5229
  msmCheckVgroupStatus(pMnode);
438,649✔
5230
  msmCheckSnodeStatus(pMnode);
438,649✔
5231
}
438,649✔
5232

5233
void msmCheckSnodesState(SMnode *pMnode) {
438,649✔
5234
  if (!MST_READY_FOR_SNODE_LOOP()) {
438,649✔
5235
    return;
392,821✔
5236
  }
5237

5238
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
45,828✔
5239

5240
  void* pIter = NULL;
45,828✔
5241
  int32_t snodeId = 0;
45,828✔
5242
  while (true) {
51,080✔
5243
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
96,908✔
5244
    if (NULL == pIter) {
96,908✔
5245
      break;
45,828✔
5246
    }
5247

5248
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
51,080✔
5249
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
51,080✔
5250
      continue;
50,889✔
5251
    }
5252

5253
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
191✔
5254
    if (NULL == pSnode->streamTasks) {
191✔
5255
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5256
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5257
      continue;
×
5258
    }
5259
    
5260
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
191✔
5261
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5262
    
5263
    msmHandleSnodeLost(pMnode, pSnode);
191✔
5264
  }
5265

5266
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
45,828✔
5267
}
5268

5269
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
11,555,104✔
5270
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
11,555,104✔
5271
  if (0 == active || MND_STM_STATE_NORMAL != state) {
11,555,104✔
5272
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,942✔
5273
    return false;
2,942✔
5274
  }
5275

5276
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
11,552,162✔
5277
    mstTrace("ignore health check since no stream now");
10,674,864✔
5278
    return false;
10,674,864✔
5279
  }
5280

5281
  return true;
877,298✔
5282
}
5283

5284
void msmHealthCheck(SMnode *pMnode) {
11,116,455✔
5285
  if (!msmCheckNeedHealthCheck(pMnode)) {
11,116,455✔
5286
    return;
10,677,806✔
5287
  }
5288

5289
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
859,926✔
5290
  
5291
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
438,649✔
5292
  if (!msmCheckNeedHealthCheck(pMnode)) {
438,649✔
5293
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5294
    return;
×
5295
  }
5296
  
5297
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
438,649✔
5298
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
438,649✔
5299

5300
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
438,649✔
5301
  
5302
  msmCheckStreamsStatus(pMnode);
438,649✔
5303
  msmCheckTasksStatus(pMnode);
438,649✔
5304
  msmCheckSnodesState(pMnode);
438,649✔
5305

5306
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
438,649✔
5307

5308
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
438,649✔
5309
}
5310

5311
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
908✔
5312
  SStreamObj *pStream = pObj;
908✔
5313
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
908✔
5314
    return true;
×
5315
  }
5316
  
5317
  pStream->updateTime = *(int64_t*)p1;
908✔
5318
  
5319
  (*(int32_t*)p2)++;
908✔
5320
  
5321
  return true;
908✔
5322
}
5323

5324
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
24,464✔
5325
  int32_t code = 0;
24,464✔
5326
  int8_t  stopped = 0;
24,464✔
5327
  
5328
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
24,464✔
5329
  
5330
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
24,464✔
5331
  if (NULL == pStatus) {
24,464✔
5332
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,614✔
5333
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,614✔
5334
    goto _exit;
3,614✔
5335
  }
5336

5337
  stopped = atomic_load_8(&pStatus->stopped);
20,850✔
5338
  if (stopped) {
20,850✔
5339
    mstsError("stream already stopped, stopped:%d", stopped);
×
5340
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5341
    goto _exit;
×
5342
  }
5343

5344
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
20,850✔
5345
    pAddr->taskId = pStatus->triggerTask->id.taskId;
16,541✔
5346
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
16,541✔
5347
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
16,541✔
5348
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
16,541✔
5349
    goto _exit;
16,541✔
5350
  }
5351

5352
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
4,309✔
5353
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
4,309✔
5354

5355
_exit:
24,464✔
5356
  
5357
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
24,464✔
5358

5359
  return code;
24,464✔
5360
}
5361

5362
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
439,476✔
5363
  int32_t code = TSDB_CODE_SUCCESS;
439,476✔
5364
  int32_t lino = 0;
439,476✔
5365
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
439,476✔
5366
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
439,476✔
5367
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
439,476✔
5368

5369
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
877,172✔
5370

5371
  mStreamMgmt.stat.activeTimes++;
439,476✔
5372
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
439,476✔
5373
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
439,476✔
5374
  if (NULL == mStreamMgmt.tCtx) {
439,476✔
5375
    code = terrno;
×
5376
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5377
    goto _exit;
×
5378
  }
5379

5380
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
439,476✔
5381
  if (mStreamMgmt.actionQ == NULL) {
439,476✔
5382
    code = terrno;
×
5383
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5384
    goto _exit;
×
5385
  }
5386
  
5387
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
439,476✔
5388
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
439,476✔
5389
  
5390
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
439,476✔
5391
  
5392
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,631,868✔
5393
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,192,392✔
5394

5395
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
13,154,352✔
5396
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,961,960✔
5397
      if (pCtx->deployStm[m] == NULL) {
10,961,960✔
5398
        code = terrno;
×
5399
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5400
        goto _exit;
×
5401
      }
5402
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
10,961,960✔
5403
      
5404
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,961,960✔
5405
      if (pCtx->actionStm[m] == NULL) {
10,961,960✔
5406
        code = terrno;
×
5407
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5408
        goto _exit;
×
5409
      }
5410
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
10,961,960✔
5411
    }
5412
  }
5413
  
5414
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
439,476✔
5415
  if (mStreamMgmt.streamMap == NULL) {
439,476✔
5416
    code = terrno;
×
5417
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5418
    goto _exit;
×
5419
  }
5420
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
439,476✔
5421
  
5422
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
439,476✔
5423
  if (mStreamMgmt.taskMap == NULL) {
439,476✔
5424
    code = terrno;
×
5425
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5426
    goto _exit;
×
5427
  }
5428
  
5429
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
439,476✔
5430
  if (mStreamMgmt.vgroupMap == NULL) {
439,476✔
5431
    code = terrno;
×
5432
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5433
    goto _exit;
×
5434
  }
5435
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
439,476✔
5436

5437
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
439,476✔
5438
  if (mStreamMgmt.snodeMap == NULL) {
439,476✔
5439
    code = terrno;
×
5440
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5441
    goto _exit;
×
5442
  }
5443
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
439,476✔
5444
  
5445
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
439,476✔
5446
  if (mStreamMgmt.dnodeMap == NULL) {
439,476✔
5447
    code = terrno;
×
5448
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5449
    goto _exit;
×
5450
  }
5451

5452
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
439,476✔
5453
  if (mStreamMgmt.toDeployVgMap == NULL) {
439,476✔
5454
    code = terrno;
×
5455
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5456
    goto _exit;
×
5457
  }
5458
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
439,476✔
5459
  
5460
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
439,476✔
5461
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
439,476✔
5462
    code = terrno;
×
5463
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5464
    goto _exit;
×
5465
  }
5466
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
439,476✔
5467

5468
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
439,476✔
5469
  if (mStreamMgmt.toUpdateScanMap == NULL) {
439,476✔
5470
    code = terrno;
×
5471
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5472
    goto _exit;
×
5473
  }
5474
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
439,476✔
5475

5476
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
439,476✔
5477
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
439,476✔
5478

5479
  mStreamMgmt.lastTaskId = 1;
439,476✔
5480

5481
  int32_t activeStreamNum = 0;
439,476✔
5482
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
439,476✔
5483

5484
  if (activeStreamNum > 0) {
439,476✔
5485
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
728✔
5486
  } else {
5487
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
438,748✔
5488
  }
5489

5490
_exit:
439,476✔
5491

5492
  if (code) {
439,476✔
5493
    msmDestroyRuntimeInfo(pMnode);
×
5494
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5495
  } else {
5496
    mstInfo("mnode stream runtime init done");
439,476✔
5497
  }
5498

5499
  return code;
439,476✔
5500
}
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