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

taosdata / TDengine / #4912

04 Jan 2026 09:05AM UTC coverage: 64.888% (-0.1%) from 65.028%
#4912

push

travis-ci

web-flow
merge: from main to 3.0 branch #34156

1206 of 4524 new or added lines in 22 files covered. (26.66%)

5351 existing lines in 123 files now uncovered.

194856 of 300296 relevant lines covered (64.89%)

118198896.2 hits per line

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

76.2
/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() {
714,508✔
34
  SStmQNode* pQNode = NULL;
714,508✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
714,508✔
37
    return;
357,254✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,786,095✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,716,570✔
49
    taosHashCleanup(pCtx->deployStm[m]);
8,930,475✔
50
    taosHashCleanup(pCtx->actionStm[m]);
8,930,475✔
51
  }
52
}
1,786,095✔
53

54
void msmDestroyThreadCtxs() {
714,508✔
55
  if (NULL == mStreamMgmt.tCtx) {
714,508✔
56
    return;
357,254✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,143,349✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,786,095✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
357,254✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
714,508✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
714,508✔
94
}
714,508✔
95

96

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

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

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

111
    pStatus = pStream;
483✔
112
  }
113

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

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

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

136
_exit:
404✔
137

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

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

145

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

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

200
  pSnode = NULL;
373,895✔
201

202
_exit:
373,895✔
203

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

208
  return code;
373,895✔
209
}
210

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

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

235
  pDnode = NULL;
809,482✔
236

237
_exit:
809,482✔
238

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

243
  return code;
809,482✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
438,050✔
275
  int32_t code = TSDB_CODE_SUCCESS;
438,050✔
276
  int32_t lino = 0;
438,050✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
438,050✔
278
  if (NULL == pStream) {
438,050✔
279
    SStmVgStreamStatus stream = {0};
329,432✔
280
    if (trigReader) {
329,432✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
199,209✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
199,209✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
398,418✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
130,223✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
130,223✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
260,446✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
329,432✔
290
    goto _exit;
329,432✔
291
  }
292
  
293
  if (trigReader) {
108,618✔
294
    if (NULL == pStream->trigReaders) {
21,239✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
21,239✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
21,239✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
42,478✔
300
    goto _exit;
21,239✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
87,379✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
60,441✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
60,441✔
306
  }
307

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

310
_exit:
87,379✔
311

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

320
  return code;
438,050✔
321
}
322

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

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

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

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

350
  return code;
438,050✔
351
}
352

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

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

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

404
  return code;
439,338✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
547,470✔
409
  int32_t code = TSDB_CODE_SUCCESS;
547,470✔
410
  int32_t lino = 0;
547,470✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
547,470✔
412
  if (NULL == pStream) {
547,470✔
413
    SStmSnodeStreamStatus stream = {0};
139,601✔
414
    if (deployId < 0) {
139,601✔
415
      stream.trigger = pStatus;
3,026✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
136,575✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
136,575✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
273,150✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
139,601✔
423
    goto _exit;
139,601✔
424
  }
425
  
426
  if (deployId < 0) {
407,869✔
427
    if (NULL != pStream->trigger) {
131,616✔
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;
131,616✔
433
    goto _exit;
131,616✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
276,253✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
258,595✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
258,595✔
439
  }
440

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

443
_exit:
276,253✔
444

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

453
  return code;
547,470✔
454
}
455

456

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

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

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

484
  return code;
547,470✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
134,642✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
28,409✔
568
      ext.deployed = false;
28,409✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
56,818✔
570

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

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

614
  return code;
412,828✔
615
}
616

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

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

632
_exit:
395,170✔
633

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

638
  return code;
395,170✔
639
}
640

641

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

648
  while (true) {
649
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &pCtx->pReq->snodeId, sizeof(pCtx->pReq->snodeId));
787,143✔
650
    if (NULL == pStatus) {
787,143✔
651
      if (noExists) {
9,687✔
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,687✔
657
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pCtx->pMnode));
9,687✔
658
      
659
      continue;
9,687✔
660
    }
661

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

672
        continue;
×
673
      }
674

675
      return code;
9,687✔
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) {
39,748,476✔
691
  int32_t  code = TSDB_CODE_SUCCESS;
39,748,476✔
692
  int32_t  lino = 0;
39,748,476✔
693
  SStmVgroupStatus* pStatus = taosHashGet(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
39,748,476✔
694
  if (NULL == pStatus) {
39,748,476✔
695
    mstDebug("vgroup %d not exists in vgroupMap, ignore update upTs", vgId);
36,600,451✔
696
    return;
36,600,451✔
697
  }
698

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

707
      continue;
×
708
    }
709

710
    return;
52,269✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
13,906,581✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
53,434,925✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
39,528,344✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
39,528,344✔
725
  }
726

727
_exit:
13,906,581✔
728

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

733
  return code;
13,906,581✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
278,189✔
739
  int32_t num = taosArrayGetSize(pList);
278,189✔
740
  for (int32_t i = 0; i < num; ++i) {
813,557✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
685,165✔
742
    if (pScan->readFromCache) {
685,165✔
743
      return pScan->scanPlan;
149,797✔
744
    }
745
  }
746

747
  return NULL;
128,392✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
439,338✔
775
}
776

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

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

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

805
_exit:
131,616✔
806

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

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

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

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

875
  SStreamTaskAddr addr;
134,243✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
134,642✔
877
  if (triggerReaderNum > 0) {
134,642✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
133,737✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
133,737✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
299,517✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
164,875✔
884
    addr.taskId = pStatus->id.taskId;
164,875✔
885
    addr.nodeId = pStatus->id.nodeId;
164,875✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
164,875✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
329,750✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
164,875✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
134,642✔
892
  pMsg->streamName = pInfo->streamName;
134,642✔
893

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

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

901
_exit:
131,616✔
902

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

912

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

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

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

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

957

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

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

981

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

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

999
  return code;
529,812✔
1000
}
1001

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

1006
int64_t msmAssignTaskSeriousId(void) {
985,198✔
1007
  return taosGetTimestampNs();
985,198✔
1008
}
1009

1010

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

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

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

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

1035
_exit:
681,780✔
1036

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

1041
  return code;
681,780✔
1042
}
1043

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

1055
    if (alive) {
266,419✔
1056
      return snodeId;
266,419✔
1057
    }
1058
    
1059
    if (snodeId == mainSnodeId) {
×
1060
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
×
1061
      if (NULL == pSnode) {
×
1062
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1063
        return 0;
×
1064
      }
1065
      
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

1072
      snodeId = pSnode->replicaId;
×
1073
      mndReleaseSnode(pMnode, pSnode);
×
1074
      
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) {
402,333✔
1092
  int32_t code = TSDB_CODE_SUCCESS;
402,333✔
1093
  int32_t lino = 0;
402,333✔
1094
  int32_t snodeIdx = 0;
402,333✔
1095
  int32_t snodeId = 0;
402,333✔
1096
  void      *pIter = NULL;
402,333✔
1097
  SSnodeObj *pObj = NULL;
402,333✔
1098
  bool alive = false;
402,333✔
1099
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
402,333✔
1100
  if (snodeNum <= 0) {
402,333✔
1101
    mstsInfo("no available snode now, num:%d", snodeNum);
2,300✔
1102
    goto _exit;
2,300✔
1103
  }
1104

1105
  int32_t snodeTarget = taosRand() % snodeNum;
400,033✔
1106

1107
  while (1) {
1108
    pIter = sdbFetch(pMnode->pSdb, SDB_SNODE, pIter, (void **)&pObj);
415,361✔
1109
    if (pIter == NULL) {
415,361✔
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);
415,361✔
1120
    if (code) {
415,361✔
1121
      sdbRelease(pMnode->pSdb, pObj);
×
1122
      sdbCancelFetch(pMnode->pSdb, pIter);
×
1123
      pObj = NULL;
×
1124
      TAOS_CHECK_EXIT(code);
×
1125
    }
1126
    
1127
    if (!alive) {
415,361✔
1128
      sdbRelease(pMnode->pSdb, pObj);
×
1129
      continue;
×
1130
    }
1131

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

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

1144
_exit:
402,333✔
1145

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

1150
  if (0 == snodeId) {
402,333✔
1151
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,300✔
1152
  }
1153

1154
  return snodeId;
402,333✔
1155
}
1156

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

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

1168
_exit:
529,812✔
1169

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

1174
  return snodeId;
529,812✔
1175
}
1176

1177

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

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

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

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

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

1210
_exit:
134,642✔
1211

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

1216
  return code;
134,642✔
1217
}
1218

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

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

1245
_exit:
220,448✔
1246

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

1251
  return code;
220,448✔
1252
}
1253

1254
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
134,642✔
1255
  int32_t code = TSDB_CODE_SUCCESS;
134,642✔
1256
  int32_t lino = 0;
134,642✔
1257
  int64_t streamId = pStream->pCreate->streamId;
134,642✔
1258
  SSdb   *pSdb = pCtx->pMnode->pSdb;
134,642✔
1259
  SStmTaskStatus* pState = NULL;
134,642✔
1260
  SVgObj *pVgroup = NULL;
134,642✔
1261
  SDbObj* pDb = NULL;
134,642✔
1262
  
1263
  switch (pStream->pCreate->triggerTblType) {
134,642✔
1264
    case TSDB_NORMAL_TABLE:
70,210✔
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);
70,210✔
1269
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
70,210✔
1270
      pState = taosArrayGet(pInfo->trigReaders, 0);
70,210✔
1271
      
1272
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
70,210✔
1273
      break;
70,210✔
1274
    }
1275
    case TSDB_SUPER_TABLE: {
63,527✔
1276
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
63,527✔
1277
      if (NULL == pDb) {
63,527✔
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));
63,527✔
1284
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
63,527✔
1285
      
1286
      void *pIter = NULL;
63,527✔
1287
      while (1) {
347,020✔
1288
        SStmTaskDeploy info = {0};
410,547✔
1289
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
410,547✔
1290
        if (pIter == NULL) {
410,547✔
1291
          break;
63,527✔
1292
        }
1293
      
1294
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
347,020✔
1295
          pState = taosArrayReserve(pInfo->trigReaders, 1);
94,665✔
1296

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

1306
        sdbRelease(pSdb, pVgroup);
347,020✔
1307
      }
1308
      break;
63,527✔
1309
    }
1310
    default:
905✔
1311
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
905✔
1312
      break;
905✔
1313
  }
1314

1315
_exit:
134,642✔
1316

1317
  mndReleaseDb(pCtx->pMnode, pDb);
134,642✔
1318

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

1323
  return code;
134,642✔
1324
}
1325

1326
int32_t msmUPAddScanTask(SStmGrpCtx* pCtx, SStreamObj* pStream, char* scanPlan, int32_t vgId, int64_t taskId) {
215,640✔
1327
  int32_t code = TSDB_CODE_SUCCESS;
215,640✔
1328
  int32_t lino = 0;
215,640✔
1329
  SSubplan* pSubplan = NULL;
215,640✔
1330
  int64_t streamId = pStream->pCreate->streamId;
215,640✔
1331
  int64_t key[2] = {streamId, 0};
215,640✔
1332
  SStmTaskSrcAddr addr;
215,241✔
1333
  TAOS_CHECK_EXIT(nodesStringToNode(scanPlan, (SNode**)&pSubplan));
215,640✔
1334
  addr.isFromCache = false;
215,640✔
1335
  
1336
  if (MNODE_HANDLE == vgId) {
215,640✔
1337
    mndGetMnodeEpSet(pCtx->pMnode, &addr.epset);
×
1338
  } else if (vgId > MNODE_HANDLE) {
215,640✔
1339
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
215,640✔
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;
215,640✔
1346
  addr.vgId = vgId;
215,640✔
1347
  addr.groupId = pSubplan->id.groupId;
215,640✔
1348

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

1351
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
215,640✔
1352
  if (NULL == ppRes) {
215,640✔
1353
    SArray* pRes = taosArrayInit(1, sizeof(addr));
215,640✔
1354
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
215,640✔
1355
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
431,280✔
1356
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
215,640✔
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);
215,640✔
1362
  
1363
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
215,640✔
1364
  
1365
_exit:
215,640✔
1366

1367
  nodesDestroyNode((SNode*)pSubplan);
215,640✔
1368

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

1373
  return code;
215,640✔
1374
}
1375

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

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

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

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

1412
  return code;
65,615✔
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) {
217,602✔
1417
  int32_t code = TSDB_CODE_SUCCESS;
217,602✔
1418
  int32_t lino = 0;
217,602✔
1419

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

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

1444
_exit:
217,602✔
1445

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

1450
  return code;
217,602✔
1451
}
1452

1453

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

1463
  
1464
  for (int32_t i = 0; i < calcTasksNum; ++i) {
415,736✔
1465
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
281,094✔
1466
    if (pScan->readFromCache) {
281,094✔
1467
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
65,615✔
1468
      continue;
65,615✔
1469
    }
1470
    
1471
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
215,479✔
1472
    for (int32_t m = 0; m < vgNum; ++m) {
430,958✔
1473
      pState = tdListReserve(pInfo->calcReaders);
215,479✔
1474
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
215,479✔
1475

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

1481
_exit:
134,642✔
1482

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

1487
  return code;
134,642✔
1488
}
1489

1490

1491

1492
static int32_t msmUPPrepareReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
161✔
1493
  int32_t code = TSDB_CODE_SUCCESS;
161✔
1494
  int32_t lino = 0;
161✔
1495
  int64_t streamId = pStream->pCreate->streamId;
161✔
1496
  int32_t calcTasksNum = taosArrayGetSize(pStream->pCreate->calcScanPlanList);
161✔
1497
  if (calcTasksNum <= 0) {
161✔
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);
161✔
1503
  
1504
  for (int32_t i = 0; i < calcTasksNum; ++i) {
322✔
1505
    SStreamCalcScan* pScan = taosArrayGet(pStream->pCreate->calcScanPlanList, i);
161✔
1506
    if (pScan->readFromCache) {
161✔
1507
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
×
1508
      continue;
×
1509
    }
1510
    
1511
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
161✔
1512
    for (int32_t m = 0; m < vgNum; ++m) {
322✔
1513
      SStmTaskStatus* pReader = (SStmTaskStatus*)pNode->data;
161✔
1514
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pReader->id.nodeId, pReader->id.taskId));
161✔
1515
      pNode = TD_DLIST_NODE_NEXT(pNode);
161✔
1516
    }
1517
  }
1518

1519
_exit:
161✔
1520

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

1525
  return code;
161✔
1526
}
1527

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

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

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

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

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

1565
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
861,262✔
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);
861,262✔
1569
}
1570

1571
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
17,658✔
1572
  int64_t streamId = pStream->pCreate->streamId;
17,658✔
1573
  int32_t runnerNum = taosArrayGetSize(pRunners);
17,658✔
1574
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
44,145✔
1575
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
44,145✔
1576
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
44,145✔
1577
    if (pPlan->id.subplanId == subplanId) {
44,145✔
1578
      *taskId = pDeploy->task.taskId;
17,658✔
1579
      *ppParent = &pDeploy->task;
17,658✔
1580
      return TSDB_CODE_SUCCESS;
17,658✔
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) {
412,828✔
1590
  int32_t code = TSDB_CODE_SUCCESS;
412,828✔
1591
  int32_t lino = 0;
412,828✔
1592
  int64_t key[2] = {streamId, -1};
412,828✔
1593
  SNode* pNode = NULL;
412,828✔
1594
  SStreamTask* pTask = &pDeploy->task;
412,828✔
1595
  FOREACH(pNode, pPlan->pChildren) {
1,274,090✔
1596
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
861,262✔
1597
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
17,658✔
1598
      continue;
17,658✔
1599
    }
1600
    
1601
    SValueNode* pVal = (SValueNode*)pNode;
843,604✔
1602
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
843,604✔
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);
843,604✔
1608

1609
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
843,604✔
1610
    if (NULL == ppRes) {
843,604✔
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);
843,604✔
1616
    for (int32_t i = 0; i < childrenNum; ++i) {
1,687,208✔
1617
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
843,604✔
1618
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
843,604✔
1619
    }
1620
  }
1621

1622
_exit:
412,828✔
1623

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

1628
  return code;
412,828✔
1629
}
1630

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

1639
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
412,828✔
1640

1641
  SNode* pTmp = NULL;
412,828✔
1642
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,274,090✔
1643
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
861,262✔
1644
      ERASE_NODE(pPlan->pChildren);
843,604✔
1645
      continue;
843,604✔
1646
    }
1647
    WHERE_NEXT;
17,658✔
1648
  }
1649
  nodesClearList(pPlan->pChildren);
412,828✔
1650
  pPlan->pChildren = NULL;
412,828✔
1651

1652
  if (NULL == pPlan->pParents) {
412,828✔
1653
    goto _exit;
395,170✔
1654
  }
1655

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

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

1675
  return code;
412,828✔
1676
}
1677

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

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

1693
_exit:
395,170✔
1694

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

1699
  return code;
395,170✔
1700
}
1701

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

1715
  if (pDag->numOfSubplans <= 0) {
131,616✔
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) {
131,616✔
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);
131,616✔
1726
  if (levelNum <= 0) {
131,616✔
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;
131,616✔
1732
  
1733
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
131,616✔
1734
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
131,616✔
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);
131,616✔
1740
  if (taskNum != 1) {
131,616✔
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);
131,616✔
1746
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
131,616✔
1747
  
1748
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
526,464✔
1749
    totalTaskNum = 0;
394,848✔
1750

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

1757
    taskIdx = 0;
394,848✔
1758
    
1759
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
798,525✔
1760
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
403,677✔
1761
      if (NULL == plans) {
403,677✔
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)) {
403,677✔
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);
403,677✔
1772
      if (taskNum <= 0) {
403,677✔
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;
403,677✔
1778
      if (totalTaskNum > pDag->numOfSubplans) {
403,677✔
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) {
816,183✔
1784
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
412,506✔
1785
        pState = taosArrayReserve(deployList, 1);
412,506✔
1786

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

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

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

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

1817
    if (totalTaskNum != pDag->numOfSubplans) {
394,848✔
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));
394,848✔
1823

1824
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
394,848✔
1825

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

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

1834
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
526,464✔
1835
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
394,848✔
1836
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
394,848✔
1837
  }
1838
  
1839
  pInfo->runnerNum = totalTaskNum;
131,616✔
1840
  
1841
_exit:
131,616✔
1842

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

1847
  taosArrayDestroy(deployTaskList);
131,616✔
1848
  nodesDestroyNode((SNode *)pDag);
131,616✔
1849

1850
  return code;
131,616✔
1851
}
1852

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

1870
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
483✔
1871
    deployId = pAction->deployId[r];
322✔
1872

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

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

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

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

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

1895
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
322✔
1896
        if (pRunner->id.taskIdx != newTaskIdx) {
322✔
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;
322✔
1902

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

1913
        pRunner++;
322✔
1914
      }
1915

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

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

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

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

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

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

1931
_exit:
161✔
1932

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

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

1940
  return code;
161✔
1941
}
1942

1943

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

1952
_exit:
126,501✔
1953

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

1958
  return code;
126,501✔
1959
}
1960

1961

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

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

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

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

1984
  taosHashClear(mStreamMgmt.toUpdateScanMap);
131,616✔
1985
  mStreamMgmt.toUpdateScanNum = 0;
131,616✔
1986

1987
_exit:
131,616✔
1988

1989
  nodesDestroyNode((SNode *)pPlan);
131,616✔
1990

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

1995
  return code;
131,616✔
1996
}
1997

1998

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

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

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

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

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

2022
  return code;
134,642✔
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) {
129,527✔
2076
  int32_t code = TSDB_CODE_SUCCESS;
129,527✔
2077
  int32_t lino = 0;
129,527✔
2078
  int64_t streamId = pStream->pCreate->streamId;
129,527✔
2079

2080
  pStatus->lastActionTs = INT64_MIN;
129,527✔
2081

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

2087
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
129,527✔
2088
  
2089
  if (pStream->pCreate->numOfCalcSubplan > 0) {
129,527✔
2090
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
126,501✔
2091
    
2092
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
126,501✔
2093
  }
2094

2095
  if (initList) {
129,527✔
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:
129,527✔
2114

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

2119
  return code;
129,527✔
2120
}
2121

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

2128
  if (NULL == pStatus) {
134,642✔
2129
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
129,527✔
2130

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

2133
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
129,527✔
2134
  }
2135
  
2136
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
134,642✔
2137

2138
  mstLogSStmStatus("stream deployed", streamId, pStatus);
134,642✔
2139

2140
_exit:
134,642✔
2141

2142
  if (code) {
134,642✔
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;
134,642✔
2152
}
2153

2154

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

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

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

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

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

2182
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
10,619✔
2183
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
5,127✔
2184
    (void)mstWaitLock(&pSnode->lock, true);
5,127✔
2185

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

2199
    taskNum = taosArrayGetSize(pSnode->runnerList);
5,127✔
2200
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
5,127✔
2201
      for (int32_t i = 0; i < taskNum; ++i) {
26,910✔
2202
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
21,783✔
2203
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
21,783✔
2204
          continue;
20,334✔
2205
        }
2206
        
2207
        mstDestroySStmTaskToDeployExt(pExt);
1,449✔
2208
        pExt->deployed = true;
1,449✔
2209
      }
2210
    }
2211

2212
    taosRUnLockLatch(&pSnode->lock);
5,127✔
2213
  }
2214

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

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

2232
  size_t keyLen = 0;
5,492✔
2233
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
373,469✔
2234
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
367,977✔
2235
    if (*pStreamId == streamId) {
367,977✔
2236
      int64_t taskId = *(pStreamId + 1);
34,216✔
2237
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
34,216✔
2238
      if (code) {
34,216✔
2239
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2240
      } else {
2241
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
34,216✔
2242
      }
2243
    }
2244
  }
2245

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

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

2263
  mstResetSStmStatus(pStatus);
5,115✔
2264

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

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

2276
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
135,474✔
2277
  if (pStatus) {
135,474✔
2278
    stopped = atomic_load_8(&pStatus->stopped);
5,115✔
2279
    if (0 == stopped) {
5,115✔
2280
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
483✔
2281
      msmResetStreamForRedeploy(streamId, pStatus);
483✔
2282
    } else {
2283
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
4,632✔
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,632✔
2289
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
4,632✔
2290
        msmResetStreamForRedeploy(streamId, pStatus);
4,632✔
2291
      }
2292
    }
2293
  }
2294

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

2301
  TAOS_CHECK_EXIT(code);
134,642✔
2302

2303
  if (pStatus && pStream->pCreate->streamId != streamId) {
134,642✔
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);
134,642✔
2311
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
134,642✔
2312
  if (userStopped || userDropped) {
134,642✔
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));
134,642✔
2318

2319
_exit:
134,642✔
2320

2321
  mndReleaseStream(pCtx->pMnode, pStream);
134,642✔
2322

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

2327
  return code;
134,642✔
2328
}
2329

2330
static int32_t msmReLaunchReaderTask(SStreamObj* pStream, SStmTaskAction* pAction, SStmStatus* pStatus) {
1,288✔
2331
  int32_t code = TSDB_CODE_SUCCESS;
1,288✔
2332
  int32_t lino = 0;
1,288✔
2333
  int64_t streamId = pAction->streamId;
1,288✔
2334
  SStmTaskStatus** ppTask = taosHashGet(mStreamMgmt.taskMap, &pAction->streamId, sizeof(pAction->streamId) + sizeof(pAction->id.taskId));
1,288✔
2335
  if (NULL == ppTask) {
1,288✔
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};
1,288✔
2341
  info.task.type = pAction->type;
1,288✔
2342
  info.task.streamId = pAction->streamId;
1,288✔
2343
  info.task.taskId = pAction->id.taskId;
1,288✔
2344
  info.task.seriousId = (*ppTask)->id.seriousId;
1,288✔
2345
  info.task.nodeId = pAction->id.nodeId;
1,288✔
2346
  info.task.taskIdx = pAction->id.taskIdx;
1,288✔
2347
  
2348
  bool isTriggerReader = STREAM_IS_TRIGGER_READER(pAction->flag);
1,288✔
2349
  SStreamCalcScan* scanPlan = NULL;
1,288✔
2350
  if (!isTriggerReader) {
1,288✔
2351
    scanPlan = taosArrayGet(pStatus->pCreate->calcScanPlanList, pAction->id.taskIdx);
644✔
2352
    if (NULL == scanPlan) {
644✔
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));
1,288✔
2360
  TAOS_CHECK_EXIT(msmTDAddToVgroupMap(mStreamMgmt.toDeployVgMap, &info, pAction->streamId));
1,288✔
2361

2362
_exit:
1,288✔
2363

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

2368
  return code;
1,288✔
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) {
161✔
2413
  int32_t code = TSDB_CODE_SUCCESS;
161✔
2414
  int32_t lino = 0;
161✔
2415
  int64_t streamId = pAction->streamId;
161✔
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;
161✔
2430
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
161✔
2431
//  }
2432
  
2433
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
161✔
2434
  
2435
  SQueryPlan* pPlan = NULL;
161✔
2436
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
161✔
2437
  
2438
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
161✔
2439
  
2440
  taosHashClear(mStreamMgmt.toUpdateScanMap);
161✔
2441
  mStreamMgmt.toUpdateScanNum = 0;
161✔
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:
161✔
2462

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

2467
  return code;
161✔
2468
}
2469

2470

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

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

2480
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,449✔
2481
  if (NULL == pStatus) {
1,449✔
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,449✔
2487
  if (stopped) {
1,449✔
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,449✔
2493
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,449✔
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,449✔
2499

2500
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,449✔
2501
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,449✔
2502
  if (userStopped || userDropped) {
1,449✔
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,449✔
2508
    case STREAM_READER_TASK:
1,288✔
2509
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,288✔
2510
      break;
1,288✔
2511
/*
2512
    case STREAM_TRIGGER_TASK:
2513
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2514
      break;
2515
*/
2516
    case STREAM_RUNNER_TASK:
161✔
2517
      if (pAction->multiRunner) {
161✔
2518
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
161✔
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;
161✔
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,449✔
2531

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

2536
  if (code) {
1,449✔
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,449✔
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) {
377✔
2593
  int32_t code = TSDB_CODE_SUCCESS;
377✔
2594
  int32_t lino = 0;
377✔
2595

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

2598
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
377✔
2599

2600
_exit:
377✔
2601

2602
  if (code) {
377✔
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));
377✔
2606
  }
2607

2608
  return code;
377✔
2609
}
2610

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

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

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

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

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

2631
_exit:
×
2632

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

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

2639
  return;
10,150✔
2640
}
2641

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

2646
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,012✔
2647
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,012✔
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,012✔
2653
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,012✔
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,012✔
2660

2661
_exit:
3,012✔
2662

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

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

2669
  return code;
3,012✔
2670
}
2671

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

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

2693
_exit:
25,951✔
2694

2695
  if (code) {
25,951✔
2696
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2697
  }
2698
}
25,951✔
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) {
983,910✔
2738
  int32_t code = TSDB_CODE_SUCCESS;
983,910✔
2739
  int32_t lino = 0;
983,910✔
2740
  int64_t streamId = pDeploy->task.streamId;
983,910✔
2741
  
2742
  switch (pDeploy->task.type) {
983,910✔
2743
    case STREAM_READER_TASK:
438,372✔
2744
      if (NULL == pStream->readerTasks) {
438,372✔
2745
        pStream->streamId = streamId;
185,032✔
2746
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
185,032✔
2747
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
185,032✔
2748
      }
2749
      
2750
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
876,744✔
2751
      break;
438,372✔
2752
    case STREAM_TRIGGER_TASK:
134,159✔
2753
      pStream->streamId = streamId;
134,159✔
2754
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
134,159✔
2755
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
134,159✔
2756
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
134,159✔
2757
      break;
134,159✔
2758
    case STREAM_RUNNER_TASK:
411,379✔
2759
      if (NULL == pStream->runnerTasks) {
411,379✔
2760
        pStream->streamId = streamId;
135,931✔
2761
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
135,931✔
2762
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
135,931✔
2763
      }      
2764
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
822,758✔
2765
      break;
411,379✔
2766
    default:
×
2767
      break;
×
2768
  }
2769

2770
_exit:
983,910✔
2771

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

2776
  return code;
983,910✔
2777
}
2778

2779
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
983,910✔
2780
  int32_t code = TSDB_CODE_SUCCESS;
983,910✔
2781
  int32_t lino = 0;
983,910✔
2782
  int64_t streamId = pDeploy->task.streamId;
983,910✔
2783
  SStreamTask* pTask = &pDeploy->task;
983,910✔
2784
  SStmStreamDeploy streamDeploy = {0};
983,910✔
2785
  SStmStreamDeploy* pStream = NULL;
983,910✔
2786
   
2787
  while (true) {
2788
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
983,910✔
2789
    if (NULL == pStream) {
983,910✔
2790
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
192,603✔
2791
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
192,603✔
2792
      if (TSDB_CODE_SUCCESS == code) {
192,603✔
2793
        goto _exit;
192,603✔
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));
791,307✔
2805
    
2806
    break;
791,307✔
2807
  }
2808
  
2809
_exit:
983,910✔
2810

2811
  taosHashRelease(pHash, pStream);
983,910✔
2812

2813
  if (code) {
983,910✔
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);
983,910✔
2817
  }
2818

2819
  return code;
983,910✔
2820
}
2821

2822

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

2828
  for (int32_t i = 0; i < taskNum; ++i) {
1,119,337✔
2829
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
986,808✔
2830
    if (pExt->deployed) {
986,808✔
2831
      continue;
2,898✔
2832
    }
2833

2834
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
983,910✔
2835
    pExt->deployed = true;
983,910✔
2836

2837
    (void)atomic_add_fetch_32(deployed, 1);
983,910✔
2838
  }
2839

2840
_exit:
132,529✔
2841

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

2846
  return code;
132,529✔
2847
}
2848

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

2856
  mstDebug("start to add stream vgroup tasks deploy");
38,198✔
2857
  
2858
  for (int32_t i = 0; i < vgNum; ++i) {
258,330✔
2859
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
220,132✔
2860

2861
    msmUpdateVgroupUpTs(pCtx, *vgId);
220,132✔
2862

2863
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
220,132✔
2864
    if (NULL == pVg) {
220,132✔
2865
      continue;
143,096✔
2866
    }
2867

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

2881
_exit:
38,198✔
2882

2883
  if (code) {
38,198✔
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;
38,198✔
2892
}
2893

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

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

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

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

2919
_exit:
28,768✔
2920

2921
  if (code) {
28,768✔
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;
28,768✔
2930
}
2931

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

2943
_exit:
437,327✔
2944

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

2949
  return code;
437,327✔
2950
}
2951

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

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

2963
  while (1) {
192,603✔
2964
    pIter = taosHashIterate(pCtx->deployStm, pIter);
232,293✔
2965
    if (pIter == NULL) {
232,293✔
2966
      break;
39,690✔
2967
    }
2968
    
2969
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
192,603✔
2970
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
385,206✔
2971

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

2976
    mstClearSStmStreamDeploy(pDeploy);
192,603✔
2977
    
2978
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
192,603✔
2979
  }
2980
  
2981
_exit:
39,690✔
2982

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

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

2994
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
38,198✔
2995
  int32_t code = TSDB_CODE_SUCCESS;
38,198✔
2996
  int32_t lino = 0;
38,198✔
2997
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
38,198✔
2998
  SStmVgTasksToDeploy* pVg = NULL;
38,198✔
2999
  
3000
  for (int32_t i = 0; i < vgNum; ++i) {
258,330✔
3001
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
220,132✔
3002
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
220,132✔
3003
    if (NULL == pVg) {
220,132✔
3004
      continue;
143,096✔
3005
    }
3006

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

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

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

3035
      mstDestroySStmTaskToDeployExt(pExt);
1,932✔
3036

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

3045
_exit:
38,198✔
3046

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

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

3056
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
31,938✔
3057
  if (!GOT_SNODE(snodeId)) {
31,938✔
3058
    return;
966✔
3059
  }
3060
  
3061
  int32_t code = TSDB_CODE_SUCCESS;
30,972✔
3062
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
30,972✔
3063
  if (NULL == pSnode) {
30,972✔
3064
    return;
2,204✔
3065
  }
3066

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

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

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

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

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

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

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

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

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

3134
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
14,168,233✔
3135
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,168,233✔
3136
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
38,198✔
3137
  }
3138

3139
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
14,168,233✔
3140
    msmCleanDeployedSnodeTasks(pHb->snodeId);
31,938✔
3141
  }
3142
}
14,168,233✔
3143

3144
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
14,168,233✔
3145
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,168,233✔
3146
  if (mStreamMgmt.tCtx) {
14,168,233✔
3147
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
14,100,156✔
3148
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
14,100,156✔
3149
  }
3150
}
14,168,233✔
3151

3152
int32_t msmGrpAddActionStart(SHashObj* pHash, int64_t streamId, SStmTaskId* pId) {
188,554✔
3153
  int32_t code = TSDB_CODE_SUCCESS;
188,554✔
3154
  int32_t lino = 0;
188,554✔
3155
  int32_t action = STREAM_ACT_START;
188,554✔
3156
  SStmAction *pAction = taosHashGet(pHash, &streamId, sizeof(streamId));
188,554✔
3157
  if (pAction) {
188,554✔
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};
188,554✔
3163
    newAction.actions = action;
188,554✔
3164
    newAction.start.triggerId = *pId;
188,554✔
3165
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
188,554✔
3166
    mstsDebug("stream add START action, actions:%x", newAction.actions);
188,554✔
3167
  }
3168

3169
_exit:
188,554✔
3170

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

3175
  return code;
188,554✔
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) {
56,170✔
3206
  int32_t code = TSDB_CODE_SUCCESS;
56,170✔
3207
  int32_t lino = 0;
56,170✔
3208
  int32_t action = STREAM_ACT_UNDEPLOY;
56,170✔
3209
  bool    dropped = false;
56,170✔
3210

3211
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
56,170✔
3212
  mstsDebug("stream dropped: %d", dropped);
56,170✔
3213
  
3214
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
56,170✔
3215
  if (pAction) {
56,170✔
3216
    pAction->actions |= action;
37,692✔
3217
    if (NULL == pAction->undeploy.taskList) {
37,692✔
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);
75,384✔
3223
    if (pAction->undeploy.doCheckpoint) {
37,692✔
3224
      pAction->undeploy.doCheckpoint = dropped ? false : true;
18,699✔
3225
    }
3226
    if (!pAction->undeploy.doCleanup) {
37,692✔
3227
      pAction->undeploy.doCleanup = dropped ? true : false;
18,699✔
3228
    }
3229
    
3230
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
37,692✔
3231
  } else {
3232
    SStmAction newAction = {0};
18,478✔
3233
    newAction.actions = action;
18,478✔
3234
    newAction.undeploy.doCheckpoint = dropped ? false : true;
18,478✔
3235
    newAction.undeploy.doCleanup = dropped ? true : false;
18,478✔
3236
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
18,478✔
3237
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
18,478✔
3238
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
36,956✔
3239
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
18,478✔
3240
    
3241
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
18,478✔
3242
  }
3243

3244
_exit:
56,170✔
3245

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

3250
  return code;
56,170✔
3251
}
3252

3253
int32_t msmGrpAddActionRecalc(SStmGrpCtx* pCtx, int64_t streamId, SArray* recalcList) {
3,012✔
3254
  int32_t code = TSDB_CODE_SUCCESS;
3,012✔
3255
  int32_t lino = 0;
3,012✔
3256
  int32_t action = STREAM_ACT_RECALC;
3,012✔
3257
  SStmAction newAction = {0};
3,012✔
3258
  
3259
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
3,012✔
3260
  if (pAction) {
3,012✔
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,012✔
3267
    newAction.recalc.recalcList = recalcList;
3,012✔
3268
    
3269
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
3,012✔
3270
    
3271
    mstsDebug("stream add recalc action, listSize:%d", (int32_t)taosArrayGetSize(recalcList));
3,012✔
3272
  }
3273

3274
_exit:
3,012✔
3275

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

3281
  return code;
3,012✔
3282
}
3283

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

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

3294
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
250,895✔
3295
  for (int32_t i = 0; i < readerNum; ++i) {
550,785✔
3296
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
299,890✔
3297
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
299,890✔
3298
      return false;
×
3299
    }
3300
  }
3301

3302
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
250,895✔
3303
  for (int32_t i = 0; i < readerNum; ++i) {
306,468✔
3304
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
55,573✔
3305
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
55,573✔
3306
      return false;
×
3307
    }
3308
  }
3309

3310
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
250,895✔
3311
  SListNode* pNode = listHead(pStream->calcReaders);
250,895✔
3312
  for (int32_t i = 0; i < readerNum; ++i) {
774,703✔
3313
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
523,808✔
3314
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
523,808✔
3315
      return false;
×
3316
    }
3317
    pNode = TD_DLIST_NODE_NEXT(pNode);
523,808✔
3318
  }
3319

3320
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
798,995✔
3321
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
610,441✔
3322
    for (int32_t m = 0; m < runnerNum; ++m) {
1,175,197✔
3323
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
627,097✔
3324
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
627,097✔
3325
        return false;
62,341✔
3326
      }
3327
    }
3328
  }
3329
  
3330
  return true;
188,554✔
3331
}
3332

3333

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

3342
  msttDebug("start to handle task abnormal status %d", pTask->status);
8,231,049✔
3343
  
3344
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
8,231,049✔
3345
  if (NULL == pStatus) {
8,231,049✔
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;
10,293✔
3349
  }
3350

3351
  stopped = atomic_load_8(&pStatus->stopped);
8,231,049✔
3352
  if (stopped) {
8,231,049✔
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,231,049✔
3359
    case STREAM_STATUS_INIT:      
8,226,371✔
3360
      if (STREAM_TRIGGER_TASK != pMsg->type) {
8,226,371✔
3361
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
7,859,207✔
3362
        return;
7,859,207✔
3363
      }
3364
      
3365
      if (INT64_MIN == pStatus->lastActionTs) {
367,164✔
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) {
367,164✔
3371
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
116,269✔
3372
        return;
116,269✔
3373
      }
3374

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

3397
_exit:
255,573✔
3398

3399
  if (code) {
255,573✔
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) {
55,463✔
3406
  int32_t code = TSDB_CODE_SUCCESS;
55,463✔
3407
  int32_t lino = 0;
55,463✔
3408
  SStreamTask* pTask = (SStreamTask*)pStatus;
55,463✔
3409
  int64_t streamId = pStatus->streamId;
55,463✔
3410

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

3413
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
55,463✔
3414

3415
_exit:
55,463✔
3416

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

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

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

3446
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
2,822,975✔
3447
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,427,435✔
3448
    if (NULL == pStatus->detailStatus) {
1,427,435✔
3449
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
132,886✔
3450
      if (NULL == pStatus->detailStatus) {
132,886✔
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,427,435✔
3457
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,427,435✔
3458
  }
3459

3460
_exit:
1,395,540✔
3461

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

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

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

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

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

3494
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
21,508,782✔
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,508,782✔
3503
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,701,158✔
3504
        (*ppStatus)->runningStartTs = pCtx->currTs;
723,087✔
3505
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
978,071✔
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,508,782✔
3515
    (*ppStatus)->status = pTask->status;
21,508,782✔
3516
    (*ppStatus)->lastUpTs = pCtx->currTs;
21,508,782✔
3517
    
3518
    if (STREAM_STATUS_RUNNING != pTask->status) {
21,508,782✔
3519
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
8,231,049✔
3520
    }
3521
    
3522
    if (STREAM_TRIGGER_TASK == pTask->type) {
21,508,782✔
3523
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
2,822,975✔
3524
    }
3525
  }
3526

3527
_exit:
717,642✔
3528

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

3533
  return code;
717,642✔
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) {
188,554✔
3682
  int32_t code = TSDB_CODE_SUCCESS;
188,554✔
3683
  int32_t lino = 0;
188,554✔
3684
  if (NULL == pCtx->pRsp->start.taskList) {
188,554✔
3685
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
67,388✔
3686
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
67,388✔
3687
  }
3688

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

3698
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
377,108✔
3699
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
188,554✔
3700

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

3703
  return;
188,554✔
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) {
18,478✔
3712
  int32_t code = TSDB_CODE_SUCCESS;
18,478✔
3713
  int32_t lino = 0;
18,478✔
3714
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
18,478✔
3715
  if (NULL == pCtx->pRsp->undeploy.taskList) {
18,478✔
3716
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
15,421✔
3717
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
15,421✔
3718
  }
3719

3720
  SStreamTaskUndeploy undeploy;
18,478✔
3721
  for (int32_t i = 0; i < dropNum; ++i) {
74,648✔
3722
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
56,170✔
3723
    undeploy.task = *pTask;
56,170✔
3724
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
56,170✔
3725
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
56,170✔
3726

3727
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
112,340✔
3728
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
56,170✔
3729

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

3733
  return;
18,478✔
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,012✔
3780
  int32_t code = TSDB_CODE_SUCCESS;
3,012✔
3781
  int32_t lino = 0;
3,012✔
3782

3783
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,012✔
3784
  if (NULL == pStream) {
3,012✔
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,012✔
3790
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3791
    return;
×
3792
  }
3793

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

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

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

3808
_exit:
3,012✔
3809

3810
  if (code) {
3,012✔
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,012✔
3814
  }
3815
}
3816

3817

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

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

3826
  while (1) {
210,044✔
3827
    pIter = taosHashIterate(pCtx->actionStm, pIter);
295,364✔
3828
    if (pIter == NULL) {
295,364✔
3829
      break;
85,320✔
3830
    }
3831

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

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

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

3848
    if (STREAM_ACT_START & pAction->actions) {
191,566✔
3849
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
188,554✔
3850
    }
3851
  }
3852
  
3853
_exit:
85,320✔
3854

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

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

3863
  return code;
85,320✔
3864
}
3865

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

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

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

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

3894
        continue;
×
3895
      }
3896

3897
      return code;
1,244✔
3898
    }
3899

3900
    break;
3901
  }
3902

3903
_exit:
143,825✔
3904

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

3909
  return code;  
143,825✔
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) {
328✔
3960
  int32_t code = TSDB_CODE_SUCCESS;
328✔
3961
  int32_t lino = 0;
328✔
3962
  int32_t minVal = watchError ? 0 : 1;
328✔
3963

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

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

3972
  if (watchError) {
328✔
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)) {
328✔
3982
    mstInfo("no stream tasks remain during watch state");
328✔
3983
    goto _exit;
328✔
3984
  }
3985

3986
  msmWatchCheckStreamMap(pCtx);
×
3987

3988
_exit:
328✔
3989

3990
  mStreamMgmt.lastTaskId += 100000;
328✔
3991

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

3994
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
328✔
3995

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

4000
  return code;
328✔
4001
}
4002

4003

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

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

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

4020
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
11,552✔
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,552✔
4026
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
328✔
4027
  }
4028

4029
_exit:
11,552✔
4030

4031
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
11,552✔
4032
  
4033
  if (code) {
11,552✔
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,552✔
4040
}
4041

4042
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
4,838,685✔
4043
  int32_t listSize = taosArrayGetSize(pOReaders);
4,838,685✔
4044
  int32_t totalSize = 0;
4,838,685✔
4045
  
4046
  for (int32_t i = 0; i < listSize; ++i) {
5,826,577✔
4047
    SArray* pList = taosArrayGetP(pOReaders, i);
987,892✔
4048
    totalSize += taosArrayGetSize(pList);
987,892✔
4049
  }
4050

4051
  return totalSize;
4,838,685✔
4052
}
4053

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

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

4063

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

4068
  if (NULL == pStatus->trigOReaders) {
55,573✔
4069
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
48,106✔
4070
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
48,106✔
4071
  }
4072

4073
  while (true) {
48,106✔
4074
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
103,679✔
4075

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

4080
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
96,212✔
4081
      continue;
48,106✔
4082
    }
4083

4084
    *ppRes = *ppOReaderList;
55,573✔
4085
    break;
55,573✔
4086
  }
4087

4088
_exit:
55,573✔
4089

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

4094
  return code;
55,573✔
4095
}
4096

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

4103
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
64,508✔
4104
  for (int32_t i = 0; i < readerNum; ++i) {
135,132✔
4105
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
79,559✔
4106
    if (pReader->id.nodeId == vgId) {
79,559✔
4107
      readerExists = true;
8,935✔
4108
      break;
8,935✔
4109
    }
4110
  }
4111

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

4122
_exit:
8,935✔
4123

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

4128
  return code;
64,508✔
4129
}
4130

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

4149
  if (NULL == pCtx->pRsp->rsps.rspList) {
56,516✔
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);
56,516✔
4155
  rsp.task = *(SStreamTask*)pTask;
56,516✔
4156

4157
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
56,516✔
4158
  if (NULL == pStatus) {
56,516✔
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) {
56,516✔
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); 
56,516✔
4169

4170
  stopped = atomic_load_8(&pStatus->stopped);
56,516✔
4171
  if (stopped) {
56,516✔
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) {
56,516✔
4177
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4178
    goto _exit;
×
4179
  }
4180

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

4185
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
56,516✔
4186
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
56,516✔
4187
  
4188
  for (int32_t i = 0; i < tbNum; ++i) {
225,435✔
4189
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
168,919✔
4190
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
168,919✔
4191
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
337,838✔
4192
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
168,919✔
4193
  }
4194

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

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

4219
_exit:
56,516✔
4220

4221
  tFreeSStreamMgmtReq(pMgmtReq);
56,516✔
4222
  taosMemoryFree(pMgmtReq);
56,516✔
4223

4224
  tSimpleHashCleanup(pVgs);
56,516✔
4225
  mstDestroyDbVgroupsHash(pDbVgroups);
56,516✔
4226

4227
  if (code) {
56,516✔
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);
113,032✔
4235
  }
4236

4237
_final:
56,516✔
4238

4239
  if (finalCode) {
56,516✔
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;
56,516✔
4246
}
4247

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

4262
_exit:
2,123✔
4263

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

4268
  return code;
2,123✔
4269
}
4270

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

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

4290
  if (!readerExists) {
2,123✔
4291
    if (NULL == pStatus->calcReaders) {
2,123✔
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,123✔
4297
      pReader = (SStmTaskStatus*)pNode->data;
2,123✔
4298
      taskIdx = pReader->id.taskIdx + 1;
2,123✔
4299
    }
4300

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

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

4314
_exit:
2,123✔
4315

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

4320
  return code;
2,123✔
4321
}
4322

4323

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

4341
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,123✔
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,123✔
4347
  rsp.task = *(SStreamTask*)pTask;
2,123✔
4348

4349
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,123✔
4350
  if (NULL == pStatus) {
2,123✔
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,123✔
4356
  if (stopped) {
2,123✔
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,123✔
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,123✔
4367
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,123✔
4368

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

4384
_exit:
2,123✔
4385

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

4389
  if (code) {
2,123✔
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,246✔
4396
  }
4397

4398

4399
_final:
2,123✔
4400

4401
  if (finalCode) {
2,123✔
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,123✔
4408
}
4409

4410

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

4415
  switch (pTask->pMgmtReq->type) {
58,639✔
4416
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
56,516✔
4417
      msmDeployTriggerOrigReader(pCtx, pTask);
56,516✔
4418
      break;
56,516✔
4419
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,123✔
4420
      msmDeployRunnerOrigReader(pCtx, pTask);
2,123✔
4421
      break;
2,123✔
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:
58,639✔
4429

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

4434
  return code;
58,639✔
4435
}
4436

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

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

4462
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
58,639✔
4463
  }
4464

4465
_exit:
14,105✔
4466

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

4471
  return code;
14,105✔
4472
}
4473

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

4479
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
14,088,604✔
4480
  if (GOT_SNODE(pReq->snodeId)) {
13,944,779✔
4481
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
775,238✔
4482
  }
4483
  
4484
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
13,944,779✔
4485
    msmHandleStreamActions(pCtx);
25,951✔
4486
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
25,951✔
4487
  }
4488

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

4495
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,944,779✔
4496
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
38,198✔
4497
  } else {
4498
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
13,906,581✔
4499
  }
4500

4501
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
13,944,779✔
4502
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
30,972✔
4503
  }
4504

4505
  if (taosHashGetSize(pCtx->deployStm) > 0) {
13,944,779✔
4506
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
39,690✔
4507
  }
4508

4509
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,944,779✔
4510
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
717,642✔
4511
  }
4512

4513
  if (taosHashGetSize(pCtx->actionStm) > 0) {
13,944,779✔
4514
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
85,320✔
4515
  }
4516

4517
_exit:
13,944,779✔
4518

4519
  if (code) {
14,088,604✔
4520
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
143,825✔
4521
  }
4522

4523
  return code;
14,088,604✔
4524
}
4525

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

4531
  if (TSDB_CODE_SUCCESS != code) {
14,168,233✔
4532
    goto _exit;
143,825✔
4533
  }
4534

4535
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
14,024,408✔
4536
  if (code < 0) {
14,024,408✔
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));
14,024,408✔
4542
  if (buf == NULL) {
14,024,408✔
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;
14,024,408✔
4548
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
14,024,408✔
4549

4550
  SEncoder encoder;
14,022,254✔
4551
  tEncoderInit(&encoder, abuf, tlen);
14,024,408✔
4552
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
14,024,408✔
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);
14,024,408✔
4560

4561
_exit:
14,168,233✔
4562

4563
  pMsg->code = code;
14,168,233✔
4564
  pMsg->info = *pRpcInfo;
14,168,233✔
4565
  if (TSDB_CODE_SUCCESS == code) {
14,168,233✔
4566
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
14,024,408✔
4567
    pMsg->pCont = buf;
14,024,408✔
4568
  }
4569
}
14,168,233✔
4570

4571

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

4577
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
14,168,233✔
4578

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

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

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

4608
_exit:
14,168,233✔
4609

4610
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
14,168,233✔
4611

4612
  msmCleanStreamGrpCtx(pHb);
14,168,233✔
4613
  msmClearStreamToDeployMaps(pHb);
14,168,233✔
4614

4615
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
14,168,233✔
4616
  
4617
  tFreeSMStreamHbRspMsg(&rsp);
14,168,233✔
4618

4619
  return code;
14,168,233✔
4620
}
4621

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

4627
  mstInfo("start to process mnode become leader");
357,254✔
4628

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

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

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

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

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

4651
  streamRemoveVnodeLeader(MNODE_HANDLE);
435,285✔
4652

4653
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
435,285✔
4654
    taosWLockLatch(&mStreamMgmt.runtimeLock);
357,254✔
4655
    msmDestroyRuntimeInfo(pMnode);
357,254✔
4656
    mStreamMgmt.stat.inactiveTimes++;
357,254✔
4657
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
357,254✔
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,850✔
4672
  int32_t code = TSDB_CODE_SUCCESS;
6,850✔
4673
  int32_t lino = 0;
6,850✔
4674
  SStreamObj* pStream = pObj;
6,850✔
4675
  SSnodeObj* pSnode = p1;
6,850✔
4676
  SArray** ppRes = p2;
6,850✔
4677

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

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

4688
  return true;
6,850✔
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) {
37,050✔
4703
  int32_t code = TSDB_CODE_SUCCESS;
37,050✔
4704
  int32_t lino = 0;
37,050✔
4705
  
4706
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
37,050✔
4707
  TAOS_CHECK_EXIT(code);
37,050✔
4708

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

4715
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4716

4717
_exit:
37,050✔
4718

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

4723
  return code;
37,050✔
4724
}
4725

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

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

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

4755
    return true;
81,769✔
4756
  }
4757

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

4760
  return true;
403✔
4761
}
4762

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

4775
    pStatus = (SStmStatus*)pIter;
245,440✔
4776

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

4785
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
245,063✔
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)) {
245,063✔
4792
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
10,922✔
4793
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
7,223✔
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,223✔
4797
        continue;
7,223✔
4798
      }
4799

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

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

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

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

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

4829
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
2,428,454✔
4830
  for (int32_t i = 0; i < taskNum; ++i) {
4,971,751✔
4831
    SStmTaskStatus* pTask = *(pList + i);
2,543,458✔
4832

4833
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
2,543,458✔
4834
      continue;
50,848✔
4835
    }
4836
    
4837
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
2,493,408✔
4838
      continue;
2,491,959✔
4839
    }
4840

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

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

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

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

4866
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
95,042✔
4867
  void* pIter = NULL;
95,042✔
4868
  SStmVgStreamStatus* pVg = NULL;
95,042✔
4869
  int64_t streamId = 0;
95,042✔
4870
  
4871
  while (true) {
896,741✔
4872
    pIter = taosHashIterate(pStreams, pIter);
991,783✔
4873
    if (NULL == pIter) {
991,783✔
4874
      break;
95,042✔
4875
    }
4876

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

4880
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
896,741✔
4881
    if (taskNum > 0) {
896,741✔
4882
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
535,394✔
4883
    }
4884

4885
    taskNum = taosArrayGetSize(pVg->calcReaders);
896,741✔
4886
    if (taskNum > 0) {
896,741✔
4887
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
560,173✔
4888
    }
4889
  }
4890
}
95,042✔
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) {
227,865✔
4919
  void* pIter = NULL;
227,865✔
4920
  int32_t code = 0;
227,865✔
4921
  
4922
  while (true) {
896,223✔
4923
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,124,088✔
4924
    if (NULL == pIter) {
1,124,088✔
4925
      break;
227,865✔
4926
    }
4927

4928
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
896,223✔
4929
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
896,223✔
4930
      continue;
799,451✔
4931
    }
4932
    
4933
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
96,772✔
4934

4935
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
96,772✔
4936
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
1,730✔
4937
      if (NULL == pVgroup) {
1,730✔
4938
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
1,730✔
4939
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
1,730✔
4940
        if (code) {
1,730✔
4941
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4942
        }
4943
        continue;
1,730✔
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);
95,042✔
4954

4955
    msmCheckVgroupStreamStatus(pVg->streamTasks);
95,042✔
4956
  }
4957
}
227,865✔
4958

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

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

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

4987
  (void)msmSTAddSnodesToMap(pMnode);
805✔
4988

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

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

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

5031
  taosHashClear(pSnode->streamTasks);
805✔
5032
}
805✔
5033

5034

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

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

5049
    if (NULL != pSnode->trigger) {
350,156✔
5050
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
341,229✔
5051
    }
5052

5053
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,400,624✔
5054
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,050,468✔
5055
      if (taskNum > 0) {
1,050,468✔
5056
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
991,658✔
5057
      }
5058
    }
5059
  }
5060
}
31,084✔
5061

5062

5063
void msmCheckSnodeStatus(SMnode *pMnode) {
227,865✔
5064
  void* pIter = NULL;
227,865✔
5065
  
5066
  while (true) {
281,373✔
5067
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
509,238✔
5068
    if (NULL == pIter) {
509,238✔
5069
      break;
227,865✔
5070
    }
5071

5072
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
281,373✔
5073
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
281,373✔
5074
      continue;
246,429✔
5075
    }
5076

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

5095
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
31,084✔
5096
  }
5097
}
227,865✔
5098

5099

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

5103
  msmCheckVgroupStatus(pMnode);
227,865✔
5104
  msmCheckSnodeStatus(pMnode);
227,865✔
5105
}
227,865✔
5106

5107
void msmCheckSnodesState(SMnode *pMnode) {
227,865✔
5108
  if (!MST_READY_FOR_SNODE_LOOP()) {
227,865✔
5109
    return;
200,717✔
5110
  }
5111

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

5114
  void* pIter = NULL;
27,148✔
5115
  int32_t snodeId = 0;
27,148✔
5116
  while (true) {
29,742✔
5117
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
56,890✔
5118
    if (NULL == pIter) {
56,890✔
5119
      break;
27,148✔
5120
    }
5121

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

5127
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
161✔
5128
    if (NULL == pSnode->streamTasks) {
161✔
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", 
161✔
5135
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5136
    
5137
    msmHandleSnodeLost(pMnode, pSnode);
161✔
5138
  }
5139

5140
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
27,148✔
5141
}
5142

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

5150
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
8,000,313✔
5151
    mstTrace("ignore health check since no stream now");
7,544,583✔
5152
    return false;
7,544,583✔
5153
  }
5154

5155
  return true;
455,730✔
5156
}
5157

5158
void msmHealthCheck(SMnode *pMnode) {
7,774,338✔
5159
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,774,338✔
5160
    return;
7,546,473✔
5161
  }
5162

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

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

5180
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
227,865✔
5181

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

5185
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
798✔
5186
  SStreamObj *pStream = pObj;
798✔
5187
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
798✔
5188
    return true;
×
5189
  }
5190
  
5191
  pStream->updateTime = *(int64_t*)p1;
798✔
5192
  
5193
  (*(int32_t*)p2)++;
798✔
5194
  
5195
  return true;
798✔
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) {
357,254✔
5237
  int32_t code = TSDB_CODE_SUCCESS;
357,254✔
5238
  int32_t lino = 0;
357,254✔
5239
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
357,254✔
5240
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
357,254✔
5241
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
357,254✔
5242

5243
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
714,447✔
5244

5245
  mStreamMgmt.stat.activeTimes++;
357,254✔
5246
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
357,254✔
5247
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
357,254✔
5248
  if (NULL == mStreamMgmt.tCtx) {
357,254✔
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));
357,254✔
5255
  if (mStreamMgmt.actionQ == NULL) {
357,254✔
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));
357,254✔
5262
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
357,254✔
5263
  
5264
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
357,254✔
5265
  
5266
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,143,349✔
5267
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,786,095✔
5268

5269
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
10,716,570✔
5270
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
8,930,475✔
5271
      if (pCtx->deployStm[m] == NULL) {
8,930,475✔
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);
8,930,475✔
5277
      
5278
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
8,930,475✔
5279
      if (pCtx->actionStm[m] == NULL) {
8,930,475✔
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);
8,930,475✔
5285
    }
5286
  }
5287
  
5288
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
357,254✔
5289
  if (mStreamMgmt.streamMap == NULL) {
357,254✔
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);
357,254✔
5295
  
5296
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
357,254✔
5297
  if (mStreamMgmt.taskMap == NULL) {
357,254✔
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);
357,254✔
5304
  if (mStreamMgmt.vgroupMap == NULL) {
357,254✔
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);
357,254✔
5310

5311
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
357,254✔
5312
  if (mStreamMgmt.snodeMap == NULL) {
357,254✔
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);
357,254✔
5318
  
5319
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
357,254✔
5320
  if (mStreamMgmt.dnodeMap == NULL) {
357,254✔
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);
357,254✔
5327
  if (mStreamMgmt.toDeployVgMap == NULL) {
357,254✔
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);
357,254✔
5333
  
5334
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
357,254✔
5335
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
357,254✔
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);
357,254✔
5341

5342
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
357,254✔
5343
  if (mStreamMgmt.toUpdateScanMap == NULL) {
357,254✔
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);
357,254✔
5349

5350
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
357,254✔
5351
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
357,254✔
5352

5353
  mStreamMgmt.lastTaskId = 1;
357,254✔
5354

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

5358
  if (activeStreamNum > 0) {
357,254✔
5359
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
630✔
5360
  } else {
5361
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
356,624✔
5362
  }
5363

5364
_exit:
357,254✔
5365

5366
  if (code) {
357,254✔
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");
357,254✔
5371
  }
5372

5373
  return code;
357,254✔
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