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

taosdata / TDengine / #4902

27 Dec 2025 02:36PM UTC coverage: 65.642% (-0.09%) from 65.734%
#4902

push

travis-ci

web-flow
fix: ci errors (#34079)

192706 of 293572 relevant lines covered (65.64%)

117199453.55 hits per line

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

72.82
/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() {
738,428✔
34
  SStmQNode* pQNode = NULL;
738,428✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
738,428✔
37
    return;
369,214✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,845,878✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
11,075,268✔
49
    taosHashCleanup(pCtx->deployStm[m]);
9,229,390✔
50
    taosHashCleanup(pCtx->actionStm[m]);
9,229,390✔
51
  }
52
}
1,845,878✔
53

54
void msmDestroyThreadCtxs() {
738,428✔
55
  if (NULL == mStreamMgmt.tCtx) {
738,428✔
56
    return;
369,214✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,215,092✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,845,878✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
369,214✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
738,428✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
738,428✔
94
}
738,428✔
95

96

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

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

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

111
    pStatus = pStream;
560✔
112
  }
113

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

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

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

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

136
_exit:
420✔
137

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

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

145

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

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

200
  pSnode = NULL;
386,870✔
201

202
_exit:
386,870✔
203

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

208
  return code;
386,870✔
209
}
210

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

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

235
  pDnode = NULL;
812,694✔
236

237
_exit:
812,694✔
238

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

243
  return code;
812,694✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
449,919✔
275
  int32_t code = TSDB_CODE_SUCCESS;
449,919✔
276
  int32_t lino = 0;
449,919✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
449,919✔
278
  if (NULL == pStream) {
449,919✔
279
    SStmVgStreamStatus stream = {0};
338,105✔
280
    if (trigReader) {
338,105✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
202,723✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
202,723✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
405,446✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
135,382✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
135,382✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
270,764✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
338,105✔
290
    goto _exit;
338,105✔
291
  }
292
  
293
  if (trigReader) {
111,814✔
294
    if (NULL == pStream->trigReaders) {
21,995✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
21,995✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
21,995✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
43,990✔
300
    goto _exit;
21,995✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
89,819✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
61,806✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
61,806✔
306
  }
307

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

310
_exit:
89,819✔
311

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

320
  return code;
449,919✔
321
}
322

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

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

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

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

350
  return code;
449,919✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
451,039✔
363
    if (NULL == pVg) {
451,039✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
93,913✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
93,913✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
187,826✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
93,913✔
368
      if (TSDB_CODE_SUCCESS == code) {
93,913✔
369
        goto _return;
93,913✔
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);
357,126✔
381
    if (NULL == pVg->taskList) {
357,126✔
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)) {
714,252✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
357,126✔
390
    
391
    taosHashRelease(pVgMap, pVg);
357,126✔
392
    break;
357,126✔
393
  }
394
  
395
_return:
451,039✔
396

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

404
  return code;
451,039✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
557,728✔
409
  int32_t code = TSDB_CODE_SUCCESS;
557,728✔
410
  int32_t lino = 0;
557,728✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
557,728✔
412
  if (NULL == pStream) {
557,728✔
413
    SStmSnodeStreamStatus stream = {0};
142,650✔
414
    if (deployId < 0) {
142,650✔
415
      stream.trigger = pStatus;
3,134✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
139,516✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
139,516✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
279,032✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
142,650✔
423
    goto _exit;
142,650✔
424
  }
425
  
426
  if (deployId < 0) {
415,078✔
427
    if (NULL != pStream->trigger) {
134,267✔
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;
134,267✔
433
    goto _exit;
134,267✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
280,811✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
263,285✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
263,285✔
439
  }
440

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

443
_exit:
280,811✔
444

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

453
  return code;
557,728✔
454
}
455

456

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

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

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

484
  return code;
557,728✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
137,401✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
30,443✔
568
      ext.deployed = false;
30,443✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
60,886✔
570

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

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

614
  return code;
420,327✔
615
}
616

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

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

632
_exit:
402,801✔
633

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

638
  return code;
402,801✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
8,653✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
206✔
682

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

687
  return code;  
206✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
57,008✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
13,593,206✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
51,754,847✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
38,161,641✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
38,161,641✔
725
  }
726

727
_exit:
13,593,206✔
728

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

733
  return code;
13,593,206✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
284,803✔
739
  int32_t num = taosArrayGetSize(pList);
284,803✔
740
  for (int32_t i = 0; i < num; ++i) {
836,784✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
704,355✔
742
    if (pScan->readFromCache) {
704,355✔
743
      return pScan->scanPlan;
152,374✔
744
    }
745
  }
746

747
  return NULL;
132,429✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
451,039✔
775
}
776

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

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

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

805
_exit:
134,267✔
806

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

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

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

865
  pMsg->eventTypes = pStream->pCreate->eventTypes;
137,401✔
866
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
137,401✔
867
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
137,401✔
868
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
137,401✔
869
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
137,401✔
870
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
137,401✔
871
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
59,525✔
872
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
59,525✔
873
  }
874

875
  SStreamTaskAddr addr;
136,946✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
137,401✔
877
  if (triggerReaderNum > 0) {
137,401✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
136,455✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
136,455✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
304,455✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
167,054✔
884
    addr.taskId = pStatus->id.taskId;
167,054✔
885
    addr.nodeId = pStatus->id.nodeId;
167,054✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
167,054✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
334,108✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
167,054✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
137,401✔
892
  pMsg->streamName = pInfo->streamName;
137,401✔
893

894
  if (0 == pInfo->runnerNum) {
137,401✔
895
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
3,134✔
896
    return code;
3,134✔
897
  }
898

899
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
134,267✔
900

901
_exit:
134,267✔
902

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

912

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

920
  pMsg->execReplica = pInfo->runnerReplica;
420,327✔
921
  pMsg->streamName = pInfo->streamName;
420,327✔
922
  //TAOS_CHECK_EXIT(nodesCloneNode((SNode*)plan, (SNode**)&pMsg->pPlan));
923
  pMsg->pPlan = plan;
420,327✔
924
  pMsg->outDBFName = pInfo->pCreate->outDB;
420,327✔
925
  pMsg->outTblName = pInfo->pCreate->outTblName;
420,327✔
926
  pMsg->outTblType = pStream->pCreate->outTblType;
420,327✔
927
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
420,327✔
928
  pMsg->calcNotifyOnly = pStream->pCreate->calcNotifyOnly;
420,327✔
929
  pMsg->topPlan = topPlan;
420,327✔
930
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
420,327✔
931
  pMsg->addOptions = pStream->pCreate->addOptions;
420,327✔
932
  if(pStream->pCreate->trigger.sliding.overlap) {
420,327✔
933
    pMsg->addOptions |= CALC_SLIDING_OVERLAP;
12,201✔
934
  }
935
  pMsg->outCols = pInfo->pCreate->outCols;
420,327✔
936
  pMsg->outTags = pInfo->pCreate->outTags;
420,327✔
937
  pMsg->outStbUid = pStream->pCreate->outStbUid;
420,327✔
938
  pMsg->outStbSversion = pStream->pCreate->outStbSversion;
420,327✔
939
  
940
  pMsg->subTblNameExpr = pInfo->pCreate->subTblNameExpr;
420,327✔
941
  pMsg->tagValueExpr = pInfo->pCreate->tagValueExpr;
420,327✔
942
  pMsg->forceOutCols = pInfo->pCreate->forceOutCols;
420,327✔
943

944
_exit:
420,327✔
945

946
  if (code) {
420,327✔
947
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
948
  }
949
  
950
  return code;
420,327✔
951
}
952

953

954
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
334,732✔
955
  int32_t code = TSDB_CODE_SUCCESS;
334,732✔
956
  int32_t lino = 0;
334,732✔
957
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
334,732✔
958
  SListNode* pNode = pList ? listHead(pList) : NULL;
334,732✔
959
  
960
  for (int32_t i = 0; i < taskNum; ++i) {
784,651✔
961
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
449,919✔
962
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
449,919✔
963
    if (pNode) {
449,919✔
964
      pNode = TD_DLIST_NODE_NEXT(pNode);
222,935✔
965
    }
966
  }
967
  
968
_exit:
334,732✔
969

970
  if (code) {
334,732✔
971
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
972
  }
973
  
974
  return code;
334,732✔
975
}
976

977

978
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
540,202✔
979
  int32_t code = TSDB_CODE_SUCCESS;
540,202✔
980
  int32_t lino = 0;
540,202✔
981
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
540,202✔
982
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
540,202✔
983
  
984
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,097,930✔
985
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
557,728✔
986
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
557,728✔
987
  }
988
  
989
_exit:
540,202✔
990

991
  if (code) {
540,202✔
992
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
993
  }
994

995
  return code;
540,202✔
996
}
997

998
int64_t msmAssignTaskId(void) {
1,007,647✔
999
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,007,647✔
1000
}
1001

1002
int64_t msmAssignTaskSeriousId(void) {
1,007,647✔
1003
  return taosGetTimestampNs();
1,007,647✔
1004
}
1005

1006

1007
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
701,938✔
1008
  int32_t code = TSDB_CODE_SUCCESS;
701,938✔
1009
  int32_t lino = 0;
701,938✔
1010
  bool     noExists = false;
701,938✔
1011
  SStmSnodeStatus* pStatus = NULL;
701,938✔
1012

1013
  while (true) {
1014
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
710,035✔
1015
    if (NULL == pStatus) {
710,035✔
1016
      if (noExists) {
8,097✔
1017
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1018
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1019
      }
1020

1021
      noExists = true;
8,097✔
1022
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
8,097✔
1023
      
1024
      continue;
8,097✔
1025
    }
1026

1027
    *alive = (pStatus->runnerThreadNum >= 0);
701,938✔
1028
    break;
701,938✔
1029
  }
1030

1031
_exit:
701,938✔
1032

1033
  if (code) {
701,938✔
1034
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1035
  }
1036

1037
  return code;
701,938✔
1038
}
1039

1040
int32_t msmRetrieveStaticSnodeId(SMnode* pMnode, SStreamObj* pStream) {
271,668✔
1041
  int32_t code = TSDB_CODE_SUCCESS;
271,668✔
1042
  int32_t lino = 0;
271,668✔
1043
  bool alive = false;
271,668✔
1044
  int32_t mainSnodeId = atomic_load_32(&pStream->mainSnodeId);
271,668✔
1045
  int32_t snodeId = mainSnodeId;
271,668✔
1046
  int64_t streamId = pStream->pCreate->streamId;
271,668✔
1047
  
1048
  while (true) {
1049
    TAOS_CHECK_EXIT(msmIsSnodeAlive(pMnode, snodeId, streamId, &alive));
271,668✔
1050

1051
    if (alive) {
271,668✔
1052
      return snodeId;
271,668✔
1053
    }
1054
    
1055
    if (snodeId == mainSnodeId) {
×
1056
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
1057
      if (NULL == pSnode) {
×
1058
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1059
        return 0;
×
1060
      }
1061
      
1062
      if (pSnode->replicaId <= 0) {
×
1063
        mstsError("no available snode now, mainSnodeId:%d, replicaId:%d", mainSnodeId, pSnode->replicaId);
×
1064
        mndReleaseSnode(pMnode, pSnode);
×
1065
        return 0;
×
1066
      }
1067

1068
      snodeId = pSnode->replicaId;
×
1069
      mndReleaseSnode(pMnode, pSnode);
×
1070
      
1071
      continue;
×
1072
    }
1073

1074
    mstsError("no available snode now, mainSnodeId:%d, followerSnodeId:%d", mainSnodeId, snodeId);
×
1075
    return 0;
×
1076
  }
1077

1078
_exit:
×
1079

1080
  if (code) {
×
1081
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1082
  }
1083

1084
  return 0;
×
1085
}
1086

1087
int32_t msmAssignRandomSnodeId(SMnode* pMnode, int64_t streamId) {
416,063✔
1088
  int32_t code = TSDB_CODE_SUCCESS;
416,063✔
1089
  int32_t lino = 0;
416,063✔
1090
  int32_t snodeIdx = 0;
416,063✔
1091
  int32_t snodeId = 0;
416,063✔
1092
  void      *pIter = NULL;
416,063✔
1093
  SSnodeObj *pObj = NULL;
416,063✔
1094
  bool alive = false;
416,063✔
1095
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
416,063✔
1096
  if (snodeNum <= 0) {
416,063✔
1097
    mstsInfo("no available snode now, num:%d", snodeNum);
2,410✔
1098
    goto _exit;
2,410✔
1099
  }
1100

1101
  int32_t snodeTarget = taosRand() % snodeNum;
413,653✔
1102

1103
  while (1) {
1104
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
430,270✔
1105
    if (pIter == NULL) {
430,270✔
1106
      if (0 == snodeId) {
×
1107
        mstsError("no alive snode now, snodeNum:%d", snodeNum);
×
1108
        break;
×
1109
      }
1110
      
1111
      snodeId = 0;
×
1112
      continue;
×
1113
    }
1114

1115
    code = msmIsSnodeAlive(pMnode, pObj->id, streamId, &alive);
430,270✔
1116
    if (code) {
430,270✔
1117
      sdbRelease(pMnode->pSdb, pObj);
×
1118
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1119
      pObj = NULL;
×
1120
      TAOS_CHECK_EXIT(code);
×
1121
    }
1122
    
1123
    if (!alive) {
430,270✔
1124
      sdbRelease(pMnode->pSdb, pObj);
×
1125
      continue;
×
1126
    }
1127

1128
    snodeId = pObj->id;
430,270✔
1129
    if (snodeIdx == snodeTarget) {
430,270✔
1130
      sdbRelease(pMnode->pSdb, pObj);
413,653✔
1131
      sdbCancelFetch(pMnode->pSdb, pIter);
413,653✔
1132
      pObj = NULL;
413,653✔
1133
      goto _exit;
413,653✔
1134
    }
1135

1136
    sdbRelease(pMnode->pSdb, pObj);
16,617✔
1137
    snodeIdx++;
16,617✔
1138
  }
1139

1140
_exit:
416,063✔
1141

1142
  if (code) {
416,063✔
1143
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1144
  }
1145

1146
  if (0 == snodeId) {
416,063✔
1147
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,410✔
1148
  }
1149

1150
  return snodeId;
416,063✔
1151
}
1152

1153
int32_t msmAssignTaskSnodeId(SMnode* pMnode, SStreamObj* pStream, bool isStatic) {
540,202✔
1154
  int64_t streamId = pStream->pCreate->streamId;
540,202✔
1155
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
540,202✔
1156
  int32_t snodeId = 0;
540,202✔
1157
  if (snodeNum <= 0) {
540,202✔
1158
    mstsInfo("no available snode now, num:%d", snodeNum);
×
1159
    goto _exit;
×
1160
  }
1161

1162
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
540,202✔
1163

1164
_exit:
540,202✔
1165

1166
  if (0 == snodeId) {
540,202✔
1167
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1168
  }
1169

1170
  return snodeId;
540,202✔
1171
}
1172

1173

1174
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
137,401✔
1175
  int32_t code = TSDB_CODE_SUCCESS;
137,401✔
1176
  int32_t lino = 0;
137,401✔
1177
  int64_t streamId = pStream->pCreate->streamId;
137,401✔
1178

1179
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
137,401✔
1180
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
137,401✔
1181

1182
  pInfo->triggerTask->id.taskId = pCtx->triggerTaskId;
137,401✔
1183
  pInfo->triggerTask->id.deployId = 0;
137,401✔
1184
  pInfo->triggerTask->id.seriousId = msmAssignTaskSeriousId();
137,401✔
1185
  pInfo->triggerTask->id.nodeId = pCtx->triggerNodeId;
137,401✔
1186
  pInfo->triggerTask->id.taskIdx = 0;
137,401✔
1187
  pInfo->triggerTask->type = STREAM_TRIGGER_TASK;
137,401✔
1188
  pInfo->triggerTask->lastUpTs = pCtx->currTs;
137,401✔
1189
  pInfo->triggerTask->pStream = pInfo;
137,401✔
1190

1191
  SStmTaskDeploy info = {0};
137,401✔
1192
  info.task.type = pInfo->triggerTask->type;
137,401✔
1193
  info.task.streamId = streamId;
137,401✔
1194
  info.task.taskId =  pInfo->triggerTask->id.taskId;
137,401✔
1195
  info.task.seriousId = pInfo->triggerTask->id.seriousId;
137,401✔
1196
  info.task.nodeId =  pInfo->triggerTask->id.nodeId;
137,401✔
1197
  info.task.taskIdx =  pInfo->triggerTask->id.taskIdx;
137,401✔
1198
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pInfo, &info, pStream));
137,401✔
1199
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
137,401✔
1200
  
1201
  (void)atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
137,401✔
1202

1203
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
137,401✔
1204
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
137,401✔
1205

1206
_exit:
137,401✔
1207

1208
  if (code) {
137,401✔
1209
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1210
  }
1211

1212
  return code;
137,401✔
1213
}
1214

1215
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
224,718✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
224,718✔
1217
  int32_t lino = 0;
224,718✔
1218

1219
  pState->id.taskId = msmAssignTaskId();
224,718✔
1220
  pState->id.deployId = 0;
224,718✔
1221
  pState->id.seriousId = msmAssignTaskSeriousId();
224,718✔
1222
  pState->id.nodeId = nodeId;
224,718✔
1223
  pState->id.taskIdx = 0;
224,718✔
1224
  pState->type = STREAM_READER_TASK;
224,718✔
1225
  pState->flags = STREAM_FLAG_TRIGGER_READER;
224,718✔
1226
  pState->status = STREAM_STATUS_UNDEPLOYED;
224,718✔
1227
  pState->lastUpTs = pCtx->currTs;
224,718✔
1228
  pState->pStream = pInfo;
224,718✔
1229
  
1230
  SStmTaskDeploy info = {0};
224,718✔
1231
  info.task.type = pState->type;
224,718✔
1232
  info.task.streamId = streamId;
224,718✔
1233
  info.task.taskId = pState->id.taskId;
224,718✔
1234
  info.task.flags = pState->flags;
224,718✔
1235
  info.task.seriousId = pState->id.seriousId;
224,718✔
1236
  info.task.nodeId = pState->id.nodeId;
224,718✔
1237
  info.task.taskIdx = pState->id.taskIdx;
224,718✔
1238
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, NULL, pInfo, true));
224,718✔
1239
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
224,718✔
1240

1241
_exit:
224,718✔
1242

1243
  if (code) {
224,718✔
1244
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1245
  }
1246

1247
  return code;
224,718✔
1248
}
1249

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

1279
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
64,462✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
64,462✔
1281
      
1282
      void *pIter = NULL;
64,462✔
1283
      while (1) {
355,191✔
1284
        SStmTaskDeploy info = {0};
419,653✔
1285
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
419,653✔
1286
        if (pIter == NULL) {
419,653✔
1287
          break;
64,462✔
1288
        }
1289
      
1290
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
355,191✔
1291
          pState = taosArrayReserve(pInfo->trigReaders, 1);
95,061✔
1292

1293
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
95,061✔
1294
          if (code) {
95,061✔
1295
            sdbRelease(pSdb, pVgroup);
×
1296
            sdbCancelFetch(pSdb, pIter);
×
1297
            pVgroup = NULL;
×
1298
            TAOS_CHECK_EXIT(code);
×
1299
          }
1300
        }
1301

1302
        sdbRelease(pSdb, pVgroup);
355,191✔
1303
      }
1304
      break;
64,462✔
1305
    }
1306
    default:
946✔
1307
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
946✔
1308
      break;
946✔
1309
  }
1310

1311
_exit:
137,401✔
1312

1313
  mndReleaseDb(pCtx->pMnode, pDb);
137,401✔
1314

1315
  if (code) {
137,401✔
1316
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1317
  }
1318

1319
  return code;
137,401✔
1320
}
1321

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

1345
  key[1] = pSubplan->id.subplanId;
222,935✔
1346

1347
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
222,935✔
1348
  if (NULL == ppRes) {
222,935✔
1349
    SArray* pRes = taosArrayInit(1, sizeof(addr));
222,935✔
1350
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
222,935✔
1351
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
445,870✔
1352
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
222,935✔
1353
  } else {
1354
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1355
  }
1356

1357
  mstsDebug("calcReader %" PRIx64 " added to toUpdateScan, vgId:%d, groupId:%d, subplanId:%d", taskId, vgId, pSubplan->id.groupId, pSubplan->id.subplanId);
222,935✔
1358
  
1359
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
222,935✔
1360
  
1361
_exit:
222,935✔
1362

1363
  nodesDestroyNode((SNode*)pSubplan);
222,935✔
1364

1365
  if (code) {
222,935✔
1366
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1367
  }
1368

1369
  return code;
222,935✔
1370
}
1371

1372
int32_t msmUPAddCacheTask(SStmGrpCtx* pCtx, SStreamCalcScan* pScan, SStreamObj* pStream) {
65,281✔
1373
  int32_t code = TSDB_CODE_SUCCESS;
65,281✔
1374
  int32_t lino = 0;
65,281✔
1375
  SSubplan* pSubplan = NULL;
65,281✔
1376
  int64_t streamId = pStream->pCreate->streamId;
65,281✔
1377
  int64_t key[2] = {streamId, 0};
65,281✔
1378
  TAOS_CHECK_EXIT(nodesStringToNode(pScan->scanPlan, (SNode**)&pSubplan));
65,281✔
1379

1380
  SStmTaskSrcAddr addr;
65,281✔
1381
  addr.isFromCache = true;
65,281✔
1382
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
65,281✔
1383
  addr.taskId = pCtx->triggerTaskId;
65,281✔
1384
  addr.vgId = pCtx->triggerNodeId;
65,281✔
1385
  addr.groupId = pSubplan->id.groupId;
65,281✔
1386

1387
  key[1] = pSubplan->id.subplanId;
65,281✔
1388
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
65,281✔
1389
  if (NULL == ppRes) {
65,281✔
1390
    SArray* pRes = taosArrayInit(1, sizeof(addr));
65,281✔
1391
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
65,281✔
1392
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
130,562✔
1393
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
65,281✔
1394
  } else {
1395
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1396
  }
1397
  
1398
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
65,281✔
1399
  
1400
_exit:
65,281✔
1401

1402
  nodesDestroyNode((SNode*)pSubplan);
65,281✔
1403
  
1404
  if (code) {
65,281✔
1405
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1406
  }
1407

1408
  return code;
65,281✔
1409
}
1410

1411

1412
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
225,201✔
1413
  int32_t code = TSDB_CODE_SUCCESS;
225,201✔
1414
  int32_t lino = 0;
225,201✔
1415

1416
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
225,201✔
1417

1418
  pState->id.taskId = msmAssignTaskId();
225,201✔
1419
  pState->id.deployId = 0;
225,201✔
1420
  pState->id.seriousId = msmAssignTaskSeriousId();
225,201✔
1421
  pState->id.nodeId = nodeId;
225,201✔
1422
  pState->id.taskIdx = taskIdx;
225,201✔
1423
  pState->type = STREAM_READER_TASK;
225,201✔
1424
  pState->flags = 0;
225,201✔
1425
  pState->status = STREAM_STATUS_UNDEPLOYED;
225,201✔
1426
  pState->lastUpTs = pCtx->currTs;
225,201✔
1427
  pState->pStream = pInfo;
225,201✔
1428
  
1429
  SStmTaskDeploy info = {0};
225,201✔
1430
  info.task.type = pState->type;
225,201✔
1431
  info.task.streamId = streamId;
225,201✔
1432
  info.task.taskId = pState->id.taskId;
225,201✔
1433
  info.task.flags = pState->flags;
225,201✔
1434
  info.task.seriousId = pState->id.seriousId;
225,201✔
1435
  info.task.nodeId = pState->id.nodeId;
225,201✔
1436
  info.task.taskIdx = pState->id.taskIdx;
225,201✔
1437
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, calcScanPlan, pInfo, false));
225,201✔
1438
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, streamId));
225,201✔
1439

1440
_exit:
225,201✔
1441

1442
  if (code) {
225,201✔
1443
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1444
  }
1445

1446
  return code;
225,201✔
1447
}
1448

1449

1450
static int32_t msmTDAddCalcReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
137,401✔
1451
  int32_t code = TSDB_CODE_SUCCESS;
137,401✔
1452
  int32_t lino = 0;
137,401✔
1453
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
137,401✔
1454
  int64_t streamId = pStream->pCreate->streamId;
137,401✔
1455
  SStmTaskStatus* pState = NULL;
137,401✔
1456
  pInfo->calcReaders = tdListNew(sizeof(SStmTaskStatus));
137,401✔
1457
  TSDB_CHECK_NULL(pInfo->calcReaders, code, lino, _exit, terrno);
137,401✔
1458

1459
  
1460
  for (int32_t i = 0; i < calcTasksNum; ++i) {
425,617✔
1461
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
288,216✔
1462
    if (pScan->readFromCache) {
288,216✔
1463
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
65,281✔
1464
      continue;
65,281✔
1465
    }
1466
    
1467
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
222,935✔
1468
    for (int32_t m = 0; m < vgNum; ++m) {
445,870✔
1469
      pState = tdListReserve(pInfo->calcReaders);
222,935✔
1470
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
222,935✔
1471

1472
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
222,935✔
1473
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
222,935✔
1474
    }
1475
  }
1476

1477
_exit:
137,401✔
1478

1479
  if (code) {
137,401✔
1480
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1481
  }
1482

1483
  return code;
137,401✔
1484
}
1485

1486

1487

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

1515
_exit:
×
1516

1517
  if (code) {
×
1518
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1519
  }
1520

1521
  return code;
×
1522
}
1523

1524
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
137,401✔
1525
  int32_t code = TSDB_CODE_SUCCESS;
137,401✔
1526
  int32_t lino = 0;
137,401✔
1527
  int64_t streamId = pStream->pCreate->streamId;
137,401✔
1528
  
1529
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
137,401✔
1530
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
137,401✔
1531

1532
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
137,401✔
1533
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
137,401✔
1534
  
1535
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
137,401✔
1536
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
137,401✔
1537
  
1538
_exit:
137,401✔
1539

1540
  if (code) {
137,401✔
1541
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1542
  }
1543
  
1544
  return code;
137,401✔
1545
}
1546

1547
int32_t msmUpdatePlanSourceAddr(SStreamTask* pTask, int64_t streamId, SSubplan* plan, int64_t clientId, SStmTaskSrcAddr* pSrc, int32_t msgType, int64_t srcSubplanId) {
882,174✔
1548
  SDownstreamSourceNode source = {
882,174✔
1549
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1550
      .clientId = clientId,
1551
      .taskId = pSrc->taskId,
882,174✔
1552
      .sId = 0,
1553
      .execId = 0,
1554
      .fetchMsgType = msgType,
1555
      .localExec = false,
1556
  };
1557

1558
  source.addr.epSet = pSrc->epset;
882,174✔
1559
  source.addr.nodeId = pSrc->vgId;
882,174✔
1560

1561
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
882,174✔
1562
      plan->id.subplanId, pSrc->groupId, srcSubplanId, source.clientId, source.taskId, source.addr.nodeId, TMSG_INFO(source.fetchMsgType));
1563
  
1564
  return qSetSubplanExecutionNode(plan, pSrc->groupId, &source);
882,174✔
1565
}
1566

1567
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
17,526✔
1568
  int64_t streamId = pStream->pCreate->streamId;
17,526✔
1569
  int32_t runnerNum = taosArrayGetSize(pRunners);
17,526✔
1570
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
43,815✔
1571
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
43,815✔
1572
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
43,815✔
1573
    if (pPlan->id.subplanId == subplanId) {
43,815✔
1574
      *taskId = pDeploy->task.taskId;
17,526✔
1575
      *ppParent = &pDeploy->task;
17,526✔
1576
      return TSDB_CODE_SUCCESS;
17,526✔
1577
    }
1578
  }
1579

1580
  mstsError("subplanId %d not found in runner list", subplanId);
×
1581

1582
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1583
}
1584

1585
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
420,327✔
1586
  int32_t code = TSDB_CODE_SUCCESS;
420,327✔
1587
  int32_t lino = 0;
420,327✔
1588
  int64_t key[2] = {streamId, -1};
420,327✔
1589
  SNode* pNode = NULL;
420,327✔
1590
  SStreamTask* pTask = &pDeploy->task;
420,327✔
1591
  FOREACH(pNode, pPlan->pChildren) {
1,302,501✔
1592
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
882,174✔
1593
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
17,526✔
1594
      continue;
17,526✔
1595
    }
1596
    
1597
    SValueNode* pVal = (SValueNode*)pNode;
864,648✔
1598
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
864,648✔
1599
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1600
      continue;
×
1601
    }
1602

1603
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
864,648✔
1604

1605
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
864,648✔
1606
    if (NULL == ppRes) {
864,648✔
1607
      msttError("lowest runner subplan ID:%d,%d can't get its child ID:%" PRId64 " addr", pPlan->id.groupId, pPlan->id.subplanId, key[1]);
×
1608
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1609
    }
1610

1611
    int32_t childrenNum = taosArrayGetSize(*ppRes);
864,648✔
1612
    for (int32_t i = 0; i < childrenNum; ++i) {
1,729,296✔
1613
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
864,648✔
1614
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
864,648✔
1615
    }
1616
  }
1617

1618
_exit:
420,327✔
1619

1620
  if (code) {
420,327✔
1621
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1622
  }
1623

1624
  return code;
420,327✔
1625
}
1626

1627
int32_t msmUpdateRunnerPlan(SStmGrpCtx* pCtx, SArray* pRunners, int32_t beginIdx, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
420,327✔
1628
  int32_t code = TSDB_CODE_SUCCESS;
420,327✔
1629
  int32_t lino = 0;
420,327✔
1630
  SSubplan* pPlan = pDeploy->msg.runner.pPlan;
420,327✔
1631
  SStreamTask* pTask = &pDeploy->task;
420,327✔
1632
  SStreamTask* parentTask = NULL;
420,327✔
1633
  int64_t streamId = pStream->pCreate->streamId;
420,327✔
1634

1635
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
420,327✔
1636

1637
  SNode* pTmp = NULL;
420,327✔
1638
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,302,501✔
1639
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
882,174✔
1640
      ERASE_NODE(pPlan->pChildren);
864,648✔
1641
      continue;
864,648✔
1642
    }
1643
    WHERE_NEXT;
17,526✔
1644
  }
1645
  nodesClearList(pPlan->pChildren);
420,327✔
1646
  pPlan->pChildren = NULL;
420,327✔
1647

1648
  if (NULL == pPlan->pParents) {
420,327✔
1649
    goto _exit;
402,801✔
1650
  }
1651

1652
  SNode* pNode = NULL;
17,526✔
1653
  int64_t parentTaskId = 0;
17,526✔
1654
  SStmTaskSrcAddr addr = {0};
17,526✔
1655
  addr.taskId = pDeploy->task.taskId;
17,526✔
1656
  addr.vgId = pDeploy->task.nodeId;
17,526✔
1657
  addr.groupId = pPlan->id.groupId;
17,526✔
1658
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
17,526✔
1659
  FOREACH(pNode, pPlan->pParents) {
35,052✔
1660
    SSubplan* pSubplan = (SSubplan*)pNode;
17,526✔
1661
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
17,526✔
1662
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
17,526✔
1663
  }
1664
  
1665
_exit:
418,962✔
1666

1667
  if (code) {
420,327✔
1668
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1669
  }
1670

1671
  return code;
420,327✔
1672
}
1673

1674
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
402,801✔
1675
  int32_t code = TSDB_CODE_SUCCESS;
402,801✔
1676
  int32_t lino = 0;
402,801✔
1677
  int64_t streamId = pStream->pCreate->streamId;
402,801✔
1678
  int32_t runnerNum = taosArrayGetSize(pRunners);
402,801✔
1679
  
1680
  for (int32_t i = 0; i < runnerNum; ++i) {
823,128✔
1681
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
420,327✔
1682
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
420,327✔
1683
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
420,327✔
1684

1685
    SStreamTask* pTask = &pDeploy->task;
420,327✔
1686
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
420,327✔
1687
  }
1688

1689
_exit:
402,801✔
1690

1691
  if (code) {
402,801✔
1692
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1693
  }
1694

1695
  return code;
402,801✔
1696
}
1697

1698
int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream) {
134,267✔
1699
  int32_t code = 0;
134,267✔
1700
  int32_t lino = 0;
134,267✔
1701
  int64_t streamId = pStream->pCreate->streamId;
134,267✔
1702
  SArray* deployTaskList = NULL;
134,267✔
1703
  SArray* deployList = NULL;
134,267✔
1704
  int32_t deployNodeId = 0;
134,267✔
1705
  SStmTaskStatus* pState = NULL;
134,267✔
1706
  int32_t taskIdx = 0;
134,267✔
1707
  SNodeListNode *plans = NULL;
134,267✔
1708
  int32_t        taskNum = 0;
134,267✔
1709
  int32_t        totalTaskNum = 0;
134,267✔
1710

1711
  if (pDag->numOfSubplans <= 0) {
134,267✔
1712
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1713
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1714
  }
1715

1716
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
134,267✔
1717
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1718
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1719
  }
1720

1721
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
134,267✔
1722
  if (levelNum <= 0) {
134,267✔
1723
    mstsError("invalid level num:%d", levelNum);
×
1724
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1725
  }
1726

1727
  int32_t        lowestLevelIdx = levelNum - 1;
134,267✔
1728
  
1729
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
134,267✔
1730
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
134,267✔
1731
    mstsError("invalid level plan, level:0, planNodeType:%d", nodeType(plans));
×
1732
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1733
  }
1734
  
1735
  taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
134,267✔
1736
  if (taskNum != 1) {
134,267✔
1737
    mstsError("invalid level plan number:%d, level:0", taskNum);
×
1738
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1739
  }
1740

1741
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
134,267✔
1742
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
134,267✔
1743
  
1744
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
537,068✔
1745
    totalTaskNum = 0;
402,801✔
1746

1747
    deployList = pInfo->runners[deployId];
402,801✔
1748
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
402,801✔
1749
    if (!GOT_SNODE(deployNodeId)) {
402,801✔
1750
      TAOS_CHECK_EXIT(terrno);
×
1751
    }
1752

1753
    taskIdx = 0;
402,801✔
1754
    
1755
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
814,365✔
1756
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
411,564✔
1757
      if (NULL == plans) {
411,564✔
1758
        mstsError("empty level plan, level:%d", i);
×
1759
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1760
      }
1761

1762
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
411,564✔
1763
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1764
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1765
      }
1766

1767
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
411,564✔
1768
      if (taskNum <= 0) {
411,564✔
1769
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1770
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1771
      }
1772

1773
      totalTaskNum += taskNum;
411,564✔
1774
      if (totalTaskNum > pDag->numOfSubplans) {
411,564✔
1775
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1776
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1777
      }
1778

1779
      for (int32_t n = 0; n < taskNum; ++n) {
831,891✔
1780
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
420,327✔
1781
        pState = taosArrayReserve(deployList, 1);
420,327✔
1782

1783
        pState->id.taskId = msmAssignTaskId();
420,327✔
1784
        pState->id.deployId = deployId;
420,327✔
1785
        pState->id.seriousId = msmAssignTaskSeriousId();
420,327✔
1786
        pState->id.nodeId = deployNodeId;
420,327✔
1787
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
420,327✔
1788
        pState->type = STREAM_RUNNER_TASK;
420,327✔
1789
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
420,327✔
1790
        pState->status = STREAM_STATUS_UNDEPLOYED;
420,327✔
1791
        pState->lastUpTs = pCtx->currTs;
420,327✔
1792
        pState->pStream = pInfo;
420,327✔
1793

1794
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
420,327✔
1795
        pDeploy->task.type = pState->type;
420,327✔
1796
        pDeploy->task.streamId = streamId;
420,327✔
1797
        pDeploy->task.taskId = pState->id.taskId;
420,327✔
1798
        pDeploy->task.flags = pState->flags;
420,327✔
1799
        pDeploy->task.seriousId = pState->id.seriousId;
420,327✔
1800
        pDeploy->task.deployId = pState->id.deployId;
420,327✔
1801
        pDeploy->task.nodeId = pState->id.nodeId;
420,327✔
1802
        pDeploy->task.taskIdx = pState->id.taskIdx;
420,327✔
1803
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
420,327✔
1804

1805
        SStreamTask* pTask = &pDeploy->task;
420,327✔
1806
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
420,327✔
1807
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1808
      }
1809

1810
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
411,564✔
1811
    }
1812

1813
    if (totalTaskNum != pDag->numOfSubplans) {
402,801✔
1814
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1815
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1816
    }
1817

1818
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
402,801✔
1819

1820
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
402,801✔
1821

1822
    nodesDestroyNode((SNode *)pDag);
402,801✔
1823
    pDag = NULL;
402,801✔
1824
    
1825
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
402,801✔
1826

1827
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
402,801✔
1828
  }
1829

1830
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
537,068✔
1831
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
402,801✔
1832
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
402,801✔
1833
  }
1834
  
1835
  pInfo->runnerNum = totalTaskNum;
134,267✔
1836
  
1837
_exit:
134,267✔
1838

1839
  if (code) {
134,267✔
1840
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1841
  }
1842

1843
  taosArrayDestroy(deployTaskList);
134,267✔
1844
  nodesDestroyNode((SNode *)pDag);
134,267✔
1845

1846
  return code;
134,267✔
1847
}
1848

1849
int32_t msmReBuildRunnerTasks(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream, SStmTaskAction* pAction) {
×
1850
  int32_t code = 0;
×
1851
  int32_t lino = 0;
×
1852
  int64_t streamId = pStream->pCreate->streamId;
×
1853
  int32_t newNodeId = 0;
×
1854
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
×
1855
  int32_t        lowestLevelIdx = levelNum - 1;
×
1856
  SNodeListNode *plans = NULL;
×
1857
  int32_t        taskNum = 0;
×
1858
  int32_t        totalTaskNum = 0;
×
1859
  int32_t        deployId = 0;
×
1860
  SStmTaskStatus* pRunner = NULL;
×
1861
  SStmTaskStatus* pStartRunner = NULL;
×
1862
  int32_t taskIdx = 0;
×
1863
  SArray* deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
×
1864
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
×
1865

1866
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
×
1867
    deployId = pAction->deployId[r];
×
1868

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

1871
    pStartRunner = pRunner;
×
1872
    totalTaskNum = 0;
×
1873

1874
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
×
1875
    if (!GOT_SNODE(newNodeId)) {
×
1876
      TAOS_CHECK_EXIT(terrno);
×
1877
    }
1878

1879
    taskIdx = 0;
×
1880
    
1881
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
×
1882
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
×
1883
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
×
1884
      totalTaskNum += taskNum;
×
1885

1886
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
×
1887
      
1888
      for (int32_t n = 0; n < taskNum; ++n) {
×
1889
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
×
1890

1891
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
×
1892
        if (pRunner->id.taskIdx != newTaskIdx) {
×
1893
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1894
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1895
        }
1896

1897
        pRunner->id.nodeId = newNodeId;
×
1898

1899
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
×
1900
        pDeploy->task.type = pRunner->type;
×
1901
        pDeploy->task.streamId = streamId;
×
1902
        pDeploy->task.taskId = pRunner->id.taskId;
×
1903
        pDeploy->task.flags = pRunner->flags;
×
1904
        pDeploy->task.seriousId = pRunner->id.seriousId;
×
1905
        pDeploy->task.nodeId = pRunner->id.nodeId;
×
1906
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
×
1907
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
×
1908

1909
        pRunner++;
×
1910
      }
1911

1912
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
×
1913
    }
1914

1915
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
×
1916

1917
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
×
1918

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

1921
    nodesDestroyNode((SNode *)pDag);
×
1922
    pDag = NULL;
×
1923

1924
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
×
1925
  }
1926

1927
_exit:
×
1928

1929
  if (code) {
×
1930
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1931
  }
1932

1933
  nodesDestroyNode((SNode *)pDag);
×
1934
  taosArrayDestroy(deployTaskList);
×
1935

1936
  return code;
×
1937
}
1938

1939

1940
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
128,068✔
1941
  int32_t code = TSDB_CODE_SUCCESS;
128,068✔
1942
  int32_t lino = 0;
128,068✔
1943
  //STREAMTODO 
1944
  
1945
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
128,068✔
1946
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
128,068✔
1947

1948
_exit:
128,068✔
1949

1950
  if (code) {
128,068✔
1951
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1952
  }
1953

1954
  return code;
128,068✔
1955
}
1956

1957

1958
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
137,401✔
1959
  if (NULL == pStream->pCreate->calcPlan) {
137,401✔
1960
    return TSDB_CODE_SUCCESS;
3,134✔
1961
  }
1962
  
1963
  int32_t code = TSDB_CODE_SUCCESS;
134,267✔
1964
  int32_t lino = 0;
134,267✔
1965
  int64_t streamId = pStream->pCreate->streamId;
134,267✔
1966
  SQueryPlan* pPlan = NULL;
134,267✔
1967

1968
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
134,267✔
1969

1970
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
537,068✔
1971
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
402,801✔
1972
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
402,801✔
1973
  }
1974

1975
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
134,267✔
1976
  pPlan = NULL;
134,267✔
1977
  
1978
  TAOS_CHECK_EXIT(code);
134,267✔
1979

1980
  taosHashClear(mStreamMgmt.toUpdateScanMap);
134,267✔
1981
  mStreamMgmt.toUpdateScanNum = 0;
134,267✔
1982

1983
_exit:
134,267✔
1984

1985
  nodesDestroyNode((SNode *)pPlan);
134,267✔
1986

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

1991
  return code;
134,267✔
1992
}
1993

1994

1995
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
137,401✔
1996
  int32_t code = TSDB_CODE_SUCCESS;
137,401✔
1997
  int32_t lino = 0;
137,401✔
1998
  int64_t streamId = pStream->pCreate->streamId;
137,401✔
1999

2000
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
137,401✔
2001

2002
  pCtx->triggerTaskId = msmAssignTaskId();
137,401✔
2003
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
137,401✔
2004
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
137,401✔
2005
    TAOS_CHECK_EXIT(terrno);
×
2006
  }
2007

2008
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
137,401✔
2009
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
137,401✔
2010
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
137,401✔
2011
  
2012
_exit:
137,401✔
2013

2014
  if (code) {
137,401✔
2015
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2016
  }
2017

2018
  return code;
137,401✔
2019
}
2020

2021
static int32_t msmInitTrigReaderList(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
×
2022
  int32_t code = TSDB_CODE_SUCCESS;
×
2023
  int32_t lino = 0;
×
2024
  int64_t streamId = pStream->pCreate->streamId;
×
2025
  SSdb   *pSdb = pCtx->pMnode->pSdb;
×
2026
  SStmTaskStatus* pState = NULL;
×
2027
  SDbObj* pDb = NULL;
×
2028
  
2029
  switch (pStream->pCreate->triggerTblType) {
×
2030
    case TSDB_NORMAL_TABLE:
×
2031
    case TSDB_CHILD_TABLE:
2032
    case TSDB_VIRTUAL_CHILD_TABLE:
2033
    case TSDB_VIRTUAL_NORMAL_TABLE: {
2034
      pInfo->trigReaders = taosArrayInit_s(sizeof(SStmTaskStatus), 1);
×
2035
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2036
      pInfo->trigReaderNum = 1;
×
2037
      break;
×
2038
    }
2039
    case TSDB_SUPER_TABLE: {
×
2040
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
×
2041
      if (NULL == pDb) {
×
2042
        code = terrno;
×
2043
        mstsError("failed to acquire db %s, error:%s", pStream->pCreate->triggerDB, terrstr());
×
2044
        goto _exit;
×
2045
      }
2046

2047
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
×
2048
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
×
2049
      pInfo->trigReaderNum = pDb->cfg.numOfVgroups;
×
2050
      mndReleaseDb(pCtx->pMnode, pDb);
×
2051
      pDb = NULL;
×
2052
      break;
×
2053
    }
2054
    default:
×
2055
      pInfo->trigReaderNum = 0;
×
2056
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
×
2057
      break;
×
2058
  }
2059

2060
_exit:
×
2061

2062
  if (code) {
×
2063
    mndReleaseDb(pCtx->pMnode, pDb);
×
2064
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2065
  }
2066

2067
  return code;
×
2068
}
2069

2070

2071
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
131,202✔
2072
  int32_t code = TSDB_CODE_SUCCESS;
131,202✔
2073
  int32_t lino = 0;
131,202✔
2074
  int64_t streamId = pStream->pCreate->streamId;
131,202✔
2075

2076
  pStatus->lastActionTs = INT64_MIN;
131,202✔
2077

2078
  if (NULL == pStatus->streamName) {
131,202✔
2079
    pStatus->streamName = taosStrdup(pStream->name);
131,202✔
2080
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
131,202✔
2081
  }
2082

2083
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
131,202✔
2084
  
2085
  if (pStream->pCreate->numOfCalcSubplan > 0) {
131,202✔
2086
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
128,068✔
2087
    
2088
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
128,068✔
2089
  }
2090

2091
  if (initList) {
131,202✔
2092
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2093

2094
    int32_t subPlanNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
×
2095
    if (subPlanNum > 0) {
×
2096
      pStatus->calcReaderNum = subPlanNum;
×
2097
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
2098
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
2099
    }
2100

2101
    if (pStatus->runnerNum > 0) {
×
2102
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2103
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2104
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2105
      }
2106
    }
2107
  }
2108
  
2109
_exit:
131,202✔
2110

2111
  if (code) {
131,202✔
2112
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2113
  }
2114

2115
  return code;
131,202✔
2116
}
2117

2118
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
137,401✔
2119
  int32_t code = TSDB_CODE_SUCCESS;
137,401✔
2120
  int32_t lino = 0;
137,401✔
2121
  int64_t streamId = pStream->pCreate->streamId;
137,401✔
2122
  SStmStatus info = {0};
137,401✔
2123

2124
  if (NULL == pStatus) {
137,401✔
2125
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
131,202✔
2126

2127
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
131,202✔
2128

2129
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
131,202✔
2130
  }
2131
  
2132
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
137,401✔
2133

2134
  mstLogSStmStatus("stream deployed", streamId, pStatus);
137,401✔
2135

2136
_exit:
137,401✔
2137

2138
  if (code) {
137,401✔
2139
    if (NULL != pStatus) {
×
2140
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2141
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2142
    }
2143
    
2144
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2145
  }
2146

2147
  return code;
137,401✔
2148
}
2149

2150

2151
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
6,601✔
2152
  int32_t code = TSDB_CODE_SUCCESS;
6,601✔
2153
  void* pIter = NULL;
6,601✔
2154

2155
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
10,847✔
2156
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
4,246✔
2157
    (void)mstWaitLock(&pVg->lock, true);
4,246✔
2158

2159
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
4,246✔
2160
    if (atomic_load_32(&pVg->deployed) == taskNum) {
4,246✔
2161
      taosRUnLockLatch(&pVg->lock);
×
2162
      continue;
×
2163
    }
2164

2165
    for (int32_t i = 0; i < taskNum; ++i) {
22,384✔
2166
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
18,138✔
2167
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
18,138✔
2168
        continue;
17,298✔
2169
      }
2170

2171
      mstDestroySStmTaskToDeployExt(pExt);
840✔
2172
      pExt->deployed = true;
840✔
2173
    }
2174
    
2175
    taosRUnLockLatch(&pVg->lock);
4,246✔
2176
  }
2177

2178
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
12,755✔
2179
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
6,154✔
2180
    (void)mstWaitLock(&pSnode->lock, true);
6,154✔
2181

2182
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
6,154✔
2183
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
6,154✔
2184
      for (int32_t i = 0; i < taskNum; ++i) {
12,860✔
2185
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
8,946✔
2186
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
8,946✔
2187
          continue;
8,526✔
2188
        }
2189
        
2190
        mstDestroySStmTaskToDeployExt(pExt);
420✔
2191
        pExt->deployed = true;
420✔
2192
      }
2193
    }
2194

2195
    taskNum = taosArrayGetSize(pSnode->runnerList);
6,154✔
2196
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
6,154✔
2197
      for (int32_t i = 0; i < taskNum; ++i) {
33,412✔
2198
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
27,258✔
2199
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
27,258✔
2200
          continue;
25,998✔
2201
        }
2202
        
2203
        mstDestroySStmTaskToDeployExt(pExt);
1,260✔
2204
        pExt->deployed = true;
1,260✔
2205
      }
2206
    }
2207

2208
    taosRUnLockLatch(&pSnode->lock);
6,154✔
2209
  }
2210

2211
  
2212
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
19,082✔
2213
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
12,481✔
2214
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
12,481✔
2215
    if (TSDB_CODE_SUCCESS == code) {
12,481✔
2216
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
8,421✔
2217
    }
2218
  }
2219

2220
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
29,538✔
2221
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
22,937✔
2222
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
22,937✔
2223
    if (TSDB_CODE_SUCCESS == code) {
22,937✔
2224
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
9,273✔
2225
    }
2226
  }
2227

2228
  size_t keyLen = 0;
6,601✔
2229
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
485,699✔
2230
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
479,098✔
2231
    if (*pStreamId == streamId) {
479,098✔
2232
      int64_t taskId = *(pStreamId + 1);
40,906✔
2233
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
40,906✔
2234
      if (code) {
40,906✔
2235
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2236
      } else {
2237
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
40,906✔
2238
      }
2239
    }
2240
  }
2241

2242
  if (fromStreamMap) {
6,601✔
2243
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
402✔
2244
    if (code) {
402✔
2245
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2246
    } else {
2247
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
402✔
2248
    }
2249
  }
2250
  
2251
  return code;
6,601✔
2252
}
2253

2254
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
6,199✔
2255
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
6,199✔
2256
  
2257
  (void)msmSTRemoveStream(streamId, false);  
6,199✔
2258

2259
  mstResetSStmStatus(pStatus);
6,199✔
2260

2261
  pStatus->deployTimes++;
6,199✔
2262
}
6,199✔
2263

2264
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
137,587✔
2265
  int32_t code = TSDB_CODE_SUCCESS;
137,587✔
2266
  int32_t lino = 0;
137,587✔
2267
  int64_t streamId = pAction->streamId;
137,587✔
2268
  char* streamName = pAction->streamName;
137,587✔
2269
  SStreamObj* pStream = NULL;
137,587✔
2270
  int8_t stopped = 0;
137,587✔
2271

2272
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
137,587✔
2273
  if (pStatus) {
137,587✔
2274
    stopped = atomic_load_8(&pStatus->stopped);
6,199✔
2275
    if (0 == stopped) {
6,199✔
2276
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
420✔
2277
      msmResetStreamForRedeploy(streamId, pStatus);
420✔
2278
    } else {
2279
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
5,779✔
2280
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2281
        return code;
×
2282
      }
2283
      
2284
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
5,779✔
2285
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
5,779✔
2286
        msmResetStreamForRedeploy(streamId, pStatus);
5,779✔
2287
      }
2288
    }
2289
  }
2290

2291
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
137,587✔
2292
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
137,587✔
2293
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
186✔
2294
    return TSDB_CODE_SUCCESS;
186✔
2295
  }
2296

2297
  TAOS_CHECK_EXIT(code);
137,401✔
2298

2299
  if (pStatus && pStream->pCreate->streamId != streamId) {
137,401✔
2300
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2301
    atomic_store_8(&pStatus->stopped, 2);
×
2302
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2303
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2304
  }
2305

2306
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
137,401✔
2307
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
137,401✔
2308
  if (userStopped || userDropped) {
137,401✔
2309
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
×
2310
    goto _exit;
×
2311
  }
2312
  
2313
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
137,401✔
2314

2315
_exit:
137,401✔
2316

2317
  mndReleaseStream(pCtx->pMnode, pStream);
137,401✔
2318

2319
  if (code) {
137,401✔
2320
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2321
  }
2322

2323
  return code;
137,401✔
2324
}
2325

2326
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
1,120✔
2327
  int32_t code = TSDB_CODE_SUCCESS;
1,120✔
2328
  int32_t lino = 0;
1,120✔
2329
  int64_t streamId = pAction->streamId;
1,120✔
2330
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
1,120✔
2331
  if (NULL == ppTask) {
1,120✔
2332
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
×
2333
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2334
  }
2335
  
2336
  SStmTaskDeploy info = {0};
1,120✔
2337
  info.task.type = pAction->type;
1,120✔
2338
  info.task.streamId = pAction->streamId;
1,120✔
2339
  info.task.taskId = pAction->id.taskId;
1,120✔
2340
  info.task.seriousId = (*ppTask)->id.seriousId;
1,120✔
2341
  info.task.nodeId = pAction->id.nodeId;
1,120✔
2342
  info.task.taskIdx = pAction->id.taskIdx;
1,120✔
2343
  
2344
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
1,120✔
2345
  SStreamCalcScan* scanPlan = NULL;
1,120✔
2346
  if (!isTriggerReader) {
1,120✔
2347
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
560✔
2348
    if (NULL == scanPlan) {
560✔
2349
      mstsError("fail to get TASK:%" PRId64 " scanPlan, taskIdx:%d, scanPlanNum:%zu", 
×
2350
          pAction->id.taskId, pAction->id.taskIdx, taosArrayGetSize(pStatus->pCreate->calcScanPlanList));
2351
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2352
    }
2353
  }
2354
  
2355
  TAOS_CHECK_EXIT(msmBuildReaderDeployInfo(&info, scanPlan ? scanPlan->scanPlan : NULL, pStatus, isTriggerReader));
1,120✔
2356
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
1,120✔
2357

2358
_exit:
1,120✔
2359

2360
  if (code) {
1,120✔
2361
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2362
  }
2363

2364
  return code;
1,120✔
2365
}
2366

2367
/*
2368
static int32_t msmReLaunchTriggerTask(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
2369
  int32_t code = TSDB_CODE_SUCCESS;
2370
  int32_t lino = 0;
2371
  int64_t streamId = pAction->streamId;
2372
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
2373
  if (NULL == ppTask) {
2374
    mstsError("TASK:%" PRId64 " not in taskMap, remain:%d", pAction->id.taskId, taosHashGetSize(mStreamMgmt.taskMap));
2375
    TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
2376
  }
2377
  
2378
  (*ppTask)->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2379
  if (!GOT_SNODE((*ppTask)->id.nodeId)) {
2380
    mstsError("no avaible snode for deploying trigger task, seriousId: %" PRId64, (*ppTask)->id.seriousId);
2381
    return TSDB_CODE_SUCCESS;
2382
  }
2383
  
2384
  SStmTaskDeploy info = {0};
2385
  info.task.type = pAction->type;
2386
  info.task.streamId = streamId;
2387
  info.task.taskId = pAction->id.taskId;
2388
  info.task.seriousId = (*ppTask)->id.seriousId;
2389
  info.task.nodeId = (*ppTask)->id.nodeId;
2390
  info.task.taskIdx = pAction->id.taskIdx;
2391
  
2392
  TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2393
  TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2394
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, *ppTask, 1, -1));
2395
  
2396
  atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2397

2398
_exit:
2399

2400
  if (code) {
2401
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2402
  }
2403

2404
  return code;
2405
}
2406
*/
2407

2408
static int32_t msmReLaunchRunnerDeploy(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
×
2409
  int32_t code = TSDB_CODE_SUCCESS;
×
2410
  int32_t lino = 0;
×
2411
  int64_t streamId = pAction->streamId;
×
2412
  
2413
/*
2414
  if (pAction->triggerStatus) {
2415
    pCtx->triggerTaskId = pAction->triggerStatus->id.taskId;
2416
    pAction->triggerStatus->id.nodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
2417
    if (!GOT_SNODE(pAction->triggerStatus->id.nodeId)) {
2418
      mstsError("no avaible snode for deploying trigger task, seriousId:%" PRId64, pAction->triggerStatus->id.seriousId);
2419
      return TSDB_CODE_SUCCESS;
2420
    }
2421
  
2422
    pCtx->triggerNodeId = pAction->triggerStatus->id.nodeId;
2423
  } else {
2424
*/
2425
  pCtx->triggerTaskId = pStatus->triggerTask->id.taskId;
×
2426
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
×
2427
//  }
2428
  
2429
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
×
2430
  
2431
  SQueryPlan* pPlan = NULL;
×
2432
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
×
2433
  
2434
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
×
2435
  
2436
  taosHashClear(mStreamMgmt.toUpdateScanMap);
×
2437
  mStreamMgmt.toUpdateScanNum = 0;
×
2438
  
2439
/*
2440
  if (pAction->triggerStatus) {
2441
    SStmTaskDeploy info = {0};
2442
    info.task.type = STREAM_TRIGGER_TASK;
2443
    info.task.streamId = streamId;
2444
    info.task.taskId = pCtx->triggerTaskId;
2445
    info.task.seriousId = pAction->triggerStatus->id.seriousId;
2446
    info.task.nodeId = pCtx->triggerNodeId;
2447
    info.task.taskIdx = 0;
2448
  
2449
    TAOS_CHECK_EXIT(msmBuildTriggerDeployInfo(pCtx->pMnode, pStatus, &info, pStream));
2450
    TAOS_CHECK_EXIT(msmTDAddTriggerToSnodeMap(&info, pStream));
2451
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pAction->triggerStatus, 1, -1));
2452
    
2453
    atomic_add_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2454
  }
2455
*/
2456

2457
_exit:
×
2458

2459
  if (code) {
×
2460
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2461
  }
2462

2463
  return code;
×
2464
}
2465

2466

2467
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
1,120✔
2468
  int32_t code = TSDB_CODE_SUCCESS;
1,120✔
2469
  int32_t lino = 0;
1,120✔
2470
  int64_t streamId = pAction->streamId;
1,120✔
2471
  int64_t taskId = pAction->id.taskId;
1,120✔
2472
  SStreamObj* pStream = NULL;
1,120✔
2473

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

2476
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,120✔
2477
  if (NULL == pStatus) {
1,120✔
2478
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2479
    return TSDB_CODE_SUCCESS;
×
2480
  }
2481

2482
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,120✔
2483
  if (stopped) {
1,120✔
2484
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
2485
    return TSDB_CODE_SUCCESS;
×
2486
  }
2487

2488
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,120✔
2489
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,120✔
2490
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2491
    return TSDB_CODE_SUCCESS;
×
2492
  }
2493

2494
  TAOS_CHECK_EXIT(code);
1,120✔
2495

2496
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,120✔
2497
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,120✔
2498
  if (userStopped || userDropped) {
1,120✔
2499
    mstsWarn("stream %s is stopped %d or removing %d, ignore task deploy", pStatus->streamName, userStopped, userDropped);
×
2500
    goto _exit;
×
2501
  }
2502

2503
  switch (pAction->type) {
1,120✔
2504
    case STREAM_READER_TASK:
1,120✔
2505
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,120✔
2506
      break;
1,120✔
2507
/*
2508
    case STREAM_TRIGGER_TASK:
2509
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2510
      break;
2511
*/
2512
    case STREAM_RUNNER_TASK:
×
2513
      if (pAction->multiRunner) {
×
2514
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
×
2515
      } else {
2516
        mstsError("runner TASK:%" PRId64 " requires relaunch", pAction->id.taskId);
×
2517
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
2518
      }
2519
      break;
×
2520
    default:
×
2521
      mstsError("TASK:%" PRId64 " invalid task type:%d", pAction->id.taskId, pAction->type);
×
2522
      TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
2523
      break;
×
2524
  }
2525

2526
_exit:
1,120✔
2527

2528
  if (pStream) {
1,120✔
2529
    mndReleaseStream(pCtx->pMnode, pStream);
1,120✔
2530
  }
2531

2532
  if (code) {
1,120✔
2533
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2534
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2535
  }
2536

2537
  return code;
1,120✔
2538
}
2539

2540
static int32_t msmTDRemoveStream(int64_t streamId) {
×
2541
  void* pIter = NULL;
×
2542
  
2543
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
×
2544
    while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
×
2545
      SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
×
2546
      int32_t taskNum = taosArrayGetSize(pVg->taskList);
×
2547
      if (atomic_load_32(&pVg->deployed) == taskNum) {
×
2548
        continue;
×
2549
      }
2550
      
2551
      for (int32_t i = 0; i < taskNum; ++i) {
×
2552
        SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
×
2553
        if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2554
          pExt->deployed = true;
×
2555
        }
2556
      }
2557
    }
2558
  }
2559

2560
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
×
2561
    while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
×
2562
      SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
×
2563
      int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
×
2564
      if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
×
2565
        for (int32_t i = 0; i < taskNum; ++i) {
×
2566
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
×
2567
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2568
            pExt->deployed = true;
×
2569
          }
2570
        }
2571
      }
2572

2573
      taskNum = taosArrayGetSize(pSnode->runnerList);
×
2574
      if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
×
2575
        for (int32_t i = 0; i < taskNum; ++i) {
×
2576
          SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
×
2577
          if (pExt->deploy.task.streamId == streamId && !pExt->deployed) {
×
2578
            pExt->deployed = true;
×
2579
          }
2580
        }
2581
      }
2582
    }
2583
  }
2584

2585
  return TSDB_CODE_SUCCESS;
×
2586
}
2587

2588
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
402✔
2589
  int32_t code = TSDB_CODE_SUCCESS;
402✔
2590
  int32_t lino = 0;
402✔
2591

2592
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
402✔
2593

2594
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
402✔
2595

2596
_exit:
402✔
2597

2598
  if (code) {
402✔
2599
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2600
  } else {
2601
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
402✔
2602
  }
2603

2604
  return code;
402✔
2605
}
2606

2607
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
9,505✔
2608
  int32_t code = TSDB_CODE_SUCCESS;
9,505✔
2609
  int32_t lino = 0;
9,505✔
2610

2611
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
9,505✔
2612
  if (0 == active || MND_STM_STATE_NORMAL != state) {
9,505✔
2613
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
300✔
2614
    return;
300✔
2615
  }
2616

2617
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
9,205✔
2618
  if (NULL == pStream) {
9,205✔
2619
    mstsInfo("stream %s already not in streamMap", streamName);
186✔
2620
    goto _exit;
186✔
2621
  }
2622

2623
  atomic_store_8(&pStream->stopped, 2);
9,019✔
2624

2625
  mstsInfo("set stream %s stopped by user", streamName);
9,019✔
2626

2627
_exit:
×
2628

2629
  taosHashRelease(mStreamMgmt.streamMap, pStream);
9,205✔
2630

2631
  if (code) {
9,205✔
2632
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2633
  }
2634

2635
  return;
9,205✔
2636
}
2637

2638
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,481✔
2639
  int32_t code = TSDB_CODE_SUCCESS;
3,481✔
2640
  int32_t lino = 0;
3,481✔
2641

2642
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,481✔
2643
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,481✔
2644
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2645
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2646
  }
2647

2648
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,481✔
2649
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,481✔
2650
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
2651
    mstsInfo("stream still not in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2652
    goto _exit;
×
2653
  }
2654

2655
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,481✔
2656

2657
_exit:
3,481✔
2658

2659
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,481✔
2660

2661
  if (code) {
3,481✔
2662
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2663
  }
2664

2665
  return code;
3,481✔
2666
}
2667

2668
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
26,671✔
2669
  int32_t code = TSDB_CODE_SUCCESS;
26,671✔
2670
  int32_t lino = 0;
26,671✔
2671
  SStmQNode* pQNode = NULL;
26,671✔
2672

2673
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
165,378✔
2674
    switch (pQNode->type) {
138,707✔
2675
      case STREAM_ACT_DEPLOY:
138,707✔
2676
        if (pQNode->streamAct) {
138,707✔
2677
          mstDebug("start to handle stream deploy action");
137,587✔
2678
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
137,587✔
2679
        } else {
2680
          mstDebug("start to handle task deploy action");
1,120✔
2681
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,120✔
2682
        }
2683
        break;
138,707✔
2684
      default:
×
2685
        break;
×
2686
    }
2687
  }
2688

2689
_exit:
26,671✔
2690

2691
  if (code) {
26,671✔
2692
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2693
  }
2694
}
26,671✔
2695

2696
void msmStopAllStreamsByGrant(int32_t errCode) {
×
2697
  SStmStatus* pStatus = NULL;
×
2698
  void* pIter = NULL;
×
2699
  int64_t streamId = 0;
×
2700
  
2701
  while (true) {
2702
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
2703
    if (NULL == pIter) {
×
2704
      break;
×
2705
    }
2706

2707
    pStatus = (SStmStatus*)pIter;
×
2708

2709
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2710
    atomic_store_8(&pStatus->stopped, 4);
×
2711

2712
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2713
  }
2714
}
×
2715

2716
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2717
  mstInfo("stream grant expired");
×
2718

2719
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2720
    mstWarn("mnode stream is NOT active, ignore handling");
×
2721
    return errCode;
×
2722
  }
2723

2724
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2725

2726
  msmStopAllStreamsByGrant(errCode);
×
2727

2728
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2729
  
2730
  return errCode;
×
2731
}
2732

2733
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,006,247✔
2734
  int32_t code = TSDB_CODE_SUCCESS;
1,006,247✔
2735
  int32_t lino = 0;
1,006,247✔
2736
  int64_t streamId = pDeploy->task.streamId;
1,006,247✔
2737
  
2738
  switch (pDeploy->task.type) {
1,006,247✔
2739
    case STREAM_READER_TASK:
450,199✔
2740
      if (NULL == pStream->readerTasks) {
450,199✔
2741
        pStream->streamId = streamId;
191,211✔
2742
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
191,211✔
2743
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
191,211✔
2744
      }
2745
      
2746
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
900,398✔
2747
      break;
450,199✔
2748
    case STREAM_TRIGGER_TASK:
136,981✔
2749
      pStream->streamId = streamId;
136,981✔
2750
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
136,981✔
2751
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
136,981✔
2752
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
136,981✔
2753
      break;
136,981✔
2754
    case STREAM_RUNNER_TASK:
419,067✔
2755
      if (NULL == pStream->runnerTasks) {
419,067✔
2756
        pStream->streamId = streamId;
139,547✔
2757
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
139,547✔
2758
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
139,547✔
2759
      }      
2760
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
838,134✔
2761
      break;
419,067✔
2762
    default:
×
2763
      break;
×
2764
  }
2765

2766
_exit:
1,006,247✔
2767

2768
  if (code) {
1,006,247✔
2769
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2770
  }
2771

2772
  return code;
1,006,247✔
2773
}
2774

2775
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,006,247✔
2776
  int32_t code = TSDB_CODE_SUCCESS;
1,006,247✔
2777
  int32_t lino = 0;
1,006,247✔
2778
  int64_t streamId = pDeploy->task.streamId;
1,006,247✔
2779
  SStreamTask* pTask = &pDeploy->task;
1,006,247✔
2780
  SStmStreamDeploy streamDeploy = {0};
1,006,247✔
2781
  SStmStreamDeploy* pStream = NULL;
1,006,247✔
2782
   
2783
  while (true) {
2784
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,006,247✔
2785
    if (NULL == pStream) {
1,006,247✔
2786
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
199,621✔
2787
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
199,621✔
2788
      if (TSDB_CODE_SUCCESS == code) {
199,621✔
2789
        goto _exit;
199,621✔
2790
      }
2791

2792
      if (TSDB_CODE_DUP_KEY != code) {
×
2793
        goto _exit;
×
2794
      }    
2795

2796
      tFreeSStmStreamDeploy(&streamDeploy);
×
2797
      continue;
×
2798
    }
2799

2800
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
806,626✔
2801
    
2802
    break;
806,626✔
2803
  }
2804
  
2805
_exit:
1,006,247✔
2806

2807
  taosHashRelease(pHash, pStream);
1,006,247✔
2808

2809
  if (code) {
1,006,247✔
2810
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2811
  } else {
2812
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,006,247✔
2813
  }
2814

2815
  return code;
1,006,247✔
2816
}
2817

2818

2819
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
157,622✔
2820
  int32_t code = TSDB_CODE_SUCCESS;
157,622✔
2821
  int32_t lino = 0;
157,622✔
2822
  int32_t taskNum = taosArrayGetSize(pTasks);
157,622✔
2823

2824
  for (int32_t i = 0; i < taskNum; ++i) {
1,166,389✔
2825
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,008,767✔
2826
    if (pExt->deployed) {
1,008,767✔
2827
      continue;
2,520✔
2828
    }
2829

2830
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,006,247✔
2831
    pExt->deployed = true;
1,006,247✔
2832

2833
    (void)atomic_add_fetch_32(deployed, 1);
1,006,247✔
2834
  }
2835

2836
_exit:
157,622✔
2837

2838
  if (code) {
157,622✔
2839
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2840
  }
2841

2842
  return code;
157,622✔
2843
}
2844

2845
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
44,966✔
2846
  int32_t code = TSDB_CODE_SUCCESS;
44,966✔
2847
  int32_t lino = 0;
44,966✔
2848
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
44,966✔
2849
  SStmVgTasksToDeploy* pVg = NULL;
44,966✔
2850
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2851

2852
  mstDebug("start to add stream vgroup tasks deploy");
44,966✔
2853
  
2854
  for (int32_t i = 0; i < vgNum; ++i) {
297,516✔
2855
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
252,550✔
2856

2857
    msmUpdateVgroupUpTs(pCtx, *vgId);
252,550✔
2858

2859
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
252,550✔
2860
    if (NULL == pVg) {
252,550✔
2861
      continue;
155,713✔
2862
    }
2863

2864
    if (taosRTryLockLatch(&pVg->lock)) {
96,837✔
2865
      continue;
×
2866
    }
2867
    
2868
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
96,837✔
2869
      taosRUnLockLatch(&pVg->lock);
×
2870
      continue;
×
2871
    }
2872
    
2873
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
96,837✔
2874
    taosRUnLockLatch(&pVg->lock);
96,837✔
2875
  }
2876

2877
_exit:
44,966✔
2878

2879
  if (code) {
44,966✔
2880
    if (pVg) {
×
2881
      taosRUnLockLatch(&pVg->lock);
×
2882
    }
2883

2884
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2885
  }
2886

2887
  return code;
44,966✔
2888
}
2889

2890
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
32,885✔
2891
  int32_t code = TSDB_CODE_SUCCESS;
32,885✔
2892
  int32_t lino = 0;
32,885✔
2893
  SStmSnodeTasksDeploy* pSnode = NULL;
32,885✔
2894
  SStreamHbMsg* pReq = pCtx->pReq;
32,885✔
2895

2896
  mstDebug("start to add stream snode tasks deploy");
32,885✔
2897
  
2898
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
32,885✔
2899
  if (NULL == pSnode) {
32,885✔
2900
    return TSDB_CODE_SUCCESS;
1,501✔
2901
  }
2902

2903
  (void)mstWaitLock(&pSnode->lock, false);
31,384✔
2904
  
2905
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
31,384✔
2906
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
29,401✔
2907
  }
2908

2909
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
31,384✔
2910
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
31,384✔
2911
  }
2912
  
2913
  taosWUnLockLatch(&pSnode->lock);
31,384✔
2914

2915
_exit:
31,384✔
2916

2917
  if (code) {
31,384✔
2918
    if (pSnode) {
×
2919
      taosWUnLockLatch(&pSnode->lock);
×
2920
    }
2921

2922
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2923
  }
2924

2925
  return code;
31,384✔
2926
}
2927

2928
int32_t msmUpdateStreamLastActTs(int64_t streamId, int64_t currTs) {
452,437✔
2929
  int32_t code = TSDB_CODE_SUCCESS;
452,437✔
2930
  int32_t lino = 0;
452,437✔
2931
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
452,437✔
2932
  if (NULL == pStatus) {
452,437✔
2933
    mstsWarn("stream already not exists in streamMap, mapSize:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2934
    return TSDB_CODE_SUCCESS;
×
2935
  }
2936
  
2937
  pStatus->lastActionTs = currTs;
452,437✔
2938

2939
_exit:
452,437✔
2940

2941
  if (code) {
452,437✔
2942
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2943
  }
2944

2945
  return code;
452,437✔
2946
}
2947

2948
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
44,460✔
2949
  int32_t code = TSDB_CODE_SUCCESS;
44,460✔
2950
  int32_t lino = 0;
44,460✔
2951
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
44,460✔
2952
  void* pIter = NULL;
44,460✔
2953

2954
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
44,460✔
2955
  
2956
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
44,460✔
2957
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
44,460✔
2958

2959
  while (1) {
199,621✔
2960
    pIter = taosHashIterate(pCtx->deployStm, pIter);
244,081✔
2961
    if (pIter == NULL) {
244,081✔
2962
      break;
44,460✔
2963
    }
2964
    
2965
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
199,621✔
2966
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
399,242✔
2967

2968
    int64_t streamId = pDeploy->streamId;
199,621✔
2969
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
199,621✔
2970
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
2971

2972
    mstClearSStmStreamDeploy(pDeploy);
199,621✔
2973
    
2974
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
199,621✔
2975
  }
2976
  
2977
_exit:
44,460✔
2978

2979
  if (pIter) {
44,460✔
2980
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2981
  }
2982

2983
  if (code) {
44,460✔
2984
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2985
  }
2986
  
2987
  return code;
44,460✔
2988
}
2989

2990
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
44,966✔
2991
  int32_t code = TSDB_CODE_SUCCESS;
44,966✔
2992
  int32_t lino = 0;
44,966✔
2993
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
44,966✔
2994
  SStmVgTasksToDeploy* pVg = NULL;
44,966✔
2995
  
2996
  for (int32_t i = 0; i < vgNum; ++i) {
297,516✔
2997
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
252,550✔
2998
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
252,550✔
2999
    if (NULL == pVg) {
252,550✔
3000
      continue;
155,713✔
3001
    }
3002

3003
    if (taosWTryLockLatch(&pVg->lock)) {
96,837✔
3004
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3005
      continue;
×
3006
    }
3007
    
3008
    if (atomic_load_32(&pVg->deployed) <= 0) {
96,837✔
3009
      taosWUnLockLatch(&pVg->lock);
×
3010
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3011
      continue;
×
3012
    }
3013

3014
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
96,837✔
3015
    if (atomic_load_32(&pVg->deployed) == taskNum) {
96,837✔
3016
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
93,773✔
3017
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
93,773✔
3018
      pVg->taskList = NULL;
93,773✔
3019
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
93,773✔
3020
      taosWUnLockLatch(&pVg->lock);
93,773✔
3021
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
93,773✔
3022
      continue;
93,773✔
3023
    }
3024

3025
    for (int32_t m = taskNum - 1; m >= 0; --m) {
30,329✔
3026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
27,265✔
3027
      if (!pExt->deployed) {
27,265✔
3028
        continue;
2,924✔
3029
      }
3030

3031
      mstDestroySStmTaskToDeployExt(pExt);
24,341✔
3032

3033
      taosArrayRemove(pVg->taskList, m);
24,341✔
3034
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
24,341✔
3035
    }
3036
    atomic_store_32(&pVg->deployed, 0);
3,064✔
3037
    taosWUnLockLatch(&pVg->lock);
3,064✔
3038
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
3,064✔
3039
  }
3040

3041
_exit:
44,966✔
3042

3043
  if (code) {
44,966✔
3044
    if (pVg) {
×
3045
      taosWUnLockLatch(&pVg->lock);
×
3046
    }
3047

3048
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3049
  }
3050
}
44,966✔
3051

3052
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
33,710✔
3053
  if (!GOT_SNODE(snodeId)) {
33,710✔
3054
    return;
825✔
3055
  }
3056
  
3057
  int32_t code = TSDB_CODE_SUCCESS;
32,885✔
3058
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
32,885✔
3059
  if (NULL == pSnode) {
32,885✔
3060
    return;
1,501✔
3061
  }
3062

3063
  if (taosWTryLockLatch(&pSnode->lock)) {
31,384✔
3064
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3065
    return;
×
3066
  }
3067

3068
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
31,384✔
3069
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
31,384✔
3070
  
3071
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
31,384✔
3072
    taosWUnLockLatch(&pSnode->lock);
×
3073
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3074
    return;
×
3075
  }
3076

3077
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
31,384✔
3078
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
30,513✔
3079
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
30,513✔
3080
    pSnode->triggerList = NULL;
30,513✔
3081
  }
3082

3083
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
31,384✔
3084
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
30,373✔
3085
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
30,373✔
3086
    pSnode->runnerList = NULL;
30,373✔
3087
  }
3088

3089
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
31,384✔
3090
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
30,373✔
3091
    taosWUnLockLatch(&pSnode->lock);
30,373✔
3092
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
30,373✔
3093
    return;
30,373✔
3094
  }
3095

3096
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
1,011✔
3097
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
14,869✔
3098
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
13,998✔
3099
      if (!pExt->deployed) {
13,998✔
3100
        continue;
731✔
3101
      }
3102

3103
      mstDestroySStmTaskToDeployExt(pExt);
13,267✔
3104
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
13,267✔
3105
      taosArrayRemove(pSnode->triggerList, m);
13,267✔
3106
    }
3107
    
3108
    pSnode->triggerDeployed = 0;
871✔
3109
  }
3110

3111
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
1,011✔
3112
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
40,392✔
3113
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
39,381✔
3114
      if (!pExt->deployed) {
39,381✔
3115
        continue;
731✔
3116
      }
3117

3118
      mstDestroySStmTaskToDeployExt(pExt);
38,650✔
3119
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
38,650✔
3120
      taosArrayRemove(pSnode->runnerList, m);
38,650✔
3121
    }
3122
    
3123
    pSnode->runnerDeployed = 0;
1,011✔
3124
  }
3125
  
3126
  taosWUnLockLatch(&pSnode->lock);
1,011✔
3127
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
1,011✔
3128
}
3129

3130
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
13,870,062✔
3131
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,870,062✔
3132
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
44,966✔
3133
  }
3134

3135
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
13,870,062✔
3136
    msmCleanDeployedSnodeTasks(pHb->snodeId);
33,710✔
3137
  }
3138
}
13,870,062✔
3139

3140
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
13,870,062✔
3141
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
13,870,062✔
3142
  if (mStreamMgmt.tCtx) {
13,870,062✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
13,783,135✔
3144
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
13,783,135✔
3145
  }
3146
}
13,870,062✔
3147

3148
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
194,686✔
3149
  int32_t code = TSDB_CODE_SUCCESS;
194,686✔
3150
  int32_t lino = 0;
194,686✔
3151
  int32_t action = STREAM_ACT_START;
194,686✔
3152
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
194,686✔
3153
  if (pAction) {
194,686✔
3154
    pAction->actions |= action;
×
3155
    pAction->start.triggerId = *pId;
×
3156
    mstsDebug("stream append START action, actions:%x", pAction->actions);
×
3157
  } else {
3158
    SStmAction newAction = {0};
194,686✔
3159
    newAction.actions = action;
194,686✔
3160
    newAction.start.triggerId = *pId;
194,686✔
3161
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
194,686✔
3162
    mstsDebug("stream add START action, actions:%x", newAction.actions);
194,686✔
3163
  }
3164

3165
_exit:
194,686✔
3166

3167
  if (code) {
194,686✔
3168
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3169
  }
3170

3171
  return code;
194,686✔
3172
}
3173

3174
int32_t msmGrpAddActionUpdateTrigger(SHashObj* pHash, int64_t streamId) {
×
3175
  int32_t code = TSDB_CODE_SUCCESS;
×
3176
  int32_t lino = 0;
×
3177
  int32_t action = STREAM_ACT_UPDATE_TRIGGER;
×
3178
  
3179
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
×
3180
  if (pAction) {
×
3181
    pAction->actions |= action;
×
3182
    mstsDebug("stream append UPDATE_TRIGGER action, actions:%x", pAction->actions);
×
3183
  } else {
3184
    SStmAction newAction = {0};
×
3185
    newAction.actions = action;
×
3186
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
×
3187
    mstsDebug("stream add UPDATE_TRIGGER action, actions:%x", newAction.actions);
×
3188
  }
3189

3190
_exit:
×
3191

3192
  if (code) {
×
3193
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3194
  }
3195

3196
  return code;
×
3197
}
3198

3199

3200

3201
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
58,130✔
3202
  int32_t code = TSDB_CODE_SUCCESS;
58,130✔
3203
  int32_t lino = 0;
58,130✔
3204
  int32_t action = STREAM_ACT_UNDEPLOY;
58,130✔
3205
  bool    dropped = false;
58,130✔
3206

3207
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
58,130✔
3208
  mstsDebug("stream dropped: %d", dropped);
58,130✔
3209
  
3210
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
58,130✔
3211
  if (pAction) {
58,130✔
3212
    pAction->actions |= action;
39,143✔
3213
    if (NULL == pAction->undeploy.taskList) {
39,143✔
3214
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3215
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3216
    }
3217

3218
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
78,286✔
3219
    if (pAction->undeploy.doCheckpoint) {
39,143✔
3220
      pAction->undeploy.doCheckpoint = dropped ? false : true;
23,969✔
3221
    }
3222
    if (!pAction->undeploy.doCleanup) {
39,143✔
3223
      pAction->undeploy.doCleanup = dropped ? true : false;
23,969✔
3224
    }
3225
    
3226
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
39,143✔
3227
  } else {
3228
    SStmAction newAction = {0};
18,987✔
3229
    newAction.actions = action;
18,987✔
3230
    newAction.undeploy.doCheckpoint = dropped ? false : true;
18,987✔
3231
    newAction.undeploy.doCleanup = dropped ? true : false;
18,987✔
3232
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
18,987✔
3233
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
18,987✔
3234
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
37,974✔
3235
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
18,987✔
3236
    
3237
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
18,987✔
3238
  }
3239

3240
_exit:
58,130✔
3241

3242
  if (code) {
58,130✔
3243
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3244
  }
3245

3246
  return code;
58,130✔
3247
}
3248

3249
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,481✔
3250
  int32_t code = TSDB_CODE_SUCCESS;
3,481✔
3251
  int32_t lino = 0;
3,481✔
3252
  int32_t action = STREAM_ACT_RECALC;
3,481✔
3253
  SStmAction newAction = {0};
3,481✔
3254
  
3255
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,481✔
3256
  if (pAction) {
3,481✔
3257
    pAction->actions |= action;
×
3258
    pAction->recalc.recalcList = recalcList;
×
3259

3260
    mstsDebug("stream append recalc action, listSize:%d, actions:%x", (int32_t)taosArrayGetSize(recalcList), pAction->actions);
×
3261
  } else {
3262
    newAction.actions = action;
3,481✔
3263
    newAction.recalc.recalcList = recalcList;
3,481✔
3264
    
3265
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,481✔
3266
    
3267
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,481✔
3268
  }
3269

3270
_exit:
3,481✔
3271

3272
  if (code) {
3,481✔
3273
    mstDestroySStmAction(&newAction);
×
3274
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3275
  }
3276

3277
  return code;
3,481✔
3278
}
3279

3280
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
242,040✔
3281
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
242,040✔
3282
  if (NULL == pStream) {
242,040✔
3283
    return false;
×
3284
  }
3285

3286
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
242,040✔
3287
    return false;
×
3288
  }
3289

3290
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
242,040✔
3291
  for (int32_t i = 0; i < readerNum; ++i) {
532,379✔
3292
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
290,339✔
3293
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
290,339✔
3294
      return false;
×
3295
    }
3296
  }
3297

3298
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
242,040✔
3299
  for (int32_t i = 0; i < readerNum; ++i) {
299,704✔
3300
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
57,664✔
3301
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
57,664✔
3302
      return false;
×
3303
    }
3304
  }
3305

3306
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
242,040✔
3307
  SListNode* pNode = listHead(pStream->calcReaders);
242,040✔
3308
  for (int32_t i = 0; i < readerNum; ++i) {
756,385✔
3309
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
514,345✔
3310
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
514,345✔
3311
      return false;
×
3312
    }
3313
    pNode = TD_DLIST_NODE_NEXT(pNode);
514,345✔
3314
  }
3315

3316
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
807,924✔
3317
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
613,238✔
3318
    for (int32_t m = 0; m < runnerNum; ++m) {
1,196,648✔
3319
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
630,764✔
3320
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
630,764✔
3321
        return false;
47,354✔
3322
      }
3323
    }
3324
  }
3325
  
3326
  return true;
194,686✔
3327
}
3328

3329

3330
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
7,961,439✔
3331
  int32_t code = TSDB_CODE_SUCCESS;
7,961,439✔
3332
  int32_t lino = 0;
7,961,439✔
3333
  int32_t action = 0;
7,961,439✔
3334
  int64_t streamId = pMsg->streamId;
7,961,439✔
3335
  SStreamTask* pTask = (SStreamTask*)pMsg;
7,961,439✔
3336
  int8_t  stopped = 0;
7,961,439✔
3337

3338
  msttDebug("start to handle task abnormal status %d", pTask->status);
7,961,439✔
3339
  
3340
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
7,961,439✔
3341
  if (NULL == pStatus) {
7,961,439✔
3342
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3343
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3344
    return;
11,897✔
3345
  }
3346

3347
  stopped = atomic_load_8(&pStatus->stopped);
7,961,439✔
3348
  if (stopped) {
7,961,439✔
3349
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3350
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3351
    return;
×
3352
  }
3353
  
3354
  switch (pMsg->status) {
7,961,439✔
3355
    case STREAM_STATUS_INIT:      
7,956,433✔
3356
      if (STREAM_TRIGGER_TASK != pMsg->type) {
7,956,433✔
3357
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
7,577,358✔
3358
        return;
7,577,358✔
3359
      }
3360
      
3361
      if (INT64_MIN == pStatus->lastActionTs) {
379,075✔
3362
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3363
        return;
×
3364
      }
3365
      
3366
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
379,075✔
3367
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
137,035✔
3368
        return;
137,035✔
3369
      }
3370

3371
      if (STREAM_IS_RUNNING(pStatus)) {
242,040✔
3372
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3373
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
242,040✔
3374
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
194,686✔
3375
      }
3376
      break;
242,040✔
3377
    case STREAM_STATUS_FAILED:
5,006✔
3378
      //STREAMTODO ADD ERRCODE HANDLE
3379
      if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
5,006✔
3380
        msttWarn("task failed with error:%s, try to undeploy whole stream, idx:%d", tstrerror(pMsg->errorCode),
5,006✔
3381
                 pMsg->taskIdx);
3382
        msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
5,006✔
3383
      } else {
3384
        msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode),
×
3385
                 pMsg->taskIdx);
3386
        TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3387
      }
3388
      break;
5,006✔
3389
    default:
×
3390
      break;
×
3391
  }
3392

3393
_exit:
247,046✔
3394

3395
  if (code) {
247,046✔
3396
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3397
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3398
  }
3399
}
3400

3401
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
58,130✔
3402
  int32_t code = TSDB_CODE_SUCCESS;
58,130✔
3403
  int32_t lino = 0;
58,130✔
3404
  SStreamTask* pTask = (SStreamTask*)pStatus;
58,130✔
3405
  int64_t streamId = pStatus->streamId;
58,130✔
3406

3407
  msttInfo("start to handle task error, type: %d", err);
58,130✔
3408

3409
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
58,130✔
3410

3411
_exit:
58,130✔
3412

3413
  if (code) {
58,130✔
3414
    // IGNORE STOP STREAM BY ERROR  
3415
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3416
  }
3417
}
58,130✔
3418

3419
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
2,735,611✔
3420
  int32_t code = TSDB_CODE_SUCCESS;
2,735,611✔
3421
  int32_t lino = 0;
2,735,611✔
3422
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
2,735,611✔
3423

3424
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
2,735,611✔
3425
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3426
  }
3427
  
3428
  SArray* userRecalcList = NULL;
2,735,611✔
3429
  if (atomic_load_ptr(&pStream->userRecalcList)) {
2,735,611✔
3430
    taosWLockLatch(&pStream->userRecalcLock);
3,481✔
3431
    if (pStream->userRecalcList) {
3,481✔
3432
      userRecalcList = pStream->userRecalcList;
3,481✔
3433
      pStream->userRecalcList = NULL;
3,481✔
3434
    }
3435
    taosWUnLockLatch(&pStream->userRecalcLock);
3,481✔
3436
    
3437
    if (userRecalcList) {
3,481✔
3438
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,481✔
3439
    }
3440
  }
3441

3442
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
2,735,611✔
3443
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,386,126✔
3444
    if (NULL == pStatus->detailStatus) {
1,386,126✔
3445
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
136,189✔
3446
      if (NULL == pStatus->detailStatus) {
136,189✔
3447
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3448
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3449
      }
3450
    }
3451
    
3452
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,386,126✔
3453
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,386,126✔
3454
  }
3455

3456
_exit:
1,349,485✔
3457

3458
  if (code) {
2,735,611✔
3459
    // IGNORE STOP STREAM BY ERROR
3460
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3461
  }
3462
}
2,735,611✔
3463

3464
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
718,578✔
3465
  int32_t code = TSDB_CODE_SUCCESS;
718,578✔
3466
  int32_t lino = 0;
718,578✔
3467
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
718,578✔
3468

3469
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
718,578✔
3470

3471
  for (int32_t i = 0; i < num; ++i) {
21,883,878✔
3472
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
21,165,300✔
3473
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
21,165,300✔
3474
    
3475
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
21,165,300✔
3476
    if (NULL == ppStatus) {
21,165,300✔
3477
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
5,634✔
3478
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
5,634✔
3479
      continue;
5,634✔
3480
    }
3481

3482
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
21,159,666✔
3483
    int8_t stopped = atomic_load_8(&pStream->stopped);
21,159,666✔
3484
    if (stopped) {
21,159,666✔
3485
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
52,496✔
3486
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
52,496✔
3487
      continue;
52,496✔
3488
    }
3489

3490
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
21,107,170✔
3491
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3492
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3493
          
3494
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3495
      continue;
×
3496
    }
3497

3498
    if ((*ppStatus)->status != pTask->status) {
21,107,170✔
3499
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,761,471✔
3500
        (*ppStatus)->runningStartTs = pCtx->currTs;
755,276✔
3501
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,006,195✔
3502
        if (pStream->triggerTask) {
×
3503
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3504
        }
3505
        
3506
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3507
      }
3508
    }
3509
    
3510
    (*ppStatus)->errCode = pTask->errorCode;
21,107,170✔
3511
    (*ppStatus)->status = pTask->status;
21,107,170✔
3512
    (*ppStatus)->lastUpTs = pCtx->currTs;
21,107,170✔
3513
    
3514
    if (STREAM_STATUS_RUNNING != pTask->status) {
21,107,170✔
3515
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
7,961,439✔
3516
    }
3517
    
3518
    if (STREAM_TRIGGER_TASK == pTask->type) {
21,107,170✔
3519
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
2,735,611✔
3520
    }
3521
  }
3522

3523
_exit:
718,578✔
3524

3525
  if (code) {
718,578✔
3526
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3527
  }
3528

3529
  return code;
718,578✔
3530
}
3531

3532
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3533
  int32_t code = TSDB_CODE_SUCCESS;
×
3534
  int32_t lino = 0;
×
3535
  int64_t streamId = pTask->streamId;
×
3536
  SStreamObj* pStream = NULL;
×
3537

3538
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3539
  if (NULL == pStatus) {
×
3540
    SStmStatus status = {0};
×
3541
    TAOS_CHECK_EXIT(mndAcquireStreamById(pCtx->pMnode, streamId, &pStream));
×
3542
    TSDB_CHECK_NULL(pStream, code, lino, _exit, TSDB_CODE_MND_STREAM_NOT_EXIST);
×
3543
    if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags) || pStream->pCreate->vtableCalc) {
×
3544
      mndReleaseStream(pCtx->pMnode, pStream);
×
3545
      msttDebug("virtual table task ignored, triggerTblType:%d, vtableCalc:%dstatus:%s", 
×
3546
          pStream->pCreate->triggerTblType, pStream->pCreate->vtableCalc, gStreamStatusStr[pTask->status]);
3547
      return code;
×
3548
    }
3549

3550
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3551
    mndReleaseStream(pCtx->pMnode, pStream);
×
3552

3553
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3554
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3555
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3556
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3557
  }
3558

3559
  SStmTaskStatus* pNewTask = NULL;
×
3560
  switch (pTask->type) {
×
3561
    case STREAM_READER_TASK: {
×
3562
      void* pList = STREAM_IS_TRIGGER_READER(pTask->flags) ? (void*)pStatus->trigReaders : (void*)pStatus->calcReaders;
×
3563
      if (NULL == pList) {
×
3564
        mstsError("%sReader list is NULL", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc");
×
3565
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3566
      }
3567
      int32_t readerSize = STREAM_IS_TRIGGER_READER(pTask->flags) ? pStatus->trigReaderNum : pStatus->calcReaderNum;
×
3568
      if ((STREAM_IS_TRIGGER_READER(pTask->flags) && taosArrayGetSize(pList) >= readerSize) ||
×
3569
          MST_LIST_SIZE((SList*)pList) >= readerSize){
×
3570
        mstsError("%sReader list is already full, size:%d, expSize:%d", STREAM_IS_TRIGGER_READER(pTask->flags) ? "trig" : "calc",
×
3571
            (int32_t)taosArrayGetSize(pList), readerSize);
3572
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3573
      }
3574
      
3575
      SStmTaskStatus taskStatus = {0};
×
3576
      taskStatus.pStream = pStatus;
×
3577
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3578
      if (STREAM_IS_TRIGGER_READER(pTask->flags)) {
×
3579
        pNewTask = taosArrayPush(pList, &taskStatus);
×
3580
        TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3581
      } else {
3582
        TAOS_CHECK_EXIT(tdListAppend(pStatus->calcReaders, &taskStatus));
×
3583
      }
3584
      
3585
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3586
      TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pNewTask, STREAM_IS_TRIGGER_READER(pTask->flags)));
×
3587
      break;
×
3588
    }
3589
    case STREAM_TRIGGER_TASK: {
×
3590
      taosMemoryFreeClear(pStatus->triggerTask);
×
3591
      pStatus->triggerTask = taosMemoryCalloc(1, sizeof(*pStatus->triggerTask));
×
3592
      TSDB_CHECK_NULL(pStatus->triggerTask, code, lino, _exit, terrno);
×
3593
      pStatus->triggerTask->pStream = pStatus;
×
3594
      mstSetTaskStatusFromMsg(pCtx, pStatus->triggerTask, pTask);
×
3595
      pNewTask = pStatus->triggerTask;
×
3596

3597
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3598
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, 0));
×
3599
      break;
×
3600
    }
3601
    case STREAM_RUNNER_TASK:{
×
3602
      if (NULL == pStatus->runners[pTask->deployId]) {
×
3603
        mstsError("deploy %d runner list is NULL", pTask->deployId);
×
3604
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3605
      }
3606
      if (taosArrayGetSize(pStatus->runners[pTask->deployId]) >= pStatus->runnerNum) {
×
3607
        mstsError("deploy %d runner list is already full, size:%d, expSize:%d", pTask->deployId, 
×
3608
            (int32_t)taosArrayGetSize(pStatus->runners[pTask->deployId]), pStatus->runnerNum);
3609
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3610
      }    
3611
      
3612
      SStmTaskStatus taskStatus = {0};
×
3613
      taskStatus.pStream = pStatus;
×
3614
      mstSetTaskStatusFromMsg(pCtx, &taskStatus, pTask);
×
3615
      pNewTask = taosArrayPush(pStatus->runners[pTask->deployId], &taskStatus);
×
3616
      TSDB_CHECK_NULL(pNewTask, code, lino, _exit, terrno);
×
3617

3618
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3619
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3620
      break;
×
3621
    }
3622
    default: {
×
3623
      msttError("invalid task type:%d in task status", pTask->type);
×
3624
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3625
      break;
×
3626
    }
3627
  }
3628

3629
_exit:
×
3630

3631
  if (code) {
×
3632
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3633
  } else {
3634
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3635
  }
3636

3637
  return code;
×
3638
}
3639

3640
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3641
  int32_t code = TSDB_CODE_SUCCESS;
×
3642
  int32_t lino = 0;
×
3643
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3644

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

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

3651
    if (pTask->taskId >= mStreamMgmt.lastTaskId) {
×
3652
      mStreamMgmt.lastTaskId = pTask->taskId + 1;
×
3653
    }
3654
    
3655
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
×
3656
    if (NULL == ppStatus) {
×
3657
      msttInfo("task still not in taskMap, will try to add it, taskIdx:%d", pTask->taskIdx);
×
3658
      
3659
      TAOS_CHECK_EXIT(msmWatchRecordNewTask(pCtx, pTask));
×
3660
      
3661
      continue;
×
3662
    }
3663
    
3664
    (*ppStatus)->status = pTask->status;
×
3665
    (*ppStatus)->lastUpTs = pCtx->currTs;
×
3666
  }
3667

3668
_exit:
×
3669

3670
  if (code) {
×
3671
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3672
  }
3673

3674
  return code;
×
3675
}
3676

3677
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
194,686✔
3678
  int32_t code = TSDB_CODE_SUCCESS;
194,686✔
3679
  int32_t lino = 0;
194,686✔
3680
  if (NULL == pCtx->pRsp->start.taskList) {
194,686✔
3681
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
72,082✔
3682
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
72,082✔
3683
  }
3684

3685
  SStmTaskId* pId = &pAction->start.triggerId;
194,686✔
3686
  SStreamTaskStart start = {0};
194,686✔
3687
  start.task.type = STREAM_TRIGGER_TASK;
194,686✔
3688
  start.task.streamId = streamId;
194,686✔
3689
  start.task.taskId = pId->taskId;
194,686✔
3690
  start.task.seriousId = pId->seriousId;
194,686✔
3691
  start.task.nodeId = pId->nodeId;
194,686✔
3692
  start.task.taskIdx = pId->taskIdx;
194,686✔
3693

3694
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
389,372✔
3695
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
194,686✔
3696

3697
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
194,686✔
3698

3699
  return;
194,686✔
3700

3701
_exit:
×
3702

3703
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3704
}
3705

3706

3707
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
18,987✔
3708
  int32_t code = TSDB_CODE_SUCCESS;
18,987✔
3709
  int32_t lino = 0;
18,987✔
3710
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
18,987✔
3711
  if (NULL == pCtx->pRsp->undeploy.taskList) {
18,987✔
3712
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
15,413✔
3713
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
15,413✔
3714
  }
3715

3716
  SStreamTaskUndeploy undeploy;
18,987✔
3717
  for (int32_t i = 0; i < dropNum; ++i) {
77,117✔
3718
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
58,130✔
3719
    undeploy.task = *pTask;
58,130✔
3720
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
58,130✔
3721
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
58,130✔
3722

3723
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
116,260✔
3724
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
58,130✔
3725

3726
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
58,130✔
3727
  }
3728

3729
  return;
18,987✔
3730

3731
_exit:
×
3732

3733
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3734
}
3735

3736
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3737
  int32_t code = TSDB_CODE_SUCCESS;
×
3738
  int32_t lino = 0;
×
3739

3740
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3741
  if (NULL == pStream) {
×
3742
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3743
    return;
×
3744
  }
3745

3746
  if (NULL == pStream->triggerTask) {
×
3747
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3748
    return;
×
3749
  }
3750

3751
  SStreamMgmtRsp rsp = {0};
×
3752
  rsp.reqId = INT64_MIN;
×
3753
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3754
  rsp.task.streamId = streamId;
×
3755
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3756

3757
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3758

3759
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3760
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3761
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3762
  }
3763

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

3766
_exit:
×
3767

3768
  if (code) {
×
3769
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3770
  } else {
3771
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3772
  }
3773
}
3774

3775
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,481✔
3776
  int32_t code = TSDB_CODE_SUCCESS;
3,481✔
3777
  int32_t lino = 0;
3,481✔
3778

3779
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,481✔
3780
  if (NULL == pStream) {
3,481✔
3781
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3782
    return;
×
3783
  }
3784

3785
  if (NULL == pStream->triggerTask) {
3,481✔
3786
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3787
    return;
×
3788
  }
3789

3790
  SStreamMgmtRsp rsp = {0};
3,481✔
3791
  rsp.reqId = INT64_MIN;
3,481✔
3792
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,481✔
3793
  rsp.task.streamId = streamId;
3,481✔
3794
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,481✔
3795
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,481✔
3796

3797
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,481✔
3798
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,481✔
3799
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,481✔
3800
  }
3801

3802
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,962✔
3803

3804
_exit:
3,481✔
3805

3806
  if (code) {
3,481✔
3807
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3808
  } else {
3809
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,481✔
3810
  }
3811
}
3812

3813

3814
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
90,624✔
3815
  int32_t code = TSDB_CODE_SUCCESS;
90,624✔
3816
  int32_t lino = 0;
90,624✔
3817
  void* pIter = NULL;
90,624✔
3818
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
90,624✔
3819

3820
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
90,624✔
3821

3822
  while (1) {
217,154✔
3823
    pIter = taosHashIterate(pCtx->actionStm, pIter);
307,778✔
3824
    if (pIter == NULL) {
307,778✔
3825
      break;
90,624✔
3826
    }
3827

3828
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
217,154✔
3829
    SStmAction *pAction = (SStmAction *)pIter;
217,154✔
3830
    
3831
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
217,154✔
3832
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
18,987✔
3833
      continue;
18,987✔
3834
    }
3835

3836
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
198,167✔
3837
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3838
    }
3839

3840
    if (STREAM_ACT_RECALC & pAction->actions) {
198,167✔
3841
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,481✔
3842
    }
3843

3844
    if (STREAM_ACT_START & pAction->actions) {
198,167✔
3845
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
194,686✔
3846
    }
3847
  }
3848
  
3849
_exit:
90,624✔
3850

3851
  if (pIter) {
90,624✔
3852
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3853
  }
3854

3855
  if (code) {
90,624✔
3856
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3857
  }
3858

3859
  return code;
90,624✔
3860
}
3861

3862
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
13,783,135✔
3863
  int32_t  code = TSDB_CODE_SUCCESS;
13,783,135✔
3864
  int32_t  lino = 0;
13,783,135✔
3865
  int64_t* lastTs = NULL;
13,783,135✔
3866
  bool     noExists = false;
13,783,135✔
3867

3868
  while (true) {
3869
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
14,226,615✔
3870
    if (NULL == lastTs) {
14,226,615✔
3871
      if (noExists) {
576,646✔
3872
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
133,166✔
3873
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
133,166✔
3874
      }
3875

3876
      noExists = true;
443,480✔
3877
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
443,480✔
3878
      
3879
      continue;
443,480✔
3880
    }
3881

3882
    while (true) {
×
3883
      int64_t lastTsValue = atomic_load_64(lastTs);
13,649,969✔
3884
      if (pCtx->currTs > lastTsValue) {
13,649,969✔
3885
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
13,647,904✔
3886
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
13,647,904✔
3887
          return code;
13,647,904✔
3888
        }
3889

3890
        continue;
×
3891
      }
3892

3893
      return code;
2,065✔
3894
    }
3895

3896
    break;
3897
  }
3898

3899
_exit:
133,166✔
3900

3901
  if (code) {
133,166✔
3902
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
133,166✔
3903
  }
3904

3905
  return code;  
133,166✔
3906
}
3907

3908
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
3909
  SStmStatus* pStatus = NULL;
×
3910
  int32_t trigReaderNum = 0;
×
3911
  int32_t calcReaderNum = 0;
×
3912
  int32_t runnerNum = 0;
×
3913
  int64_t streamId = 0;
×
3914
  void* pIter = NULL;
×
3915
  while (true) {
3916
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
3917
    if (NULL == pIter) {
×
3918
      return;
×
3919
    }
3920

3921
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3922
    pStatus = (SStmStatus*)pIter;
×
3923

3924
    if (NULL == pStatus->triggerTask) {
×
3925
      mstsWarn("no trigger task recored, deployTimes:%" PRId64, pStatus->deployTimes);
×
3926
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3927
      continue;
×
3928
    }
3929
    
3930
    trigReaderNum = taosArrayGetSize(pStatus->trigReaders);
×
3931
    if (pStatus->trigReaderNum != trigReaderNum) {
×
3932
      mstsWarn("trigReaderNum %d mis-match with expected %d", trigReaderNum, pStatus->trigReaderNum);
×
3933
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3934
      continue;
×
3935
    }
3936

3937
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
3938
    if (pStatus->calcReaderNum != calcReaderNum) {
×
3939
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
3940
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3941
      continue;
×
3942
    }
3943

3944
    for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
3945
      runnerNum = taosArrayGetSize(pStatus->runners[i]);
×
3946
      if (runnerNum != pStatus->runnerNum) {
×
3947
        mstsWarn("runner deploy %d runnerNum %d mis-match with expected %d", i, runnerNum, pStatus->runnerNum);
×
3948
        msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3949
        continue;
×
3950
      }
3951
    }
3952
  }
3953
}
3954

3955
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
635✔
3956
  int32_t code = TSDB_CODE_SUCCESS;
635✔
3957
  int32_t lino = 0;
635✔
3958
  int32_t minVal = watchError ? 0 : 1;
635✔
3959

3960
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
635✔
3961
    return code;
×
3962
  }
3963

3964
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
635✔
3965
    (void)sched_yield();
×
3966
  }
3967

3968
  if (watchError) {
635✔
3969
    taosHashClear(mStreamMgmt.vgroupMap);
×
3970
    taosHashClear(mStreamMgmt.snodeMap);
×
3971
    taosHashClear(mStreamMgmt.taskMap);
×
3972
    taosHashClear(mStreamMgmt.streamMap);
×
3973
    mstInfo("watch error happends, clear all maps");
×
3974
    goto _exit;
×
3975
  }
3976

3977
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
635✔
3978
    mstInfo("no stream tasks remain during watch state");
635✔
3979
    goto _exit;
635✔
3980
  }
3981

3982
  msmWatchCheckStreamMap(pCtx);
×
3983

3984
_exit:
635✔
3985

3986
  mStreamMgmt.lastTaskId += 100000;
635✔
3987

3988
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
635✔
3989

3990
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
635✔
3991

3992
  if (code) {
635✔
3993
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3994
  }
3995

3996
  return code;
635✔
3997
}
3998

3999

4000
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
11,591✔
4001
  int32_t code = TSDB_CODE_SUCCESS;
11,591✔
4002
  int32_t lino = 0;
11,591✔
4003
  SStreamHbMsg* pReq = pCtx->pReq;
11,591✔
4004

4005
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
11,591✔
4006
  
4007
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
11,591✔
4008
    goto _exit;
×
4009
  }
4010

4011
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
11,591✔
4012
  if (GOT_SNODE(pReq->snodeId)) {
11,591✔
4013
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,240✔
4014
  }
4015

4016
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
11,591✔
4017
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4018
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4019
  }
4020

4021
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
11,591✔
4022
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
635✔
4023
  }
4024

4025
_exit:
11,591✔
4026

4027
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
11,591✔
4028
  
4029
  if (code) {
11,591✔
4030
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4031

4032
    (void)msmWatchHandleEnding(pCtx, true);
×
4033
  }
4034

4035
  return code;
11,591✔
4036
}
4037

4038
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
4,444,998✔
4039
  int32_t listSize = taosArrayGetSize(pOReaders);
4,444,998✔
4040
  int32_t totalSize = 0;
4,444,998✔
4041
  
4042
  for (int32_t i = 0; i < listSize; ++i) {
5,486,056✔
4043
    SArray* pList = taosArrayGetP(pOReaders, i);
1,041,058✔
4044
    totalSize += taosArrayGetSize(pList);
1,041,058✔
4045
  }
4046

4047
  return totalSize;
4,444,998✔
4048
}
4049

4050
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
657,008✔
4051
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
657,008✔
4052
  if (NULL == pList) {
657,008✔
4053
    return NULL;
×
4054
  }
4055

4056
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
657,008✔
4057
}
4058

4059

4060
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
57,664✔
4061
  int32_t code = TSDB_CODE_SUCCESS;
57,664✔
4062
  int32_t lino = 0;
57,664✔
4063

4064
  if (NULL == pStatus->trigOReaders) {
57,664✔
4065
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
49,942✔
4066
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
49,942✔
4067
  }
4068

4069
  while (true) {
49,942✔
4070
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
107,606✔
4071

4072
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
107,606✔
4073
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
49,942✔
4074
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
49,942✔
4075

4076
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
99,884✔
4077
      continue;
49,942✔
4078
    }
4079

4080
    *ppRes = *ppOReaderList;
57,664✔
4081
    break;
57,664✔
4082
  }
4083

4084
_exit:
57,664✔
4085

4086
  if (code) {
57,664✔
4087
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4088
  }
4089

4090
  return code;
57,664✔
4091
}
4092

4093
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
67,215✔
4094
  int32_t code = TSDB_CODE_SUCCESS;
67,215✔
4095
  int32_t lino = 0;
67,215✔
4096
  bool    readerExists = false;
67,215✔
4097
  int64_t streamId = pTask->streamId;
67,215✔
4098

4099
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
67,215✔
4100
  for (int32_t i = 0; i < readerNum; ++i) {
140,487✔
4101
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
82,823✔
4102
    if (pReader->id.nodeId == vgId) {
82,823✔
4103
      readerExists = true;
9,551✔
4104
      break;
9,551✔
4105
    }
4106
  }
4107

4108
  if (!readerExists) {
67,215✔
4109
    SArray* pReaderList = NULL;
57,664✔
4110
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
57,664✔
4111
    
4112
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
57,664✔
4113
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
57,664✔
4114
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
57,664✔
4115
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
57,664✔
4116
  }
4117

4118
_exit:
9,551✔
4119

4120
  if (code) {
67,215✔
4121
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4122
  }
4123

4124
  return code;
67,215✔
4125
}
4126

4127
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
58,917✔
4128
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
58,917✔
4129
  int32_t lino = 0;
58,917✔
4130
  int32_t vgId = 0;
58,917✔
4131
  int64_t streamId = pTask->streamId;
58,917✔
4132
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
58,917✔
4133
  int32_t tbNum = taosArrayGetSize(pTbs);
58,917✔
4134
  SStreamDbTableName* pName = NULL;
58,917✔
4135
  SSHashObj* pDbVgroups = NULL;
58,917✔
4136
  SStreamMgmtRsp rsp = {0};
58,917✔
4137
  rsp.reqId = pTask->pMgmtReq->reqId;
58,917✔
4138
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
58,917✔
4139
  int32_t iter = 0;
58,917✔
4140
  void* p = NULL;
58,917✔
4141
  SSHashObj* pVgs = NULL;
58,917✔
4142
  SStreamMgmtReq* pMgmtReq = NULL;
58,917✔
4143
  int8_t stopped = 0;
58,917✔
4144

4145
  if (NULL == pCtx->pRsp->rsps.rspList) {
58,917✔
4146
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4147
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4148
  }
4149
  
4150
  TSWAP(pTask->pMgmtReq, pMgmtReq);
58,917✔
4151
  rsp.task = *(SStreamTask*)pTask;
58,917✔
4152

4153
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
58,917✔
4154
  if (NULL == pStatus) {
58,917✔
4155
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4156
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4157
  }
4158

4159
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
58,917✔
4160
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4161
    goto _exit;
×
4162
  }
4163

4164
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
58,917✔
4165

4166
  stopped = atomic_load_8(&pStatus->stopped);
58,917✔
4167
  if (stopped) {
58,917✔
4168
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4169
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4170
  }
4171

4172
  if (tbNum <= 0) {
58,917✔
4173
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4174
    goto _exit;
×
4175
  }
4176

4177
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
58,917✔
4178
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
58,917✔
4179
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
58,917✔
4180

4181
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
58,917✔
4182
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
58,917✔
4183
  
4184
  for (int32_t i = 0; i < tbNum; ++i) {
234,570✔
4185
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
175,653✔
4186
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
175,653✔
4187
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
351,306✔
4188
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
175,653✔
4189
  }
4190

4191
  int32_t vgNum = tSimpleHashGetSize(pVgs);
58,917✔
4192
  while (true) {
4193
    p = tSimpleHashIterate(pVgs, p, &iter);
126,132✔
4194
    if (NULL == p) {
126,132✔
4195
      break;
58,917✔
4196
    }
4197
    
4198
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
67,215✔
4199
  }
4200
  
4201
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
58,917✔
4202
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
58,917✔
4203
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
58,917✔
4204

4205
  SStreamTaskAddr addr;
58,917✔
4206
  for (int32_t i = 0; i < vgNum; ++i) {
116,581✔
4207
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
57,664✔
4208
    addr.taskId = pOTask->id.taskId;
57,664✔
4209
    addr.nodeId = pOTask->id.nodeId;
57,664✔
4210
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
57,664✔
4211
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
115,328✔
4212
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
57,664✔
4213
  }
4214

4215
_exit:
58,917✔
4216

4217
  tFreeSStreamMgmtReq(pMgmtReq);
58,917✔
4218
  taosMemoryFree(pMgmtReq);
58,917✔
4219

4220
  tSimpleHashCleanup(pVgs);
58,917✔
4221
  mstDestroyDbVgroupsHash(pDbVgroups);
58,917✔
4222

4223
  if (code) {
58,917✔
4224
    rsp.code = code;
×
4225
    
4226
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4227

4228
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4229
  } else {
4230
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
117,834✔
4231
  }
4232

4233
_final:
58,917✔
4234

4235
  if (finalCode) {
58,917✔
4236
    tFreeSStreamMgmtRsp(&rsp);
×
4237
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4238
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4239
  }
4240

4241
  return finalCode;
58,917✔
4242
}
4243

4244
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
2,266✔
4245
  int32_t num = taosArrayGetSize(pList);
2,266✔
4246
  SStreamCalcScan* pScan = NULL;
2,266✔
4247
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
2,266✔
4248
  int64_t planUid = 0;
2,266✔
4249
  for (int32_t i = 0; i < num; ++i) {
2,266✔
4250
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
2,266✔
4251
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
2,266✔
4252
    if (0 != planUid && planUid == uid) {
2,266✔
4253
      *ppRes = pScan;
2,266✔
4254
      break;
2,266✔
4255
    }
4256
  }
4257

4258
_exit:
2,266✔
4259

4260
  if (code) {
2,266✔
4261
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4262
  }
4263

4264
  return code;
2,266✔
4265
}
4266

4267
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,266✔
4268
  int32_t code = TSDB_CODE_SUCCESS;
2,266✔
4269
  int32_t lino = 0;
2,266✔
4270
  bool    readerExists = false;
2,266✔
4271
  int64_t streamId = pTask->streamId;
2,266✔
4272
  SListNode* pNode = listHead(pStatus->calcReaders);
2,266✔
4273
  SStmTaskStatus* pReader = NULL;
2,266✔
4274
  int32_t taskIdx = 0;
2,266✔
4275

4276
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,266✔
4277
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
21,012✔
4278
    pReader = (SStmTaskStatus*)pNode->data;
18,746✔
4279
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
18,746✔
4280
      readerExists = true;
×
4281
      pAddr->taskId = pReader->id.taskId;
×
4282
      break;
×
4283
    }
4284
  }
4285

4286
  if (!readerExists) {
2,266✔
4287
    if (NULL == pStatus->calcReaders) {
2,266✔
4288
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4289
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4290
      taskIdx = 0;
×
4291
    } else {
4292
      pNode = listTail(pStatus->calcReaders);
2,266✔
4293
      pReader = (SStmTaskStatus*)pNode->data;
2,266✔
4294
      taskIdx = pReader->id.taskIdx + 1;
2,266✔
4295
    }
4296

4297
    SStreamCalcScan* pScan = NULL;
2,266✔
4298
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,266✔
4299
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,266✔
4300
    pReader = tdListReserve(pStatus->calcReaders);
2,266✔
4301
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,266✔
4302
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,266✔
4303
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,266✔
4304
    pAddr->taskId = pReader->id.taskId;
2,266✔
4305
  }
4306

4307
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,266✔
4308
  pAddr->nodeId = vgId;
2,266✔
4309

4310
_exit:
2,266✔
4311

4312
  if (code) {
2,266✔
4313
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4314
  }
4315

4316
  return code;
2,266✔
4317
}
4318

4319

4320
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,266✔
4321
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
2,266✔
4322
  int32_t lino = 0;
2,266✔
4323
  int32_t vgId = 0;
2,266✔
4324
  int64_t streamId = pTask->streamId;
2,266✔
4325
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,266✔
4326
  int32_t reqNum = taosArrayGetSize(pReqs);
2,266✔
4327
  SStreamOReaderDeployReq* pReq = NULL;
2,266✔
4328
  SStreamOReaderDeployRsp* pRsp = NULL;
2,266✔
4329
  SStreamMgmtRsp rsp = {0};
2,266✔
4330
  rsp.reqId = pTask->pMgmtReq->reqId;
2,266✔
4331
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,266✔
4332
  SStreamMgmtReq* pMgmtReq = NULL;
2,266✔
4333
  int8_t stopped = 0;
2,266✔
4334
  int32_t vgNum = 0;
2,266✔
4335
  SStreamTaskAddr* pAddr = NULL;
2,266✔
4336

4337
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,266✔
4338
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4339
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4340
  }
4341
  
4342
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,266✔
4343
  rsp.task = *(SStreamTask*)pTask;
2,266✔
4344

4345
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,266✔
4346
  if (NULL == pStatus) {
2,266✔
4347
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4348
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4349
  }
4350

4351
  stopped = atomic_load_8(&pStatus->stopped);
2,266✔
4352
  if (stopped) {
2,266✔
4353
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4354
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4355
  }
4356

4357
  if (reqNum <= 0) {
2,266✔
4358
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4359
    goto _exit;
×
4360
  }
4361

4362
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,266✔
4363
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,266✔
4364

4365
  for (int32_t i = 0; i < reqNum; ++i) {
4,532✔
4366
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,266✔
4367
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,266✔
4368
    pRsp->execId = pReq->execId;
2,266✔
4369
    vgNum = taosArrayGetSize(pReq->vgIds);
2,266✔
4370
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,266✔
4371
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,266✔
4372
    
4373
    for (int32_t n = 0; n < vgNum; ++n) {
4,532✔
4374
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,266✔
4375
      pAddr = taosArrayGet(pRsp->vgList, n);
2,266✔
4376
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,266✔
4377
    }
4378
  }
4379

4380
_exit:
2,266✔
4381

4382
  tFreeSStreamMgmtReq(pMgmtReq);
2,266✔
4383
  taosMemoryFree(pMgmtReq);
2,266✔
4384

4385
  if (code) {
2,266✔
4386
    rsp.code = code;
×
4387
    
4388
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4389
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4390
  } else {
4391
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,532✔
4392
  }
4393

4394

4395
_final:
2,266✔
4396

4397
  if (finalCode) {
2,266✔
4398
    tFreeSStreamMgmtRsp(&rsp);
×
4399
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4400
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4401
  }
4402

4403
  return finalCode;
2,266✔
4404
}
4405

4406

4407
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
61,183✔
4408
  int32_t code = TSDB_CODE_SUCCESS;
61,183✔
4409
  int32_t lino = 0;
61,183✔
4410

4411
  switch (pTask->pMgmtReq->type) {
61,183✔
4412
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
58,917✔
4413
      msmDeployTriggerOrigReader(pCtx, pTask);
58,917✔
4414
      break;
58,917✔
4415
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,266✔
4416
      msmDeployRunnerOrigReader(pCtx, pTask);
2,266✔
4417
      break;
2,266✔
4418
    default:
×
4419
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4420
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4421
      break;
×
4422
  }
4423

4424
_exit:
61,183✔
4425

4426
  if (code) {
61,183✔
4427
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4428
  }
4429

4430
  return code;
61,183✔
4431
}
4432

4433
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
16,889✔
4434
  int32_t code = TSDB_CODE_SUCCESS;
16,889✔
4435
  int32_t lino = 0;
16,889✔
4436
  SStreamHbMsg* pReq = pCtx->pReq;
16,889✔
4437
  SStmTaskStatusMsg* pTask = NULL;
16,889✔
4438
  
4439
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
16,889✔
4440
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
16,889✔
4441
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
16,889✔
4442
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
16,889✔
4443
  }
4444
  
4445
  for (int32_t i = 0; i < reqNum; ++i) {
78,072✔
4446
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
61,183✔
4447
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
61,183✔
4448
    if (NULL == pTask) {
61,183✔
4449
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4450
      continue;
×
4451
    }
4452

4453
    if (NULL == pTask->pMgmtReq) {
61,183✔
4454
      msttError("idx %d without mgmtReq", idx);
×
4455
      continue;
×
4456
    }
4457

4458
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
61,183✔
4459
  }
4460

4461
_exit:
16,889✔
4462

4463
  if (code) {
16,889✔
4464
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4465
  }
4466

4467
  return code;
16,889✔
4468
}
4469

4470
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
13,771,544✔
4471
  int32_t code = TSDB_CODE_SUCCESS;
13,771,544✔
4472
  int32_t lino = 0;
13,771,544✔
4473
  SStreamHbMsg* pReq = pCtx->pReq;
13,771,544✔
4474

4475
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
13,771,544✔
4476
  if (GOT_SNODE(pReq->snodeId)) {
13,638,378✔
4477
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
806,501✔
4478
  }
4479
  
4480
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
13,638,172✔
4481
    msmHandleStreamActions(pCtx);
26,671✔
4482
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
26,671✔
4483
  }
4484

4485
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
13,638,172✔
4486
    code = msmHandleStreamRequests(pCtx);
16,889✔
4487
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
16,889✔
4488
    TAOS_CHECK_EXIT(code);
16,889✔
4489
  }
4490

4491
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,638,172✔
4492
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
44,966✔
4493
  } else {
4494
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
13,593,206✔
4495
  }
4496

4497
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
13,638,172✔
4498
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
32,885✔
4499
  }
4500

4501
  if (taosHashGetSize(pCtx->deployStm) > 0) {
13,638,172✔
4502
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
44,460✔
4503
  }
4504

4505
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,638,172✔
4506
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
718,578✔
4507
  }
4508

4509
  if (taosHashGetSize(pCtx->actionStm) > 0) {
13,638,172✔
4510
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
90,624✔
4511
  }
4512

4513
_exit:
13,638,172✔
4514

4515
  if (code) {
13,771,544✔
4516
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
133,372✔
4517
  }
4518

4519
  return code;
13,771,544✔
4520
}
4521

4522
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
13,870,062✔
4523
  int32_t lino = 0;
13,870,062✔
4524
  int32_t tlen = 0;
13,870,062✔
4525
  void   *buf = NULL;
13,870,062✔
4526

4527
  if (TSDB_CODE_SUCCESS != code) {
13,870,062✔
4528
    goto _exit;
133,372✔
4529
  }
4530

4531
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
13,736,690✔
4532
  if (code < 0) {
13,736,690✔
4533
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4534
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4535
  }
4536

4537
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
13,736,690✔
4538
  if (buf == NULL) {
13,736,690✔
4539
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4540
    TAOS_CHECK_EXIT(terrno);    
×
4541
  }
4542

4543
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
13,736,690✔
4544
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
13,736,690✔
4545

4546
  SEncoder encoder;
13,734,297✔
4547
  tEncoderInit(&encoder, abuf, tlen);
13,736,690✔
4548
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
13,736,690✔
4549
    rpcFreeCont(buf);
×
4550
    buf = NULL;
×
4551
    tEncoderClear(&encoder);
×
4552
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4553
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4554
  }
4555
  tEncoderClear(&encoder);
13,736,690✔
4556

4557
_exit:
13,870,062✔
4558

4559
  pMsg->code = code;
13,870,062✔
4560
  pMsg->info = *pRpcInfo;
13,870,062✔
4561
  if (TSDB_CODE_SUCCESS == code) {
13,870,062✔
4562
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
13,736,690✔
4563
    pMsg->pCont = buf;
13,736,690✔
4564
  }
4565
}
13,870,062✔
4566

4567

4568
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
13,870,062✔
4569
  int32_t code = TSDB_CODE_SUCCESS;
13,870,062✔
4570
  SMStreamHbRspMsg rsp = {0};
13,870,062✔
4571
  rsp.streamGId = pHb->streamGId;
13,870,062✔
4572

4573
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
13,870,062✔
4574

4575
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
13,870,062✔
4576
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
86,927✔
4577
    goto _exit;
86,927✔
4578
  }
4579

4580
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
13,783,135✔
4581
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
13,783,135✔
4582

4583
  pCtx->tidx = tidx;
13,783,135✔
4584
  pCtx->pMnode = pMnode;
13,783,135✔
4585
  pCtx->currTs = currTs;
13,783,135✔
4586
  pCtx->pReq = pHb;
13,783,135✔
4587
  pCtx->pRsp = &rsp;
13,783,135✔
4588
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
13,783,135✔
4589
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
13,783,135✔
4590
  
4591
  switch (atomic_load_8(&mStreamMgmt.state)) {
13,783,135✔
4592
    case MND_STM_STATE_WATCH:
11,591✔
4593
      code = msmWatchHandleHbMsg(pCtx);
11,591✔
4594
      break;
11,591✔
4595
    case MND_STM_STATE_NORMAL:
13,771,544✔
4596
      code = msmNormalHandleHbMsg(pCtx);
13,771,544✔
4597
      break;
13,771,544✔
4598
    default:
×
4599
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4600
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4601
      break;
×
4602
  }
4603

4604
_exit:
13,870,062✔
4605

4606
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
13,870,062✔
4607

4608
  msmCleanStreamGrpCtx(pHb);
13,870,062✔
4609
  msmClearStreamToDeployMaps(pHb);
13,870,062✔
4610

4611
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
13,870,062✔
4612
  
4613
  tFreeSMStreamHbRspMsg(&rsp);
13,870,062✔
4614

4615
  return code;
13,870,062✔
4616
}
4617

4618
void msmHandleBecomeLeader(SMnode *pMnode) {
369,214✔
4619
  if (tsDisableStream) {
369,214✔
4620
    return;
×
4621
  }
4622

4623
  mstInfo("start to process mnode become leader");
369,214✔
4624

4625
  int32_t code = 0;
369,214✔
4626
  streamAddVnodeLeader(MNODE_HANDLE);
369,214✔
4627
  
4628
  taosWLockLatch(&mStreamMgmt.runtimeLock);
369,214✔
4629
  msmDestroyRuntimeInfo(pMnode);
369,214✔
4630
  code = msmInitRuntimeInfo(pMnode);
369,214✔
4631
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
369,214✔
4632

4633
  if (TSDB_CODE_SUCCESS == code) {
369,214✔
4634
    atomic_store_8(&mStreamMgmt.active, 1);
369,214✔
4635
  }
4636

4637
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
369,214✔
4638
}
4639

4640
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
456,254✔
4641
  if (tsDisableStream) {
456,254✔
4642
    return;
×
4643
  }
4644

4645
  mstInfo("start to process mnode become not leader");
456,254✔
4646

4647
  streamRemoveVnodeLeader(MNODE_HANDLE);
456,254✔
4648

4649
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
456,254✔
4650
    taosWLockLatch(&mStreamMgmt.runtimeLock);
369,214✔
4651
    msmDestroyRuntimeInfo(pMnode);
369,214✔
4652
    mStreamMgmt.stat.inactiveTimes++;
369,214✔
4653
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
369,214✔
4654
  }
4655
}
4656

4657

4658
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4659
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4660
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4661
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4662
  } else {
4663
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4664
  }
4665
}
×
4666

4667
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
6,716✔
4668
  int32_t code = TSDB_CODE_SUCCESS;
6,716✔
4669
  int32_t lino = 0;
6,716✔
4670
  SStreamObj* pStream = pObj;
6,716✔
4671
  SSnodeObj* pSnode = p1;
6,716✔
4672
  SArray** ppRes = p2;
6,716✔
4673

4674
  if (pStream->mainSnodeId == pSnode->id) {
6,716✔
4675
    if (NULL == *ppRes) {
4,065✔
4676
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
573✔
4677
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
573✔
4678
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
573✔
4679
    }
4680

4681
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
8,130✔
4682
  }
4683

4684
  return true;
6,716✔
4685

4686
_exit:
×
4687

4688
  if (code) {
×
4689
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4690
  }  
4691

4692
  *(int32_t*)p3 = code;
×
4693

4694
  return false;
×
4695
}
4696

4697

4698
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
38,069✔
4699
  int32_t code = TSDB_CODE_SUCCESS;
38,069✔
4700
  int32_t lino = 0;
38,069✔
4701
  
4702
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
38,069✔
4703
  TAOS_CHECK_EXIT(code);
38,069✔
4704

4705
  int32_t streamNum = taosArrayGetSize(*ppRes);
38,069✔
4706
  if (streamNum > 0 && 0 == pSnode->replicaId) {
38,069✔
4707
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
192✔
4708
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
192✔
4709
  }
4710

4711
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4712

4713
_exit:
38,069✔
4714

4715
  if (code) {
38,069✔
4716
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
192✔
4717
  }  
4718

4719
  return code;
38,069✔
4720
}
4721

4722
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
395,605✔
4723
  SStreamObj* pStream = pObj;
395,605✔
4724
  int64_t streamId = pStream->pCreate->streamId;
395,605✔
4725
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
395,605✔
4726
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
395,605✔
4727
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
395,605✔
4728
  
4729
  if ((userDropped || userStopped) && (NULL == pStatus)) {
395,605✔
4730
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
384✔
4731
    return true;
384✔
4732
  }
4733
  
4734
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
395,221✔
4735
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
299,505✔
4736
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4737
    return true;
299,505✔
4738
  }
4739

4740
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
95,716✔
4741
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
16,384✔
4742
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4743
    return true;
16,384✔
4744
  }  
4745

4746
  if (pStatus) {
79,332✔
4747
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
79,108✔
4748
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4749
    }
4750

4751
    return true;
79,108✔
4752
  }
4753

4754
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
224✔
4755

4756
  return true;
224✔
4757
}
4758

4759
void msmCheckLoopStreamMap(SMnode *pMnode) {
33,850✔
4760
  SStmStatus* pStatus = NULL;
33,850✔
4761
  void* pIter = NULL;
33,850✔
4762
  int8_t stopped = 0;
33,850✔
4763
  int64_t streamId = 0;
33,850✔
4764
  
4765
  while (true) {
4766
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
268,193✔
4767
    if (NULL == pIter) {
268,193✔
4768
      break;
33,850✔
4769
    }
4770

4771
    pStatus = (SStmStatus*)pIter;
234,343✔
4772

4773
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
234,343✔
4774
    stopped = atomic_load_8(&pStatus->stopped);
234,343✔
4775
    if (MST_IS_USER_STOPPED(stopped)) {
234,343✔
4776
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
402✔
4777
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
402✔
4778
      continue;
402✔
4779
    }
4780

4781
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
233,941✔
4782
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4783
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4784
      continue;
×
4785
    }
4786

4787
    if (MST_IS_ERROR_STOPPED(stopped)) {
233,941✔
4788
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
14,092✔
4789
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
9,532✔
4790
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4791
            
4792
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
9,532✔
4793
        continue;
9,532✔
4794
      }
4795

4796
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
4,560✔
4797
      continue;
4,560✔
4798
    }
4799

4800
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
219,849✔
4801
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4802
      continue;
×
4803
    }
4804
  }
4805
}
33,850✔
4806

4807
void msmCheckStreamsStatus(SMnode *pMnode) {
234,356✔
4808
  SStmCheckStatusCtx ctx = {0};
234,356✔
4809

4810
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
234,356✔
4811
  
4812
  if (MST_READY_FOR_SDB_LOOP()) {
234,356✔
4813
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
47,269✔
4814
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
47,269✔
4815
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
47,269✔
4816
  }
4817

4818
  if (MST_READY_FOR_MAP_LOOP()) {
234,356✔
4819
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
33,850✔
4820
    msmCheckLoopStreamMap(pMnode);
33,850✔
4821
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
33,850✔
4822
  }
4823
}
234,356✔
4824

4825
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
2,299,259✔
4826
  for (int32_t i = 0; i < taskNum; ++i) {
4,709,639✔
4827
    SStmTaskStatus* pTask = *(pList + i);
2,410,380✔
4828

4829
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
2,410,380✔
4830
      continue;
56,441✔
4831
    }
4832
    
4833
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
2,355,773✔
4834
      continue;
2,354,653✔
4835
    }
4836

4837
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,120✔
4838
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,120✔
4839
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
×
4840
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4841
          
4842
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
×
4843
      break;
×
4844
    }
4845

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

4849
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,120✔
4850
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,120✔
4851

4852
    SStmTaskAction task = {0};
1,120✔
4853
    task.streamId = streamId;
1,120✔
4854
    task.id = pTask->id;
1,120✔
4855
    task.flag = pTask->flags;
1,120✔
4856
    task.type = pTask->type;
1,120✔
4857
    
4858
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,120✔
4859
  }
4860
}
2,299,259✔
4861

4862
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
96,188✔
4863
  void* pIter = NULL;
96,188✔
4864
  SStmVgStreamStatus* pVg = NULL;
96,188✔
4865
  int64_t streamId = 0;
96,188✔
4866
  
4867
  while (true) {
888,916✔
4868
    pIter = taosHashIterate(pStreams, pIter);
985,104✔
4869
    if (NULL == pIter) {
985,104✔
4870
      break;
96,188✔
4871
    }
4872

4873
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
888,916✔
4874
    pVg = (SStmVgStreamStatus*)pIter;
888,916✔
4875

4876
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
888,916✔
4877
    if (taskNum > 0) {
888,916✔
4878
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
529,990✔
4879
    }
4880

4881
    taskNum = taosArrayGetSize(pVg->calcReaders);
888,916✔
4882
    if (taskNum > 0) {
888,916✔
4883
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
569,728✔
4884
    }
4885
  }
4886
}
96,188✔
4887

4888
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4889
  int64_t streamId = 0;
×
4890
  void* pIter = NULL;
×
4891
  SStmVgStreamStatus* pStream = NULL;
×
4892

4893
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4894
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4895
    return;
×
4896
  }
4897

4898
  
4899
  while (true) {
4900
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4901
    if (NULL == pIter) {
×
4902
      break;
×
4903
    }
4904

4905
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4906
    
4907
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4908
  }
4909

4910
  taosHashClear(pVg->streamTasks);
×
4911
}
4912

4913

4914
void msmCheckVgroupStatus(SMnode *pMnode) {
234,356✔
4915
  void* pIter = NULL;
234,356✔
4916
  int32_t code = 0;
234,356✔
4917
  
4918
  while (true) {
898,749✔
4919
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,133,105✔
4920
    if (NULL == pIter) {
1,133,105✔
4921
      break;
234,356✔
4922
    }
4923

4924
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
898,749✔
4925
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
898,749✔
4926
      continue;
800,767✔
4927
    }
4928
    
4929
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
97,982✔
4930

4931
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
97,982✔
4932
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
1,794✔
4933
      if (NULL == pVgroup) {
1,794✔
4934
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
1,794✔
4935
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
1,794✔
4936
        if (code) {
1,794✔
4937
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4938
        }
4939
        continue;
1,794✔
4940
      }
4941
      mndReleaseVgroup(pMnode, pVgroup);
×
4942
      
4943
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4944
      
4945
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
4946
      continue;
×
4947
    }
4948

4949
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
96,188✔
4950

4951
    msmCheckVgroupStreamStatus(pVg->streamTasks);
96,188✔
4952
  }
4953
}
234,356✔
4954

4955
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
×
4956
  *deployNum = 0;
×
4957
  
4958
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
×
4959
    if (pStream->runners[i]) {
×
4960
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
×
4961
      for (int32_t t = 0; t < taskNum; ++t) {
×
4962
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
×
4963
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
×
4964
        if (stopped) {
×
4965
          mstsDebug("stream already stopped %d, ignore it", stopped);
×
4966
          *deployNum = 0;
×
4967
          return;
×
4968
        }
4969

4970
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
×
4971
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
×
4972
      }
4973
      
4974
      deployId[*deployNum] = i;
×
4975
      (*deployNum)++;
×
4976
    }
4977
  }
4978
}
4979

4980
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
700✔
4981
  pSnode->runnerThreadNum = -1;
700✔
4982

4983
  (void)msmSTAddSnodesToMap(pMnode);
700✔
4984

4985
  int64_t streamId = 0;
700✔
4986
  void* pIter = NULL;
700✔
4987
  SStmSnodeStreamStatus* pStream = NULL;
700✔
4988
  int32_t deployNum = 0;
700✔
4989
  SStmTaskAction task = {0};
700✔
4990
  
4991
  while (true) {
4992
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
1,260✔
4993
    if (NULL == pIter) {
1,260✔
4994
      break;
700✔
4995
    }
4996

4997
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
560✔
4998
    
4999
    task.streamId = streamId;
560✔
5000
    
5001
    pStream = (SStmSnodeStreamStatus*)pIter;
560✔
5002
    if (pStream->trigger) {
560✔
5003
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
560✔
5004
      if (stopped) {
560✔
5005
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
5006
        continue;
×
5007
      }
5008

5009
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
560✔
5010
      
5011
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
560✔
5012
    } else {
5013
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
×
5014
      
5015
      if (task.deployNum > 0) {
×
5016
        //task.triggerStatus = pStream->trigger;
5017
        task.multiRunner = true;
×
5018
        task.type = STREAM_RUNNER_TASK;
×
5019
        
5020
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
×
5021
        
5022
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
×
5023
      }
5024
    }
5025
  }
5026

5027
  taosHashClear(pSnode->streamTasks);
700✔
5028
}
700✔
5029

5030

5031
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
31,571✔
5032
  void* pIter = NULL;
31,571✔
5033
  SStmSnodeStreamStatus* pSnode = NULL;
31,571✔
5034
  int64_t streamId = 0;
31,571✔
5035
  
5036
  while (true) {
5037
    pIter = taosHashIterate(pStreams, pIter);
347,911✔
5038
    if (NULL == pIter) {
347,911✔
5039
      break;
31,571✔
5040
    }
5041

5042
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
316,340✔
5043
    pSnode = (SStmSnodeStreamStatus*)pIter;
316,340✔
5044

5045
    if (NULL != pSnode->trigger) {
316,340✔
5046
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
307,864✔
5047
    }
5048

5049
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,265,360✔
5050
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
949,020✔
5051
      if (taskNum > 0) {
949,020✔
5052
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
891,677✔
5053
      }
5054
    }
5055
  }
5056
}
31,571✔
5057

5058

5059
void msmCheckSnodeStatus(SMnode *pMnode) {
234,356✔
5060
  void* pIter = NULL;
234,356✔
5061
  
5062
  while (true) {
284,496✔
5063
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
518,852✔
5064
    if (NULL == pIter) {
518,852✔
5065
      break;
234,356✔
5066
    }
5067

5068
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
284,496✔
5069
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
284,496✔
5070
      continue;
248,774✔
5071
    }
5072

5073
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
35,722✔
5074
    
5075
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
35,722✔
5076
    if (NULL == pSnode->streamTasks) {
35,722✔
5077
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
3,591✔
5078
      continue;
3,591✔
5079
    }
5080
    
5081
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
32,131✔
5082
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
560✔
5083
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5084
      
5085
      msmHandleSnodeLost(pMnode, pSnode);
560✔
5086
      continue;
560✔
5087
    }
5088
    
5089
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
31,571✔
5090

5091
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
31,571✔
5092
  }
5093
}
234,356✔
5094

5095

5096
void msmCheckTasksStatus(SMnode *pMnode) {
234,356✔
5097
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
234,356✔
5098

5099
  msmCheckVgroupStatus(pMnode);
234,356✔
5100
  msmCheckSnodeStatus(pMnode);
234,356✔
5101
}
234,356✔
5102

5103
void msmCheckSnodesState(SMnode *pMnode) {
234,356✔
5104
  if (!MST_READY_FOR_SNODE_LOOP()) {
234,356✔
5105
    return;
206,037✔
5106
  }
5107

5108
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
28,319✔
5109

5110
  void* pIter = NULL;
28,319✔
5111
  int32_t snodeId = 0;
28,319✔
5112
  while (true) {
30,784✔
5113
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
59,103✔
5114
    if (NULL == pIter) {
59,103✔
5115
      break;
28,319✔
5116
    }
5117

5118
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
30,784✔
5119
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
30,784✔
5120
      continue;
30,644✔
5121
    }
5122

5123
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
140✔
5124
    if (NULL == pSnode->streamTasks) {
140✔
5125
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5126
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5127
      continue;
×
5128
    }
5129
    
5130
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
140✔
5131
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5132
    
5133
    msmHandleSnodeLost(pMnode, pSnode);
140✔
5134
  }
5135

5136
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
28,319✔
5137
}
5138

5139
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
7,850,782✔
5140
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
7,850,782✔
5141
  if (0 == active || MND_STM_STATE_NORMAL != state) {
7,850,782✔
5142
    mstTrace("ignore health check since active:%d state:%d", active, state);
1,905✔
5143
    return false;
1,905✔
5144
  }
5145

5146
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,848,877✔
5147
    mstTrace("ignore health check since no stream now");
7,380,165✔
5148
    return false;
7,380,165✔
5149
  }
5150

5151
  return true;
468,712✔
5152
}
5153

5154
void msmHealthCheck(SMnode *pMnode) {
7,616,426✔
5155
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,616,426✔
5156
    return;
7,382,070✔
5157
  }
5158

5159
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
445,420✔
5160
  
5161
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
234,356✔
5162
  if (!msmCheckNeedHealthCheck(pMnode)) {
234,356✔
5163
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5164
    return;
×
5165
  }
5166
  
5167
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
234,356✔
5168
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
234,356✔
5169

5170
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
234,356✔
5171
  
5172
  msmCheckStreamsStatus(pMnode);
234,356✔
5173
  msmCheckTasksStatus(pMnode);
234,356✔
5174
  msmCheckSnodesState(pMnode);
234,356✔
5175

5176
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
234,356✔
5177

5178
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
234,356✔
5179
}
5180

5181
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
827✔
5182
  SStreamObj *pStream = pObj;
827✔
5183
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
827✔
5184
    return true;
×
5185
  }
5186
  
5187
  pStream->updateTime = *(int64_t*)p1;
827✔
5188
  
5189
  (*(int32_t*)p2)++;
827✔
5190
  
5191
  return true;
827✔
5192
}
5193

5194
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5195
  int32_t code = 0;
×
5196
  int8_t  stopped = 0;
×
5197
  
5198
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5199
  
5200
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5201
  if (NULL == pStatus) {
×
5202
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5203
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5204
    goto _exit;
×
5205
  }
5206

5207
  stopped = atomic_load_8(&pStatus->stopped);
×
5208
  if (stopped) {
×
5209
    mstsError("stream already stopped, stopped:%d", stopped);
×
5210
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5211
    goto _exit;
×
5212
  }
5213

5214
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5215
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5216
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5217
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5218
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5219
    goto _exit;
×
5220
  }
5221

5222
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
×
5223
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5224

5225
_exit:
×
5226
  
5227
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5228

5229
  return code;
×
5230
}
5231

5232
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
369,214✔
5233
  int32_t code = TSDB_CODE_SUCCESS;
369,214✔
5234
  int32_t lino = 0;
369,214✔
5235
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
369,214✔
5236
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
369,214✔
5237
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
369,214✔
5238

5239
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
738,347✔
5240

5241
  mStreamMgmt.stat.activeTimes++;
369,214✔
5242
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
369,214✔
5243
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
369,214✔
5244
  if (NULL == mStreamMgmt.tCtx) {
369,214✔
5245
    code = terrno;
×
5246
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5247
    goto _exit;
×
5248
  }
5249

5250
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
369,214✔
5251
  if (mStreamMgmt.actionQ == NULL) {
369,214✔
5252
    code = terrno;
×
5253
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5254
    goto _exit;
×
5255
  }
5256
  
5257
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
369,214✔
5258
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
369,214✔
5259
  
5260
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
369,214✔
5261
  
5262
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,215,092✔
5263
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,845,878✔
5264

5265
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
11,075,268✔
5266
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,229,390✔
5267
      if (pCtx->deployStm[m] == NULL) {
9,229,390✔
5268
        code = terrno;
×
5269
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5270
        goto _exit;
×
5271
      }
5272
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
9,229,390✔
5273
      
5274
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,229,390✔
5275
      if (pCtx->actionStm[m] == NULL) {
9,229,390✔
5276
        code = terrno;
×
5277
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5278
        goto _exit;
×
5279
      }
5280
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
9,229,390✔
5281
    }
5282
  }
5283
  
5284
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
369,214✔
5285
  if (mStreamMgmt.streamMap == NULL) {
369,214✔
5286
    code = terrno;
×
5287
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5288
    goto _exit;
×
5289
  }
5290
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
369,214✔
5291
  
5292
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
369,214✔
5293
  if (mStreamMgmt.taskMap == NULL) {
369,214✔
5294
    code = terrno;
×
5295
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5296
    goto _exit;
×
5297
  }
5298
  
5299
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
369,214✔
5300
  if (mStreamMgmt.vgroupMap == NULL) {
369,214✔
5301
    code = terrno;
×
5302
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5303
    goto _exit;
×
5304
  }
5305
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
369,214✔
5306

5307
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
369,214✔
5308
  if (mStreamMgmt.snodeMap == NULL) {
369,214✔
5309
    code = terrno;
×
5310
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5311
    goto _exit;
×
5312
  }
5313
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
369,214✔
5314
  
5315
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
369,214✔
5316
  if (mStreamMgmt.dnodeMap == NULL) {
369,214✔
5317
    code = terrno;
×
5318
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5319
    goto _exit;
×
5320
  }
5321

5322
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
369,214✔
5323
  if (mStreamMgmt.toDeployVgMap == NULL) {
369,214✔
5324
    code = terrno;
×
5325
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5326
    goto _exit;
×
5327
  }
5328
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
369,214✔
5329
  
5330
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
369,214✔
5331
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
369,214✔
5332
    code = terrno;
×
5333
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5334
    goto _exit;
×
5335
  }
5336
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
369,214✔
5337

5338
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
369,214✔
5339
  if (mStreamMgmt.toUpdateScanMap == NULL) {
369,214✔
5340
    code = terrno;
×
5341
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5342
    goto _exit;
×
5343
  }
5344
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
369,214✔
5345

5346
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
369,214✔
5347
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
369,214✔
5348

5349
  mStreamMgmt.lastTaskId = 1;
369,214✔
5350

5351
  int32_t activeStreamNum = 0;
369,214✔
5352
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
369,214✔
5353

5354
  if (activeStreamNum > 0) {
369,214✔
5355
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
635✔
5356
  } else {
5357
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
368,579✔
5358
  }
5359

5360
_exit:
369,214✔
5361

5362
  if (code) {
369,214✔
5363
    msmDestroyRuntimeInfo(pMnode);
×
5364
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5365
  } else {
5366
    mstInfo("mnode stream runtime init done");
369,214✔
5367
  }
5368

5369
  return code;
369,214✔
5370
}
5371

5372

5373

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