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

taosdata / TDengine / #4908

30 Dec 2025 10:52AM UTC coverage: 65.386% (-0.2%) from 65.541%
#4908

push

travis-ci

web-flow
enh: drop multi-stream (#33962)

60 of 106 new or added lines in 4 files covered. (56.6%)

1330 existing lines in 113 files now uncovered.

193461 of 295877 relevant lines covered (65.39%)

115765274.47 hits per line

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

76.17
/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() {
732,444✔
34
  SStmQNode* pQNode = NULL;
732,444✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
732,444✔
37
    return;
366,222✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,830,919✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,985,514✔
49
    taosHashCleanup(pCtx->deployStm[m]);
9,154,595✔
50
    taosHashCleanup(pCtx->actionStm[m]);
9,154,595✔
51
  }
52
}
1,830,919✔
53

54
void msmDestroyThreadCtxs() {
732,444✔
55
  if (NULL == mStreamMgmt.tCtx) {
732,444✔
56
    return;
366,222✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,197,141✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,830,919✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
366,222✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
732,444✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
732,444✔
94
}
732,444✔
95

96

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

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

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

111
    pStatus = pStream;
320✔
112
  }
113

114
  int8_t stopped = atomic_load_8(&pStatus->stopped);
4,473✔
115
  if (stopped) {
4,473✔
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) {
4,473✔
121
    pStatus->fatalRetryTimes = 0;
382✔
122
    mstsDebug("reset stream retryTimes, running duation:%" PRId64 "ms", currTs - pStatus->triggerTask->runningStartTs);
382✔
123
  }
124

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

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

136
_exit:
422✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
4,473✔
139

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

145

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

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

200
  pSnode = NULL;
385,901✔
201

202
_exit:
385,901✔
203

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

208
  return code;
385,901✔
209
}
210

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

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

235
  pDnode = NULL;
820,513✔
236

237
_exit:
820,513✔
238

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

243
  return code;
820,513✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
455,455✔
275
  int32_t code = TSDB_CODE_SUCCESS;
455,455✔
276
  int32_t lino = 0;
455,455✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
455,455✔
278
  if (NULL == pStream) {
455,455✔
279
    SStmVgStreamStatus stream = {0};
342,932✔
280
    if (trigReader) {
342,932✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
208,238✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
208,238✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
416,476✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
134,694✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
134,694✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
269,388✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
342,932✔
290
    goto _exit;
342,932✔
291
  }
292
  
293
  if (trigReader) {
112,523✔
294
    if (NULL == pStream->trigReaders) {
21,867✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
21,867✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
21,867✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
43,734✔
300
    goto _exit;
21,867✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
90,656✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
62,791✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
62,791✔
306
  }
307

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

310
_exit:
90,656✔
311

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

320
  return code;
455,455✔
321
}
322

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

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

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

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

350
  return code;
455,455✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
456,415✔
363
    if (NULL == pVg) {
456,415✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
83,174✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
83,174✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
166,348✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
83,174✔
368
      if (TSDB_CODE_SUCCESS == code) {
83,174✔
369
        goto _return;
83,174✔
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);
373,241✔
381
    if (NULL == pVg->taskList) {
373,241✔
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)) {
746,482✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
373,241✔
390
    
391
    taosHashRelease(pVgMap, pVg);
373,241✔
392
    break;
373,241✔
393
  }
394
  
395
_return:
456,415✔
396

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

404
  return code;
456,415✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
573,675✔
409
  int32_t code = TSDB_CODE_SUCCESS;
573,675✔
410
  int32_t lino = 0;
573,675✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
573,675✔
412
  if (NULL == pStream) {
573,675✔
413
    SStmSnodeStreamStatus stream = {0};
146,852✔
414
    if (deployId < 0) {
146,852✔
415
      stream.trigger = pStatus;
3,120✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
143,732✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
143,732✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
287,464✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
146,852✔
423
    goto _exit;
146,852✔
424
  }
425
  
426
  if (deployId < 0) {
426,823✔
427
    if (NULL != pStream->trigger) {
137,787✔
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;
137,787✔
433
    goto _exit;
137,787✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
289,036✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
270,184✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
270,184✔
439
  }
440

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

443
_exit:
289,036✔
444

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

453
  return code;
573,675✔
454
}
455

456

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

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

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

484
  return code;
573,675✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
140,907✔
551
}
552

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

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

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

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

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

614
  return code;
432,768✔
615
}
616

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

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

632
_exit:
413,916✔
633

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

638
  return code;
413,916✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
9,831✔
676
    }
677

678
    break;
679
  }
680

UNCOV
681
_exit:
×
682

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

UNCOV
687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
55,401✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
13,512,474✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
51,023,417✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
37,510,943✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
37,510,943✔
725
  }
726

727
_exit:
13,512,474✔
728

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

733
  return code;
13,512,474✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
289,770✔
739
  int32_t num = taosArrayGetSize(pList);
289,770✔
740
  for (int32_t i = 0; i < num; ++i) {
842,418✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
709,430✔
742
    if (pScan->readFromCache) {
709,430✔
743
      return pScan->scanPlan;
156,782✔
744
    }
745
  }
746

747
  return NULL;
132,988✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
456,415✔
775
}
776

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

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

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

805
_exit:
137,787✔
806

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

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

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

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

875
  SStreamTaskAddr addr;
140,459✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
140,907✔
877
  if (triggerReaderNum > 0) {
140,907✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
139,966✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
139,966✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
313,751✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
172,844✔
884
    addr.taskId = pStatus->id.taskId;
172,844✔
885
    addr.nodeId = pStatus->id.nodeId;
172,844✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
172,844✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
345,688✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
172,844✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
140,907✔
892
  pMsg->streamName = pInfo->streamName;
140,907✔
893

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

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

901
_exit:
137,787✔
902

903
  if (code) {
137,787✔
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));
137,787✔
907
  }
908
  
909
  return TSDB_CODE_SUCCESS;
137,787✔
910
}
911

912

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

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

946
  pMsg->colCids = pInfo->pCreate->colCids;
432,768✔
947
  pMsg->tagCids = pInfo->pCreate->tagCids;
432,768✔
948
_exit:
432,768✔
949

950
  if (code) {
432,768✔
951
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
952
  }
953
  
954
  return code;
432,768✔
955
}
956

957

958
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
341,319✔
959
  int32_t code = TSDB_CODE_SUCCESS;
341,319✔
960
  int32_t lino = 0;
341,319✔
961
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
341,319✔
962
  SListNode* pNode = pList ? listHead(pList) : NULL;
341,319✔
963
  
964
  for (int32_t i = 0; i < taskNum; ++i) {
796,774✔
965
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
455,455✔
966
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
455,455✔
967
    if (pNode) {
455,455✔
968
      pNode = TD_DLIST_NODE_NEXT(pNode);
223,106✔
969
    }
970
  }
971
  
972
_exit:
341,319✔
973

974
  if (code) {
341,319✔
975
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
976
  }
977
  
978
  return code;
341,319✔
979
}
980

981

982
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
554,823✔
983
  int32_t code = TSDB_CODE_SUCCESS;
554,823✔
984
  int32_t lino = 0;
554,823✔
985
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
554,823✔
986
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
554,823✔
987
  
988
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,128,498✔
989
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
573,675✔
990
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
573,675✔
991
  }
992
  
993
_exit:
554,823✔
994

995
  if (code) {
554,823✔
996
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
997
  }
998

999
  return code;
554,823✔
1000
}
1001

1002
int64_t msmAssignTaskId(void) {
1,028,575✔
1003
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,028,575✔
1004
}
1005

1006
int64_t msmAssignTaskSeriousId(void) {
1,028,575✔
1007
  return taosGetTimestampNs();
1,028,575✔
1008
}
1009

1010

1011
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
717,585✔
1012
  int32_t code = TSDB_CODE_SUCCESS;
717,585✔
1013
  int32_t lino = 0;
717,585✔
1014
  bool     noExists = false;
717,585✔
1015
  SStmSnodeStatus* pStatus = NULL;
717,585✔
1016

1017
  while (true) {
1018
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
725,223✔
1019
    if (NULL == pStatus) {
725,223✔
1020
      if (noExists) {
7,638✔
1021
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1022
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1023
      }
1024

1025
      noExists = true;
7,638✔
1026
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
7,638✔
1027
      
1028
      continue;
7,638✔
1029
    }
1030

1031
    *alive = (pStatus->runnerThreadNum >= 0);
717,585✔
1032
    break;
717,585✔
1033
  }
1034

1035
_exit:
717,585✔
1036

1037
  if (code) {
717,585✔
1038
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1039
  }
1040

1041
  return code;
717,585✔
1042
}
1043

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

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

UNCOV
1072
      snodeId = pSnode->replicaId;
×
UNCOV
1073
      mndReleaseSnode(pMnode, pSnode);
×
1074
      
UNCOV
1075
      continue;
×
1076
    }
1077

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

1082
_exit:
×
1083

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

1088
  return 0;
×
1089
}
1090

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

1105
  int32_t snodeTarget = taosRand() % snodeNum;
418,501✔
1106

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

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

1132
    snodeId = pObj->id;
438,336✔
1133
    if (snodeIdx == snodeTarget) {
438,336✔
1134
      sdbRelease(pMnode->pSdb, pObj);
418,501✔
1135
      sdbCancelFetch(pMnode->pSdb, pIter);
418,501✔
1136
      pObj = NULL;
418,501✔
1137
      goto _exit;
418,501✔
1138
    }
1139

1140
    sdbRelease(pMnode->pSdb, pObj);
19,835✔
1141
    snodeIdx++;
19,835✔
1142
  }
1143

1144
_exit:
420,891✔
1145

1146
  if (code) {
420,891✔
1147
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1148
  }
1149

1150
  if (0 == snodeId) {
420,891✔
1151
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,390✔
1152
  }
1153

1154
  return snodeId;
420,891✔
1155
}
1156

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

1166
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
554,823✔
1167

1168
_exit:
554,823✔
1169

1170
  if (0 == snodeId) {
554,823✔
1171
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1172
  }
1173

1174
  return snodeId;
554,823✔
1175
}
1176

1177

1178
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
140,907✔
1179
  int32_t code = TSDB_CODE_SUCCESS;
140,907✔
1180
  int32_t lino = 0;
140,907✔
1181
  int64_t streamId = pStream->pCreate->streamId;
140,907✔
1182

1183
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
140,907✔
1184
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
140,907✔
1185

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

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

1207
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
140,907✔
1208
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
140,907✔
1209

1210
_exit:
140,907✔
1211

1212
  if (code) {
140,907✔
1213
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1214
  }
1215

1216
  return code;
140,907✔
1217
}
1218

1219
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
230,105✔
1220
  int32_t code = TSDB_CODE_SUCCESS;
230,105✔
1221
  int32_t lino = 0;
230,105✔
1222

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

1245
_exit:
230,105✔
1246

1247
  if (code) {
230,105✔
1248
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1249
  }
1250

1251
  return code;
230,105✔
1252
}
1253

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

1283
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
66,582✔
1284
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
66,582✔
1285
      
1286
      void *pIter = NULL;
66,582✔
1287
      while (1) {
352,562✔
1288
        SStmTaskDeploy info = {0};
419,144✔
1289
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
419,144✔
1290
        if (pIter == NULL) {
419,144✔
1291
          break;
66,582✔
1292
        }
1293
      
1294
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
352,562✔
1295
          pState = taosArrayReserve(pInfo->trigReaders, 1);
99,460✔
1296

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

1306
        sdbRelease(pSdb, pVgroup);
352,562✔
1307
      }
1308
      break;
66,582✔
1309
    }
1310
    default:
941✔
1311
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
941✔
1312
      break;
941✔
1313
  }
1314

1315
_exit:
140,907✔
1316

1317
  mndReleaseDb(pCtx->pMnode, pDb);
140,907✔
1318

1319
  if (code) {
140,907✔
1320
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1321
  }
1322

1323
  return code;
140,907✔
1324
}
1325

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

1349
  key[1] = pSubplan->id.subplanId;
223,661✔
1350

1351
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
223,661✔
1352
  if (NULL == ppRes) {
223,661✔
1353
    SArray* pRes = taosArrayInit(1, sizeof(addr));
223,661✔
1354
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
223,661✔
1355
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
447,322✔
1356
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
223,661✔
1357
  } else {
1358
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1359
  }
1360

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

1367
  nodesDestroyNode((SNode*)pSubplan);
223,661✔
1368

1369
  if (code) {
223,661✔
1370
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1371
  }
1372

1373
  return code;
223,661✔
1374
}
1375

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

1384
  SStmTaskSrcAddr addr;
68,995✔
1385
  addr.isFromCache = true;
68,995✔
1386
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
68,995✔
1387
  addr.taskId = pCtx->triggerTaskId;
68,995✔
1388
  addr.vgId = pCtx->triggerNodeId;
68,995✔
1389
  addr.groupId = pSubplan->id.groupId;
68,995✔
1390

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

1406
  nodesDestroyNode((SNode*)pSubplan);
68,995✔
1407
  
1408
  if (code) {
68,995✔
1409
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1410
  }
1411

1412
  return code;
68,995✔
1413
}
1414

1415

1416
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
225,350✔
1417
  int32_t code = TSDB_CODE_SUCCESS;
225,350✔
1418
  int32_t lino = 0;
225,350✔
1419

1420
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
225,350✔
1421

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

1444
_exit:
225,350✔
1445

1446
  if (code) {
225,350✔
1447
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1448
  }
1449

1450
  return code;
225,350✔
1451
}
1452

1453

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

1463
  
1464
  for (int32_t i = 0; i < calcTasksNum; ++i) {
433,008✔
1465
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
292,101✔
1466
    if (pScan->readFromCache) {
292,101✔
1467
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
68,995✔
1468
      continue;
68,995✔
1469
    }
1470
    
1471
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
223,106✔
1472
    for (int32_t m = 0; m < vgNum; ++m) {
446,212✔
1473
      pState = tdListReserve(pInfo->calcReaders);
223,106✔
1474
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
223,106✔
1475

1476
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
223,106✔
1477
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
223,106✔
1478
    }
1479
  }
1480

1481
_exit:
140,907✔
1482

1483
  if (code) {
140,907✔
1484
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1485
  }
1486

1487
  return code;
140,907✔
1488
}
1489

1490

1491

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

1519
_exit:
555✔
1520

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

1525
  return code;
555✔
1526
}
1527

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

1536
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
140,907✔
1537
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
140,907✔
1538
  
1539
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
140,907✔
1540
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
140,907✔
1541
  
1542
_exit:
140,907✔
1543

1544
  if (code) {
140,907✔
1545
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1546
  }
1547
  
1548
  return code;
140,907✔
1549
}
1550

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

1562
  source.addr.epSet = pSrc->epset;
895,710✔
1563
  source.addr.nodeId = pSrc->vgId;
895,710✔
1564

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

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

1584
  mstsError("subplanId %d not found in runner list", subplanId);
×
1585

1586
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1587
}
1588

1589
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
432,768✔
1590
  int32_t code = TSDB_CODE_SUCCESS;
432,768✔
1591
  int32_t lino = 0;
432,768✔
1592
  int64_t key[2] = {streamId, -1};
432,768✔
1593
  SNode* pNode = NULL;
432,768✔
1594
  SStreamTask* pTask = &pDeploy->task;
432,768✔
1595
  FOREACH(pNode, pPlan->pChildren) {
1,328,478✔
1596
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
895,710✔
1597
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
18,852✔
1598
      continue;
18,852✔
1599
    }
1600
    
1601
    SValueNode* pVal = (SValueNode*)pNode;
876,858✔
1602
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
876,858✔
1603
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1604
      continue;
×
1605
    }
1606

1607
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
876,858✔
1608

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

1615
    int32_t childrenNum = taosArrayGetSize(*ppRes);
876,858✔
1616
    for (int32_t i = 0; i < childrenNum; ++i) {
1,753,716✔
1617
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
876,858✔
1618
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
876,858✔
1619
    }
1620
  }
1621

1622
_exit:
432,768✔
1623

1624
  if (code) {
432,768✔
1625
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1626
  }
1627

1628
  return code;
432,768✔
1629
}
1630

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

1639
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
432,768✔
1640

1641
  SNode* pTmp = NULL;
432,768✔
1642
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,328,478✔
1643
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
895,710✔
1644
      ERASE_NODE(pPlan->pChildren);
876,858✔
1645
      continue;
876,858✔
1646
    }
1647
    WHERE_NEXT;
18,852✔
1648
  }
1649
  nodesClearList(pPlan->pChildren);
432,768✔
1650
  pPlan->pChildren = NULL;
432,768✔
1651

1652
  if (NULL == pPlan->pParents) {
432,768✔
1653
    goto _exit;
413,916✔
1654
  }
1655

1656
  SNode* pNode = NULL;
18,852✔
1657
  int64_t parentTaskId = 0;
18,852✔
1658
  SStmTaskSrcAddr addr = {0};
18,852✔
1659
  addr.taskId = pDeploy->task.taskId;
18,852✔
1660
  addr.vgId = pDeploy->task.nodeId;
18,852✔
1661
  addr.groupId = pPlan->id.groupId;
18,852✔
1662
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
18,852✔
1663
  FOREACH(pNode, pPlan->pParents) {
37,704✔
1664
    SSubplan* pSubplan = (SSubplan*)pNode;
18,852✔
1665
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
18,852✔
1666
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
18,852✔
1667
  }
1668
  
1669
_exit:
431,424✔
1670

1671
  if (code) {
432,768✔
1672
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1673
  }
1674

1675
  return code;
432,768✔
1676
}
1677

1678
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
413,916✔
1679
  int32_t code = TSDB_CODE_SUCCESS;
413,916✔
1680
  int32_t lino = 0;
413,916✔
1681
  int64_t streamId = pStream->pCreate->streamId;
413,916✔
1682
  int32_t runnerNum = taosArrayGetSize(pRunners);
413,916✔
1683
  
1684
  for (int32_t i = 0; i < runnerNum; ++i) {
846,684✔
1685
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
432,768✔
1686
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
432,768✔
1687
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
432,768✔
1688

1689
    SStreamTask* pTask = &pDeploy->task;
432,768✔
1690
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
432,768✔
1691
  }
1692

1693
_exit:
413,916✔
1694

1695
  if (code) {
413,916✔
1696
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1697
  }
1698

1699
  return code;
413,916✔
1700
}
1701

1702
int32_t msmBuildRunnerTasksImpl(SStmGrpCtx* pCtx, SQueryPlan* pDag, SStmStatus* pInfo, SStreamObj* pStream) {
137,787✔
1703
  int32_t code = 0;
137,787✔
1704
  int32_t lino = 0;
137,787✔
1705
  int64_t streamId = pStream->pCreate->streamId;
137,787✔
1706
  SArray* deployTaskList = NULL;
137,787✔
1707
  SArray* deployList = NULL;
137,787✔
1708
  int32_t deployNodeId = 0;
137,787✔
1709
  SStmTaskStatus* pState = NULL;
137,787✔
1710
  int32_t taskIdx = 0;
137,787✔
1711
  SNodeListNode *plans = NULL;
137,787✔
1712
  int32_t        taskNum = 0;
137,787✔
1713
  int32_t        totalTaskNum = 0;
137,787✔
1714

1715
  if (pDag->numOfSubplans <= 0) {
137,787✔
1716
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1717
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1718
  }
1719

1720
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
137,787✔
1721
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1722
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1723
  }
1724

1725
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
137,787✔
1726
  if (levelNum <= 0) {
137,787✔
1727
    mstsError("invalid level num:%d", levelNum);
×
1728
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1729
  }
1730

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

1745
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
137,787✔
1746
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
137,787✔
1747
  
1748
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
551,148✔
1749
    totalTaskNum = 0;
413,361✔
1750

1751
    deployList = pInfo->runners[deployId];
413,361✔
1752
    deployNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == deployId) ? true : false);
413,361✔
1753
    if (!GOT_SNODE(deployNodeId)) {
413,361✔
1754
      TAOS_CHECK_EXIT(terrno);
×
1755
    }
1756

1757
    taskIdx = 0;
413,361✔
1758
    
1759
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
836,148✔
1760
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
422,787✔
1761
      if (NULL == plans) {
422,787✔
1762
        mstsError("empty level plan, level:%d", i);
×
1763
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1764
      }
1765

1766
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
422,787✔
1767
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1768
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1769
      }
1770

1771
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
422,787✔
1772
      if (taskNum <= 0) {
422,787✔
1773
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1774
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1775
      }
1776

1777
      totalTaskNum += taskNum;
422,787✔
1778
      if (totalTaskNum > pDag->numOfSubplans) {
422,787✔
1779
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1780
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1781
      }
1782

1783
      for (int32_t n = 0; n < taskNum; ++n) {
855,000✔
1784
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
432,213✔
1785
        pState = taosArrayReserve(deployList, 1);
432,213✔
1786

1787
        pState->id.taskId = msmAssignTaskId();
432,213✔
1788
        pState->id.deployId = deployId;
432,213✔
1789
        pState->id.seriousId = msmAssignTaskSeriousId();
432,213✔
1790
        pState->id.nodeId = deployNodeId;
432,213✔
1791
        pState->id.taskIdx = MND_SET_RUNNER_TASKIDX(i, n);
432,213✔
1792
        pState->type = STREAM_RUNNER_TASK;
432,213✔
1793
        pState->flags = (0 == i) ? STREAM_FLAG_TOP_RUNNER : 0;
432,213✔
1794
        pState->status = STREAM_STATUS_UNDEPLOYED;
432,213✔
1795
        pState->lastUpTs = pCtx->currTs;
432,213✔
1796
        pState->pStream = pInfo;
432,213✔
1797

1798
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
432,213✔
1799
        pDeploy->task.type = pState->type;
432,213✔
1800
        pDeploy->task.streamId = streamId;
432,213✔
1801
        pDeploy->task.taskId = pState->id.taskId;
432,213✔
1802
        pDeploy->task.flags = pState->flags;
432,213✔
1803
        pDeploy->task.seriousId = pState->id.seriousId;
432,213✔
1804
        pDeploy->task.deployId = pState->id.deployId;
432,213✔
1805
        pDeploy->task.nodeId = pState->id.nodeId;
432,213✔
1806
        pDeploy->task.taskIdx = pState->id.taskIdx;
432,213✔
1807
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
432,213✔
1808

1809
        SStreamTask* pTask = &pDeploy->task;
432,213✔
1810
        msttDebug("runner task deploy built, subplan level:%d, taskIdx:%d, groupId:%d, subplanId:%d",
432,213✔
1811
            i, pTask->taskIdx, plan->id.groupId, plan->id.subplanId);
1812
      }
1813

1814
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
422,787✔
1815
    }
1816

1817
    if (totalTaskNum != pDag->numOfSubplans) {
413,361✔
1818
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1819
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1820
    }
1821

1822
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
413,361✔
1823

1824
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
413,361✔
1825

1826
    nodesDestroyNode((SNode *)pDag);
413,361✔
1827
    pDag = NULL;
413,361✔
1828
    
1829
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
413,361✔
1830

1831
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
413,361✔
1832
  }
1833

1834
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
551,148✔
1835
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
413,361✔
1836
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
413,361✔
1837
  }
1838
  
1839
  pInfo->runnerNum = totalTaskNum;
137,787✔
1840
  
1841
_exit:
137,787✔
1842

1843
  if (code) {
137,787✔
1844
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1845
  }
1846

1847
  taosArrayDestroy(deployTaskList);
137,787✔
1848
  nodesDestroyNode((SNode *)pDag);
137,787✔
1849

1850
  return code;
137,787✔
1851
}
1852

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

1870
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
1,110✔
1871
    deployId = pAction->deployId[r];
555✔
1872

1873
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
555✔
1874

1875
    pStartRunner = pRunner;
555✔
1876
    totalTaskNum = 0;
555✔
1877

1878
    newNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, (0 == r) ? true : false);
555✔
1879
    if (!GOT_SNODE(newNodeId)) {
555✔
1880
      TAOS_CHECK_EXIT(terrno);
×
1881
    }
1882

1883
    taskIdx = 0;
555✔
1884
    
1885
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,110✔
1886
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
555✔
1887
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
555✔
1888
      totalTaskNum += taskNum;
555✔
1889

1890
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
555✔
1891
      
1892
      for (int32_t n = 0; n < taskNum; ++n) {
1,110✔
1893
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
555✔
1894

1895
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
555✔
1896
        if (pRunner->id.taskIdx != newTaskIdx) {
555✔
1897
          mstsError("runner TASK:%" PRId64 " taskIdx %d mismatch with newTaskIdx:%d", pRunner->id.taskId, pRunner->id.taskIdx, newTaskIdx);
×
1898
          TAOS_CHECK_EXIT(TSDB_CODE_STREAM_INTERNAL_ERROR);
×
1899
        }
1900

1901
        pRunner->id.nodeId = newNodeId;
555✔
1902

1903
        SStmTaskDeploy* pDeploy = taosArrayGet(deployTaskList, taskIdx++);
555✔
1904
        pDeploy->task.type = pRunner->type;
555✔
1905
        pDeploy->task.streamId = streamId;
555✔
1906
        pDeploy->task.taskId = pRunner->id.taskId;
555✔
1907
        pDeploy->task.flags = pRunner->flags;
555✔
1908
        pDeploy->task.seriousId = pRunner->id.seriousId;
555✔
1909
        pDeploy->task.nodeId = pRunner->id.nodeId;
555✔
1910
        pDeploy->task.taskIdx = pRunner->id.taskIdx;
555✔
1911
        TAOS_CHECK_EXIT(msmBuildRunnerDeployInfo(pDeploy, plan, pStream, pInfo, 0 == i));
555✔
1912

1913
        pRunner++;
555✔
1914
      }
1915

1916
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
555✔
1917
    }
1918

1919
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
555✔
1920

1921
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
555✔
1922

1923
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
555✔
1924

1925
    nodesDestroyNode((SNode *)pDag);
555✔
1926
    pDag = NULL;
555✔
1927

1928
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
555✔
1929
  }
1930

1931
_exit:
555✔
1932

1933
  if (code) {
555✔
1934
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1935
  }
1936

1937
  nodesDestroyNode((SNode *)pDag);
555✔
1938
  taosArrayDestroy(deployTaskList);
555✔
1939

1940
  return code;
555✔
1941
}
1942

1943

1944
int32_t msmSetStreamRunnerExecReplica(int64_t streamId, SStmStatus* pInfo) {
132,703✔
1945
  int32_t code = TSDB_CODE_SUCCESS;
132,703✔
1946
  int32_t lino = 0;
132,703✔
1947
  //STREAMTODO 
1948
  
1949
  pInfo->runnerDeploys = MND_STREAM_RUNNER_DEPLOY_NUM;
132,703✔
1950
  pInfo->runnerReplica = MND_STREAM_RUNNER_REPLICA_NUM;
132,703✔
1951

1952
_exit:
132,703✔
1953

1954
  if (code) {
132,703✔
1955
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1956
  }
1957

1958
  return code;
132,703✔
1959
}
1960

1961

1962
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
140,907✔
1963
  if (NULL == pStream->pCreate->calcPlan) {
140,907✔
1964
    return TSDB_CODE_SUCCESS;
3,120✔
1965
  }
1966
  
1967
  int32_t code = TSDB_CODE_SUCCESS;
137,787✔
1968
  int32_t lino = 0;
137,787✔
1969
  int64_t streamId = pStream->pCreate->streamId;
137,787✔
1970
  SQueryPlan* pPlan = NULL;
137,787✔
1971

1972
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
137,787✔
1973

1974
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
551,148✔
1975
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
413,361✔
1976
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
413,361✔
1977
  }
1978

1979
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
137,787✔
1980
  pPlan = NULL;
137,787✔
1981
  
1982
  TAOS_CHECK_EXIT(code);
137,787✔
1983

1984
  taosHashClear(mStreamMgmt.toUpdateScanMap);
137,787✔
1985
  mStreamMgmt.toUpdateScanNum = 0;
137,787✔
1986

1987
_exit:
137,787✔
1988

1989
  nodesDestroyNode((SNode *)pPlan);
137,787✔
1990

1991
  if (code) {
137,787✔
1992
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1993
  }
1994

1995
  return code;
137,787✔
1996
}
1997

1998

1999
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
140,907✔
2000
  int32_t code = TSDB_CODE_SUCCESS;
140,907✔
2001
  int32_t lino = 0;
140,907✔
2002
  int64_t streamId = pStream->pCreate->streamId;
140,907✔
2003

2004
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
140,907✔
2005

2006
  pCtx->triggerTaskId = msmAssignTaskId();
140,907✔
2007
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
140,907✔
2008
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
140,907✔
2009
    TAOS_CHECK_EXIT(terrno);
×
2010
  }
2011

2012
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
140,907✔
2013
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
140,907✔
2014
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
140,907✔
2015
  
2016
_exit:
140,907✔
2017

2018
  if (code) {
140,907✔
2019
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2020
  }
2021

2022
  return code;
140,907✔
2023
}
2024

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

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

2064
_exit:
×
2065

2066
  if (code) {
×
2067
    mndReleaseDb(pCtx->pMnode, pDb);
×
2068
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2069
  }
2070

2071
  return code;
×
2072
}
2073

2074

2075
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
135,823✔
2076
  int32_t code = TSDB_CODE_SUCCESS;
135,823✔
2077
  int32_t lino = 0;
135,823✔
2078
  int64_t streamId = pStream->pCreate->streamId;
135,823✔
2079

2080
  pStatus->lastActionTs = INT64_MIN;
135,823✔
2081

2082
  if (NULL == pStatus->streamName) {
135,823✔
2083
    pStatus->streamName = taosStrdup(pStream->name);
135,823✔
2084
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
135,823✔
2085
  }
2086

2087
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
135,823✔
2088
  
2089
  if (pStream->pCreate->numOfCalcSubplan > 0) {
135,823✔
2090
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
132,703✔
2091
    
2092
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
132,703✔
2093
  }
2094

2095
  if (initList) {
135,823✔
2096
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2097

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

2105
    if (pStatus->runnerNum > 0) {
×
2106
      for (int32_t i = 0; i < pStatus->runnerDeploys; ++i) {
×
2107
        pStatus->runners[i] = taosArrayInit(pStatus->runnerNum, sizeof(SStmTaskStatus));
×
2108
        TSDB_CHECK_NULL(pStatus->runners[i], code, lino, _exit, terrno);
×
2109
      }
2110
    }
2111
  }
2112
  
2113
_exit:
135,823✔
2114

2115
  if (code) {
135,823✔
2116
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2117
  }
2118

2119
  return code;
135,823✔
2120
}
2121

2122
static int32_t msmDeployStreamTasks(SStmGrpCtx* pCtx, SStreamObj* pStream, SStmStatus* pStatus) {
140,907✔
2123
  int32_t code = TSDB_CODE_SUCCESS;
140,907✔
2124
  int32_t lino = 0;
140,907✔
2125
  int64_t streamId = pStream->pCreate->streamId;
140,907✔
2126
  SStmStatus info = {0};
140,907✔
2127

2128
  if (NULL == pStatus) {
140,907✔
2129
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
135,823✔
2130

2131
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
135,823✔
2132

2133
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
135,823✔
2134
  }
2135
  
2136
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
140,907✔
2137

2138
  mstLogSStmStatus("stream deployed", streamId, pStatus);
140,907✔
2139

2140
_exit:
140,907✔
2141

2142
  if (code) {
140,907✔
2143
    if (NULL != pStatus) {
×
2144
      msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2145
      mstsError("stream build error:%s, will try to stop current stream", tstrerror(code));
×
2146
    }
2147
    
2148
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2149
  }
2150

2151
  return code;
140,907✔
2152
}
2153

2154

2155
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
6,896✔
2156
  int32_t code = TSDB_CODE_SUCCESS;
6,896✔
2157
  void* pIter = NULL;
6,896✔
2158

2159
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
10,567✔
2160
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
3,671✔
2161
    (void)mstWaitLock(&pVg->lock, true);
3,671✔
2162

2163
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
3,671✔
2164
    if (atomic_load_32(&pVg->deployed) == taskNum) {
3,671✔
2165
      taosRUnLockLatch(&pVg->lock);
×
2166
      continue;
×
2167
    }
2168

2169
    for (int32_t i = 0; i < taskNum; ++i) {
18,157✔
2170
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
14,486✔
2171
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
14,486✔
2172
        continue;
13,526✔
2173
      }
2174

2175
      mstDestroySStmTaskToDeployExt(pExt);
960✔
2176
      pExt->deployed = true;
960✔
2177
    }
2178
    
2179
    taosRUnLockLatch(&pVg->lock);
3,671✔
2180
  }
2181

2182
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
14,475✔
2183
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
7,579✔
2184
    (void)mstWaitLock(&pSnode->lock, true);
7,579✔
2185

2186
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
7,579✔
2187
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
7,579✔
2188
      for (int32_t i = 0; i < taskNum; ++i) {
12,333✔
2189
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
7,444✔
2190
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
7,444✔
2191
          continue;
6,964✔
2192
        }
2193
        
2194
        mstDestroySStmTaskToDeployExt(pExt);
480✔
2195
        pExt->deployed = true;
480✔
2196
      }
2197
    }
2198

2199
    taskNum = taosArrayGetSize(pSnode->runnerList);
7,579✔
2200
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
7,579✔
2201
      for (int32_t i = 0; i < taskNum; ++i) {
31,321✔
2202
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
23,742✔
2203
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
23,742✔
2204
          continue;
22,302✔
2205
        }
2206
        
2207
        mstDestroySStmTaskToDeployExt(pExt);
1,440✔
2208
        pExt->deployed = true;
1,440✔
2209
      }
2210
    }
2211

2212
    taosRUnLockLatch(&pSnode->lock);
7,579✔
2213
  }
2214

2215
  
2216
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
26,602✔
2217
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
19,706✔
2218
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
19,706✔
2219
    if (TSDB_CODE_SUCCESS == code) {
19,706✔
2220
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
9,841✔
2221
    }
2222
  }
2223

2224
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
28,053✔
2225
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
21,157✔
2226
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
21,157✔
2227
    if (TSDB_CODE_SUCCESS == code) {
21,157✔
2228
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
9,572✔
2229
    }
2230
  }
2231

2232
  size_t keyLen = 0;
6,896✔
2233
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
426,582✔
2234
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
419,686✔
2235
    if (*pStreamId == streamId) {
419,686✔
2236
      int64_t taskId = *(pStreamId + 1);
42,689✔
2237
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
42,689✔
2238
      if (code) {
42,689✔
2239
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2240
      } else {
2241
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
42,689✔
2242
      }
2243
    }
2244
  }
2245

2246
  if (fromStreamMap) {
6,896✔
2247
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
1,812✔
2248
    if (code) {
1,812✔
2249
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2250
    } else {
2251
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,812✔
2252
    }
2253
  }
2254
  
2255
  return code;
6,896✔
2256
}
2257

2258
static void msmResetStreamForRedeploy(int64_t streamId, SStmStatus* pStatus) {
5,084✔
2259
  mstsInfo("try to reset stream for redeploy, stopped:%d, current deployTimes:%" PRId64, atomic_load_8(&pStatus->stopped), pStatus->deployTimes);
5,084✔
2260
  
2261
  (void)msmSTRemoveStream(streamId, false);  
5,084✔
2262

2263
  mstResetSStmStatus(pStatus);
5,084✔
2264

2265
  pStatus->deployTimes++;
5,084✔
2266
}
5,084✔
2267

2268
static int32_t msmLaunchStreamDeployAction(SStmGrpCtx* pCtx, SStmStreamAction* pAction) {
141,773✔
2269
  int32_t code = TSDB_CODE_SUCCESS;
141,773✔
2270
  int32_t lino = 0;
141,773✔
2271
  int64_t streamId = pAction->streamId;
141,773✔
2272
  char* streamName = pAction->streamName;
141,773✔
2273
  SStreamObj* pStream = NULL;
141,773✔
2274
  int8_t stopped = 0;
141,773✔
2275

2276
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
141,773✔
2277
  if (pStatus) {
141,773✔
2278
    stopped = atomic_load_8(&pStatus->stopped);
5,084✔
2279
    if (0 == stopped) {
5,084✔
2280
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
480✔
2281
      msmResetStreamForRedeploy(streamId, pStatus);
480✔
2282
    } else {
2283
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
4,604✔
2284
        mstsWarn("stream %s already stopped by user, stopped:%d, ignore deploy it", pAction->streamName, stopped);
×
2285
        return code;
×
2286
      }
2287
      
2288
      if (stopped == atomic_val_compare_exchange_8(&pStatus->stopped, stopped, 0)) {
4,604✔
2289
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
4,604✔
2290
        msmResetStreamForRedeploy(streamId, pStatus);
4,604✔
2291
      }
2292
    }
2293
  }
2294

2295
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
141,773✔
2296
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
141,773✔
2297
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
866✔
2298
    return TSDB_CODE_SUCCESS;
866✔
2299
  }
2300

2301
  TAOS_CHECK_EXIT(code);
140,907✔
2302

2303
  if (pStatus && pStream->pCreate->streamId != streamId) {
140,907✔
2304
    mstsWarn("stream %s already dropped by user, ignore deploy it", pAction->streamName);
×
2305
    atomic_store_8(&pStatus->stopped, 2);
×
2306
    mstsInfo("set stream %s stopped by user since streamId mismatch", streamName);
×
2307
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_EXIST);
×
2308
  }
2309

2310
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
140,907✔
2311
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
140,907✔
2312
  if (userStopped || userDropped) {
140,907✔
2313
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
×
2314
    goto _exit;
×
2315
  }
2316
  
2317
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
140,907✔
2318

2319
_exit:
140,907✔
2320

2321
  mndReleaseStream(pCtx->pMnode, pStream);
140,907✔
2322

2323
  if (code) {
140,907✔
2324
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2325
  }
2326

2327
  return code;
140,907✔
2328
}
2329

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

2362
_exit:
960✔
2363

2364
  if (code) {
960✔
2365
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2366
  }
2367

2368
  return code;
960✔
2369
}
2370

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

2402
_exit:
2403

2404
  if (code) {
2405
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
2406
  }
2407

2408
  return code;
2409
}
2410
*/
2411

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

2461
_exit:
555✔
2462

2463
  if (code) {
555✔
2464
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2465
  }
2466

2467
  return code;
555✔
2468
}
2469

2470

2471
static int32_t msmLaunchTaskDeployAction(SStmGrpCtx* pCtx, SStmTaskAction* pAction) {
1,515✔
2472
  int32_t code = TSDB_CODE_SUCCESS;
1,515✔
2473
  int32_t lino = 0;
1,515✔
2474
  int64_t streamId = pAction->streamId;
1,515✔
2475
  int64_t taskId = pAction->id.taskId;
1,515✔
2476
  SStreamObj* pStream = NULL;
1,515✔
2477

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

2480
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,515✔
2481
  if (NULL == pStatus) {
1,515✔
2482
    mstsWarn("stream not in streamMap, remain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
2483
    return TSDB_CODE_SUCCESS;
×
2484
  }
2485

2486
  int8_t stopped = atomic_load_8(&pStatus->stopped);
1,515✔
2487
  if (stopped) {
1,515✔
2488
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
2489
    return TSDB_CODE_SUCCESS;
×
2490
  }
2491

2492
  code = mndAcquireStream(pCtx->pMnode, pStatus->streamName, &pStream);
1,515✔
2493
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,515✔
2494
    mstsWarn("stream %s no longer exists, ignore task deploy", pStatus->streamName);
×
2495
    return TSDB_CODE_SUCCESS;
×
2496
  }
2497

2498
  TAOS_CHECK_EXIT(code);
1,515✔
2499

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

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

2530
_exit:
1,515✔
2531

2532
  if (pStream) {
1,515✔
2533
    mndReleaseStream(pCtx->pMnode, pStream);
1,515✔
2534
  }
2535

2536
  if (code) {
1,515✔
2537
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2538
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2539
  }
2540

2541
  return code;
1,515✔
2542
}
2543

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

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

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

2589
  return TSDB_CODE_SUCCESS;
×
2590
}
2591

2592
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
1,812✔
2593
  int32_t code = TSDB_CODE_SUCCESS;
1,812✔
2594
  int32_t lino = 0;
1,812✔
2595

2596
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,812✔
2597

2598
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
1,812✔
2599

2600
_exit:
1,812✔
2601

2602
  if (code) {
1,812✔
2603
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2604
  } else {
2605
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,812✔
2606
  }
2607

2608
  return code;
1,812✔
2609
}
2610

2611
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
10,842✔
2612
  int32_t code = TSDB_CODE_SUCCESS;
10,842✔
2613
  int32_t lino = 0;
10,842✔
2614

2615
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
10,842✔
2616
  if (0 == active || MND_STM_STATE_NORMAL != state) {
10,842✔
2617
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
302✔
2618
    return;
302✔
2619
  }
2620

2621
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
10,540✔
2622
  if (NULL == pStream) {
10,540✔
2623
    mstsInfo("stream %s already not in streamMap", streamName);
662✔
2624
    goto _exit;
662✔
2625
  }
2626

2627
  atomic_store_8(&pStream->stopped, 2);
9,878✔
2628

2629
  mstsInfo("set stream %s stopped by user", streamName);
9,878✔
2630

2631
_exit:
×
2632

2633
  taosHashRelease(mStreamMgmt.streamMap, pStream);
10,540✔
2634

2635
  if (code) {
10,540✔
2636
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2637
  }
2638

2639
  return;
10,540✔
2640
}
2641

2642
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,196✔
2643
  int32_t code = TSDB_CODE_SUCCESS;
3,196✔
2644
  int32_t lino = 0;
3,196✔
2645

2646
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,196✔
2647
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,196✔
2648
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
×
2649
    return TSDB_CODE_MND_STREAM_NOT_AVAILABLE;
×
2650
  }
2651

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

2659
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,196✔
2660

2661
_exit:
3,196✔
2662

2663
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,196✔
2664

2665
  if (code) {
3,196✔
2666
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2667
  }
2668

2669
  return code;
3,196✔
2670
}
2671

2672
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
28,273✔
2673
  int32_t code = TSDB_CODE_SUCCESS;
28,273✔
2674
  int32_t lino = 0;
28,273✔
2675
  SStmQNode* pQNode = NULL;
28,273✔
2676

2677
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
171,561✔
2678
    switch (pQNode->type) {
143,288✔
2679
      case STREAM_ACT_DEPLOY:
143,288✔
2680
        if (pQNode->streamAct) {
143,288✔
2681
          mstDebug("start to handle stream deploy action");
141,773✔
2682
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
141,773✔
2683
        } else {
2684
          mstDebug("start to handle task deploy action");
1,515✔
2685
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,515✔
2686
        }
2687
        break;
143,288✔
2688
      default:
×
2689
        break;
×
2690
    }
2691
  }
2692

2693
_exit:
28,273✔
2694

2695
  if (code) {
28,273✔
2696
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2697
  }
2698
}
28,273✔
2699

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

2711
    pStatus = (SStmStatus*)pIter;
×
2712

2713
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
2714
    atomic_store_8(&pStatus->stopped, 4);
×
2715

2716
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2717
  }
2718
}
×
2719

2720
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2721
  mstInfo("stream grant expired");
×
2722

2723
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
×
2724
    mstWarn("mnode stream is NOT active, ignore handling");
×
2725
    return errCode;
×
2726
  }
2727

2728
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2729

2730
  msmStopAllStreamsByGrant(errCode);
×
2731

2732
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2733
  
2734
  return errCode;
×
2735
}
2736

2737
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,026,975✔
2738
  int32_t code = TSDB_CODE_SUCCESS;
1,026,975✔
2739
  int32_t lino = 0;
1,026,975✔
2740
  int64_t streamId = pDeploy->task.streamId;
1,026,975✔
2741
  
2742
  switch (pDeploy->task.type) {
1,026,975✔
2743
    case STREAM_READER_TASK:
455,455✔
2744
      if (NULL == pStream->readerTasks) {
455,455✔
2745
        pStream->streamId = streamId;
192,737✔
2746
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
192,737✔
2747
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
192,737✔
2748
      }
2749
      
2750
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
910,910✔
2751
      break;
455,455✔
2752
    case STREAM_TRIGGER_TASK:
140,427✔
2753
      pStream->streamId = streamId;
140,427✔
2754
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
140,427✔
2755
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
140,427✔
2756
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
140,427✔
2757
      break;
140,427✔
2758
    case STREAM_RUNNER_TASK:
431,093✔
2759
      if (NULL == pStream->runnerTasks) {
431,093✔
2760
        pStream->streamId = streamId;
143,092✔
2761
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
143,092✔
2762
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
143,092✔
2763
      }      
2764
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
862,186✔
2765
      break;
431,093✔
2766
    default:
×
2767
      break;
×
2768
  }
2769

2770
_exit:
1,026,975✔
2771

2772
  if (code) {
1,026,975✔
2773
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2774
  }
2775

2776
  return code;
1,026,975✔
2777
}
2778

2779
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,026,975✔
2780
  int32_t code = TSDB_CODE_SUCCESS;
1,026,975✔
2781
  int32_t lino = 0;
1,026,975✔
2782
  int64_t streamId = pDeploy->task.streamId;
1,026,975✔
2783
  SStreamTask* pTask = &pDeploy->task;
1,026,975✔
2784
  SStmStreamDeploy streamDeploy = {0};
1,026,975✔
2785
  SStmStreamDeploy* pStream = NULL;
1,026,975✔
2786
   
2787
  while (true) {
2788
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,026,975✔
2789
    if (NULL == pStream) {
1,026,975✔
2790
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
201,011✔
2791
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
201,011✔
2792
      if (TSDB_CODE_SUCCESS == code) {
201,011✔
2793
        goto _exit;
201,011✔
2794
      }
2795

2796
      if (TSDB_CODE_DUP_KEY != code) {
×
2797
        goto _exit;
×
2798
      }    
2799

2800
      tFreeSStmStreamDeploy(&streamDeploy);
×
2801
      continue;
×
2802
    }
2803

2804
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
825,964✔
2805
    
2806
    break;
825,964✔
2807
  }
2808
  
2809
_exit:
1,026,975✔
2810

2811
  taosHashRelease(pHash, pStream);
1,026,975✔
2812

2813
  if (code) {
1,026,975✔
2814
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2815
  } else {
2816
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,026,975✔
2817
  }
2818

2819
  return code;
1,026,975✔
2820
}
2821

2822

2823
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
142,998✔
2824
  int32_t code = TSDB_CODE_SUCCESS;
142,998✔
2825
  int32_t lino = 0;
142,998✔
2826
  int32_t taskNum = taosArrayGetSize(pTasks);
142,998✔
2827

2828
  for (int32_t i = 0; i < taskNum; ++i) {
1,172,853✔
2829
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,029,855✔
2830
    if (pExt->deployed) {
1,029,855✔
2831
      continue;
2,880✔
2832
    }
2833

2834
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,026,975✔
2835
    pExt->deployed = true;
1,026,975✔
2836

2837
    (void)atomic_add_fetch_32(deployed, 1);
1,026,975✔
2838
  }
2839

2840
_exit:
142,998✔
2841

2842
  if (code) {
142,998✔
2843
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2844
  }
2845

2846
  return code;
142,998✔
2847
}
2848

2849
int32_t msmGrpAddDeployVgTasks(SStmGrpCtx* pCtx) {
41,556✔
2850
  int32_t code = TSDB_CODE_SUCCESS;
41,556✔
2851
  int32_t lino = 0;
41,556✔
2852
  int32_t vgNum = taosArrayGetSize(pCtx->pReq->pVgLeaders);
41,556✔
2853
  SStmVgTasksToDeploy* pVg = NULL;
41,556✔
2854
  //int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pCtx->pReq->streamGId);
2855

2856
  mstDebug("start to add stream vgroup tasks deploy");
41,556✔
2857
  
2858
  for (int32_t i = 0; i < vgNum; ++i) {
283,628✔
2859
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
242,072✔
2860

2861
    msmUpdateVgroupUpTs(pCtx, *vgId);
242,072✔
2862

2863
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
242,072✔
2864
    if (NULL == pVg) {
242,072✔
2865
      continue;
158,898✔
2866
    }
2867

2868
    if (taosRTryLockLatch(&pVg->lock)) {
83,174✔
2869
      continue;
×
2870
    }
2871
    
2872
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
83,174✔
2873
      taosRUnLockLatch(&pVg->lock);
×
2874
      continue;
×
2875
    }
2876
    
2877
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
83,174✔
2878
    taosRUnLockLatch(&pVg->lock);
83,174✔
2879
  }
2880

2881
_exit:
41,556✔
2882

2883
  if (code) {
41,556✔
2884
    if (pVg) {
×
2885
      taosRUnLockLatch(&pVg->lock);
×
2886
    }
2887

2888
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2889
  }
2890

2891
  return code;
41,556✔
2892
}
2893

2894
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
47,577✔
2895
  int32_t code = TSDB_CODE_SUCCESS;
47,577✔
2896
  int32_t lino = 0;
47,577✔
2897
  SStmSnodeTasksDeploy* pSnode = NULL;
47,577✔
2898
  SStreamHbMsg* pReq = pCtx->pReq;
47,577✔
2899

2900
  mstDebug("start to add stream snode tasks deploy");
47,577✔
2901
  
2902
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
47,577✔
2903
  if (NULL == pSnode) {
47,577✔
2904
    return TSDB_CODE_SUCCESS;
16,715✔
2905
  }
2906

2907
  (void)mstWaitLock(&pSnode->lock, false);
30,862✔
2908
  
2909
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
30,862✔
2910
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
28,962✔
2911
  }
2912

2913
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
30,862✔
2914
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
30,862✔
2915
  }
2916
  
2917
  taosWUnLockLatch(&pSnode->lock);
30,862✔
2918

2919
_exit:
30,862✔
2920

2921
  if (code) {
30,862✔
2922
    if (pSnode) {
×
2923
      taosWUnLockLatch(&pSnode->lock);
×
2924
    }
2925

2926
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2927
  }
2928

2929
  return code;
30,862✔
2930
}
2931

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

2943
_exit:
457,485✔
2944

2945
  if (code) {
457,485✔
2946
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2947
  }
2948

2949
  return code;
457,485✔
2950
}
2951

2952
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
43,076✔
2953
  int32_t code = TSDB_CODE_SUCCESS;
43,076✔
2954
  int32_t lino = 0;
43,076✔
2955
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
43,076✔
2956
  void* pIter = NULL;
43,076✔
2957

2958
  mstDebug("start to add group %d deploy streams, streamNum:%d", pCtx->pReq->streamGId, taosHashGetSize(pCtx->deployStm));
43,076✔
2959
  
2960
  pCtx->pRsp->deploy.streamList = taosArrayInit(streamNum, sizeof(SStmStreamDeploy));
43,076✔
2961
  TSDB_CHECK_NULL(pCtx->pRsp->deploy.streamList, code, lino, _exit, terrno);
43,076✔
2962

2963
  while (1) {
201,011✔
2964
    pIter = taosHashIterate(pCtx->deployStm, pIter);
244,087✔
2965
    if (pIter == NULL) {
244,087✔
2966
      break;
43,076✔
2967
    }
2968
    
2969
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
201,011✔
2970
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
402,022✔
2971

2972
    int64_t streamId = pDeploy->streamId;
201,011✔
2973
    mstsDebug("stream DEPLOY added to dnode %d hb rsp, readerTasks:%zu, triggerTask:%d, runnerTasks:%zu", 
201,011✔
2974
        pCtx->pReq->dnodeId, taosArrayGetSize(pDeploy->readerTasks), pDeploy->triggerTask ? 1 : 0, taosArrayGetSize(pDeploy->runnerTasks));
2975

2976
    mstClearSStmStreamDeploy(pDeploy);
201,011✔
2977
    
2978
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
201,011✔
2979
  }
2980
  
2981
_exit:
43,076✔
2982

2983
  if (pIter) {
43,076✔
2984
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2985
  }
2986

2987
  if (code) {
43,076✔
2988
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2989
  }
2990
  
2991
  return code;
43,076✔
2992
}
2993

2994
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
41,556✔
2995
  int32_t code = TSDB_CODE_SUCCESS;
41,556✔
2996
  int32_t lino = 0;
41,556✔
2997
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
41,556✔
2998
  SStmVgTasksToDeploy* pVg = NULL;
41,556✔
2999
  
3000
  for (int32_t i = 0; i < vgNum; ++i) {
283,628✔
3001
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
242,072✔
3002
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
242,072✔
3003
    if (NULL == pVg) {
242,072✔
3004
      continue;
158,898✔
3005
    }
3006

3007
    if (taosWTryLockLatch(&pVg->lock)) {
83,174✔
3008
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3009
      continue;
×
3010
    }
3011
    
3012
    if (atomic_load_32(&pVg->deployed) <= 0) {
83,174✔
3013
      taosWUnLockLatch(&pVg->lock);
×
3014
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3015
      continue;
×
3016
    }
3017

3018
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
83,174✔
3019
    if (atomic_load_32(&pVg->deployed) == taskNum) {
83,174✔
3020
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
83,014✔
3021
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
83,014✔
3022
      pVg->taskList = NULL;
83,014✔
3023
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
83,014✔
3024
      taosWUnLockLatch(&pVg->lock);
83,014✔
3025
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
83,014✔
3026
      continue;
83,014✔
3027
    }
3028

3029
    for (int32_t m = taskNum - 1; m >= 0; --m) {
2,080✔
3030
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
1,920✔
3031
      if (!pExt->deployed) {
1,920✔
3032
        continue;
×
3033
      }
3034

3035
      mstDestroySStmTaskToDeployExt(pExt);
1,920✔
3036

3037
      taosArrayRemove(pVg->taskList, m);
1,920✔
3038
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
1,920✔
3039
    }
3040
    atomic_store_32(&pVg->deployed, 0);
160✔
3041
    taosWUnLockLatch(&pVg->lock);
160✔
3042
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
160✔
3043
  }
3044

3045
_exit:
41,556✔
3046

3047
  if (code) {
41,556✔
3048
    if (pVg) {
×
3049
      taosWUnLockLatch(&pVg->lock);
×
3050
    }
3051

3052
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3053
  }
3054
}
41,556✔
3055

3056
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
57,762✔
3057
  if (!GOT_SNODE(snodeId)) {
57,762✔
3058
    return;
10,185✔
3059
  }
3060
  
3061
  int32_t code = TSDB_CODE_SUCCESS;
47,577✔
3062
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
47,577✔
3063
  if (NULL == pSnode) {
47,577✔
3064
    return;
16,715✔
3065
  }
3066

3067
  if (taosWTryLockLatch(&pSnode->lock)) {
30,862✔
3068
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3069
    return;
×
3070
  }
3071

3072
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
30,862✔
3073
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
30,862✔
3074
  
3075
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
30,862✔
UNCOV
3076
    taosWUnLockLatch(&pSnode->lock);
×
UNCOV
3077
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
UNCOV
3078
    return;
×
3079
  }
3080

3081
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
30,862✔
3082
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
30,382✔
3083
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
30,382✔
3084
    pSnode->triggerList = NULL;
30,382✔
3085
  }
3086

3087
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
30,862✔
3088
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
30,382✔
3089
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
30,382✔
3090
    pSnode->runnerList = NULL;
30,382✔
3091
  }
3092

3093
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
30,862✔
3094
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
30,382✔
3095
    taosWUnLockLatch(&pSnode->lock);
30,382✔
3096
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
30,382✔
3097
    return;
30,382✔
3098
  }
3099

3100
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
480✔
3101
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
1,440✔
3102
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
960✔
3103
      if (!pExt->deployed) {
960✔
3104
        continue;
×
3105
      }
3106

3107
      mstDestroySStmTaskToDeployExt(pExt);
960✔
3108
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
960✔
3109
      taosArrayRemove(pSnode->triggerList, m);
960✔
3110
    }
3111
    
3112
    pSnode->triggerDeployed = 0;
480✔
3113
  }
3114

3115
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
480✔
3116
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
3,360✔
3117
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
2,880✔
3118
      if (!pExt->deployed) {
2,880✔
3119
        continue;
×
3120
      }
3121

3122
      mstDestroySStmTaskToDeployExt(pExt);
2,880✔
3123
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2,880✔
3124
      taosArrayRemove(pSnode->runnerList, m);
2,880✔
3125
    }
3126
    
3127
    pSnode->runnerDeployed = 0;
480✔
3128
  }
3129
  
3130
  taosWUnLockLatch(&pSnode->lock);
480✔
3131
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
480✔
3132
}
3133

3134
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
13,792,808✔
3135
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,792,808✔
3136
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
41,556✔
3137
  }
3138

3139
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
13,792,808✔
3140
    msmCleanDeployedSnodeTasks(pHb->snodeId);
57,762✔
3141
  }
3142
}
13,792,808✔
3143

3144
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
13,792,808✔
3145
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
13,792,808✔
3146
  if (mStreamMgmt.tCtx) {
13,792,808✔
3147
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
13,713,169✔
3148
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
13,713,169✔
3149
  }
3150
}
13,792,808✔
3151

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

3169
_exit:
196,537✔
3170

3171
  if (code) {
196,537✔
3172
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3173
  }
3174

3175
  return code;
196,537✔
3176
}
3177

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

3194
_exit:
×
3195

3196
  if (code) {
×
3197
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3198
  }
3199

3200
  return code;
×
3201
}
3202

3203

3204

3205
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
59,937✔
3206
  int32_t code = TSDB_CODE_SUCCESS;
59,937✔
3207
  int32_t lino = 0;
59,937✔
3208
  int32_t action = STREAM_ACT_UNDEPLOY;
59,937✔
3209
  bool    dropped = false;
59,937✔
3210

3211
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
59,937✔
3212
  mstsDebug("stream dropped: %d", dropped);
59,937✔
3213
  
3214
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
59,937✔
3215
  if (pAction) {
59,937✔
3216
    pAction->actions |= action;
40,911✔
3217
    if (NULL == pAction->undeploy.taskList) {
40,911✔
3218
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3219
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3220
    }
3221

3222
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
81,822✔
3223
    if (pAction->undeploy.doCheckpoint) {
40,911✔
3224
      pAction->undeploy.doCheckpoint = dropped ? false : true;
18,639✔
3225
    }
3226
    if (!pAction->undeploy.doCleanup) {
40,911✔
3227
      pAction->undeploy.doCleanup = dropped ? true : false;
18,639✔
3228
    }
3229
    
3230
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
40,911✔
3231
  } else {
3232
    SStmAction newAction = {0};
19,026✔
3233
    newAction.actions = action;
19,026✔
3234
    newAction.undeploy.doCheckpoint = dropped ? false : true;
19,026✔
3235
    newAction.undeploy.doCleanup = dropped ? true : false;
19,026✔
3236
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
19,026✔
3237
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
19,026✔
3238
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
38,052✔
3239
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
19,026✔
3240
    
3241
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
19,026✔
3242
  }
3243

3244
_exit:
59,937✔
3245

3246
  if (code) {
59,937✔
3247
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3248
  }
3249

3250
  return code;
59,937✔
3251
}
3252

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

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

3274
_exit:
3,196✔
3275

3276
  if (code) {
3,196✔
3277
    mstDestroySStmAction(&newAction);
×
3278
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3279
  }
3280

3281
  return code;
3,196✔
3282
}
3283

3284
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
270,026✔
3285
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
270,026✔
3286
  if (NULL == pStream) {
270,026✔
3287
    return false;
×
3288
  }
3289

3290
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
270,026✔
3291
    return false;
×
3292
  }
3293

3294
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
270,026✔
3295
  for (int32_t i = 0; i < readerNum; ++i) {
591,781✔
3296
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
321,755✔
3297
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
321,755✔
3298
      return false;
×
3299
    }
3300
  }
3301

3302
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
270,026✔
3303
  for (int32_t i = 0; i < readerNum; ++i) {
327,287✔
3304
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
57,261✔
3305
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
57,261✔
3306
      return false;
×
3307
    }
3308
  }
3309

3310
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
270,026✔
3311
  SListNode* pNode = listHead(pStream->calcReaders);
270,026✔
3312
  for (int32_t i = 0; i < readerNum; ++i) {
815,383✔
3313
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
545,357✔
3314
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
545,357✔
3315
      return false;
×
3316
    }
3317
    pNode = TD_DLIST_NODE_NEXT(pNode);
545,357✔
3318
  }
3319

3320
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
853,883✔
3321
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
657,346✔
3322
    for (int32_t m = 0; m < runnerNum; ++m) {
1,258,585✔
3323
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
674,728✔
3324
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
674,728✔
3325
        return false;
73,489✔
3326
      }
3327
    }
3328
  }
3329
  
3330
  return true;
196,537✔
3331
}
3332

3333

3334
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
8,398,808✔
3335
  int32_t code = TSDB_CODE_SUCCESS;
8,398,808✔
3336
  int32_t lino = 0;
8,398,808✔
3337
  int32_t action = 0;
8,398,808✔
3338
  int64_t streamId = pMsg->streamId;
8,398,808✔
3339
  SStreamTask* pTask = (SStreamTask*)pMsg;
8,398,808✔
3340
  int8_t  stopped = 0;
8,398,808✔
3341

3342
  msttDebug("start to handle task abnormal status %d", pTask->status);
8,398,808✔
3343
  
3344
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
8,398,808✔
3345
  if (NULL == pStatus) {
8,398,808✔
3346
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3347
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3348
    return;
12,098✔
3349
  }
3350

3351
  stopped = atomic_load_8(&pStatus->stopped);
8,398,808✔
3352
  if (stopped) {
8,398,808✔
3353
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3354
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3355
    return;
×
3356
  }
3357
  
3358
  switch (pMsg->status) {
8,398,808✔
3359
    case STREAM_STATUS_INIT:      
8,393,928✔
3360
      if (STREAM_TRIGGER_TASK != pMsg->type) {
8,393,928✔
3361
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
8,002,279✔
3362
        return;
8,002,279✔
3363
      }
3364
      
3365
      if (INT64_MIN == pStatus->lastActionTs) {
391,649✔
3366
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3367
        return;
×
3368
      }
3369
      
3370
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
391,649✔
3371
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
121,623✔
3372
        return;
121,623✔
3373
      }
3374

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

3397
_exit:
274,906✔
3398

3399
  if (code) {
274,906✔
3400
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3401
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3402
  }
3403
}
3404

3405
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
59,210✔
3406
  int32_t code = TSDB_CODE_SUCCESS;
59,210✔
3407
  int32_t lino = 0;
59,210✔
3408
  SStreamTask* pTask = (SStreamTask*)pStatus;
59,210✔
3409
  int64_t streamId = pStatus->streamId;
59,210✔
3410

3411
  msttInfo("start to handle task error, type: %d", err);
59,210✔
3412

3413
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
59,210✔
3414

3415
_exit:
59,210✔
3416

3417
  if (code) {
59,210✔
3418
    // IGNORE STOP STREAM BY ERROR  
3419
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3420
  }
3421
}
59,210✔
3422

3423
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
2,884,535✔
3424
  int32_t code = TSDB_CODE_SUCCESS;
2,884,535✔
3425
  int32_t lino = 0;
2,884,535✔
3426
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
2,884,535✔
3427

3428
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
2,884,535✔
3429
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3430
  }
3431
  
3432
  SArray* userRecalcList = NULL;
2,884,535✔
3433
  if (atomic_load_ptr(&pStream->userRecalcList)) {
2,884,535✔
3434
    taosWLockLatch(&pStream->userRecalcLock);
3,196✔
3435
    if (pStream->userRecalcList) {
3,196✔
3436
      userRecalcList = pStream->userRecalcList;
3,196✔
3437
      pStream->userRecalcList = NULL;
3,196✔
3438
    }
3439
    taosWUnLockLatch(&pStream->userRecalcLock);
3,196✔
3440
    
3441
    if (userRecalcList) {
3,196✔
3442
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
3,196✔
3443
    }
3444
  }
3445

3446
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
2,884,535✔
3447
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,447,041✔
3448
    if (NULL == pStatus->detailStatus) {
1,447,041✔
3449
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
138,754✔
3450
      if (NULL == pStatus->detailStatus) {
138,754✔
3451
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3452
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3453
      }
3454
    }
3455
    
3456
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,447,041✔
3457
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,447,041✔
3458
  }
3459

3460
_exit:
1,437,494✔
3461

3462
  if (code) {
2,884,535✔
3463
    // IGNORE STOP STREAM BY ERROR
3464
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3465
  }
3466
}
2,884,535✔
3467

3468
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
740,435✔
3469
  int32_t code = TSDB_CODE_SUCCESS;
740,435✔
3470
  int32_t lino = 0;
740,435✔
3471
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
740,435✔
3472

3473
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
740,435✔
3474

3475
  for (int32_t i = 0; i < num; ++i) {
22,578,340✔
3476
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
21,837,905✔
3477
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
21,837,905✔
3478
    
3479
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
21,837,905✔
3480
    if (NULL == ppStatus) {
21,837,905✔
3481
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
4,160✔
3482
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
4,160✔
3483
      continue;
4,160✔
3484
    }
3485

3486
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
21,833,745✔
3487
    int8_t stopped = atomic_load_8(&pStream->stopped);
21,833,745✔
3488
    if (stopped) {
21,833,745✔
3489
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
55,050✔
3490
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
55,050✔
3491
      continue;
55,050✔
3492
    }
3493

3494
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
21,778,695✔
3495
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3496
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3497
          
3498
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3499
      continue;
×
3500
    }
3501

3502
    if ((*ppStatus)->status != pTask->status) {
21,778,695✔
3503
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,768,066✔
3504
        (*ppStatus)->runningStartTs = pCtx->currTs;
752,446✔
3505
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,015,620✔
3506
        if (pStream->triggerTask) {
×
3507
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3508
        }
3509
        
3510
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3511
      }
3512
    }
3513
    
3514
    (*ppStatus)->errCode = pTask->errorCode;
21,778,695✔
3515
    (*ppStatus)->status = pTask->status;
21,778,695✔
3516
    (*ppStatus)->lastUpTs = pCtx->currTs;
21,778,695✔
3517
    
3518
    if (STREAM_STATUS_RUNNING != pTask->status) {
21,778,695✔
3519
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
8,398,808✔
3520
    }
3521
    
3522
    if (STREAM_TRIGGER_TASK == pTask->type) {
21,778,695✔
3523
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
2,884,535✔
3524
    }
3525
  }
3526

3527
_exit:
740,435✔
3528

3529
  if (code) {
740,435✔
3530
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3531
  }
3532

3533
  return code;
740,435✔
3534
}
3535

3536
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3537
  int32_t code = TSDB_CODE_SUCCESS;
×
3538
  int32_t lino = 0;
×
3539
  int64_t streamId = pTask->streamId;
×
3540
  SStreamObj* pStream = NULL;
×
3541

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

3554
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3555
    mndReleaseStream(pCtx->pMnode, pStream);
×
3556

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

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

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

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

3633
_exit:
×
3634

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

3641
  return code;
×
3642
}
3643

3644
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3645
  int32_t code = TSDB_CODE_SUCCESS;
×
3646
  int32_t lino = 0;
×
3647
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3648

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

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

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

3672
_exit:
×
3673

3674
  if (code) {
×
3675
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3676
  }
3677

3678
  return code;
×
3679
}
3680

3681
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
196,537✔
3682
  int32_t code = TSDB_CODE_SUCCESS;
196,537✔
3683
  int32_t lino = 0;
196,537✔
3684
  if (NULL == pCtx->pRsp->start.taskList) {
196,537✔
3685
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
70,686✔
3686
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
70,686✔
3687
  }
3688

3689
  SStmTaskId* pId = &pAction->start.triggerId;
196,537✔
3690
  SStreamTaskStart start = {0};
196,537✔
3691
  start.task.type = STREAM_TRIGGER_TASK;
196,537✔
3692
  start.task.streamId = streamId;
196,537✔
3693
  start.task.taskId = pId->taskId;
196,537✔
3694
  start.task.seriousId = pId->seriousId;
196,537✔
3695
  start.task.nodeId = pId->nodeId;
196,537✔
3696
  start.task.taskIdx = pId->taskIdx;
196,537✔
3697

3698
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
393,074✔
3699
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
196,537✔
3700

3701
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
196,537✔
3702

3703
  return;
196,537✔
3704

3705
_exit:
×
3706

3707
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3708
}
3709

3710

3711
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
19,026✔
3712
  int32_t code = TSDB_CODE_SUCCESS;
19,026✔
3713
  int32_t lino = 0;
19,026✔
3714
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
19,026✔
3715
  if (NULL == pCtx->pRsp->undeploy.taskList) {
19,026✔
3716
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
16,997✔
3717
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
16,997✔
3718
  }
3719

3720
  SStreamTaskUndeploy undeploy;
19,026✔
3721
  for (int32_t i = 0; i < dropNum; ++i) {
78,963✔
3722
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
59,937✔
3723
    undeploy.task = *pTask;
59,937✔
3724
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
59,937✔
3725
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
59,937✔
3726

3727
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
119,874✔
3728
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
59,937✔
3729

3730
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
59,937✔
3731
  }
3732

3733
  return;
19,026✔
3734

3735
_exit:
×
3736

3737
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3738
}
3739

3740
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3741
  int32_t code = TSDB_CODE_SUCCESS;
×
3742
  int32_t lino = 0;
×
3743

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

3750
  if (NULL == pStream->triggerTask) {
×
3751
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3752
    return;
×
3753
  }
3754

3755
  SStreamMgmtRsp rsp = {0};
×
3756
  rsp.reqId = INT64_MIN;
×
3757
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3758
  rsp.task.streamId = streamId;
×
3759
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3760

3761
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3762

3763
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3764
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3765
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3766
  }
3767

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

3770
_exit:
×
3771

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

3779
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,196✔
3780
  int32_t code = TSDB_CODE_SUCCESS;
3,196✔
3781
  int32_t lino = 0;
3,196✔
3782

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

3789
  if (NULL == pStream->triggerTask) {
3,196✔
3790
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3791
    return;
×
3792
  }
3793

3794
  SStreamMgmtRsp rsp = {0};
3,196✔
3795
  rsp.reqId = INT64_MIN;
3,196✔
3796
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,196✔
3797
  rsp.task.streamId = streamId;
3,196✔
3798
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,196✔
3799
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,196✔
3800

3801
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,196✔
3802
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
2,814✔
3803
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
2,814✔
3804
  }
3805

3806
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,392✔
3807

3808
_exit:
3,196✔
3809

3810
  if (code) {
3,196✔
3811
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3812
  } else {
3813
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,196✔
3814
  }
3815
}
3816

3817

3818
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
89,992✔
3819
  int32_t code = TSDB_CODE_SUCCESS;
89,992✔
3820
  int32_t lino = 0;
89,992✔
3821
  void* pIter = NULL;
89,992✔
3822
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
89,992✔
3823

3824
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
89,992✔
3825

3826
  while (1) {
218,759✔
3827
    pIter = taosHashIterate(pCtx->actionStm, pIter);
308,751✔
3828
    if (pIter == NULL) {
308,751✔
3829
      break;
89,992✔
3830
    }
3831

3832
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
218,759✔
3833
    SStmAction *pAction = (SStmAction *)pIter;
218,759✔
3834
    
3835
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
218,759✔
3836
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
19,026✔
3837
      continue;
19,026✔
3838
    }
3839

3840
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
199,733✔
3841
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3842
    }
3843

3844
    if (STREAM_ACT_RECALC & pAction->actions) {
199,733✔
3845
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,196✔
3846
    }
3847

3848
    if (STREAM_ACT_START & pAction->actions) {
199,733✔
3849
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
196,537✔
3850
    }
3851
  }
3852
  
3853
_exit:
89,992✔
3854

3855
  if (pIter) {
89,992✔
3856
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3857
  }
3858

3859
  if (code) {
89,992✔
3860
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3861
  }
3862

3863
  return code;
89,992✔
3864
}
3865

3866
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
13,713,169✔
3867
  int32_t  code = TSDB_CODE_SUCCESS;
13,713,169✔
3868
  int32_t  lino = 0;
13,713,169✔
3869
  int64_t* lastTs = NULL;
13,713,169✔
3870
  bool     noExists = false;
13,713,169✔
3871

3872
  while (true) {
3873
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
14,167,460✔
3874
    if (NULL == lastTs) {
14,167,460✔
3875
      if (noExists) {
602,090✔
3876
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
147,799✔
3877
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
147,799✔
3878
      }
3879

3880
      noExists = true;
454,291✔
3881
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
454,291✔
3882
      
3883
      continue;
454,291✔
3884
    }
3885

3886
    while (true) {
×
3887
      int64_t lastTsValue = atomic_load_64(lastTs);
13,565,370✔
3888
      if (pCtx->currTs > lastTsValue) {
13,565,370✔
3889
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
13,563,255✔
3890
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
13,563,255✔
3891
          return code;
13,563,255✔
3892
        }
3893

3894
        continue;
×
3895
      }
3896

3897
      return code;
2,115✔
3898
    }
3899

3900
    break;
3901
  }
3902

3903
_exit:
147,799✔
3904

3905
  if (code) {
147,799✔
3906
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
147,799✔
3907
  }
3908

3909
  return code;  
147,799✔
3910
}
3911

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

3925
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3926
    pStatus = (SStmStatus*)pIter;
×
3927

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

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

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

3959
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
637✔
3960
  int32_t code = TSDB_CODE_SUCCESS;
637✔
3961
  int32_t lino = 0;
637✔
3962
  int32_t minVal = watchError ? 0 : 1;
637✔
3963

3964
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
637✔
3965
    return code;
×
3966
  }
3967

3968
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
637✔
3969
    (void)sched_yield();
×
3970
  }
3971

3972
  if (watchError) {
637✔
3973
    taosHashClear(mStreamMgmt.vgroupMap);
×
3974
    taosHashClear(mStreamMgmt.snodeMap);
×
3975
    taosHashClear(mStreamMgmt.taskMap);
×
3976
    taosHashClear(mStreamMgmt.streamMap);
×
3977
    mstInfo("watch error happends, clear all maps");
×
3978
    goto _exit;
×
3979
  }
3980

3981
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
637✔
3982
    mstInfo("no stream tasks remain during watch state");
637✔
3983
    goto _exit;
637✔
3984
  }
3985

3986
  msmWatchCheckStreamMap(pCtx);
×
3987

3988
_exit:
637✔
3989

3990
  mStreamMgmt.lastTaskId += 100000;
637✔
3991

3992
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
637✔
3993

3994
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
637✔
3995

3996
  if (code) {
637✔
3997
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3998
  }
3999

4000
  return code;
637✔
4001
}
4002

4003

4004
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
11,340✔
4005
  int32_t code = TSDB_CODE_SUCCESS;
11,340✔
4006
  int32_t lino = 0;
11,340✔
4007
  SStreamHbMsg* pReq = pCtx->pReq;
11,340✔
4008

4009
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
11,340✔
4010
  
4011
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
11,340✔
4012
    goto _exit;
×
4013
  }
4014

4015
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
11,340✔
4016
  if (GOT_SNODE(pReq->snodeId)) {
11,340✔
4017
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,246✔
4018
  }
4019

4020
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
11,340✔
4021
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4022
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4023
  }
4024

4025
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
11,340✔
4026
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
637✔
4027
  }
4028

4029
_exit:
11,340✔
4030

4031
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
11,340✔
4032
  
4033
  if (code) {
11,340✔
4034
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4035

4036
    (void)msmWatchHandleEnding(pCtx, true);
×
4037
  }
4038

4039
  return code;
11,340✔
4040
}
4041

4042
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,000,368✔
4043
  int32_t listSize = taosArrayGetSize(pOReaders);
5,000,368✔
4044
  int32_t totalSize = 0;
5,000,368✔
4045
  
4046
  for (int32_t i = 0; i < listSize; ++i) {
6,022,850✔
4047
    SArray* pList = taosArrayGetP(pOReaders, i);
1,022,482✔
4048
    totalSize += taosArrayGetSize(pList);
1,022,482✔
4049
  }
4050

4051
  return totalSize;
5,000,368✔
4052
}
4053

4054
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
641,761✔
4055
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
641,761✔
4056
  if (NULL == pList) {
641,761✔
4057
    return NULL;
×
4058
  }
4059

4060
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
641,761✔
4061
}
4062

4063

4064
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
57,261✔
4065
  int32_t code = TSDB_CODE_SUCCESS;
57,261✔
4066
  int32_t lino = 0;
57,261✔
4067

4068
  if (NULL == pStatus->trigOReaders) {
57,261✔
4069
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
49,586✔
4070
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
49,586✔
4071
  }
4072

4073
  while (true) {
49,586✔
4074
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
106,847✔
4075

4076
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
106,847✔
4077
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
49,586✔
4078
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
49,586✔
4079

4080
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
99,172✔
4081
      continue;
49,586✔
4082
    }
4083

4084
    *ppRes = *ppOReaderList;
57,261✔
4085
    break;
57,261✔
4086
  }
4087

4088
_exit:
57,261✔
4089

4090
  if (code) {
57,261✔
4091
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4092
  }
4093

4094
  return code;
57,261✔
4095
}
4096

4097
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
66,827✔
4098
  int32_t code = TSDB_CODE_SUCCESS;
66,827✔
4099
  int32_t lino = 0;
66,827✔
4100
  bool    readerExists = false;
66,827✔
4101
  int64_t streamId = pTask->streamId;
66,827✔
4102

4103
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
66,827✔
4104
  for (int32_t i = 0; i < readerNum; ++i) {
139,603✔
4105
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
82,342✔
4106
    if (pReader->id.nodeId == vgId) {
82,342✔
4107
      readerExists = true;
9,566✔
4108
      break;
9,566✔
4109
    }
4110
  }
4111

4112
  if (!readerExists) {
66,827✔
4113
    SArray* pReaderList = NULL;
57,261✔
4114
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
57,261✔
4115
    
4116
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
57,261✔
4117
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
57,261✔
4118
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
57,261✔
4119
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
57,261✔
4120
  }
4121

4122
_exit:
9,566✔
4123

4124
  if (code) {
66,827✔
4125
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4126
  }
4127

4128
  return code;
66,827✔
4129
}
4130

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

4149
  if (NULL == pCtx->pRsp->rsps.rspList) {
58,579✔
4150
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4151
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4152
  }
4153
  
4154
  TSWAP(pTask->pMgmtReq, pMgmtReq);
58,579✔
4155
  rsp.task = *(SStreamTask*)pTask;
58,579✔
4156

4157
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
58,579✔
4158
  if (NULL == pStatus) {
58,579✔
4159
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4160
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4161
  }
4162

4163
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
58,579✔
4164
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4165
    goto _exit;
×
4166
  }
4167

4168
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
58,579✔
4169

4170
  stopped = atomic_load_8(&pStatus->stopped);
58,579✔
4171
  if (stopped) {
58,579✔
4172
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4173
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4174
  }
4175

4176
  if (tbNum <= 0) {
58,579✔
4177
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4178
    goto _exit;
×
4179
  }
4180

4181
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
58,579✔
4182
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
58,579✔
4183
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
58,579✔
4184

4185
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
58,579✔
4186
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
58,579✔
4187
  
4188
  for (int32_t i = 0; i < tbNum; ++i) {
233,275✔
4189
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
174,696✔
4190
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
174,696✔
4191
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
349,392✔
4192
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
174,696✔
4193
  }
4194

4195
  int32_t vgNum = tSimpleHashGetSize(pVgs);
58,579✔
4196
  while (true) {
4197
    p = tSimpleHashIterate(pVgs, p, &iter);
125,406✔
4198
    if (NULL == p) {
125,406✔
4199
      break;
58,579✔
4200
    }
4201
    
4202
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
66,827✔
4203
  }
4204
  
4205
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
58,579✔
4206
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
58,579✔
4207
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
58,579✔
4208

4209
  SStreamTaskAddr addr;
58,579✔
4210
  for (int32_t i = 0; i < vgNum; ++i) {
115,840✔
4211
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
57,261✔
4212
    addr.taskId = pOTask->id.taskId;
57,261✔
4213
    addr.nodeId = pOTask->id.nodeId;
57,261✔
4214
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
57,261✔
4215
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
114,522✔
4216
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
57,261✔
4217
  }
4218

4219
_exit:
58,579✔
4220

4221
  tFreeSStreamMgmtReq(pMgmtReq);
58,579✔
4222
  taosMemoryFree(pMgmtReq);
58,579✔
4223

4224
  tSimpleHashCleanup(pVgs);
58,579✔
4225
  mstDestroyDbVgroupsHash(pDbVgroups);
58,579✔
4226

4227
  if (code) {
58,579✔
4228
    rsp.code = code;
×
4229
    
4230
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4231

4232
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4233
  } else {
4234
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
117,158✔
4235
  }
4236

4237
_final:
58,579✔
4238

4239
  if (finalCode) {
58,579✔
4240
    tFreeSStreamMgmtRsp(&rsp);
×
4241
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4242
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4243
  }
4244

4245
  return finalCode;
58,579✔
4246
}
4247

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

4262
_exit:
2,244✔
4263

4264
  if (code) {
2,244✔
4265
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4266
  }
4267

4268
  return code;
2,244✔
4269
}
4270

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

4280
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,244✔
4281
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
20,808✔
4282
    pReader = (SStmTaskStatus*)pNode->data;
18,564✔
4283
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
18,564✔
4284
      readerExists = true;
×
4285
      pAddr->taskId = pReader->id.taskId;
×
4286
      break;
×
4287
    }
4288
  }
4289

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

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

4311
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,244✔
4312
  pAddr->nodeId = vgId;
2,244✔
4313

4314
_exit:
2,244✔
4315

4316
  if (code) {
2,244✔
4317
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4318
  }
4319

4320
  return code;
2,244✔
4321
}
4322

4323

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

4341
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,244✔
4342
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4343
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4344
  }
4345
  
4346
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,244✔
4347
  rsp.task = *(SStreamTask*)pTask;
2,244✔
4348

4349
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,244✔
4350
  if (NULL == pStatus) {
2,244✔
4351
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4352
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4353
  }
4354

4355
  stopped = atomic_load_8(&pStatus->stopped);
2,244✔
4356
  if (stopped) {
2,244✔
4357
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4358
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4359
  }
4360

4361
  if (reqNum <= 0) {
2,244✔
4362
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4363
    goto _exit;
×
4364
  }
4365

4366
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,244✔
4367
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,244✔
4368

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

4384
_exit:
2,244✔
4385

4386
  tFreeSStreamMgmtReq(pMgmtReq);
2,244✔
4387
  taosMemoryFree(pMgmtReq);
2,244✔
4388

4389
  if (code) {
2,244✔
4390
    rsp.code = code;
×
4391
    
4392
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4393
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4394
  } else {
4395
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,488✔
4396
  }
4397

4398

4399
_final:
2,244✔
4400

4401
  if (finalCode) {
2,244✔
4402
    tFreeSStreamMgmtRsp(&rsp);
×
4403
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4404
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4405
  }
4406

4407
  return finalCode;
2,244✔
4408
}
4409

4410

4411
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
60,823✔
4412
  int32_t code = TSDB_CODE_SUCCESS;
60,823✔
4413
  int32_t lino = 0;
60,823✔
4414

4415
  switch (pTask->pMgmtReq->type) {
60,823✔
4416
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
58,579✔
4417
      msmDeployTriggerOrigReader(pCtx, pTask);
58,579✔
4418
      break;
58,579✔
4419
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,244✔
4420
      msmDeployRunnerOrigReader(pCtx, pTask);
2,244✔
4421
      break;
2,244✔
4422
    default:
×
4423
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4424
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4425
      break;
×
4426
  }
4427

4428
_exit:
60,823✔
4429

4430
  if (code) {
60,823✔
4431
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4432
  }
4433

4434
  return code;
60,823✔
4435
}
4436

4437
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
15,565✔
4438
  int32_t code = TSDB_CODE_SUCCESS;
15,565✔
4439
  int32_t lino = 0;
15,565✔
4440
  SStreamHbMsg* pReq = pCtx->pReq;
15,565✔
4441
  SStmTaskStatusMsg* pTask = NULL;
15,565✔
4442
  
4443
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
15,565✔
4444
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
15,565✔
4445
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
15,565✔
4446
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
15,565✔
4447
  }
4448
  
4449
  for (int32_t i = 0; i < reqNum; ++i) {
76,388✔
4450
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
60,823✔
4451
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
60,823✔
4452
    if (NULL == pTask) {
60,823✔
4453
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4454
      continue;
×
4455
    }
4456

4457
    if (NULL == pTask->pMgmtReq) {
60,823✔
4458
      msttError("idx %d without mgmtReq", idx);
×
4459
      continue;
×
4460
    }
4461

4462
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
60,823✔
4463
  }
4464

4465
_exit:
15,565✔
4466

4467
  if (code) {
15,565✔
4468
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4469
  }
4470

4471
  return code;
15,565✔
4472
}
4473

4474
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
13,701,829✔
4475
  int32_t code = TSDB_CODE_SUCCESS;
13,701,829✔
4476
  int32_t lino = 0;
13,701,829✔
4477
  SStreamHbMsg* pReq = pCtx->pReq;
13,701,829✔
4478

4479
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
13,701,829✔
4480
  if (GOT_SNODE(pReq->snodeId)) {
13,554,030✔
4481
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
811,547✔
4482
  }
4483
  
4484
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
13,554,030✔
4485
    msmHandleStreamActions(pCtx);
28,273✔
4486
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
28,273✔
4487
  }
4488

4489
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
13,554,030✔
4490
    code = msmHandleStreamRequests(pCtx);
15,565✔
4491
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
15,565✔
4492
    TAOS_CHECK_EXIT(code);
15,565✔
4493
  }
4494

4495
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,554,030✔
4496
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
41,556✔
4497
  } else {
4498
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
13,512,474✔
4499
  }
4500

4501
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
13,554,030✔
4502
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
47,577✔
4503
  }
4504

4505
  if (taosHashGetSize(pCtx->deployStm) > 0) {
13,554,030✔
4506
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
43,076✔
4507
  }
4508

4509
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,554,030✔
4510
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
740,435✔
4511
  }
4512

4513
  if (taosHashGetSize(pCtx->actionStm) > 0) {
13,554,030✔
4514
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
89,992✔
4515
  }
4516

4517
_exit:
13,554,030✔
4518

4519
  if (code) {
13,701,829✔
4520
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
147,799✔
4521
  }
4522

4523
  return code;
13,701,829✔
4524
}
4525

4526
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
13,792,808✔
4527
  int32_t lino = 0;
13,792,808✔
4528
  int32_t tlen = 0;
13,792,808✔
4529
  void   *buf = NULL;
13,792,808✔
4530

4531
  if (TSDB_CODE_SUCCESS != code) {
13,792,808✔
4532
    goto _exit;
147,799✔
4533
  }
4534

4535
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
13,645,009✔
4536
  if (code < 0) {
13,645,009✔
4537
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4538
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4539
  }
4540

4541
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
13,645,009✔
4542
  if (buf == NULL) {
13,645,009✔
4543
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4544
    TAOS_CHECK_EXIT(terrno);    
×
4545
  }
4546

4547
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
13,645,009✔
4548
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
13,645,009✔
4549

4550
  SEncoder encoder;
13,642,587✔
4551
  tEncoderInit(&encoder, abuf, tlen);
13,645,009✔
4552
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
13,645,009✔
4553
    rpcFreeCont(buf);
×
4554
    buf = NULL;
×
4555
    tEncoderClear(&encoder);
×
4556
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4557
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4558
  }
4559
  tEncoderClear(&encoder);
13,645,009✔
4560

4561
_exit:
13,792,808✔
4562

4563
  pMsg->code = code;
13,792,808✔
4564
  pMsg->info = *pRpcInfo;
13,792,808✔
4565
  if (TSDB_CODE_SUCCESS == code) {
13,792,808✔
4566
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
13,645,009✔
4567
    pMsg->pCont = buf;
13,645,009✔
4568
  }
4569
}
13,792,808✔
4570

4571

4572
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
13,792,808✔
4573
  int32_t code = TSDB_CODE_SUCCESS;
13,792,808✔
4574
  SMStreamHbRspMsg rsp = {0};
13,792,808✔
4575
  rsp.streamGId = pHb->streamGId;
13,792,808✔
4576

4577
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
13,792,808✔
4578

4579
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
13,792,808✔
4580
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
79,639✔
4581
    goto _exit;
79,639✔
4582
  }
4583

4584
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
13,713,169✔
4585
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
13,713,169✔
4586

4587
  pCtx->tidx = tidx;
13,713,169✔
4588
  pCtx->pMnode = pMnode;
13,713,169✔
4589
  pCtx->currTs = currTs;
13,713,169✔
4590
  pCtx->pReq = pHb;
13,713,169✔
4591
  pCtx->pRsp = &rsp;
13,713,169✔
4592
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
13,713,169✔
4593
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
13,713,169✔
4594
  
4595
  switch (atomic_load_8(&mStreamMgmt.state)) {
13,713,169✔
4596
    case MND_STM_STATE_WATCH:
11,340✔
4597
      code = msmWatchHandleHbMsg(pCtx);
11,340✔
4598
      break;
11,340✔
4599
    case MND_STM_STATE_NORMAL:
13,701,829✔
4600
      code = msmNormalHandleHbMsg(pCtx);
13,701,829✔
4601
      break;
13,701,829✔
4602
    default:
×
4603
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4604
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4605
      break;
×
4606
  }
4607

4608
_exit:
13,792,808✔
4609

4610
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
13,792,808✔
4611

4612
  msmCleanStreamGrpCtx(pHb);
13,792,808✔
4613
  msmClearStreamToDeployMaps(pHb);
13,792,808✔
4614

4615
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
13,792,808✔
4616
  
4617
  tFreeSMStreamHbRspMsg(&rsp);
13,792,808✔
4618

4619
  return code;
13,792,808✔
4620
}
4621

4622
void msmHandleBecomeLeader(SMnode *pMnode) {
366,222✔
4623
  if (tsDisableStream) {
366,222✔
4624
    return;
×
4625
  }
4626

4627
  mstInfo("start to process mnode become leader");
366,222✔
4628

4629
  int32_t code = 0;
366,222✔
4630
  streamAddVnodeLeader(MNODE_HANDLE);
366,222✔
4631
  
4632
  taosWLockLatch(&mStreamMgmt.runtimeLock);
366,222✔
4633
  msmDestroyRuntimeInfo(pMnode);
366,222✔
4634
  code = msmInitRuntimeInfo(pMnode);
366,222✔
4635
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
366,222✔
4636

4637
  if (TSDB_CODE_SUCCESS == code) {
366,222✔
4638
    atomic_store_8(&mStreamMgmt.active, 1);
366,222✔
4639
  }
4640

4641
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
366,222✔
4642
}
4643

4644
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
453,529✔
4645
  if (tsDisableStream) {
453,529✔
4646
    return;
×
4647
  }
4648

4649
  mstInfo("start to process mnode become not leader");
453,529✔
4650

4651
  streamRemoveVnodeLeader(MNODE_HANDLE);
453,529✔
4652

4653
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
453,529✔
4654
    taosWLockLatch(&mStreamMgmt.runtimeLock);
366,222✔
4655
    msmDestroyRuntimeInfo(pMnode);
366,222✔
4656
    mStreamMgmt.stat.inactiveTimes++;
366,222✔
4657
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
366,222✔
4658
  }
4659
}
4660

4661

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

4671
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
6,707✔
4672
  int32_t code = TSDB_CODE_SUCCESS;
6,707✔
4673
  int32_t lino = 0;
6,707✔
4674
  SStreamObj* pStream = pObj;
6,707✔
4675
  SSnodeObj* pSnode = p1;
6,707✔
4676
  SArray** ppRes = p2;
6,707✔
4677

4678
  if (pStream->mainSnodeId == pSnode->id) {
6,707✔
4679
    if (NULL == *ppRes) {
4,037✔
4680
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
586✔
4681
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
586✔
4682
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
586✔
4683
    }
4684

4685
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
8,074✔
4686
  }
4687

4688
  return true;
6,707✔
4689

4690
_exit:
×
4691

4692
  if (code) {
×
4693
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4694
  }  
4695

4696
  *(int32_t*)p3 = code;
×
4697

4698
  return false;
×
4699
}
4700

4701

4702
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
36,045✔
4703
  int32_t code = TSDB_CODE_SUCCESS;
36,045✔
4704
  int32_t lino = 0;
36,045✔
4705
  
4706
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
36,045✔
4707
  TAOS_CHECK_EXIT(code);
36,045✔
4708

4709
  int32_t streamNum = taosArrayGetSize(*ppRes);
36,045✔
4710
  if (streamNum > 0 && 0 == pSnode->replicaId) {
36,045✔
4711
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
191✔
4712
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
191✔
4713
  }
4714

4715
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4716

4717
_exit:
36,045✔
4718

4719
  if (code) {
36,045✔
4720
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
191✔
4721
  }  
4722

4723
  return code;
36,045✔
4724
}
4725

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

4744
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
100,679✔
4745
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
17,747✔
4746
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4747
    return true;
17,747✔
4748
  }  
4749

4750
  if (pStatus) {
82,932✔
4751
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
82,511✔
4752
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4753
    }
4754

4755
    return true;
82,511✔
4756
  }
4757

4758
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
421✔
4759

4760
  return true;
421✔
4761
}
4762

4763
void msmCheckLoopStreamMap(SMnode *pMnode) {
33,535✔
4764
  SStmStatus* pStatus = NULL;
33,535✔
4765
  void* pIter = NULL;
33,535✔
4766
  int8_t stopped = 0;
33,535✔
4767
  int64_t streamId = 0;
33,535✔
4768
  
4769
  while (true) {
4770
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
241,398✔
4771
    if (NULL == pIter) {
241,398✔
4772
      break;
33,535✔
4773
    }
4774

4775
    pStatus = (SStmStatus*)pIter;
207,863✔
4776

4777
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
207,863✔
4778
    stopped = atomic_load_8(&pStatus->stopped);
207,863✔
4779
    if (MST_IS_USER_STOPPED(stopped)) {
207,863✔
4780
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
1,812✔
4781
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
1,812✔
4782
      continue;
1,812✔
4783
    }
4784

4785
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
206,051✔
4786
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4787
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4788
      continue;
×
4789
    }
4790

4791
    if (MST_IS_ERROR_STOPPED(stopped)) {
206,051✔
4792
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
10,656✔
4793
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
7,172✔
4794
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4795
            
4796
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
7,172✔
4797
        continue;
7,172✔
4798
      }
4799

4800
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
3,484✔
4801
      continue;
3,484✔
4802
    }
4803

4804
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
195,395✔
4805
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4806
      continue;
×
4807
    }
4808
  }
4809
}
33,535✔
4810

4811
void msmCheckStreamsStatus(SMnode *pMnode) {
241,896✔
4812
  SStmCheckStatusCtx ctx = {0};
241,896✔
4813

4814
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
241,896✔
4815
  
4816
  if (MST_READY_FOR_SDB_LOOP()) {
241,896✔
4817
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
47,354✔
4818
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
47,354✔
4819
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
47,354✔
4820
  }
4821

4822
  if (MST_READY_FOR_MAP_LOOP()) {
241,896✔
4823
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
33,535✔
4824
    msmCheckLoopStreamMap(pMnode);
33,535✔
4825
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
33,535✔
4826
  }
4827
}
241,896✔
4828

4829
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
2,279,791✔
4830
  for (int32_t i = 0; i < taskNum; ++i) {
4,673,994✔
4831
    SStmTaskStatus* pTask = *(pList + i);
2,394,203✔
4832

4833
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
2,394,203✔
4834
      continue;
66,197✔
4835
    }
4836
    
4837
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
2,329,826✔
4838
      continue;
2,328,866✔
4839
    }
4840

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

4850
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
960✔
4851
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4852

4853
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
960✔
4854
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
960✔
4855

4856
    SStmTaskAction task = {0};
960✔
4857
    task.streamId = streamId;
960✔
4858
    task.id = pTask->id;
960✔
4859
    task.flag = pTask->flags;
960✔
4860
    task.type = pTask->type;
960✔
4861
    
4862
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
960✔
4863
  }
4864
}
2,279,791✔
4865

4866
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
102,321✔
4867
  void* pIter = NULL;
102,321✔
4868
  SStmVgStreamStatus* pVg = NULL;
102,321✔
4869
  int64_t streamId = 0;
102,321✔
4870
  
4871
  while (true) {
918,467✔
4872
    pIter = taosHashIterate(pStreams, pIter);
1,020,788✔
4873
    if (NULL == pIter) {
1,020,788✔
4874
      break;
102,321✔
4875
    }
4876

4877
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
918,467✔
4878
    pVg = (SStmVgStreamStatus*)pIter;
918,467✔
4879

4880
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
918,467✔
4881
    if (taskNum > 0) {
918,467✔
4882
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
530,879✔
4883
    }
4884

4885
    taskNum = taosArrayGetSize(pVg->calcReaders);
918,467✔
4886
    if (taskNum > 0) {
918,467✔
4887
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
582,231✔
4888
    }
4889
  }
4890
}
102,321✔
4891

4892
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4893
  int64_t streamId = 0;
×
4894
  void* pIter = NULL;
×
4895
  SStmVgStreamStatus* pStream = NULL;
×
4896

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

4902
  
4903
  while (true) {
4904
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4905
    if (NULL == pIter) {
×
4906
      break;
×
4907
    }
4908

4909
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4910
    
4911
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4912
  }
4913

4914
  taosHashClear(pVg->streamTasks);
×
4915
}
4916

4917

4918
void msmCheckVgroupStatus(SMnode *pMnode) {
241,896✔
4919
  void* pIter = NULL;
241,896✔
4920
  int32_t code = 0;
241,896✔
4921
  
4922
  while (true) {
959,119✔
4923
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,201,015✔
4924
    if (NULL == pIter) {
1,201,015✔
4925
      break;
241,896✔
4926
    }
4927

4928
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
959,119✔
4929
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
959,119✔
4930
      continue;
855,019✔
4931
    }
4932
    
4933
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
104,100✔
4934

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

4953
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
102,321✔
4954

4955
    msmCheckVgroupStreamStatus(pVg->streamTasks);
102,321✔
4956
  }
4957
}
241,896✔
4958

4959
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
1,260✔
4960
  *deployNum = 0;
1,260✔
4961
  
4962
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
4,100✔
4963
    if (pStream->runners[i]) {
3,545✔
4964
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
1,260✔
4965
      for (int32_t t = 0; t < taskNum; ++t) {
1,815✔
4966
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
1,260✔
4967
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
1,260✔
4968
        if (stopped) {
1,260✔
4969
          mstsDebug("stream already stopped %d, ignore it", stopped);
705✔
4970
          *deployNum = 0;
705✔
4971
          return;
705✔
4972
        }
4973

4974
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
555✔
4975
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
555✔
4976
      }
4977
      
4978
      deployId[*deployNum] = i;
555✔
4979
      (*deployNum)++;
555✔
4980
    }
4981
  }
4982
}
4983

4984
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
2,210✔
4985
  pSnode->runnerThreadNum = -1;
2,210✔
4986

4987
  (void)msmSTAddSnodesToMap(pMnode);
2,210✔
4988

4989
  int64_t streamId = 0;
2,210✔
4990
  void* pIter = NULL;
2,210✔
4991
  SStmSnodeStreamStatus* pStream = NULL;
2,210✔
4992
  int32_t deployNum = 0;
2,210✔
4993
  SStmTaskAction task = {0};
2,210✔
4994
  
4995
  while (true) {
4996
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
4,495✔
4997
    if (NULL == pIter) {
4,495✔
4998
      break;
2,210✔
4999
    }
5000

5001
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,285✔
5002
    
5003
    task.streamId = streamId;
2,285✔
5004
    
5005
    pStream = (SStmSnodeStreamStatus*)pIter;
2,285✔
5006
    if (pStream->trigger) {
2,285✔
5007
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
1,025✔
5008
      if (stopped) {
1,025✔
5009
        mstsDebug("stream already stopped %d, ignore it", stopped);
705✔
5010
        continue;
705✔
5011
      }
5012

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

5031
  taosHashClear(pSnode->streamTasks);
2,210✔
5032
}
2,210✔
5033

5034

5035
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
34,017✔
5036
  void* pIter = NULL;
34,017✔
5037
  SStmSnodeStreamStatus* pSnode = NULL;
34,017✔
5038
  int64_t streamId = 0;
34,017✔
5039
  
5040
  while (true) {
5041
    pIter = taosHashIterate(pStreams, pIter);
345,319✔
5042
    if (NULL == pIter) {
345,319✔
5043
      break;
34,017✔
5044
    }
5045

5046
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
311,302✔
5047
    pSnode = (SStmSnodeStreamStatus*)pIter;
311,302✔
5048

5049
    if (NULL != pSnode->trigger) {
311,302✔
5050
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
298,192✔
5051
    }
5052

5053
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,245,208✔
5054
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
933,906✔
5055
      if (taskNum > 0) {
933,906✔
5056
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
868,489✔
5057
      }
5058
    }
5059
  }
5060
}
34,017✔
5061

5062

5063
void msmCheckSnodeStatus(SMnode *pMnode) {
241,896✔
5064
  void* pIter = NULL;
241,896✔
5065
  
5066
  while (true) {
317,001✔
5067
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
558,897✔
5068
    if (NULL == pIter) {
558,897✔
5069
      break;
241,896✔
5070
    }
5071

5072
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
317,001✔
5073
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
317,001✔
5074
      continue;
278,084✔
5075
    }
5076

5077
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
38,917✔
5078
    
5079
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
38,917✔
5080
    if (NULL == pSnode->streamTasks) {
38,917✔
5081
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
3,320✔
5082
      continue;
3,320✔
5083
    }
5084
    
5085
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
35,597✔
5086
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,580✔
5087
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5088
      
5089
      msmHandleSnodeLost(pMnode, pSnode);
1,580✔
5090
      continue;
1,580✔
5091
    }
5092
    
5093
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
34,017✔
5094

5095
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
34,017✔
5096
  }
5097
}
241,896✔
5098

5099

5100
void msmCheckTasksStatus(SMnode *pMnode) {
241,896✔
5101
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
241,896✔
5102

5103
  msmCheckVgroupStatus(pMnode);
241,896✔
5104
  msmCheckSnodeStatus(pMnode);
241,896✔
5105
}
241,896✔
5106

5107
void msmCheckSnodesState(SMnode *pMnode) {
241,896✔
5108
  if (!MST_READY_FOR_SNODE_LOOP()) {
241,896✔
5109
    return;
212,987✔
5110
  }
5111

5112
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
28,909✔
5113

5114
  void* pIter = NULL;
28,909✔
5115
  int32_t snodeId = 0;
28,909✔
5116
  while (true) {
32,699✔
5117
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
61,608✔
5118
    if (NULL == pIter) {
61,608✔
5119
      break;
28,909✔
5120
    }
5121

5122
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
32,699✔
5123
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
32,699✔
5124
      continue;
32,069✔
5125
    }
5126

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

5140
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
28,909✔
5141
}
5142

5143
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
7,691,019✔
5144
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
7,691,019✔
5145
  if (0 == active || MND_STM_STATE_NORMAL != state) {
7,691,019✔
5146
    mstTrace("ignore health check since active:%d state:%d", active, state);
1,911✔
5147
    return false;
1,911✔
5148
  }
5149

5150
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,689,108✔
5151
    mstTrace("ignore health check since no stream now");
7,205,316✔
5152
    return false;
7,205,316✔
5153
  }
5154

5155
  return true;
483,792✔
5156
}
5157

5158
void msmHealthCheck(SMnode *pMnode) {
7,449,123✔
5159
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,449,123✔
5160
    return;
7,207,227✔
5161
  }
5162

5163
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
456,996✔
5164
  
5165
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
241,896✔
5166
  if (!msmCheckNeedHealthCheck(pMnode)) {
241,896✔
5167
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5168
    return;
×
5169
  }
5170
  
5171
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
241,896✔
5172
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
241,896✔
5173

5174
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
241,896✔
5175
  
5176
  msmCheckStreamsStatus(pMnode);
241,896✔
5177
  msmCheckTasksStatus(pMnode);
241,896✔
5178
  msmCheckSnodesState(pMnode);
241,896✔
5179

5180
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
241,896✔
5181

5182
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
241,896✔
5183
}
5184

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

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

5211
  stopped = atomic_load_8(&pStatus->stopped);
×
5212
  if (stopped) {
×
5213
    mstsError("stream already stopped, stopped:%d", stopped);
×
5214
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5215
    goto _exit;
×
5216
  }
5217

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

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

5229
_exit:
×
5230
  
5231
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5232

5233
  return code;
×
5234
}
5235

5236
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
366,222✔
5237
  int32_t code = TSDB_CODE_SUCCESS;
366,222✔
5238
  int32_t lino = 0;
366,222✔
5239
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
366,222✔
5240
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
366,222✔
5241
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
366,222✔
5242

5243
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
732,362✔
5244

5245
  mStreamMgmt.stat.activeTimes++;
366,222✔
5246
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
366,222✔
5247
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
366,222✔
5248
  if (NULL == mStreamMgmt.tCtx) {
366,222✔
5249
    code = terrno;
×
5250
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5251
    goto _exit;
×
5252
  }
5253

5254
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
366,222✔
5255
  if (mStreamMgmt.actionQ == NULL) {
366,222✔
5256
    code = terrno;
×
5257
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5258
    goto _exit;
×
5259
  }
5260
  
5261
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
366,222✔
5262
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
366,222✔
5263
  
5264
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
366,222✔
5265
  
5266
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,197,141✔
5267
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,830,919✔
5268

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

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

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

5342
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
366,222✔
5343
  if (mStreamMgmt.toUpdateScanMap == NULL) {
366,222✔
5344
    code = terrno;
×
5345
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5346
    goto _exit;
×
5347
  }
5348
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
366,222✔
5349

5350
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
366,222✔
5351
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
366,222✔
5352

5353
  mStreamMgmt.lastTaskId = 1;
366,222✔
5354

5355
  int32_t activeStreamNum = 0;
366,222✔
5356
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
366,222✔
5357

5358
  if (activeStreamNum > 0) {
366,222✔
5359
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
637✔
5360
  } else {
5361
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
365,585✔
5362
  }
5363

5364
_exit:
366,222✔
5365

5366
  if (code) {
366,222✔
5367
    msmDestroyRuntimeInfo(pMnode);
×
5368
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5369
  } else {
5370
    mstInfo("mnode stream runtime init done");
366,222✔
5371
  }
5372

5373
  return code;
366,222✔
5374
}
5375

5376

5377

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