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

taosdata / TDengine / #5004

26 Mar 2026 12:51PM UTC coverage: 72.338% (+0.02%) from 72.317%
#5004

push

travis-ci

web-flow
merge: from main to 3.0 branch #34951

512 of 851 new or added lines in 47 files covered. (60.16%)

5499 existing lines in 143 files now uncovered.

253935 of 351039 relevant lines covered (72.34%)

130889634.0 hits per line

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

76.92
/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() {
838,092✔
34
  SStmQNode* pQNode = NULL;
838,092✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
838,092✔
37
    return;
419,046✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,092,675✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,556,050✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,463,375✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,463,375✔
51
  }
52
}
2,092,675✔
53

54
void msmDestroyThreadCtxs() {
838,092✔
55
  if (NULL == mStreamMgmt.tCtx) {
838,092✔
56
    return;
419,046✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,511,721✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,092,675✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
419,046✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
838,092✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
838,092✔
94
}
838,092✔
95

96

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

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

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

111
    pStatus = pStream;
437✔
112
  }
113

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

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

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

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

136
_exit:
×
137

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

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

145

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

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

200
  pSnode = NULL;
448,856✔
201

202
_exit:
448,856✔
203

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

208
  return code;
448,856✔
209
}
210

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

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

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

235
  pDnode = NULL;
928,606✔
236

237
_exit:
928,606✔
238

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

243
  return code;
928,606✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
676,558✔
275
  int32_t code = TSDB_CODE_SUCCESS;
676,558✔
276
  int32_t lino = 0;
676,558✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
676,558✔
278
  if (NULL == pStream) {
676,558✔
279
    SStmVgStreamStatus stream = {0};
482,271✔
280
    if (trigReader) {
482,271✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
279,114✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
279,114✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
558,228✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
203,157✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
203,157✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
406,314✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
482,271✔
290
    goto _exit;
482,271✔
291
  }
292
  
293
  if (trigReader) {
194,287✔
294
    if (NULL == pStream->trigReaders) {
23,417✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
23,417✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
23,417✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
46,834✔
300
    goto _exit;
23,417✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
170,870✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
111,725✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
111,725✔
306
  }
307

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

310
_exit:
170,870✔
311

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

320
  return code;
676,558✔
321
}
322

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

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

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

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

350
  return code;
676,558✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
677,662✔
363
    if (NULL == pVg) {
677,662✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
198,058✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
198,058✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
396,116✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
198,058✔
368
      if (TSDB_CODE_SUCCESS == code) {
198,058✔
369
        goto _return;
198,058✔
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);
479,604✔
381
    if (NULL == pVg->taskList) {
479,604✔
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)) {
959,208✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
479,604✔
390
    
391
    taosHashRelease(pVgMap, pVg);
479,604✔
392
    break;
479,604✔
393
  }
394
  
395
_return:
677,662✔
396

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

404
  return code;
677,662✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
891,218✔
409
  int32_t code = TSDB_CODE_SUCCESS;
891,218✔
410
  int32_t lino = 0;
891,218✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
891,218✔
412
  if (NULL == pStream) {
891,218✔
413
    SStmSnodeStreamStatus stream = {0};
206,898✔
414
    if (deployId < 0) {
206,898✔
415
      stream.trigger = pStatus;
3,291✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
203,607✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
203,607✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
407,214✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
206,898✔
423
    goto _exit;
206,898✔
424
  }
425
  
426
  if (deployId < 0) {
684,320✔
427
    if (NULL != pStream->trigger) {
195,429✔
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;
195,429✔
433
    goto _exit;
195,429✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
488,891✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
384,512✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
384,512✔
439
  }
440

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

443
_exit:
488,891✔
444

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

453
  return code;
891,218✔
454
}
455

456

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

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

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

484
  return code;
891,218✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
198,720✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
59,658✔
568
      ext.deployed = false;
59,658✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
119,316✔
570

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

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

614
  return code;
692,498✔
615
}
616

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

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

632
_exit:
629,789✔
633

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

638
  return code;
629,789✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
16,026✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
184✔
682

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

687
  return code;  
184✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
95,773✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,004,633✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
62,971,413✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
46,966,780✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
46,966,780✔
725
  }
726

727
_exit:
16,004,633✔
728

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

733
  return code;
16,004,633✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
366,936✔
739
  int32_t num = taosArrayGetSize(pList);
366,936✔
740
  for (int32_t i = 0; i < num; ++i) {
1,148,283✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
955,982✔
742
    if (pScan->readFromCache) {
955,982✔
743
      return pScan->scanPlan;
174,635✔
744
    }
745
  }
746

747
  return NULL;
192,301✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
677,662✔
775
}
776

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

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

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

805
_exit:
195,429✔
806

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

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

859
  pMsg->maxDelay = pStream->pCreate->maxDelay;
198,720✔
860
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
198,720✔
861
  pMsg->watermark = pStream->pCreate->watermark;
198,720✔
862
  pMsg->expiredTime = pStream->pCreate->expiredTime;
198,720✔
863
  pMsg->idleTimeoutMs = pStream->pCreate->idleTimeoutMs;
198,720✔
864
  pMsg->trigger = pInfo->pCreate->trigger;
198,720✔
865

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

878
  SStreamTaskAddr addr;
198,289✔
879
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
198,720✔
880
  if (triggerReaderNum > 0) {
198,720✔
881
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
194,549✔
882
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
194,549✔
883
  }
884
  
885
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
439,958✔
886
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
241,238✔
887
    addr.taskId = pStatus->id.taskId;
241,238✔
888
    addr.nodeId = pStatus->id.nodeId;
241,238✔
889
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
241,238✔
890
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
482,476✔
891
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
241,238✔
892
  }
893

894
  pMsg->leaderSnodeId = pStream->mainSnodeId;
198,720✔
895
  pMsg->streamName = pInfo->streamName;
198,720✔
896

897
  if (0 == pInfo->runnerNum) {
198,720✔
898
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
3,291✔
899
    return code;
3,291✔
900
  }
901

902
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
195,429✔
903

904
_exit:
195,429✔
905

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

915

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

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

949
  pMsg->colCids = pInfo->pCreate->colCids;
692,498✔
950
  pMsg->tagCids = pInfo->pCreate->tagCids;
692,498✔
951
_exit:
692,498✔
952

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

960

961
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
461,164✔
962
  int32_t code = TSDB_CODE_SUCCESS;
461,164✔
963
  int32_t lino = 0;
461,164✔
964
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
461,164✔
965
  SListNode* pNode = pList ? listHead(pList) : NULL;
461,164✔
966
  
967
  for (int32_t i = 0; i < taskNum; ++i) {
1,137,722✔
968
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
676,558✔
969
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
676,558✔
970
    if (pNode) {
676,558✔
971
      pNode = TD_DLIST_NODE_NEXT(pNode);
371,596✔
972
    }
973
  }
974
  
975
_exit:
461,164✔
976

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

984

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

998
  if (code) {
785,375✔
UNCOV
999
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1000
  }
1001

1002
  return code;
785,375✔
1003
}
1004

1005
int64_t msmAssignTaskId(void) {
1,567,408✔
1006
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,567,408✔
1007
}
1008

1009
int64_t msmAssignTaskSeriousId(void) {
1,567,408✔
1010
  return taosGetTimestampNs();
1,567,408✔
1011
}
1012

1013

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

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

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

1034
    *alive = (pStatus->runnerThreadNum >= 0);
1,056,750✔
1035
    break;
1,056,750✔
1036
  }
1037

1038
_exit:
1,056,750✔
1039

1040
  if (code) {
1,056,750✔
UNCOV
1041
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1042
  }
1043

1044
  return code;
1,056,750✔
1045
}
1046

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

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

1075
      snodeId = pSnode->replicaId;
506✔
1076
      mndReleaseSnode(pMnode, pSnode);
506✔
1077
      
1078
      continue;
506✔
1079
    }
1080

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

UNCOV
1085
_exit:
×
1086

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

UNCOV
1091
  return 0;
×
1092
}
1093

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

1108
  int32_t snodeTarget = taosRand() % snodeNum;
619,985✔
1109

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

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

1135
    snodeId = pObj->id;
647,096✔
1136
    if (snodeIdx == snodeTarget) {
647,096✔
1137
      sdbRelease(pMnode->pSdb, pObj);
619,985✔
1138
      sdbCancelFetch(pMnode->pSdb, pIter);
619,985✔
1139
      pObj = NULL;
619,985✔
1140
      goto _exit;
619,985✔
1141
    }
1142

1143
    sdbRelease(pMnode->pSdb, pObj);
27,111✔
1144
    snodeIdx++;
27,111✔
1145
  }
1146

1147
_exit:
622,665✔
1148

1149
  if (code) {
622,665✔
UNCOV
1150
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1151
  }
1152

1153
  if (0 == snodeId) {
622,665✔
1154
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,680✔
1155
  }
1156

1157
  return snodeId;
622,665✔
1158
}
1159

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

1169
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
828,509✔
1170

1171
_exit:
828,509✔
1172

1173
  if (0 == snodeId) {
828,509✔
UNCOV
1174
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1175
  }
1176

1177
  return snodeId;
828,509✔
1178
}
1179

1180

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

1186
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
198,720✔
1187
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
198,720✔
1188

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

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

1210
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
198,720✔
1211
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
198,720✔
1212

1213
_exit:
198,720✔
1214

1215
  if (code) {
198,720✔
UNCOV
1216
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1217
  }
1218

1219
  return code;
198,720✔
1220
}
1221

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

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

1248
_exit:
302,531✔
1249

1250
  if (code) {
302,531✔
UNCOV
1251
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1252
  }
1253

1254
  return code;
302,531✔
1255
}
1256

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

1286
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
79,736✔
1287
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
79,736✔
1288
      
1289
      void *pIter = NULL;
79,736✔
1290
      while (1) {
419,418✔
1291
        SStmTaskDeploy info = {0};
499,154✔
1292
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
499,154✔
1293
        if (pIter == NULL) {
499,154✔
1294
          break;
79,736✔
1295
        }
1296
      
1297
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
419,418✔
1298
          pState = taosArrayReserve(pInfo->trigReaders, 1);
126,425✔
1299

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

1309
        sdbRelease(pSdb, pVgroup);
419,418✔
1310
      }
1311
      break;
79,736✔
1312
    }
1313
    default:
4,171✔
1314
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
4,171✔
1315
      break;
4,171✔
1316
  }
1317

1318
_exit:
198,720✔
1319

1320
  mndReleaseDb(pCtx->pMnode, pDb);
198,720✔
1321

1322
  if (code) {
198,720✔
UNCOV
1323
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1324
  }
1325

1326
  return code;
198,720✔
1327
}
1328

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

1352
  key[1] = pSubplan->id.subplanId;
371,964✔
1353

1354
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
371,964✔
1355
  if (NULL == ppRes) {
371,964✔
1356
    SArray* pRes = taosArrayInit(1, sizeof(addr));
352,527✔
1357
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
352,527✔
1358
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
705,054✔
1359
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
352,527✔
1360
  } else {
1361
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
38,874✔
1362
  }
1363

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

1370
  nodesDestroyNode((SNode*)pSubplan);
371,964✔
1371

1372
  if (code) {
371,964✔
UNCOV
1373
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1374
  }
1375

1376
  return code;
371,964✔
1377
}
1378

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

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

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

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

1415
  return code;
80,535✔
1416
}
1417

1418

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

1423
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
374,027✔
1424

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

1447
_exit:
374,027✔
1448

1449
  if (code) {
374,027✔
UNCOV
1450
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1451
  }
1452

1453
  return code;
374,027✔
1454
}
1455

1456

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

1466
  
1467
  for (int32_t i = 0; i < calcTasksNum; ++i) {
650,851✔
1468
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
452,131✔
1469
    if (pScan->readFromCache) {
452,131✔
1470
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
80,535✔
1471
      continue;
80,535✔
1472
    }
1473
    
1474
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
371,596✔
1475
    for (int32_t m = 0; m < vgNum; ++m) {
743,192✔
1476
      pState = tdListReserve(pInfo->calcReaders);
371,596✔
1477
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
371,596✔
1478

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

1484
_exit:
198,720✔
1485

1486
  if (code) {
198,720✔
UNCOV
1487
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1488
  }
1489

1490
  return code;
198,720✔
1491
}
1492

1493

1494

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

1522
_exit:
368✔
1523

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

1528
  return code;
368✔
1529
}
1530

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

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

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

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

1565
  source.addr.epSet = pSrc->epset;
1,543,610✔
1566
  source.addr.nodeId = pSrc->vgId;
1,543,610✔
1567

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

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

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

UNCOV
1589
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1590
}
1591

1592
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
692,498✔
1593
  int32_t code = TSDB_CODE_SUCCESS;
692,498✔
1594
  int32_t lino = 0;
692,498✔
1595
  int64_t key[2] = {streamId, -1};
692,498✔
1596
  SNode* pNode = NULL;
692,498✔
1597
  SStreamTask* pTask = &pDeploy->task;
692,498✔
1598
  FOREACH(pNode, pPlan->pChildren) {
2,111,968✔
1599
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,419,470✔
1600
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
62,709✔
1601
      continue;
62,709✔
1602
    }
1603
    
1604
    SValueNode* pVal = (SValueNode*)pNode;
1,356,761✔
1605
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,356,761✔
UNCOV
1606
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1607
      continue;
×
1608
    }
1609

1610
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,356,761✔
1611

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

1618
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,356,761✔
1619
    for (int32_t i = 0; i < childrenNum; ++i) {
2,837,662✔
1620
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,480,901✔
1621
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,480,901✔
1622
    }
1623
  }
1624

1625
_exit:
692,498✔
1626

1627
  if (code) {
692,498✔
UNCOV
1628
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1629
  }
1630

1631
  return code;
692,498✔
1632
}
1633

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

1642
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
692,498✔
1643

1644
  SNode* pTmp = NULL;
692,498✔
1645
  WHERE_EACH(pTmp, pPlan->pChildren) {
2,111,968✔
1646
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,419,470✔
1647
      ERASE_NODE(pPlan->pChildren);
1,356,761✔
1648
      continue;
1,356,761✔
1649
    }
1650
    WHERE_NEXT;
62,709✔
1651
  }
1652
  nodesClearList(pPlan->pChildren);
692,498✔
1653
  pPlan->pChildren = NULL;
692,498✔
1654

1655
  if (NULL == pPlan->pParents) {
692,498✔
1656
    goto _exit;
629,789✔
1657
  }
1658

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

1674
  if (code) {
692,498✔
UNCOV
1675
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1676
  }
1677

1678
  return code;
692,498✔
1679
}
1680

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

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

1696
_exit:
629,789✔
1697

1698
  if (code) {
629,789✔
UNCOV
1699
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1700
  }
1701

1702
  return code;
629,789✔
1703
}
1704

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

1720
  if (dagIdx >= 0) {
209,807✔
1721
    subQ = true;
14,378✔
1722
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
14,378✔
1723
  } else {
1724
    pDag = *pRoot;
195,429✔
1725
  }
1726

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

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

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

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

1757
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
209,807✔
1758
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
209,807✔
1759
  
1760
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
839,228✔
1761
    totalTaskNum = 0;
629,421✔
1762

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

1769
    taskIdx = 0;
629,421✔
1770

1771
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,281,036✔
1772
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
651,615✔
1773
      if (NULL == plans) {
651,615✔
UNCOV
1774
        mstsError("empty level plan, level:%d", i);
×
1775
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1776
      }
1777

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

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

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

1795
      for (int32_t n = 0; n < taskNum; ++n) {
1,343,745✔
1796
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
692,130✔
1797
        pState = taosArrayReserve(deployList, 1);
692,130✔
1798

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

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

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

1825
        if (subQ) {
692,130✔
1826
          if (i == 0) {
60,810✔
1827
            SStmTaskSrcAddr addr = {0};
43,134✔
1828
            SDownstreamSourceNode* pSource = NULL;
43,134✔
1829

1830
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
43,134✔
1831

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

1837
            pSource->addr.epSet = addr.epset;
43,134✔
1838
            pSource->addr.nodeId = addr.vgId;
43,134✔
1839

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

1847
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
43,134✔
1848
          }
1849
        } else {
1850
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
631,320✔
1851
        }
1852
      }
1853

1854
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
651,615✔
1855
    }
1856

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

1862
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
629,421✔
1863

1864
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
629,421✔
1865

1866
    nodesDestroyNode((SNode *)(*pRoot));
629,421✔
1867
    *pRoot = NULL;
629,421✔
1868
    
1869
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
629,421✔
1870
    if (subQ) {
629,421✔
1871
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
43,134✔
1872
    } else {
1873
      pDag = *pRoot;
586,287✔
1874
    }
1875

1876
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
629,421✔
1877
  }
1878

1879
  if (!subQ) {
209,807✔
1880
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
781,716✔
1881
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
586,287✔
1882
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
586,287✔
1883
    }
1884
  }
1885
  
1886
  pInfo->runnerNum += totalTaskNum;
209,807✔
1887
  
1888
_exit:
209,807✔
1889

1890
  if (code) {
209,807✔
UNCOV
1891
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1892
  }
1893

1894
  taosArrayDestroy(deployTaskList);
209,807✔
1895

1896
  return code;
209,807✔
1897
}
1898

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

1916
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
736✔
1917
    deployId = pAction->deployId[r];
368✔
1918

1919
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
368✔
1920

1921
    pStartRunner = pRunner;
368✔
1922
    totalTaskNum = 0;
368✔
1923

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

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

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

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

1947
        pRunner->id.nodeId = newNodeId;
368✔
1948

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

1959
        pRunner++;
368✔
1960
      }
1961

1962
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
368✔
1963
    }
1964

1965
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
368✔
1966

1967
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
368✔
1968

1969
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
368✔
1970

1971
    nodesDestroyNode((SNode *)pDag);
368✔
1972
    pDag = NULL;
368✔
1973

1974
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
368✔
1975
  }
1976

1977
_exit:
368✔
1978

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

1983
  nodesDestroyNode((SNode *)pDag);
368✔
1984
  taosArrayDestroy(deployTaskList);
368✔
1985

1986
  return code;
368✔
1987
}
1988

1989

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

1998
_exit:
188,408✔
1999

2000
  if (code) {
188,408✔
UNCOV
2001
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2002
  }
2003

2004
  return code;
188,408✔
2005
}
2006

2007

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

2015
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
819,870✔
2016
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
624,441✔
2017
    (void)mstWaitLock(&pVg->lock, true);
624,441✔
2018

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

2025
    for (int32_t i = 0; i < taskNum; ++i) {
3,027,969✔
2026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
2,403,528✔
2027
      if (pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
2,403,528✔
2028
        continue;
1,819,995✔
2029
      }
2030

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

2041
    taosRUnLockLatch(&pVg->lock);
624,441✔
2042
  }
2043

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

2049
  return code;
195,429✔
2050
}
2051

2052
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,720✔
2053
  if (NULL == pStream->pCreate->calcPlan) {
198,720✔
2054
    return TSDB_CODE_SUCCESS;
3,291✔
2055
  }
2056

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

2065
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
195,429✔
2066

2067
  FOREACH(pNode, pPlan->pChildren) {
209,807✔
2068
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
14,378✔
2069

2070
    subQueryPlans += calcSubQPlan->numOfSubplans;
14,378✔
2071
  }
2072

2073
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
781,716✔
2074
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans + subQueryPlans, sizeof(SStmTaskStatus));
586,287✔
2075
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
586,287✔
2076
  }
2077

2078
  for (int32_t i = 0; i < LIST_LENGTH(pPlan->pChildren); ++i) {
209,807✔
2079
    code = msmBuildRunnerTasksImpl(pCtx, i, pInfo, pStream, &pPlan, &pSubEP);
14,378✔
2080
    TAOS_CHECK_EXIT(code);
14,378✔
2081
  }
2082

2083
  code = msmBuildRunnerTasksImpl(pCtx, -1, pInfo, pStream, &pPlan, &pSubEP);
195,429✔
2084
  TAOS_CHECK_EXIT(code);
195,429✔
2085

2086
  taosHashClear(mStreamMgmt.toUpdateScanMap);
195,429✔
2087
  mStreamMgmt.toUpdateScanNum = 0;
195,429✔
2088

2089
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
195,429✔
2090

2091
_exit:
195,429✔
2092
  nodesDestroyNode((SNode *)pPlan);
195,429✔
2093
  if (pSubEP) {
195,429✔
2094
    nodesDestroyList(pSubEP);
12,634✔
2095
  }
2096

2097
  if (code) {
195,429✔
UNCOV
2098
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2099
  }
2100

2101
  return code;
195,429✔
2102
}
2103

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

2109
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
198,720✔
2110

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

2117
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
198,720✔
2118
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
198,720✔
2119
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
198,720✔
2120
  
2121
_exit:
198,720✔
2122

2123
  if (code) {
198,720✔
UNCOV
2124
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2125
  }
2126

2127
  return code;
198,720✔
2128
}
2129

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

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

UNCOV
2169
_exit:
×
2170

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

UNCOV
2176
  return code;
×
2177
}
2178

2179

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

2185
  pStatus->lastActionTs = INT64_MIN;
191,699✔
2186

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

2192
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
191,699✔
2193
  
2194
  if (pStream->pCreate->numOfCalcSubplan > 0) {
191,699✔
2195
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
188,408✔
2196
    
2197
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
188,408✔
2198
  }
2199

2200
  if (initList) {
191,699✔
UNCOV
2201
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2202

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

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

2220
  if (code) {
191,699✔
UNCOV
2221
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2222
  }
2223

2224
  return code;
191,699✔
2225
}
2226

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

2233
  if (NULL == pStatus) {
198,720✔
2234
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
191,699✔
2235

2236
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
191,699✔
2237

2238
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
191,699✔
2239
  }
2240
  
2241
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
198,720✔
2242

2243
  mstLogSStmStatus("stream deployed", streamId, pStatus);
198,720✔
2244

2245
_exit:
198,720✔
2246

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

2256
  return code;
198,720✔
2257
}
2258

2259

2260
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
9,404✔
2261
  int32_t code = TSDB_CODE_SUCCESS;
9,404✔
2262
  void* pIter = NULL;
9,404✔
2263

2264
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
15,761✔
2265
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
6,357✔
2266
    (void)mstWaitLock(&pVg->lock, true);
6,357✔
2267

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

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

UNCOV
2280
      mstDestroySStmTaskToDeployExt(pExt);
×
2281
      pExt->deployed = true;
×
2282
    }
2283
    
2284
    taosRUnLockLatch(&pVg->lock);
6,357✔
2285
  }
2286

2287
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
17,342✔
2288
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
7,938✔
2289
    (void)mstWaitLock(&pSnode->lock, true);
7,938✔
2290

2291
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
7,938✔
2292
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
7,938✔
2293
      for (int32_t i = 0; i < taskNum; ++i) {
18,784✔
2294
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
12,134✔
2295
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
12,134✔
2296
          continue;
11,881✔
2297
        }
2298
        
2299
        mstDestroySStmTaskToDeployExt(pExt);
253✔
2300
        pExt->deployed = true;
253✔
2301
      }
2302
    }
2303

2304
    taskNum = taosArrayGetSize(pSnode->runnerList);
7,938✔
2305
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
7,938✔
2306
      for (int32_t i = 0; i < taskNum; ++i) {
42,569✔
2307
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
34,631✔
2308
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
34,631✔
2309
          continue;
34,125✔
2310
        }
2311
        
2312
        mstDestroySStmTaskToDeployExt(pExt);
506✔
2313
        pExt->deployed = true;
506✔
2314
      }
2315
    }
2316

2317
    taosRUnLockLatch(&pSnode->lock);
7,938✔
2318
  }
2319

2320
  
2321
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
33,735✔
2322
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
24,331✔
2323
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
24,331✔
2324
    if (TSDB_CODE_SUCCESS == code) {
24,331✔
2325
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
12,049✔
2326
    }
2327
  }
2328

2329
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
37,496✔
2330
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
28,092✔
2331
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
28,092✔
2332
    if (TSDB_CODE_SUCCESS == code) {
28,092✔
2333
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
13,964✔
2334
    }
2335
  }
2336

2337
  size_t keyLen = 0;
9,404✔
2338
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
590,908✔
2339
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
581,504✔
2340
    if (*pStreamId == streamId) {
581,504✔
2341
      int64_t taskId = *(pStreamId + 1);
58,591✔
2342
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
58,591✔
2343
      if (code) {
58,591✔
UNCOV
2344
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2345
      } else {
2346
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
58,591✔
2347
      }
2348
    }
2349
  }
2350

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

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

2368
  mstResetSStmStatus(pStatus);
7,205✔
2369

2370
  pStatus->deployTimes++;
7,205✔
2371
}
7,205✔
2372

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

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

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

2406
  TAOS_CHECK_EXIT(code);
198,904✔
2407

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

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

2424
_exit:
198,904✔
2425

2426
  mndReleaseStream(pCtx->pMnode, pStream);
198,904✔
2427

2428
  if (code) {
198,904✔
UNCOV
2429
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2430
  }
2431

2432
  return code;
198,904✔
2433
}
2434

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

2467
_exit:
1,104✔
2468

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

2473
  return code;
1,104✔
2474
}
2475

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

2507
_exit:
2508

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

2513
  return code;
2514
}
2515
*/
2516

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

2566
_exit:
368✔
2567

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

2572
  return code;
368✔
2573
}
2574

2575

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

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

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

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

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

2603
  TAOS_CHECK_EXIT(code);
1,472✔
2604

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

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

2635
_exit:
1,472✔
2636

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

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

2646
  return code;
1,472✔
2647
}
2648

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

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

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

UNCOV
2694
  return TSDB_CODE_SUCCESS;
×
2695
}
2696

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

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

2703
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
2,199✔
2704

2705
_exit:
2,199✔
2706

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

2713
  return code;
2,199✔
2714
}
2715

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

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

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

2732
  atomic_store_8(&pStream->stopped, 2);
34,886✔
2733

2734
  mstsInfo("set stream %s stopped by user", streamName);
34,886✔
2735

UNCOV
2736
_exit:
×
2737

2738
  taosHashRelease(mStreamMgmt.streamMap, pStream);
35,975✔
2739

2740
  if (code) {
35,975✔
UNCOV
2741
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2742
  }
2743

2744
  return;
35,975✔
2745
}
2746

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

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

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

2764
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,412✔
2765

2766
_exit:
3,412✔
2767

2768
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,412✔
2769

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

2774
  return code;
3,412✔
2775
}
2776

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

2782
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
253,533✔
2783
    switch (pQNode->type) {
201,453✔
2784
      case STREAM_ACT_DEPLOY:
201,453✔
2785
        if (pQNode->streamAct) {
201,453✔
2786
          mstDebug("start to handle stream deploy action");
199,981✔
2787
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
199,981✔
2788
        } else {
2789
          mstDebug("start to handle task deploy action");
1,472✔
2790
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,472✔
2791
        }
2792
        break;
201,453✔
UNCOV
2793
      default:
×
2794
        break;
×
2795
    }
2796
  }
2797

2798
_exit:
52,080✔
2799

2800
  if (code) {
52,080✔
UNCOV
2801
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2802
  }
2803
}
52,080✔
2804

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

UNCOV
2816
    pStatus = (SStmStatus*)pIter;
×
2817

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

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

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

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

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

UNCOV
2835
  msmStopAllStreamsByGrant(errCode);
×
2836

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

2842
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,568,121✔
2843
  int32_t code = TSDB_CODE_SUCCESS;
1,568,121✔
2844
  int32_t lino = 0;
1,568,121✔
2845
  int64_t streamId = pDeploy->task.streamId;
1,568,121✔
2846
  
2847
  switch (pDeploy->task.type) {
1,568,121✔
2848
    case STREAM_READER_TASK:
677,662✔
2849
      if (NULL == pStream->readerTasks) {
677,662✔
2850
        pStream->streamId = streamId;
257,779✔
2851
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
257,779✔
2852
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
257,779✔
2853
      }
2854
      
2855
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,355,324✔
2856
      break;
677,662✔
2857
    case STREAM_TRIGGER_TASK:
198,467✔
2858
      pStream->streamId = streamId;
198,467✔
2859
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
198,467✔
2860
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
198,467✔
2861
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
198,467✔
2862
      break;
198,467✔
2863
    case STREAM_RUNNER_TASK:
691,992✔
2864
      if (NULL == pStream->runnerTasks) {
691,992✔
2865
        pStream->streamId = streamId;
205,252✔
2866
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
205,252✔
2867
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
205,252✔
2868
      }      
2869
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,383,984✔
2870
      break;
691,992✔
UNCOV
2871
    default:
×
2872
      break;
×
2873
  }
2874

2875
_exit:
1,568,121✔
2876

2877
  if (code) {
1,568,121✔
UNCOV
2878
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2879
  }
2880

2881
  return code;
1,568,121✔
2882
}
2883

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

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

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

2909
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,292,694✔
2910
    
2911
    break;
1,292,694✔
2912
  }
2913
  
2914
_exit:
1,568,121✔
2915

2916
  taosHashRelease(pHash, pStream);
1,568,121✔
2917

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

2924
  return code;
1,568,121✔
2925
}
2926

2927

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

2933
  for (int32_t i = 0; i < taskNum; ++i) {
1,895,083✔
2934
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,577,301✔
2935
    if (pExt->deployed) {
1,577,301✔
2936
      continue;
9,180✔
2937
    }
2938

2939
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,568,121✔
2940
    pExt->deployed = true;
1,568,121✔
2941

2942
    (void)atomic_add_fetch_32(deployed, 1);
1,568,121✔
2943
  }
2944

2945
_exit:
317,782✔
2946

2947
  if (code) {
317,782✔
UNCOV
2948
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2949
  }
2950

2951
  return code;
317,782✔
2952
}
2953

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

2961
  mstDebug("start to add stream vgroup tasks deploy");
87,329✔
2962
  
2963
  for (int32_t i = 0; i < vgNum; ++i) {
542,951✔
2964
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
455,622✔
2965

2966
    msmUpdateVgroupUpTs(pCtx, *vgId);
455,622✔
2967

2968
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
455,622✔
2969
    if (NULL == pVg) {
455,622✔
2970
      continue;
256,034✔
2971
    }
2972

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

2986
_exit:
87,329✔
2987

2988
  if (code) {
87,329✔
UNCOV
2989
    if (pVg) {
×
2990
      taosRUnLockLatch(&pVg->lock);
×
2991
    }
2992

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

2996
  return code;
87,329✔
2997
}
2998

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

3005
  mstDebug("start to add stream snode tasks deploy");
89,567✔
3006
  
3007
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
89,567✔
3008
  if (NULL == pSnode) {
89,567✔
3009
    return TSDB_CODE_SUCCESS;
28,401✔
3010
  }
3011

3012
  (void)mstWaitLock(&pSnode->lock, false);
61,166✔
3013
  
3014
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
61,166✔
3015
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
57,028✔
3016
  }
3017

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

3024
_exit:
61,166✔
3025

3026
  if (code) {
61,166✔
UNCOV
3027
    if (pSnode) {
×
3028
      taosWUnLockLatch(&pSnode->lock);
×
3029
    }
3030

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

3034
  return code;
61,166✔
3035
}
3036

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

3048
_exit:
716,662✔
3049

3050
  if (code) {
716,662✔
UNCOV
3051
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3052
  }
3053

3054
  return code;
716,662✔
3055
}
3056

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

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

3068
  while (1) {
275,427✔
3069
    pIter = taosHashIterate(pCtx->deployStm, pIter);
357,992✔
3070
    if (pIter == NULL) {
357,992✔
3071
      break;
82,565✔
3072
    }
3073
    
3074
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
275,427✔
3075
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
550,854✔
3076

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

3081
    mstClearSStmStreamDeploy(pDeploy);
275,427✔
3082
    
3083
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
275,427✔
3084
  }
3085
  
3086
_exit:
82,565✔
3087

3088
  if (pIter) {
82,565✔
UNCOV
3089
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3090
  }
3091

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

3099
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
87,329✔
3100
  int32_t code = TSDB_CODE_SUCCESS;
87,329✔
3101
  int32_t lino = 0;
87,329✔
3102
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
87,329✔
3103
  SStmVgTasksToDeploy* pVg = NULL;
87,329✔
3104
  
3105
  for (int32_t i = 0; i < vgNum; ++i) {
542,951✔
3106
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
455,622✔
3107
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
455,622✔
3108
    if (NULL == pVg) {
455,622✔
3109
      continue;
256,034✔
3110
    }
3111

3112
    if (taosWTryLockLatch(&pVg->lock)) {
199,588✔
3113
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
1,530✔
3114
      continue;
1,530✔
3115
    }
3116
    
3117
    if (atomic_load_32(&pVg->deployed) <= 0) {
198,058✔
UNCOV
3118
      taosWUnLockLatch(&pVg->lock);
×
UNCOV
3119
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
UNCOV
3120
      continue;
×
3121
    }
3122

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

UNCOV
3134
    for (int32_t m = taskNum - 1; m >= 0; --m) {
×
UNCOV
3135
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
×
UNCOV
3136
      if (!pExt->deployed) {
×
UNCOV
3137
        continue;
×
3138
      }
3139

UNCOV
3140
      mstDestroySStmTaskToDeployExt(pExt);
×
3141

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

3150
_exit:
87,329✔
3151

3152
  if (code) {
87,329✔
UNCOV
3153
    if (pVg) {
×
3154
      taosWUnLockLatch(&pVg->lock);
×
3155
    }
3156

UNCOV
3157
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3158
  }
3159
}
87,329✔
3160

3161
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
102,678✔
3162
  if (!GOT_SNODE(snodeId)) {
102,678✔
3163
    return;
13,111✔
3164
  }
3165
  
3166
  int32_t code = TSDB_CODE_SUCCESS;
89,567✔
3167
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
89,567✔
3168
  if (NULL == pSnode) {
89,567✔
3169
    return;
28,401✔
3170
  }
3171

3172
  if (taosWTryLockLatch(&pSnode->lock)) {
61,166✔
UNCOV
3173
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3174
    return;
×
3175
  }
3176

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

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

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

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

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

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

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

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

3239
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,330,231✔
3240
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,330,231✔
3241
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
87,329✔
3242
  }
3243

3244
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,330,231✔
3245
    msmCleanDeployedSnodeTasks(pHb->snodeId);
102,678✔
3246
  }
3247
}
16,330,231✔
3248

3249
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
16,330,231✔
3250
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,330,231✔
3251
  if (mStreamMgmt.tCtx) {
16,330,231✔
3252
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
16,256,787✔
3253
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
16,256,787✔
3254
  }
3255
}
16,330,231✔
3256

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

3274
_exit:
193,528✔
3275

3276
  if (code) {
193,528✔
UNCOV
3277
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3278
  }
3279

3280
  return code;
193,528✔
3281
}
3282

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

UNCOV
3299
_exit:
×
3300

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

UNCOV
3305
  return code;
×
3306
}
3307

3308

3309

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

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

3327
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
397,338✔
3328
    if (pAction->undeploy.doCheckpoint) {
198,669✔
3329
      pAction->undeploy.doCheckpoint = dropped ? false : true;
30,686✔
3330
    }
3331
    if (!pAction->undeploy.doCleanup) {
198,669✔
3332
      pAction->undeploy.doCleanup = dropped ? true : false;
30,686✔
3333
    }
3334
    
3335
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
198,669✔
3336
  } else {
3337
    SStmAction newAction = {0};
49,038✔
3338
    newAction.actions = action;
49,038✔
3339
    newAction.undeploy.doCheckpoint = dropped ? false : true;
49,038✔
3340
    newAction.undeploy.doCleanup = dropped ? true : false;
49,038✔
3341
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
49,038✔
3342
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
49,038✔
3343
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
98,076✔
3344
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
49,038✔
3345
    
3346
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
49,038✔
3347
  }
3348

3349
_exit:
247,707✔
3350

3351
  if (code) {
247,707✔
UNCOV
3352
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3353
  }
3354

3355
  return code;
247,707✔
3356
}
3357

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

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

3379
_exit:
3,412✔
3380

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

3386
  return code;
3,412✔
3387
}
3388

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

3395
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
263,376✔
UNCOV
3396
    return false;
×
3397
  }
3398

3399
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
263,376✔
3400
  for (int32_t i = 0; i < readerNum; ++i) {
581,226✔
3401
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
318,402✔
3402
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
318,402✔
3403
      return false;
552✔
3404
    }
3405
  }
3406

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

3415
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
262,824✔
3416
  SListNode* pNode = listHead(pStream->calcReaders);
262,824✔
3417
  for (int32_t i = 0; i < readerNum; ++i) {
787,291✔
3418
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
524,467✔
3419
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
524,467✔
UNCOV
3420
      return false;
×
3421
    }
3422
    pNode = TD_DLIST_NODE_NEXT(pNode);
524,467✔
3423
  }
3424

3425
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
837,128✔
3426
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
643,600✔
3427
    for (int32_t m = 0; m < runnerNum; ++m) {
1,321,407✔
3428
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
747,103✔
3429
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
747,103✔
3430
        return false;
69,296✔
3431
      }
3432
    }
3433
  }
3434
  
3435
  return true;
193,528✔
3436
}
3437

3438

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

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

3456
  stopped = atomic_load_8(&pStatus->stopped);
11,455,142✔
3457
  if (stopped) {
11,455,142✔
UNCOV
3458
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3459
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3460
    return;
×
3461
  }
3462
  
3463
  switch (pMsg->status) {
11,455,142✔
3464
    case STREAM_STATUS_INIT:      
11,448,644✔
3465
      if (STREAM_TRIGGER_TASK != pMsg->type) {
11,448,644✔
3466
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
11,057,764✔
3467
        return;
11,057,764✔
3468
      }
3469
      
3470
      if (INT64_MIN == pStatus->lastActionTs) {
390,880✔
UNCOV
3471
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3472
        return;
×
3473
      }
3474
      
3475
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
390,880✔
3476
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
127,504✔
3477
        return;
127,504✔
3478
      }
3479

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

3502
_exit:
269,874✔
3503

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

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

3516
  msttInfo("start to handle task error, type: %d", err);
246,942✔
3517

3518
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
246,942✔
3519

3520
_exit:
246,942✔
3521

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

3528
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
4,047,887✔
3529
  int32_t code = TSDB_CODE_SUCCESS;
4,047,887✔
3530
  int32_t lino = 0;
4,047,887✔
3531
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
4,047,887✔
3532

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

3551
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
4,047,887✔
3552
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,032,844✔
3553
    if (NULL == pStatus->detailStatus) {
2,032,844✔
3554
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
193,291✔
3555
      if (NULL == pStatus->detailStatus) {
193,291✔
UNCOV
3556
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3557
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3558
      }
3559
    }
3560
    
3561
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,032,844✔
3562
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,032,844✔
3563
  }
3564

3565
_exit:
2,015,043✔
3566

3567
  if (code) {
4,047,887✔
3568
    // IGNORE STOP STREAM BY ERROR
UNCOV
3569
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3570
  }
3571
}
4,047,887✔
3572

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

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

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

3591
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
33,113,200✔
3592
    int8_t stopped = atomic_load_8(&pStream->stopped);
33,113,200✔
3593
    if (stopped) {
33,113,200✔
3594
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
244,414✔
3595
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
244,414✔
3596
      continue;
244,414✔
3597
    }
3598

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

3607
    if ((*ppStatus)->status != pTask->status) {
32,868,786✔
3608
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,588,749✔
3609
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,106,307✔
3610
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,482,442✔
UNCOV
3611
        if (pStream->triggerTask) {
×
3612
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3613
        }
3614
        
UNCOV
3615
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3616
      }
3617
    }
3618
    
3619
    (*ppStatus)->errCode = pTask->errorCode;
32,868,786✔
3620
    (*ppStatus)->status = pTask->status;
32,868,786✔
3621
    (*ppStatus)->lastUpTs = pCtx->currTs;
32,868,786✔
3622
    
3623
    if (STREAM_STATUS_RUNNING != pTask->status) {
32,868,786✔
3624
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
11,455,142✔
3625
    }
3626
    
3627
    if (STREAM_TRIGGER_TASK == pTask->type) {
32,868,786✔
3628
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
4,047,887✔
3629
    }
3630
  }
3631

3632
_exit:
1,149,868✔
3633

3634
  if (code) {
1,149,868✔
UNCOV
3635
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3636
  }
3637

3638
  return code;
1,149,868✔
3639
}
3640

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

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

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

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

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

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

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

UNCOV
3741
_exit:
×
3742

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

UNCOV
3749
  return code;
×
3750
}
3751

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

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

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

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

UNCOV
3780
_exit:
×
3781

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

UNCOV
3786
  return code;
×
3787
}
3788

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

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

3806
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
387,056✔
3807
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
193,528✔
3808

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

3811
  return;
193,528✔
3812

UNCOV
3813
_exit:
×
3814

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

3818

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

3828
  SStreamTaskUndeploy undeploy;
49,038✔
3829
  for (int32_t i = 0; i < dropNum; ++i) {
296,745✔
3830
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
247,707✔
3831
    undeploy.task = *pTask;
247,707✔
3832
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
247,707✔
3833
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
247,707✔
3834

3835
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
495,414✔
3836
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
247,707✔
3837

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

3841
  return;
49,038✔
3842

UNCOV
3843
_exit:
×
3844

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

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

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

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

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

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

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

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

UNCOV
3878
_exit:
×
3879

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

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

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

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

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

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

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

3916
_exit:
3,412✔
3917

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

3925

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

3932
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
135,508✔
3933

3934
  while (1) {
245,978✔
3935
    pIter = taosHashIterate(pCtx->actionStm, pIter);
381,486✔
3936
    if (pIter == NULL) {
381,486✔
3937
      break;
135,508✔
3938
    }
3939

3940
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
245,978✔
3941
    SStmAction *pAction = (SStmAction *)pIter;
245,978✔
3942
    
3943
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
245,978✔
3944
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
49,038✔
3945
      continue;
49,038✔
3946
    }
3947

3948
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
196,940✔
UNCOV
3949
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3950
    }
3951

3952
    if (STREAM_ACT_RECALC & pAction->actions) {
196,940✔
3953
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,412✔
3954
    }
3955

3956
    if (STREAM_ACT_START & pAction->actions) {
196,940✔
3957
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
193,528✔
3958
    }
3959
  }
3960
  
3961
_exit:
135,508✔
3962

3963
  if (pIter) {
135,508✔
UNCOV
3964
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3965
  }
3966

3967
  if (code) {
135,508✔
UNCOV
3968
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3969
  }
3970

3971
  return code;
135,508✔
3972
}
3973

3974
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
16,256,787✔
3975
  int32_t  code = TSDB_CODE_SUCCESS;
16,256,787✔
3976
  int32_t  lino = 0;
16,256,787✔
3977
  int64_t* lastTs = NULL;
16,256,787✔
3978
  bool     noExists = false;
16,256,787✔
3979

3980
  while (true) {
3981
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
16,766,347✔
3982
    if (NULL == lastTs) {
16,766,347✔
3983
      if (noExists) {
657,897✔
3984
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
148,337✔
3985
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
148,337✔
3986
      }
3987

3988
      noExists = true;
509,560✔
3989
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
509,560✔
3990
      
3991
      continue;
509,560✔
3992
    }
3993

UNCOV
3994
    while (true) {
×
3995
      int64_t lastTsValue = atomic_load_64(lastTs);
16,108,450✔
3996
      if (pCtx->currTs > lastTsValue) {
16,108,450✔
3997
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
16,105,857✔
3998
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
16,105,857✔
3999
          return code;
16,105,857✔
4000
        }
4001

UNCOV
4002
        continue;
×
4003
      }
4004

4005
      return code;
2,593✔
4006
    }
4007

4008
    break;
4009
  }
4010

4011
_exit:
148,337✔
4012

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

4017
  return code;  
148,337✔
4018
}
4019

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

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

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

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

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

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

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

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

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

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

UNCOV
4094
  msmWatchCheckStreamMap(pCtx);
×
4095

4096
_exit:
323✔
4097

4098
  mStreamMgmt.lastTaskId += 100000;
323✔
4099

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

4102
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
323✔
4103

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

4108
  return code;
323✔
4109
}
4110

4111

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

4117
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
16,304✔
4118
  
4119
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
16,304✔
UNCOV
4120
    goto _exit;
×
4121
  }
4122

4123
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,304✔
4124
  if (GOT_SNODE(pReq->snodeId)) {
16,304✔
4125
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,614✔
4126
  }
4127

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

4133
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
16,304✔
4134
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
323✔
4135
  }
4136

4137
_exit:
16,304✔
4138

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

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

4147
  return code;
16,304✔
4148
}
4149

4150
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
4,806,008✔
4151
  int32_t listSize = taosArrayGetSize(pOReaders);
4,806,008✔
4152
  int32_t totalSize = 0;
4,806,008✔
4153
  
4154
  for (int32_t i = 0; i < listSize; ++i) {
5,355,914✔
4155
    SArray* pList = taosArrayGetP(pOReaders, i);
549,906✔
4156
    totalSize += taosArrayGetSize(pList);
549,906✔
4157
  }
4158

4159
  return totalSize;
4,806,008✔
4160
}
4161

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

4168
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
341,639✔
4169
}
4170

4171

4172
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
61,293✔
4173
  int32_t code = TSDB_CODE_SUCCESS;
61,293✔
4174
  int32_t lino = 0;
61,293✔
4175

4176
  if (NULL == pStatus->trigOReaders) {
61,293✔
4177
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
53,204✔
4178
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
53,204✔
4179
  }
4180

4181
  while (true) {
53,204✔
4182
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
114,497✔
4183

4184
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
114,497✔
4185
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
53,204✔
4186
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
53,204✔
4187

4188
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
106,408✔
4189
      continue;
53,204✔
4190
    }
4191

4192
    *ppRes = *ppOReaderList;
61,293✔
4193
    break;
61,293✔
4194
  }
4195

4196
_exit:
61,293✔
4197

4198
  if (code) {
61,293✔
UNCOV
4199
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4200
  }
4201

4202
  return code;
61,293✔
4203
}
4204

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

4211
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
71,888✔
4212
  for (int32_t i = 0; i < readerNum; ++i) {
149,510✔
4213
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
88,217✔
4214
    if (pReader->id.nodeId == vgId) {
88,217✔
4215
      readerExists = true;
10,595✔
4216
      break;
10,595✔
4217
    }
4218
  }
4219

4220
  if (!readerExists) {
71,888✔
4221
    SArray* pReaderList = NULL;
61,293✔
4222
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
61,293✔
4223
    
4224
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
61,293✔
4225
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
61,293✔
4226
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
61,293✔
4227
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
61,293✔
4228
  }
4229

4230
_exit:
10,595✔
4231

4232
  if (code) {
71,888✔
UNCOV
4233
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4234
  }
4235

4236
  return code;
71,888✔
4237
}
4238

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

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

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

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

4276
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
63,190✔
4277

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

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

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

4293
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
63,190✔
4294
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
63,190✔
4295
  
4296
  for (int32_t i = 0; i < tbNum; ++i) {
248,922✔
4297
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
185,732✔
4298
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
185,732✔
4299
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
371,464✔
4300
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
185,732✔
4301
  }
4302

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

4317
  SStreamTaskAddr addr;
63,190✔
4318
  for (int32_t i = 0; i < vgNum; ++i) {
124,483✔
4319
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
61,293✔
4320
    addr.taskId = pOTask->id.taskId;
61,293✔
4321
    addr.nodeId = pOTask->id.nodeId;
61,293✔
4322
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
61,293✔
4323
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
122,586✔
4324
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
61,293✔
4325
  }
4326

4327
_exit:
63,190✔
4328

4329
  tFreeSStreamMgmtReq(pMgmtReq);
63,190✔
4330
  taosMemoryFree(pMgmtReq);
63,190✔
4331

4332
  tSimpleHashCleanup(pVgs);
63,190✔
4333
  mstDestroyDbVgroupsHash(pDbVgroups);
63,190✔
4334

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

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

4345
_final:
63,190✔
4346

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

4353
  return finalCode;
63,190✔
4354
}
4355

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

4370
_exit:
2,431✔
4371

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

4376
  return code;
2,431✔
4377
}
4378

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

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

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

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

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

4422
_exit:
2,431✔
4423

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

4428
  return code;
2,431✔
4429
}
4430

4431

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

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

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

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

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

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

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

4492
_exit:
2,431✔
4493

4494
  tFreeSStreamMgmtReq(pMgmtReq);
2,431✔
4495
  taosMemoryFree(pMgmtReq);
2,431✔
4496

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

4506

4507
_final:
2,431✔
4508

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

4515
  return finalCode;
2,431✔
4516
}
4517

4518

4519
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
65,621✔
4520
  int32_t code = TSDB_CODE_SUCCESS;
65,621✔
4521
  int32_t lino = 0;
65,621✔
4522

4523
  switch (pTask->pMgmtReq->type) {
65,621✔
4524
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
63,190✔
4525
      msmDeployTriggerOrigReader(pCtx, pTask);
63,190✔
4526
      break;
63,190✔
4527
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,431✔
4528
      msmDeployRunnerOrigReader(pCtx, pTask);
2,431✔
4529
      break;
2,431✔
UNCOV
4530
    default:
×
4531
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4532
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4533
      break;
×
4534
  }
4535

4536
_exit:
65,621✔
4537

4538
  if (code) {
65,621✔
UNCOV
4539
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4540
  }
4541

4542
  return code;
65,621✔
4543
}
4544

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

4565
    if (NULL == pTask->pMgmtReq) {
65,621✔
UNCOV
4566
      msttError("idx %d without mgmtReq", idx);
×
4567
      continue;
×
4568
    }
4569

4570
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
65,621✔
4571
  }
4572

4573
_exit:
24,317✔
4574

4575
  if (code) {
24,317✔
UNCOV
4576
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4577
  }
4578

4579
  return code;
24,317✔
4580
}
4581

4582
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
16,240,483✔
4583
  int32_t code = TSDB_CODE_SUCCESS;
16,240,483✔
4584
  int32_t lino = 0;
16,240,483✔
4585
  SStreamHbMsg* pReq = pCtx->pReq;
16,240,483✔
4586

4587
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,240,483✔
4588
  if (GOT_SNODE(pReq->snodeId)) {
16,092,146✔
4589
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,303,860✔
4590
  }
4591
  
4592
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,091,962✔
4593
    msmHandleStreamActions(pCtx);
52,080✔
4594
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
52,080✔
4595
  }
4596

4597
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
16,091,962✔
4598
    code = msmHandleStreamRequests(pCtx);
24,317✔
4599
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
24,317✔
4600
    TAOS_CHECK_EXIT(code);
24,317✔
4601
  }
4602

4603
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,091,962✔
4604
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
87,329✔
4605
  } else {
4606
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,004,633✔
4607
  }
4608

4609
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,091,962✔
4610
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
89,567✔
4611
  }
4612

4613
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,091,962✔
4614
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
82,565✔
4615
  }
4616

4617
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,091,962✔
4618
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,149,868✔
4619
  }
4620

4621
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,091,962✔
4622
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
135,508✔
4623
  }
4624

4625
_exit:
16,091,962✔
4626

4627
  if (code) {
16,240,483✔
4628
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
148,521✔
4629
  }
4630

4631
  return code;
16,240,483✔
4632
}
4633

4634
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
16,330,231✔
4635
  int32_t lino = 0;
16,330,231✔
4636
  int32_t tlen = 0;
16,330,231✔
4637
  void   *buf = NULL;
16,330,231✔
4638

4639
  if (TSDB_CODE_SUCCESS != code) {
16,330,231✔
4640
    goto _exit;
148,521✔
4641
  }
4642

4643
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
16,181,710✔
4644
  if (code < 0) {
16,181,710✔
UNCOV
4645
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4646
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4647
  }
4648

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

4655
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
16,181,710✔
4656
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
16,181,710✔
4657

4658
  SEncoder encoder;
16,178,871✔
4659
  tEncoderInit(&encoder, abuf, tlen);
16,181,710✔
4660
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
16,181,710✔
UNCOV
4661
    rpcFreeCont(buf);
×
4662
    buf = NULL;
×
4663
    tEncoderClear(&encoder);
×
4664
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4665
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4666
  }
4667
  tEncoderClear(&encoder);
16,181,710✔
4668

4669
_exit:
16,330,231✔
4670

4671
  pMsg->code = code;
16,330,231✔
4672
  pMsg->info = *pRpcInfo;
16,330,231✔
4673
  if (TSDB_CODE_SUCCESS == code) {
16,330,231✔
4674
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
16,181,710✔
4675
    pMsg->pCont = buf;
16,181,710✔
4676
  }
4677
}
16,330,231✔
4678

4679

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

4685
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,330,231✔
4686

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

4692
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,256,787✔
4693
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
16,256,787✔
4694

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

4716
_exit:
16,330,231✔
4717

4718
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
16,330,231✔
4719

4720
  msmCleanStreamGrpCtx(pHb);
16,330,231✔
4721
  msmClearStreamToDeployMaps(pHb);
16,330,231✔
4722

4723
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
16,330,231✔
4724
  
4725
  tFreeSMStreamHbRspMsg(&rsp);
16,330,231✔
4726

4727
  return code;
16,330,231✔
4728
}
4729

4730
void msmHandleBecomeLeader(SMnode *pMnode) {
419,046✔
4731
  if (tsDisableStream) {
419,046✔
UNCOV
4732
    return;
×
4733
  }
4734

4735
  mstInfo("start to process mnode become leader");
419,046✔
4736

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

4745
  if (TSDB_CODE_SUCCESS == code) {
419,046✔
4746
    atomic_store_8(&mStreamMgmt.active, 1);
419,046✔
4747
  }
4748

4749
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
419,046✔
4750
}
4751

4752
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
511,040✔
4753
  if (tsDisableStream) {
511,040✔
UNCOV
4754
    return;
×
4755
  }
4756

4757
  mstInfo("start to process mnode become not leader");
511,040✔
4758

4759
  streamRemoveVnodeLeader(MNODE_HANDLE);
511,040✔
4760

4761
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
511,040✔
4762
    taosWLockLatch(&mStreamMgmt.runtimeLock);
419,046✔
4763
    msmDestroyRuntimeInfo(pMnode);
419,046✔
4764
    mStreamMgmt.stat.inactiveTimes++;
419,046✔
4765
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
419,046✔
4766
  }
4767
}
4768

4769

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

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

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

4793
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
9,524✔
4794
  }
4795

4796
  return true;
7,775✔
4797

UNCOV
4798
_exit:
×
4799

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

UNCOV
4804
  *(int32_t*)p3 = code;
×
4805

UNCOV
4806
  return false;
×
4807
}
4808

4809

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

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

4823
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4824

4825
_exit:
40,383✔
4826

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

4831
  return code;
40,383✔
4832
}
4833

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

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

4858
  if (pStatus) {
27,316✔
4859
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
26,942✔
UNCOV
4860
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4861
    }
4862

4863
    return true;
26,942✔
4864
  }
4865

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

4868
  return true;
374✔
4869
}
4870

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

4883
    pStatus = (SStmStatus*)pIter;
225,884✔
4884

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

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

4899
    if (MST_IS_ERROR_STOPPED(stopped)) {
223,685✔
4900
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
15,509✔
4901
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
10,412✔
4902
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4903
            
4904
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
10,412✔
4905
        continue;
10,412✔
4906
      }
4907

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

4912
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
208,176✔
UNCOV
4913
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4914
      continue;
×
4915
    }
4916
  }
4917
}
48,875✔
4918

4919
void msmCheckStreamsStatus(SMnode *pMnode) {
406,704✔
4920
  SStmCheckStatusCtx ctx = {0};
406,704✔
4921

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

4930
  if (MST_READY_FOR_MAP_LOOP()) {
406,704✔
4931
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
48,875✔
4932
    msmCheckLoopStreamMap(pMnode);
48,875✔
4933
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
48,875✔
4934
  }
4935
}
406,704✔
4936

4937
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,731,977✔
4938
  for (int32_t i = 0; i < taskNum; ++i) {
7,949,550✔
4939
    SStmTaskStatus* pTask = *(pList + i);
4,217,573✔
4940

4941
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
4,217,573✔
4942
      continue;
560,399✔
4943
    }
4944
    
4945
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,659,092✔
4946
      continue;
3,657,988✔
4947
    }
4948

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

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

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

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

4974
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
157,677✔
4975
  void* pIter = NULL;
157,677✔
4976
  SStmVgStreamStatus* pVg = NULL;
157,677✔
4977
  int64_t streamId = 0;
157,677✔
4978
  
4979
  while (true) {
1,398,776✔
4980
    pIter = taosHashIterate(pStreams, pIter);
1,556,453✔
4981
    if (NULL == pIter) {
1,556,453✔
4982
      break;
157,677✔
4983
    }
4984

4985
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,398,776✔
4986
    pVg = (SStmVgStreamStatus*)pIter;
1,398,776✔
4987

4988
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,398,776✔
4989
    if (taskNum > 0) {
1,398,776✔
4990
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
840,342✔
4991
    }
4992

4993
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,398,776✔
4994
    if (taskNum > 0) {
1,398,776✔
4995
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
945,400✔
4996
    }
4997
  }
4998
}
157,677✔
4999

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

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

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

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

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

5025

5026
void msmCheckVgroupStatus(SMnode *pMnode) {
406,704✔
5027
  void* pIter = NULL;
406,704✔
5028
  int32_t code = 0;
406,704✔
5029
  
5030
  while (true) {
1,476,870✔
5031
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,883,574✔
5032
    if (NULL == pIter) {
1,883,574✔
5033
      break;
406,704✔
5034
    }
5035

5036
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,476,870✔
5037
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,476,870✔
5038
      continue;
1,314,041✔
5039
    }
5040
    
5041
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
162,829✔
5042

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

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

5063
    msmCheckVgroupStreamStatus(pVg->streamTasks);
157,677✔
5064
  }
5065
}
406,704✔
5066

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

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

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

5095
  (void)msmSTAddSnodesToMap(pMnode);
3,818✔
5096

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

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

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

5139
  taosHashClear(pSnode->streamTasks);
3,818✔
5140
}
3,818✔
5141

5142

5143
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
49,288✔
5144
  void* pIter = NULL;
49,288✔
5145
  SStmSnodeStreamStatus* pSnode = NULL;
49,288✔
5146
  int64_t streamId = 0;
49,288✔
5147
  
5148
  while (true) {
5149
    pIter = taosHashIterate(pStreams, pIter);
565,898✔
5150
    if (NULL == pIter) {
565,898✔
5151
      break;
49,288✔
5152
    }
5153

5154
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
516,610✔
5155
    pSnode = (SStmSnodeStreamStatus*)pIter;
516,610✔
5156

5157
    if (NULL != pSnode->trigger) {
516,610✔
5158
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
498,287✔
5159
    }
5160

5161
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,066,440✔
5162
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,549,830✔
5163
      if (taskNum > 0) {
1,549,830✔
5164
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,447,948✔
5165
      }
5166
    }
5167
  }
5168
}
49,288✔
5169

5170

5171
void msmCheckSnodeStatus(SMnode *pMnode) {
406,704✔
5172
  void* pIter = NULL;
406,704✔
5173
  
5174
  while (true) {
542,199✔
5175
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
948,903✔
5176
    if (NULL == pIter) {
948,903✔
5177
      break;
406,704✔
5178
    }
5179

5180
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
542,199✔
5181
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
542,199✔
5182
      continue;
478,138✔
5183
    }
5184

5185
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
64,061✔
5186
    
5187
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
64,061✔
5188
    if (NULL == pSnode->streamTasks) {
64,061✔
5189
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
11,645✔
5190
      continue;
11,645✔
5191
    }
5192
    
5193
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
52,416✔
5194
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
3,128✔
5195
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5196
      
5197
      msmHandleSnodeLost(pMnode, pSnode);
3,128✔
5198
      continue;
3,128✔
5199
    }
5200
    
5201
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
49,288✔
5202

5203
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
49,288✔
5204
  }
5205
}
406,704✔
5206

5207

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

5211
  msmCheckVgroupStatus(pMnode);
406,704✔
5212
  msmCheckSnodeStatus(pMnode);
406,704✔
5213
}
406,704✔
5214

5215
void msmCheckSnodesState(SMnode *pMnode) {
406,704✔
5216
  if (!MST_READY_FOR_SNODE_LOOP()) {
406,704✔
5217
    return;
363,217✔
5218
  }
5219

5220
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
43,487✔
5221

5222
  void* pIter = NULL;
43,487✔
5223
  int32_t snodeId = 0;
43,487✔
5224
  while (true) {
49,885✔
5225
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
93,372✔
5226
    if (NULL == pIter) {
93,372✔
5227
      break;
43,487✔
5228
    }
5229

5230
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
49,885✔
5231
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
49,885✔
5232
      continue;
49,195✔
5233
    }
5234

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

5248
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
43,487✔
5249
}
5250

5251
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
10,213,676✔
5252
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
10,213,676✔
5253
  if (0 == active || MND_STM_STATE_NORMAL != state) {
10,213,676✔
5254
    mstTrace("ignore health check since active:%d state:%d", active, state);
3,546✔
5255
    return false;
3,546✔
5256
  }
5257

5258
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
10,210,130✔
5259
    mstTrace("ignore health check since no stream now");
9,396,722✔
5260
    return false;
9,396,722✔
5261
  }
5262

5263
  return true;
813,408✔
5264
}
5265

5266
void msmHealthCheck(SMnode *pMnode) {
9,806,972✔
5267
  if (!msmCheckNeedHealthCheck(pMnode)) {
9,806,972✔
5268
    return;
9,400,268✔
5269
  }
5270

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

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

5288
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
406,704✔
5289

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

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

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

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

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

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

5337
_exit:
20,992✔
5338
  
5339
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
20,992✔
5340

5341
  return code;
20,992✔
5342
}
5343

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

5351
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
837,177✔
5352

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

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

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

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

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

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

5458
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
419,046✔
5459
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
419,046✔
5460

5461
  mStreamMgmt.lastTaskId = 1;
419,046✔
5462

5463
  int32_t activeStreamNum = 0;
419,046✔
5464
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
419,046✔
5465

5466
  if (activeStreamNum > 0) {
419,046✔
5467
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
961✔
5468
  } else {
5469
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
418,085✔
5470
  }
5471

5472
_exit:
419,046✔
5473

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

5481
  return code;
419,046✔
5482
}
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