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

taosdata / TDengine / #5009

29 Mar 2026 04:32AM UTC coverage: 72.26% (+0.02%) from 72.241%
#5009

push

travis-ci

web-flow
refactor: do some internal refactor for TDgpt. (#34955)

253662 of 351039 relevant lines covered (72.26%)

131649114.75 hits per line

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

76.22
/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() {
845,404✔
34
  SStmQNode* pQNode = NULL;
845,404✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
845,404✔
37
    return;
422,702✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,108,648✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,651,888✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,543,240✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,543,240✔
51
  }
52
}
2,108,648✔
53

54
void msmDestroyThreadCtxs() {
845,404✔
55
  if (NULL == mStreamMgmt.tCtx) {
845,404✔
56
    return;
422,702✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,531,350✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,108,648✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
422,702✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
845,404✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
845,404✔
94
}
845,404✔
95

96

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

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

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

111
    pStatus = pStream;
186✔
112
  }
113

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

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

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

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

136
_exit:
×
137

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

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

145

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

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

200
  pSnode = NULL;
449,029✔
201

202
_exit:
449,029✔
203

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

208
  return code;
449,029✔
209
}
210

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

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

235
  pDnode = NULL;
926,576✔
236

237
_exit:
926,576✔
238

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

243
  return code;
926,576✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
656,426✔
275
  int32_t code = TSDB_CODE_SUCCESS;
656,426✔
276
  int32_t lino = 0;
656,426✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
656,426✔
278
  if (NULL == pStream) {
656,426✔
279
    SStmVgStreamStatus stream = {0};
469,745✔
280
    if (trigReader) {
469,745✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
273,885✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
273,885✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
547,770✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
195,860✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
195,860✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
391,720✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
469,745✔
290
    goto _exit;
469,745✔
291
  }
292
  
293
  if (trigReader) {
186,681✔
294
    if (NULL == pStream->trigReaders) {
22,565✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
22,565✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
22,565✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
45,130✔
300
    goto _exit;
22,565✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
164,116✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
107,907✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
107,907✔
306
  }
307

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

310
_exit:
164,116✔
311

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

320
  return code;
656,426✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
84,637✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
84,637✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
84,637✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
571,789✔
339
  }
340
  
341
_exit:
656,270✔
342

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

350
  return code;
656,426✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
657,542✔
363
    if (NULL == pVg) {
657,542✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
143,891✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
143,891✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
287,782✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
143,891✔
368
      if (TSDB_CODE_SUCCESS == code) {
143,891✔
369
        goto _return;
143,891✔
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);
513,651✔
381
    if (NULL == pVg->taskList) {
513,651✔
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,027,302✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
513,651✔
390
    
391
    taosHashRelease(pVgMap, pVg);
513,651✔
392
    break;
513,651✔
393
  }
394
  
395
_return:
657,542✔
396

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

404
  return code;
657,542✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
861,276✔
409
  int32_t code = TSDB_CODE_SUCCESS;
861,276✔
410
  int32_t lino = 0;
861,276✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
861,276✔
412
  if (NULL == pStream) {
861,276✔
413
    SStmSnodeStreamStatus stream = {0};
199,745✔
414
    if (deployId < 0) {
199,745✔
415
      stream.trigger = pStatus;
3,176✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
196,569✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
196,569✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
393,138✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
199,745✔
423
    goto _exit;
199,745✔
424
  }
425
  
426
  if (deployId < 0) {
661,531✔
427
    if (NULL != pStream->trigger) {
189,985✔
428
      mstsWarn("stream already got trigger task %" PRIx64 " SID:%" PRIx64 " in snode %d, replace it with task %" PRIx64 " SID:%" PRIx64, 
×
429
          pStream->trigger->id.taskId, pStream->trigger->id.seriousId, pStatus->id.nodeId, pStatus->id.taskId, pStatus->id.seriousId);
430
    }
431
    
432
    pStream->trigger = pStatus;
189,985✔
433
    goto _exit;
189,985✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
471,546✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
374,078✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
374,078✔
439
  }
440

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

443
_exit:
471,546✔
444

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

453
  return code;
861,276✔
454
}
455

456

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

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

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

484
  return code;
861,276✔
485
}
486

487

488

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

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

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

507
      code = taosHashPut(mStreamMgmt.toDeploySnodeMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &snode, sizeof(snode));
236✔
508
      if (TSDB_CODE_SUCCESS == code) {
236✔
509
        goto _return;
236✔
510
      }
511

512
      if (TSDB_CODE_DUP_KEY != code) {
×
513
        goto _return;
×
514
      }    
515

516
      taosArrayDestroy(snode.triggerList);
×
517
      continue;
×
518
    }
519
    
520
    taosWLockLatch(&pSnode->lock);
192,925✔
521
    if (NULL == pSnode->triggerList) {
192,925✔
522
      pSnode->triggerList = taosArrayInit(10, sizeof(SStmTaskToDeployExt));
47,998✔
523
      if (NULL == pSnode->triggerList) {
47,998✔
524
        taosWUnLockLatch(&pSnode->lock);
×
525
        TSDB_CHECK_NULL(pSnode->triggerList, code, lino, _return, terrno);
×
526
      }
527
    }
528
    
529
    ext.deploy = *pDeploy;
192,925✔
530
    ext.deployed = false;
192,925✔
531
    
532
    if (NULL == taosArrayPush(pSnode->triggerList, &ext)) {
385,850✔
533
      taosWUnLockLatch(&pSnode->lock);
×
534
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
535
    }
536
    taosWUnLockLatch(&pSnode->lock);
192,925✔
537
    
538
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
192,925✔
539
    break;
192,925✔
540
  }
541
  
542
_return:
193,161✔
543

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

550
  return code;
193,161✔
551
}
552

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

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

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

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

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

614
  return code;
668,115✔
615
}
616

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

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

632
_exit:
611,733✔
633

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

638
  return code;
611,733✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
14,267✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
×
682

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

687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
82,242✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,152,831✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
64,918,506✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
48,765,675✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
48,765,675✔
725
  }
726

727
_exit:
16,152,831✔
728

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

733
  return code;
16,152,831✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
359,294✔
739
  int32_t num = taosArrayGetSize(pList);
359,294✔
740
  for (int32_t i = 0; i < num; ++i) {
1,106,164✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
919,471✔
742
    if (pScan->readFromCache) {
919,471✔
743
      return pScan->scanPlan;
172,601✔
744
    }
745
  }
746

747
  return NULL;
186,693✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
657,542✔
775
}
776

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

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

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

805
_exit:
189,985✔
806

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

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

826
  mndReleaseSnode(pMnode, pSnode);
×
827

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

833
  return TSDB_CODE_SUCCESS;
×
834
}
835

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

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

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

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

878
  SStreamTaskAddr addr;
192,723✔
879
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
193,161✔
880
  if (triggerReaderNum > 0) {
193,161✔
881
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
192,189✔
882
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
192,189✔
883
  }
884
  
885
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
430,307✔
886
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
237,146✔
887
    addr.taskId = pStatus->id.taskId;
237,146✔
888
    addr.nodeId = pStatus->id.nodeId;
237,146✔
889
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
237,146✔
890
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
474,292✔
891
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
237,146✔
892
  }
893

894
  pMsg->leaderSnodeId = pStream->mainSnodeId;
193,161✔
895
  pMsg->streamName = pInfo->streamName;
193,161✔
896

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

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

904
_exit:
189,985✔
905

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

915

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

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

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

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

960

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

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

984

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

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

1002
  return code;
763,488✔
1003
}
1004

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

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

1013

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

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

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

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

1038
_exit:
1,026,753✔
1039

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

1044
  return code;
1,026,753✔
1045
}
1046

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

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

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

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

1085
_exit:
×
1086

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

1091
  return 0;
×
1092
}
1093

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

1108
  int32_t snodeTarget = taosRand() % snodeNum;
606,950✔
1109

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

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

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

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

1147
_exit:
609,650✔
1148

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

1153
  if (0 == snodeId) {
609,650✔
1154
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,700✔
1155
  }
1156

1157
  return snodeId;
609,650✔
1158
}
1159

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

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

1171
_exit:
804,894✔
1172

1173
  if (0 == snodeId) {
804,894✔
1174
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1175
  }
1176

1177
  return snodeId;
804,894✔
1178
}
1179

1180

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

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

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

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

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

1213
_exit:
193,161✔
1214

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

1219
  return code;
193,161✔
1220
}
1221

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

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

1248
_exit:
296,450✔
1249

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

1254
  return code;
296,450✔
1255
}
1256

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

1286
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
78,948✔
1287
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
78,948✔
1288
      
1289
      void *pIter = NULL;
78,948✔
1290
      while (1) {
414,863✔
1291
        SStmTaskDeploy info = {0};
493,811✔
1292
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
493,811✔
1293
        if (pIter == NULL) {
493,811✔
1294
          break;
78,948✔
1295
        }
1296
      
1297
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
414,863✔
1298
          pState = taosArrayReserve(pInfo->trigReaders, 1);
123,905✔
1299

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

1309
        sdbRelease(pSdb, pVgroup);
414,863✔
1310
      }
1311
      break;
78,948✔
1312
    }
1313
    default:
972✔
1314
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
972✔
1315
      break;
972✔
1316
  }
1317

1318
_exit:
193,161✔
1319

1320
  mndReleaseDb(pCtx->pMnode, pDb);
193,161✔
1321

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

1326
  return code;
193,161✔
1327
}
1328

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

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

1354
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
357,840✔
1355
  if (NULL == ppRes) {
357,840✔
1356
    SArray* pRes = taosArrayInit(1, sizeof(addr));
339,174✔
1357
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
339,174✔
1358
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
678,348✔
1359
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
339,174✔
1360
  } else {
1361
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
37,332✔
1362
  }
1363

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

1370
  nodesDestroyNode((SNode*)pSubplan);
357,840✔
1371

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

1376
  return code;
357,840✔
1377
}
1378

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

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

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

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

1415
  return code;
80,275✔
1416
}
1417

1418

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

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

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

1447
_exit:
359,976✔
1448

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

1453
  return code;
359,976✔
1454
}
1455

1456

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

1466
  
1467
  for (int32_t i = 0; i < calcTasksNum; ++i) {
630,904✔
1468
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
437,743✔
1469
    if (pScan->readFromCache) {
437,743✔
1470
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
80,275✔
1471
      continue;
80,275✔
1472
    }
1473
    
1474
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
357,468✔
1475
    for (int32_t m = 0; m < vgNum; ++m) {
714,936✔
1476
      pState = tdListReserve(pInfo->calcReaders);
357,468✔
1477
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
357,468✔
1478

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

1484
_exit:
193,161✔
1485

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

1490
  return code;
193,161✔
1491
}
1492

1493

1494

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

1522
_exit:
372✔
1523

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

1528
  return code;
372✔
1529
}
1530

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

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

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

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

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

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

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

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

1589
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1590
}
1591

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

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

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

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

1625
_exit:
668,115✔
1626

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

1631
  return code;
668,115✔
1632
}
1633

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

1642
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
668,115✔
1643

1644
  SNode* pTmp = NULL;
668,115✔
1645
  WHERE_EACH(pTmp, pPlan->pChildren) {
2,038,098✔
1646
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,369,983✔
1647
      ERASE_NODE(pPlan->pChildren);
1,313,601✔
1648
      continue;
1,313,601✔
1649
    }
1650
    WHERE_NEXT;
56,382✔
1651
  }
1652
  nodesClearList(pPlan->pChildren);
668,115✔
1653
  pPlan->pChildren = NULL;
668,115✔
1654

1655
  if (NULL == pPlan->pParents) {
668,115✔
1656
    goto _exit;
611,733✔
1657
  }
1658

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

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

1678
  return code;
668,115✔
1679
}
1680

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

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

1696
_exit:
611,733✔
1697

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

1702
  return code;
611,733✔
1703
}
1704

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

1720
  if (dagIdx >= 0) {
203,787✔
1721
    subQ = true;
13,802✔
1722
    pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
13,802✔
1723
  } else {
1724
    pDag = *pRoot;
189,985✔
1725
  }
1726

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

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

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

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

1757
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
203,787✔
1758
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
203,787✔
1759
  
1760
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
815,148✔
1761
    totalTaskNum = 0;
611,361✔
1762

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

1769
    taskIdx = 0;
611,361✔
1770

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

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

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

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

1795
      for (int32_t n = 0; n < taskNum; ++n) {
1,299,795✔
1796
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
667,743✔
1797
        pState = taosArrayReserve(deployList, 1);
667,743✔
1798

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

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

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

1825
        if (subQ) {
667,743✔
1826
          if (i == 0) {
52,710✔
1827
            SStmTaskSrcAddr addr = {0};
41,406✔
1828
            SDownstreamSourceNode* pSource = NULL;
41,406✔
1829

1830
            TAOS_CHECK_EXIT(nodesMakeNode(QUERY_NODE_DOWNSTREAM_SOURCE, (SNode**)&pSource));
41,406✔
1831

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

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

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

1847
            TAOS_CHECK_EXIT(nodesListMakeStrictAppend(subEP, (SNode *)pSource));
41,406✔
1848
          }
1849
        } else {
1850
          TAOS_CHECK_EXIT(nodesCloneList(*subEP, &plan->pSubQ));
615,033✔
1851
        }
1852
      }
1853

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

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

1862
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
611,361✔
1863

1864
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
611,361✔
1865

1866
    nodesDestroyNode((SNode *)(*pRoot));
611,361✔
1867
    *pRoot = NULL;
611,361✔
1868
    
1869
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)pRoot));
611,361✔
1870
    if (subQ) {
611,361✔
1871
      pDag = (SQueryPlan *)nodesListGetNode((*pRoot)->pChildren, dagIdx);
41,406✔
1872
    } else {
1873
      pDag = *pRoot;
569,955✔
1874
    }
1875

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

1879
  if (!subQ) {
203,787✔
1880
    for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
759,940✔
1881
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
569,955✔
1882
      TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
569,955✔
1883
    }
1884
  }
1885
  
1886
  pInfo->runnerNum += totalTaskNum;
203,787✔
1887
  
1888
_exit:
203,787✔
1889

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

1894
  taosArrayDestroy(deployTaskList);
203,787✔
1895

1896
  return code;
203,787✔
1897
}
1898

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

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

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

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

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

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

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

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

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

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

1959
        pRunner++;
372✔
1960
      }
1961

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

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

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

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

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

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

1977
_exit:
372✔
1978

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

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

1986
  return code;
372✔
1987
}
1988

1989

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

1998
_exit:
183,457✔
1999

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

2004
  return code;
183,457✔
2005
}
2006

2007

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

2015
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
823,670✔
2016
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
633,685✔
2017
    (void)mstWaitLock(&pVg->lock, true);
633,685✔
2018

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

2025
    for (int32_t i = 0; i < taskNum; ++i) {
9,727,309✔
2026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
9,093,624✔
2027
      if (pExt->deploy.task.streamId != streamId || STREAM_READER_TASK != pExt->deploy.task.type) {
9,093,624✔
2028
        continue;
8,502,186✔
2029
      }
2030

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

2041
    taosRUnLockLatch(&pVg->lock);
633,685✔
2042
  }
2043

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

2049
  return code;
189,985✔
2050
}
2051

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

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

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

2067
  FOREACH(pNode, pPlan->pChildren) {
203,787✔
2068
    SQueryPlan *calcSubQPlan = (SQueryPlan *)pNode;
13,802✔
2069

2070
    subQueryPlans += calcSubQPlan->numOfSubplans;
13,802✔
2071
  }
2072

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

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

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

2086
  taosHashClear(mStreamMgmt.toUpdateScanMap);
189,985✔
2087
  mStreamMgmt.toUpdateScanNum = 0;
189,985✔
2088

2089
  TAOS_CHECK_EXIT(msmUpdateCalcReaderTasks(pStream, pSubEP));
189,985✔
2090

2091
_exit:
189,985✔
2092
  nodesDestroyNode((SNode *)pPlan);
189,985✔
2093
  if (pSubEP) {
189,985✔
2094
    nodesDestroyList(pSubEP);
12,238✔
2095
  }
2096

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

2101
  return code;
189,985✔
2102
}
2103

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

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

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

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

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

2127
  return code;
193,161✔
2128
}
2129

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

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

2169
_exit:
×
2170

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

2176
  return code;
×
2177
}
2178

2179

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

2185
  pStatus->lastActionTs = INT64_MIN;
186,633✔
2186

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

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

2200
  if (initList) {
186,633✔
2201
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2202

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

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

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

2224
  return code;
186,633✔
2225
}
2226

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

2233
  if (NULL == pStatus) {
193,161✔
2234
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
186,633✔
2235

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

2238
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
186,633✔
2239
  }
2240
  
2241
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
193,161✔
2242

2243
  mstLogSStmStatus("stream deployed", streamId, pStatus);
193,161✔
2244

2245
_exit:
193,161✔
2246

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

2256
  return code;
193,161✔
2257
}
2258

2259

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

2264
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
13,200✔
2265
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
5,676✔
2266
    (void)mstWaitLock(&pVg->lock, true);
5,676✔
2267

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

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

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

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

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

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

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

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

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

2337
  size_t keyLen = 0;
7,524✔
2338
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
550,234✔
2339
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
542,710✔
2340
    if (*pStreamId == streamId) {
542,710✔
2341
      int64_t taskId = *(pStreamId + 1);
45,094✔
2342
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
45,094✔
2343
      if (code) {
45,094✔
2344
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2345
      } else {
2346
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
45,094✔
2347
      }
2348
    }
2349
  }
2350

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

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

2368
  mstResetSStmStatus(pStatus);
7,086✔
2369

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

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

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

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

2406
  TAOS_CHECK_EXIT(code);
193,719✔
2407

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

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

2424
_exit:
193,719✔
2425

2426
  mndReleaseStream(pCtx->pMnode, pStream);
193,719✔
2427

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

2432
  return code;
193,719✔
2433
}
2434

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

2467
_exit:
1,116✔
2468

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

2473
  return code;
1,116✔
2474
}
2475

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

2507
_exit:
2508

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

2513
  return code;
2514
}
2515
*/
2516

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

2566
_exit:
372✔
2567

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

2572
  return code;
372✔
2573
}
2574

2575

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

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

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

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

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

2603
  TAOS_CHECK_EXIT(code);
1,488✔
2604

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

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

2635
_exit:
1,488✔
2636

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

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

2646
  return code;
1,488✔
2647
}
2648

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

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

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

2694
  return TSDB_CODE_SUCCESS;
×
2695
}
2696

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

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

2703
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
438✔
2704

2705
_exit:
438✔
2706

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

2713
  return code;
438✔
2714
}
2715

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

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

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

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

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

2736
_exit:
×
2737

2738
  taosHashRelease(mStreamMgmt.streamMap, pStream);
36,034✔
2739

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

2744
  return;
36,034✔
2745
}
2746

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

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

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

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

2766
_exit:
3,444✔
2767

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

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

2774
  return code;
3,444✔
2775
}
2776

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

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

2798
_exit:
47,669✔
2799

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

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

2816
    pStatus = (SStmStatus*)pIter;
×
2817

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

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

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

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

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

2835
  msmStopAllStreamsByGrant(errCode);
×
2836

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

2842
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,518,074✔
2843
  int32_t code = TSDB_CODE_SUCCESS;
1,518,074✔
2844
  int32_t lino = 0;
1,518,074✔
2845
  int64_t streamId = pDeploy->task.streamId;
1,518,074✔
2846
  
2847
  switch (pDeploy->task.type) {
1,518,074✔
2848
    case STREAM_READER_TASK:
657,542✔
2849
      if (NULL == pStream->readerTasks) {
657,542✔
2850
        pStream->streamId = streamId;
249,287✔
2851
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
249,287✔
2852
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
249,287✔
2853
      }
2854
      
2855
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,315,084✔
2856
      break;
657,542✔
2857
    case STREAM_TRIGGER_TASK:
192,975✔
2858
      pStream->streamId = streamId;
192,975✔
2859
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
192,975✔
2860
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
192,975✔
2861
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
192,975✔
2862
      break;
192,975✔
2863
    case STREAM_RUNNER_TASK:
667,557✔
2864
      if (NULL == pStream->runnerTasks) {
667,557✔
2865
        pStream->streamId = streamId;
196,383✔
2866
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
196,383✔
2867
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
196,383✔
2868
      }      
2869
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,335,114✔
2870
      break;
667,557✔
2871
    default:
×
2872
      break;
×
2873
  }
2874

2875
_exit:
1,518,074✔
2876

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

2881
  return code;
1,518,074✔
2882
}
2883

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

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

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

2909
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,258,175✔
2910
    
2911
    break;
1,258,175✔
2912
  }
2913
  
2914
_exit:
1,518,074✔
2915

2916
  taosHashRelease(pHash, pStream);
1,518,074✔
2917

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

2924
  return code;
1,518,074✔
2925
}
2926

2927

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

2933
  for (int32_t i = 0; i < taskNum; ++i) {
1,760,116✔
2934
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,518,074✔
2935
    if (pExt->deployed) {
1,518,074✔
2936
      continue;
×
2937
    }
2938

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

2942
    (void)atomic_add_fetch_32(deployed, 1);
1,518,074✔
2943
  }
2944

2945
_exit:
242,042✔
2946

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

2951
  return code;
242,042✔
2952
}
2953

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

2961
  mstDebug("start to add stream vgroup tasks deploy");
64,668✔
2962
  
2963
  for (int32_t i = 0; i < vgNum; ++i) {
420,984✔
2964
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
356,316✔
2965

2966
    msmUpdateVgroupUpTs(pCtx, *vgId);
356,316✔
2967

2968
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
356,316✔
2969
    if (NULL == pVg) {
356,316✔
2970
      continue;
212,425✔
2971
    }
2972

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

2986
_exit:
64,668✔
2987

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

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

2996
  return code;
64,668✔
2997
}
2998

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

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

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

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

3024
_exit:
50,103✔
3025

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

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

3034
  return code;
50,103✔
3035
}
3036

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

3048
_exit:
688,807✔
3049

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

3054
  return code;
688,807✔
3055
}
3056

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

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

3068
  while (1) {
259,899✔
3069
    pIter = taosHashIterate(pCtx->deployStm, pIter);
325,333✔
3070
    if (pIter == NULL) {
325,333✔
3071
      break;
65,434✔
3072
    }
3073
    
3074
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
259,899✔
3075
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
519,798✔
3076

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

3081
    mstClearSStmStreamDeploy(pDeploy);
259,899✔
3082
    
3083
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
259,899✔
3084
  }
3085
  
3086
_exit:
65,434✔
3087

3088
  if (pIter) {
65,434✔
3089
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
3090
  }
3091

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

3099
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
64,668✔
3100
  int32_t code = TSDB_CODE_SUCCESS;
64,668✔
3101
  int32_t lino = 0;
64,668✔
3102
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
64,668✔
3103
  SStmVgTasksToDeploy* pVg = NULL;
64,668✔
3104
  
3105
  for (int32_t i = 0; i < vgNum; ++i) {
420,984✔
3106
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
356,316✔
3107
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
356,316✔
3108
    if (NULL == pVg) {
356,316✔
3109
      continue;
212,425✔
3110
    }
3111

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

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

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

3140
      mstDestroySStmTaskToDeployExt(pExt);
×
3141

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

3150
_exit:
64,668✔
3151

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

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

3161
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
142,111✔
3162
  if (!GOT_SNODE(snodeId)) {
142,111✔
3163
    return;
24,558✔
3164
  }
3165
  
3166
  int32_t code = TSDB_CODE_SUCCESS;
117,553✔
3167
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
117,553✔
3168
  if (NULL == pSnode) {
117,553✔
3169
    return;
67,450✔
3170
  }
3171

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

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

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

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

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

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

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

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

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

3239
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,447,515✔
3240
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,447,515✔
3241
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
64,668✔
3242
  }
3243

3244
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,447,515✔
3245
    msmCleanDeployedSnodeTasks(pHb->snodeId);
142,111✔
3246
  }
3247
}
16,447,515✔
3248

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

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

3274
_exit:
191,231✔
3275

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

3280
  return code;
191,231✔
3281
}
3282

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

3299
_exit:
×
3300

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

3305
  return code;
×
3306
}
3307

3308

3309

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

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

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

3349
_exit:
237,677✔
3350

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

3355
  return code;
237,677✔
3356
}
3357

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

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

3379
_exit:
3,444✔
3380

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

3386
  return code;
3,444✔
3387
}
3388

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

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

3399
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
277,105✔
3400
  for (int32_t i = 0; i < readerNum; ++i) {
613,174✔
3401
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
336,069✔
3402
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
336,069✔
3403
      return false;
×
3404
    }
3405
  }
3406

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

3415
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
277,105✔
3416
  SListNode* pNode = listHead(pStream->calcReaders);
277,105✔
3417
  for (int32_t i = 0; i < readerNum; ++i) {
841,598✔
3418
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
564,493✔
3419
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
564,493✔
3420
      return false;
×
3421
    }
3422
    pNode = TD_DLIST_NODE_NEXT(pNode);
564,493✔
3423
  }
3424

3425
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
851,686✔
3426
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
660,455✔
3427
    for (int32_t m = 0; m < runnerNum; ++m) {
1,330,358✔
3428
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
755,777✔
3429
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
755,777✔
3430
        return false;
85,874✔
3431
      }
3432
    }
3433
  }
3434
  
3435
  return true;
191,231✔
3436
}
3437

3438

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

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

3456
  stopped = atomic_load_8(&pStatus->stopped);
9,919,733✔
3457
  if (stopped) {
9,919,733✔
3458
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3459
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3460
    return;
×
3461
  }
3462
  
3463
  switch (pMsg->status) {
9,919,733✔
3464
    case STREAM_STATUS_INIT:      
9,913,120✔
3465
      if (STREAM_TRIGGER_TASK != pMsg->type) {
9,913,120✔
3466
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
9,526,293✔
3467
        return;
9,526,293✔
3468
      }
3469
      
3470
      if (INT64_MIN == pStatus->lastActionTs) {
386,827✔
3471
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3472
        return;
×
3473
      }
3474
      
3475
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
386,827✔
3476
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
109,722✔
3477
        return;
109,722✔
3478
      }
3479

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

3502
_exit:
283,718✔
3503

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

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

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

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

3520
_exit:
236,942✔
3521

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

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

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

3551
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
3,288,147✔
3552
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,648,707✔
3553
    if (NULL == pStatus->detailStatus) {
1,648,707✔
3554
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
192,029✔
3555
      if (NULL == pStatus->detailStatus) {
192,029✔
3556
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3557
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3558
      }
3559
    }
3560
    
3561
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,648,707✔
3562
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,648,707✔
3563
  }
3564

3565
_exit:
1,639,440✔
3566

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

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

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

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

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

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

3607
    if ((*ppStatus)->status != pTask->status) {
25,773,014✔
3608
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,548,827✔
3609
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,093,819✔
3610
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,455,008✔
3611
        if (pStream->triggerTask) {
×
3612
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3613
        }
3614
        
3615
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3616
      }
3617
    }
3618
    
3619
    (*ppStatus)->errCode = pTask->errorCode;
25,773,014✔
3620
    (*ppStatus)->status = pTask->status;
25,773,014✔
3621
    (*ppStatus)->lastUpTs = pCtx->currTs;
25,773,014✔
3622
    
3623
    if (STREAM_STATUS_RUNNING != pTask->status) {
25,773,014✔
3624
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
9,919,733✔
3625
    }
3626
    
3627
    if (STREAM_TRIGGER_TASK == pTask->type) {
25,773,014✔
3628
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
3,288,147✔
3629
    }
3630
  }
3631

3632
_exit:
1,068,742✔
3633

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

3638
  return code;
1,068,742✔
3639
}
3640

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

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

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

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

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

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

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

3741
_exit:
×
3742

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

3749
  return code;
×
3750
}
3751

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

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

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

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

3780
_exit:
×
3781

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

3786
  return code;
×
3787
}
3788

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

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

3806
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
382,462✔
3807
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
191,231✔
3808

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

3811
  return;
191,231✔
3812

3813
_exit:
×
3814

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

3818

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

3828
  SStreamTaskUndeploy undeploy;
46,853✔
3829
  for (int32_t i = 0; i < dropNum; ++i) {
284,530✔
3830
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
237,677✔
3831
    undeploy.task = *pTask;
237,677✔
3832
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
237,677✔
3833
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
237,677✔
3834

3835
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
475,354✔
3836
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
237,677✔
3837

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

3841
  return;
46,853✔
3842

3843
_exit:
×
3844

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

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

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

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

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

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

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

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

3878
_exit:
×
3879

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

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

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

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

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

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

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

3916
_exit:
3,444✔
3917

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

3925

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

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

3934
  while (1) {
241,528✔
3935
    pIter = taosHashIterate(pCtx->actionStm, pIter);
363,867✔
3936
    if (pIter == NULL) {
363,867✔
3937
      break;
122,339✔
3938
    }
3939

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

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

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

3956
    if (STREAM_ACT_START & pAction->actions) {
194,675✔
3957
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
191,231✔
3958
    }
3959
  }
3960
  
3961
_exit:
122,339✔
3962

3963
  if (pIter) {
122,339✔
3964
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3965
  }
3966

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

3971
  return code;
122,339✔
3972
}
3973

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

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

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

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

4002
        continue;
×
4003
      }
4004

4005
      return code;
2,938✔
4006
    }
4007

4008
    break;
4009
  }
4010

4011
_exit:
135,876✔
4012

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

4017
  return code;  
135,876✔
4018
}
4019

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

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

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

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

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

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

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

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

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

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

4094
  msmWatchCheckStreamMap(pCtx);
×
4095

4096
_exit:
368✔
4097

4098
  mStreamMgmt.lastTaskId += 100000;
368✔
4099

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

4102
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
368✔
4103

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

4108
  return code;
368✔
4109
}
4110

4111

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

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

4123
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
12,764✔
4124
  if (GOT_SNODE(pReq->snodeId)) {
12,764✔
4125
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,165✔
4126
  }
4127

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

4133
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
12,764✔
4134
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
368✔
4135
  }
4136

4137
_exit:
12,764✔
4138

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

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

4147
  return code;
12,764✔
4148
}
4149

4150
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,080,502✔
4151
  int32_t listSize = taosArrayGetSize(pOReaders);
5,080,502✔
4152
  int32_t totalSize = 0;
5,080,502✔
4153
  
4154
  for (int32_t i = 0; i < listSize; ++i) {
5,407,603✔
4155
    SArray* pList = taosArrayGetP(pOReaders, i);
327,101✔
4156
    totalSize += taosArrayGetSize(pList);
327,101✔
4157
  }
4158

4159
  return totalSize;
5,080,502✔
4160
}
4161

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

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

4171

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

4176
  if (NULL == pStatus->trigOReaders) {
59,304✔
4177
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
51,509✔
4178
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
51,509✔
4179
  }
4180

4181
  while (true) {
51,509✔
4182
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
110,813✔
4183

4184
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
110,813✔
4185
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
51,509✔
4186
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
51,509✔
4187

4188
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
103,018✔
4189
      continue;
51,509✔
4190
    }
4191

4192
    *ppRes = *ppOReaderList;
59,304✔
4193
    break;
59,304✔
4194
  }
4195

4196
_exit:
59,304✔
4197

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

4202
  return code;
59,304✔
4203
}
4204

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

4211
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
70,029✔
4212
  for (int32_t i = 0; i < readerNum; ++i) {
145,077✔
4213
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
85,773✔
4214
    if (pReader->id.nodeId == vgId) {
85,773✔
4215
      readerExists = true;
10,725✔
4216
      break;
10,725✔
4217
    }
4218
  }
4219

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

4230
_exit:
10,725✔
4231

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

4236
  return code;
70,029✔
4237
}
4238

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

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

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

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

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

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

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

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

4293
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
61,616✔
4294
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
61,616✔
4295
  
4296
  for (int32_t i = 0; i < tbNum; ++i) {
241,778✔
4297
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
180,162✔
4298
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
180,162✔
4299
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
360,324✔
4300
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
180,162✔
4301
  }
4302

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

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

4327
_exit:
61,616✔
4328

4329
  tFreeSStreamMgmtReq(pMgmtReq);
61,616✔
4330
  taosMemoryFree(pMgmtReq);
61,616✔
4331

4332
  tSimpleHashCleanup(pVgs);
61,616✔
4333
  mstDestroyDbVgroupsHash(pDbVgroups);
61,616✔
4334

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

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

4345
_final:
61,616✔
4346

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

4353
  return finalCode;
61,616✔
4354
}
4355

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

4370
_exit:
2,508✔
4371

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

4376
  return code;
2,508✔
4377
}
4378

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

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

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

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

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

4422
_exit:
2,508✔
4423

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

4428
  return code;
2,508✔
4429
}
4430

4431

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

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

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

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

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

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

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

4492
_exit:
2,508✔
4493

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

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

4506

4507
_final:
2,508✔
4508

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

4515
  return finalCode;
2,508✔
4516
}
4517

4518

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

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

4536
_exit:
64,124✔
4537

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

4542
  return code;
64,124✔
4543
}
4544

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

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

4570
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
64,124✔
4571
  }
4572

4573
_exit:
18,121✔
4574

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

4579
  return code;
18,121✔
4580
}
4581

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

4587
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,353,375✔
4588
  if (GOT_SNODE(pReq->snodeId)) {
16,217,499✔
4589
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,212,963✔
4590
  }
4591
  
4592
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,217,499✔
4593
    msmHandleStreamActions(pCtx);
47,669✔
4594
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
47,669✔
4595
  }
4596

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

4603
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,217,499✔
4604
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
64,668✔
4605
  } else {
4606
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,152,831✔
4607
  }
4608

4609
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,217,499✔
4610
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
117,553✔
4611
  }
4612

4613
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,217,499✔
4614
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
65,434✔
4615
  }
4616

4617
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,217,499✔
4618
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,068,742✔
4619
  }
4620

4621
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,217,499✔
4622
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
122,339✔
4623
  }
4624

4625
_exit:
16,217,499✔
4626

4627
  if (code) {
16,353,375✔
4628
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
135,876✔
4629
  }
4630

4631
  return code;
16,353,375✔
4632
}
4633

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

4639
  if (TSDB_CODE_SUCCESS != code) {
16,447,515✔
4640
    goto _exit;
135,876✔
4641
  }
4642

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

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

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

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

4669
_exit:
16,447,515✔
4670

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

4679

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

4685
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,447,515✔
4686

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

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

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

4716
_exit:
16,447,515✔
4717

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

4720
  msmCleanStreamGrpCtx(pHb);
16,447,515✔
4721
  msmClearStreamToDeployMaps(pHb);
16,447,515✔
4722

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

4727
  return code;
16,447,515✔
4728
}
4729

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

4735
  mstInfo("start to process mnode become leader");
422,702✔
4736

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

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

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

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

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

4759
  streamRemoveVnodeLeader(MNODE_HANDLE);
518,570✔
4760

4761
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
518,570✔
4762
    taosWLockLatch(&mStreamMgmt.runtimeLock);
422,702✔
4763
    msmDestroyRuntimeInfo(pMnode);
422,702✔
4764
    mStreamMgmt.stat.inactiveTimes++;
422,702✔
4765
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
422,702✔
4766
  }
4767
}
4768

4769

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

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

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

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

4796
  return true;
8,044✔
4797

4798
_exit:
×
4799

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

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

4806
  return false;
×
4807
}
4808

4809

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

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

4823
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4824

4825
_exit:
39,569✔
4826

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

4831
  return code;
39,569✔
4832
}
4833

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

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

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

4863
    return true;
27,049✔
4864
  }
4865

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

4868
  return true;
667✔
4869
}
4870

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

4883
    pStatus = (SStmStatus*)pIter;
295,957✔
4884

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

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

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

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

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

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

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

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

4937
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
2,922,047✔
4938
  for (int32_t i = 0; i < taskNum; ++i) {
6,269,646✔
4939
    SStmTaskStatus* pTask = *(pList + i);
3,347,599✔
4940

4941
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
3,347,599✔
4942
      continue;
533,135✔
4943
    }
4944
    
4945
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
2,815,376✔
4946
      continue;
2,814,260✔
4947
    }
4948

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

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

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

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

4974
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
145,948✔
4975
  void* pIter = NULL;
145,948✔
4976
  SStmVgStreamStatus* pVg = NULL;
145,948✔
4977
  int64_t streamId = 0;
145,948✔
4978
  
4979
  while (true) {
981,840✔
4980
    pIter = taosHashIterate(pStreams, pIter);
1,127,788✔
4981
    if (NULL == pIter) {
1,127,788✔
4982
      break;
145,948✔
4983
    }
4984

4985
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
981,840✔
4986
    pVg = (SStmVgStreamStatus*)pIter;
981,840✔
4987

4988
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
981,840✔
4989
    if (taskNum > 0) {
981,840✔
4990
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
593,839✔
4991
    }
4992

4993
    taskNum = taosArrayGetSize(pVg->calcReaders);
981,840✔
4994
    if (taskNum > 0) {
981,840✔
4995
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
663,013✔
4996
    }
4997
  }
4998
}
145,948✔
4999

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

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

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

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

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

5025

5026
void msmCheckVgroupStatus(SMnode *pMnode) {
365,636✔
5027
  void* pIter = NULL;
365,636✔
5028
  int32_t code = 0;
365,636✔
5029
  
5030
  while (true) {
1,364,425✔
5031
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,730,061✔
5032
    if (NULL == pIter) {
1,730,061✔
5033
      break;
365,636✔
5034
    }
5035

5036
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,364,425✔
5037
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,364,425✔
5038
      continue;
1,213,319✔
5039
    }
5040
    
5041
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
151,106✔
5042

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

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

5063
    msmCheckVgroupStreamStatus(pVg->streamTasks);
145,948✔
5064
  }
5065
}
365,636✔
5066

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

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

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

5095
  (void)msmSTAddSnodesToMap(pMnode);
1,302✔
5096

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

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

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

5139
  taosHashClear(pSnode->streamTasks);
1,302✔
5140
}
1,302✔
5141

5142

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

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

5157
    if (NULL != pSnode->trigger) {
438,488✔
5158
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
424,908✔
5159
    }
5160

5161
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,753,952✔
5162
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,315,464✔
5163
      if (taskNum > 0) {
1,315,464✔
5164
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,240,287✔
5165
      }
5166
    }
5167
  }
5168
}
41,491✔
5169

5170

5171
void msmCheckSnodeStatus(SMnode *pMnode) {
365,636✔
5172
  void* pIter = NULL;
365,636✔
5173
  
5174
  while (true) {
437,116✔
5175
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
802,752✔
5176
    if (NULL == pIter) {
802,752✔
5177
      break;
365,636✔
5178
    }
5179

5180
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
437,116✔
5181
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
437,116✔
5182
      continue;
384,030✔
5183
    }
5184

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

5203
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
41,491✔
5204
  }
5205
}
365,636✔
5206

5207

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

5211
  msmCheckVgroupStatus(pMnode);
365,636✔
5212
  msmCheckSnodeStatus(pMnode);
365,636✔
5213
}
365,636✔
5214

5215
void msmCheckSnodesState(SMnode *pMnode) {
365,636✔
5216
  if (!MST_READY_FOR_SNODE_LOOP()) {
365,636✔
5217
    return;
322,965✔
5218
  }
5219

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

5222
  void* pIter = NULL;
42,671✔
5223
  int32_t snodeId = 0;
42,671✔
5224
  while (true) {
46,481✔
5225
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
89,152✔
5226
    if (NULL == pIter) {
89,152✔
5227
      break;
42,671✔
5228
    }
5229

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

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

5248
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
42,671✔
5249
}
5250

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

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

5263
  return true;
731,272✔
5264
}
5265

5266
void msmHealthCheck(SMnode *pMnode) {
10,009,115✔
5267
  if (!msmCheckNeedHealthCheck(pMnode)) {
10,009,115✔
5268
    return;
9,643,479✔
5269
  }
5270

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

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

5288
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
365,636✔
5289

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

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

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

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

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

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

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

5341
  return code;
21,960✔
5342
}
5343

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

5351
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
843,719✔
5352

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

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

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

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

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

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

5458
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
422,702✔
5459
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
422,702✔
5460

5461
  mStreamMgmt.lastTaskId = 1;
422,702✔
5462

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

5466
  if (activeStreamNum > 0) {
422,702✔
5467
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
712✔
5468
  } else {
5469
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
421,990✔
5470
  }
5471

5472
_exit:
422,702✔
5473

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

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

© 2026 Coveralls, Inc