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

taosdata / TDengine / #5052

13 May 2026 12:00PM UTC coverage: 73.338% (-0.02%) from 73.358%
#5052

push

travis-ci

web-flow
feat: taosdump support stream backup/restore (#35326)

139 of 170 new or added lines in 3 files covered. (81.76%)

761 existing lines in 163 files now uncovered.

281469 of 383795 relevant lines covered (73.34%)

134502812.98 hits per line

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

74.08
/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() {
970,222✔
34
  SStmQNode* pQNode = NULL;
970,222✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
970,222✔
37
    return;
485,111✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,420,562✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
14,523,372✔
49
    taosHashCleanup(pCtx->deployStm[m]);
12,102,810✔
50
    taosHashCleanup(pCtx->actionStm[m]);
12,102,810✔
51
  }
52
}
2,420,562✔
53

54
void msmDestroyThreadCtxs() {
970,222✔
55
  if (NULL == mStreamMgmt.tCtx) {
970,222✔
56
    return;
485,111✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,905,673✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,420,562✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
485,111✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
970,222✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
970,222✔
94
}
970,222✔
95

96

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

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

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

111
    pStatus = pStream;
221✔
112
  }
113

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

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

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

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

136
_exit:
×
137

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

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

145

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

186
    tasks.lastUpTs = taosGetTimestampMs();
46,918✔
187
    code = taosHashPut(mStreamMgmt.snodeMap, &pSnode->id, sizeof(pSnode->id), &tasks, sizeof(tasks));
46,918✔
188
    if (code && TSDB_CODE_DUP_KEY != code) {
46,918✔
189
      sdbRelease(pMnode->pSdb, pSnode);
×
190
      sdbCancelFetch(pMnode->pSdb, pIter);
×
191
      pSnode = NULL;
×
192
      TAOS_CHECK_EXIT(code);
×
193
    }
194

195
    code = TSDB_CODE_SUCCESS;
46,918✔
196
  
197
    sdbRelease(pMnode->pSdb, pSnode);
46,918✔
198
  }
199

200
  pSnode = NULL;
518,028✔
201

202
_exit:
518,028✔
203

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

208
  return code;
518,028✔
209
}
210

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

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

235
  pDnode = NULL;
1,085,706✔
236

237
_exit:
1,085,706✔
238

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

243
  return code;
1,085,706✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
795,449✔
275
  int32_t code = TSDB_CODE_SUCCESS;
795,449✔
276
  int32_t lino = 0;
795,449✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
795,449✔
278
  if (NULL == pStream) {
795,449✔
279
    SStmVgStreamStatus stream = {0};
562,860✔
280
    if (trigReader) {
562,860✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
344,918✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
344,918✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
689,836✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
217,942✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
217,942✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
435,884✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
562,860✔
290
    goto _exit;
562,860✔
291
  }
292
  
293
  if (trigReader) {
232,589✔
294
    if (NULL == pStream->trigReaders) {
25,360✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
25,360✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
25,360✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
50,720✔
300
    goto _exit;
25,360✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
207,229✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
140,740✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
140,740✔
306
  }
307

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

310
_exit:
207,229✔
311

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

320
  return code;
795,449✔
321
}
322

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

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

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

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

350
  return code;
795,449✔
351
}
352

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

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

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

404
  return code;
797,016✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
1,097,480✔
409
  int32_t code = TSDB_CODE_SUCCESS;
1,097,480✔
410
  int32_t lino = 0;
1,097,480✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
1,097,480✔
412
  if (NULL == pStream) {
1,097,480✔
413
    SStmSnodeStreamStatus stream = {0};
254,804✔
414
    if (deployId < 0) {
254,804✔
415
      stream.trigger = pStatus;
3,567✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
251,237✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
251,237✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
502,474✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
254,804✔
423
    goto _exit;
254,804✔
424
  }
425
  
426
  if (deployId < 0) {
842,676✔
427
    if (NULL != pStream->trigger) {
242,555✔
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;
242,555✔
433
    goto _exit;
242,555✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
600,121✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
477,564✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
477,564✔
439
  }
440

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

443
_exit:
600,121✔
444

445
  if (code) {
1,097,480✔
446
    mstsError("%s task %" PRIx64 " SID:%" PRIx64 " failed to add to snode %d streamHash deployId:%d in %s at line %d, error:%s", 
×
447
        (deployId < 0) ? "trigger" : "runner", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId, deployId, __FUNCTION__, lino, tstrerror(code));
448
  } else {
449
    mstsDebug("%s task %" PRIx64 " SID:%" PRIx64 " added to snode %d streamHash deployId:%d", 
1,097,480✔
450
        (deployId < 0) ? "trigger" : "runner", pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.nodeId, deployId);
451
  }
452

453
  return code;
1,097,480✔
454
}
455

456

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

461
  SStmSnodeStatus* pSnode = taosHashGet(mStreamMgmt.snodeMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId));
1,097,480✔
462
  if (NULL == pSnode) {
1,097,480✔
463
    mstsWarn("snode %d not exists in snodeMap anymore, may be dropped", pStatus->id.nodeId);
×
464
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
465
  } else {
466
    if (NULL == pSnode->streamTasks) {
1,097,480✔
467
      pSnode->streamTasks = taosHashInit(2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
31,075✔
468
      TSDB_CHECK_NULL(pSnode->streamTasks, code, lino, _exit, terrno);
31,075✔
469
      taosHashSetFreeFp(pSnode->streamTasks, mstDestroySStmSnodeStreamStatus);
31,075✔
470
    }
471
    
472
    TAOS_CHECK_EXIT(msmSTAddToSnodeStreamHash(pSnode->streamTasks, streamId, pStatus, deployId));
1,097,480✔
473
  }
474
  
475
_exit:
1,097,480✔
476

477
  if (code) {
1,097,480✔
478
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
479
  } else {
480
    mstsDebug("%s task %" PRIx64 " tidx %d added to snodeMap, snodeId:%d", (deployId < 0) ? "trigger" : "runner", 
1,097,480✔
481
        pStatus->id.taskId, pStatus->id.taskIdx, pStatus->id.nodeId);
482
  }
483

484
  return code;
1,097,480✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
246,122✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
79,979✔
568
      ext.deployed = false;
79,979✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
159,958✔
570

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

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

614
  return code;
851,358✔
615
}
616

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

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

632
_exit:
776,067✔
633

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

638
  return code;
776,067✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
17,969✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
289✔
682

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

687
  return code;  
289✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
100,533✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
19,623,466✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
81,439,630✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
61,816,164✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
61,816,164✔
725
  }
726

727
_exit:
19,623,466✔
728

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

733
  return code;
19,623,466✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
441,817✔
739
  int32_t num = taosArrayGetSize(pList);
441,817✔
740
  for (int32_t i = 0; i < num; ++i) {
1,328,104✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
1,097,784✔
742
    if (pScan->readFromCache) {
1,097,784✔
743
      return pScan->scanPlan;
211,497✔
744
    }
745
  }
746

747
  return NULL;
230,320✔
748
}
749

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

780
  return TSDB_CODE_SUCCESS;
797,016✔
781
}
782

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

787
  if (pInfo->runnerDeploys > 0) {
242,555✔
788
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
242,555✔
789
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
242,555✔
790
  }
791
  
792
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
970,220✔
793
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
727,665✔
794
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
727,665✔
795

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

811
_exit:
242,555✔
812

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

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

832
  mndReleaseSnode(pMnode, pSnode);
×
833

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

839
  return TSDB_CODE_SUCCESS;
×
840
}
841

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

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

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

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

885
  SStreamTaskAddr addr;
245,676✔
886
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
246,122✔
887
  if (triggerReaderNum > 0) {
246,122✔
888
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
245,024✔
889
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
245,024✔
890
  }
891
  
892
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
549,515✔
893
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
303,393✔
894
    addr.taskId = pStatus->id.taskId;
303,393✔
895
    addr.nodeId = pStatus->id.nodeId;
303,393✔
896
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
303,393✔
897
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
606,786✔
898
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
303,393✔
899
  }
900

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

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

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

912
_exit:
242,555✔
913

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

923

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

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

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

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

968

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

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

992

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

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

1010
  return code;
973,787✔
1011
}
1012

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

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

1021

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

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

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

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

1046
_exit:
1,300,544✔
1047

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

1052
  return code;
1,300,544✔
1053
}
1054

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

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

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

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

1093
_exit:
×
1094

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

1099
  return 0;
×
1100
}
1101

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

1116
  int32_t snodeTarget = taosRand() % snodeNum;
768,521✔
1117

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

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

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

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

1155
_exit:
771,591✔
1156

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

1161
  if (0 == snodeId) {
771,591✔
1162
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
3,070✔
1163
  }
1164

1165
  return snodeId;
771,591✔
1166
}
1167

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

1177
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
1,022,189✔
1178

1179
_exit:
1,022,189✔
1180

1181
  if (0 == snodeId) {
1,022,189✔
1182
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1183
  }
1184

1185
  return snodeId;
1,022,189✔
1186
}
1187

1188

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

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

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

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

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

1221
_exit:
246,122✔
1222

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

1227
  return code;
246,122✔
1228
}
1229

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

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

1256
_exit:
370,278✔
1257

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

1262
  return code;
370,278✔
1263
}
1264

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

1294
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
97,573✔
1295
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
97,573✔
1296
      
1297
      void *pIter = NULL;
97,573✔
1298
      while (1) {
473,538✔
1299
        SStmTaskDeploy info = {0};
571,111✔
1300
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
571,111✔
1301
        if (pIter == NULL) {
571,111✔
1302
          break;
97,573✔
1303
        }
1304
      
1305
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
473,538✔
1306
          pState = taosArrayReserve(pInfo->trigReaders, 1);
155,942✔
1307

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

1317
        sdbRelease(pSdb, pVgroup);
473,538✔
1318
      }
1319
      break;
97,573✔
1320
    }
1321
    default:
1,098✔
1322
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,098✔
1323
      break;
1,098✔
1324
  }
1325

1326
_exit:
246,122✔
1327

1328
  mndReleaseDb(pCtx->pMnode, pDb);
246,122✔
1329

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

1334
  return code;
246,122✔
1335
}
1336

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

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

1362
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
422,333✔
1363
  if (NULL == ppRes) {
422,333✔
1364
    SArray* pRes = taosArrayInit(1, sizeof(addr));
400,757✔
1365
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
400,757✔
1366
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
801,514✔
1367
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
400,757✔
1368
  } else {
1369
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
43,152✔
1370
  }
1371

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

1378
  nodesDestroyNode((SNode*)pSubplan);
422,333✔
1379

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

1384
  return code;
422,333✔
1385
}
1386

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

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

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

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

1423
  return code;
105,660✔
1424
}
1425

1426

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

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

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

1455
_exit:
425,171✔
1456

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

1461
  return code;
425,171✔
1462
}
1463

1464

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

1474
  
1475
  for (int32_t i = 0; i < calcTasksNum; ++i) {
774,115✔
1476
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
527,993✔
1477
    if (pScan->readFromCache) {
527,993✔
1478
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
105,660✔
1479
      continue;
105,660✔
1480
    }
1481
    
1482
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
422,333✔
1483
    for (int32_t m = 0; m < vgNum; ++m) {
844,666✔
1484
      pState = tdListReserve(pInfo->calcReaders);
422,333✔
1485
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
422,333✔
1486

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

1492
_exit:
246,122✔
1493

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

1498
  return code;
246,122✔
1499
}
1500

1501

1502

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

1530
_exit:
×
1531

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

1536
  return code;
×
1537
}
1538

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

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

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

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

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

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

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

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

1597
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1598
}
1599

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

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

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

1626
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,583,979✔
1627
    for (int32_t i = 0; i < childrenNum; ++i) {
3,305,898✔
1628
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,721,919✔
1629
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,721,919✔
1630
    }
1631
  }
1632

1633
_exit:
851,358✔
1634

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

1639
  return code;
851,358✔
1640
}
1641

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

1650
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
851,358✔
1651

1652
  SNode* pTmp = NULL;
851,358✔
1653
  WHERE_EACH(pTmp, pPlan->pChildren) {
2,510,628✔
1654
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,659,270✔
1655
      ERASE_NODE(pPlan->pChildren);
1,583,979✔
1656
      continue;
1,583,979✔
1657
    }
1658
    WHERE_NEXT;
75,291✔
1659
  }
1660
  nodesClearList(pPlan->pChildren);
851,358✔
1661
  pPlan->pChildren = NULL;
851,358✔
1662

1663
  if (NULL == pPlan->pParents) {
851,358✔
1664
    goto _exit;
776,067✔
1665
  }
1666

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

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

1686
  return code;
851,358✔
1687
}
1688

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

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

1704
_exit:
776,067✔
1705

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

1710
  return code;
776,067✔
1711
}
1712

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

1728
  if (dagIdx >= 0) {
258,689✔
1729
    subQ = true;
16,134✔
1730
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
16,134✔
1731
  } else {
1732
    pDag = *pRoot;
242,555✔
1733
  }
1734

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

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

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

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

1765
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
258,689✔
1766
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
258,689✔
1767
  
1768
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
1,034,756✔
1769
    totalTaskNum = 0;
776,067✔
1770

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

1777
    taskIdx = 0;
776,067✔
1778

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

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

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

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

1803
      for (int32_t n = 0; n < taskNum; ++n) {
1,654,746✔
1804
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
851,358✔
1805
        pState = taosArrayReserve(deployList, 1);
851,358✔
1806

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

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

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

1833
        if (subQ) {
851,358✔
1834
          if (i == 0) {
68,958✔
1835
            SStmTaskSrcAddr addr = {0};
48,402✔
1836
            SDownstreamSourceNode* pSource = NULL;
48,402✔
1837

1838
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
48,402✔
1839

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

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

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

1855
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
48,402✔
1856
          }
1857
        } else {
1858
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
782,400✔
1859
        }
1860
      }
1861

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

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

1870
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
776,067✔
1871

1872
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
776,067✔
1873

1874
    nodesDestroyNode((SNode *)(*pRoot));
776,067✔
1875
    *pRoot = NULL;
776,067✔
1876
    
1877
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
776,067✔
1878
    if (subQ) {
776,067✔
1879
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
48,402✔
1880
    } else {
1881
      pDag = *pRoot;
727,665✔
1882
    }
1883

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

1887
  if (!subQ) {
258,689✔
1888
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
970,220✔
1889
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
727,665✔
1890
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
727,665✔
1891
    }
1892
  }
1893
  
1894
  pInfo->runnerNum += totalTaskNum;
258,689✔
1895
  
1896
_exit:
258,689✔
1897

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

1902
  taosArrayDestroy(deployTaskList);
258,689✔
1903

1904
  return code;
258,689✔
1905
}
1906

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

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

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

1929
    pStartRunner = pRunner;
×
1930
    totalTaskNum = 0;
×
1931

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

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

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

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

1955
        pRunner->id.nodeId = newNodeId;
×
1956

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

1967
        pRunner++;
×
1968
      }
1969

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

1973
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
×
1974

1975
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
×
1976

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

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

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

1985
_exit:
×
1986

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

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

1994
  return code;
×
1995
}
1996

1997

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

2006
_exit:
234,535✔
2007

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

2012
  return code;
234,535✔
2013
}
2014

2015

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

2024
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
956,127✔
2025
    pVg = (SStmVgTasksToDeploy*)pIter;
713,572✔
2026
    (void)mstWaitLock(&pVg->lock, false);
713,572✔
2027

2028
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
713,572✔
2029
    if (atomic_load_32(&pVg->deployed) == taskNum) {
713,572✔
2030
      taosWUnLockLatch(&pVg->lock);
192✔
2031
      pVg = NULL;
192✔
2032
      continue;
192✔
2033
    }
2034

2035
    for (int32_t i = 0; i < taskNum; ++i) {
10,935,678✔
2036
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
10,222,298✔
2037
      if (pExt->deployed || pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
10,222,298✔
2038
        continue;
9,500,139✔
2039
      }
2040

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

2057
    taosWUnLockLatch(&pVg->lock);
713,380✔
2058
    pVg = NULL;
713,380✔
2059
  }
2060

2061
_exit:
242,555✔
2062
  if (code) {
242,555✔
2063
    if (pVg) taosWUnLockLatch(&pVg->lock);
×
2064
    if (pIter) taosHashCancelIterate(mStreamMgmt.toDeployVgMap, pIter);
×
2065
    nodesDestroyNode((SNode*)pSubplan);
×
2066
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2067
  }
2068

2069
  return code;
242,555✔
2070
}
2071

2072
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
246,122✔
2073
  if (NULL == pStream->pCreate->calcPlan) {
246,122✔
2074
    return TSDB_CODE_SUCCESS;
3,567✔
2075
  }
2076

2077
  int32_t code = TSDB_CODE_SUCCESS;
242,555✔
2078
  int32_t lino = 0;
242,555✔
2079
  int64_t streamId = pStream->pCreate->streamId;
242,555✔
2080
  SQueryPlan* pPlan = NULL;
242,555✔
2081
  SNodeList*  pSubEP = NULL;
242,555✔
2082
  SNode*      pNode = NULL;
242,555✔
2083
  int32_t     subQueryPlans = 0;
242,555✔
2084

2085
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
242,555✔
2086

2087
  FOREACH(pNode, pPlan->pChildren) {
258,689✔
2088
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
16,134✔
2089

2090
    subQueryPlans += calcSubQPlan->numOfSubplans;
16,134✔
2091
  }
2092

2093
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
970,220✔
2094
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans + subQueryPlans, sizeof(SStmTaskStatus));
727,665✔
2095
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
727,665✔
2096
  }
2097

2098
  for (int32_t i = 0; i < LIST_LENGTH(pPlan->pChildren); ++i) {
258,689✔
2099
    code = msmBuildRunnerTasksImpl(pCtx, i, pInfo, pStream, &pPlan, &pSubEP);
16,134✔
2100
    TAOS_CHECK_EXIT(code);
16,134✔
2101
  }
2102

2103
  code = msmBuildRunnerTasksImpl(pCtx, -1, pInfo, pStream, &pPlan, &pSubEP);
242,555✔
2104
  TAOS_CHECK_EXIT(code);
242,555✔
2105

2106
  taosHashClear(mStreamMgmt.toUpdateScanMap);
242,555✔
2107
  mStreamMgmt.toUpdateScanNum = 0;
242,555✔
2108

2109
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
242,555✔
2110

2111
_exit:
242,555✔
2112
  nodesDestroyNode((SNode *)pPlan);
242,555✔
2113
  if (pSubEP) {
242,555✔
2114
    nodesDestroyList(pSubEP);
14,149✔
2115
  }
2116

2117
  if (code) {
242,555✔
2118
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2119
  }
2120

2121
  return code;
242,555✔
2122
}
2123

2124
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
246,122✔
2125
  int32_t code = TSDB_CODE_SUCCESS;
246,122✔
2126
  int32_t lino = 0;
246,122✔
2127
  int64_t streamId = pStream->pCreate->streamId;
246,122✔
2128

2129
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
246,122✔
2130

2131
  pCtx->triggerTaskId = msmAssignTaskId();
246,122✔
2132
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
246,122✔
2133
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
246,122✔
2134
    TAOS_CHECK_EXIT(terrno);
×
2135
  }
2136

2137
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
246,122✔
2138
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
246,122✔
2139
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
246,122✔
2140
  
2141
_exit:
246,122✔
2142

2143
  if (code) {
246,122✔
2144
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2145
  }
2146

2147
  return code;
246,122✔
2148
}
2149

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

2176
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
2177
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2178
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2179
      mndReleaseDb(pCtx->pMnode, pDb);
×
2180
      pDb = NULL;
×
2181
      break;
×
2182
    }
2183
    default:
×
2184
      pInfo->trigReaderNum = 0;
×
2185
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2186
      break;
×
2187
  }
2188

2189
_exit:
×
2190

2191
  if (code) {
×
2192
    mndReleaseDb(pCtx->pMnode, pDb);
×
2193
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2194
  }
2195

2196
  return code;
×
2197
}
2198

2199

2200
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
238,102✔
2201
  int32_t code = TSDB_CODE_SUCCESS;
238,102✔
2202
  int32_t lino = 0;
238,102✔
2203
  int64_t streamId = pStream->pCreate->streamId;
238,102✔
2204

2205
  pStatus->lastActionTs = INT64_MIN;
238,102✔
2206

2207
  if (NULL == pStatus->streamName) {
238,102✔
2208
    pStatus->streamName = taosStrdup(pStream->name);
238,102✔
2209
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
238,102✔
2210
  }
2211

2212
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
238,102✔
2213
  
2214
  if (pStream->pCreate->numOfCalcSubplan > 0) {
238,102✔
2215
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
234,535✔
2216
    
2217
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
234,535✔
2218
  }
2219

2220
  if (initList) {
238,102✔
2221
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2222

2223
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
2224
    if (subPlanNum > 0) {
×
2225
      pStatus->calcReaderNum = subPlanNum;
×
2226
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2227
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2228
    }
2229

2230
    if (pStatus->runnerNum > 0) {
×
2231
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2232
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2233
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2234
      }
2235
    }
2236
  }
2237
  
2238
_exit:
238,102✔
2239

2240
  if (code) {
238,102✔
2241
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2242
  }
2243

2244
  return code;
238,102✔
2245
}
2246

2247
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
246,122✔
2248
  int32_t code = TSDB_CODE_SUCCESS;
246,122✔
2249
  int32_t lino = 0;
246,122✔
2250
  int64_t streamId = pStream->pCreate->streamId;
246,122✔
2251
  SStmStatus info = {0};
246,122✔
2252

2253
  if (NULL == pStatus) {
246,122✔
2254
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
238,102✔
2255

2256
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
238,102✔
2257

2258
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
238,102✔
2259
  }
2260
  
2261
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
246,122✔
2262

2263
  mstLogSStmStatus("stream deployed", streamId, pStatus);
246,122✔
2264

2265
_exit:
246,122✔
2266

2267
  if (code) {
246,122✔
2268
    if (NULL != pStatus) {
×
2269
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2270
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2271
    }
2272
    
2273
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2274
  }
2275

2276
  return code;
246,122✔
2277
}
2278

2279

2280
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
9,259✔
2281
  int32_t code = TSDB_CODE_SUCCESS;
9,259✔
2282
  void* pIter = NULL;
9,259✔
2283

2284
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
18,994✔
2285
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
9,735✔
2286
    (void)mstWaitLock(&pVg->lock, false);
9,735✔
2287

2288
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
9,735✔
2289
    if (atomic_load_32(&pVg->deployed) == taskNum) {
9,735✔
2290
      taosWUnLockLatch(&pVg->lock);
×
2291
      continue;
×
2292
    }
2293

2294
    for (int32_t i = 0; i < taskNum; ++i) {
40,914✔
2295
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
31,179✔
2296
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
31,179✔
2297
        continue;
30,497✔
2298
      }
2299

2300
      mstDestroySStmTaskToDeployExt(pExt);
682✔
2301
      pExt->deployed = true;
682✔
2302
    }
2303
    
2304
    taosWUnLockLatch(&pVg->lock);
9,735✔
2305
  }
2306

2307
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
15,697✔
2308
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
6,438✔
2309
    (void)mstWaitLock(&pSnode->lock, false);
6,438✔
2310

2311
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
6,438✔
2312
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
6,438✔
2313
      for (int32_t i = 0; i < taskNum; ++i) {
18,656✔
2314
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
12,881✔
2315
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
12,881✔
2316
          continue;
12,532✔
2317
        }
2318
        
2319
        mstDestroySStmTaskToDeployExt(pExt);
349✔
2320
        pExt->deployed = true;
349✔
2321
      }
2322
    }
2323

2324
    taskNum = taosArrayGetSize(pSnode->runnerList);
6,438✔
2325
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
6,438✔
2326
      for (int32_t i = 0; i < taskNum; ++i) {
45,081✔
2327
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
38,643✔
2328
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
38,643✔
2329
          continue;
37,596✔
2330
        }
2331
        
2332
        mstDestroySStmTaskToDeployExt(pExt);
1,047✔
2333
        pExt->deployed = true;
1,047✔
2334
      }
2335
    }
2336

2337
    taosWUnLockLatch(&pSnode->lock);
6,438✔
2338
  }
2339

2340
  
2341
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
25,811✔
2342
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
16,552✔
2343
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
16,552✔
2344
    if (TSDB_CODE_SUCCESS == code) {
16,552✔
2345
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
10,364✔
2346
    }
2347
  }
2348

2349
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
41,891✔
2350
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
32,632✔
2351
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
32,632✔
2352
    if (TSDB_CODE_SUCCESS == code) {
32,632✔
2353
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
14,294✔
2354
    }
2355
  }
2356

2357
  size_t keyLen = 0;
9,259✔
2358
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
660,802✔
2359
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
651,543✔
2360
    if (*pStreamId == streamId) {
651,543✔
2361
      int64_t taskId = *(pStreamId + 1);
55,399✔
2362
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
55,399✔
2363
      if (code) {
55,399✔
2364
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2365
      } else {
2366
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
55,399✔
2367
      }
2368
    }
2369
  }
2370

2371
  if (fromStreamMap) {
9,259✔
2372
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
576✔
2373
    if (code) {
576✔
2374
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2375
    } else {
2376
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
576✔
2377
    }
2378
  }
2379
  
2380
  return code;
9,259✔
2381
}
2382

2383
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
8,683✔
2384
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
8,683✔
2385
  
2386
  (void)msmSTRemoveStream(streamId, false);  
8,683✔
2387

2388
  mstResetSStmStatus(pStatus);
8,683✔
2389

2390
  pStatus->deployTimes++;
8,683✔
2391
}
8,683✔
2392

2393
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
249,658✔
2394
  int32_t code = TSDB_CODE_SUCCESS;
249,658✔
2395
  int32_t lino = 0;
249,658✔
2396
  int64_t streamId = pAction->streamId;
249,658✔
2397
  char* streamName = pAction->streamName;
249,658✔
2398
  SStreamObj* pStream = NULL;
249,658✔
2399
  int8_t stopped = 0;
249,658✔
2400

2401
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
249,658✔
2402
  if (pStatus) {
249,658✔
2403
    stopped = atomic_load_8(&pStatus->stopped);
8,683✔
2404
    if (0 == stopped) {
8,683✔
2405
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
1,012✔
2406
      msmResetStreamForRedeploy(streamId, pStatus);
1,012✔
2407
    } else {
2408
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
7,671✔
2409
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2410
        return code;
×
2411
      }
2412
      
2413
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
7,671✔
2414
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
7,671✔
2415
        msmResetStreamForRedeploy(streamId, pStatus);
7,671✔
2416
      }
2417
    }
2418
  }
2419

2420
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
249,658✔
2421
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
249,658✔
2422
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
2,705✔
2423
    return TSDB_CODE_SUCCESS;
2,705✔
2424
  }
2425

2426
  TAOS_CHECK_EXIT(code);
246,953✔
2427

2428
  if (pStatus && pStream->pCreate->streamId != streamId) {
246,953✔
2429
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2430
    atomic_store_8(&pStatus->stopped, 2);
×
2431
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2432
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2433
  }
2434

2435
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
246,953✔
2436
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
246,953✔
2437
  if (userStopped || userDropped) {
246,953✔
2438
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
831✔
2439
    goto _exit;
831✔
2440
  }
2441
  
2442
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
246,122✔
2443

2444
_exit:
246,953✔
2445

2446
  mndReleaseStream(pCtx->pMnode, pStream);
246,953✔
2447

2448
  if (code) {
246,953✔
2449
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2450
  }
2451

2452
  return code;
246,953✔
2453
}
2454

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

2487
_exit:
1,567✔
2488

2489
  if (code) {
1,567✔
2490
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2491
  }
2492

2493
  return code;
1,567✔
2494
}
2495

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

2527
_exit:
2528

2529
  if (code) {
2530
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2531
  }
2532

2533
  return code;
2534
}
2535
*/
2536

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

2586
_exit:
×
2587

2588
  if (code) {
×
2589
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2590
  }
2591

2592
  return code;
×
2593
}
2594

2595

2596
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
1,567✔
2597
  int32_t code = TSDB_CODE_SUCCESS;
1,567✔
2598
  int32_t lino = 0;
1,567✔
2599
  int64_t streamId = pAction->streamId;
1,567✔
2600
  int64_t taskId = pAction->id.taskId;
1,567✔
2601
  SStreamObj* pStream = NULL;
1,567✔
2602

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

2605
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,567✔
2606
  if (NULL == pStatus) {
1,567✔
2607
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2608
    return TSDB_CODE_SUCCESS;
×
2609
  }
2610

2611
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,567✔
2612
  if (stopped) {
1,567✔
UNCOV
2613
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
UNCOV
2614
    return TSDB_CODE_SUCCESS;
×
2615
  }
2616

2617
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,567✔
2618
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,567✔
2619
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2620
    return TSDB_CODE_SUCCESS;
×
2621
  }
2622

2623
  TAOS_CHECK_EXIT(code);
1,567✔
2624

2625
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,567✔
2626
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,567✔
2627
  if (userStopped || userDropped) {
1,567✔
2628
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2629
    goto _exit;
×
2630
  }
2631

2632
  switch (pAction->type) {
1,567✔
2633
    case STREAM_READER_TASK:
1,567✔
2634
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,567✔
2635
      break;
1,567✔
2636
/*
2637
    case STREAM_TRIGGER_TASK:
2638
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2639
      break;
2640
*/
2641
    case STREAM_RUNNER_TASK:
×
2642
      if (pAction->multiRunner) {
×
2643
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
×
2644
      } else {
2645
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
2646
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2647
      }
2648
      break;
×
2649
    default:
×
2650
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2651
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2652
      break;
×
2653
  }
2654

2655
_exit:
1,567✔
2656

2657
  if (pStream) {
1,567✔
2658
    mndReleaseStream(pCtx->pMnode, pStream);
1,567✔
2659
  }
2660

2661
  if (code) {
1,567✔
2662
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2663
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2664
  }
2665

2666
  return code;
1,567✔
2667
}
2668

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

2689
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
2690
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2691
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2692
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2693
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2694
        for (int32_t i = 0; i < taskNum; ++i) {
×
2695
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2696
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2697
            pExt->deployed = true;
×
2698
          }
2699
        }
2700
      }
2701

2702
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
2703
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2704
        for (int32_t i = 0; i < taskNum; ++i) {
×
2705
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2706
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2707
            pExt->deployed = true;
×
2708
          }
2709
        }
2710
      }
2711
    }
2712
  }
2713

2714
  return TSDB_CODE_SUCCESS;
×
2715
}
2716

2717
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
576✔
2718
  int32_t code = TSDB_CODE_SUCCESS;
576✔
2719
  int32_t lino = 0;
576✔
2720

2721
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
576✔
2722

2723
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
576✔
2724

2725
_exit:
576✔
2726

2727
  if (code) {
576✔
2728
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2729
  } else {
2730
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
576✔
2731
  }
2732

2733
  return code;
576✔
2734
}
2735

2736
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
42,832✔
2737
  int32_t code = TSDB_CODE_SUCCESS;
42,832✔
2738
  int32_t lino = 0;
42,832✔
2739

2740
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
42,832✔
2741
  if (0 == active || MND_STM_STATE_NORMAL != state) {
42,832✔
2742
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
357✔
2743
    return;
357✔
2744
  }
2745

2746
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
42,475✔
2747
  if (NULL == pStream) {
42,475✔
2748
    mstsInfo("stream %s already not in streamMap", streamName);
2,247✔
2749
    goto _exit;
2,247✔
2750
  }
2751

2752
  atomic_store_8(&pStream->stopped, 2);
40,228✔
2753

2754
  mstsInfo("set stream %s stopped by user", streamName);
40,228✔
2755

2756
_exit:
×
2757

2758
  taosHashRelease(mStreamMgmt.streamMap, pStream);
42,475✔
2759

2760
  if (code) {
42,475✔
2761
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2762
  }
2763

2764
  return;
42,475✔
2765
}
2766

2767
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,984✔
2768
  int32_t code = TSDB_CODE_SUCCESS;
3,984✔
2769
  int32_t lino = 0;
3,984✔
2770

2771
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,984✔
2772
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,984✔
2773
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2774
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2775
  }
2776

2777
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,984✔
2778
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,984✔
2779
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2780
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2781
    goto _exit;
×
2782
  }
2783

2784
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,984✔
2785

2786
_exit:
3,984✔
2787

2788
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,984✔
2789

2790
  if (code) {
3,984✔
2791
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2792
  }
2793

2794
  return code;
3,984✔
2795
}
2796

2797
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
76,146✔
2798
  int32_t code = TSDB_CODE_SUCCESS;
76,146✔
2799
  int32_t lino = 0;
76,146✔
2800
  SStmQNode* pQNode = NULL;
76,146✔
2801

2802
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
327,371✔
2803
    switch (pQNode->type) {
251,225✔
2804
      case STREAM_ACT_DEPLOY:
251,225✔
2805
        if (pQNode->streamAct) {
251,225✔
2806
          mstDebug("start to handle stream deploy action");
249,658✔
2807
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
249,658✔
2808
        } else {
2809
          mstDebug("start to handle task deploy action");
1,567✔
2810
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,567✔
2811
        }
2812
        break;
251,225✔
2813
      default:
×
2814
        break;
×
2815
    }
2816
  }
2817

2818
_exit:
76,146✔
2819

2820
  if (code) {
76,146✔
2821
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2822
  }
2823
}
76,146✔
2824

2825
void msmStopAllStreamsByGrant(int32_t errCode) {
×
2826
  SStmStatus* pStatus = NULL;
×
2827
  void* pIter = NULL;
×
2828
  int64_t streamId = 0;
×
2829
  
2830
  while (true) {
2831
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
2832
    if (NULL == pIter) {
×
2833
      break;
×
2834
    }
2835

2836
    pStatus = (SStmStatus*)pIter;
×
2837

2838
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2839
    atomic_store_8(&pStatus->stopped, 4);
×
2840

2841
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2842
  }
2843
}
×
2844

2845
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2846
  mstInfo("stream grant expired");
×
2847

2848
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2849
    mstWarn("mnode stream is NOT active, ignore handling");
×
2850
    return errCode;
×
2851
  }
2852

2853
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2854

2855
  msmStopAllStreamsByGrant(errCode);
×
2856

2857
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2858
  
2859
  return errCode;
×
2860
}
2861

2862
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,892,418✔
2863
  int32_t code = TSDB_CODE_SUCCESS;
1,892,418✔
2864
  int32_t lino = 0;
1,892,418✔
2865
  int64_t streamId = pDeploy->task.streamId;
1,892,418✔
2866
  
2867
  switch (pDeploy->task.type) {
1,892,418✔
2868
    case STREAM_READER_TASK:
796,334✔
2869
      if (NULL == pStream->readerTasks) {
796,334✔
2870
        pStream->streamId = streamId;
310,743✔
2871
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
310,743✔
2872
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
310,743✔
2873
      }
2874
      
2875
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,592,668✔
2876
      break;
796,334✔
2877
    case STREAM_TRIGGER_TASK:
245,773✔
2878
      pStream->streamId = streamId;
245,773✔
2879
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
245,773✔
2880
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
245,773✔
2881
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
245,773✔
2882
      break;
245,773✔
2883
    case STREAM_RUNNER_TASK:
850,311✔
2884
      if (NULL == pStream->runnerTasks) {
850,311✔
2885
        pStream->streamId = streamId;
250,888✔
2886
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
250,888✔
2887
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
250,888✔
2888
      }      
2889
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,700,622✔
2890
      break;
850,311✔
2891
    default:
×
2892
      break;
×
2893
  }
2894

2895
_exit:
1,892,418✔
2896

2897
  if (code) {
1,892,418✔
2898
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2899
  }
2900

2901
  return code;
1,892,418✔
2902
}
2903

2904
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,892,418✔
2905
  int32_t code = TSDB_CODE_SUCCESS;
1,892,418✔
2906
  int32_t lino = 0;
1,892,418✔
2907
  int64_t streamId = pDeploy->task.streamId;
1,892,418✔
2908
  SStreamTask* pTask = &pDeploy->task;
1,892,418✔
2909
  SStmStreamDeploy streamDeploy = {0};
1,892,418✔
2910
  SStmStreamDeploy* pStream = NULL;
1,892,418✔
2911
   
2912
  while (true) {
2913
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,892,418✔
2914
    if (NULL == pStream) {
1,892,418✔
2915
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
322,959✔
2916
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
322,959✔
2917
      if (TSDB_CODE_SUCCESS == code) {
322,959✔
2918
        goto _exit;
322,959✔
2919
      }
2920

2921
      if (TSDB_CODE_DUP_KEY != code) {
×
2922
        goto _exit;
×
2923
      }    
2924

2925
      tFreeSStmStreamDeploy(&streamDeploy);
×
2926
      continue;
×
2927
    }
2928

2929
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,569,459✔
2930
    
2931
    break;
1,569,459✔
2932
  }
2933
  
2934
_exit:
1,892,418✔
2935

2936
  taosHashRelease(pHash, pStream);
1,892,418✔
2937

2938
  if (code) {
1,892,418✔
2939
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2940
  } else {
2941
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,892,418✔
2942
  }
2943

2944
  return code;
1,892,418✔
2945
}
2946

2947

2948
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
346,078✔
2949
  int32_t code = TSDB_CODE_SUCCESS;
346,078✔
2950
  int32_t lino = 0;
346,078✔
2951
  int32_t taskNum = taosArrayGetSize(pTasks);
346,078✔
2952

2953
  for (int32_t i = 0; i < taskNum; ++i) {
2,240,574✔
2954
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,894,496✔
2955
    if (pExt->deployed) {
1,894,496✔
2956
      continue;
2,078✔
2957
    }
2958

2959
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,892,418✔
2960
    pExt->deployed = true;
1,892,418✔
2961

2962
    (void)atomic_add_fetch_32(deployed, 1);
1,892,418✔
2963
  }
2964

2965
_exit:
346,078✔
2966

2967
  if (code) {
346,078✔
2968
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2969
  }
2970

2971
  return code;
346,078✔
2972
}
2973

2974
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
102,948✔
2975
  int32_t code = TSDB_CODE_SUCCESS;
102,948✔
2976
  int32_t lino = 0;
102,948✔
2977
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
102,948✔
2978
  SStmVgTasksToDeploy* pVg = NULL;
102,948✔
2979
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2980

2981
  mstDebug("start to add stream vgroup tasks deploy");
102,948✔
2982
  
2983
  for (int32_t i = 0; i < vgNum; ++i) {
619,832✔
2984
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
516,884✔
2985

2986
    msmUpdateVgroupUpTs(pCtx, *vgId);
516,884✔
2987

2988
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
516,884✔
2989
    if (NULL == pVg) {
516,884✔
2990
      continue;
327,173✔
2991
    }
2992

2993
    if (taosWTryLockLatch(&pVg->lock)) {
189,711✔
UNCOV
2994
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
UNCOV
2995
      pVg = NULL;
×
UNCOV
2996
      continue;
×
2997
    }
2998

2999
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
189,711✔
3000
      taosWUnLockLatch(&pVg->lock);
32✔
3001
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
32✔
3002
      pVg = NULL;
32✔
3003
      continue;
32✔
3004
    }
3005

3006
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
189,679✔
3007
    taosWUnLockLatch(&pVg->lock);
189,679✔
3008
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
189,679✔
3009
    pVg = NULL;
189,679✔
3010
  }
3011

3012
_exit:
102,948✔
3013

3014
  if (code) {
102,948✔
3015
    if (pVg) {
×
3016
      taosWUnLockLatch(&pVg->lock);
×
3017
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3018
    }
3019

3020
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3021
  }
3022

3023
  return code;
102,948✔
3024
}
3025

3026
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
84,147✔
3027
  int32_t code = TSDB_CODE_SUCCESS;
84,147✔
3028
  int32_t lino = 0;
84,147✔
3029
  SStmSnodeTasksDeploy* pSnode = NULL;
84,147✔
3030
  SStreamHbMsg* pReq = pCtx->pReq;
84,147✔
3031

3032
  mstDebug("start to add stream snode tasks deploy");
84,147✔
3033
  
3034
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
84,147✔
3035
  if (NULL == pSnode) {
84,147✔
3036
    return TSDB_CODE_SUCCESS;
3,881✔
3037
  }
3038

3039
  (void)mstWaitLock(&pSnode->lock, false);
80,266✔
3040
  
3041
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
80,266✔
3042
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
76,133✔
3043
  }
3044

3045
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
80,266✔
3046
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
80,266✔
3047
  }
3048
  
3049
  taosWUnLockLatch(&pSnode->lock);
80,266✔
3050

3051
_exit:
80,266✔
3052

3053
  if (code) {
80,266✔
3054
    if (pSnode) {
×
3055
      taosWUnLockLatch(&pSnode->lock);
×
3056
    }
3057

3058
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3059
  }
3060

3061
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
80,266✔
3062
  return code;
80,266✔
3063
}
3064

3065
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
852,487✔
3066
  int32_t code = TSDB_CODE_SUCCESS;
852,487✔
3067
  int32_t lino = 0;
852,487✔
3068
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
852,487✔
3069
  if (NULL == pStatus) {
852,487✔
UNCOV
3070
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
3071
    return TSDB_CODE_SUCCESS;
×
3072
  }
3073
  
3074
  pStatus->lastActionTs = currTs;
852,487✔
3075

3076
_exit:
852,487✔
3077

3078
  if (code) {
852,487✔
3079
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3080
  }
3081

3082
  return code;
852,487✔
3083
}
3084

3085
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
98,904✔
3086
  int32_t code = TSDB_CODE_SUCCESS;
98,904✔
3087
  int32_t lino = 0;
98,904✔
3088
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
98,904✔
3089
  void* pIter = NULL;
98,904✔
3090

3091
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
98,904✔
3092
  
3093
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
98,904✔
3094
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
98,904✔
3095

3096
  while (1) {
322,959✔
3097
    pIter = taosHashIterate(pCtx->deployStm, pIter);
421,863✔
3098
    if (pIter == NULL) {
421,863✔
3099
      break;
98,904✔
3100
    }
3101
    
3102
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
322,959✔
3103
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
645,918✔
3104

3105
    int64_t streamId = pDeploy->streamId;
322,959✔
3106
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
322,959✔
3107
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
3108

3109
    mstClearSStmStreamDeploy(pDeploy);
322,959✔
3110
    
3111
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
322,959✔
3112
  }
3113
  
3114
_exit:
98,904✔
3115

3116
  if (pIter) {
98,904✔
3117
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3118
  }
3119

3120
  if (code) {
98,904✔
3121
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3122
  }
3123
  
3124
  return code;
98,904✔
3125
}
3126

3127
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
102,948✔
3128
  int32_t code = TSDB_CODE_SUCCESS;
102,948✔
3129
  int32_t lino = 0;
102,948✔
3130
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
102,948✔
3131
  SStmVgTasksToDeploy* pVg = NULL;
102,948✔
3132
  
3133
  for (int32_t i = 0; i < vgNum; ++i) {
619,832✔
3134
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
516,884✔
3135
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
516,884✔
3136
    if (NULL == pVg) {
516,884✔
3137
      continue;
327,173✔
3138
    }
3139

3140
    if (taosWTryLockLatch(&pVg->lock)) {
189,711✔
3141
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3142
      continue;
×
3143
    }
3144
    
3145
    if (atomic_load_32(&pVg->deployed) <= 0) {
189,711✔
3146
      taosWUnLockLatch(&pVg->lock);
32✔
3147
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
32✔
3148
      continue;
32✔
3149
    }
3150

3151
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
189,679✔
3152
    if (atomic_load_32(&pVg->deployed) == taskNum) {
189,679✔
3153
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
189,362✔
3154
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
189,362✔
3155
      pVg->taskList = NULL;
189,362✔
3156
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
189,362✔
3157
      taosWUnLockLatch(&pVg->lock);
189,362✔
3158
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
189,362✔
3159
      continue;
189,362✔
3160
    }
3161

3162
    for (int32_t m = taskNum - 1; m >= 0; --m) {
1,681✔
3163
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
1,364✔
3164
      if (!pExt->deployed) {
1,364✔
3165
        continue;
×
3166
      }
3167

3168
      mstDestroySStmTaskToDeployExt(pExt);
1,364✔
3169

3170
      taosArrayRemove(pVg->taskList, m);
1,364✔
3171
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
1,364✔
3172
    }
3173
    atomic_store_32(&pVg->deployed, 0);
317✔
3174
    taosWUnLockLatch(&pVg->lock);
317✔
3175
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
317✔
3176
  }
3177

3178
_exit:
102,948✔
3179

3180
  if (code) {
102,948✔
3181
    if (pVg) {
×
3182
      taosWUnLockLatch(&pVg->lock);
×
3183
    }
3184

3185
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3186
  }
3187
}
102,948✔
3188

3189
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
91,231✔
3190
  if (!GOT_SNODE(snodeId)) {
91,231✔
3191
    return;
7,084✔
3192
  }
3193
  
3194
  int32_t code = TSDB_CODE_SUCCESS;
84,147✔
3195
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
84,147✔
3196
  if (NULL == pSnode) {
84,147✔
3197
    return;
3,881✔
3198
  }
3199

3200
  if (taosWTryLockLatch(&pSnode->lock)) {
80,266✔
3201
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3202
    return;
×
3203
  }
3204

3205
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
80,266✔
3206
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
80,266✔
3207
  
3208
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
80,266✔
3209
    taosWUnLockLatch(&pSnode->lock);
×
3210
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3211
    return;
×
3212
  }
3213

3214
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
80,266✔
3215
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
79,965✔
3216
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
79,965✔
3217
    pSnode->triggerList = NULL;
79,965✔
3218
  }
3219

3220
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
80,266✔
3221
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
79,965✔
3222
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
79,965✔
3223
    pSnode->runnerList = NULL;
79,965✔
3224
  }
3225

3226
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
80,266✔
3227
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
79,965✔
3228
    taosWUnLockLatch(&pSnode->lock);
79,965✔
3229
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
79,965✔
3230
    return;
79,965✔
3231
  }
3232

3233
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
301✔
3234
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
999✔
3235
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
698✔
3236
      if (!pExt->deployed) {
698✔
3237
        continue;
×
3238
      }
3239

3240
      mstDestroySStmTaskToDeployExt(pExt);
698✔
3241
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
698✔
3242
      taosArrayRemove(pSnode->triggerList, m);
698✔
3243
    }
3244
    
3245
    pSnode->triggerDeployed = 0;
301✔
3246
  }
3247

3248
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
301✔
3249
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
2,395✔
3250
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
2,094✔
3251
      if (!pExt->deployed) {
2,094✔
3252
        continue;
×
3253
      }
3254

3255
      mstDestroySStmTaskToDeployExt(pExt);
2,094✔
3256
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2,094✔
3257
      taosArrayRemove(pSnode->runnerList, m);
2,094✔
3258
    }
3259
    
3260
    pSnode->runnerDeployed = 0;
301✔
3261
  }
3262
  
3263
  taosWUnLockLatch(&pSnode->lock);
301✔
3264
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
301✔
3265
}
3266

3267
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
20,012,659✔
3268
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
20,012,659✔
3269
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
102,948✔
3270
  }
3271

3272
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
20,012,659✔
3273
    msmCleanDeployedSnodeTasks(pHb->snodeId);
91,231✔
3274
  }
3275
}
20,012,659✔
3276

3277
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
20,012,659✔
3278
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
20,012,659✔
3279
  if (mStreamMgmt.tCtx) {
20,012,659✔
3280
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
19,918,839✔
3281
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
19,918,839✔
3282
  }
3283
}
20,012,659✔
3284

3285
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
241,174✔
3286
  int32_t code = TSDB_CODE_SUCCESS;
241,174✔
3287
  int32_t lino = 0;
241,174✔
3288
  int32_t action = STREAM_ACT_START;
241,174✔
3289
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
241,174✔
3290
  if (pAction) {
241,174✔
3291
    pAction->actions |= action;
×
3292
    pAction->start.triggerId = *pId;
×
3293
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3294
  } else {
3295
    SStmAction newAction = {0};
241,174✔
3296
    newAction.actions = action;
241,174✔
3297
    newAction.start.triggerId = *pId;
241,174✔
3298
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
241,174✔
3299
    mstsDebug("stream add START action, actions:%x", newAction.actions);
241,174✔
3300
  }
3301

3302
_exit:
241,174✔
3303

3304
  if (code) {
241,174✔
3305
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3306
  }
3307

3308
  return code;
241,174✔
3309
}
3310

3311
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
3312
  int32_t code = TSDB_CODE_SUCCESS;
×
3313
  int32_t lino = 0;
×
3314
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3315
  
3316
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
3317
  if (pAction) {
×
3318
    pAction->actions |= action;
×
3319
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3320
  } else {
3321
    SStmAction newAction = {0};
×
3322
    newAction.actions = action;
×
3323
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3324
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3325
  }
3326

3327
_exit:
×
3328

3329
  if (code) {
×
3330
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3331
  }
3332

3333
  return code;
×
3334
}
3335

3336

3337

3338
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
288,354✔
3339
  int32_t code = TSDB_CODE_SUCCESS;
288,354✔
3340
  int32_t lino = 0;
288,354✔
3341
  int32_t action = STREAM_ACT_UNDEPLOY;
288,354✔
3342
  bool    dropped = false;
288,354✔
3343

3344
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
288,354✔
3345
  mstsDebug("stream dropped: %d", dropped);
288,354✔
3346
  
3347
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
288,354✔
3348
  if (pAction) {
288,354✔
3349
    pAction->actions |= action;
231,811✔
3350
    if (NULL == pAction->undeploy.taskList) {
231,811✔
3351
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3352
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3353
    }
3354

3355
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
463,622✔
3356
    if (pAction->undeploy.doCheckpoint) {
231,811✔
3357
      pAction->undeploy.doCheckpoint = dropped ? false : true;
34,764✔
3358
    }
3359
    if (!pAction->undeploy.doCleanup) {
231,811✔
3360
      pAction->undeploy.doCleanup = dropped ? true : false;
34,764✔
3361
    }
3362
    
3363
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
231,811✔
3364
  } else {
3365
    SStmAction newAction = {0};
56,543✔
3366
    newAction.actions = action;
56,543✔
3367
    newAction.undeploy.doCheckpoint = dropped ? false : true;
56,543✔
3368
    newAction.undeploy.doCleanup = dropped ? true : false;
56,543✔
3369
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
56,543✔
3370
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
56,543✔
3371
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
113,086✔
3372
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
56,543✔
3373
    
3374
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
56,543✔
3375
  }
3376

3377
_exit:
288,354✔
3378

3379
  if (code) {
288,354✔
3380
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3381
  }
3382

3383
  return code;
288,354✔
3384
}
3385

3386
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,984✔
3387
  int32_t code = TSDB_CODE_SUCCESS;
3,984✔
3388
  int32_t lino = 0;
3,984✔
3389
  int32_t action = STREAM_ACT_RECALC;
3,984✔
3390
  SStmAction newAction = {0};
3,984✔
3391
  
3392
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,984✔
3393
  if (pAction) {
3,984✔
3394
    pAction->actions |= action;
×
3395
    pAction->recalc.recalcList = recalcList;
×
3396

3397
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3398
  } else {
3399
    newAction.actions = action;
3,984✔
3400
    newAction.recalc.recalcList = recalcList;
3,984✔
3401
    
3402
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,984✔
3403
    
3404
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,984✔
3405
  }
3406

3407
_exit:
3,984✔
3408

3409
  if (code) {
3,984✔
3410
    mstDestroySStmAction(&newAction);
×
3411
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3412
  }
3413

3414
  return code;
3,984✔
3415
}
3416

3417
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
332,060✔
3418
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
332,060✔
3419
  if (NULL == pStream) {
332,060✔
3420
    return false;
×
3421
  }
3422

3423
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
332,060✔
3424
    return false;
×
3425
  }
3426

3427
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
332,060✔
3428
  for (int32_t i = 0; i < readerNum; ++i) {
732,968✔
3429
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
400,908✔
3430
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
400,908✔
3431
      return false;
×
3432
    }
3433
  }
3434

3435
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
332,060✔
3436
  for (int32_t i = 0; i < readerNum; ++i) {
332,060✔
3437
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
×
3438
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
×
3439
      return false;
×
3440
    }
3441
  }
3442

3443
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
332,060✔
3444
  SListNode* pNode = listHead(pStream->calcReaders);
332,060✔
3445
  for (int32_t i = 0; i < readerNum; ++i) {
986,163✔
3446
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
654,103✔
3447
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
654,103✔
3448
      return false;
×
3449
    }
3450
    pNode = TD_DLIST_NODE_NEXT(pNode);
654,103✔
3451
  }
3452

3453
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,044,881✔
3454
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
803,707✔
3455
    for (int32_t m = 0; m < runnerNum; ++m) {
1,638,205✔
3456
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
925,384✔
3457
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
925,384✔
3458
        return false;
90,886✔
3459
      }
3460
    }
3461
  }
3462
  
3463
  return true;
241,174✔
3464
}
3465

3466

3467
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
17,140,469✔
3468
  int32_t code = TSDB_CODE_SUCCESS;
17,140,469✔
3469
  int32_t lino = 0;
17,140,469✔
3470
  int32_t action = 0;
17,140,469✔
3471
  int64_t streamId = pMsg->streamId;
17,140,469✔
3472
  SStreamTask* pTask = (SStreamTask*)pMsg;
17,140,469✔
3473
  int8_t  stopped = 0;
17,140,469✔
3474

3475
  msttDebug("start to handle task abnormal status %d", pTask->status);
17,140,469✔
3476
  
3477
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
17,140,469✔
3478
  if (NULL == pStatus) {
17,140,469✔
3479
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3480
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3481
    return;
13,428✔
3482
  }
3483

3484
  stopped = atomic_load_8(&pStatus->stopped);
17,140,469✔
3485
  if (stopped) {
17,140,469✔
3486
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3487
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3488
    return;
×
3489
  }
3490
  
3491
  switch (pMsg->status) {
17,140,469✔
3492
    case STREAM_STATUS_INIT:      
17,132,503✔
3493
      if (STREAM_TRIGGER_TASK != pMsg->type) {
17,132,503✔
3494
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
16,651,944✔
3495
        return;
16,651,944✔
3496
      }
3497
      
3498
      if (INT64_MIN == pStatus->lastActionTs) {
480,559✔
3499
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3500
        return;
×
3501
      }
3502
      
3503
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
480,559✔
3504
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
148,499✔
3505
        return;
148,499✔
3506
      }
3507

3508
      if (STREAM_IS_RUNNING(pStatus)) {
332,060✔
3509
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3510
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
332,060✔
3511
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
241,174✔
3512
      }
3513
      break;
332,060✔
3514
    case STREAM_STATUS_FAILED:
7,966✔
3515
      //STREAMTODO ADD ERRCODE HANDLE
3516
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
7,966✔
3517
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
6,901✔
3518
                 pMsg->taskIdx);
3519
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
6,901✔
3520
      } else {
3521
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
1,065✔
3522
                 pMsg->taskIdx);
3523
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
1,065✔
3524
      }
3525
      break;
7,966✔
3526
    default:
×
3527
      break;
×
3528
  }
3529

3530
_exit:
340,026✔
3531

3532
  if (code) {
340,026✔
3533
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3534
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3535
  }
3536
}
3537

3538
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
287,289✔
3539
  int32_t code = TSDB_CODE_SUCCESS;
287,289✔
3540
  int32_t lino = 0;
287,289✔
3541
  SStreamTask* pTask = (SStreamTask*)pStatus;
287,289✔
3542
  int64_t streamId = pStatus->streamId;
287,289✔
3543

3544
  msttInfo("start to handle task error, type: %d", err);
287,289✔
3545

3546
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
287,289✔
3547

3548
_exit:
287,289✔
3549

3550
  if (code) {
287,289✔
3551
    // IGNORE STOP STREAM BY ERROR  
3552
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3553
  }
3554
}
287,289✔
3555

3556
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
7,185,169✔
3557
  int32_t code = TSDB_CODE_SUCCESS;
7,185,169✔
3558
  int32_t lino = 0;
7,185,169✔
3559
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
7,185,169✔
3560

3561
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
7,185,169✔
3562
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3563
  }
3564
  
3565
  SArray* userRecalcList = NULL;
7,185,169✔
3566
  if (atomic_load_ptr(&pStream->userRecalcList)) {
7,185,169✔
3567
    taosWLockLatch(&pStream->userRecalcLock);
3,984✔
3568
    if (pStream->userRecalcList) {
3,984✔
3569
      userRecalcList = pStream->userRecalcList;
3,984✔
3570
      pStream->userRecalcList = NULL;
3,984✔
3571
    }
3572
    taosWUnLockLatch(&pStream->userRecalcLock);
3,984✔
3573
    
3574
    if (userRecalcList) {
3,984✔
3575
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,984✔
3576
    }
3577
  }
3578

3579
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
7,185,169✔
3580
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
3,588,102✔
3581
    if (NULL == pStatus->detailStatus) {
3,588,102✔
3582
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
242,128✔
3583
      if (NULL == pStatus->detailStatus) {
242,128✔
3584
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3585
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3586
      }
3587
    }
3588
    
3589
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
3,588,102✔
3590
    taosWUnLockLatch(&pStatus->detailStatusLock);
3,588,102✔
3591
  }
3592

3593
_exit:
3,597,067✔
3594

3595
  if (code) {
7,185,169✔
3596
    // IGNORE STOP STREAM BY ERROR
3597
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3598
  }
3599
}
7,185,169✔
3600

3601
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,746,355✔
3602
  int32_t code = TSDB_CODE_SUCCESS;
1,746,355✔
3603
  int32_t lino = 0;
1,746,355✔
3604
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,746,355✔
3605

3606
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,746,355✔
3607

3608
  for (int32_t i = 0; i < num; ++i) {
60,026,521✔
3609
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
58,280,166✔
3610
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
58,280,166✔
3611
    
3612
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
58,280,166✔
3613
    if (NULL == ppStatus) {
58,280,166✔
3614
      msttWarn("task no longer exists in taskMap, streamId:0x%" PRIx64 " taskId:0x%" PRIx64 " type:%s status:%s taskIdx:%d",
1,854✔
3615
          (uint64_t)pTask->streamId, (uint64_t)pTask->taskId, gStreamTaskTypeStr[pTask->type], gStreamStatusStr[pTask->status], pTask->taskIdx);
3616
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
1,854✔
3617
      continue;
1,854✔
3618
    }
3619

3620
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
58,278,312✔
3621
    int8_t stopped = atomic_load_8(&pStream->stopped);
58,278,312✔
3622
    if (stopped) {
58,278,312✔
3623
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
285,435✔
3624
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
285,435✔
3625
      continue;
285,435✔
3626
    }
3627

3628
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
57,992,877✔
3629
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3630
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3631
          
3632
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3633
      continue;
×
3634
    }
3635

3636
    if ((*ppStatus)->status != pTask->status) {
57,992,877✔
3637
      if (STREAM_STATUS_RUNNING == pTask->status) {
3,155,835✔
3638
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,352,613✔
3639
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,803,222✔
3640
        if (pStream->triggerTask) {
×
3641
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3642
        }
3643
        
3644
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3645
      }
3646

3647
      if (STREAM_TRIGGER_TASK == pTask->type && STREAM_STATUS_RUNNING == pTask->status) {
3,155,835✔
3648
        mstInfo("streamId:0x%" PRIx64 " trigger task status updated to Running in streamMap", (uint64_t)pTask->streamId);
240,837✔
3649
      }
3650
    }
3651
    
3652
    (*ppStatus)->errCode = pTask->errorCode;
57,992,877✔
3653
    (*ppStatus)->status = pTask->status;
57,992,877✔
3654
    (*ppStatus)->lastUpTs = pCtx->currTs;
57,992,877✔
3655
    
3656
    if (STREAM_STATUS_RUNNING != pTask->status) {
57,992,877✔
3657
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
17,140,469✔
3658
    }
3659
    
3660
    if (STREAM_TRIGGER_TASK == pTask->type) {
57,992,877✔
3661
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
7,185,169✔
3662
    }
3663
  }
3664

3665
_exit:
1,746,355✔
3666

3667
  if (code) {
1,746,355✔
3668
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3669
  }
3670

3671
  return code;
1,746,355✔
3672
}
3673

3674
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3675
  int32_t code = TSDB_CODE_SUCCESS;
×
3676
  int32_t lino = 0;
×
3677
  int64_t streamId = pTask->streamId;
×
3678
  SStreamObj* pStream = NULL;
×
3679

3680
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3681
  if (NULL == pStatus) {
×
3682
    SStmStatus status = {0};
×
3683
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3684
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3685
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
3686
      mndReleaseStream(pCtx->pMnode, pStream);
×
3687
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3688
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3689
      return code;
×
3690
    }
3691

3692
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3693
    mndReleaseStream(pCtx->pMnode, pStream);
×
3694

3695
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3696
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3697
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3698
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3699
  }
3700

3701
  SStmTaskStatus* pNewTask = NULL;
×
3702
  switch (pTask->type) {
×
3703
    case STREAM_READER_TASK: {
×
3704
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3705
      if (NULL == pList) {
×
3706
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
3707
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3708
      }
3709
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3710
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3711
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3712
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3713
            (int32_t)taosArrayGetSize(pList), readerSize);
3714
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3715
      }
3716
      
3717
      SStmTaskStatus taskStatus = {0};
×
3718
      taskStatus.pStream = pStatus;
×
3719
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3720
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3721
        pNewTask = taosArrayPush(pList, &taskStatus);
×
3722
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3723
      } else {
3724
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3725
        SListNode* pTailNode = tdListGetTail(pStatus->calcReaders);
×
3726
        QUERY_CHECK_NULL(pTailNode, code, lino, _exit, TSDB_CODE_INTERNAL_ERROR);
×
3727
        pNewTask = (SStmTaskStatus*)pTailNode->data;
×
3728
      }
3729
      
3730
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3731
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3732
      break;
×
3733
    }
3734
    case STREAM_TRIGGER_TASK: {
×
3735
      taosMemoryFreeClear(pStatus->triggerTask);
×
3736
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3737
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3738
      pStatus->triggerTask->pStream = pStatus;
×
3739
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
3740
      pNewTask = pStatus->triggerTask;
×
3741

3742
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3743
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3744
      break;
×
3745
    }
3746
    case STREAM_RUNNER_TASK:{
×
3747
      if (NULL == pStatus->runners[pTask->deployId]) {
×
3748
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3749
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3750
      }
3751
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
3752
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3753
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3754
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3755
      }    
3756
      
3757
      SStmTaskStatus taskStatus = {0};
×
3758
      taskStatus.pStream = pStatus;
×
3759
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3760
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3761
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3762

3763
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3764
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3765
      break;
×
3766
    }
3767
    default: {
×
3768
      msttError("invalid task type:%d in task status", pTask->type);
×
3769
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3770
      break;
×
3771
    }
3772
  }
3773

3774
_exit:
×
3775

3776
  if (code) {
×
3777
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3778
  } else {
3779
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3780
  }
3781

3782
  return code;
×
3783
}
3784

3785
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3786
  int32_t code = TSDB_CODE_SUCCESS;
×
3787
  int32_t lino = 0;
×
3788
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3789

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

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

3796
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3797
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3798
    }
3799
    
3800
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3801
    if (NULL == ppStatus) {
×
3802
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3803
      
3804
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3805
      
3806
      continue;
×
3807
    }
3808
    
3809
    (*ppStatus)->status = pTask->status;
×
3810
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3811
  }
3812

3813
_exit:
×
3814

3815
  if (code) {
×
3816
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3817
  }
3818

3819
  return code;
×
3820
}
3821

3822
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
241,174✔
3823
  int32_t code = TSDB_CODE_SUCCESS;
241,174✔
3824
  int32_t lino = 0;
241,174✔
3825
  if (NULL == pCtx->pRsp->start.taskList) {
241,174✔
3826
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
126,094✔
3827
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
126,094✔
3828
  }
3829

3830
  SStmTaskId* pId = &pAction->start.triggerId;
241,174✔
3831
  SStreamTaskStart start = {0};
241,174✔
3832
  start.task.type = STREAM_TRIGGER_TASK;
241,174✔
3833
  start.task.streamId = streamId;
241,174✔
3834
  start.task.taskId = pId->taskId;
241,174✔
3835
  start.task.seriousId = pId->seriousId;
241,174✔
3836
  start.task.nodeId = pId->nodeId;
241,174✔
3837
  start.task.taskIdx = pId->taskIdx;
241,174✔
3838

3839
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
482,348✔
3840
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
241,174✔
3841

3842
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
241,174✔
3843

3844
  return;
241,174✔
3845

3846
_exit:
×
3847

3848
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3849
}
3850

3851

3852
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
56,543✔
3853
  int32_t code = TSDB_CODE_SUCCESS;
56,543✔
3854
  int32_t lino = 0;
56,543✔
3855
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
56,543✔
3856
  if (NULL == pCtx->pRsp->undeploy.taskList) {
56,543✔
3857
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
40,422✔
3858
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
40,422✔
3859
  }
3860

3861
  SStreamTaskUndeploy undeploy;
56,543✔
3862
  for (int32_t i = 0; i < dropNum; ++i) {
344,897✔
3863
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
288,354✔
3864
    undeploy.task = *pTask;
288,354✔
3865
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
288,354✔
3866
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
288,354✔
3867

3868
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
576,708✔
3869
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
288,354✔
3870

3871
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
288,354✔
3872
  }
3873

3874
  return;
56,543✔
3875

3876
_exit:
×
3877

3878
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3879
}
3880

3881
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3882
  int32_t code = TSDB_CODE_SUCCESS;
×
3883
  int32_t lino = 0;
×
3884

3885
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3886
  if (NULL == pStream) {
×
3887
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3888
    return;
×
3889
  }
3890

3891
  if (NULL == pStream->triggerTask) {
×
3892
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3893
    return;
×
3894
  }
3895

3896
  SStreamMgmtRsp rsp = {0};
×
3897
  rsp.reqId = INT64_MIN;
×
3898
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3899
  rsp.task.streamId = streamId;
×
3900
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3901

3902
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3903

3904
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3905
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3906
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3907
  }
3908

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

3911
_exit:
×
3912

3913
  if (code) {
×
3914
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3915
  } else {
3916
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3917
  }
3918
}
3919

3920
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,984✔
3921
  int32_t code = TSDB_CODE_SUCCESS;
3,984✔
3922
  int32_t lino = 0;
3,984✔
3923

3924
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,984✔
3925
  if (NULL == pStream) {
3,984✔
3926
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3927
    return;
×
3928
  }
3929

3930
  if (NULL == pStream->triggerTask) {
3,984✔
3931
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3932
    return;
×
3933
  }
3934

3935
  SStreamMgmtRsp rsp = {0};
3,984✔
3936
  rsp.reqId = INT64_MIN;
3,984✔
3937
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,984✔
3938
  rsp.task.streamId = streamId;
3,984✔
3939
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,984✔
3940
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,984✔
3941

3942
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,984✔
3943
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,743✔
3944
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,743✔
3945
  }
3946

3947
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
7,968✔
3948

3949
_exit:
3,984✔
3950

3951
  if (code) {
3,984✔
3952
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3953
  } else {
3954
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,984✔
3955
  }
3956
}
3957

3958

3959
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
170,259✔
3960
  int32_t code = TSDB_CODE_SUCCESS;
170,259✔
3961
  int32_t lino = 0;
170,259✔
3962
  void* pIter = NULL;
170,259✔
3963
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
170,259✔
3964

3965
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
170,259✔
3966

3967
  while (1) {
301,701✔
3968
    pIter = taosHashIterate(pCtx->actionStm, pIter);
471,960✔
3969
    if (pIter == NULL) {
471,960✔
3970
      break;
170,259✔
3971
    }
3972

3973
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
301,701✔
3974
    SStmAction *pAction = (SStmAction *)pIter;
301,701✔
3975
    
3976
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
301,701✔
3977
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
56,543✔
3978
      continue;
56,543✔
3979
    }
3980

3981
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
245,158✔
3982
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3983
    }
3984

3985
    if (STREAM_ACT_RECALC & pAction->actions) {
245,158✔
3986
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,984✔
3987
    }
3988

3989
    if (STREAM_ACT_START & pAction->actions) {
245,158✔
3990
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
241,174✔
3991
    }
3992
  }
3993
  
3994
_exit:
170,259✔
3995

3996
  if (pIter) {
170,259✔
3997
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3998
  }
3999

4000
  if (code) {
170,259✔
4001
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4002
  }
4003

4004
  return code;
170,259✔
4005
}
4006

4007
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
19,918,839✔
4008
  int32_t  code = TSDB_CODE_SUCCESS;
19,918,839✔
4009
  int32_t  lino = 0;
19,918,839✔
4010
  int64_t* lastTs = NULL;
19,918,839✔
4011
  bool     noExists = false;
19,918,839✔
4012

4013
  while (true) {
4014
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
20,519,434✔
4015
    if (NULL == lastTs) {
20,519,434✔
4016
      if (noExists) {
779,102✔
4017
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
178,507✔
4018
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
178,507✔
4019
      }
4020

4021
      noExists = true;
600,595✔
4022
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
600,595✔
4023
      
4024
      continue;
600,595✔
4025
    }
4026

4027
    while (true) {
×
4028
      int64_t lastTsValue = atomic_load_64(lastTs);
19,740,332✔
4029
      if (pCtx->currTs > lastTsValue) {
19,740,332✔
4030
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
19,737,373✔
4031
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
19,737,373✔
4032
          return code;
19,737,373✔
4033
        }
4034

4035
        continue;
×
4036
      }
4037

4038
      return code;
2,959✔
4039
    }
4040

4041
    break;
4042
  }
4043

4044
_exit:
178,507✔
4045

4046
  if (code) {
178,507✔
4047
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
178,507✔
4048
  }
4049

4050
  return code;  
178,507✔
4051
}
4052

4053
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
4054
  SStmStatus* pStatus = NULL;
×
4055
  int32_t trigReaderNum = 0;
×
4056
  int32_t calcReaderNum = 0;
×
4057
  int32_t runnerNum = 0;
×
4058
  int64_t streamId = 0;
×
4059
  void* pIter = NULL;
×
4060
  while (true) {
4061
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
4062
    if (NULL == pIter) {
×
4063
      return;
×
4064
    }
4065

4066
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4067
    pStatus = (SStmStatus*)pIter;
×
4068

4069
    if (NULL == pStatus->triggerTask) {
×
4070
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
4071
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4072
      continue;
×
4073
    }
4074
    
4075
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
4076
    if (pStatus->trigReaderNum != trigReaderNum) {
×
4077
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
4078
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4079
      continue;
×
4080
    }
4081

4082
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
4083
    if (pStatus->calcReaderNum != calcReaderNum) {
×
4084
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
4085
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4086
      continue;
×
4087
    }
4088

4089
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
4090
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
4091
      if (runnerNum != pStatus->runnerNum) {
×
4092
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
4093
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
4094
        continue;
×
4095
      }
4096
    }
4097
  }
4098
}
4099

4100
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
387✔
4101
  int32_t code = TSDB_CODE_SUCCESS;
387✔
4102
  int32_t lino = 0;
387✔
4103
  int32_t minVal = watchError ? 0 : 1;
387✔
4104

4105
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
387✔
4106
    return code;
×
4107
  }
4108

4109
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
387✔
4110
    (void)sched_yield();
×
4111
  }
4112

4113
  if (watchError) {
387✔
4114
    taosHashClear(mStreamMgmt.vgroupMap);
×
4115
    taosHashClear(mStreamMgmt.snodeMap);
×
4116
    taosHashClear(mStreamMgmt.taskMap);
×
4117
    taosHashClear(mStreamMgmt.streamMap);
×
4118
    mstInfo("watch error happends, clear all maps");
×
4119
    goto _exit;
×
4120
  }
4121

4122
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
387✔
4123
    mstInfo("no stream tasks remain during watch state");
387✔
4124
    goto _exit;
387✔
4125
  }
4126

4127
  msmWatchCheckStreamMap(pCtx);
×
4128

4129
_exit:
387✔
4130

4131
  mStreamMgmt.lastTaskId += 100000;
387✔
4132

4133
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
387✔
4134

4135
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
387✔
4136

4137
  if (code) {
387✔
4138
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4139
  }
4140

4141
  return code;
387✔
4142
}
4143

4144

4145
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
13,629✔
4146
  int32_t code = TSDB_CODE_SUCCESS;
13,629✔
4147
  int32_t lino = 0;
13,629✔
4148
  SStreamHbMsg* pReq = pCtx->pReq;
13,629✔
4149

4150
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
13,629✔
4151
  
4152
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
13,629✔
4153
    goto _exit;
×
4154
  }
4155

4156
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
13,629✔
4157
  if (GOT_SNODE(pReq->snodeId)) {
13,629✔
4158
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,307✔
4159
  }
4160

4161
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,629✔
4162
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4163
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4164
  }
4165

4166
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
13,629✔
4167
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
387✔
4168
  }
4169

4170
_exit:
13,629✔
4171

4172
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
13,629✔
4173
  
4174
  if (code) {
13,629✔
4175
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4176

4177
    (void)msmWatchHandleEnding(pCtx, true);
×
4178
  }
4179

4180
  return code;
13,629✔
4181
}
4182

4183
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
8,862,356✔
4184
  int32_t listSize = taosArrayGetSize(pOReaders);
8,862,356✔
4185
  int32_t totalSize = 0;
8,862,356✔
4186
  
4187
  for (int32_t i = 0; i < listSize; ++i) {
9,207,894✔
4188
    SArray* pList = taosArrayGetP(pOReaders, i);
345,538✔
4189
    totalSize += taosArrayGetSize(pList);
345,538✔
4190
  }
4191

4192
  return totalSize;
8,862,356✔
4193
}
4194

4195
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
233,090✔
4196
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
233,090✔
4197
  if (NULL == pList) {
233,090✔
4198
    return NULL;
×
4199
  }
4200

4201
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
233,090✔
4202
}
4203

4204

4205
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
66,885✔
4206
  int32_t code = TSDB_CODE_SUCCESS;
66,885✔
4207
  int32_t lino = 0;
66,885✔
4208

4209
  if (NULL == pStatus->trigOReaders) {
66,885✔
4210
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
58,124✔
4211
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
58,124✔
4212
  }
4213

4214
  while (true) {
58,124✔
4215
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
125,009✔
4216

4217
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
125,009✔
4218
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
58,124✔
4219
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
58,124✔
4220

4221
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
116,248✔
4222
      continue;
58,124✔
4223
    }
4224

4225
    *ppRes = *ppOReaderList;
66,885✔
4226
    break;
66,885✔
4227
  }
4228

4229
_exit:
66,885✔
4230

4231
  if (code) {
66,885✔
4232
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4233
  }
4234

4235
  return code;
66,885✔
4236
}
4237

4238
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
79,336✔
4239
  int32_t code = TSDB_CODE_SUCCESS;
79,336✔
4240
  int32_t lino = 0;
79,336✔
4241
  bool    readerExists = false;
79,336✔
4242
  int64_t streamId = pTask->streamId;
79,336✔
4243

4244
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
79,336✔
4245
  for (int32_t i = 0; i < readerNum; ++i) {
163,924✔
4246
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
97,039✔
4247
    if (pReader->id.nodeId == vgId) {
97,039✔
4248
      readerExists = true;
12,451✔
4249
      break;
12,451✔
4250
    }
4251
  }
4252

4253
  if (!readerExists) {
79,336✔
4254
    SArray* pReaderList = NULL;
66,885✔
4255
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
66,885✔
4256
    
4257
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
66,885✔
4258
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
66,885✔
4259
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
66,885✔
4260
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
66,885✔
4261
  }
4262

4263
_exit:
12,451✔
4264

4265
  if (code) {
79,336✔
4266
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4267
  }
4268

4269
  return code;
79,336✔
4270
}
4271

4272
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
69,852✔
4273
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
69,852✔
4274
  int32_t lino = 0;
69,852✔
4275
  int32_t vgId = 0;
69,852✔
4276
  int64_t streamId = pTask->streamId;
69,852✔
4277
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
69,852✔
4278
  int32_t tbNum = taosArrayGetSize(pTbs);
69,852✔
4279
  SStreamDbTableName* pName = NULL;
69,852✔
4280
  SSHashObj* pDbVgroups = NULL;
69,852✔
4281
  SStreamMgmtRsp rsp = {0};
69,852✔
4282
  rsp.reqId = pTask->pMgmtReq->reqId;
69,852✔
4283
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
69,852✔
4284
  int32_t iter = 0;
69,852✔
4285
  void* p = NULL;
69,852✔
4286
  SSHashObj* pVgs = NULL;
69,852✔
4287
  SStreamMgmtReq* pMgmtReq = NULL;
69,852✔
4288
  int8_t stopped = 0;
69,852✔
4289

4290
  if (NULL == pCtx->pRsp->rsps.rspList) {
69,852✔
4291
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4292
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4293
  }
4294
  
4295
  TSWAP(pTask->pMgmtReq, pMgmtReq);
69,852✔
4296
  rsp.task = *(SStreamTask*)pTask;
69,852✔
4297

4298
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
69,852✔
4299
  if (NULL == pStatus) {
69,852✔
4300
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4301
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4302
  }
4303

4304
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
69,852✔
4305
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4306
    goto _exit;
×
4307
  }
4308

4309
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
69,852✔
4310

4311
  stopped = atomic_load_8(&pStatus->stopped);
69,852✔
4312
  if (stopped) {
69,852✔
4313
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4314
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4315
  }
4316

4317
  if (tbNum <= 0) {
69,852✔
4318
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4319
    goto _exit;
×
4320
  }
4321

4322
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
69,852✔
4323
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
69,852✔
4324
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
69,852✔
4325

4326
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
69,852✔
4327
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
69,852✔
4328
  
4329
  for (int32_t i = 0; i < tbNum; ++i) {
273,267✔
4330
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
203,415✔
4331
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
203,415✔
4332
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
406,830✔
4333
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
203,415✔
4334
  }
4335

4336
  int32_t vgNum = tSimpleHashGetSize(pVgs);
69,852✔
4337
  while (true) {
4338
    p = tSimpleHashIterate(pVgs, p, &iter);
149,188✔
4339
    if (NULL == p) {
149,188✔
4340
      break;
69,852✔
4341
    }
4342
    
4343
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
79,336✔
4344
  }
4345
  
4346
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
69,852✔
4347
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
69,852✔
4348
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
69,852✔
4349

4350
  SStreamTaskAddr addr;
69,852✔
4351
  for (int32_t i = 0; i < vgNum; ++i) {
136,737✔
4352
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
66,885✔
4353
    addr.taskId = pOTask->id.taskId;
66,885✔
4354
    addr.nodeId = pOTask->id.nodeId;
66,885✔
4355
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
66,885✔
4356
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
133,770✔
4357
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
66,885✔
4358
  }
4359

4360
_exit:
69,852✔
4361

4362
  tFreeSStreamMgmtReq(pMgmtReq);
69,852✔
4363
  taosMemoryFree(pMgmtReq);
69,852✔
4364

4365
  tSimpleHashCleanup(pVgs);
69,852✔
4366
  mstDestroyDbVgroupsHash(pDbVgroups);
69,852✔
4367

4368
  if (code) {
69,852✔
4369
    rsp.code = code;
×
4370
    
4371
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4372

4373
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4374
  } else {
4375
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
139,704✔
4376
  }
4377

4378
_final:
69,852✔
4379

4380
  if (finalCode) {
69,852✔
4381
    tFreeSStreamMgmtRsp(&rsp);
×
4382
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4383
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4384
  }
4385

4386
  return finalCode;
69,852✔
4387
}
4388

4389
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,838✔
4390
  int32_t num = taosArrayGetSize(pList);
2,838✔
4391
  SStreamCalcScan* pScan = NULL;
2,838✔
4392
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,838✔
4393
  int64_t planUid = 0;
2,838✔
4394
  for (int32_t i = 0; i < num; ++i) {
2,838✔
4395
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,838✔
4396
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,838✔
4397
    if (0 != planUid && planUid == uid) {
2,838✔
4398
      *ppRes = pScan;
2,838✔
4399
      break;
2,838✔
4400
    }
4401
  }
4402

4403
_exit:
2,838✔
4404

4405
  if (code) {
2,838✔
4406
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4407
  }
4408

4409
  return code;
2,838✔
4410
}
4411

4412
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,838✔
4413
  int32_t code = TSDB_CODE_SUCCESS;
2,838✔
4414
  int32_t lino = 0;
2,838✔
4415
  bool    readerExists = false;
2,838✔
4416
  int64_t streamId = pTask->streamId;
2,838✔
4417
  SListNode* pNode = listHead(pStatus->calcReaders);
2,838✔
4418
  SStmTaskStatus* pReader = NULL;
2,838✔
4419
  int32_t taskIdx = 0;
2,838✔
4420

4421
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,838✔
4422
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
26,316✔
4423
    pReader = (SStmTaskStatus*)pNode->data;
23,478✔
4424
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
23,478✔
4425
      readerExists = true;
×
4426
      pAddr->taskId = pReader->id.taskId;
×
4427
      break;
×
4428
    }
4429
  }
4430

4431
  if (!readerExists) {
2,838✔
4432
    if (NULL == pStatus->calcReaders) {
2,838✔
4433
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4434
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4435
      taskIdx = 0;
×
4436
    } else {
4437
      pNode = listTail(pStatus->calcReaders);
2,838✔
4438
      pReader = (SStmTaskStatus*)pNode->data;
2,838✔
4439
      taskIdx = pReader->id.taskIdx + 1;
2,838✔
4440
    }
4441

4442
    SStreamCalcScan* pScan = NULL;
2,838✔
4443
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,838✔
4444
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,838✔
4445
    pReader = tdListReserve(pStatus->calcReaders);
2,838✔
4446
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,838✔
4447
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,838✔
4448
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,838✔
4449
    pAddr->taskId = pReader->id.taskId;
2,838✔
4450
  }
4451

4452
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,838✔
4453
  pAddr->nodeId = vgId;
2,838✔
4454

4455
_exit:
2,838✔
4456

4457
  if (code) {
2,838✔
4458
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4459
  }
4460

4461
  return code;
2,838✔
4462
}
4463

4464

4465
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,838✔
4466
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,838✔
4467
  int32_t lino = 0;
2,838✔
4468
  int32_t vgId = 0;
2,838✔
4469
  int64_t streamId = pTask->streamId;
2,838✔
4470
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,838✔
4471
  int32_t reqNum = taosArrayGetSize(pReqs);
2,838✔
4472
  SStreamOReaderDeployReq* pReq = NULL;
2,838✔
4473
  SStreamOReaderDeployRsp* pRsp = NULL;
2,838✔
4474
  SStreamMgmtRsp rsp = {0};
2,838✔
4475
  rsp.reqId = pTask->pMgmtReq->reqId;
2,838✔
4476
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,838✔
4477
  SStreamMgmtReq* pMgmtReq = NULL;
2,838✔
4478
  int8_t stopped = 0;
2,838✔
4479
  int32_t vgNum = 0;
2,838✔
4480
  SStreamTaskAddr* pAddr = NULL;
2,838✔
4481

4482
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,838✔
4483
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4484
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4485
  }
4486
  
4487
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,838✔
4488
  rsp.task = *(SStreamTask*)pTask;
2,838✔
4489

4490
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,838✔
4491
  if (NULL == pStatus) {
2,838✔
4492
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4493
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4494
  }
4495

4496
  stopped = atomic_load_8(&pStatus->stopped);
2,838✔
4497
  if (stopped) {
2,838✔
4498
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4499
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4500
  }
4501

4502
  if (reqNum <= 0) {
2,838✔
4503
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4504
    goto _exit;
×
4505
  }
4506

4507
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,838✔
4508
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,838✔
4509

4510
  for (int32_t i = 0; i < reqNum; ++i) {
5,676✔
4511
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,838✔
4512
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,838✔
4513
    pRsp->execId = pReq->execId;
2,838✔
4514
    vgNum = taosArrayGetSize(pReq->vgIds);
2,838✔
4515
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,838✔
4516
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,838✔
4517
    
4518
    for (int32_t n = 0; n < vgNum; ++n) {
5,676✔
4519
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,838✔
4520
      pAddr = taosArrayGet(pRsp->vgList, n);
2,838✔
4521
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,838✔
4522
    }
4523
  }
4524

4525
_exit:
2,838✔
4526

4527
  tFreeSStreamMgmtReq(pMgmtReq);
2,838✔
4528
  taosMemoryFree(pMgmtReq);
2,838✔
4529

4530
  if (code) {
2,838✔
4531
    rsp.code = code;
×
4532
    
4533
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4534
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4535
  } else {
4536
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
5,676✔
4537
  }
4538

4539

4540
_final:
2,838✔
4541

4542
  if (finalCode) {
2,838✔
4543
    tFreeSStreamMgmtRsp(&rsp);
×
4544
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4545
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4546
  }
4547

4548
  return finalCode;
2,838✔
4549
}
4550

4551

4552
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
72,690✔
4553
  int32_t code = TSDB_CODE_SUCCESS;
72,690✔
4554
  int32_t lino = 0;
72,690✔
4555

4556
  switch (pTask->pMgmtReq->type) {
72,690✔
4557
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
69,852✔
4558
      msmDeployTriggerOrigReader(pCtx, pTask);
69,852✔
4559
      break;
69,852✔
4560
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,838✔
4561
      msmDeployRunnerOrigReader(pCtx, pTask);
2,838✔
4562
      break;
2,838✔
4563
    default:
×
4564
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4565
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4566
      break;
×
4567
  }
4568

4569
_exit:
72,690✔
4570

4571
  if (code) {
72,690✔
4572
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4573
  }
4574

4575
  return code;
72,690✔
4576
}
4577

4578
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
20,115✔
4579
  int32_t code = TSDB_CODE_SUCCESS;
20,115✔
4580
  int32_t lino = 0;
20,115✔
4581
  SStreamHbMsg* pReq = pCtx->pReq;
20,115✔
4582
  SStmTaskStatusMsg* pTask = NULL;
20,115✔
4583
  
4584
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
20,115✔
4585
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
20,115✔
4586
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
20,115✔
4587
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
20,115✔
4588
  }
4589
  
4590
  for (int32_t i = 0; i < reqNum; ++i) {
92,805✔
4591
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
72,690✔
4592
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
72,690✔
4593
    if (NULL == pTask) {
72,690✔
4594
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4595
      continue;
×
4596
    }
4597

4598
    if (NULL == pTask->pMgmtReq) {
72,690✔
4599
      msttError("idx %d without mgmtReq", idx);
×
4600
      continue;
×
4601
    }
4602

4603
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
72,690✔
4604
  }
4605

4606
_exit:
20,115✔
4607

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

4612
  return code;
20,115✔
4613
}
4614

4615
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
19,905,210✔
4616
  int32_t code = TSDB_CODE_SUCCESS;
19,905,210✔
4617
  int32_t lino = 0;
19,905,210✔
4618
  SStreamHbMsg* pReq = pCtx->pReq;
19,905,210✔
4619

4620
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
19,905,210✔
4621
  if (GOT_SNODE(pReq->snodeId)) {
19,726,703✔
4622
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,920,636✔
4623
  }
4624
  
4625
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
19,726,414✔
4626
    msmHandleStreamActions(pCtx);
76,146✔
4627
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
76,146✔
4628
  }
4629

4630
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
19,726,414✔
4631
    code = msmHandleStreamRequests(pCtx);
20,115✔
4632
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
20,115✔
4633
    TAOS_CHECK_EXIT(code);
20,115✔
4634
  }
4635

4636
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
19,726,414✔
4637
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
102,948✔
4638
  } else {
4639
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
19,623,466✔
4640
  }
4641

4642
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
19,726,414✔
4643
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
84,147✔
4644
  }
4645

4646
  if (taosHashGetSize(pCtx->deployStm) > 0) {
19,726,414✔
4647
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
98,904✔
4648
  }
4649

4650
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
19,726,414✔
4651
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,746,355✔
4652
  }
4653

4654
  if (taosHashGetSize(pCtx->actionStm) > 0) {
19,726,414✔
4655
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
170,259✔
4656
  }
4657

4658
_exit:
19,726,414✔
4659

4660
  if (code) {
19,905,210✔
4661
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
178,796✔
4662
  }
4663

4664
  return code;
19,905,210✔
4665
}
4666

4667
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
20,012,659✔
4668
  int32_t lino = 0;
20,012,659✔
4669
  int32_t tlen = 0;
20,012,659✔
4670
  void   *buf = NULL;
20,012,659✔
4671

4672
  if (TSDB_CODE_SUCCESS != code) {
20,012,659✔
4673
    goto _exit;
178,796✔
4674
  }
4675

4676
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
19,833,863✔
4677
  if (code < 0) {
19,833,863✔
4678
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4679
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4680
  }
4681

4682
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
19,833,863✔
4683
  if (buf == NULL) {
19,833,863✔
4684
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4685
    TAOS_CHECK_EXIT(terrno);    
×
4686
  }
4687

4688
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
19,833,863✔
4689
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
19,833,863✔
4690

4691
  SEncoder encoder;
19,829,792✔
4692
  tEncoderInit(&encoder, abuf, tlen);
19,833,863✔
4693
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
19,833,863✔
4694
    rpcFreeCont(buf);
×
4695
    buf = NULL;
×
4696
    tEncoderClear(&encoder);
×
4697
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4698
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4699
  }
4700
  tEncoderClear(&encoder);
19,833,863✔
4701

4702
_exit:
20,012,659✔
4703

4704
  pMsg->code = code;
20,012,659✔
4705
  pMsg->info = *pRpcInfo;
20,012,659✔
4706
  if (TSDB_CODE_SUCCESS == code) {
20,012,659✔
4707
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
19,833,863✔
4708
    pMsg->pCont = buf;
19,833,863✔
4709
  }
4710
}
20,012,659✔
4711

4712

4713
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
20,012,659✔
4714
  int32_t code = TSDB_CODE_SUCCESS;
20,012,659✔
4715
  SMStreamHbRspMsg rsp = {0};
20,012,659✔
4716
  rsp.streamGId = pHb->streamGId;
20,012,659✔
4717

4718
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
20,012,659✔
4719

4720
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
20,012,659✔
4721
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
93,820✔
4722
    goto _exit;
93,820✔
4723
  }
4724

4725
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
19,918,839✔
4726
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
19,918,839✔
4727

4728
  pCtx->tidx = tidx;
19,918,839✔
4729
  pCtx->pMnode = pMnode;
19,918,839✔
4730
  pCtx->currTs = currTs;
19,918,839✔
4731
  pCtx->pReq = pHb;
19,918,839✔
4732
  pCtx->pRsp = &rsp;
19,918,839✔
4733
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
19,918,839✔
4734
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
19,918,839✔
4735
  
4736
  switch (atomic_load_8(&mStreamMgmt.state)) {
19,918,839✔
4737
    case MND_STM_STATE_WATCH:
13,629✔
4738
      code = msmWatchHandleHbMsg(pCtx);
13,629✔
4739
      break;
13,629✔
4740
    case MND_STM_STATE_NORMAL:
19,905,210✔
4741
      code = msmNormalHandleHbMsg(pCtx);
19,905,210✔
4742
      break;
19,905,210✔
4743
    default:
×
4744
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4745
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4746
      break;
×
4747
  }
4748

4749
_exit:
20,012,659✔
4750

4751
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
20,012,659✔
4752

4753
  msmCleanStreamGrpCtx(pHb);
20,012,659✔
4754
  msmClearStreamToDeployMaps(pHb);
20,012,659✔
4755

4756
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
20,012,659✔
4757
  
4758
  tFreeSMStreamHbRspMsg(&rsp);
20,012,659✔
4759

4760
  return code;
20,012,659✔
4761
}
4762

4763
void msmHandleBecomeLeader(SMnode *pMnode) {
485,111✔
4764
  if (tsDisableStream) {
485,111✔
4765
    return;
×
4766
  }
4767

4768
  mstInfo("start to process mnode become leader");
485,111✔
4769

4770
  int32_t code = 0;
485,111✔
4771
  streamAddVnodeLeader(MNODE_HANDLE);
485,111✔
4772
  
4773
  taosWLockLatch(&mStreamMgmt.runtimeLock);
485,111✔
4774
  msmDestroyRuntimeInfo(pMnode);
485,111✔
4775
  code = msmInitRuntimeInfo(pMnode);
485,111✔
4776
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
485,111✔
4777

4778
  if (TSDB_CODE_SUCCESS == code) {
485,111✔
4779
    atomic_store_8(&mStreamMgmt.active, 1);
485,111✔
4780
  }
4781

4782
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
485,111✔
4783
}
4784

4785
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
605,425✔
4786
  if (tsDisableStream) {
605,425✔
4787
    return;
×
4788
  }
4789

4790
  mstInfo("start to process mnode become not leader");
605,425✔
4791

4792
  streamRemoveVnodeLeader(MNODE_HANDLE);
605,425✔
4793

4794
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
605,425✔
4795
    taosWLockLatch(&mStreamMgmt.runtimeLock);
485,111✔
4796
    msmDestroyRuntimeInfo(pMnode);
485,111✔
4797
    mStreamMgmt.stat.inactiveTimes++;
485,111✔
4798
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
485,111✔
4799
  }
4800
}
4801

4802

4803
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4804
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4805
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4806
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4807
  } else {
4808
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4809
  }
4810
}
×
4811

4812
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
9,352✔
4813
  int32_t code = TSDB_CODE_SUCCESS;
9,352✔
4814
  int32_t lino = 0;
9,352✔
4815
  SStreamObj* pStream = pObj;
9,352✔
4816
  SSnodeObj* pSnode = p1;
9,352✔
4817
  SArray** ppRes = p2;
9,352✔
4818

4819
  if (pStream->mainSnodeId == pSnode->id) {
9,352✔
4820
    if (NULL == *ppRes) {
5,996✔
4821
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,301✔
4822
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
1,301✔
4823
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
1,301✔
4824
    }
4825

4826
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
11,992✔
4827
  }
4828

4829
  return true;
9,352✔
4830

4831
_exit:
×
4832

4833
  if (code) {
×
4834
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4835
  }  
4836

4837
  *(int32_t*)p3 = code;
×
4838

4839
  return false;
×
4840
}
4841

4842

4843
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
49,124✔
4844
  int32_t code = TSDB_CODE_SUCCESS;
49,124✔
4845
  int32_t lino = 0;
49,124✔
4846
  
4847
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
49,124✔
4848
  TAOS_CHECK_EXIT(code);
49,124✔
4849

4850
  int32_t streamNum = taosArrayGetSize(*ppRes);
49,124✔
4851
  if (streamNum > 0 && 0 == pSnode->replicaId) {
49,124✔
4852
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
241✔
4853
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
241✔
4854
  }
4855

4856
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4857

4858
_exit:
49,124✔
4859

4860
  if (code) {
49,124✔
4861
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
241✔
4862
  }  
4863

4864
  return code;
49,124✔
4865
}
4866

4867
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
802,933✔
4868
  SStreamObj* pStream = pObj;
802,933✔
4869
  int64_t streamId = pStream->pCreate->streamId;
802,933✔
4870
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
802,933✔
4871
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
802,933✔
4872
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
802,933✔
4873
  
4874
  if ((userDropped || userStopped) && (NULL == pStatus)) {
802,933✔
4875
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
241✔
4876
    return true;
241✔
4877
  }
4878
  
4879
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
802,692✔
4880
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
658,835✔
4881
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4882
    return true;
658,835✔
4883
  }
4884

4885
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
143,857✔
4886
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
58,798✔
4887
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4888
    return true;
58,798✔
4889
  }  
4890

4891
  if (pStatus) {
85,059✔
4892
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
83,842✔
4893
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4894
    }
4895

4896
    return true;
83,842✔
4897
  }
4898

4899
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
1,217✔
4900

4901
  return true;
1,217✔
4902
}
4903

4904
void msmCheckLoopStreamMap(SMnode *pMnode) {
60,342✔
4905
  SStmStatus* pStatus = NULL;
60,342✔
4906
  void* pIter = NULL;
60,342✔
4907
  int8_t stopped = 0;
60,342✔
4908
  int64_t streamId = 0;
60,342✔
4909
  
4910
  while (true) {
4911
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
336,148✔
4912
    if (NULL == pIter) {
336,148✔
4913
      break;
60,342✔
4914
    }
4915

4916
    pStatus = (SStmStatus*)pIter;
275,806✔
4917

4918
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
275,806✔
4919
    stopped = atomic_load_8(&pStatus->stopped);
275,806✔
4920
    if (MST_IS_USER_STOPPED(stopped)) {
275,806✔
4921
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
576✔
4922
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
576✔
4923
      continue;
576✔
4924
    }
4925

4926
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
275,230✔
4927
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4928
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4929
      continue;
×
4930
    }
4931

4932
    if (MST_IS_ERROR_STOPPED(stopped)) {
275,230✔
4933
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
17,702✔
4934
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
11,887✔
4935
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4936
            
4937
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
11,887✔
4938
        continue;
11,887✔
4939
      }
4940

4941
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
5,815✔
4942
      continue;
5,815✔
4943
    }
4944

4945
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
257,528✔
4946
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4947
      continue;
×
4948
    }
4949
  }
4950
}
60,342✔
4951

4952
void msmCheckStreamsStatus(SMnode *pMnode) {
571,199✔
4953
  SStmCheckStatusCtx ctx = {0};
571,199✔
4954

4955
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
571,199✔
4956
  
4957
  if (MST_READY_FOR_SDB_LOOP()) {
571,199✔
4958
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
132,554✔
4959
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
132,554✔
4960
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
132,554✔
4961
  }
4962

4963
  if (MST_READY_FOR_MAP_LOOP()) {
571,199✔
4964
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
60,342✔
4965
    msmCheckLoopStreamMap(pMnode);
60,342✔
4966
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
60,342✔
4967
  }
4968
}
571,199✔
4969

4970
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
6,483,667✔
4971
  for (int32_t i = 0; i < taskNum; ++i) {
13,683,159✔
4972
    SStmTaskStatus* pTask = *(pList + i);
7,199,492✔
4973

4974
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
7,199,492✔
4975
      continue;
717,938✔
4976
    }
4977
    
4978
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
6,482,484✔
4979
      continue;
6,480,917✔
4980
    }
4981

4982
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,567✔
4983
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,567✔
4984
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4985
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4986
          
4987
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
4988
      break;
×
4989
    }
4990

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

4994
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,567✔
4995
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,567✔
4996

4997
    SStmTaskAction task = {0};
1,567✔
4998
    task.streamId = streamId;
1,567✔
4999
    task.id = pTask->id;
1,567✔
5000
    task.flag = pTask->flags;
1,567✔
5001
    task.type = pTask->type;
1,567✔
5002
    
5003
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,567✔
5004
  }
5005
}
6,483,667✔
5006

5007
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
223,707✔
5008
  void* pIter = NULL;
223,707✔
5009
  SStmVgStreamStatus* pVg = NULL;
223,707✔
5010
  int64_t streamId = 0;
223,707✔
5011
  
5012
  while (true) {
2,578,251✔
5013
    pIter = taosHashIterate(pStreams, pIter);
2,801,958✔
5014
    if (NULL == pIter) {
2,801,958✔
5015
      break;
223,707✔
5016
    }
5017

5018
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,578,251✔
5019
    pVg = (SStmVgStreamStatus*)pIter;
2,578,251✔
5020

5021
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
2,578,251✔
5022
    if (taskNum > 0) {
2,578,251✔
5023
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,520,736✔
5024
    }
5025

5026
    taskNum = taosArrayGetSize(pVg->calcReaders);
2,578,251✔
5027
    if (taskNum > 0) {
2,578,251✔
5028
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,709,852✔
5029
    }
5030
  }
5031
}
223,707✔
5032

5033
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
5034
  int64_t streamId = 0;
×
5035
  void* pIter = NULL;
×
5036
  SStmVgStreamStatus* pStream = NULL;
×
5037

5038
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
5039
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5040
    return;
×
5041
  }
5042

5043
  
5044
  while (true) {
5045
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
5046
    if (NULL == pIter) {
×
5047
      break;
×
5048
    }
5049

5050
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
5051
    
5052
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
5053
  }
5054

5055
  taosHashClear(pVg->streamTasks);
×
5056
}
5057

5058

5059
void msmCheckVgroupStatus(SMnode *pMnode) {
571,199✔
5060
  void* pIter = NULL;
571,199✔
5061
  int32_t code = 0;
571,199✔
5062
  
5063
  while (true) {
2,108,718✔
5064
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
2,679,917✔
5065
    if (NULL == pIter) {
2,679,917✔
5066
      break;
571,199✔
5067
    }
5068

5069
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
2,108,718✔
5070
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
2,108,718✔
5071
      continue;
1,878,557✔
5072
    }
5073
    
5074
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
230,161✔
5075

5076
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
230,161✔
5077
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
6,454✔
5078
      if (NULL == pVgroup) {
6,454✔
5079
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
6,454✔
5080
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
6,454✔
5081
        if (code) {
6,454✔
5082
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
5083
        }
5084
        continue;
6,454✔
5085
      }
5086
      mndReleaseVgroup(pMnode, pVgroup);
×
5087
      
5088
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
5089
      
5090
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
5091
      continue;
×
5092
    }
5093

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

5096
    msmCheckVgroupStreamStatus(pVg->streamTasks);
223,707✔
5097
  }
5098
}
571,199✔
5099

5100
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
×
5101
  *deployNum = 0;
×
5102
  
5103
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
×
5104
    if (pStream->runners[i]) {
×
5105
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
×
5106
      for (int32_t t = 0; t < taskNum; ++t) {
×
5107
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
×
5108
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
×
5109
        if (stopped) {
×
5110
          mstsDebug("stream already stopped %d, ignore it", stopped);
×
5111
          *deployNum = 0;
×
5112
          return;
×
5113
        }
5114

5115
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
×
5116
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
×
5117
      }
5118
      
5119
      deployId[*deployNum] = i;
×
5120
      (*deployNum)++;
×
5121
    }
5122
  }
5123
}
5124

5125
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
1,547✔
5126
  pSnode->runnerThreadNum = -1;
1,547✔
5127

5128
  (void)msmSTAddSnodesToMap(pMnode);
1,547✔
5129

5130
  int64_t streamId = 0;
1,547✔
5131
  void* pIter = NULL;
1,547✔
5132
  SStmSnodeStreamStatus* pStream = NULL;
1,547✔
5133
  int32_t deployNum = 0;
1,547✔
5134
  SStmTaskAction task = {0};
1,547✔
5135
  
5136
  while (true) {
5137
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
1,768✔
5138
    if (NULL == pIter) {
1,768✔
5139
      break;
1,547✔
5140
    }
5141

5142
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
221✔
5143
    
5144
    task.streamId = streamId;
221✔
5145
    
5146
    pStream = (SStmSnodeStreamStatus*)pIter;
221✔
5147
    if (pStream->trigger) {
221✔
5148
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
221✔
5149
      if (stopped) {
221✔
5150
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
5151
        continue;
×
5152
      }
5153

5154
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
221✔
5155
      
5156
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
221✔
5157
    } else {
5158
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
×
5159
      
5160
      if (task.deployNum > 0) {
×
5161
        //task.triggerStatus = pStream->trigger;
5162
        task.multiRunner = true;
×
5163
        task.type = STREAM_RUNNER_TASK;
×
5164
        
5165
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
×
5166
        
5167
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
×
5168
      }
5169
    }
5170
  }
5171

5172
  taosHashClear(pSnode->streamTasks);
1,547✔
5173
}
1,547✔
5174

5175

5176
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
63,059✔
5177
  void* pIter = NULL;
63,059✔
5178
  SStmSnodeStreamStatus* pSnode = NULL;
63,059✔
5179
  int64_t streamId = 0;
63,059✔
5180
  
5181
  while (true) {
5182
    pIter = taosHashIterate(pStreams, pIter);
916,902✔
5183
    if (NULL == pIter) {
916,902✔
5184
      break;
63,059✔
5185
    }
5186

5187
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
853,843✔
5188
    pSnode = (SStmSnodeStreamStatus*)pIter;
853,843✔
5189

5190
    if (NULL != pSnode->trigger) {
853,843✔
5191
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
834,547✔
5192
    }
5193

5194
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
3,415,372✔
5195
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
2,561,529✔
5196
      if (taskNum > 0) {
2,561,529✔
5197
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
2,418,532✔
5198
      }
5199
    }
5200
  }
5201
}
63,059✔
5202

5203

5204
void msmCheckSnodeStatus(SMnode *pMnode) {
571,199✔
5205
  void* pIter = NULL;
571,199✔
5206
  
5207
  while (true) {
688,298✔
5208
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
1,259,497✔
5209
    if (NULL == pIter) {
1,259,497✔
5210
      break;
571,199✔
5211
    }
5212

5213
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
688,298✔
5214
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
688,298✔
5215
      continue;
607,537✔
5216
    }
5217

5218
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
80,761✔
5219
    
5220
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
80,761✔
5221
    if (NULL == pSnode->streamTasks) {
80,761✔
5222
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
16,376✔
5223
      continue;
16,376✔
5224
    }
5225
    
5226
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
64,385✔
5227
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,326✔
5228
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5229
      
5230
      msmHandleSnodeLost(pMnode, pSnode);
1,326✔
5231
      continue;
1,326✔
5232
    }
5233
    
5234
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
63,059✔
5235

5236
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
63,059✔
5237
  }
5238
}
571,199✔
5239

5240

5241
void msmCheckTasksStatus(SMnode *pMnode) {
571,199✔
5242
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
571,199✔
5243

5244
  msmCheckVgroupStatus(pMnode);
571,199✔
5245
  msmCheckSnodeStatus(pMnode);
571,199✔
5246
}
571,199✔
5247

5248
void msmCheckSnodesState(SMnode *pMnode) {
571,199✔
5249
  if (!MST_READY_FOR_SNODE_LOOP()) {
571,199✔
5250
    return;
516,705✔
5251
  }
5252

5253
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
54,494✔
5254

5255
  void* pIter = NULL;
54,494✔
5256
  int32_t snodeId = 0;
54,494✔
5257
  while (true) {
60,300✔
5258
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
114,794✔
5259
    if (NULL == pIter) {
114,794✔
5260
      break;
54,494✔
5261
    }
5262

5263
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
60,300✔
5264
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
60,300✔
5265
      continue;
60,079✔
5266
    }
5267

5268
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
221✔
5269
    if (NULL == pSnode->streamTasks) {
221✔
UNCOV
5270
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
UNCOV
5271
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
UNCOV
5272
      continue;
×
5273
    }
5274
    
5275
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
221✔
5276
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5277
    
5278
    msmHandleSnodeLost(pMnode, pSnode);
221✔
5279
  }
5280

5281
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
54,494✔
5282
}
5283

5284
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
12,581,343✔
5285
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
12,581,343✔
5286
  if (0 == active || MND_STM_STATE_NORMAL != state) {
12,581,343✔
5287
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,307✔
5288
    return false;
2,307✔
5289
  }
5290

5291
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
12,579,036✔
5292
    mstTrace("ignore health check since no stream now");
11,436,638✔
5293
    return false;
11,436,638✔
5294
  }
5295

5296
  return true;
1,142,398✔
5297
}
5298

5299
void msmHealthCheck(SMnode *pMnode) {
12,010,144✔
5300
  if (!msmCheckNeedHealthCheck(pMnode)) {
12,010,144✔
5301
    return;
11,438,945✔
5302
  }
5303

5304
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
1,122,040✔
5305
  
5306
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
571,199✔
5307
  if (!msmCheckNeedHealthCheck(pMnode)) {
571,199✔
5308
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5309
    return;
×
5310
  }
5311
  
5312
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
571,199✔
5313
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
571,199✔
5314

5315
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
571,199✔
5316
  
5317
  msmCheckStreamsStatus(pMnode);
571,199✔
5318
  msmCheckTasksStatus(pMnode);
571,199✔
5319
  msmCheckSnodesState(pMnode);
571,199✔
5320

5321
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
571,199✔
5322

5323
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
571,199✔
5324
}
5325

5326
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
939✔
5327
  SStreamObj *pStream = pObj;
939✔
5328
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
939✔
5329
    return true;
×
5330
  }
5331
  
5332
  pStream->updateTime = *(int64_t*)p1;
939✔
5333
  
5334
  (*(int32_t*)p2)++;
939✔
5335
  
5336
  return true;
939✔
5337
}
5338

5339
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
27,913✔
5340
  int32_t code = 0;
27,913✔
5341
  int8_t  stopped = 0;
27,913✔
5342
  
5343
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
27,913✔
5344
  
5345
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
27,913✔
5346
  if (NULL == pStatus) {
27,913✔
5347
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,578✔
5348
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,578✔
5349
    goto _exit;
3,578✔
5350
  }
5351

5352
  stopped = atomic_load_8(&pStatus->stopped);
24,335✔
5353
  if (stopped) {
24,335✔
5354
    mstsError("stream already stopped, stopped:%d", stopped);
×
5355
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5356
    goto _exit;
×
5357
  }
5358

5359
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
24,335✔
5360
    pAddr->taskId = pStatus->triggerTask->id.taskId;
18,910✔
5361
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
18,910✔
5362
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
18,910✔
5363
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
18,910✔
5364
    goto _exit;
18,910✔
5365
  }
5366

5367
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
5,425✔
5368
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
5,425✔
5369

5370
_exit:
27,913✔
5371
  
5372
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
27,913✔
5373

5374
  return code;
27,913✔
5375
}
5376

5377
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
485,111✔
5378
  int32_t code = TSDB_CODE_SUCCESS;
485,111✔
5379
  int32_t lino = 0;
485,111✔
5380
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
485,111✔
5381
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
485,111✔
5382
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
485,111✔
5383

5384
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
968,387✔
5385

5386
  mStreamMgmt.stat.activeTimes++;
485,111✔
5387
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
485,111✔
5388
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
485,111✔
5389
  if (NULL == mStreamMgmt.tCtx) {
485,111✔
5390
    code = terrno;
×
5391
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5392
    goto _exit;
×
5393
  }
5394

5395
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
485,111✔
5396
  if (mStreamMgmt.actionQ == NULL) {
485,111✔
5397
    code = terrno;
×
5398
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5399
    goto _exit;
×
5400
  }
5401
  
5402
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
485,111✔
5403
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
485,111✔
5404
  
5405
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
485,111✔
5406
  
5407
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,905,673✔
5408
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,420,562✔
5409

5410
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
14,523,372✔
5411
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
12,102,810✔
5412
      if (pCtx->deployStm[m] == NULL) {
12,102,810✔
5413
        code = terrno;
×
5414
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5415
        goto _exit;
×
5416
      }
5417
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
12,102,810✔
5418
      
5419
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
12,102,810✔
5420
      if (pCtx->actionStm[m] == NULL) {
12,102,810✔
5421
        code = terrno;
×
5422
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5423
        goto _exit;
×
5424
      }
5425
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
12,102,810✔
5426
    }
5427
  }
5428
  
5429
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
485,111✔
5430
  if (mStreamMgmt.streamMap == NULL) {
485,111✔
5431
    code = terrno;
×
5432
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5433
    goto _exit;
×
5434
  }
5435
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
485,111✔
5436
  
5437
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
485,111✔
5438
  if (mStreamMgmt.taskMap == NULL) {
485,111✔
5439
    code = terrno;
×
5440
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5441
    goto _exit;
×
5442
  }
5443
  
5444
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
485,111✔
5445
  if (mStreamMgmt.vgroupMap == NULL) {
485,111✔
5446
    code = terrno;
×
5447
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5448
    goto _exit;
×
5449
  }
5450
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
485,111✔
5451

5452
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
485,111✔
5453
  if (mStreamMgmt.snodeMap == NULL) {
485,111✔
5454
    code = terrno;
×
5455
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5456
    goto _exit;
×
5457
  }
5458
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
485,111✔
5459
  
5460
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
485,111✔
5461
  if (mStreamMgmt.dnodeMap == NULL) {
485,111✔
5462
    code = terrno;
×
5463
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5464
    goto _exit;
×
5465
  }
5466

5467
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
485,111✔
5468
  if (mStreamMgmt.toDeployVgMap == NULL) {
485,111✔
5469
    code = terrno;
×
5470
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5471
    goto _exit;
×
5472
  }
5473
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
485,111✔
5474
  
5475
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
485,111✔
5476
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
485,111✔
5477
    code = terrno;
×
5478
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5479
    goto _exit;
×
5480
  }
5481
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
485,111✔
5482

5483
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
485,111✔
5484
  if (mStreamMgmt.toUpdateScanMap == NULL) {
485,111✔
5485
    code = terrno;
×
5486
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5487
    goto _exit;
×
5488
  }
5489
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
485,111✔
5490

5491
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
485,111✔
5492
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
485,111✔
5493

5494
  mStreamMgmt.lastTaskId = 1;
485,111✔
5495

5496
  int32_t activeStreamNum = 0;
485,111✔
5497
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
485,111✔
5498

5499
  if (activeStreamNum > 0) {
485,111✔
5500
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
759✔
5501
  } else {
5502
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
484,352✔
5503
  }
5504

5505
_exit:
485,111✔
5506

5507
  if (code) {
485,111✔
5508
    msmDestroyRuntimeInfo(pMnode);
×
5509
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5510
  } else {
5511
    mstInfo("mnode stream runtime init done");
485,111✔
5512
  }
5513

5514
  return code;
485,111✔
5515
}
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