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

taosdata / TDengine / #4997

20 Mar 2026 06:10AM UTC coverage: 71.739% (-0.3%) from 72.069%
#4997

push

travis-ci

web-flow
feat: add query phase tracking for SHOW QUERIES (#34706)

148 of 183 new or added lines in 10 files covered. (80.87%)

9273 existing lines in 172 files now uncovered.

244572 of 340921 relevant lines covered (71.74%)

133392941.95 hits per line

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

73.35
/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() {
818,682✔
34
  SStmQNode* pQNode = NULL;
818,682✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
818,682✔
37
    return;
409,341✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,041,847✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,251,082✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,209,235✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,209,235✔
51
  }
52
}
2,041,847✔
53

54
void msmDestroyThreadCtxs() {
818,682✔
55
  if (NULL == mStreamMgmt.tCtx) {
818,682✔
56
    return;
409,341✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,451,188✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,041,847✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
409,341✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
818,682✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
818,682✔
94
}
818,682✔
95

96

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

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

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

111
    pStatus = pStream;
184✔
112
  }
113

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

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

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

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

136
_exit:
460✔
137

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

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

145

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

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

200
  pSnode = NULL;
434,304✔
201

202
_exit:
434,304✔
203

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

208
  return code;
434,304✔
209
}
210

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

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

235
  pDnode = NULL;
902,315✔
236

237
_exit:
902,315✔
238

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

243
  return code;
902,315✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
529,607✔
275
  int32_t code = TSDB_CODE_SUCCESS;
529,607✔
276
  int32_t lino = 0;
529,607✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
529,607✔
278
  if (NULL == pStream) {
529,607✔
279
    SStmVgStreamStatus stream = {0};
384,598✔
280
    if (trigReader) {
384,598✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
249,673✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
249,673✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
499,346✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
134,925✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
134,925✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
269,850✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
384,598✔
290
    goto _exit;
384,598✔
291
  }
292
  
293
  if (trigReader) {
145,009✔
294
    if (NULL == pStream->trigReaders) {
20,022✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
20,022✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
20,022✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
40,044✔
300
    goto _exit;
20,022✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
124,987✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
93,268✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
93,268✔
306
  }
307

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

310
_exit:
124,987✔
311

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

320
  return code;
529,607✔
321
}
322

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

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

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

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

350
  return code;
529,607✔
351
}
352

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

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

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

404
  return code;
530,711✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
752,035✔
409
  int32_t code = TSDB_CODE_SUCCESS;
752,035✔
410
  int32_t lino = 0;
752,035✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
752,035✔
412
  if (NULL == pStream) {
752,035✔
413
    SStmSnodeStreamStatus stream = {0};
180,671✔
414
    if (deployId < 0) {
180,671✔
415
      stream.trigger = pStatus;
2,878✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
177,793✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
177,793✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
355,586✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
180,671✔
423
    goto _exit;
180,671✔
424
  }
425
  
426
  if (deployId < 0) {
571,364✔
427
    if (NULL != pStream->trigger) {
172,431✔
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;
172,431✔
433
    goto _exit;
172,431✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
398,933✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
339,500✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
339,500✔
439
  }
440

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

443
_exit:
398,933✔
444

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

453
  return code;
752,035✔
454
}
455

456

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

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

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

484
  return code;
752,035✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
175,309✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
46,378✔
568
      ext.deployed = false;
46,378✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
92,756✔
570

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

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

614
  return code;
576,726✔
615
}
616

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

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

632
_exit:
532,272✔
633

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

638
  return code;
532,272✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
13,718✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
268✔
682

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

687
  return code;  
268✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
75,414✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
15,507,443✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
61,162,665✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
45,655,222✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
45,655,222✔
725
  }
726

727
_exit:
15,507,443✔
728

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

733
  return code;
15,507,443✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
326,636✔
739
  int32_t num = taosArrayGetSize(pList);
326,636✔
740
  for (int32_t i = 0; i < num; ++i) {
921,399✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
755,538✔
742
    if (pScan->readFromCache) {
755,538✔
743
      return pScan->scanPlan;
160,775✔
744
    }
745
  }
746

747
  return NULL;
165,861✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
530,711✔
775
}
776

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

781
  if (pInfo->runnerDeploys > 0) {
172,431✔
782
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
172,431✔
783
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
172,431✔
784
  }
785
  
786
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
689,724✔
787
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
517,293✔
788
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
517,293✔
789

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

805
_exit:
172,431✔
806

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

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

826
  mndReleaseSnode(pMnode, pSnode);
×
827

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

833
  return TSDB_CODE_SUCCESS;
×
834
}
835

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

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

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

865
  pMsg->eventTypes = pStream->pCreate->eventTypes;
175,309✔
866
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
175,309✔
867
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
175,309✔
868
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
175,309✔
869
  pMsg->calcPkSlotId = pStream->pCreate->calcPkSlotId;
175,309✔
870
  pMsg->triPkSlotId = pStream->pCreate->triPkSlotId;
175,309✔
871
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
175,309✔
872
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
175,309✔
873
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
56,389✔
874
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
56,389✔
875
  }
876

877
  SStreamTaskAddr addr;
174,877✔
878
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
175,309✔
879
  if (triggerReaderNum > 0) {
175,309✔
880
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
174,414✔
881
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
174,414✔
882
  }
883
  
884
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
391,940✔
885
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
216,631✔
886
    addr.taskId = pStatus->id.taskId;
216,631✔
887
    addr.nodeId = pStatus->id.nodeId;
216,631✔
888
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
216,631✔
889
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
433,262✔
890
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
216,631✔
891
  }
892

893
  pMsg->leaderSnodeId = pStream->mainSnodeId;
175,309✔
894
  pMsg->streamName = pInfo->streamName;
175,309✔
895

896
  if (0 == pInfo->runnerNum) {
175,309✔
897
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
2,878✔
898
    return code;
2,878✔
899
  }
900

901
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
172,431✔
902

903
_exit:
172,431✔
904

905
  if (code) {
172,431✔
906
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
907
  } else {
908
    mstsDebug("trigger deploy info built, readerNum:%d, runnerNum:%d", (int32_t)taosArrayGetSize(pMsg->readerList), (int32_t)taosArrayGetSize(pMsg->runnerList));
172,431✔
909
  }
910
  
911
  return TSDB_CODE_SUCCESS;
172,431✔
912
}
913

914

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

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

948
  pMsg->colCids = pInfo->pCreate->colCids;
576,726✔
949
  pMsg->tagCids = pInfo->pCreate->tagCids;
576,726✔
950
_exit:
576,726✔
951

952
  if (code) {
576,726✔
953
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
954
  }
955
  
956
  return code;
576,726✔
957
}
958

959

960
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
406,080✔
961
  int32_t code = TSDB_CODE_SUCCESS;
406,080✔
962
  int32_t lino = 0;
406,080✔
963
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
406,080✔
964
  SListNode* pNode = pList ? listHead(pList) : NULL;
406,080✔
965
  
966
  for (int32_t i = 0; i < taskNum; ++i) {
935,687✔
967
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
529,607✔
968
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
529,607✔
969
    if (pNode) {
529,607✔
970
      pNode = TD_DLIST_NODE_NEXT(pNode);
257,514✔
971
    }
972
  }
973
  
974
_exit:
406,080✔
975

976
  if (code) {
406,080✔
977
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
978
  }
979
  
980
  return code;
406,080✔
981
}
982

983

984
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
692,602✔
985
  int32_t code = TSDB_CODE_SUCCESS;
692,602✔
986
  int32_t lino = 0;
692,602✔
987
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
692,602✔
988
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
692,602✔
989
  
990
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,444,637✔
991
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
752,035✔
992
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
752,035✔
993
  }
994
  
995
_exit:
692,602✔
996

997
  if (code) {
692,602✔
998
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
999
  }
1000

1001
  return code;
692,602✔
1002
}
1003

1004
int64_t msmAssignTaskId(void) {
1,281,642✔
1005
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,281,642✔
1006
}
1007

1008
int64_t msmAssignTaskSeriousId(void) {
1,281,642✔
1009
  return taosGetTimestampNs();
1,281,642✔
1010
}
1011

1012

1013
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
907,245✔
1014
  int32_t code = TSDB_CODE_SUCCESS;
907,245✔
1015
  int32_t lino = 0;
907,245✔
1016
  bool     noExists = false;
907,245✔
1017
  SStmSnodeStatus* pStatus = NULL;
907,245✔
1018

1019
  while (true) {
1020
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
917,596✔
1021
    if (NULL == pStatus) {
917,596✔
1022
      if (noExists) {
10,351✔
1023
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1024
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1025
      }
1026

1027
      noExists = true;
10,351✔
1028
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
10,351✔
1029
      
1030
      continue;
10,351✔
1031
    }
1032

1033
    *alive = (pStatus->runnerThreadNum >= 0);
907,245✔
1034
    break;
907,245✔
1035
  }
1036

1037
_exit:
907,245✔
1038

1039
  if (code) {
907,245✔
1040
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1041
  }
1042

1043
  return code;
907,245✔
1044
}
1045

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

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

1074
      snodeId = pSnode->replicaId;
×
1075
      mndReleaseSnode(pMnode, pSnode);
×
1076
      
1077
      continue;
×
1078
    }
1079

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

1084
_exit:
×
1085

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

1090
  return 0;
×
1091
}
1092

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

1107
  int32_t snodeTarget = taosRand() % snodeNum;
536,038✔
1108

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

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

1134
    snodeId = pObj->id;
554,512✔
1135
    if (snodeIdx == snodeTarget) {
554,512✔
1136
      sdbRelease(pMnode->pSdb, pObj);
536,038✔
1137
      sdbCancelFetch(pMnode->pSdb, pIter);
536,038✔
1138
      pObj = NULL;
536,038✔
1139
      goto _exit;
536,038✔
1140
    }
1141

1142
    sdbRelease(pMnode->pSdb, pObj);
18,474✔
1143
    snodeIdx++;
18,474✔
1144
  }
1145

1146
_exit:
538,648✔
1147

1148
  if (code) {
538,648✔
1149
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1150
  }
1151

1152
  if (0 == snodeId) {
538,648✔
1153
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,610✔
1154
  }
1155

1156
  return snodeId;
538,648✔
1157
}
1158

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

1168
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
707,581✔
1169

1170
_exit:
707,581✔
1171

1172
  if (0 == snodeId) {
707,581✔
1173
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1174
  }
1175

1176
  return snodeId;
707,581✔
1177
}
1178

1179

1180
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
175,309✔
1181
  int32_t code = TSDB_CODE_SUCCESS;
175,309✔
1182
  int32_t lino = 0;
175,309✔
1183
  int64_t streamId = pStream->pCreate->streamId;
175,309✔
1184

1185
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
175,309✔
1186
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
175,309✔
1187

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

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

1209
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
175,309✔
1210
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
175,309✔
1211

1212
_exit:
175,309✔
1213

1214
  if (code) {
175,309✔
1215
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1216
  }
1217

1218
  return code;
175,309✔
1219
}
1220

1221
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
269,695✔
1222
  int32_t code = TSDB_CODE_SUCCESS;
269,695✔
1223
  int32_t lino = 0;
269,695✔
1224

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

1247
_exit:
269,695✔
1248

1249
  if (code) {
269,695✔
1250
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1251
  }
1252

1253
  return code;
269,695✔
1254
}
1255

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

1285
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
74,728✔
1286
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
74,728✔
1287
      
1288
      void *pIter = NULL;
74,728✔
1289
      while (1) {
375,170✔
1290
        SStmTaskDeploy info = {0};
449,898✔
1291
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
449,898✔
1292
        if (pIter == NULL) {
449,898✔
1293
          break;
74,728✔
1294
        }
1295
      
1296
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
375,170✔
1297
          pState = taosArrayReserve(pInfo->trigReaders, 1);
116,945✔
1298

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

1308
        sdbRelease(pSdb, pVgroup);
375,170✔
1309
      }
1310
      break;
74,728✔
1311
    }
1312
    default:
895✔
1313
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
895✔
1314
      break;
895✔
1315
  }
1316

1317
_exit:
175,309✔
1318

1319
  mndReleaseDb(pCtx->pMnode, pDb);
175,309✔
1320

1321
  if (code) {
175,309✔
1322
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1323
  }
1324

1325
  return code;
175,309✔
1326
}
1327

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

1351
  key[1] = pSubplan->id.subplanId;
257,514✔
1352

1353
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
257,514✔
1354
  if (NULL == ppRes) {
257,514✔
1355
    SArray* pRes = taosArrayInit(1, sizeof(addr));
252,521✔
1356
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
252,521✔
1357
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
505,042✔
1358
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
252,521✔
1359
  } else {
1360
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
9,986✔
1361
  }
1362

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

1369
  nodesDestroyNode((SNode*)pSubplan);
257,514✔
1370

1371
  if (code) {
257,514✔
1372
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1373
  }
1374

1375
  return code;
257,514✔
1376
}
1377

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

1386
  SStmTaskSrcAddr addr;
75,948✔
1387
  addr.isFromCache = true;
75,948✔
1388
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
75,948✔
1389
  addr.taskId = pCtx->triggerTaskId;
75,948✔
1390
  addr.vgId = pCtx->triggerNodeId;
75,948✔
1391
  addr.groupId = pSubplan->id.groupId;
75,948✔
1392

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

1408
  nodesDestroyNode((SNode*)pSubplan);
75,948✔
1409
  
1410
  if (code) {
75,948✔
1411
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1412
  }
1413

1414
  return code;
75,948✔
1415
}
1416

1417

1418
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
259,912✔
1419
  int32_t code = TSDB_CODE_SUCCESS;
259,912✔
1420
  int32_t lino = 0;
259,912✔
1421

1422
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
259,912✔
1423

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

1446
_exit:
259,912✔
1447

1448
  if (code) {
259,912✔
1449
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1450
  }
1451

1452
  return code;
259,912✔
1453
}
1454

1455

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

1465
  
1466
  for (int32_t i = 0; i < calcTasksNum; ++i) {
508,771✔
1467
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
333,462✔
1468
    if (pScan->readFromCache) {
333,462✔
1469
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
75,948✔
1470
      continue;
75,948✔
1471
    }
1472
    
1473
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
257,514✔
1474
    for (int32_t m = 0; m < vgNum; ++m) {
515,028✔
1475
      pState = tdListReserve(pInfo->calcReaders);
257,514✔
1476
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
257,514✔
1477

1478
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
257,514✔
1479
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
257,514✔
1480
    }
1481
  }
1482

1483
_exit:
175,309✔
1484

1485
  if (code) {
175,309✔
1486
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1487
  }
1488

1489
  return code;
175,309✔
1490
}
1491

1492

1493

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

UNCOV
1521
_exit:
×
1522

UNCOV
1523
  if (code) {
×
1524
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1525
  }
1526

UNCOV
1527
  return code;
×
1528
}
1529

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

1538
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
175,309✔
1539
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
175,309✔
1540
  
1541
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
175,309✔
1542
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
175,309✔
1543
  
1544
_exit:
175,309✔
1545

1546
  if (code) {
175,309✔
1547
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1548
  }
1549
  
1550
  return code;
175,309✔
1551
}
1552

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

1564
  source.addr.epSet = pSrc->epset;
1,079,208✔
1565
  source.addr.nodeId = pSrc->vgId;
1,079,208✔
1566

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

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

1586
  mstsError("subplanId %d not found in runner list", subplanId);
×
1587

1588
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1589
}
1590

1591
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
576,726✔
1592
  int32_t code = TSDB_CODE_SUCCESS;
576,726✔
1593
  int32_t lino = 0;
576,726✔
1594
  int64_t key[2] = {streamId, -1};
576,726✔
1595
  SNode* pNode = NULL;
576,726✔
1596
  SStreamTask* pTask = &pDeploy->task;
576,726✔
1597
  FOREACH(pNode, pPlan->pChildren) {
1,621,566✔
1598
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,044,840✔
1599
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
44,454✔
1600
      continue;
44,454✔
1601
    }
1602
    
1603
    SValueNode* pVal = (SValueNode*)pNode;
1,000,386✔
1604
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,000,386✔
1605
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1606
      continue;
×
1607
    }
1608

1609
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,000,386✔
1610

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

1617
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,000,386✔
1618
    for (int32_t i = 0; i < childrenNum; ++i) {
2,035,140✔
1619
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,034,754✔
1620
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,034,754✔
1621
    }
1622
  }
1623

1624
_exit:
576,726✔
1625

1626
  if (code) {
576,726✔
1627
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1628
  }
1629

1630
  return code;
576,726✔
1631
}
1632

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

1641
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
576,726✔
1642

1643
  SNode* pTmp = NULL;
576,726✔
1644
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,621,566✔
1645
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,044,840✔
1646
      ERASE_NODE(pPlan->pChildren);
1,000,386✔
1647
      continue;
1,000,386✔
1648
    }
1649
    WHERE_NEXT;
44,454✔
1650
  }
1651
  nodesClearList(pPlan->pChildren);
576,726✔
1652
  pPlan->pChildren = NULL;
576,726✔
1653

1654
  if (NULL == pPlan->pParents) {
576,726✔
1655
    goto _exit;
532,272✔
1656
  }
1657

1658
  SNode* pNode = NULL;
44,454✔
1659
  int64_t parentTaskId = 0;
44,454✔
1660
  SStmTaskSrcAddr addr = {0};
44,454✔
1661
  addr.taskId = pDeploy->task.taskId;
44,454✔
1662
  addr.vgId = pDeploy->task.nodeId;
44,454✔
1663
  addr.groupId = pPlan->id.groupId;
44,454✔
1664
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
44,454✔
1665
  FOREACH(pNode, pPlan->pParents) {
88,908✔
1666
    SSubplan* pSubplan = (SSubplan*)pNode;
44,454✔
1667
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
44,454✔
1668
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
44,454✔
1669
  }
1670
  
1671
_exit:
575,430✔
1672

1673
  if (code) {
576,726✔
1674
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1675
  }
1676

1677
  return code;
576,726✔
1678
}
1679

1680
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
532,272✔
1681
  int32_t code = TSDB_CODE_SUCCESS;
532,272✔
1682
  int32_t lino = 0;
532,272✔
1683
  int64_t streamId = pStream->pCreate->streamId;
532,272✔
1684
  int32_t runnerNum = taosArrayGetSize(pRunners);
532,272✔
1685
  
1686
  for (int32_t i = 0; i < runnerNum; ++i) {
1,108,998✔
1687
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
576,726✔
1688
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
576,726✔
1689
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
576,726✔
1690

1691
    SStreamTask* pTask = &pDeploy->task;
576,726✔
1692
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
576,726✔
1693
  }
1694

1695
_exit:
532,272✔
1696

1697
  if (code) {
532,272✔
1698
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1699
  }
1700

1701
  return code;
532,272✔
1702
}
1703

1704
static int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, int32_t dagIdx, SStmStatus* pInfo, SStreamObj* pStream, SQueryPlan** pRoot, SNodeList** subEP) {
177,424✔
1705
  int32_t code = 0;
177,424✔
1706
  int32_t lino = 0;
177,424✔
1707
  int64_t streamId = pStream->pCreate->streamId;
177,424✔
1708
  SArray* deployTaskList = NULL;
177,424✔
1709
  SArray* deployList = NULL;
177,424✔
1710
  int32_t deployNodeId = 0;
177,424✔
1711
  SStmTaskStatus* pState = NULL;
177,424✔
1712
  int32_t taskIdx = 0;
177,424✔
1713
  SNodeListNode *plans = NULL;
177,424✔
1714
  SQueryPlan    *pDag = NULL;
177,424✔
1715
  int32_t        taskNum = 0;
177,424✔
1716
  int32_t        totalTaskNum = 0;
177,424✔
1717
  bool           subQ = false;
177,424✔
1718

1719
  if (dagIdx >= 0) {
177,424✔
1720
    subQ = true;
4,993✔
1721
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
4,993✔
1722
  } else {
1723
    pDag = *pRoot;
172,431✔
1724
  }
1725

1726
  if (pDag->numOfSubplans <= 0) {
177,424✔
1727
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1728
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1729
  }
1730

1731
  if ((*pRoot)->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
177,424✔
1732
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1733
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1734
  }
1735

1736
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
177,424✔
1737
  if (levelNum <= 0) {
177,424✔
1738
    mstsError("invalid level num:%d", levelNum);
×
1739
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1740
  }
1741

1742
  int32_t        lowestLevelIdx = levelNum - 1;
177,424✔
1743
  
1744
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
177,424✔
1745
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
177,424✔
1746
    mstsError("invalid level plan, level:0, planNodeType:%d", nodeType(plans));
×
1747
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1748
  }
1749
  
1750
  taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
177,424✔
1751
  if (taskNum != 1) {
177,424✔
1752
    mstsError("invalid level plan number:%d, level:0", taskNum);
×
1753
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1754
  }
1755

1756
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
177,424✔
1757
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
177,424✔
1758
  
1759
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
709,696✔
1760
    totalTaskNum = 0;
532,272✔
1761

1762
    deployList = pInfo->runners[deployId];
532,272✔
1763
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
532,272✔
1764
    if (!GOT_SNODE(deployNodeId)) {
532,272✔
1765
      TAOS_CHECK_EXIT(terrno);
×
1766
    }
1767

1768
    taskIdx = 0;
532,272✔
1769

1770
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,082,142✔
1771
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
549,870✔
1772
      if (NULL == plans) {
549,870✔
1773
        mstsError("empty level plan, level:%d", i);
×
1774
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1775
      }
1776

1777
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
549,870✔
1778
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1779
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1780
      }
1781

1782
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
549,870✔
1783
      if (taskNum <= 0) {
549,870✔
1784
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1785
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1786
      }
1787

1788
      totalTaskNum += taskNum;
549,870✔
1789
      if (totalTaskNum > pDag->numOfSubplans) {
549,870✔
1790
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1791
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1792
      }
1793

1794
      for (int32_t n = 0; n < taskNum; ++n) {
1,126,596✔
1795
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
576,726✔
1796
        pState = taosArrayReserve(deployList, 1);
576,726✔
1797

1798
        pState->id.taskId = msmAssignTaskId();
576,726✔
1799
        pState->id.deployId = deployId;
576,726✔
1800
        pState->id.seriousId = msmAssignTaskSeriousId();
576,726✔
1801
        pState->id.nodeId = deployNodeId;
576,726✔
1802
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
576,726✔
1803
        pState->type = STREAM_RUNNER_TASK;
576,726✔
1804
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
576,726✔
1805
        pState->status = STREAM_STATUS_UNDEPLOYED;
576,726✔
1806
        pState->lastUpTs = pCtx->currTs;
576,726✔
1807
        pState->pStream = pInfo;
576,726✔
1808

1809
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
576,726✔
1810
        pDeploy->task.type = pState->type;
576,726✔
1811
        pDeploy->task.streamId = streamId;
576,726✔
1812
        pDeploy->task.taskId = pState->id.taskId;
576,726✔
1813
        pDeploy->task.flags = pState->flags;
576,726✔
1814
        pDeploy->task.seriousId = pState->id.seriousId;
576,726✔
1815
        pDeploy->task.deployId = pState->id.deployId;
576,726✔
1816
        pDeploy->task.nodeId = pState->id.nodeId;
576,726✔
1817
        pDeploy->task.taskIdx = pState->id.taskIdx;
576,726✔
1818
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i && !subQ));
576,726✔
1819

1820
        SStreamTask* pTask = &pDeploy->task;
576,726✔
1821
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
576,726✔
1822
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1823

1824
        if (subQ) {
576,726✔
1825
          if (i == 0) {
14,979✔
1826
            SStmTaskSrcAddr addr = {0};
14,979✔
1827
            SDownstreamSourceNode* pSource = NULL;
14,979✔
1828

1829
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
14,979✔
1830

1831
            addr.taskId = pDeploy->task.taskId;
14,979✔
1832
            addr.vgId = pDeploy->task.nodeId;
14,979✔
1833
            addr.groupId = plan->id.groupId;
14,979✔
1834
            addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
14,979✔
1835

1836
            pSource->addr.epSet = addr.epset;
14,979✔
1837
            pSource->addr.nodeId = addr.vgId;
14,979✔
1838

1839
            pSource->clientId = streamId;
14,979✔
1840
            pSource->taskId = pDeploy->task.taskId;
14,979✔
1841
            pSource->sId = 0;
14,979✔
1842
            pSource->execId = 0;
14,979✔
1843
            pSource->fetchMsgType = TDMT_STREAM_FETCH_FROM_RUNNER;
14,979✔
1844
            pSource->localExec = false;
14,979✔
1845

1846
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
14,979✔
1847
          }
1848
        } else {
1849
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
561,747✔
1850
        }
1851
      }
1852

1853
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
549,870✔
1854
    }
1855

1856
    if (totalTaskNum != pDag->numOfSubplans) {
532,272✔
1857
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1858
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1859
    }
1860

1861
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
532,272✔
1862

1863
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
532,272✔
1864

1865
    nodesDestroyNode((SNode *)(*pRoot));
532,272✔
1866
    *pRoot = NULL;
532,272✔
1867
    
1868
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
532,272✔
1869
    if (subQ) {
532,272✔
1870
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
14,979✔
1871
    } else {
1872
      pDag = *pRoot;
517,293✔
1873
    }
1874

1875
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
532,272✔
1876
  }
1877

1878
  if (!subQ) {
177,424✔
1879
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
689,724✔
1880
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
517,293✔
1881
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
517,293✔
1882
    }
1883
  }
1884
  
1885
  pInfo->runnerNum += totalTaskNum;
177,424✔
1886
  
1887
_exit:
177,424✔
1888

1889
  if (code) {
177,424✔
1890
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1891
  }
1892

1893
  taosArrayDestroy(deployTaskList);
177,424✔
1894

1895
  return code;
177,424✔
1896
}
1897

UNCOV
1898
int32_t msmReBuildRunnerTasks(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream, SStmTaskAction* pAction) {
×
UNCOV
1899
  int32_t code = 0;
×
UNCOV
1900
  int32_t lino = 0;
×
UNCOV
1901
  int64_t streamId = pStream->pCreate->streamId;
×
UNCOV
1902
  int32_t newNodeId = 0;
×
UNCOV
1903
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
×
UNCOV
1904
  int32_t        lowestLevelIdx = levelNum - 1;
×
UNCOV
1905
  SNodeListNode *plans = NULL;
×
UNCOV
1906
  int32_t        taskNum = 0;
×
UNCOV
1907
  int32_t        totalTaskNum = 0;
×
UNCOV
1908
  int32_t        deployId = 0;
×
UNCOV
1909
  SStmTaskStatus* pRunner = NULL;
×
UNCOV
1910
  SStmTaskStatus* pStartRunner = NULL;
×
UNCOV
1911
  int32_t taskIdx = 0;
×
UNCOV
1912
  SArray* deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
×
UNCOV
1913
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
×
1914

UNCOV
1915
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
×
UNCOV
1916
    deployId = pAction->deployId[r];
×
1917

UNCOV
1918
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
×
1919

UNCOV
1920
    pStartRunner = pRunner;
×
UNCOV
1921
    totalTaskNum = 0;
×
1922

UNCOV
1923
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
×
UNCOV
1924
    if (!GOT_SNODE(newNodeId)) {
×
1925
      TAOS_CHECK_EXIT(terrno);
×
1926
    }
1927

UNCOV
1928
    taskIdx = 0;
×
1929
    
UNCOV
1930
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
×
UNCOV
1931
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
×
UNCOV
1932
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
×
UNCOV
1933
      totalTaskNum += taskNum;
×
1934

UNCOV
1935
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
×
1936
      
UNCOV
1937
      for (int32_t n = 0; n < taskNum; ++n) {
×
UNCOV
1938
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
×
1939

UNCOV
1940
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
×
UNCOV
1941
        if (pRunner->id.taskIdx != newTaskIdx) {
×
1942
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1943
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1944
        }
1945

UNCOV
1946
        pRunner->id.nodeId = newNodeId;
×
1947

UNCOV
1948
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
×
UNCOV
1949
        pDeploy->task.type = pRunner->type;
×
UNCOV
1950
        pDeploy->task.streamId = streamId;
×
UNCOV
1951
        pDeploy->task.taskId = pRunner->id.taskId;
×
UNCOV
1952
        pDeploy->task.flags = pRunner->flags;
×
UNCOV
1953
        pDeploy->task.seriousId = pRunner->id.seriousId;
×
UNCOV
1954
        pDeploy->task.nodeId = pRunner->id.nodeId;
×
UNCOV
1955
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
×
UNCOV
1956
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
×
1957

UNCOV
1958
        pRunner++;
×
1959
      }
1960

UNCOV
1961
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
×
1962
    }
1963

UNCOV
1964
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
×
1965

UNCOV
1966
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
×
1967

UNCOV
1968
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
×
1969

UNCOV
1970
    nodesDestroyNode((SNode *)pDag);
×
UNCOV
1971
    pDag = NULL;
×
1972

UNCOV
1973
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
×
1974
  }
1975

UNCOV
1976
_exit:
×
1977

UNCOV
1978
  if (code) {
×
1979
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1980
  }
1981

UNCOV
1982
  nodesDestroyNode((SNode *)pDag);
×
UNCOV
1983
  taosArrayDestroy(deployTaskList);
×
1984

UNCOV
1985
  return code;
×
1986
}
1987

1988

1989
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
166,464✔
1990
  int32_t code = TSDB_CODE_SUCCESS;
166,464✔
1991
  int32_t lino = 0;
166,464✔
1992
  //STREAMTODO 
1993
  
1994
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
166,464✔
1995
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
166,464✔
1996

1997
_exit:
166,464✔
1998

1999
  if (code) {
166,464✔
2000
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2001
  }
2002

2003
  return code;
166,464✔
2004
}
2005

2006

2007
static int32_t msmUpdateCalcReaderTasks(SStreamObj* pStream, SNodeList* pSubEP) {
172,431✔
2008
  int32_t   code = TSDB_CODE_SUCCESS;
172,431✔
2009
  int32_t   lino = 0;
172,431✔
2010
  int64_t   streamId = pStream->pCreate->streamId;
172,431✔
2011
  void*     pIter = NULL;
172,431✔
2012
  SSubplan* pSubplan = NULL;
172,431✔
2013

2014
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
648,518✔
2015
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
476,087✔
2016
    (void)mstWaitLock(&pVg->lock, true);
476,087✔
2017

2018
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
476,087✔
2019
    if (atomic_load_32(&pVg->deployed) == taskNum) {
476,087✔
2020
      taosRUnLockLatch(&pVg->lock);
×
2021
      continue;
×
2022
    }
2023

2024
    for (int32_t i = 0; i < taskNum; ++i) {
3,297,240✔
2025
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
2,821,153✔
2026
      if (pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
2,821,153✔
2027
        continue;
2,355,182✔
2028
      }
2029

2030
      if (!pExt->deploy.msg.reader.triggerReader) {
465,971✔
2031
        SStreamReaderDeployFromCalc* pCalcReaderDeploy = &pExt->deploy.msg.reader.msg.calc;
253,542✔
2032
        TAOS_CHECK_EXIT(nodesStringToNode(pCalcReaderDeploy->calcScanPlan, (SNode**)&pSubplan));
253,542✔
2033
        TAOS_CHECK_EXIT(nodesCloneList(pSubEP, &pSubplan->pSubQ));
253,542✔
2034
        TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pSubplan, false, (char**)&pCalcReaderDeploy->calcScanPlan, NULL));
253,542✔
2035
        pCalcReaderDeploy->freeScanPlan = true;
253,542✔
2036
        nodesDestroyNode((SNode *)pSubplan);
253,542✔
2037
      }
2038
    }
2039

2040
    taosRUnLockLatch(&pVg->lock);
476,087✔
2041
  }
2042

2043
_exit:
172,431✔
2044
  if (code) {
172,431✔
2045
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2046
  }
2047

2048
  return code;
172,431✔
2049
}
2050

2051
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
175,309✔
2052
  if (NULL == pStream->pCreate->calcPlan) {
175,309✔
2053
    return TSDB_CODE_SUCCESS;
2,878✔
2054
  }
2055

2056
  int32_t code = TSDB_CODE_SUCCESS;
172,431✔
2057
  int32_t lino = 0;
172,431✔
2058
  int64_t streamId = pStream->pCreate->streamId;
172,431✔
2059
  SQueryPlan* pPlan = NULL;
172,431✔
2060
  SNodeList*  pSubEP = NULL;
172,431✔
2061
  SNode*      pNode = NULL;
172,431✔
2062
  int32_t     subQueryPlans = 0;
172,431✔
2063

2064
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
172,431✔
2065

2066
  FOREACH(pNode, pPlan->pChildren) {
177,424✔
2067
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
4,993✔
2068

2069
    subQueryPlans += calcSubQPlan->numOfSubplans;
4,993✔
2070
  }
2071

2072
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
689,724✔
2073
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans + subQueryPlans, sizeof(SStmTaskStatus));
517,293✔
2074
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
517,293✔
2075
  }
2076

2077
  for (int32_t i = 0; i < LIST_LENGTH(pPlan->pChildren); ++i) {
177,424✔
2078
    code = msmBuildRunnerTasksImpl(pCtx, i, pInfo, pStream, &pPlan, &pSubEP);
4,993✔
2079
    TAOS_CHECK_EXIT(code);
4,993✔
2080
  }
2081

2082
  code = msmBuildRunnerTasksImpl(pCtx, -1, pInfo, pStream, &pPlan, &pSubEP);
172,431✔
2083
  TAOS_CHECK_EXIT(code);
172,431✔
2084

2085
  taosHashClear(mStreamMgmt.toUpdateScanMap);
172,431✔
2086
  mStreamMgmt.toUpdateScanNum = 0;
172,431✔
2087

2088
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
172,431✔
2089

2090
_exit:
172,431✔
2091
  nodesDestroyNode((SNode *)pPlan);
172,431✔
2092
  if (pSubEP) {
172,431✔
2093
    nodesDestroyList(pSubEP);
4,258✔
2094
  }
2095

2096
  if (code) {
172,431✔
2097
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2098
  }
2099

2100
  return code;
172,431✔
2101
}
2102

2103
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
175,309✔
2104
  int32_t code = TSDB_CODE_SUCCESS;
175,309✔
2105
  int32_t lino = 0;
175,309✔
2106
  int64_t streamId = pStream->pCreate->streamId;
175,309✔
2107

2108
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
175,309✔
2109

2110
  pCtx->triggerTaskId = msmAssignTaskId();
175,309✔
2111
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
175,309✔
2112
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
175,309✔
2113
    TAOS_CHECK_EXIT(terrno);
×
2114
  }
2115

2116
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
175,309✔
2117
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
175,309✔
2118
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
175,309✔
2119
  
2120
_exit:
175,309✔
2121

2122
  if (code) {
175,309✔
2123
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2124
  }
2125

2126
  return code;
175,309✔
2127
}
2128

2129
static int32_t msmInitTrigReaderList(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
×
2130
  int32_t code = TSDB_CODE_SUCCESS;
×
2131
  int32_t lino = 0;
×
2132
  int64_t streamId = pStream->pCreate->streamId;
×
2133
  SSdb   *pSdb = pCtx->pMnode->pSdb;
×
2134
  SStmTaskStatus* pState = NULL;
×
2135
  SDbObj* pDb = NULL;
×
2136
  
2137
  switch (pStream->pCreate->triggerTblType) {
×
2138
    case TSDB_NORMAL_TABLE:
×
2139
    case TSDB_CHILD_TABLE:
2140
    case TSDB_VIRTUAL_CHILD_TABLE:
2141
    case TSDB_VIRTUAL_NORMAL_TABLE: {
2142
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
×
2143
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2144
      pInfo->trigReaderNum = 1;
×
2145
      break;
×
2146
    }
2147
    case TSDB_SUPER_TABLE: {
×
2148
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
×
2149
      if (NULL == pDb) {
×
2150
        code = terrno;
×
2151
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
2152
        goto _exit;
×
2153
      }
2154

2155
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
2156
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2157
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2158
      mndReleaseDb(pCtx->pMnode, pDb);
×
2159
      pDb = NULL;
×
2160
      break;
×
2161
    }
2162
    default:
×
2163
      pInfo->trigReaderNum = 0;
×
2164
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2165
      break;
×
2166
  }
2167

2168
_exit:
×
2169

2170
  if (code) {
×
2171
    mndReleaseDb(pCtx->pMnode, pDb);
×
2172
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2173
  }
2174

2175
  return code;
×
2176
}
2177

2178

2179
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
169,342✔
2180
  int32_t code = TSDB_CODE_SUCCESS;
169,342✔
2181
  int32_t lino = 0;
169,342✔
2182
  int64_t streamId = pStream->pCreate->streamId;
169,342✔
2183

2184
  pStatus->lastActionTs = INT64_MIN;
169,342✔
2185

2186
  if (NULL == pStatus->streamName) {
169,342✔
2187
    pStatus->streamName = taosStrdup(pStream->name);
169,342✔
2188
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
169,342✔
2189
  }
2190

2191
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
169,342✔
2192
  
2193
  if (pStream->pCreate->numOfCalcSubplan > 0) {
169,342✔
2194
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
166,464✔
2195
    
2196
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
166,464✔
2197
  }
2198

2199
  if (initList) {
169,342✔
2200
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2201

2202
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
2203
    if (subPlanNum > 0) {
×
2204
      pStatus->calcReaderNum = subPlanNum;
×
2205
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2206
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2207
    }
2208

2209
    if (pStatus->runnerNum > 0) {
×
2210
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2211
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2212
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2213
      }
2214
    }
2215
  }
2216
  
2217
_exit:
169,342✔
2218

2219
  if (code) {
169,342✔
2220
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2221
  }
2222

2223
  return code;
169,342✔
2224
}
2225

2226
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
175,309✔
2227
  int32_t code = TSDB_CODE_SUCCESS;
175,309✔
2228
  int32_t lino = 0;
175,309✔
2229
  int64_t streamId = pStream->pCreate->streamId;
175,309✔
2230
  SStmStatus info = {0};
175,309✔
2231

2232
  if (NULL == pStatus) {
175,309✔
2233
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
169,342✔
2234

2235
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
169,342✔
2236

2237
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
169,342✔
2238
  }
2239
  
2240
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
175,309✔
2241

2242
  mstLogSStmStatus("stream deployed", streamId, pStatus);
175,309✔
2243

2244
_exit:
175,309✔
2245

2246
  if (code) {
175,309✔
2247
    if (NULL != pStatus) {
×
2248
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2249
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2250
    }
2251
    
2252
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2253
  }
2254

2255
  return code;
175,309✔
2256
}
2257

2258

2259
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
6,951✔
2260
  int32_t code = TSDB_CODE_SUCCESS;
6,951✔
2261
  void* pIter = NULL;
6,951✔
2262

2263
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
10,913✔
2264
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
3,962✔
2265
    (void)mstWaitLock(&pVg->lock, true);
3,962✔
2266

2267
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
3,962✔
2268
    if (atomic_load_32(&pVg->deployed) == taskNum) {
3,962✔
2269
      taosRUnLockLatch(&pVg->lock);
×
2270
      continue;
×
2271
    }
2272

2273
    for (int32_t i = 0; i < taskNum; ++i) {
18,534✔
2274
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
14,572✔
2275
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
14,572✔
2276
        continue;
14,572✔
2277
      }
2278

2279
      mstDestroySStmTaskToDeployExt(pExt);
×
2280
      pExt->deployed = true;
×
2281
    }
2282
    
2283
    taosRUnLockLatch(&pVg->lock);
3,962✔
2284
  }
2285

2286
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
12,165✔
2287
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
5,214✔
2288
    (void)mstWaitLock(&pSnode->lock, true);
5,214✔
2289

2290
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
5,214✔
2291
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
5,214✔
2292
      for (int32_t i = 0; i < taskNum; ++i) {
11,612✔
2293
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
7,502✔
2294
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
7,502✔
2295
          continue;
7,502✔
2296
        }
2297
        
2298
        mstDestroySStmTaskToDeployExt(pExt);
×
2299
        pExt->deployed = true;
×
2300
      }
2301
    }
2302

2303
    taskNum = taosArrayGetSize(pSnode->runnerList);
5,214✔
2304
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
5,214✔
2305
      for (int32_t i = 0; i < taskNum; ++i) {
27,720✔
2306
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
22,506✔
2307
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
22,506✔
2308
          continue;
22,506✔
2309
        }
2310
        
2311
        mstDestroySStmTaskToDeployExt(pExt);
×
2312
        pExt->deployed = true;
×
2313
      }
2314
    }
2315

2316
    taosRUnLockLatch(&pSnode->lock);
5,214✔
2317
  }
2318

2319
  
2320
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
19,974✔
2321
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
13,023✔
2322
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
13,023✔
2323
    if (TSDB_CODE_SUCCESS == code) {
13,023✔
2324
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
8,239✔
2325
    }
2326
  }
2327

2328
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
31,262✔
2329
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
24,311✔
2330
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
24,311✔
2331
    if (TSDB_CODE_SUCCESS == code) {
24,311✔
2332
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
9,305✔
2333
    }
2334
  }
2335

2336
  size_t keyLen = 0;
6,951✔
2337
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
499,881✔
2338
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
492,930✔
2339
    if (*pStreamId == streamId) {
492,930✔
2340
      int64_t taskId = *(pStreamId + 1);
39,830✔
2341
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
39,830✔
2342
      if (code) {
39,830✔
2343
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2344
      } else {
2345
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
39,830✔
2346
      }
2347
    }
2348
  }
2349

2350
  if (fromStreamMap) {
6,951✔
2351
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
432✔
2352
    if (code) {
432✔
2353
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2354
    } else {
2355
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
432✔
2356
    }
2357
  }
2358
  
2359
  return code;
6,951✔
2360
}
2361

2362
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
6,519✔
2363
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
6,519✔
2364
  
2365
  (void)msmSTRemoveStream(streamId, false);  
6,519✔
2366

2367
  mstResetSStmStatus(pStatus);
6,519✔
2368

2369
  pStatus->deployTimes++;
6,519✔
2370
}
6,519✔
2371

2372
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
176,893✔
2373
  int32_t code = TSDB_CODE_SUCCESS;
176,893✔
2374
  int32_t lino = 0;
176,893✔
2375
  int64_t streamId = pAction->streamId;
176,893✔
2376
  char* streamName = pAction->streamName;
176,893✔
2377
  SStreamObj* pStream = NULL;
176,893✔
2378
  int8_t stopped = 0;
176,893✔
2379

2380
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
176,893✔
2381
  if (pStatus) {
176,893✔
2382
    stopped = atomic_load_8(&pStatus->stopped);
6,519✔
2383
    if (0 == stopped) {
6,519✔
2384
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
552✔
2385
      msmResetStreamForRedeploy(streamId, pStatus);
552✔
2386
    } else {
2387
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
5,967✔
2388
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2389
        return code;
×
2390
      }
2391
      
2392
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
5,967✔
2393
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
5,967✔
2394
        msmResetStreamForRedeploy(streamId, pStatus);
5,967✔
2395
      }
2396
    }
2397
  }
2398

2399
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
176,893✔
2400
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
176,893✔
2401
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
1,032✔
2402
    return TSDB_CODE_SUCCESS;
1,032✔
2403
  }
2404

2405
  TAOS_CHECK_EXIT(code);
175,861✔
2406

2407
  if (pStatus && pStream->pCreate->streamId != streamId) {
175,861✔
2408
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2409
    atomic_store_8(&pStatus->stopped, 2);
×
2410
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2411
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2412
  }
2413

2414
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
175,861✔
2415
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
175,861✔
2416
  if (userStopped || userDropped) {
175,861✔
2417
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
552✔
2418
    goto _exit;
552✔
2419
  }
2420
  
2421
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
175,309✔
2422

2423
_exit:
175,861✔
2424

2425
  mndReleaseStream(pCtx->pMnode, pStream);
175,861✔
2426

2427
  if (code) {
175,861✔
2428
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2429
  }
2430

2431
  return code;
175,861✔
2432
}
2433

2434
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
1,104✔
2435
  int32_t code = TSDB_CODE_SUCCESS;
1,104✔
2436
  int32_t lino = 0;
1,104✔
2437
  int64_t streamId = pAction->streamId;
1,104✔
2438
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
1,104✔
2439
  if (NULL == ppTask) {
1,104✔
2440
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
×
2441
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2442
  }
2443
  
2444
  SStmTaskDeploy info = {0};
1,104✔
2445
  info.task.type = pAction->type;
1,104✔
2446
  info.task.streamId = pAction->streamId;
1,104✔
2447
  info.task.taskId = pAction->id.taskId;
1,104✔
2448
  info.task.seriousId = (*ppTask)->id.seriousId;
1,104✔
2449
  info.task.nodeId = pAction->id.nodeId;
1,104✔
2450
  info.task.taskIdx = pAction->id.taskIdx;
1,104✔
2451
  
2452
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
1,104✔
2453
  SStreamCalcScan* scanPlan = NULL;
1,104✔
2454
  if (!isTriggerReader) {
1,104✔
2455
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
552✔
2456
    if (NULL == scanPlan) {
552✔
2457
      mstsError("fail to get TASK:%" PRId64 " scanPlan, taskIdx:%d, scanPlanNum:%zu", 
×
2458
          pAction->id.taskId, pAction->id.taskIdx, taosArrayGetSize(pStatus->pCreate->calcScanPlanList));
2459
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2460
    }
2461
  }
2462
  
2463
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, scanPlan ? scanPlan->scanPlan : NULL, pStatus, isTriggerReader));
1,104✔
2464
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
1,104✔
2465

2466
_exit:
1,104✔
2467

2468
  if (code) {
1,104✔
2469
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2470
  }
2471

2472
  return code;
1,104✔
2473
}
2474

2475
/*
2476
static int32_t msmReLaunchTriggerTask(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2477
  int32_t code = TSDB_CODE_SUCCESS;
2478
  int32_t lino = 0;
2479
  int64_t streamId = pAction->streamId;
2480
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2481
  if (NULL == ppTask) {
2482
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
2483
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
2484
  }
2485
  
2486
  (*ppTask)->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2487
  if (!GOT_SNODE((*ppTask)->id.nodeId)) {
2488
    mstsError("no avaible snode for deploying trigger task, seriousId: %" PRId64, (*ppTask)->id.seriousId);
2489
    return TSDB_CODE_SUCCESS;
2490
  }
2491
  
2492
  SStmTaskDeploy info = {0};
2493
  info.task.type = pAction->type;
2494
  info.task.streamId = streamId;
2495
  info.task.taskId = pAction->id.taskId;
2496
  info.task.seriousId = (*ppTask)->id.seriousId;
2497
  info.task.nodeId = (*ppTask)->id.nodeId;
2498
  info.task.taskIdx = pAction->id.taskIdx;
2499
  
2500
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2501
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2502
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, *ppTask, 1, -1));
2503
  
2504
  atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2505

2506
_exit:
2507

2508
  if (code) {
2509
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2510
  }
2511

2512
  return code;
2513
}
2514
*/
2515

UNCOV
2516
static int32_t msmReLaunchRunnerDeploy(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
×
UNCOV
2517
  int32_t code = TSDB_CODE_SUCCESS;
×
UNCOV
2518
  int32_t lino = 0;
×
UNCOV
2519
  int64_t streamId = pAction->streamId;
×
2520
  
2521
/*
2522
  if (pAction->triggerStatus) {
2523
    pCtx->triggerTaskId = pAction->triggerStatus->id.taskId;
2524
    pAction->triggerStatus->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2525
    if (!GOT_SNODE(pAction->triggerStatus->id.nodeId)) {
2526
      mstsError("no avaible snode for deploying trigger task, seriousId:%" PRId64, pAction->triggerStatus->id.seriousId);
2527
      return TSDB_CODE_SUCCESS;
2528
    }
2529
  
2530
    pCtx->triggerNodeId = pAction->triggerStatus->id.nodeId;
2531
  } else {
2532
*/
UNCOV
2533
  pCtx->triggerTaskId = pStatus->triggerTask->id.taskId;
×
UNCOV
2534
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
×
2535
//  }
2536
  
UNCOV
2537
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
×
2538
  
UNCOV
2539
  SQueryPlan* pPlan = NULL;
×
UNCOV
2540
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
×
2541
  
UNCOV
2542
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
×
2543
  
UNCOV
2544
  taosHashClear(mStreamMgmt.toUpdateScanMap);
×
UNCOV
2545
  mStreamMgmt.toUpdateScanNum = 0;
×
2546
  
2547
/*
2548
  if (pAction->triggerStatus) {
2549
    SStmTaskDeploy info = {0};
2550
    info.task.type = STREAM_TRIGGER_TASK;
2551
    info.task.streamId = streamId;
2552
    info.task.taskId = pCtx->triggerTaskId;
2553
    info.task.seriousId = pAction->triggerStatus->id.seriousId;
2554
    info.task.nodeId = pCtx->triggerNodeId;
2555
    info.task.taskIdx = 0;
2556
  
2557
    TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2558
    TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2559
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pAction->triggerStatus, 1, -1));
2560
    
2561
    atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2562
  }
2563
*/
2564

UNCOV
2565
_exit:
×
2566

UNCOV
2567
  if (code) {
×
2568
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2569
  }
2570

UNCOV
2571
  return code;
×
2572
}
2573

2574

2575
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
1,104✔
2576
  int32_t code = TSDB_CODE_SUCCESS;
1,104✔
2577
  int32_t lino = 0;
1,104✔
2578
  int64_t streamId = pAction->streamId;
1,104✔
2579
  int64_t taskId = pAction->id.taskId;
1,104✔
2580
  SStreamObj* pStream = NULL;
1,104✔
2581

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

2584
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,104✔
2585
  if (NULL == pStatus) {
1,104✔
2586
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2587
    return TSDB_CODE_SUCCESS;
×
2588
  }
2589

2590
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,104✔
2591
  if (stopped) {
1,104✔
2592
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
2593
    return TSDB_CODE_SUCCESS;
×
2594
  }
2595

2596
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,104✔
2597
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,104✔
2598
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2599
    return TSDB_CODE_SUCCESS;
×
2600
  }
2601

2602
  TAOS_CHECK_EXIT(code);
1,104✔
2603

2604
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,104✔
2605
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,104✔
2606
  if (userStopped || userDropped) {
1,104✔
2607
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2608
    goto _exit;
×
2609
  }
2610

2611
  switch (pAction->type) {
1,104✔
2612
    case STREAM_READER_TASK:
1,104✔
2613
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,104✔
2614
      break;
1,104✔
2615
/*
2616
    case STREAM_TRIGGER_TASK:
2617
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2618
      break;
2619
*/
UNCOV
2620
    case STREAM_RUNNER_TASK:
×
UNCOV
2621
      if (pAction->multiRunner) {
×
UNCOV
2622
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
×
2623
      } else {
2624
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
2625
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2626
      }
UNCOV
2627
      break;
×
2628
    default:
×
2629
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2630
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2631
      break;
×
2632
  }
2633

2634
_exit:
1,104✔
2635

2636
  if (pStream) {
1,104✔
2637
    mndReleaseStream(pCtx->pMnode, pStream);
1,104✔
2638
  }
2639

2640
  if (code) {
1,104✔
2641
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2642
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2643
  }
2644

2645
  return code;
1,104✔
2646
}
2647

2648
static int32_t msmTDRemoveStream(int64_t streamId) {
×
2649
  void* pIter = NULL;
×
2650
  
2651
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
×
2652
    while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
×
2653
      SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
×
2654
      int32_t taskNum = taosArrayGetSize(pVg->taskList);
×
2655
      if (atomic_load_32(&pVg->deployed) == taskNum) {
×
2656
        continue;
×
2657
      }
2658
      
2659
      for (int32_t i = 0; i < taskNum; ++i) {
×
2660
        SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
×
2661
        if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2662
          pExt->deployed = true;
×
2663
        }
2664
      }
2665
    }
2666
  }
2667

2668
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
2669
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2670
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2671
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2672
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2673
        for (int32_t i = 0; i < taskNum; ++i) {
×
2674
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2675
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2676
            pExt->deployed = true;
×
2677
          }
2678
        }
2679
      }
2680

2681
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
2682
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2683
        for (int32_t i = 0; i < taskNum; ++i) {
×
2684
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2685
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2686
            pExt->deployed = true;
×
2687
          }
2688
        }
2689
      }
2690
    }
2691
  }
2692

2693
  return TSDB_CODE_SUCCESS;
×
2694
}
2695

2696
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
432✔
2697
  int32_t code = TSDB_CODE_SUCCESS;
432✔
2698
  int32_t lino = 0;
432✔
2699

2700
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
432✔
2701

2702
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
432✔
2703

2704
_exit:
432✔
2705

2706
  if (code) {
432✔
2707
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2708
  } else {
2709
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
432✔
2710
  }
2711

2712
  return code;
432✔
2713
}
2714

2715
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
32,825✔
2716
  int32_t code = TSDB_CODE_SUCCESS;
32,825✔
2717
  int32_t lino = 0;
32,825✔
2718

2719
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
32,825✔
2720
  if (0 == active || MND_STM_STATE_NORMAL != state) {
32,825✔
2721
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
331✔
2722
    return;
331✔
2723
  }
2724

2725
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
32,494✔
2726
  if (NULL == pStream) {
32,494✔
2727
    mstsInfo("stream %s already not in streamMap", streamName);
1,341✔
2728
    goto _exit;
1,341✔
2729
  }
2730

2731
  atomic_store_8(&pStream->stopped, 2);
31,153✔
2732

2733
  mstsInfo("set stream %s stopped by user", streamName);
31,153✔
2734

2735
_exit:
×
2736

2737
  taosHashRelease(mStreamMgmt.streamMap, pStream);
32,494✔
2738

2739
  if (code) {
32,494✔
2740
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2741
  }
2742

2743
  return;
32,494✔
2744
}
2745

2746
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,420✔
2747
  int32_t code = TSDB_CODE_SUCCESS;
3,420✔
2748
  int32_t lino = 0;
3,420✔
2749

2750
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,420✔
2751
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,420✔
2752
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2753
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2754
  }
2755

2756
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,420✔
2757
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,420✔
2758
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2759
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2760
    goto _exit;
×
2761
  }
2762

2763
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,420✔
2764

2765
_exit:
3,420✔
2766

2767
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,420✔
2768

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

2773
  return code;
3,420✔
2774
}
2775

2776
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
44,240✔
2777
  int32_t code = TSDB_CODE_SUCCESS;
44,240✔
2778
  int32_t lino = 0;
44,240✔
2779
  SStmQNode* pQNode = NULL;
44,240✔
2780

2781
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
222,237✔
2782
    switch (pQNode->type) {
177,997✔
2783
      case STREAM_ACT_DEPLOY:
177,997✔
2784
        if (pQNode->streamAct) {
177,997✔
2785
          mstDebug("start to handle stream deploy action");
176,893✔
2786
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
176,893✔
2787
        } else {
2788
          mstDebug("start to handle task deploy action");
1,104✔
2789
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,104✔
2790
        }
2791
        break;
177,997✔
2792
      default:
×
2793
        break;
×
2794
    }
2795
  }
2796

2797
_exit:
44,240✔
2798

2799
  if (code) {
44,240✔
2800
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2801
  }
2802
}
44,240✔
2803

2804
void msmStopAllStreamsByGrant(int32_t errCode) {
×
2805
  SStmStatus* pStatus = NULL;
×
2806
  void* pIter = NULL;
×
2807
  int64_t streamId = 0;
×
2808
  
2809
  while (true) {
2810
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
2811
    if (NULL == pIter) {
×
2812
      break;
×
2813
    }
2814

2815
    pStatus = (SStmStatus*)pIter;
×
2816

2817
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2818
    atomic_store_8(&pStatus->stopped, 4);
×
2819

2820
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2821
  }
2822
}
×
2823

2824
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2825
  mstInfo("stream grant expired");
×
2826

2827
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2828
    mstWarn("mnode stream is NOT active, ignore handling");
×
2829
    return errCode;
×
2830
  }
2831

2832
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2833

2834
  msmStopAllStreamsByGrant(errCode);
×
2835

2836
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2837
  
2838
  return errCode;
×
2839
}
2840

2841
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,282,746✔
2842
  int32_t code = TSDB_CODE_SUCCESS;
1,282,746✔
2843
  int32_t lino = 0;
1,282,746✔
2844
  int64_t streamId = pDeploy->task.streamId;
1,282,746✔
2845
  
2846
  switch (pDeploy->task.type) {
1,282,746✔
2847
    case STREAM_READER_TASK:
530,711✔
2848
      if (NULL == pStream->readerTasks) {
530,711✔
2849
        pStream->streamId = streamId;
225,321✔
2850
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
225,321✔
2851
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
225,321✔
2852
      }
2853
      
2854
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,061,422✔
2855
      break;
530,711✔
2856
    case STREAM_TRIGGER_TASK:
175,309✔
2857
      pStream->streamId = streamId;
175,309✔
2858
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
175,309✔
2859
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
175,309✔
2860
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
175,309✔
2861
      break;
175,309✔
2862
    case STREAM_RUNNER_TASK:
576,726✔
2863
      if (NULL == pStream->runnerTasks) {
576,726✔
2864
        pStream->streamId = streamId;
177,793✔
2865
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
177,793✔
2866
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
177,793✔
2867
      }      
2868
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,153,452✔
2869
      break;
576,726✔
2870
    default:
×
2871
      break;
×
2872
  }
2873

2874
_exit:
1,282,746✔
2875

2876
  if (code) {
1,282,746✔
2877
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2878
  }
2879

2880
  return code;
1,282,746✔
2881
}
2882

2883
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,282,746✔
2884
  int32_t code = TSDB_CODE_SUCCESS;
1,282,746✔
2885
  int32_t lino = 0;
1,282,746✔
2886
  int64_t streamId = pDeploy->task.streamId;
1,282,746✔
2887
  SStreamTask* pTask = &pDeploy->task;
1,282,746✔
2888
  SStmStreamDeploy streamDeploy = {0};
1,282,746✔
2889
  SStmStreamDeploy* pStream = NULL;
1,282,746✔
2890
   
2891
  while (true) {
2892
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,282,746✔
2893
    if (NULL == pStream) {
1,282,746✔
2894
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
234,579✔
2895
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
234,579✔
2896
      if (TSDB_CODE_SUCCESS == code) {
234,579✔
2897
        goto _exit;
234,579✔
2898
      }
2899

2900
      if (TSDB_CODE_DUP_KEY != code) {
×
2901
        goto _exit;
×
2902
      }    
2903

2904
      tFreeSStmStreamDeploy(&streamDeploy);
×
2905
      continue;
×
2906
    }
2907

2908
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,048,167✔
2909
    
2910
    break;
1,048,167✔
2911
  }
2912
  
2913
_exit:
1,282,746✔
2914

2915
  taosHashRelease(pHash, pStream);
1,282,746✔
2916

2917
  if (code) {
1,282,746✔
2918
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2919
  } else {
2920
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,282,746✔
2921
  }
2922

2923
  return code;
1,282,746✔
2924
}
2925

2926

2927
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
221,187✔
2928
  int32_t code = TSDB_CODE_SUCCESS;
221,187✔
2929
  int32_t lino = 0;
221,187✔
2930
  int32_t taskNum = taosArrayGetSize(pTasks);
221,187✔
2931

2932
  for (int32_t i = 0; i < taskNum; ++i) {
1,509,229✔
2933
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,288,042✔
2934
    if (pExt->deployed) {
1,288,042✔
2935
      continue;
5,296✔
2936
    }
2937

2938
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,282,746✔
2939
    pExt->deployed = true;
1,282,746✔
2940

2941
    (void)atomic_add_fetch_32(deployed, 1);
1,282,746✔
2942
  }
2943

2944
_exit:
221,187✔
2945

2946
  if (code) {
221,187✔
2947
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2948
  }
2949

2950
  return code;
221,187✔
2951
}
2952

2953
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
60,613✔
2954
  int32_t code = TSDB_CODE_SUCCESS;
60,613✔
2955
  int32_t lino = 0;
60,613✔
2956
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
60,613✔
2957
  SStmVgTasksToDeploy* pVg = NULL;
60,613✔
2958
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2959

2960
  mstDebug("start to add stream vgroup tasks deploy");
60,613✔
2961
  
2962
  for (int32_t i = 0; i < vgNum; ++i) {
389,001✔
2963
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
328,388✔
2964

2965
    msmUpdateVgroupUpTs(pCtx, *vgId);
328,388✔
2966

2967
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
328,388✔
2968
    if (NULL == pVg) {
328,388✔
2969
      continue;
200,673✔
2970
    }
2971

2972
    if (taosRTryLockLatch(&pVg->lock)) {
127,715✔
2973
      continue;
×
2974
    }
2975
    
2976
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
127,715✔
UNCOV
2977
      taosRUnLockLatch(&pVg->lock);
×
UNCOV
2978
      continue;
×
2979
    }
2980
    
2981
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
127,715✔
2982
    taosRUnLockLatch(&pVg->lock);
127,715✔
2983
  }
2984

2985
_exit:
60,613✔
2986

2987
  if (code) {
60,613✔
2988
    if (pVg) {
×
2989
      taosRUnLockLatch(&pVg->lock);
×
2990
    }
2991

2992
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2993
  }
2994

2995
  return code;
60,613✔
2996
}
2997

2998
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
50,180✔
2999
  int32_t code = TSDB_CODE_SUCCESS;
50,180✔
3000
  int32_t lino = 0;
50,180✔
3001
  SStmSnodeTasksDeploy* pSnode = NULL;
50,180✔
3002
  SStreamHbMsg* pReq = pCtx->pReq;
50,180✔
3003

3004
  mstDebug("start to add stream snode tasks deploy");
50,180✔
3005
  
3006
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
50,180✔
3007
  if (NULL == pSnode) {
50,180✔
3008
    return TSDB_CODE_SUCCESS;
2,910✔
3009
  }
3010

3011
  (void)mstWaitLock(&pSnode->lock, false);
47,270✔
3012
  
3013
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
47,270✔
3014
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
46,202✔
3015
  }
3016

3017
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
47,270✔
3018
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
47,270✔
3019
  }
3020
  
3021
  taosWUnLockLatch(&pSnode->lock);
47,270✔
3022

3023
_exit:
47,270✔
3024

3025
  if (code) {
47,270✔
3026
    if (pSnode) {
×
3027
      taosWUnLockLatch(&pSnode->lock);
×
3028
    }
3029

3030
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3031
  }
3032

3033
  return code;
47,270✔
3034
}
3035

3036
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
668,707✔
3037
  int32_t code = TSDB_CODE_SUCCESS;
668,707✔
3038
  int32_t lino = 0;
668,707✔
3039
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
668,707✔
3040
  if (NULL == pStatus) {
668,707✔
3041
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3042
    return TSDB_CODE_SUCCESS;
×
3043
  }
3044
  
3045
  pStatus->lastActionTs = currTs;
668,707✔
3046

3047
_exit:
668,707✔
3048

3049
  if (code) {
668,707✔
3050
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3051
  }
3052

3053
  return code;
668,707✔
3054
}
3055

3056
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
61,266✔
3057
  int32_t code = TSDB_CODE_SUCCESS;
61,266✔
3058
  int32_t lino = 0;
61,266✔
3059
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
61,266✔
3060
  void* pIter = NULL;
61,266✔
3061

3062
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
61,266✔
3063
  
3064
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
61,266✔
3065
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
61,266✔
3066

3067
  while (1) {
234,579✔
3068
    pIter = taosHashIterate(pCtx->deployStm, pIter);
295,845✔
3069
    if (pIter == NULL) {
295,845✔
3070
      break;
61,266✔
3071
    }
3072
    
3073
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
234,579✔
3074
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
469,158✔
3075

3076
    int64_t streamId = pDeploy->streamId;
234,579✔
3077
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
234,579✔
3078
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
3079

3080
    mstClearSStmStreamDeploy(pDeploy);
234,579✔
3081
    
3082
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
234,579✔
3083
  }
3084
  
3085
_exit:
61,266✔
3086

3087
  if (pIter) {
61,266✔
3088
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3089
  }
3090

3091
  if (code) {
61,266✔
3092
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3093
  }
3094
  
3095
  return code;
61,266✔
3096
}
3097

3098
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
60,613✔
3099
  int32_t code = TSDB_CODE_SUCCESS;
60,613✔
3100
  int32_t lino = 0;
60,613✔
3101
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
60,613✔
3102
  SStmVgTasksToDeploy* pVg = NULL;
60,613✔
3103
  
3104
  for (int32_t i = 0; i < vgNum; ++i) {
389,001✔
3105
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
328,388✔
3106
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
328,388✔
3107
    if (NULL == pVg) {
328,388✔
3108
      continue;
200,673✔
3109
    }
3110

3111
    if (taosWTryLockLatch(&pVg->lock)) {
127,715✔
3112
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
662✔
3113
      continue;
662✔
3114
    }
3115
    
3116
    if (atomic_load_32(&pVg->deployed) <= 0) {
127,053✔
3117
      taosWUnLockLatch(&pVg->lock);
×
3118
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3119
      continue;
×
3120
    }
3121

3122
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
127,053✔
3123
    if (atomic_load_32(&pVg->deployed) == taskNum) {
127,053✔
3124
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
125,729✔
3125
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
125,729✔
3126
      pVg->taskList = NULL;
125,729✔
3127
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
125,729✔
3128
      taosWUnLockLatch(&pVg->lock);
125,729✔
3129
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
125,729✔
3130
      continue;
125,729✔
3131
    }
3132

3133
    for (int32_t m = taskNum - 1; m >= 0; --m) {
11,916✔
3134
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
10,592✔
3135
      if (!pExt->deployed) {
10,592✔
3136
        continue;
1,324✔
3137
      }
3138

3139
      mstDestroySStmTaskToDeployExt(pExt);
9,268✔
3140

3141
      taosArrayRemove(pVg->taskList, m);
9,268✔
3142
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
9,268✔
3143
    }
3144
    atomic_store_32(&pVg->deployed, 0);
1,324✔
3145
    taosWUnLockLatch(&pVg->lock);
1,324✔
3146
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
1,324✔
3147
  }
3148

3149
_exit:
60,613✔
3150

3151
  if (code) {
60,613✔
3152
    if (pVg) {
×
3153
      taosWUnLockLatch(&pVg->lock);
×
3154
    }
3155

3156
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3157
  }
3158
}
60,613✔
3159

3160
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
53,834✔
3161
  if (!GOT_SNODE(snodeId)) {
53,834✔
3162
    return;
3,654✔
3163
  }
3164
  
3165
  int32_t code = TSDB_CODE_SUCCESS;
50,180✔
3166
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
50,180✔
3167
  if (NULL == pSnode) {
50,180✔
3168
    return;
2,910✔
3169
  }
3170

3171
  if (taosWTryLockLatch(&pSnode->lock)) {
47,270✔
3172
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3173
    return;
×
3174
  }
3175

3176
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
47,270✔
3177
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
47,270✔
3178
  
3179
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
47,270✔
3180
    taosWUnLockLatch(&pSnode->lock);
×
3181
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3182
    return;
×
3183
  }
3184

3185
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
47,270✔
3186
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
47,270✔
3187
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
47,270✔
3188
    pSnode->triggerList = NULL;
47,270✔
3189
  }
3190

3191
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
47,270✔
3192
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
47,270✔
3193
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
47,270✔
3194
    pSnode->runnerList = NULL;
47,270✔
3195
  }
3196

3197
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
47,270✔
3198
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
47,270✔
3199
    taosWUnLockLatch(&pSnode->lock);
47,270✔
3200
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
47,270✔
3201
    return;
47,270✔
3202
  }
3203

UNCOV
3204
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
×
3205
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
×
3206
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
×
3207
      if (!pExt->deployed) {
×
3208
        continue;
×
3209
      }
3210

3211
      mstDestroySStmTaskToDeployExt(pExt);
×
3212
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
3213
      taosArrayRemove(pSnode->triggerList, m);
×
3214
    }
3215
    
3216
    pSnode->triggerDeployed = 0;
×
3217
  }
3218

UNCOV
3219
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
×
UNCOV
3220
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
×
UNCOV
3221
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
×
UNCOV
3222
      if (!pExt->deployed) {
×
UNCOV
3223
        continue;
×
3224
      }
3225

UNCOV
3226
      mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
3227
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
UNCOV
3228
      taosArrayRemove(pSnode->runnerList, m);
×
3229
    }
3230
    
UNCOV
3231
    pSnode->runnerDeployed = 0;
×
3232
  }
3233
  
UNCOV
3234
  taosWUnLockLatch(&pSnode->lock);
×
UNCOV
3235
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3236
}
3237

3238
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
15,790,168✔
3239
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
15,790,168✔
3240
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
60,613✔
3241
  }
3242

3243
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
15,790,168✔
3244
    msmCleanDeployedSnodeTasks(pHb->snodeId);
53,834✔
3245
  }
3246
}
15,790,168✔
3247

3248
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
15,790,168✔
3249
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
15,790,168✔
3250
  if (mStreamMgmt.tCtx) {
15,790,168✔
3251
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
15,716,879✔
3252
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
15,716,879✔
3253
  }
3254
}
15,790,168✔
3255

3256
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
228,886✔
3257
  int32_t code = TSDB_CODE_SUCCESS;
228,886✔
3258
  int32_t lino = 0;
228,886✔
3259
  int32_t action = STREAM_ACT_START;
228,886✔
3260
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
228,886✔
3261
  if (pAction) {
228,886✔
3262
    pAction->actions |= action;
×
3263
    pAction->start.triggerId = *pId;
×
3264
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3265
  } else {
3266
    SStmAction newAction = {0};
228,886✔
3267
    newAction.actions = action;
228,886✔
3268
    newAction.start.triggerId = *pId;
228,886✔
3269
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
228,886✔
3270
    mstsDebug("stream add START action, actions:%x", newAction.actions);
228,886✔
3271
  }
3272

3273
_exit:
228,886✔
3274

3275
  if (code) {
228,886✔
3276
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3277
  }
3278

3279
  return code;
228,886✔
3280
}
3281

3282
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
3283
  int32_t code = TSDB_CODE_SUCCESS;
×
3284
  int32_t lino = 0;
×
3285
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3286
  
3287
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
3288
  if (pAction) {
×
3289
    pAction->actions |= action;
×
3290
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3291
  } else {
3292
    SStmAction newAction = {0};
×
3293
    newAction.actions = action;
×
3294
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3295
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3296
  }
3297

3298
_exit:
×
3299

3300
  if (code) {
×
3301
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3302
  }
3303

3304
  return code;
×
3305
}
3306

3307

3308

3309
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
205,242✔
3310
  int32_t code = TSDB_CODE_SUCCESS;
205,242✔
3311
  int32_t lino = 0;
205,242✔
3312
  int32_t action = STREAM_ACT_UNDEPLOY;
205,242✔
3313
  bool    dropped = false;
205,242✔
3314

3315
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
205,242✔
3316
  mstsDebug("stream dropped: %d", dropped);
205,242✔
3317
  
3318
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
205,242✔
3319
  if (pAction) {
205,242✔
3320
    pAction->actions |= action;
161,831✔
3321
    if (NULL == pAction->undeploy.taskList) {
161,831✔
3322
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3323
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3324
    }
3325

3326
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
323,662✔
3327
    if (pAction->undeploy.doCheckpoint) {
161,831✔
3328
      pAction->undeploy.doCheckpoint = dropped ? false : true;
25,330✔
3329
    }
3330
    if (!pAction->undeploy.doCleanup) {
161,831✔
3331
      pAction->undeploy.doCleanup = dropped ? true : false;
25,330✔
3332
    }
3333
    
3334
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
161,831✔
3335
  } else {
3336
    SStmAction newAction = {0};
43,411✔
3337
    newAction.actions = action;
43,411✔
3338
    newAction.undeploy.doCheckpoint = dropped ? false : true;
43,411✔
3339
    newAction.undeploy.doCleanup = dropped ? true : false;
43,411✔
3340
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
43,411✔
3341
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
43,411✔
3342
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
86,822✔
3343
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
43,411✔
3344
    
3345
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
43,411✔
3346
  }
3347

3348
_exit:
205,242✔
3349

3350
  if (code) {
205,242✔
3351
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3352
  }
3353

3354
  return code;
205,242✔
3355
}
3356

3357
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,420✔
3358
  int32_t code = TSDB_CODE_SUCCESS;
3,420✔
3359
  int32_t lino = 0;
3,420✔
3360
  int32_t action = STREAM_ACT_RECALC;
3,420✔
3361
  SStmAction newAction = {0};
3,420✔
3362
  
3363
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,420✔
3364
  if (pAction) {
3,420✔
3365
    pAction->actions |= action;
×
3366
    pAction->recalc.recalcList = recalcList;
×
3367

3368
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3369
  } else {
3370
    newAction.actions = action;
3,420✔
3371
    newAction.recalc.recalcList = recalcList;
3,420✔
3372
    
3373
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,420✔
3374
    
3375
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,420✔
3376
  }
3377

3378
_exit:
3,420✔
3379

3380
  if (code) {
3,420✔
3381
    mstDestroySStmAction(&newAction);
×
3382
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3383
  }
3384

3385
  return code;
3,420✔
3386
}
3387

3388
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
283,947✔
3389
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
283,947✔
3390
  if (NULL == pStream) {
283,947✔
3391
    return false;
×
3392
  }
3393

3394
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
283,947✔
3395
    return false;
×
3396
  }
3397

3398
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
283,947✔
3399
  for (int32_t i = 0; i < readerNum; ++i) {
627,386✔
3400
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
343,439✔
3401
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
343,439✔
3402
      return false;
×
3403
    }
3404
  }
3405

3406
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
283,947✔
3407
  for (int32_t i = 0; i < readerNum; ++i) {
337,011✔
3408
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
53,064✔
3409
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
53,064✔
UNCOV
3410
      return false;
×
3411
    }
3412
  }
3413

3414
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
283,947✔
3415
  SListNode* pNode = listHead(pStream->calcReaders);
283,947✔
3416
  for (int32_t i = 0; i < readerNum; ++i) {
788,056✔
3417
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
504,109✔
3418
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
504,109✔
3419
      return false;
×
3420
    }
3421
    pNode = TD_DLIST_NODE_NEXT(pNode);
504,109✔
3422
  }
3423

3424
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
954,027✔
3425
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
725,141✔
3426
    for (int32_t m = 0; m < runnerNum; ++m) {
1,452,278✔
3427
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
782,198✔
3428
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
782,198✔
3429
        return false;
55,061✔
3430
      }
3431
    }
3432
  }
3433
  
3434
  return true;
228,886✔
3435
}
3436

3437

3438
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
10,908,191✔
3439
  int32_t code = TSDB_CODE_SUCCESS;
10,908,191✔
3440
  int32_t lino = 0;
10,908,191✔
3441
  int32_t action = 0;
10,908,191✔
3442
  int64_t streamId = pMsg->streamId;
10,908,191✔
3443
  SStreamTask* pTask = (SStreamTask*)pMsg;
10,908,191✔
3444
  int8_t  stopped = 0;
10,908,191✔
3445

3446
  msttDebug("start to handle task abnormal status %d", pTask->status);
10,908,191✔
3447
  
3448
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
10,908,191✔
3449
  if (NULL == pStatus) {
10,908,191✔
3450
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3451
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3452
    return;
12,640✔
3453
  }
3454

3455
  stopped = atomic_load_8(&pStatus->stopped);
10,908,191✔
3456
  if (stopped) {
10,908,191✔
3457
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3458
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3459
    return;
×
3460
  }
3461
  
3462
  switch (pMsg->status) {
10,908,191✔
3463
    case STREAM_STATUS_INIT:      
10,902,239✔
3464
      if (STREAM_TRIGGER_TASK != pMsg->type) {
10,902,239✔
3465
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
10,453,258✔
3466
        return;
10,453,258✔
3467
      }
3468
      
3469
      if (INT64_MIN == pStatus->lastActionTs) {
448,981✔
3470
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3471
        return;
×
3472
      }
3473
      
3474
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
448,981✔
3475
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
165,034✔
3476
        return;
165,034✔
3477
      }
3478

3479
      if (STREAM_IS_RUNNING(pStatus)) {
283,947✔
3480
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3481
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
283,947✔
3482
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
228,886✔
3483
      }
3484
      break;
283,947✔
3485
    case STREAM_STATUS_FAILED:
5,952✔
3486
      //STREAMTODO ADD ERRCODE HANDLE
3487
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
5,952✔
3488
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
5,290✔
3489
                 pMsg->taskIdx);
3490
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
5,290✔
3491
      } else {
3492
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
662✔
3493
                 pMsg->taskIdx);
3494
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
662✔
3495
      }
3496
      break;
5,952✔
3497
    default:
×
3498
      break;
×
3499
  }
3500

3501
_exit:
289,899✔
3502

3503
  if (code) {
289,899✔
3504
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3505
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3506
  }
3507
}
3508

3509
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
204,580✔
3510
  int32_t code = TSDB_CODE_SUCCESS;
204,580✔
3511
  int32_t lino = 0;
204,580✔
3512
  SStreamTask* pTask = (SStreamTask*)pStatus;
204,580✔
3513
  int64_t streamId = pStatus->streamId;
204,580✔
3514

3515
  msttInfo("start to handle task error, type: %d", err);
204,580✔
3516

3517
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
204,580✔
3518

3519
_exit:
204,580✔
3520

3521
  if (code) {
204,580✔
3522
    // IGNORE STOP STREAM BY ERROR  
3523
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3524
  }
3525
}
204,580✔
3526

3527
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
3,909,132✔
3528
  int32_t code = TSDB_CODE_SUCCESS;
3,909,132✔
3529
  int32_t lino = 0;
3,909,132✔
3530
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
3,909,132✔
3531

3532
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
3,909,132✔
3533
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3534
  }
3535
  
3536
  SArray* userRecalcList = NULL;
3,909,132✔
3537
  if (atomic_load_ptr(&pStream->userRecalcList)) {
3,909,132✔
3538
    taosWLockLatch(&pStream->userRecalcLock);
3,420✔
3539
    if (pStream->userRecalcList) {
3,420✔
3540
      userRecalcList = pStream->userRecalcList;
3,420✔
3541
      pStream->userRecalcList = NULL;
3,420✔
3542
    }
3543
    taosWUnLockLatch(&pStream->userRecalcLock);
3,420✔
3544
    
3545
    if (userRecalcList) {
3,420✔
3546
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,420✔
3547
    }
3548
  }
3549

3550
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
3,909,132✔
3551
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,964,145✔
3552
    if (NULL == pStatus->detailStatus) {
1,964,145✔
3553
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
173,552✔
3554
      if (NULL == pStatus->detailStatus) {
173,552✔
3555
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3556
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3557
      }
3558
    }
3559
    
3560
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,964,145✔
3561
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,964,145✔
3562
  }
3563

3564
_exit:
1,944,987✔
3565

3566
  if (code) {
3,909,132✔
3567
    // IGNORE STOP STREAM BY ERROR
3568
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3569
  }
3570
}
3,909,132✔
3571

3572
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,043,963✔
3573
  int32_t code = TSDB_CODE_SUCCESS;
1,043,963✔
3574
  int32_t lino = 0;
1,043,963✔
3575
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,043,963✔
3576

3577
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,043,963✔
3578

3579
  for (int32_t i = 0; i < num; ++i) {
31,924,716✔
3580
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
30,880,753✔
3581
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
30,880,753✔
3582
    
3583
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
30,880,753✔
3584
    if (NULL == ppStatus) {
30,880,753✔
3585
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
1,572✔
3586
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
1,572✔
3587
      continue;
1,572✔
3588
    }
3589

3590
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
30,879,181✔
3591
    int8_t stopped = atomic_load_8(&pStream->stopped);
30,879,181✔
3592
    if (stopped) {
30,879,181✔
3593
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
203,008✔
3594
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
203,008✔
3595
      continue;
203,008✔
3596
    }
3597

3598
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
30,676,173✔
3599
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3600
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3601
          
3602
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3603
      continue;
×
3604
    }
3605

3606
    if ((*ppStatus)->status != pTask->status) {
30,676,173✔
3607
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,184,765✔
3608
        (*ppStatus)->runningStartTs = pCtx->currTs;
904,382✔
3609
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,280,383✔
3610
        if (pStream->triggerTask) {
×
3611
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3612
        }
3613
        
3614
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3615
      }
3616
    }
3617
    
3618
    (*ppStatus)->errCode = pTask->errorCode;
30,676,173✔
3619
    (*ppStatus)->status = pTask->status;
30,676,173✔
3620
    (*ppStatus)->lastUpTs = pCtx->currTs;
30,676,173✔
3621
    
3622
    if (STREAM_STATUS_RUNNING != pTask->status) {
30,676,173✔
3623
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
10,908,191✔
3624
    }
3625
    
3626
    if (STREAM_TRIGGER_TASK == pTask->type) {
30,676,173✔
3627
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
3,909,132✔
3628
    }
3629
  }
3630

3631
_exit:
1,043,963✔
3632

3633
  if (code) {
1,043,963✔
3634
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3635
  }
3636

3637
  return code;
1,043,963✔
3638
}
3639

3640
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3641
  int32_t code = TSDB_CODE_SUCCESS;
×
3642
  int32_t lino = 0;
×
3643
  int64_t streamId = pTask->streamId;
×
3644
  SStreamObj* pStream = NULL;
×
3645

3646
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3647
  if (NULL == pStatus) {
×
3648
    SStmStatus status = {0};
×
3649
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3650
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3651
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
3652
      mndReleaseStream(pCtx->pMnode, pStream);
×
3653
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3654
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3655
      return code;
×
3656
    }
3657

3658
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3659
    mndReleaseStream(pCtx->pMnode, pStream);
×
3660

3661
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3662
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3663
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3664
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3665
  }
3666

3667
  SStmTaskStatus* pNewTask = NULL;
×
3668
  switch (pTask->type) {
×
3669
    case STREAM_READER_TASK: {
×
3670
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3671
      if (NULL == pList) {
×
3672
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
3673
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3674
      }
3675
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3676
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3677
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3678
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3679
            (int32_t)taosArrayGetSize(pList), readerSize);
3680
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3681
      }
3682
      
3683
      SStmTaskStatus taskStatus = {0};
×
3684
      taskStatus.pStream = pStatus;
×
3685
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3686
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3687
        pNewTask = taosArrayPush(pList, &taskStatus);
×
3688
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3689
      } else {
3690
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3691
        SListNode* pTailNode = tdListGetTail(pStatus->calcReaders);
×
3692
        QUERY_CHECK_NULL(pTailNode, code, lino, _exit, TSDB_CODE_INTERNAL_ERROR);
×
3693
        pNewTask = (SStmTaskStatus*)pTailNode->data;
×
3694
      }
3695
      
3696
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3697
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3698
      break;
×
3699
    }
3700
    case STREAM_TRIGGER_TASK: {
×
3701
      taosMemoryFreeClear(pStatus->triggerTask);
×
3702
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3703
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3704
      pStatus->triggerTask->pStream = pStatus;
×
3705
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
3706
      pNewTask = pStatus->triggerTask;
×
3707

3708
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3709
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3710
      break;
×
3711
    }
3712
    case STREAM_RUNNER_TASK:{
×
3713
      if (NULL == pStatus->runners[pTask->deployId]) {
×
3714
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3715
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3716
      }
3717
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
3718
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3719
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3720
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3721
      }    
3722
      
3723
      SStmTaskStatus taskStatus = {0};
×
3724
      taskStatus.pStream = pStatus;
×
3725
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3726
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3727
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3728

3729
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3730
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3731
      break;
×
3732
    }
3733
    default: {
×
3734
      msttError("invalid task type:%d in task status", pTask->type);
×
3735
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3736
      break;
×
3737
    }
3738
  }
3739

3740
_exit:
×
3741

3742
  if (code) {
×
3743
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3744
  } else {
3745
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3746
  }
3747

3748
  return code;
×
3749
}
3750

3751
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3752
  int32_t code = TSDB_CODE_SUCCESS;
×
3753
  int32_t lino = 0;
×
3754
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3755

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

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

3762
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3763
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3764
    }
3765
    
3766
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3767
    if (NULL == ppStatus) {
×
3768
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3769
      
3770
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3771
      
3772
      continue;
×
3773
    }
3774
    
3775
    (*ppStatus)->status = pTask->status;
×
3776
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3777
  }
3778

3779
_exit:
×
3780

3781
  if (code) {
×
3782
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3783
  }
3784

3785
  return code;
×
3786
}
3787

3788
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
228,886✔
3789
  int32_t code = TSDB_CODE_SUCCESS;
228,886✔
3790
  int32_t lino = 0;
228,886✔
3791
  if (NULL == pCtx->pRsp->start.taskList) {
228,886✔
3792
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
97,802✔
3793
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
97,802✔
3794
  }
3795

3796
  SStmTaskId* pId = &pAction->start.triggerId;
228,886✔
3797
  SStreamTaskStart start = {0};
228,886✔
3798
  start.task.type = STREAM_TRIGGER_TASK;
228,886✔
3799
  start.task.streamId = streamId;
228,886✔
3800
  start.task.taskId = pId->taskId;
228,886✔
3801
  start.task.seriousId = pId->seriousId;
228,886✔
3802
  start.task.nodeId = pId->nodeId;
228,886✔
3803
  start.task.taskIdx = pId->taskIdx;
228,886✔
3804

3805
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
457,772✔
3806
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
228,886✔
3807

3808
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
228,886✔
3809

3810
  return;
228,886✔
3811

3812
_exit:
×
3813

3814
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3815
}
3816

3817

3818
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
43,411✔
3819
  int32_t code = TSDB_CODE_SUCCESS;
43,411✔
3820
  int32_t lino = 0;
43,411✔
3821
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
43,411✔
3822
  if (NULL == pCtx->pRsp->undeploy.taskList) {
43,411✔
3823
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
29,212✔
3824
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
29,212✔
3825
  }
3826

3827
  SStreamTaskUndeploy undeploy;
43,411✔
3828
  for (int32_t i = 0; i < dropNum; ++i) {
248,653✔
3829
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
205,242✔
3830
    undeploy.task = *pTask;
205,242✔
3831
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
205,242✔
3832
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
205,242✔
3833

3834
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
410,484✔
3835
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
205,242✔
3836

3837
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
205,242✔
3838
  }
3839

3840
  return;
43,411✔
3841

3842
_exit:
×
3843

3844
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3845
}
3846

3847
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3848
  int32_t code = TSDB_CODE_SUCCESS;
×
3849
  int32_t lino = 0;
×
3850

3851
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3852
  if (NULL == pStream) {
×
3853
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3854
    return;
×
3855
  }
3856

3857
  if (NULL == pStream->triggerTask) {
×
3858
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3859
    return;
×
3860
  }
3861

3862
  SStreamMgmtRsp rsp = {0};
×
3863
  rsp.reqId = INT64_MIN;
×
3864
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3865
  rsp.task.streamId = streamId;
×
3866
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3867

3868
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3869

3870
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3871
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3872
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3873
  }
3874

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

3877
_exit:
×
3878

3879
  if (code) {
×
3880
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3881
  } else {
3882
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3883
  }
3884
}
3885

3886
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,420✔
3887
  int32_t code = TSDB_CODE_SUCCESS;
3,420✔
3888
  int32_t lino = 0;
3,420✔
3889

3890
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,420✔
3891
  if (NULL == pStream) {
3,420✔
3892
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3893
    return;
×
3894
  }
3895

3896
  if (NULL == pStream->triggerTask) {
3,420✔
3897
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3898
    return;
×
3899
  }
3900

3901
  SStreamMgmtRsp rsp = {0};
3,420✔
3902
  rsp.reqId = INT64_MIN;
3,420✔
3903
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,420✔
3904
  rsp.task.streamId = streamId;
3,420✔
3905
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,420✔
3906
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,420✔
3907

3908
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,420✔
3909
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,218✔
3910
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,218✔
3911
  }
3912

3913
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,840✔
3914

3915
_exit:
3,420✔
3916

3917
  if (code) {
3,420✔
3918
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3919
  } else {
3920
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,420✔
3921
  }
3922
}
3923

3924

3925
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
129,769✔
3926
  int32_t code = TSDB_CODE_SUCCESS;
129,769✔
3927
  int32_t lino = 0;
129,769✔
3928
  void* pIter = NULL;
129,769✔
3929
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
129,769✔
3930

3931
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
129,769✔
3932

3933
  while (1) {
275,717✔
3934
    pIter = taosHashIterate(pCtx->actionStm, pIter);
405,486✔
3935
    if (pIter == NULL) {
405,486✔
3936
      break;
129,769✔
3937
    }
3938

3939
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
275,717✔
3940
    SStmAction *pAction = (SStmAction *)pIter;
275,717✔
3941
    
3942
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
275,717✔
3943
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
43,411✔
3944
      continue;
43,411✔
3945
    }
3946

3947
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
232,306✔
3948
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3949
    }
3950

3951
    if (STREAM_ACT_RECALC & pAction->actions) {
232,306✔
3952
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,420✔
3953
    }
3954

3955
    if (STREAM_ACT_START & pAction->actions) {
232,306✔
3956
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
228,886✔
3957
    }
3958
  }
3959
  
3960
_exit:
129,769✔
3961

3962
  if (pIter) {
129,769✔
3963
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3964
  }
3965

3966
  if (code) {
129,769✔
3967
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3968
  }
3969

3970
  return code;
129,769✔
3971
}
3972

3973
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
15,716,879✔
3974
  int32_t  code = TSDB_CODE_SUCCESS;
15,716,879✔
3975
  int32_t  lino = 0;
15,716,879✔
3976
  int64_t* lastTs = NULL;
15,716,879✔
3977
  bool     noExists = false;
15,716,879✔
3978

3979
  while (true) {
3980
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
16,209,853✔
3981
    if (NULL == lastTs) {
16,209,853✔
3982
      if (noExists) {
629,082✔
3983
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
136,108✔
3984
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
136,108✔
3985
      }
3986

3987
      noExists = true;
492,974✔
3988
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
492,974✔
3989
      
3990
      continue;
492,974✔
3991
    }
3992

3993
    while (true) {
×
3994
      int64_t lastTsValue = atomic_load_64(lastTs);
15,580,771✔
3995
      if (pCtx->currTs > lastTsValue) {
15,580,771✔
3996
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
15,578,137✔
3997
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
15,578,137✔
3998
          return code;
15,578,137✔
3999
        }
4000

4001
        continue;
×
4002
      }
4003

4004
      return code;
2,634✔
4005
    }
4006

4007
    break;
4008
  }
4009

4010
_exit:
136,108✔
4011

4012
  if (code) {
136,108✔
4013
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
136,108✔
4014
  }
4015

4016
  return code;  
136,108✔
4017
}
4018

4019
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
4020
  SStmStatus* pStatus = NULL;
×
4021
  int32_t trigReaderNum = 0;
×
4022
  int32_t calcReaderNum = 0;
×
4023
  int32_t runnerNum = 0;
×
4024
  int64_t streamId = 0;
×
4025
  void* pIter = NULL;
×
4026
  while (true) {
4027
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
4028
    if (NULL == pIter) {
×
4029
      return;
×
4030
    }
4031

4032
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4033
    pStatus = (SStmStatus*)pIter;
×
4034

4035
    if (NULL == pStatus->triggerTask) {
×
4036
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
4037
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4038
      continue;
×
4039
    }
4040
    
4041
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
4042
    if (pStatus->trigReaderNum != trigReaderNum) {
×
4043
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
4044
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4045
      continue;
×
4046
    }
4047

4048
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
4049
    if (pStatus->calcReaderNum != calcReaderNum) {
×
4050
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
4051
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4052
      continue;
×
4053
    }
4054

4055
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
4056
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
4057
      if (runnerNum != pStatus->runnerNum) {
×
4058
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
4059
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4060
        continue;
×
4061
      }
4062
    }
4063
  }
4064
}
4065

4066
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
360✔
4067
  int32_t code = TSDB_CODE_SUCCESS;
360✔
4068
  int32_t lino = 0;
360✔
4069
  int32_t minVal = watchError ? 0 : 1;
360✔
4070

4071
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
360✔
4072
    return code;
×
4073
  }
4074

4075
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
360✔
4076
    (void)sched_yield();
×
4077
  }
4078

4079
  if (watchError) {
360✔
4080
    taosHashClear(mStreamMgmt.vgroupMap);
×
4081
    taosHashClear(mStreamMgmt.snodeMap);
×
4082
    taosHashClear(mStreamMgmt.taskMap);
×
4083
    taosHashClear(mStreamMgmt.streamMap);
×
4084
    mstInfo("watch error happends, clear all maps");
×
4085
    goto _exit;
×
4086
  }
4087

4088
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
360✔
4089
    mstInfo("no stream tasks remain during watch state");
360✔
4090
    goto _exit;
360✔
4091
  }
4092

4093
  msmWatchCheckStreamMap(pCtx);
×
4094

4095
_exit:
360✔
4096

4097
  mStreamMgmt.lastTaskId += 100000;
360✔
4098

4099
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
360✔
4100

4101
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
360✔
4102

4103
  if (code) {
360✔
4104
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4105
  }
4106

4107
  return code;
360✔
4108
}
4109

4110

4111
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
12,447✔
4112
  int32_t code = TSDB_CODE_SUCCESS;
12,447✔
4113
  int32_t lino = 0;
12,447✔
4114
  SStreamHbMsg* pReq = pCtx->pReq;
12,447✔
4115

4116
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
12,447✔
4117
  
4118
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
12,447✔
4119
    goto _exit;
×
4120
  }
4121

4122
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
12,447✔
4123
  if (GOT_SNODE(pReq->snodeId)) {
12,447✔
4124
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,114✔
4125
  }
4126

4127
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
12,447✔
4128
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4129
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4130
  }
4131

4132
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
12,447✔
4133
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
360✔
4134
  }
4135

4136
_exit:
12,447✔
4137

4138
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
12,447✔
4139
  
4140
  if (code) {
12,447✔
4141
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4142

4143
    (void)msmWatchHandleEnding(pCtx, true);
×
4144
  }
4145

4146
  return code;
12,447✔
4147
}
4148

4149
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,183,447✔
4150
  int32_t listSize = taosArrayGetSize(pOReaders);
5,183,447✔
4151
  int32_t totalSize = 0;
5,183,447✔
4152
  
4153
  for (int32_t i = 0; i < listSize; ++i) {
6,096,371✔
4154
    SArray* pList = taosArrayGetP(pOReaders, i);
912,924✔
4155
    totalSize += taosArrayGetSize(pList);
912,924✔
4156
  }
4157

4158
  return totalSize;
5,183,447✔
4159
}
4160

4161
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
571,016✔
4162
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
571,016✔
4163
  if (NULL == pList) {
571,016✔
4164
    return NULL;
×
4165
  }
4166

4167
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
571,016✔
4168
}
4169

4170

4171
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
53,064✔
4172
  int32_t code = TSDB_CODE_SUCCESS;
53,064✔
4173
  int32_t lino = 0;
53,064✔
4174

4175
  if (NULL == pStatus->trigOReaders) {
53,064✔
4176
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
46,004✔
4177
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
46,004✔
4178
  }
4179

4180
  while (true) {
46,004✔
4181
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
99,068✔
4182

4183
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
99,068✔
4184
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
46,004✔
4185
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
46,004✔
4186

4187
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
92,008✔
4188
      continue;
46,004✔
4189
    }
4190

4191
    *ppRes = *ppOReaderList;
53,064✔
4192
    break;
53,064✔
4193
  }
4194

4195
_exit:
53,064✔
4196

4197
  if (code) {
53,064✔
4198
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4199
  }
4200

4201
  return code;
53,064✔
4202
}
4203

4204
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
63,393✔
4205
  int32_t code = TSDB_CODE_SUCCESS;
63,393✔
4206
  int32_t lino = 0;
63,393✔
4207
  bool    readerExists = false;
63,393✔
4208
  int64_t streamId = pTask->streamId;
63,393✔
4209

4210
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
63,393✔
4211
  for (int32_t i = 0; i < readerNum; ++i) {
130,723✔
4212
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
77,659✔
4213
    if (pReader->id.nodeId == vgId) {
77,659✔
4214
      readerExists = true;
10,329✔
4215
      break;
10,329✔
4216
    }
4217
  }
4218

4219
  if (!readerExists) {
63,393✔
4220
    SArray* pReaderList = NULL;
53,064✔
4221
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
53,064✔
4222
    
4223
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
53,064✔
4224
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
53,064✔
4225
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
53,064✔
4226
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
53,064✔
4227
  }
4228

4229
_exit:
10,329✔
4230

4231
  if (code) {
63,393✔
4232
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4233
  }
4234

4235
  return code;
63,393✔
4236
}
4237

4238
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
55,727✔
4239
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
55,727✔
4240
  int32_t lino = 0;
55,727✔
4241
  int32_t vgId = 0;
55,727✔
4242
  int64_t streamId = pTask->streamId;
55,727✔
4243
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
55,727✔
4244
  int32_t tbNum = taosArrayGetSize(pTbs);
55,727✔
4245
  SStreamDbTableName* pName = NULL;
55,727✔
4246
  SSHashObj* pDbVgroups = NULL;
55,727✔
4247
  SStreamMgmtRsp rsp = {0};
55,727✔
4248
  rsp.reqId = pTask->pMgmtReq->reqId;
55,727✔
4249
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
55,727✔
4250
  int32_t iter = 0;
55,727✔
4251
  void* p = NULL;
55,727✔
4252
  SSHashObj* pVgs = NULL;
55,727✔
4253
  SStreamMgmtReq* pMgmtReq = NULL;
55,727✔
4254
  int8_t stopped = 0;
55,727✔
4255

4256
  if (NULL == pCtx->pRsp->rsps.rspList) {
55,727✔
4257
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4258
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4259
  }
4260
  
4261
  TSWAP(pTask->pMgmtReq, pMgmtReq);
55,727✔
4262
  rsp.task = *(SStreamTask*)pTask;
55,727✔
4263

4264
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
55,727✔
4265
  if (NULL == pStatus) {
55,727✔
4266
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4267
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4268
  }
4269

4270
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
55,727✔
UNCOV
4271
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
UNCOV
4272
    goto _exit;
×
4273
  }
4274

4275
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
55,727✔
4276

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

4283
  if (tbNum <= 0) {
55,727✔
4284
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4285
    goto _exit;
×
4286
  }
4287

4288
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
55,727✔
4289
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
55,727✔
4290
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
55,727✔
4291

4292
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
55,727✔
4293
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
55,727✔
4294
  
4295
  for (int32_t i = 0; i < tbNum; ++i) {
219,496✔
4296
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
163,769✔
4297
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
163,769✔
4298
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
327,538✔
4299
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
163,769✔
4300
  }
4301

4302
  int32_t vgNum = tSimpleHashGetSize(pVgs);
55,727✔
4303
  while (true) {
4304
    p = tSimpleHashIterate(pVgs, p, &iter);
119,120✔
4305
    if (NULL == p) {
119,120✔
4306
      break;
55,727✔
4307
    }
4308
    
4309
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
63,393✔
4310
  }
4311
  
4312
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
55,727✔
4313
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
55,727✔
4314
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
55,727✔
4315

4316
  SStreamTaskAddr addr;
55,727✔
4317
  for (int32_t i = 0; i < vgNum; ++i) {
108,791✔
4318
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
53,064✔
4319
    addr.taskId = pOTask->id.taskId;
53,064✔
4320
    addr.nodeId = pOTask->id.nodeId;
53,064✔
4321
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
53,064✔
4322
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
106,128✔
4323
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
53,064✔
4324
  }
4325

4326
_exit:
55,727✔
4327

4328
  tFreeSStreamMgmtReq(pMgmtReq);
55,727✔
4329
  taosMemoryFree(pMgmtReq);
55,727✔
4330

4331
  tSimpleHashCleanup(pVgs);
55,727✔
4332
  mstDestroyDbVgroupsHash(pDbVgroups);
55,727✔
4333

4334
  if (code) {
55,727✔
4335
    rsp.code = code;
×
4336
    
4337
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4338

4339
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4340
  } else {
4341
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
111,454✔
4342
  }
4343

4344
_final:
55,727✔
4345

4346
  if (finalCode) {
55,727✔
4347
    tFreeSStreamMgmtRsp(&rsp);
×
4348
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4349
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4350
  }
4351

4352
  return finalCode;
55,727✔
4353
}
4354

4355
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,398✔
4356
  int32_t num = taosArrayGetSize(pList);
2,398✔
4357
  SStreamCalcScan* pScan = NULL;
2,398✔
4358
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,398✔
4359
  int64_t planUid = 0;
2,398✔
4360
  for (int32_t i = 0; i < num; ++i) {
2,398✔
4361
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,398✔
4362
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,398✔
4363
    if (0 != planUid && planUid == uid) {
2,398✔
4364
      *ppRes = pScan;
2,398✔
4365
      break;
2,398✔
4366
    }
4367
  }
4368

4369
_exit:
2,398✔
4370

4371
  if (code) {
2,398✔
4372
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4373
  }
4374

4375
  return code;
2,398✔
4376
}
4377

4378
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,398✔
4379
  int32_t code = TSDB_CODE_SUCCESS;
2,398✔
4380
  int32_t lino = 0;
2,398✔
4381
  bool    readerExists = false;
2,398✔
4382
  int64_t streamId = pTask->streamId;
2,398✔
4383
  SListNode* pNode = listHead(pStatus->calcReaders);
2,398✔
4384
  SStmTaskStatus* pReader = NULL;
2,398✔
4385
  int32_t taskIdx = 0;
2,398✔
4386

4387
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,398✔
4388
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
22,236✔
4389
    pReader = (SStmTaskStatus*)pNode->data;
19,838✔
4390
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
19,838✔
4391
      readerExists = true;
×
4392
      pAddr->taskId = pReader->id.taskId;
×
4393
      break;
×
4394
    }
4395
  }
4396

4397
  if (!readerExists) {
2,398✔
4398
    if (NULL == pStatus->calcReaders) {
2,398✔
4399
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4400
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4401
      taskIdx = 0;
×
4402
    } else {
4403
      pNode = listTail(pStatus->calcReaders);
2,398✔
4404
      pReader = (SStmTaskStatus*)pNode->data;
2,398✔
4405
      taskIdx = pReader->id.taskIdx + 1;
2,398✔
4406
    }
4407

4408
    SStreamCalcScan* pScan = NULL;
2,398✔
4409
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,398✔
4410
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,398✔
4411
    pReader = tdListReserve(pStatus->calcReaders);
2,398✔
4412
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,398✔
4413
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,398✔
4414
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,398✔
4415
    pAddr->taskId = pReader->id.taskId;
2,398✔
4416
  }
4417

4418
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,398✔
4419
  pAddr->nodeId = vgId;
2,398✔
4420

4421
_exit:
2,398✔
4422

4423
  if (code) {
2,398✔
4424
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4425
  }
4426

4427
  return code;
2,398✔
4428
}
4429

4430

4431
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,398✔
4432
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,398✔
4433
  int32_t lino = 0;
2,398✔
4434
  int32_t vgId = 0;
2,398✔
4435
  int64_t streamId = pTask->streamId;
2,398✔
4436
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,398✔
4437
  int32_t reqNum = taosArrayGetSize(pReqs);
2,398✔
4438
  SStreamOReaderDeployReq* pReq = NULL;
2,398✔
4439
  SStreamOReaderDeployRsp* pRsp = NULL;
2,398✔
4440
  SStreamMgmtRsp rsp = {0};
2,398✔
4441
  rsp.reqId = pTask->pMgmtReq->reqId;
2,398✔
4442
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,398✔
4443
  SStreamMgmtReq* pMgmtReq = NULL;
2,398✔
4444
  int8_t stopped = 0;
2,398✔
4445
  int32_t vgNum = 0;
2,398✔
4446
  SStreamTaskAddr* pAddr = NULL;
2,398✔
4447

4448
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,398✔
4449
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4450
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4451
  }
4452
  
4453
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,398✔
4454
  rsp.task = *(SStreamTask*)pTask;
2,398✔
4455

4456
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,398✔
4457
  if (NULL == pStatus) {
2,398✔
4458
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4459
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4460
  }
4461

4462
  stopped = atomic_load_8(&pStatus->stopped);
2,398✔
4463
  if (stopped) {
2,398✔
4464
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4465
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4466
  }
4467

4468
  if (reqNum <= 0) {
2,398✔
4469
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4470
    goto _exit;
×
4471
  }
4472

4473
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,398✔
4474
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,398✔
4475

4476
  for (int32_t i = 0; i < reqNum; ++i) {
4,796✔
4477
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,398✔
4478
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,398✔
4479
    pRsp->execId = pReq->execId;
2,398✔
4480
    vgNum = taosArrayGetSize(pReq->vgIds);
2,398✔
4481
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,398✔
4482
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,398✔
4483
    
4484
    for (int32_t n = 0; n < vgNum; ++n) {
4,796✔
4485
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,398✔
4486
      pAddr = taosArrayGet(pRsp->vgList, n);
2,398✔
4487
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,398✔
4488
    }
4489
  }
4490

4491
_exit:
2,398✔
4492

4493
  tFreeSStreamMgmtReq(pMgmtReq);
2,398✔
4494
  taosMemoryFree(pMgmtReq);
2,398✔
4495

4496
  if (code) {
2,398✔
4497
    rsp.code = code;
×
4498
    
4499
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4500
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4501
  } else {
4502
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,796✔
4503
  }
4504

4505

4506
_final:
2,398✔
4507

4508
  if (finalCode) {
2,398✔
4509
    tFreeSStreamMgmtRsp(&rsp);
×
4510
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4511
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4512
  }
4513

4514
  return finalCode;
2,398✔
4515
}
4516

4517

4518
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
58,125✔
4519
  int32_t code = TSDB_CODE_SUCCESS;
58,125✔
4520
  int32_t lino = 0;
58,125✔
4521

4522
  switch (pTask->pMgmtReq->type) {
58,125✔
4523
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
55,727✔
4524
      msmDeployTriggerOrigReader(pCtx, pTask);
55,727✔
4525
      break;
55,727✔
4526
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,398✔
4527
      msmDeployRunnerOrigReader(pCtx, pTask);
2,398✔
4528
      break;
2,398✔
4529
    default:
×
4530
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4531
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4532
      break;
×
4533
  }
4534

4535
_exit:
58,125✔
4536

4537
  if (code) {
58,125✔
4538
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4539
  }
4540

4541
  return code;
58,125✔
4542
}
4543

4544
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
17,154✔
4545
  int32_t code = TSDB_CODE_SUCCESS;
17,154✔
4546
  int32_t lino = 0;
17,154✔
4547
  SStreamHbMsg* pReq = pCtx->pReq;
17,154✔
4548
  SStmTaskStatusMsg* pTask = NULL;
17,154✔
4549
  
4550
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
17,154✔
4551
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
17,154✔
4552
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
17,154✔
4553
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
17,154✔
4554
  }
4555
  
4556
  for (int32_t i = 0; i < reqNum; ++i) {
75,279✔
4557
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
58,125✔
4558
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
58,125✔
4559
    if (NULL == pTask) {
58,125✔
4560
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4561
      continue;
×
4562
    }
4563

4564
    if (NULL == pTask->pMgmtReq) {
58,125✔
4565
      msttError("idx %d without mgmtReq", idx);
×
4566
      continue;
×
4567
    }
4568

4569
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
58,125✔
4570
  }
4571

4572
_exit:
17,154✔
4573

4574
  if (code) {
17,154✔
4575
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4576
  }
4577

4578
  return code;
17,154✔
4579
}
4580

4581
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
15,704,432✔
4582
  int32_t code = TSDB_CODE_SUCCESS;
15,704,432✔
4583
  int32_t lino = 0;
15,704,432✔
4584
  SStreamHbMsg* pReq = pCtx->pReq;
15,704,432✔
4585

4586
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
15,704,432✔
4587
  if (GOT_SNODE(pReq->snodeId)) {
15,568,324✔
4588
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,195,794✔
4589
  }
4590
  
4591
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
15,568,056✔
4592
    msmHandleStreamActions(pCtx);
44,240✔
4593
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
44,240✔
4594
  }
4595

4596
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
15,568,056✔
4597
    code = msmHandleStreamRequests(pCtx);
17,154✔
4598
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
17,154✔
4599
    TAOS_CHECK_EXIT(code);
17,154✔
4600
  }
4601

4602
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
15,568,056✔
4603
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
60,613✔
4604
  } else {
4605
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
15,507,443✔
4606
  }
4607

4608
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
15,568,056✔
4609
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
50,180✔
4610
  }
4611

4612
  if (taosHashGetSize(pCtx->deployStm) > 0) {
15,568,056✔
4613
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
61,266✔
4614
  }
4615

4616
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
15,568,056✔
4617
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,043,963✔
4618
  }
4619

4620
  if (taosHashGetSize(pCtx->actionStm) > 0) {
15,568,056✔
4621
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
129,769✔
4622
  }
4623

4624
_exit:
15,568,056✔
4625

4626
  if (code) {
15,704,432✔
4627
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
136,376✔
4628
  }
4629

4630
  return code;
15,704,432✔
4631
}
4632

4633
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
15,790,168✔
4634
  int32_t lino = 0;
15,790,168✔
4635
  int32_t tlen = 0;
15,790,168✔
4636
  void   *buf = NULL;
15,790,168✔
4637

4638
  if (TSDB_CODE_SUCCESS != code) {
15,790,168✔
4639
    goto _exit;
136,376✔
4640
  }
4641

4642
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
15,653,792✔
4643
  if (code < 0) {
15,653,792✔
4644
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4645
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4646
  }
4647

4648
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
15,653,792✔
4649
  if (buf == NULL) {
15,653,792✔
4650
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4651
    TAOS_CHECK_EXIT(terrno);    
×
4652
  }
4653

4654
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
15,653,792✔
4655
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
15,653,792✔
4656

4657
  SEncoder encoder;
15,650,391✔
4658
  tEncoderInit(&encoder, abuf, tlen);
15,653,792✔
4659
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
15,653,792✔
4660
    rpcFreeCont(buf);
×
4661
    buf = NULL;
×
4662
    tEncoderClear(&encoder);
×
4663
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4664
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4665
  }
4666
  tEncoderClear(&encoder);
15,653,792✔
4667

4668
_exit:
15,790,168✔
4669

4670
  pMsg->code = code;
15,790,168✔
4671
  pMsg->info = *pRpcInfo;
15,790,168✔
4672
  if (TSDB_CODE_SUCCESS == code) {
15,790,168✔
4673
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
15,653,792✔
4674
    pMsg->pCont = buf;
15,653,792✔
4675
  }
4676
}
15,790,168✔
4677

4678

4679
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
15,790,168✔
4680
  int32_t code = TSDB_CODE_SUCCESS;
15,790,168✔
4681
  SMStreamHbRspMsg rsp = {0};
15,790,168✔
4682
  rsp.streamGId = pHb->streamGId;
15,790,168✔
4683

4684
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
15,790,168✔
4685

4686
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
15,790,168✔
4687
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
73,289✔
4688
    goto _exit;
73,289✔
4689
  }
4690

4691
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
15,716,879✔
4692
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
15,716,879✔
4693

4694
  pCtx->tidx = tidx;
15,716,879✔
4695
  pCtx->pMnode = pMnode;
15,716,879✔
4696
  pCtx->currTs = currTs;
15,716,879✔
4697
  pCtx->pReq = pHb;
15,716,879✔
4698
  pCtx->pRsp = &rsp;
15,716,879✔
4699
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
15,716,879✔
4700
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
15,716,879✔
4701
  
4702
  switch (atomic_load_8(&mStreamMgmt.state)) {
15,716,879✔
4703
    case MND_STM_STATE_WATCH:
12,447✔
4704
      code = msmWatchHandleHbMsg(pCtx);
12,447✔
4705
      break;
12,447✔
4706
    case MND_STM_STATE_NORMAL:
15,704,432✔
4707
      code = msmNormalHandleHbMsg(pCtx);
15,704,432✔
4708
      break;
15,704,432✔
4709
    default:
×
4710
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4711
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4712
      break;
×
4713
  }
4714

4715
_exit:
15,790,168✔
4716

4717
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
15,790,168✔
4718

4719
  msmCleanStreamGrpCtx(pHb);
15,790,168✔
4720
  msmClearStreamToDeployMaps(pHb);
15,790,168✔
4721

4722
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
15,790,168✔
4723
  
4724
  tFreeSMStreamHbRspMsg(&rsp);
15,790,168✔
4725

4726
  return code;
15,790,168✔
4727
}
4728

4729
void msmHandleBecomeLeader(SMnode *pMnode) {
409,341✔
4730
  if (tsDisableStream) {
409,341✔
4731
    return;
×
4732
  }
4733

4734
  mstInfo("start to process mnode become leader");
409,341✔
4735

4736
  int32_t code = 0;
409,341✔
4737
  streamAddVnodeLeader(MNODE_HANDLE);
409,341✔
4738
  
4739
  taosWLockLatch(&mStreamMgmt.runtimeLock);
409,341✔
4740
  msmDestroyRuntimeInfo(pMnode);
409,341✔
4741
  code = msmInitRuntimeInfo(pMnode);
409,341✔
4742
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
409,341✔
4743

4744
  if (TSDB_CODE_SUCCESS == code) {
409,341✔
4745
    atomic_store_8(&mStreamMgmt.active, 1);
409,341✔
4746
  }
4747

4748
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
409,341✔
4749
}
4750

4751
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
501,074✔
4752
  if (tsDisableStream) {
501,074✔
4753
    return;
×
4754
  }
4755

4756
  mstInfo("start to process mnode become not leader");
501,074✔
4757

4758
  streamRemoveVnodeLeader(MNODE_HANDLE);
501,074✔
4759

4760
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
501,074✔
4761
    taosWLockLatch(&mStreamMgmt.runtimeLock);
409,341✔
4762
    msmDestroyRuntimeInfo(pMnode);
409,341✔
4763
    mStreamMgmt.stat.inactiveTimes++;
409,341✔
4764
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
409,341✔
4765
  }
4766
}
4767

4768

4769
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4770
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4771
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4772
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4773
  } else {
4774
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4775
  }
4776
}
×
4777

4778
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
7,818✔
4779
  int32_t code = TSDB_CODE_SUCCESS;
7,818✔
4780
  int32_t lino = 0;
7,818✔
4781
  SStreamObj* pStream = pObj;
7,818✔
4782
  SSnodeObj* pSnode = p1;
7,818✔
4783
  SArray** ppRes = p2;
7,818✔
4784

4785
  if (pStream->mainSnodeId == pSnode->id) {
7,818✔
4786
    if (NULL == *ppRes) {
4,244✔
4787
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
828✔
4788
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
828✔
4789
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
828✔
4790
    }
4791

4792
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
8,488✔
4793
  }
4794

4795
  return true;
7,818✔
4796

4797
_exit:
×
4798

4799
  if (code) {
×
4800
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4801
  }  
4802

4803
  *(int32_t*)p3 = code;
×
4804

4805
  return false;
×
4806
}
4807

4808

4809
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
40,687✔
4810
  int32_t code = TSDB_CODE_SUCCESS;
40,687✔
4811
  int32_t lino = 0;
40,687✔
4812
  
4813
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
40,687✔
4814
  TAOS_CHECK_EXIT(code);
40,687✔
4815

4816
  int32_t streamNum = taosArrayGetSize(*ppRes);
40,687✔
4817
  if (streamNum > 0 && 0 == pSnode->replicaId) {
40,687✔
4818
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
202✔
4819
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
202✔
4820
  }
4821

4822
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4823

4824
_exit:
40,687✔
4825

4826
  if (code) {
40,687✔
4827
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
202✔
4828
  }  
4829

4830
  return code;
40,687✔
4831
}
4832

4833
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
598,090✔
4834
  SStreamObj* pStream = pObj;
598,090✔
4835
  int64_t streamId = pStream->pCreate->streamId;
598,090✔
4836
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
598,090✔
4837
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
598,090✔
4838
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
598,090✔
4839
  
4840
  if ((userDropped || userStopped) && (NULL == pStatus)) {
598,090✔
4841
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
202✔
4842
    return true;
202✔
4843
  }
4844
  
4845
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
597,888✔
4846
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
539,391✔
4847
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4848
    return true;
539,391✔
4849
  }
4850

4851
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
58,497✔
4852
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
39,055✔
4853
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4854
    return true;
39,055✔
4855
  }  
4856

4857
  if (pStatus) {
19,442✔
4858
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
19,120✔
4859
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4860
    }
4861

4862
    return true;
19,120✔
4863
  }
4864

4865
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
322✔
4866

4867
  return true;
322✔
4868
}
4869

4870
void msmCheckLoopStreamMap(SMnode *pMnode) {
44,879✔
4871
  SStmStatus* pStatus = NULL;
44,879✔
4872
  void* pIter = NULL;
44,879✔
4873
  int8_t stopped = 0;
44,879✔
4874
  int64_t streamId = 0;
44,879✔
4875
  
4876
  while (true) {
4877
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
322,736✔
4878
    if (NULL == pIter) {
322,736✔
4879
      break;
44,879✔
4880
    }
4881

4882
    pStatus = (SStmStatus*)pIter;
277,857✔
4883

4884
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
277,857✔
4885
    stopped = atomic_load_8(&pStatus->stopped);
277,857✔
4886
    if (MST_IS_USER_STOPPED(stopped)) {
277,857✔
4887
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
432✔
4888
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
432✔
4889
      continue;
432✔
4890
    }
4891

4892
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
277,425✔
4893
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4894
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4895
      continue;
×
4896
    }
4897

4898
    if (MST_IS_ERROR_STOPPED(stopped)) {
277,425✔
4899
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
13,695✔
4900
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
9,278✔
4901
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4902
            
4903
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
9,278✔
4904
        continue;
9,278✔
4905
      }
4906

4907
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
4,417✔
4908
      continue;
4,417✔
4909
    }
4910

4911
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
263,730✔
4912
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4913
      continue;
×
4914
    }
4915
  }
4916
}
44,879✔
4917

4918
void msmCheckStreamsStatus(SMnode *pMnode) {
337,155✔
4919
  SStmCheckStatusCtx ctx = {0};
337,155✔
4920

4921
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
337,155✔
4922
  
4923
  if (MST_READY_FOR_SDB_LOOP()) {
337,155✔
4924
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
87,809✔
4925
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
87,809✔
4926
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
87,809✔
4927
  }
4928

4929
  if (MST_READY_FOR_MAP_LOOP()) {
337,155✔
4930
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
44,879✔
4931
    msmCheckLoopStreamMap(pMnode);
44,879✔
4932
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
44,879✔
4933
  }
4934
}
337,155✔
4935

4936
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,534,354✔
4937
  for (int32_t i = 0; i < taskNum; ++i) {
7,460,585✔
4938
    SStmTaskStatus* pTask = *(pList + i);
3,926,231✔
4939

4940
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
3,926,231✔
4941
      continue;
463,622✔
4942
    }
4943
    
4944
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,463,511✔
4945
      continue;
3,462,407✔
4946
    }
4947

4948
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,104✔
4949
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,104✔
4950
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4951
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4952
          
4953
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
4954
      break;
×
4955
    }
4956

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

4960
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,104✔
4961
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,104✔
4962

4963
    SStmTaskAction task = {0};
1,104✔
4964
    task.streamId = streamId;
1,104✔
4965
    task.id = pTask->id;
1,104✔
4966
    task.flag = pTask->flags;
1,104✔
4967
    task.type = pTask->type;
1,104✔
4968
    
4969
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,104✔
4970
  }
4971
}
3,534,354✔
4972

4973
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
130,663✔
4974
  void* pIter = NULL;
130,663✔
4975
  SStmVgStreamStatus* pVg = NULL;
130,663✔
4976
  int64_t streamId = 0;
130,663✔
4977
  
4978
  while (true) {
1,320,300✔
4979
    pIter = taosHashIterate(pStreams, pIter);
1,450,963✔
4980
    if (NULL == pIter) {
1,450,963✔
4981
      break;
130,663✔
4982
    }
4983

4984
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,320,300✔
4985
    pVg = (SStmVgStreamStatus*)pIter;
1,320,300✔
4986

4987
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,320,300✔
4988
    if (taskNum > 0) {
1,320,300✔
4989
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
806,372✔
4990
    }
4991

4992
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,320,300✔
4993
    if (taskNum > 0) {
1,320,300✔
4994
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
865,160✔
4995
    }
4996
  }
4997
}
130,663✔
4998

4999
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
5000
  int64_t streamId = 0;
×
5001
  void* pIter = NULL;
×
5002
  SStmVgStreamStatus* pStream = NULL;
×
5003

5004
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
5005
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5006
    return;
×
5007
  }
5008

5009
  
5010
  while (true) {
5011
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
5012
    if (NULL == pIter) {
×
5013
      break;
×
5014
    }
5015

5016
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
5017
    
5018
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
5019
  }
5020

5021
  taosHashClear(pVg->streamTasks);
×
5022
}
5023

5024

5025
void msmCheckVgroupStatus(SMnode *pMnode) {
337,155✔
5026
  void* pIter = NULL;
337,155✔
5027
  int32_t code = 0;
337,155✔
5028
  
5029
  while (true) {
1,207,518✔
5030
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,544,673✔
5031
    if (NULL == pIter) {
1,544,673✔
5032
      break;
337,155✔
5033
    }
5034

5035
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,207,518✔
5036
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,207,518✔
5037
      continue;
1,072,060✔
5038
    }
5039
    
5040
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
135,458✔
5041

5042
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
135,458✔
5043
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
4,795✔
5044
      if (NULL == pVgroup) {
4,795✔
5045
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
4,795✔
5046
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
4,795✔
5047
        if (code) {
4,795✔
5048
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
5049
        }
5050
        continue;
4,795✔
5051
      }
5052
      mndReleaseVgroup(pMnode, pVgroup);
×
5053
      
5054
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5055
      
5056
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
5057
      continue;
×
5058
    }
5059

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

5062
    msmCheckVgroupStreamStatus(pVg->streamTasks);
130,663✔
5063
  }
5064
}
337,155✔
5065

UNCOV
5066
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
×
UNCOV
5067
  *deployNum = 0;
×
5068
  
UNCOV
5069
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
×
UNCOV
5070
    if (pStream->runners[i]) {
×
UNCOV
5071
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
×
UNCOV
5072
      for (int32_t t = 0; t < taskNum; ++t) {
×
UNCOV
5073
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
×
UNCOV
5074
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
×
UNCOV
5075
        if (stopped) {
×
5076
          mstsDebug("stream already stopped %d, ignore it", stopped);
×
5077
          *deployNum = 0;
×
5078
          return;
×
5079
        }
5080

UNCOV
5081
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
×
UNCOV
5082
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
×
5083
      }
5084
      
UNCOV
5085
      deployId[*deployNum] = i;
×
UNCOV
5086
      (*deployNum)++;
×
5087
    }
5088
  }
5089
}
5090

5091
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
1,288✔
5092
  pSnode->runnerThreadNum = -1;
1,288✔
5093

5094
  (void)msmSTAddSnodesToMap(pMnode);
1,288✔
5095

5096
  int64_t streamId = 0;
1,288✔
5097
  void* pIter = NULL;
1,288✔
5098
  SStmSnodeStreamStatus* pStream = NULL;
1,288✔
5099
  int32_t deployNum = 0;
1,288✔
5100
  SStmTaskAction task = {0};
1,288✔
5101
  
5102
  while (true) {
5103
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
1,472✔
5104
    if (NULL == pIter) {
1,472✔
5105
      break;
1,288✔
5106
    }
5107

5108
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
184✔
5109
    
5110
    task.streamId = streamId;
184✔
5111
    
5112
    pStream = (SStmSnodeStreamStatus*)pIter;
184✔
5113
    if (pStream->trigger) {
184✔
5114
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
184✔
5115
      if (stopped) {
184✔
5116
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
5117
        continue;
×
5118
      }
5119

5120
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
184✔
5121
      
5122
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
184✔
5123
    } else {
UNCOV
5124
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
×
5125
      
UNCOV
5126
      if (task.deployNum > 0) {
×
5127
        //task.triggerStatus = pStream->trigger;
UNCOV
5128
        task.multiRunner = true;
×
UNCOV
5129
        task.type = STREAM_RUNNER_TASK;
×
5130
        
UNCOV
5131
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
×
5132
        
UNCOV
5133
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
×
5134
      }
5135
    }
5136
  }
5137

5138
  taosHashClear(pSnode->streamTasks);
1,288✔
5139
}
1,288✔
5140

5141

5142
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
38,489✔
5143
  void* pIter = NULL;
38,489✔
5144
  SStmSnodeStreamStatus* pSnode = NULL;
38,489✔
5145
  int64_t streamId = 0;
38,489✔
5146
  
5147
  while (true) {
5148
    pIter = taosHashIterate(pStreams, pIter);
527,283✔
5149
    if (NULL == pIter) {
527,283✔
5150
      break;
38,489✔
5151
    }
5152

5153
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
488,794✔
5154
    pSnode = (SStmSnodeStreamStatus*)pIter;
488,794✔
5155

5156
    if (NULL != pSnode->trigger) {
488,794✔
5157
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
477,818✔
5158
    }
5159

5160
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,955,176✔
5161
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,466,382✔
5162
      if (taskNum > 0) {
1,466,382✔
5163
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,385,004✔
5164
      }
5165
    }
5166
  }
5167
}
38,489✔
5168

5169

5170
void msmCheckSnodeStatus(SMnode *pMnode) {
337,155✔
5171
  void* pIter = NULL;
337,155✔
5172
  
5173
  while (true) {
400,257✔
5174
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
737,412✔
5175
    if (NULL == pIter) {
737,412✔
5176
      break;
337,155✔
5177
    }
5178

5179
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
400,257✔
5180
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
400,257✔
5181
      continue;
350,801✔
5182
    }
5183

5184
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
49,456✔
5185
    
5186
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
49,456✔
5187
    if (NULL == pSnode->streamTasks) {
49,456✔
5188
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
9,863✔
5189
      continue;
9,863✔
5190
    }
5191
    
5192
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
39,593✔
5193
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,104✔
5194
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5195
      
5196
      msmHandleSnodeLost(pMnode, pSnode);
1,104✔
5197
      continue;
1,104✔
5198
    }
5199
    
5200
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
38,489✔
5201

5202
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
38,489✔
5203
  }
5204
}
337,155✔
5205

5206

5207
void msmCheckTasksStatus(SMnode *pMnode) {
337,155✔
5208
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
337,155✔
5209

5210
  msmCheckVgroupStatus(pMnode);
337,155✔
5211
  msmCheckSnodeStatus(pMnode);
337,155✔
5212
}
337,155✔
5213

5214
void msmCheckSnodesState(SMnode *pMnode) {
337,155✔
5215
  if (!MST_READY_FOR_SNODE_LOOP()) {
337,155✔
5216
    return;
297,870✔
5217
  }
5218

5219
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
39,285✔
5220

5221
  void* pIter = NULL;
39,285✔
5222
  int32_t snodeId = 0;
39,285✔
5223
  while (true) {
42,363✔
5224
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
81,648✔
5225
    if (NULL == pIter) {
81,648✔
5226
      break;
39,285✔
5227
    }
5228

5229
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
42,363✔
5230
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
42,363✔
5231
      continue;
42,179✔
5232
    }
5233

5234
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
184✔
5235
    if (NULL == pSnode->streamTasks) {
184✔
5236
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5237
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5238
      continue;
×
5239
    }
5240
    
5241
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
184✔
5242
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5243
    
5244
    msmHandleSnodeLost(pMnode, pSnode);
184✔
5245
  }
5246

5247
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
39,285✔
5248
}
5249

5250
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
9,881,532✔
5251
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
9,881,532✔
5252
  if (0 == active || MND_STM_STATE_NORMAL != state) {
9,881,532✔
5253
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,445✔
5254
    return false;
2,445✔
5255
  }
5256

5257
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
9,879,087✔
5258
    mstTrace("ignore health check since no stream now");
9,204,777✔
5259
    return false;
9,204,777✔
5260
  }
5261

5262
  return true;
674,310✔
5263
}
5264

5265
void msmHealthCheck(SMnode *pMnode) {
9,544,377✔
5266
  if (!msmCheckNeedHealthCheck(pMnode)) {
9,544,377✔
5267
    return;
9,207,222✔
5268
  }
5269

5270
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
651,480✔
5271
  
5272
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
337,155✔
5273
  if (!msmCheckNeedHealthCheck(pMnode)) {
337,155✔
5274
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5275
    return;
×
5276
  }
5277
  
5278
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
337,155✔
5279
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
337,155✔
5280

5281
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
337,155✔
5282
  
5283
  msmCheckStreamsStatus(pMnode);
337,155✔
5284
  msmCheckTasksStatus(pMnode);
337,155✔
5285
  msmCheckSnodesState(pMnode);
337,155✔
5286

5287
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
337,155✔
5288

5289
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
337,155✔
5290
}
5291

5292
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
869✔
5293
  SStreamObj *pStream = pObj;
869✔
5294
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
869✔
5295
    return true;
×
5296
  }
5297
  
5298
  pStream->updateTime = *(int64_t*)p1;
869✔
5299
  
5300
  (*(int32_t*)p2)++;
869✔
5301
  
5302
  return true;
869✔
5303
}
5304

5305
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
21,790✔
5306
  int32_t code = 0;
21,790✔
5307
  int8_t  stopped = 0;
21,790✔
5308
  
5309
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
21,790✔
5310
  
5311
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
21,790✔
5312
  if (NULL == pStatus) {
21,790✔
5313
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,094✔
5314
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,094✔
5315
    goto _exit;
3,094✔
5316
  }
5317

5318
  stopped = atomic_load_8(&pStatus->stopped);
18,696✔
5319
  if (stopped) {
18,696✔
5320
    mstsError("stream already stopped, stopped:%d", stopped);
×
5321
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5322
    goto _exit;
×
5323
  }
5324

5325
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
18,696✔
5326
    pAddr->taskId = pStatus->triggerTask->id.taskId;
14,875✔
5327
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
14,875✔
5328
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
14,875✔
5329
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
14,875✔
5330
    goto _exit;
14,875✔
5331
  }
5332

5333
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
3,821✔
5334
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,821✔
5335

5336
_exit:
21,790✔
5337
  
5338
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
21,790✔
5339

5340
  return code;
21,790✔
5341
}
5342

5343
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
409,341✔
5344
  int32_t code = TSDB_CODE_SUCCESS;
409,341✔
5345
  int32_t lino = 0;
409,341✔
5346
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
409,341✔
5347
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
409,341✔
5348
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
409,341✔
5349

5350
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
817,004✔
5351

5352
  mStreamMgmt.stat.activeTimes++;
409,341✔
5353
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
409,341✔
5354
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
409,341✔
5355
  if (NULL == mStreamMgmt.tCtx) {
409,341✔
5356
    code = terrno;
×
5357
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5358
    goto _exit;
×
5359
  }
5360

5361
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
409,341✔
5362
  if (mStreamMgmt.actionQ == NULL) {
409,341✔
5363
    code = terrno;
×
5364
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5365
    goto _exit;
×
5366
  }
5367
  
5368
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
409,341✔
5369
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
409,341✔
5370
  
5371
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
409,341✔
5372
  
5373
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,451,188✔
5374
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,041,847✔
5375

5376
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,251,082✔
5377
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,209,235✔
5378
      if (pCtx->deployStm[m] == NULL) {
10,209,235✔
5379
        code = terrno;
×
5380
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5381
        goto _exit;
×
5382
      }
5383
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
10,209,235✔
5384
      
5385
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,209,235✔
5386
      if (pCtx->actionStm[m] == NULL) {
10,209,235✔
5387
        code = terrno;
×
5388
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5389
        goto _exit;
×
5390
      }
5391
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
10,209,235✔
5392
    }
5393
  }
5394
  
5395
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
409,341✔
5396
  if (mStreamMgmt.streamMap == NULL) {
409,341✔
5397
    code = terrno;
×
5398
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5399
    goto _exit;
×
5400
  }
5401
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
409,341✔
5402
  
5403
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
409,341✔
5404
  if (mStreamMgmt.taskMap == NULL) {
409,341✔
5405
    code = terrno;
×
5406
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5407
    goto _exit;
×
5408
  }
5409
  
5410
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
409,341✔
5411
  if (mStreamMgmt.vgroupMap == NULL) {
409,341✔
5412
    code = terrno;
×
5413
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5414
    goto _exit;
×
5415
  }
5416
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
409,341✔
5417

5418
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
409,341✔
5419
  if (mStreamMgmt.snodeMap == NULL) {
409,341✔
5420
    code = terrno;
×
5421
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5422
    goto _exit;
×
5423
  }
5424
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
409,341✔
5425
  
5426
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
409,341✔
5427
  if (mStreamMgmt.dnodeMap == NULL) {
409,341✔
5428
    code = terrno;
×
5429
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5430
    goto _exit;
×
5431
  }
5432

5433
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
409,341✔
5434
  if (mStreamMgmt.toDeployVgMap == NULL) {
409,341✔
5435
    code = terrno;
×
5436
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5437
    goto _exit;
×
5438
  }
5439
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
409,341✔
5440
  
5441
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
409,341✔
5442
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
409,341✔
5443
    code = terrno;
×
5444
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5445
    goto _exit;
×
5446
  }
5447
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
409,341✔
5448

5449
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
409,341✔
5450
  if (mStreamMgmt.toUpdateScanMap == NULL) {
409,341✔
5451
    code = terrno;
×
5452
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5453
    goto _exit;
×
5454
  }
5455
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
409,341✔
5456

5457
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
409,341✔
5458
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
409,341✔
5459

5460
  mStreamMgmt.lastTaskId = 1;
409,341✔
5461

5462
  int32_t activeStreamNum = 0;
409,341✔
5463
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
409,341✔
5464

5465
  if (activeStreamNum > 0) {
409,341✔
5466
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
695✔
5467
  } else {
5468
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
408,646✔
5469
  }
5470

5471
_exit:
409,341✔
5472

5473
  if (code) {
409,341✔
5474
    msmDestroyRuntimeInfo(pMnode);
×
5475
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5476
  } else {
5477
    mstInfo("mnode stream runtime init done");
409,341✔
5478
  }
5479

5480
  return code;
409,341✔
5481
}
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