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

taosdata / TDengine / #4987

16 Mar 2026 12:26PM UTC coverage: 73.883% (+36.6%) from 37.305%
#4987

push

travis-ci

web-flow
feat: support secure delete option. (#34591)

209 of 391 new or added lines in 24 files covered. (53.45%)

3062 existing lines in 140 files now uncovered.

261133 of 353439 relevant lines covered (73.88%)

121262425.02 hits per line

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

73.14
/source/dnode/mnode/impl/src/mndStreamMgmt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "mndStream.h"
17
#include "mndDb.h"
18
#include "mndPrivilege.h"
19
#include "mndShow.h"
20
#include "mndStb.h"
21
#include "mndTrans.h"
22
#include "osMemory.h"
23
#include "parser.h"
24
#include "taoserror.h"
25
#include "tmisce.h"
26
#include "tname.h"
27
#include "mndDnode.h"
28
#include "mndVgroup.h"
29
#include "mndSnode.h"
30
#include "mndMnode.h"
31
#include "cmdnodes.h"
32

33
void msmDestroyActionQ() {
844,168✔
34
  SStmQNode* pQNode = NULL;
844,168✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
844,168✔
37
    return;
422,084✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,107,906✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,647,436✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,539,530✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,539,530✔
51
  }
52
}
2,107,906✔
53

54
void msmDestroyThreadCtxs() {
844,168✔
55
  if (NULL == mStreamMgmt.tCtx) {
844,168✔
56
    return;
422,084✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,529,990✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,107,906✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
422,084✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
844,168✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
844,168✔
94
}
844,168✔
95

96

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

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

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

111
    pStatus = pStream;
561✔
112
  }
113

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

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

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

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

136
_exit:
482✔
137

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

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

145

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

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

200
  pSnode = NULL;
448,466✔
201

202
_exit:
448,466✔
203

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

208
  return code;
448,466✔
209
}
210

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

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

235
  pDnode = NULL;
928,365✔
236

237
_exit:
928,365✔
238

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

243
  return code;
928,365✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
591,077✔
275
  int32_t code = TSDB_CODE_SUCCESS;
591,077✔
276
  int32_t lino = 0;
591,077✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
591,077✔
278
  if (NULL == pStream) {
591,077✔
279
    SStmVgStreamStatus stream = {0};
428,658✔
280
    if (trigReader) {
428,658✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
276,418✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
276,418✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
552,836✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
152,240✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
152,240✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
304,480✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
428,658✔
290
    goto _exit;
428,658✔
291
  }
292
  
293
  if (trigReader) {
162,419✔
294
    if (NULL == pStream->trigReaders) {
23,053✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
23,053✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
23,053✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
46,106✔
300
    goto _exit;
23,053✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
139,366✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
103,590✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
103,590✔
306
  }
307

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

310
_exit:
139,366✔
311

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

320
  return code;
591,077✔
321
}
322

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

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

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

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

350
  return code;
591,077✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
592,199✔
363
    if (NULL == pVg) {
592,199✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
129,267✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
129,267✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
258,534✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
129,267✔
368
      if (TSDB_CODE_SUCCESS == code) {
129,267✔
369
        goto _return;
129,267✔
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);
462,932✔
381
    if (NULL == pVg->taskList) {
462,932✔
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)) {
925,864✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
462,932✔
390
    
391
    taosHashRelease(pVgMap, pVg);
462,932✔
392
    break;
462,932✔
393
  }
394
  
395
_return:
592,199✔
396

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

404
  return code;
592,199✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
826,754✔
409
  int32_t code = TSDB_CODE_SUCCESS;
826,754✔
410
  int32_t lino = 0;
826,754✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
826,754✔
412
  if (NULL == pStream) {
826,754✔
413
    SStmSnodeStreamStatus stream = {0};
199,216✔
414
    if (deployId < 0) {
199,216✔
415
      stream.trigger = pStatus;
3,301✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
195,915✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
195,915✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
391,830✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
199,216✔
423
    goto _exit;
199,216✔
424
  }
425
  
426
  if (deployId < 0) {
627,538✔
427
    if (NULL != pStream->trigger) {
189,337✔
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;
189,337✔
433
    goto _exit;
189,337✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
438,201✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
372,096✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
372,096✔
439
  }
440

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

443
_exit:
438,201✔
444

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

453
  return code;
826,754✔
454
}
455

456

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

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

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

484
  return code;
826,754✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
192,638✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
50,577✔
568
      ext.deployed = false;
50,577✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
101,154✔
570

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

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

614
  return code;
634,116✔
615
}
616

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

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

632
_exit:
580,746✔
633

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

638
  return code;
580,746✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
16,335✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
×
682

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

687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
79,258✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,666,540✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
66,906,570✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
50,240,030✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
50,240,030✔
725
  }
726

727
_exit:
16,666,540✔
728

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

733
  return code;
16,666,540✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
363,422✔
739
  int32_t num = taosArrayGetSize(pList);
363,422✔
740
  for (int32_t i = 0; i < num; ++i) {
1,046,573✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
861,615✔
742
    if (pScan->readFromCache) {
861,615✔
743
      return pScan->scanPlan;
178,464✔
744
    }
745
  }
746

747
  return NULL;
184,958✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
592,199✔
775
}
776

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

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

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

805
_exit:
189,337✔
806

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

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

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

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

877
  SStreamTaskAddr addr;
192,144✔
878
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
192,638✔
879
  if (triggerReaderNum > 0) {
192,638✔
880
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
191,630✔
881
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
191,630✔
882
  }
883
  
884
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
431,450✔
885
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
238,812✔
886
    addr.taskId = pStatus->id.taskId;
238,812✔
887
    addr.nodeId = pStatus->id.nodeId;
238,812✔
888
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
238,812✔
889
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
477,624✔
890
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
238,812✔
891
  }
892

893
  pMsg->leaderSnodeId = pStream->mainSnodeId;
192,638✔
894
  pMsg->streamName = pInfo->streamName;
192,638✔
895

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

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

903
_exit:
189,337✔
904

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

914

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

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

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

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

959

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

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

983

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

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

1001
  return code;
760,649✔
1002
}
1003

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

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

1012

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

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

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

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

1037
_exit:
992,719✔
1038

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

1043
  return code;
992,719✔
1044
}
1045

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

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

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

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

1084
_exit:
×
1085

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

1090
  return 0;
×
1091
}
1092

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

1107
  int32_t snodeTarget = taosRand() % snodeNum;
585,486✔
1108

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

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

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

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

1146
_exit:
588,226✔
1147

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

1152
  if (0 == snodeId) {
588,226✔
1153
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,740✔
1154
  }
1155

1156
  return snodeId;
588,226✔
1157
}
1158

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

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

1170
_exit:
773,384✔
1171

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

1176
  return snodeId;
773,384✔
1177
}
1178

1179

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

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

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

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

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

1212
_exit:
192,638✔
1213

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

1218
  return code;
192,638✔
1219
}
1220

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

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

1247
_exit:
299,471✔
1248

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

1253
  return code;
299,471✔
1254
}
1255

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

1285
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
82,929✔
1286
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
82,929✔
1287
      
1288
      void *pIter = NULL;
82,929✔
1289
      while (1) {
420,191✔
1290
        SStmTaskDeploy info = {0};
503,120✔
1291
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
503,120✔
1292
        if (pIter == NULL) {
503,120✔
1293
          break;
82,929✔
1294
        }
1295
      
1296
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
420,191✔
1297
          pState = taosArrayReserve(pInfo->trigReaders, 1);
130,111✔
1298

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

1308
        sdbRelease(pSdb, pVgroup);
420,191✔
1309
      }
1310
      break;
82,929✔
1311
    }
1312
    default:
1,008✔
1313
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,008✔
1314
      break;
1,008✔
1315
  }
1316

1317
_exit:
192,638✔
1318

1319
  mndReleaseDb(pCtx->pMnode, pDb);
192,638✔
1320

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

1325
  return code;
192,638✔
1326
}
1327

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

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

1353
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
289,021✔
1354
  if (NULL == ppRes) {
289,021✔
1355
    SArray* pRes = taosArrayInit(1, sizeof(addr));
284,622✔
1356
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
284,622✔
1357
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
569,244✔
1358
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
284,622✔
1359
  } else {
1360
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
8,798✔
1361
  }
1362

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

1369
  nodesDestroyNode((SNode*)pSubplan);
289,021✔
1370

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

1375
  return code;
289,021✔
1376
}
1377

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

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

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

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

1414
  return code;
84,212✔
1415
}
1416

1417

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

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

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

1446
_exit:
291,606✔
1447

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

1452
  return code;
291,606✔
1453
}
1454

1455

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

1465
  
1466
  for (int32_t i = 0; i < calcTasksNum; ++i) {
565,871✔
1467
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
373,233✔
1468
    if (pScan->readFromCache) {
373,233✔
1469
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
84,212✔
1470
      continue;
84,212✔
1471
    }
1472
    
1473
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
289,021✔
1474
    for (int32_t m = 0; m < vgNum; ++m) {
578,042✔
1475
      pState = tdListReserve(pInfo->calcReaders);
289,021✔
1476
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
289,021✔
1477

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

1483
_exit:
192,638✔
1484

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

1489
  return code;
192,638✔
1490
}
1491

1492

1493

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

1521
_exit:
×
1522

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

1527
  return code;
×
1528
}
1529

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

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

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

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

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

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

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

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

1588
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1589
}
1590

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

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

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

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

1624
_exit:
634,116✔
1625

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

1630
  return code;
634,116✔
1631
}
1632

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

1641
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
634,116✔
1642

1643
  SNode* pTmp = NULL;
634,116✔
1644
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,807,185✔
1645
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,173,069✔
1646
      ERASE_NODE(pPlan->pChildren);
1,119,699✔
1647
      continue;
1,119,699✔
1648
    }
1649
    WHERE_NEXT;
53,370✔
1650
  }
1651
  nodesClearList(pPlan->pChildren);
634,116✔
1652
  pPlan->pChildren = NULL;
634,116✔
1653

1654
  if (NULL == pPlan->pParents) {
634,116✔
1655
    goto _exit;
580,746✔
1656
  }
1657

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

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

1677
  return code;
634,116✔
1678
}
1679

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

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

1695
_exit:
580,746✔
1696

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

1701
  return code;
580,746✔
1702
}
1703

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

1719
  if (dagIdx >= 0) {
193,582✔
1720
    subQ = true;
4,245✔
1721
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
4,245✔
1722
  } else {
1723
    pDag = *pRoot;
189,337✔
1724
  }
1725

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

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

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

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

1756
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
193,582✔
1757
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
193,582✔
1758
  
1759
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
774,328✔
1760
    totalTaskNum = 0;
580,746✔
1761

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

1768
    taskIdx = 0;
580,746✔
1769

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

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

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

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

1794
      for (int32_t n = 0; n < taskNum; ++n) {
1,235,127✔
1795
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
634,116✔
1796
        pState = taosArrayReserve(deployList, 1);
634,116✔
1797

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

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

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

1824
        if (subQ) {
634,116✔
1825
          if (i == 0) {
18,279✔
1826
            SStmTaskSrcAddr addr = {0};
12,735✔
1827
            SDownstreamSourceNode* pSource = NULL;
12,735✔
1828

1829
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
12,735✔
1830

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

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

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

1846
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
12,735✔
1847
          }
1848
        } else {
1849
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
615,837✔
1850
        }
1851
      }
1852

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

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

1861
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
580,746✔
1862

1863
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
580,746✔
1864

1865
    nodesDestroyNode((SNode *)(*pRoot));
580,746✔
1866
    *pRoot = NULL;
580,746✔
1867
    
1868
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
580,746✔
1869
    if (subQ) {
580,746✔
1870
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
12,735✔
1871
    } else {
1872
      pDag = *pRoot;
568,011✔
1873
    }
1874

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

1878
  if (!subQ) {
193,582✔
1879
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
757,348✔
1880
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
568,011✔
1881
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
568,011✔
1882
    }
1883
  }
1884
  
1885
  pInfo->runnerNum += totalTaskNum;
193,582✔
1886
  
1887
_exit:
193,582✔
1888

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

1893
  taosArrayDestroy(deployTaskList);
193,582✔
1894

1895
  return code;
193,582✔
1896
}
1897

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

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

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

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

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

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

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

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

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

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

UNCOV
1958
        pRunner++;
×
1959
      }
1960

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

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

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

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

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

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

1976
_exit:
×
1977

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

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

UNCOV
1985
  return code;
×
1986
}
1987

1988

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

1997
_exit:
183,576✔
1998

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

2003
  return code;
183,576✔
2004
}
2005

2006

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

2014
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
748,816✔
2015
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
559,479✔
2016
    (void)mstWaitLock(&pVg->lock, true);
559,479✔
2017

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

2024
    for (int32_t i = 0; i < taskNum; ++i) {
7,999,062✔
2025
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
7,439,583✔
2026
      if (pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
7,439,583✔
2027
        continue;
6,915,051✔
2028
      }
2029

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

2040
    taosRUnLockLatch(&pVg->lock);
559,479✔
2041
  }
2042

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

2048
  return code;
189,337✔
2049
}
2050

2051
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
192,638✔
2052
  if (NULL == pStream->pCreate->calcPlan) {
192,638✔
2053
    return TSDB_CODE_SUCCESS;
3,301✔
2054
  }
2055

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

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

2066
  FOREACH(pNode, pPlan->pChildren) {
193,582✔
2067
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
4,245✔
2068

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

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

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

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

2085
  taosHashClear(mStreamMgmt.toUpdateScanMap);
189,337✔
2086
  mStreamMgmt.toUpdateScanNum = 0;
189,337✔
2087

2088
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
189,337✔
2089

2090
_exit:
189,337✔
2091
  nodesDestroyNode((SNode *)pPlan);
189,337✔
2092
  if (pSubEP) {
189,337✔
2093
    nodesDestroyList(pSubEP);
3,553✔
2094
  }
2095

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

2100
  return code;
189,337✔
2101
}
2102

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

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

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

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

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

2126
  return code;
192,638✔
2127
}
2128

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

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

2168
_exit:
×
2169

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

UNCOV
2175
  return code;
×
2176
}
2177

2178

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

2184
  pStatus->lastActionTs = INT64_MIN;
186,877✔
2185

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

2191
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
186,877✔
2192
  
2193
  if (pStream->pCreate->numOfCalcSubplan > 0) {
186,877✔
2194
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
183,576✔
2195
    
2196
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
183,576✔
2197
  }
2198

2199
  if (initList) {
186,877✔
UNCOV
2200
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2201

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

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

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

2223
  return code;
186,877✔
2224
}
2225

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

2232
  if (NULL == pStatus) {
192,638✔
2233
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
186,877✔
2234

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

2237
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
186,877✔
2238
  }
2239
  
2240
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
192,638✔
2241

2242
  mstLogSStmStatus("stream deployed", streamId, pStatus);
192,638✔
2243

2244
_exit:
192,638✔
2245

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

2255
  return code;
192,638✔
2256
}
2257

2258

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

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

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

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

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

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

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

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

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

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

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

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

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

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

2367
  mstResetSStmStatus(pStatus);
6,322✔
2368

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

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

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

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

2405
  TAOS_CHECK_EXIT(code);
193,199✔
2406

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

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

2423
_exit:
193,199✔
2424

2425
  mndReleaseStream(pCtx->pMnode, pStream);
193,199✔
2426

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

2431
  return code;
193,199✔
2432
}
2433

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

2466
_exit:
1,122✔
2467

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

2472
  return code;
1,122✔
2473
}
2474

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

2506
_exit:
2507

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

2512
  return code;
2513
}
2514
*/
2515

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

UNCOV
2565
_exit:
×
2566

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

2571
  return code;
×
2572
}
2573

2574

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

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

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

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

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

2602
  TAOS_CHECK_EXIT(code);
1,122✔
2603

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

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

2634
_exit:
1,122✔
2635

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

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

2645
  return code;
1,122✔
2646
}
2647

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

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

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

UNCOV
2693
  return TSDB_CODE_SUCCESS;
×
2694
}
2695

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

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

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

2704
_exit:
451✔
2705

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

2712
  return code;
451✔
2713
}
2714

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

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

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

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

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

UNCOV
2735
_exit:
×
2736

2737
  taosHashRelease(mStreamMgmt.streamMap, pStream);
33,459✔
2738

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

2743
  return;
33,459✔
2744
}
2745

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

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

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

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

2765
_exit:
3,644✔
2766

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

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

2773
  return code;
3,644✔
2774
}
2775

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

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

2797
_exit:
48,610✔
2798

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

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

2815
    pStatus = (SStmStatus*)pIter;
×
2816

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

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

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

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

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

UNCOV
2834
  msmStopAllStreamsByGrant(errCode);
×
2835

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

2841
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,418,953✔
2842
  int32_t code = TSDB_CODE_SUCCESS;
1,418,953✔
2843
  int32_t lino = 0;
1,418,953✔
2844
  int64_t streamId = pDeploy->task.streamId;
1,418,953✔
2845
  
2846
  switch (pDeploy->task.type) {
1,418,953✔
2847
    case STREAM_READER_TASK:
592,199✔
2848
      if (NULL == pStream->readerTasks) {
592,199✔
2849
        pStream->streamId = streamId;
248,779✔
2850
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
248,779✔
2851
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
248,779✔
2852
      }
2853
      
2854
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,184,398✔
2855
      break;
592,199✔
2856
    case STREAM_TRIGGER_TASK:
192,638✔
2857
      pStream->streamId = streamId;
192,638✔
2858
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
192,638✔
2859
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
192,638✔
2860
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
192,638✔
2861
      break;
192,638✔
2862
    case STREAM_RUNNER_TASK:
634,116✔
2863
      if (NULL == pStream->runnerTasks) {
634,116✔
2864
        pStream->streamId = streamId;
195,915✔
2865
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
195,915✔
2866
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
195,915✔
2867
      }      
2868
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,268,232✔
2869
      break;
634,116✔
2870
    default:
×
2871
      break;
×
2872
  }
2873

2874
_exit:
1,418,953✔
2875

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

2880
  return code;
1,418,953✔
2881
}
2882

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

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

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

2908
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,159,166✔
2909
    
2910
    break;
1,159,166✔
2911
  }
2912
  
2913
_exit:
1,418,953✔
2914

2915
  taosHashRelease(pHash, pStream);
1,418,953✔
2916

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

2923
  return code;
1,418,953✔
2924
}
2925

2926

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

2932
  for (int32_t i = 0; i < taskNum; ++i) {
1,647,763✔
2933
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,418,953✔
2934
    if (pExt->deployed) {
1,418,953✔
2935
      continue;
×
2936
    }
2937

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

2941
    (void)atomic_add_fetch_32(deployed, 1);
1,418,953✔
2942
  }
2943

2944
_exit:
228,810✔
2945

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

2950
  return code;
228,810✔
2951
}
2952

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

2960
  mstDebug("start to add stream vgroup tasks deploy");
65,514✔
2961
  
2962
  for (int32_t i = 0; i < vgNum; ++i) {
433,790✔
2963
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
368,276✔
2964

2965
    msmUpdateVgroupUpTs(pCtx, *vgId);
368,276✔
2966

2967
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
368,276✔
2968
    if (NULL == pVg) {
368,276✔
2969
      continue;
239,009✔
2970
    }
2971

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

2985
_exit:
65,514✔
2986

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

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

2995
  return code;
65,514✔
2996
}
2997

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

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

3011
  (void)mstWaitLock(&pSnode->lock, false);
50,818✔
3012
  
3013
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
50,818✔
3014
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
48,725✔
3015
  }
3016

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

3023
_exit:
50,818✔
3024

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

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

3033
  return code;
50,818✔
3034
}
3035

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

3047
_exit:
709,850✔
3048

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

3053
  return code;
709,850✔
3054
}
3055

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

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

3067
  while (1) {
259,787✔
3068
    pIter = taosHashIterate(pCtx->deployStm, pIter);
325,808✔
3069
    if (pIter == NULL) {
325,808✔
3070
      break;
66,021✔
3071
    }
3072
    
3073
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
259,787✔
3074
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
519,574✔
3075

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

3080
    mstClearSStmStreamDeploy(pDeploy);
259,787✔
3081
    
3082
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
259,787✔
3083
  }
3084
  
3085
_exit:
66,021✔
3086

3087
  if (pIter) {
66,021✔
UNCOV
3088
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3089
  }
3090

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

3098
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
65,514✔
3099
  int32_t code = TSDB_CODE_SUCCESS;
65,514✔
3100
  int32_t lino = 0;
65,514✔
3101
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
65,514✔
3102
  SStmVgTasksToDeploy* pVg = NULL;
65,514✔
3103
  
3104
  for (int32_t i = 0; i < vgNum; ++i) {
433,790✔
3105
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
368,276✔
3106
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
368,276✔
3107
    if (NULL == pVg) {
368,276✔
3108
      continue;
239,009✔
3109
    }
3110

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

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

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

UNCOV
3139
      mstDestroySStmTaskToDeployExt(pExt);
×
3140

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

3149
_exit:
65,514✔
3150

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

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

3160
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
57,469✔
3161
  if (!GOT_SNODE(snodeId)) {
57,469✔
3162
    return;
4,422✔
3163
  }
3164
  
3165
  int32_t code = TSDB_CODE_SUCCESS;
53,047✔
3166
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
53,047✔
3167
  if (NULL == pSnode) {
53,047✔
3168
    return;
2,229✔
3169
  }
3170

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

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

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

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

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

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

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

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

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

3238
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,974,941✔
3239
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,974,941✔
3240
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
65,514✔
3241
  }
3242

3243
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,974,941✔
3244
    msmCleanDeployedSnodeTasks(pHb->snodeId);
57,469✔
3245
  }
3246
}
16,974,941✔
3247

3248
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
16,974,941✔
3249
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,974,941✔
3250
  if (mStreamMgmt.tCtx) {
16,974,941✔
3251
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
16,890,999✔
3252
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
16,890,999✔
3253
  }
3254
}
16,974,941✔
3255

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

3273
_exit:
250,774✔
3274

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

3279
  return code;
250,774✔
3280
}
3281

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

3298
_exit:
×
3299

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

3304
  return code;
×
3305
}
3306

3307

3308

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

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

3326
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
315,332✔
3327
    if (pAction->undeploy.doCheckpoint) {
157,666✔
3328
      pAction->undeploy.doCheckpoint = dropped ? false : true;
24,448✔
3329
    }
3330
    if (!pAction->undeploy.doCleanup) {
157,666✔
3331
      pAction->undeploy.doCleanup = dropped ? true : false;
24,448✔
3332
    }
3333
    
3334
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
157,666✔
3335
  } else {
3336
    SStmAction newAction = {0};
41,623✔
3337
    newAction.actions = action;
41,623✔
3338
    newAction.undeploy.doCheckpoint = dropped ? false : true;
41,623✔
3339
    newAction.undeploy.doCleanup = dropped ? true : false;
41,623✔
3340
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
41,623✔
3341
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
41,623✔
3342
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
83,246✔
3343
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
41,623✔
3344
    
3345
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
41,623✔
3346
  }
3347

3348
_exit:
199,289✔
3349

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

3354
  return code;
199,289✔
3355
}
3356

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

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

3378
_exit:
3,644✔
3379

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

3385
  return code;
3,644✔
3386
}
3387

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

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

3398
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
328,216✔
3399
  for (int32_t i = 0; i < readerNum; ++i) {
720,521✔
3400
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
392,305✔
3401
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
392,305✔
3402
      return false;
×
3403
    }
3404
  }
3405

3406
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
328,216✔
3407
  for (int32_t i = 0; i < readerNum; ++i) {
388,875✔
3408
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
60,659✔
3409
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
60,659✔
3410
      return false;
×
3411
    }
3412
  }
3413

3414
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
328,216✔
3415
  SListNode* pNode = listHead(pStream->calcReaders);
328,216✔
3416
  for (int32_t i = 0; i < readerNum; ++i) {
953,125✔
3417
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
624,909✔
3418
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
624,909✔
3419
      return false;
×
3420
    }
3421
    pNode = TD_DLIST_NODE_NEXT(pNode);
624,909✔
3422
  }
3423

3424
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,061,455✔
3425
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
810,681✔
3426
    for (int32_t m = 0; m < runnerNum; ++m) {
1,605,714✔
3427
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
872,475✔
3428
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
872,475✔
3429
        return false;
77,442✔
3430
      }
3431
    }
3432
  }
3433
  
3434
  return true;
250,774✔
3435
}
3436

3437

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

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

3455
  stopped = atomic_load_8(&pStatus->stopped);
12,436,234✔
3456
  if (stopped) {
12,436,234✔
UNCOV
3457
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3458
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3459
    return;
×
3460
  }
3461
  
3462
  switch (pMsg->status) {
12,436,234✔
3463
    case STREAM_STATUS_INIT:      
12,431,472✔
3464
      if (STREAM_TRIGGER_TASK != pMsg->type) {
12,431,472✔
3465
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
11,940,771✔
3466
        return;
11,940,771✔
3467
      }
3468
      
3469
      if (INT64_MIN == pStatus->lastActionTs) {
490,701✔
3470
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3471
        return;
×
3472
      }
3473
      
3474
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
490,701✔
3475
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
162,485✔
3476
        return;
162,485✔
3477
      }
3478

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

3501
_exit:
332,978✔
3502

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

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

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

3517
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
199,289✔
3518

3519
_exit:
199,289✔
3520

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

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

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

3550
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
4,271,801✔
3551
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,130,547✔
3552
    if (NULL == pStatus->detailStatus) {
2,130,547✔
3553
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
189,984✔
3554
      if (NULL == pStatus->detailStatus) {
189,984✔
UNCOV
3555
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3556
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3557
      }
3558
    }
3559
    
3560
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,130,547✔
3561
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,130,547✔
3562
  }
3563

3564
_exit:
2,141,254✔
3565

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

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

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

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

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

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

3606
    if ((*ppStatus)->status != pTask->status) {
33,858,575✔
3607
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,405,849✔
3608
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,006,242✔
3609
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,399,607✔
3610
        if (pStream->triggerTask) {
×
3611
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3612
        }
3613
        
UNCOV
3614
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3615
      }
3616
    }
3617
    
3618
    (*ppStatus)->errCode = pTask->errorCode;
33,858,575✔
3619
    (*ppStatus)->status = pTask->status;
33,858,575✔
3620
    (*ppStatus)->lastUpTs = pCtx->currTs;
33,858,575✔
3621
    
3622
    if (STREAM_STATUS_RUNNING != pTask->status) {
33,858,575✔
3623
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
12,436,234✔
3624
    }
3625
    
3626
    if (STREAM_TRIGGER_TASK == pTask->type) {
33,858,575✔
3627
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
4,271,801✔
3628
    }
3629
  }
3630

3631
_exit:
1,109,823✔
3632

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

3637
  return code;
1,109,823✔
3638
}
3639

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

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

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

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

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

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

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

3740
_exit:
×
3741

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

UNCOV
3748
  return code;
×
3749
}
3750

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

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

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

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

UNCOV
3779
_exit:
×
3780

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

3785
  return code;
×
3786
}
3787

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

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

3805
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
501,548✔
3806
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
250,774✔
3807

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

3810
  return;
250,774✔
3811

UNCOV
3812
_exit:
×
3813

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

3817

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

3827
  SStreamTaskUndeploy undeploy;
41,623✔
3828
  for (int32_t i = 0; i < dropNum; ++i) {
240,912✔
3829
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
199,289✔
3830
    undeploy.task = *pTask;
199,289✔
3831
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
199,289✔
3832
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
199,289✔
3833

3834
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
398,578✔
3835
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
199,289✔
3836

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

3840
  return;
41,623✔
3841

3842
_exit:
×
3843

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

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

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

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

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

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

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

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

UNCOV
3877
_exit:
×
3878

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

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

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

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

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

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

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

3915
_exit:
3,644✔
3916

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

3924

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

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

3933
  while (1) {
296,041✔
3934
    pIter = taosHashIterate(pCtx->actionStm, pIter);
431,617✔
3935
    if (pIter == NULL) {
431,617✔
3936
      break;
135,576✔
3937
    }
3938

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

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

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

3955
    if (STREAM_ACT_START & pAction->actions) {
254,418✔
3956
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
250,774✔
3957
    }
3958
  }
3959
  
3960
_exit:
135,576✔
3961

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

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

3970
  return code;
135,576✔
3971
}
3972

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

3979
  while (true) {
3980
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
17,397,280✔
3981
    if (NULL == lastTs) {
17,397,280✔
3982
      if (noExists) {
652,541✔
3983
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
146,260✔
3984
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
146,260✔
3985
      }
3986

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

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

4001
        continue;
×
4002
      }
4003

4004
      return code;
4,768✔
4005
    }
4006

4007
    break;
4008
  }
4009

4010
_exit:
146,260✔
4011

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

4016
  return code;  
146,260✔
4017
}
4018

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

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

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

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

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

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

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

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

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

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

4093
  msmWatchCheckStreamMap(pCtx);
×
4094

4095
_exit:
370✔
4096

4097
  mStreamMgmt.lastTaskId += 100000;
370✔
4098

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

4101
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
370✔
4102

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

4107
  return code;
370✔
4108
}
4109

4110

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

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

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

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

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

4136
_exit:
12,685✔
4137

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

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

4146
  return code;
12,685✔
4147
}
4148

4149
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
6,367,534✔
4150
  int32_t listSize = taosArrayGetSize(pOReaders);
6,367,534✔
4151
  int32_t totalSize = 0;
6,367,534✔
4152
  
4153
  for (int32_t i = 0; i < listSize; ++i) {
8,033,852✔
4154
    SArray* pList = taosArrayGetP(pOReaders, i);
1,666,318✔
4155
    totalSize += taosArrayGetSize(pList);
1,666,318✔
4156
  }
4157

4158
  return totalSize;
6,367,534✔
4159
}
4160

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

4167
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
1,020,225✔
4168
}
4169

4170

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

4175
  if (NULL == pStatus->trigOReaders) {
60,659✔
4176
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
52,548✔
4177
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
52,548✔
4178
  }
4179

4180
  while (true) {
52,548✔
4181
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
113,207✔
4182

4183
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
113,207✔
4184
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
52,548✔
4185
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
52,548✔
4186

4187
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
105,096✔
4188
      continue;
52,548✔
4189
    }
4190

4191
    *ppRes = *ppOReaderList;
60,659✔
4192
    break;
60,659✔
4193
  }
4194

4195
_exit:
60,659✔
4196

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

4201
  return code;
60,659✔
4202
}
4203

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

4210
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
71,443✔
4211
  for (int32_t i = 0; i < readerNum; ++i) {
148,480✔
4212
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
87,821✔
4213
    if (pReader->id.nodeId == vgId) {
87,821✔
4214
      readerExists = true;
10,784✔
4215
      break;
10,784✔
4216
    }
4217
  }
4218

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

4229
_exit:
10,784✔
4230

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

4235
  return code;
71,443✔
4236
}
4237

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

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

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

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

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

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

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

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

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

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

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

4326
_exit:
62,702✔
4327

4328
  tFreeSStreamMgmtReq(pMgmtReq);
62,702✔
4329
  taosMemoryFree(pMgmtReq);
62,702✔
4330

4331
  tSimpleHashCleanup(pVgs);
62,702✔
4332
  mstDestroyDbVgroupsHash(pDbVgroups);
62,702✔
4333

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

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

4344
_final:
62,702✔
4345

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

4352
  return finalCode;
62,702✔
4353
}
4354

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

4369
_exit:
2,585✔
4370

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

4375
  return code;
2,585✔
4376
}
4377

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

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

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

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

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

4421
_exit:
2,585✔
4422

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

4427
  return code;
2,585✔
4428
}
4429

4430

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

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

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

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

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

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

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

4491
_exit:
2,585✔
4492

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

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

4505

4506
_final:
2,585✔
4507

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

4514
  return finalCode;
2,585✔
4515
}
4516

4517

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

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

4535
_exit:
65,287✔
4536

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

4541
  return code;
65,287✔
4542
}
4543

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

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

4569
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
65,287✔
4570
  }
4571

4572
_exit:
19,126✔
4573

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

4578
  return code;
19,126✔
4579
}
4580

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

4586
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,878,314✔
4587
  if (GOT_SNODE(pReq->snodeId)) {
16,732,054✔
4588
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,316,730✔
4589
  }
4590
  
4591
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,732,054✔
4592
    msmHandleStreamActions(pCtx);
48,610✔
4593
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
48,610✔
4594
  }
4595

4596
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
16,732,054✔
4597
    code = msmHandleStreamRequests(pCtx);
19,126✔
4598
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
19,126✔
4599
    TAOS_CHECK_EXIT(code);
19,126✔
4600
  }
4601

4602
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,732,054✔
4603
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
65,514✔
4604
  } else {
4605
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,666,540✔
4606
  }
4607

4608
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,732,054✔
4609
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
53,047✔
4610
  }
4611

4612
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,732,054✔
4613
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
66,021✔
4614
  }
4615

4616
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,732,054✔
4617
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,109,823✔
4618
  }
4619

4620
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,732,054✔
4621
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
135,576✔
4622
  }
4623

4624
_exit:
16,732,054✔
4625

4626
  if (code) {
16,878,314✔
4627
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
146,260✔
4628
  }
4629

4630
  return code;
16,878,314✔
4631
}
4632

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

4638
  if (TSDB_CODE_SUCCESS != code) {
16,974,941✔
4639
    goto _exit;
146,260✔
4640
  }
4641

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

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

4654
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
16,828,681✔
4655
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
16,828,681✔
4656

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

4668
_exit:
16,974,941✔
4669

4670
  pMsg->code = code;
16,974,941✔
4671
  pMsg->info = *pRpcInfo;
16,974,941✔
4672
  if (TSDB_CODE_SUCCESS == code) {
16,974,941✔
4673
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
16,828,681✔
4674
    pMsg->pCont = buf;
16,828,681✔
4675
  }
4676
}
16,974,941✔
4677

4678

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

4684
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,974,941✔
4685

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

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

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

4715
_exit:
16,974,941✔
4716

4717
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
16,974,941✔
4718

4719
  msmCleanStreamGrpCtx(pHb);
16,974,941✔
4720
  msmClearStreamToDeployMaps(pHb);
16,974,941✔
4721

4722
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
16,974,941✔
4723
  
4724
  tFreeSMStreamHbRspMsg(&rsp);
16,974,941✔
4725

4726
  return code;
16,974,941✔
4727
}
4728

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

4734
  mstInfo("start to process mnode become leader");
422,084✔
4735

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

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

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

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

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

4758
  streamRemoveVnodeLeader(MNODE_HANDLE);
524,544✔
4759

4760
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
524,544✔
4761
    taosWLockLatch(&mStreamMgmt.runtimeLock);
422,084✔
4762
    msmDestroyRuntimeInfo(pMnode);
422,084✔
4763
    mStreamMgmt.stat.inactiveTimes++;
422,084✔
4764
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
422,084✔
4765
  }
4766
}
4767

4768

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

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

4785
  if (pStream->mainSnodeId == pSnode->id) {
8,167✔
4786
    if (NULL == *ppRes) {
5,775✔
4787
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
945✔
4788
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
945✔
4789
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
945✔
4790
    }
4791

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

4795
  return true;
8,167✔
4796

4797
_exit:
×
4798

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

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

4805
  return false;
×
4806
}
4807

4808

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

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

4822
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4823

4824
_exit:
36,456✔
4825

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

4830
  return code;
36,456✔
4831
}
4832

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

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

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

4862
    return true;
20,393✔
4863
  }
4864

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

4867
  return true;
1,445✔
4868
}
4869

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

4882
    pStatus = (SStmStatus*)pIter;
243,753✔
4883

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

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

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

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

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

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

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

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

4936
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,780,353✔
4937
  for (int32_t i = 0; i < taskNum; ++i) {
8,015,402✔
4938
    SStmTaskStatus* pTask = *(pList + i);
4,235,049✔
4939

4940
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
4,235,049✔
4941
      continue;
502,549✔
4942
    }
4943
    
4944
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,734,936✔
4945
      continue;
3,733,814✔
4946
    }
4947

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

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

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

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

4973
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
143,450✔
4974
  void* pIter = NULL;
143,450✔
4975
  SStmVgStreamStatus* pVg = NULL;
143,450✔
4976
  int64_t streamId = 0;
143,450✔
4977
  
4978
  while (true) {
1,495,340✔
4979
    pIter = taosHashIterate(pStreams, pIter);
1,638,790✔
4980
    if (NULL == pIter) {
1,638,790✔
4981
      break;
143,450✔
4982
    }
4983

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

4987
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,495,340✔
4988
    if (taskNum > 0) {
1,495,340✔
4989
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
890,417✔
4990
    }
4991

4992
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,495,340✔
4993
    if (taskNum > 0) {
1,495,340✔
4994
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
992,497✔
4995
    }
4996
  }
4997
}
143,450✔
4998

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

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

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

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

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

5024

5025
void msmCheckVgroupStatus(SMnode *pMnode) {
364,346✔
5026
  void* pIter = NULL;
364,346✔
5027
  int32_t code = 0;
364,346✔
5028
  
5029
  while (true) {
1,330,673✔
5030
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,695,019✔
5031
    if (NULL == pIter) {
1,695,019✔
5032
      break;
364,346✔
5033
    }
5034

5035
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,330,673✔
5036
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,330,673✔
5037
      continue;
1,181,932✔
5038
    }
5039
    
5040
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
148,741✔
5041

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

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

5062
    msmCheckVgroupStreamStatus(pVg->streamTasks);
143,450✔
5063
  }
5064
}
364,346✔
5065

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

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

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

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

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

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

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

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

5141

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

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

5156
    if (NULL != pSnode->trigger) {
498,495✔
5157
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
485,548✔
5158
    }
5159

5160
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,993,980✔
5161
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,495,485✔
5162
      if (taskNum > 0) {
1,495,485✔
5163
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,411,891✔
5164
      }
5165
    }
5166
  }
5167
}
40,774✔
5168

5169

5170
void msmCheckSnodeStatus(SMnode *pMnode) {
364,346✔
5171
  void* pIter = NULL;
364,346✔
5172
  
5173
  while (true) {
428,360✔
5174
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
792,706✔
5175
    if (NULL == pIter) {
792,706✔
5176
      break;
364,346✔
5177
    }
5178

5179
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
428,360✔
5180
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
428,360✔
5181
      continue;
376,351✔
5182
    }
5183

5184
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
52,009✔
5185
    
5186
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
52,009✔
5187
    if (NULL == pSnode->streamTasks) {
52,009✔
5188
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
10,113✔
5189
      continue;
10,113✔
5190
    }
5191
    
5192
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
41,896✔
5193
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,122✔
5194
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5195
      
5196
      msmHandleSnodeLost(pMnode, pSnode);
1,122✔
5197
      continue;
1,122✔
5198
    }
5199
    
5200
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
40,774✔
5201

5202
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
40,774✔
5203
  }
5204
}
364,346✔
5205

5206

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

5210
  msmCheckVgroupStatus(pMnode);
364,346✔
5211
  msmCheckSnodeStatus(pMnode);
364,346✔
5212
}
364,346✔
5213

5214
void msmCheckSnodesState(SMnode *pMnode) {
364,346✔
5215
  if (!MST_READY_FOR_SNODE_LOOP()) {
364,346✔
5216
    return;
322,670✔
5217
  }
5218

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

5221
  void* pIter = NULL;
41,676✔
5222
  int32_t snodeId = 0;
41,676✔
5223
  while (true) {
44,878✔
5224
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
86,554✔
5225
    if (NULL == pIter) {
86,554✔
5226
      break;
41,676✔
5227
    }
5228

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

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

5247
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
41,676✔
5248
}
5249

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

5257
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
10,567,268✔
5258
    mstTrace("ignore health check since no stream now");
9,838,576✔
5259
    return false;
9,838,576✔
5260
  }
5261

5262
  return true;
728,692✔
5263
}
5264

5265
void msmHealthCheck(SMnode *pMnode) {
10,205,425✔
5266
  if (!msmCheckNeedHealthCheck(pMnode)) {
10,205,425✔
5267
    return;
9,841,079✔
5268
  }
5269

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

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

5287
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
364,346✔
5288

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

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

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

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

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

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

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

5340
  return code;
22,769✔
5341
}
5342

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

5350
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
843,237✔
5351

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

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

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

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

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

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

5457
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
422,084✔
5458
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
422,084✔
5459

5460
  mStreamMgmt.lastTaskId = 1;
422,084✔
5461

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

5465
  if (activeStreamNum > 0) {
422,084✔
5466
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
711✔
5467
  } else {
5468
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
421,373✔
5469
  }
5470

5471
_exit:
422,084✔
5472

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

5480
  return code;
422,084✔
5481
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc