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

taosdata / TDengine / #4873

04 Dec 2025 01:55AM UTC coverage: 64.558% (-0.1%) from 64.678%
#4873

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

719 of 2219 new or added lines in 36 files covered. (32.4%)

6363 existing lines in 135 files now uncovered.

159381 of 246882 relevant lines covered (64.56%)

108937395.15 hits per line

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

75.13
/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() {
838,670✔
34
  SStmQNode* pQNode = NULL;
838,670✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
838,670✔
37
    return;
419,335✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,096,305✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,577,830✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,481,525✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,481,525✔
51
  }
52
}
2,096,305✔
53

54
void msmDestroyThreadCtxs() {
838,670✔
55
  if (NULL == mStreamMgmt.tCtx) {
838,670✔
56
    return;
419,335✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,515,640✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,096,305✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
419,335✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
838,670✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
838,670✔
94
}
838,670✔
95

96

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

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

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

111
    pStatus = pStream;
2,745✔
112
  }
113

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

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

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

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

136
_exit:
391✔
137

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

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

145

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

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

200
  pSnode = NULL;
455,890✔
201

202
_exit:
455,890✔
203

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

208
  return code;
455,890✔
209
}
210

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

223
    code = taosHashPut(mStreamMgmt.dnodeMap, &pDnode->id, sizeof(pDnode->id), &lastUpTs, sizeof(lastUpTs));
1,114,529✔
224
    if (code && TSDB_CODE_DUP_KEY != code) {
1,114,529✔
225
      sdbRelease(pMnode->pSdb, pDnode);
×
226
      sdbCancelFetch(pMnode->pSdb, pIter);
×
227
      pDnode = NULL;
×
228
      TAOS_CHECK_EXIT(code);
×
229
    }
230

231
    code = TSDB_CODE_SUCCESS;
1,114,529✔
232
    sdbRelease(pMnode->pSdb, pDnode);
1,114,529✔
233
  }
234

235
  pDnode = NULL;
980,030✔
236

237
_exit:
980,030✔
238

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

243
  return code;
980,030✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
585,945✔
275
  int32_t code = TSDB_CODE_SUCCESS;
585,945✔
276
  int32_t lino = 0;
585,945✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
585,945✔
278
  if (NULL == pStream) {
585,945✔
279
    SStmVgStreamStatus stream = {0};
444,019✔
280
    if (trigReader) {
444,019✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
282,078✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
282,078✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
564,156✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
161,941✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
161,941✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
323,882✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
444,019✔
290
    goto _exit;
444,019✔
291
  }
292
  
293
  if (trigReader) {
141,926✔
294
    if (NULL == pStream->trigReaders) {
22,925✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
22,925✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
22,925✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
45,850✔
300
    goto _exit;
22,925✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
119,001✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
87,413✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
87,413✔
306
  }
307

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

310
_exit:
119,001✔
311

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

320
  return code;
585,945✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
94,513✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
94,513✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
94,513✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
491,432✔
339
  }
340
  
341
_exit:
584,893✔
342

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

350
  return code;
585,945✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
593,669✔
363
    if (NULL == pVg) {
593,669✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
133,158✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
133,158✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
266,316✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
133,158✔
368
      if (TSDB_CODE_SUCCESS == code) {
133,158✔
369
        goto _return;
133,158✔
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);
460,511✔
381
    if (NULL == pVg->taskList) {
460,511✔
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)) {
921,022✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
460,511✔
390
    
391
    taosHashRelease(pVgMap, pVg);
460,511✔
392
    break;
460,511✔
393
  }
394
  
395
_return:
593,669✔
396

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

404
  return code;
593,669✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
830,084✔
409
  int32_t code = TSDB_CODE_SUCCESS;
830,084✔
410
  int32_t lino = 0;
830,084✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
830,084✔
412
  if (NULL == pStream) {
830,084✔
413
    SStmSnodeStreamStatus stream = {0};
214,383✔
414
    if (deployId < 0) {
214,383✔
415
      stream.trigger = pStatus;
3,458✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
210,925✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
210,925✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
421,850✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
214,383✔
423
    goto _exit;
214,383✔
424
  }
425
  
426
  if (deployId < 0) {
615,701✔
427
    if (NULL != pStream->trigger) {
197,772✔
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;
197,772✔
433
    goto _exit;
197,772✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
417,929✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
384,677✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
384,677✔
439
  }
440

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

443
_exit:
417,929✔
444

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

453
  return code;
830,084✔
454
}
455

456

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

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

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

484
  return code;
830,084✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
201,230✔
551
}
552

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

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

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

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

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

614
  return code;
628,854✔
615
}
616

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

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

632
_exit:
595,602✔
633

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

638
  return code;
595,602✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
17,510✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
2,137✔
682

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

687
  return code;  
2,137✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
92,512✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,257,419✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
54,782,658✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
38,525,239✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
38,525,239✔
725
  }
726

727
_exit:
16,257,419✔
728

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

733
  return code;
16,257,419✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
375,049✔
739
  int32_t num = taosArrayGetSize(pList);
375,049✔
740
  for (int32_t i = 0; i < num; ++i) {
1,007,678✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
830,303✔
742
    if (pScan->readFromCache) {
830,303✔
743
      return pScan->scanPlan;
197,674✔
744
    }
745
  }
746

747
  return NULL;
177,375✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
593,669✔
775
}
776

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

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

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

805
_exit:
197,772✔
806

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

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

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

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

875
  SStreamTaskAddr addr;
197,548✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
201,230✔
877
  if (triggerReaderNum > 0) {
201,230✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
200,065✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
200,065✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
448,899✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
247,669✔
884
    addr.taskId = pStatus->id.taskId;
247,669✔
885
    addr.nodeId = pStatus->id.nodeId;
247,669✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
247,669✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
495,338✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
247,669✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
201,230✔
892
  pMsg->streamName = pInfo->streamName;
201,230✔
893

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

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

901
_exit:
197,772✔
902

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

912

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

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

944
_exit:
628,854✔
945

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

953

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

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

977

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

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

995
  return code;
796,832✔
996
}
997

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

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

1006

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

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

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

1027
    *alive = (pStatus->runnerThreadNum >= 0);
1,042,539✔
1028
    break;
1,042,539✔
1029
  }
1030

1031
_exit:
1,042,539✔
1032

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

1037
  return code;
1,042,539✔
1038
}
1039

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

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

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

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

1078
_exit:
×
1079

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

1084
  return 0;
×
1085
}
1086

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

1101
  int32_t snodeTarget = taosRand() % snodeNum;
605,480✔
1102

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

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

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

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

1140
_exit:
609,720✔
1141

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

1146
  if (0 == snodeId) {
609,720✔
1147
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
4,240✔
1148
  }
1149

1150
  return snodeId;
609,720✔
1151
}
1152

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

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

1164
_exit:
796,832✔
1165

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

1170
  return snodeId;
796,832✔
1171
}
1172

1173

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

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

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

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

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

1206
_exit:
201,230✔
1207

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

1212
  return code;
201,230✔
1213
}
1214

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

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

1241
_exit:
305,003✔
1242

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

1247
  return code;
305,003✔
1248
}
1249

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

1279
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
112,354✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
112,354✔
1281
      
1282
      void *pIter = NULL;
112,354✔
1283
      while (1) {
584,701✔
1284
        SStmTaskDeploy info = {0};
697,055✔
1285
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
697,055✔
1286
        if (pIter == NULL) {
697,055✔
1287
          break;
112,354✔
1288
        }
1289
      
1290
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
584,701✔
1291
          pState = taosArrayReserve(pInfo->trigReaders, 1);
159,958✔
1292

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

1302
        sdbRelease(pSdb, pVgroup);
584,701✔
1303
      }
1304
      break;
112,354✔
1305
    }
1306
    default:
1,165✔
1307
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,165✔
1308
      break;
1,165✔
1309
  }
1310

1311
_exit:
201,230✔
1312

1313
  mndReleaseDb(pCtx->pMnode, pDb);
201,230✔
1314

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

1319
  return code;
201,230✔
1320
}
1321

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

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

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

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

1363
  nodesDestroyNode((SNode*)pSubplan);
278,894✔
1364

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

1369
  return code;
278,894✔
1370
}
1371

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

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

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

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

1408
  return code;
92,962✔
1409
}
1410

1411

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

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

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

1440
_exit:
280,942✔
1441

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

1446
  return code;
280,942✔
1447
}
1448

1449

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

1459
  
1460
  for (int32_t i = 0; i < calcTasksNum; ++i) {
570,800✔
1461
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
369,570✔
1462
    if (pScan->readFromCache) {
369,570✔
1463
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
92,962✔
1464
      continue;
92,962✔
1465
    }
1466
    
1467
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
276,608✔
1468
    for (int32_t m = 0; m < vgNum; ++m) {
553,216✔
1469
      pState = tdListReserve(pInfo->calcReaders);
276,608✔
1470
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
276,608✔
1471

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

1477
_exit:
201,230✔
1478

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

1483
  return code;
201,230✔
1484
}
1485

1486

1487

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

1515
_exit:
2,286✔
1516

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

1521
  return code;
2,286✔
1522
}
1523

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

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

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

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

1558
  source.addr.epSet = pSrc->epset;
1,144,248✔
1559
  source.addr.nodeId = pSrc->vgId;
1,144,248✔
1560

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

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

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

1582
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1583
}
1584

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

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

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

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

1618
_exit:
628,854✔
1619

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

1624
  return code;
628,854✔
1625
}
1626

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

1635
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
628,854✔
1636

1637
  SNode* pTmp = NULL;
628,854✔
1638
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,773,102✔
1639
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,144,248✔
1640
      ERASE_NODE(pPlan->pChildren);
1,110,996✔
1641
      continue;
1,110,996✔
1642
    }
1643
    WHERE_NEXT;
33,252✔
1644
  }
1645
  nodesClearList(pPlan->pChildren);
628,854✔
1646
  pPlan->pChildren = NULL;
628,854✔
1647

1648
  if (NULL == pPlan->pParents) {
628,854✔
1649
    goto _exit;
595,602✔
1650
  }
1651

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

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

1671
  return code;
628,854✔
1672
}
1673

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

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

1689
_exit:
595,602✔
1690

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

1695
  return code;
595,602✔
1696
}
1697

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

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

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

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

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

1741
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
197,772✔
1742
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
197,772✔
1743
  
1744
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
791,088✔
1745
    totalTaskNum = 0;
593,316✔
1746

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

1753
    taskIdx = 0;
593,316✔
1754
    
1755
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,203,258✔
1756
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
609,942✔
1757
      if (NULL == plans) {
609,942✔
1758
        mstsError("empty level plan, level:%d", i);
×
1759
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1760
      }
1761

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

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

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

1779
      for (int32_t n = 0; n < taskNum; ++n) {
1,236,510✔
1780
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
626,568✔
1781
        pState = taosArrayReserve(deployList, 1);
626,568✔
1782

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

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

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

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

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

1818
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
593,316✔
1819

1820
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
593,316✔
1821

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

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

1830
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
791,088✔
1831
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
593,316✔
1832
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
593,316✔
1833
  }
1834
  
1835
  pInfo->runnerNum = totalTaskNum;
197,772✔
1836
  
1837
_exit:
197,772✔
1838

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

1843
  taosArrayDestroy(deployTaskList);
197,772✔
1844
  nodesDestroyNode((SNode *)pDag);
197,772✔
1845

1846
  return code;
197,772✔
1847
}
1848

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

1866
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
4,572✔
1867
    deployId = pAction->deployId[r];
2,286✔
1868

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

1871
    pStartRunner = pRunner;
2,286✔
1872
    totalTaskNum = 0;
2,286✔
1873

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

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

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

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

1897
        pRunner->id.nodeId = newNodeId;
2,286✔
1898

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

1909
        pRunner++;
2,286✔
1910
      }
1911

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

1915
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
2,286✔
1916

1917
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
2,286✔
1918

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

1921
    nodesDestroyNode((SNode *)pDag);
2,286✔
1922
    pDag = NULL;
2,286✔
1923

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

1927
_exit:
2,286✔
1928

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

1933
  nodesDestroyNode((SNode *)pDag);
2,286✔
1934
  taosArrayDestroy(deployTaskList);
2,286✔
1935

1936
  return code;
2,286✔
1937
}
1938

1939

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

1948
_exit:
184,261✔
1949

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

1954
  return code;
184,261✔
1955
}
1956

1957

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

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

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

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

1980
  taosHashClear(mStreamMgmt.toUpdateScanMap);
197,772✔
1981
  mStreamMgmt.toUpdateScanNum = 0;
197,772✔
1982

1983
_exit:
197,772✔
1984

1985
  nodesDestroyNode((SNode *)pPlan);
197,772✔
1986

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

1991
  return code;
197,772✔
1992
}
1993

1994

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

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

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

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

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

2018
  return code;
201,230✔
2019
}
2020

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

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

2060
_exit:
×
2061

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

2067
  return code;
×
2068
}
2069

2070

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

2076
  pStatus->lastActionTs = INT64_MIN;
187,719✔
2077

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

2083
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
187,719✔
2084
  
2085
  if (pStream->pCreate->numOfCalcSubplan > 0) {
187,719✔
2086
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
184,261✔
2087
    
2088
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
184,261✔
2089
  }
2090

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

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

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

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

2115
  return code;
187,719✔
2116
}
2117

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

2124
  if (NULL == pStatus) {
201,230✔
2125
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
187,719✔
2126

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

2129
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
187,719✔
2130
  }
2131
  
2132
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
201,230✔
2133

2134
  mstLogSStmStatus("stream deployed", streamId, pStatus);
201,230✔
2135

2136
_exit:
201,230✔
2137

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

2147
  return code;
201,230✔
2148
}
2149

2150

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

2155
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
22,233✔
2156
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
7,961✔
2157
    (void)mstWaitLock(&pVg->lock, true);
7,961✔
2158

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

2165
    for (int32_t i = 0; i < taskNum; ++i) {
41,575✔
2166
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
33,614✔
2167
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
33,614✔
2168
        continue;
33,614✔
2169
      }
2170

UNCOV
2171
      mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2172
      pExt->deployed = true;
×
2173
    }
2174
    
2175
    taosRUnLockLatch(&pVg->lock);
7,961✔
2176
  }
2177

2178
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
28,987✔
2179
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
14,715✔
2180
    (void)mstWaitLock(&pSnode->lock, true);
14,715✔
2181

2182
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
14,715✔
2183
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
14,715✔
2184
      for (int32_t i = 0; i < taskNum; ++i) {
26,543✔
2185
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
16,807✔
2186
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
16,807✔
2187
          continue;
16,807✔
2188
        }
2189
        
UNCOV
2190
        mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2191
        pExt->deployed = true;
×
2192
      }
2193
    }
2194

2195
    taskNum = taosArrayGetSize(pSnode->runnerList);
14,715✔
2196
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
14,715✔
2197
      for (int32_t i = 0; i < taskNum; ++i) {
65,950✔
2198
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
51,235✔
2199
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
51,235✔
2200
          continue;
51,235✔
2201
        }
2202
        
UNCOV
2203
        mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2204
        pExt->deployed = true;
×
2205
      }
2206
    }
2207

2208
    taosRUnLockLatch(&pSnode->lock);
14,715✔
2209
  }
2210

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

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

2228
  size_t keyLen = 0;
14,272✔
2229
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
950,225✔
2230
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
935,953✔
2231
    if (*pStreamId == streamId) {
935,953✔
2232
      int64_t taskId = *(pStreamId + 1);
88,774✔
2233
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
88,774✔
2234
      if (code) {
88,774✔
2235
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2236
      } else {
2237
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
88,774✔
2238
      }
2239
    }
2240
  }
2241

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

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

2259
  mstResetSStmStatus(pStatus);
13,511✔
2260

2261
  pStatus->deployTimes++;
13,511✔
2262
}
13,511✔
2263

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

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

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

2297
  TAOS_CHECK_EXIT(code);
201,230✔
2298

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

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

2315
_exit:
201,230✔
2316

2317
  mndReleaseStream(pCtx->pMnode, pStream);
201,230✔
2318

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

2323
  return code;
201,230✔
2324
}
2325

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

2358
_exit:
7,724✔
2359

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

2364
  return code;
7,724✔
2365
}
2366

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

2398
_exit:
2399

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

2404
  return code;
2405
}
2406
*/
2407

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

2457
_exit:
2,286✔
2458

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

2463
  return code;
2,286✔
2464
}
2465

2466

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

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

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

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

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

2494
  TAOS_CHECK_EXIT(code);
10,010✔
2495

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

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

2526
_exit:
10,010✔
2527

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

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

2537
  return code;
10,010✔
2538
}
2539

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

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

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

2585
  return TSDB_CODE_SUCCESS;
×
2586
}
2587

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

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

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

2596
_exit:
761✔
2597

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

2604
  return code;
761✔
2605
}
2606

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

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

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

2623
  atomic_store_8(&pStream->stopped, 2);
15,963✔
2624

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

2627
_exit:
×
2628

2629
  taosHashRelease(mStreamMgmt.streamMap, pStream);
16,333✔
2630

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

2635
  return;
16,333✔
2636
}
2637

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

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

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

2655
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
6,582✔
2656

2657
_exit:
6,582✔
2658

2659
  taosHashRelease(mStreamMgmt.streamMap, pStream);
6,582✔
2660

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

2665
  return code;
6,582✔
2666
}
2667

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

2673
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
263,845✔
2674
    switch (pQNode->type) {
213,421✔
2675
      case STREAM_ACT_DEPLOY:
213,421✔
2676
        if (pQNode->streamAct) {
213,421✔
2677
          mstDebug("start to handle stream deploy action");
203,411✔
2678
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
203,411✔
2679
        } else {
2680
          mstDebug("start to handle task deploy action");
10,010✔
2681
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
10,010✔
2682
        }
2683
        break;
213,421✔
2684
      default:
×
2685
        break;
×
2686
    }
2687
  }
2688

2689
_exit:
50,424✔
2690

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

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

2707
    pStatus = (SStmStatus*)pIter;
×
2708

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

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

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

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

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

2726
  msmStopAllStreamsByGrant(errCode);
×
2727

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

2733
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,422,939✔
2734
  int32_t code = TSDB_CODE_SUCCESS;
1,422,939✔
2735
  int32_t lino = 0;
1,422,939✔
2736
  int64_t streamId = pDeploy->task.streamId;
1,422,939✔
2737
  
2738
  switch (pDeploy->task.type) {
1,422,939✔
2739
    case STREAM_READER_TASK:
593,669✔
2740
      if (NULL == pStream->readerTasks) {
593,669✔
2741
        pStream->streamId = streamId;
258,712✔
2742
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
258,712✔
2743
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
258,712✔
2744
      }
2745
      
2746
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,187,338✔
2747
      break;
593,669✔
2748
    case STREAM_TRIGGER_TASK:
201,230✔
2749
      pStream->streamId = streamId;
201,230✔
2750
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
201,230✔
2751
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
201,230✔
2752
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
201,230✔
2753
      break;
201,230✔
2754
    case STREAM_RUNNER_TASK:
628,040✔
2755
      if (NULL == pStream->runnerTasks) {
628,040✔
2756
        pStream->streamId = streamId;
211,990✔
2757
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
211,990✔
2758
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
211,990✔
2759
      }      
2760
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,256,080✔
2761
      break;
628,040✔
2762
    default:
×
2763
      break;
×
2764
  }
2765

2766
_exit:
1,422,939✔
2767

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

2772
  return code;
1,422,939✔
2773
}
2774

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

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

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

2800
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,142,959✔
2801
    
2802
    break;
1,142,959✔
2803
  }
2804
  
2805
_exit:
1,422,939✔
2806

2807
  taosHashRelease(pHash, pStream);
1,422,939✔
2808

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

2815
  return code;
1,422,939✔
2816
}
2817

2818

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

2824
  for (int32_t i = 0; i < taskNum; ++i) {
1,664,794✔
2825
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,422,939✔
2826
    if (pExt->deployed) {
1,422,939✔
UNCOV
2827
      continue;
×
2828
    }
2829

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

2833
    (void)atomic_add_fetch_32(deployed, 1);
1,422,939✔
2834
  }
2835

2836
_exit:
241,855✔
2837

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

2842
  return code;
241,855✔
2843
}
2844

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

2852
  mstDebug("start to add stream vgroup tasks deploy");
71,566✔
2853
  
2854
  for (int32_t i = 0; i < vgNum; ++i) {
435,743✔
2855
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
364,177✔
2856

2857
    msmUpdateVgroupUpTs(pCtx, *vgId);
364,177✔
2858

2859
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
364,177✔
2860
    if (NULL == pVg) {
364,177✔
2861
      continue;
231,019✔
2862
    }
2863

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

2877
_exit:
71,566✔
2878

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

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

2887
  return code;
71,566✔
2888
}
2889

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

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

2903
  (void)mstWaitLock(&pSnode->lock, false);
56,890✔
2904
  
2905
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
56,890✔
2906
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
51,807✔
2907
  }
2908

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

2915
_exit:
56,890✔
2916

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

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

2925
  return code;
56,890✔
2926
}
2927

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

2939
_exit:
666,158✔
2940

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

2945
  return code;
666,158✔
2946
}
2947

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

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

2959
  while (1) {
279,980✔
2960
    pIter = taosHashIterate(pCtx->deployStm, pIter);
353,070✔
2961
    if (pIter == NULL) {
353,070✔
2962
      break;
73,090✔
2963
    }
2964
    
2965
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
279,980✔
2966
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
559,960✔
2967

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

2972
    mstClearSStmStreamDeploy(pDeploy);
279,980✔
2973
    
2974
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
279,980✔
2975
  }
2976
  
2977
_exit:
73,090✔
2978

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

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

2990
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
71,921✔
2991
  int32_t code = TSDB_CODE_SUCCESS;
71,921✔
2992
  int32_t lino = 0;
71,921✔
2993
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
71,921✔
2994
  SStmVgTasksToDeploy* pVg = NULL;
71,921✔
2995
  
2996
  for (int32_t i = 0; i < vgNum; ++i) {
436,098✔
2997
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
364,177✔
2998
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
364,177✔
2999
    if (NULL == pVg) {
364,177✔
3000
      continue;
231,019✔
3001
    }
3002

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

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

UNCOV
3025
    for (int32_t m = taskNum - 1; m >= 0; --m) {
×
UNCOV
3026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
×
UNCOV
3027
      if (!pExt->deployed) {
×
UNCOV
3028
        continue;
×
3029
      }
3030

UNCOV
3031
      mstDestroySStmTaskToDeployExt(pExt);
×
3032

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

3041
_exit:
71,921✔
3042

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

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

3052
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
121,542✔
3053
  if (!GOT_SNODE(snodeId)) {
121,542✔
3054
    return;
12,002✔
3055
  }
3056
  
3057
  int32_t code = TSDB_CODE_SUCCESS;
109,540✔
3058
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
109,540✔
3059
  if (NULL == pSnode) {
109,540✔
3060
    return;
52,650✔
3061
  }
3062

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

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

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

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

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

UNCOV
3096
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
×
UNCOV
3097
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
×
UNCOV
3098
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
×
UNCOV
3099
      if (!pExt->deployed) {
×
3100
        continue;
×
3101
      }
3102

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

UNCOV
3111
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
×
UNCOV
3112
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
×
UNCOV
3113
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
×
UNCOV
3114
      if (!pExt->deployed) {
×
3115
        continue;
×
3116
      }
3117

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

3130
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,731,471✔
3131
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,731,471✔
3132
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
71,921✔
3133
  }
3134

3135
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,731,471✔
3136
    msmCleanDeployedSnodeTasks(pHb->snodeId);
121,542✔
3137
  }
3138
}
16,731,471✔
3139

3140
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
16,731,471✔
3141
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,731,471✔
3142
  if (mStreamMgmt.tCtx) {
16,731,471✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
16,530,650✔
3144
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
16,530,650✔
3145
  }
3146
}
16,731,471✔
3147

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

3165
_exit:
265,173✔
3166

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

3171
  return code;
265,173✔
3172
}
3173

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

3190
_exit:
×
3191

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

3196
  return code;
×
3197
}
3198

3199

3200

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

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

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

3240
_exit:
121,005✔
3241

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

3246
  return code;
121,005✔
3247
}
3248

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

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

3270
_exit:
6,152✔
3271

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

3277
  return code;
6,152✔
3278
}
3279

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

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

3290
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
349,950✔
3291
  for (int32_t i = 0; i < readerNum; ++i) {
767,951✔
3292
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
418,001✔
3293
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
418,001✔
3294
      return false;
×
3295
    }
3296
  }
3297

3298
  readerNum = taosArrayGetSize(pStream->trigOReaders);
349,950✔
3299
  for (int32_t i = 0; i < readerNum; ++i) {
407,284✔
3300
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
57,334✔
3301
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
57,334✔
3302
      return false;
×
3303
    }
3304
  }
3305

3306
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
349,950✔
3307
  SListNode* pNode = listHead(pStream->calcReaders);
349,950✔
3308
  for (int32_t i = 0; i < readerNum; ++i) {
982,711✔
3309
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
632,761✔
3310
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
632,761✔
3311
      return false;
×
3312
    }
3313
    pNode = TD_DLIST_NODE_NEXT(pNode);
632,761✔
3314
  }
3315

3316
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,137,985✔
3317
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
872,812✔
3318
    for (int32_t m = 0; m < runnerNum; ++m) {
1,694,099✔
3319
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
906,064✔
3320
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
906,064✔
3321
        return false;
84,777✔
3322
      }
3323
    }
3324
  }
3325
  
3326
  return true;
265,173✔
3327
}
3328

3329

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

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

3347
  stopped = atomic_load_8(&pStatus->stopped);
13,135,449✔
3348
  if (stopped) {
13,135,449✔
3349
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3350
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3351
    return;
×
3352
  }
3353
  
3354
  switch (pMsg->status) {
13,135,449✔
3355
    case STREAM_STATUS_INIT:      
13,126,628✔
3356
      if (STREAM_TRIGGER_TASK != pMsg->type) {
13,126,628✔
3357
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
12,606,967✔
3358
        return;
12,606,967✔
3359
      }
3360
      
3361
      if (INT64_MIN == pStatus->lastActionTs) {
519,661✔
3362
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3363
        return;
×
3364
      }
3365
      
3366
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
519,661✔
3367
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
169,711✔
3368
        return;
169,711✔
3369
      }
3370

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

3393
_exit:
358,771✔
3394

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

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

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

3409
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
121,005✔
3410

3411
_exit:
121,005✔
3412

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

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

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

3442
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
4,853,405✔
3443
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,436,638✔
3444
    if (NULL == pStatus->detailStatus) {
2,436,638✔
3445
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
200,097✔
3446
      if (NULL == pStatus->detailStatus) {
200,097✔
UNCOV
3447
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
UNCOV
3448
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3449
      }
3450
    }
3451
    
3452
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,436,638✔
3453
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,436,638✔
3454
  }
3455

3456
_exit:
2,416,767✔
3457

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

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

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

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

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

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

3498
    if ((*ppStatus)->status != pTask->status) {
36,111,441✔
3499
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,447,955✔
3500
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,030,280✔
3501
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,417,675✔
3502
        if (pStream->triggerTask) {
×
UNCOV
3503
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3504
        }
3505
        
UNCOV
3506
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3507
      }
3508
    }
3509
    
3510
    (*ppStatus)->errCode = pTask->errorCode;
36,111,441✔
3511
    (*ppStatus)->status = pTask->status;
36,111,441✔
3512
    (*ppStatus)->lastUpTs = pCtx->currTs;
36,111,441✔
3513
    
3514
    if (STREAM_STATUS_RUNNING != pTask->status) {
36,111,441✔
3515
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
13,135,449✔
3516
    }
3517
    
3518
    if (STREAM_TRIGGER_TASK == pTask->type) {
36,111,441✔
3519
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
4,853,405✔
3520
    }
3521
  }
3522

3523
_exit:
1,522,368✔
3524

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

3529
  return code;
1,522,368✔
3530
}
3531

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

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

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

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

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

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

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

3629
_exit:
×
3630

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

UNCOV
3637
  return code;
×
3638
}
3639

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

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

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

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

3668
_exit:
×
3669

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

UNCOV
3674
  return code;
×
3675
}
3676

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

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

3694
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
530,346✔
3695
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
265,173✔
3696

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

3699
  return;
265,173✔
3700

UNCOV
3701
_exit:
×
3702

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

3706

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

3716
  SStreamTaskUndeploy undeploy;
42,220✔
3717
  for (int32_t i = 0; i < dropNum; ++i) {
163,225✔
3718
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
121,005✔
3719
    undeploy.task = *pTask;
121,005✔
3720
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
121,005✔
3721
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
121,005✔
3722

3723
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
242,010✔
3724
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
121,005✔
3725

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

3729
  return;
42,220✔
3730

UNCOV
3731
_exit:
×
3732

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

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

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

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

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

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

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

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

3766
_exit:
×
3767

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

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

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

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

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

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

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

3804
_exit:
6,152✔
3805

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

3813

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

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

3822
  while (1) {
313,545✔
3823
    pIter = taosHashIterate(pCtx->actionStm, pIter);
469,995✔
3824
    if (pIter == NULL) {
469,995✔
3825
      break;
156,450✔
3826
    }
3827

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

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

3840
    if (STREAM_ACT_RECALC & pAction->actions) {
271,325✔
3841
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
6,152✔
3842
    }
3843

3844
    if (STREAM_ACT_START & pAction->actions) {
271,325✔
3845
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
265,173✔
3846
    }
3847
  }
3848
  
3849
_exit:
156,450✔
3850

3851
  if (pIter) {
156,450✔
UNCOV
3852
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3853
  }
3854

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

3859
  return code;
156,450✔
3860
}
3861

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

3868
  while (true) {
3869
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
17,091,345✔
3870
    if (NULL == lastTs) {
17,091,345✔
3871
      if (noExists) {
759,199✔
3872
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
198,504✔
3873
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
198,504✔
3874
      }
3875

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

UNCOV
3882
    while (true) {
×
3883
      int64_t lastTsValue = atomic_load_64(lastTs);
16,332,146✔
3884
      if (pCtx->currTs > lastTsValue) {
16,332,146✔
3885
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
16,329,502✔
3886
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
16,329,502✔
3887
          return code;
16,329,502✔
3888
        }
3889

UNCOV
3890
        continue;
×
3891
      }
3892

3893
      return code;
2,644✔
3894
    }
3895

3896
    break;
3897
  }
3898

3899
_exit:
198,504✔
3900

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

3905
  return code;  
198,504✔
3906
}
3907

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

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

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

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

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

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

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

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

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

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

UNCOV
3982
  msmWatchCheckStreamMap(pCtx);
×
3983

3984
_exit:
256✔
3985

3986
  mStreamMgmt.lastTaskId += 100000;
256✔
3987

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

3990
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
256✔
3991

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

3996
  return code;
256✔
3997
}
3998

3999

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

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

4011
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
1,024✔
4012
  if (GOT_SNODE(pReq->snodeId)) {
1,024✔
4013
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,024✔
4014
  }
4015

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

4021
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
1,024✔
4022
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
256✔
4023
  }
4024

4025
_exit:
1,024✔
4026

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

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

4035
  return code;
1,024✔
4036
}
4037

4038
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
74,574✔
4039
  int32_t code = TSDB_CODE_SUCCESS;
74,574✔
4040
  int32_t lino = 0;
74,574✔
4041
  bool    readerExists = false;
74,574✔
4042
  int64_t streamId = pTask->streamId;
74,574✔
4043

4044
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
74,574✔
4045
  for (int32_t i = 0; i < readerNum; ++i) {
147,948✔
4046
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
90,614✔
4047
    if (pReader->id.nodeId == vgId) {
90,614✔
4048
      readerExists = true;
17,240✔
4049
      break;
17,240✔
4050
    }
4051
  }
4052

4053
  if (!readerExists) {
74,574✔
4054
    if (NULL == pStatus->trigOReaders) {
57,334✔
4055
      pStatus->trigOReaders = taosArrayInit(vgNum, sizeof(SStmTaskStatus));
49,286✔
4056
      TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
49,286✔
4057
    }
4058
    
4059
    SStmTaskStatus* pState = taosArrayReserve(pStatus->trigOReaders, 1);
57,334✔
4060
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
57,334✔
4061
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
57,334✔
4062
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
57,334✔
4063
  }
4064

4065
_exit:
74,574✔
4066

4067
  if (code) {
74,574✔
UNCOV
4068
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4069
  }
4070

4071
  return code;
74,574✔
4072
}
4073

4074
int32_t msmDeployTriggerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
65,786✔
4075
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
65,786✔
4076
  int32_t lino = 0;
65,786✔
4077
  int32_t vgId = 0;
65,786✔
4078
  int64_t streamId = pTask->streamId;
65,786✔
4079
  SArray* pTbs = pTask->pMgmtReq->cont.pReqs;
65,786✔
4080
  int32_t tbNum = taosArrayGetSize(pTbs);
65,786✔
4081
  SStreamDbTableName* pName = NULL;
65,786✔
4082
  SSHashObj* pDbVgroups = NULL;
65,786✔
4083
  SStreamMgmtRsp rsp = {0};
65,786✔
4084
  rsp.reqId = pTask->pMgmtReq->reqId;
65,786✔
4085
  rsp.header.msgType = STREAM_MSG_ORIGTBL_READER_INFO;
65,786✔
4086
  int32_t iter = 0;
65,786✔
4087
  void* p = NULL;
65,786✔
4088
  SSHashObj* pVgs = NULL;
65,786✔
4089
  SStreamMgmtReq* pMgmtReq = NULL;
65,786✔
4090
  int8_t stopped = 0;
65,786✔
4091

4092
  if (NULL == pCtx->pRsp->rsps.rspList) {
65,786✔
UNCOV
4093
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
UNCOV
4094
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4095
  }
4096
  
4097
  TSWAP(pTask->pMgmtReq, pMgmtReq);
65,786✔
4098
  rsp.task = *(SStreamTask*)pTask;
65,786✔
4099

4100
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
65,786✔
4101
  if (NULL == pStatus) {
65,786✔
UNCOV
4102
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
4103
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4104
  }
4105

4106
  stopped = atomic_load_8(&pStatus->stopped);
65,786✔
4107
  if (stopped) {
65,786✔
UNCOV
4108
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4109
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4110
  }
4111

4112
  if (tbNum <= 0) {
65,786✔
UNCOV
4113
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
UNCOV
4114
    goto _exit;
×
4115
  }
4116

4117
  int32_t oReaderNum = taosArrayGetSize(pStatus->trigOReaders);
65,786✔
4118
  if (oReaderNum > 0) {
65,786✔
UNCOV
4119
    mstsWarn("origReaders already exits, num:%d", oReaderNum);
×
4120
    goto _exit;
×
4121
  }
4122

4123
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
65,786✔
4124
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
65,786✔
4125
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
65,786✔
4126

4127
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
65,786✔
4128
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
65,786✔
4129
  
4130
  for (int32_t i = 0; i < tbNum; ++i) {
258,589✔
4131
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
192,803✔
4132
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
192,803✔
4133
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
385,606✔
4134
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
192,803✔
4135
  }
4136

4137
  int32_t vgNum = tSimpleHashGetSize(pVgs);
65,786✔
4138
  while (true) {
4139
    p = tSimpleHashIterate(pVgs, p, &iter);
140,360✔
4140
    if (NULL == p) {
140,360✔
4141
      break;
65,786✔
4142
    }
4143
    
4144
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
74,574✔
4145
  }
4146
  
4147
  vgNum = taosArrayGetSize(pStatus->trigOReaders);
65,786✔
4148
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
65,786✔
4149
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
65,786✔
4150

4151
  SStreamTaskAddr addr;
65,786✔
4152
  for (int32_t i = 0; i < vgNum; ++i) {
123,120✔
4153
    SStmTaskStatus* pOTask = taosArrayGet(pStatus->trigOReaders, i);
57,334✔
4154
    addr.taskId = pOTask->id.taskId;
57,334✔
4155
    addr.nodeId = pOTask->id.nodeId;
57,334✔
4156
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
57,334✔
4157
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
114,668✔
4158
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
57,334✔
4159
  }
4160

4161
_exit:
65,786✔
4162

4163
  tFreeSStreamMgmtReq(pMgmtReq);
65,786✔
4164
  taosMemoryFree(pMgmtReq);
65,786✔
4165

4166
  tSimpleHashCleanup(pVgs);
65,786✔
4167
  mstDestroyDbVgroupsHash(pDbVgroups);
65,786✔
4168

4169
  if (code) {
65,786✔
UNCOV
4170
    rsp.code = code;
×
4171
    
UNCOV
4172
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4173

UNCOV
4174
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4175
  } else {
4176
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
131,572✔
4177
  }
4178

4179
_final:
65,786✔
4180

4181
  if (finalCode) {
65,786✔
UNCOV
4182
    tFreeSStreamMgmtRsp(&rsp);
×
UNCOV
4183
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
UNCOV
4184
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4185
  }
4186

4187
  return finalCode;
65,786✔
4188
}
4189

4190
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
4,334✔
4191
  int32_t num = taosArrayGetSize(pList);
4,334✔
4192
  SStreamCalcScan* pScan = NULL;
4,334✔
4193
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
4,334✔
4194
  int64_t planUid = 0;
4,334✔
4195
  for (int32_t i = 0; i < num; ++i) {
4,334✔
4196
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
4,334✔
4197
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
4,334✔
4198
    if (0 != planUid && planUid == uid) {
4,334✔
4199
      *ppRes = pScan;
4,334✔
4200
      break;
4,334✔
4201
    }
4202
  }
4203

4204
_exit:
4,334✔
4205

4206
  if (code) {
4,334✔
UNCOV
4207
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4208
  }
4209

4210
  return code;
4,334✔
4211
}
4212

4213
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
4,334✔
4214
  int32_t code = TSDB_CODE_SUCCESS;
4,334✔
4215
  int32_t lino = 0;
4,334✔
4216
  bool    readerExists = false;
4,334✔
4217
  int64_t streamId = pTask->streamId;
4,334✔
4218
  SListNode* pNode = listHead(pStatus->calcReaders);
4,334✔
4219
  SStmTaskStatus* pReader = NULL;
4,334✔
4220
  int32_t taskIdx = 0;
4,334✔
4221

4222
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
4,334✔
4223
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
40,188✔
4224
    pReader = (SStmTaskStatus*)pNode->data;
35,854✔
4225
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
35,854✔
UNCOV
4226
      readerExists = true;
×
UNCOV
4227
      pAddr->taskId = pReader->id.taskId;
×
UNCOV
4228
      break;
×
4229
    }
4230
  }
4231

4232
  if (!readerExists) {
4,334✔
4233
    if (NULL == pStatus->calcReaders) {
4,334✔
4234
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4235
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
UNCOV
4236
      taskIdx = 0;
×
4237
    } else {
4238
      pNode = listTail(pStatus->calcReaders);
4,334✔
4239
      pReader = (SStmTaskStatus*)pNode->data;
4,334✔
4240
      taskIdx = pReader->id.taskIdx + 1;
4,334✔
4241
    }
4242

4243
    SStreamCalcScan* pScan = NULL;
4,334✔
4244
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
4,334✔
4245
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
4,334✔
4246
    pReader = tdListReserve(pStatus->calcReaders);
4,334✔
4247
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
4,334✔
4248
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
4,334✔
4249
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
4,334✔
4250
    pAddr->taskId = pReader->id.taskId;
4,334✔
4251
  }
4252

4253
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
4,334✔
4254
  pAddr->nodeId = vgId;
4,334✔
4255

4256
_exit:
4,334✔
4257

4258
  if (code) {
4,334✔
UNCOV
4259
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4260
  }
4261

4262
  return code;
4,334✔
4263
}
4264

4265

4266
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
4,334✔
4267
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
4,334✔
4268
  int32_t lino = 0;
4,334✔
4269
  int32_t vgId = 0;
4,334✔
4270
  int64_t streamId = pTask->streamId;
4,334✔
4271
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
4,334✔
4272
  int32_t reqNum = taosArrayGetSize(pReqs);
4,334✔
4273
  SStreamOReaderDeployReq* pReq = NULL;
4,334✔
4274
  SStreamOReaderDeployRsp* pRsp = NULL;
4,334✔
4275
  SStreamMgmtRsp rsp = {0};
4,334✔
4276
  rsp.reqId = pTask->pMgmtReq->reqId;
4,334✔
4277
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
4,334✔
4278
  SStreamMgmtReq* pMgmtReq = NULL;
4,334✔
4279
  int8_t stopped = 0;
4,334✔
4280
  int32_t vgNum = 0;
4,334✔
4281
  SStreamTaskAddr* pAddr = NULL;
4,334✔
4282

4283
  if (NULL == pCtx->pRsp->rsps.rspList) {
4,334✔
UNCOV
4284
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
UNCOV
4285
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4286
  }
4287
  
4288
  TSWAP(pTask->pMgmtReq, pMgmtReq);
4,334✔
4289
  rsp.task = *(SStreamTask*)pTask;
4,334✔
4290

4291
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
4,334✔
4292
  if (NULL == pStatus) {
4,334✔
UNCOV
4293
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
4294
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4295
  }
4296

4297
  stopped = atomic_load_8(&pStatus->stopped);
4,334✔
4298
  if (stopped) {
4,334✔
UNCOV
4299
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4300
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4301
  }
4302

4303
  if (reqNum <= 0) {
4,334✔
UNCOV
4304
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
UNCOV
4305
    goto _exit;
×
4306
  }
4307

4308
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
4,334✔
4309
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
4,334✔
4310

4311
  for (int32_t i = 0; i < reqNum; ++i) {
8,668✔
4312
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
4,334✔
4313
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
4,334✔
4314
    pRsp->execId = pReq->execId;
4,334✔
4315
    vgNum = taosArrayGetSize(pReq->vgIds);
4,334✔
4316
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
4,334✔
4317
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
4,334✔
4318
    
4319
    for (int32_t n = 0; n < vgNum; ++n) {
8,668✔
4320
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
4,334✔
4321
      pAddr = taosArrayGet(pRsp->vgList, n);
4,334✔
4322
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
4,334✔
4323
    }
4324
  }
4325

4326
_exit:
4,334✔
4327

4328
  tFreeSStreamMgmtReq(pMgmtReq);
4,334✔
4329
  taosMemoryFree(pMgmtReq);
4,334✔
4330

4331
  if (code) {
4,334✔
UNCOV
4332
    rsp.code = code;
×
4333
    
UNCOV
4334
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
UNCOV
4335
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4336
  } else {
4337
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
8,668✔
4338
  }
4339

4340

4341
_final:
4,334✔
4342

4343
  if (finalCode) {
4,334✔
UNCOV
4344
    tFreeSStreamMgmtRsp(&rsp);
×
UNCOV
4345
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
UNCOV
4346
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4347
  }
4348

4349
  return finalCode;
4,334✔
4350
}
4351

4352

4353
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
70,120✔
4354
  int32_t code = TSDB_CODE_SUCCESS;
70,120✔
4355
  int32_t lino = 0;
70,120✔
4356

4357
  switch (pTask->pMgmtReq->type) {
70,120✔
4358
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
65,786✔
4359
      msmDeployTriggerOrigReader(pCtx, pTask);
65,786✔
4360
      break;
65,786✔
4361
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
4,334✔
4362
      msmDeployRunnerOrigReader(pCtx, pTask);
4,334✔
4363
      break;
4,334✔
UNCOV
4364
    default:
×
UNCOV
4365
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
UNCOV
4366
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
UNCOV
4367
      break;
×
4368
  }
4369

4370
_exit:
70,120✔
4371

4372
  if (code) {
70,120✔
4373
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4374
  }
4375

4376
  return code;
70,120✔
4377
}
4378

4379
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
24,851✔
4380
  int32_t code = TSDB_CODE_SUCCESS;
24,851✔
4381
  int32_t lino = 0;
24,851✔
4382
  SStreamHbMsg* pReq = pCtx->pReq;
24,851✔
4383
  SStmTaskStatusMsg* pTask = NULL;
24,851✔
4384
  
4385
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
24,851✔
4386
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
24,851✔
4387
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
24,851✔
4388
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
24,851✔
4389
  }
4390
  
4391
  for (int32_t i = 0; i < reqNum; ++i) {
94,971✔
4392
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
70,120✔
4393
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
70,120✔
4394
    if (NULL == pTask) {
70,120✔
UNCOV
4395
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
UNCOV
4396
      continue;
×
4397
    }
4398

4399
    if (NULL == pTask->pMgmtReq) {
70,120✔
UNCOV
4400
      msttError("idx %d without mgmtReq", idx);
×
UNCOV
4401
      continue;
×
4402
    }
4403

4404
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
70,120✔
4405
  }
4406

4407
_exit:
24,851✔
4408

4409
  if (code) {
24,851✔
UNCOV
4410
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4411
  }
4412

4413
  return code;
24,851✔
4414
}
4415

4416
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
16,529,626✔
4417
  int32_t code = TSDB_CODE_SUCCESS;
16,529,626✔
4418
  int32_t lino = 0;
16,529,626✔
4419
  SStreamHbMsg* pReq = pCtx->pReq;
16,529,626✔
4420

4421
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,529,626✔
4422
  if (GOT_SNODE(pReq->snodeId)) {
16,331,122✔
4423
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,654,790✔
4424
  }
4425
  
4426
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,328,985✔
4427
    msmHandleStreamActions(pCtx);
50,424✔
4428
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
50,424✔
4429
  }
4430

4431
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
16,328,985✔
4432
    code = msmHandleStreamRequests(pCtx);
24,851✔
4433
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
24,851✔
4434
    TAOS_CHECK_EXIT(code);
24,851✔
4435
  }
4436

4437
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,328,985✔
4438
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
71,566✔
4439
  } else {
4440
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,257,419✔
4441
  }
4442

4443
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,328,985✔
4444
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
109,540✔
4445
  }
4446

4447
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,328,985✔
4448
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
73,090✔
4449
  }
4450

4451
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,328,985✔
4452
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,522,368✔
4453
  }
4454

4455
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,328,985✔
4456
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
156,450✔
4457
  }
4458

4459
_exit:
16,328,985✔
4460

4461
  if (code) {
16,529,626✔
4462
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
200,641✔
4463
  }
4464

4465
  return code;
16,529,626✔
4466
}
4467

4468
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
16,731,471✔
4469
  int32_t lino = 0;
16,731,471✔
4470
  int32_t tlen = 0;
16,731,471✔
4471
  void   *buf = NULL;
16,731,471✔
4472

4473
  if (TSDB_CODE_SUCCESS != code) {
16,731,471✔
4474
    goto _exit;
200,641✔
4475
  }
4476

4477
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
16,530,830✔
4478
  if (code < 0) {
16,530,830✔
UNCOV
4479
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
UNCOV
4480
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4481
  }
4482

4483
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
16,530,830✔
4484
  if (buf == NULL) {
16,530,830✔
UNCOV
4485
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4486
    TAOS_CHECK_EXIT(terrno);    
×
4487
  }
4488

4489
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
16,530,830✔
4490
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
16,530,830✔
4491

4492
  SEncoder encoder;
16,511,852✔
4493
  tEncoderInit(&encoder, abuf, tlen);
16,530,830✔
4494
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
16,530,830✔
UNCOV
4495
    rpcFreeCont(buf);
×
UNCOV
4496
    buf = NULL;
×
UNCOV
4497
    tEncoderClear(&encoder);
×
UNCOV
4498
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
UNCOV
4499
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4500
  }
4501
  tEncoderClear(&encoder);
16,530,830✔
4502

4503
_exit:
16,731,471✔
4504

4505
  pMsg->code = code;
16,731,471✔
4506
  pMsg->info = *pRpcInfo;
16,731,471✔
4507
  if (TSDB_CODE_SUCCESS == code) {
16,731,471✔
4508
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
16,530,830✔
4509
    pMsg->pCont = buf;
16,530,830✔
4510
  }
4511
}
16,731,471✔
4512

4513

4514
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
16,731,471✔
4515
  int32_t code = TSDB_CODE_SUCCESS;
16,731,471✔
4516
  SMStreamHbRspMsg rsp = {0};
16,731,471✔
4517
  rsp.streamGId = pHb->streamGId;
16,731,471✔
4518

4519
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,731,471✔
4520

4521
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
16,731,471✔
4522
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
200,821✔
4523
    goto _exit;
200,821✔
4524
  }
4525

4526
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,530,650✔
4527
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
16,530,650✔
4528

4529
  pCtx->tidx = tidx;
16,530,650✔
4530
  pCtx->pMnode = pMnode;
16,530,650✔
4531
  pCtx->currTs = currTs;
16,530,650✔
4532
  pCtx->pReq = pHb;
16,530,650✔
4533
  pCtx->pRsp = &rsp;
16,530,650✔
4534
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
16,530,650✔
4535
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
16,530,650✔
4536
  
4537
  switch (atomic_load_8(&mStreamMgmt.state)) {
16,530,650✔
4538
    case MND_STM_STATE_WATCH:
1,024✔
4539
      code = msmWatchHandleHbMsg(pCtx);
1,024✔
4540
      break;
1,024✔
4541
    case MND_STM_STATE_NORMAL:
16,529,626✔
4542
      code = msmNormalHandleHbMsg(pCtx);
16,529,626✔
4543
      break;
16,529,626✔
UNCOV
4544
    default:
×
UNCOV
4545
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
UNCOV
4546
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
UNCOV
4547
      break;
×
4548
  }
4549

4550
_exit:
16,731,471✔
4551

4552
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
16,731,471✔
4553

4554
  msmCleanStreamGrpCtx(pHb);
16,731,471✔
4555
  msmClearStreamToDeployMaps(pHb);
16,731,471✔
4556

4557
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
16,731,471✔
4558
  
4559
  tFreeSMStreamHbRspMsg(&rsp);
16,731,471✔
4560

4561
  return code;
16,731,471✔
4562
}
4563

4564
void msmHandleBecomeLeader(SMnode *pMnode) {
419,335✔
4565
  if (tsDisableStream) {
419,335✔
UNCOV
4566
    return;
×
4567
  }
4568

4569
  mstInfo("start to process mnode become leader");
419,335✔
4570

4571
  int32_t code = 0;
419,335✔
4572
  streamAddVnodeLeader(MNODE_HANDLE);
419,335✔
4573
  
4574
  taosWLockLatch(&mStreamMgmt.runtimeLock);
419,335✔
4575
  msmDestroyRuntimeInfo(pMnode);
419,335✔
4576
  code = msmInitRuntimeInfo(pMnode);
419,335✔
4577
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
419,335✔
4578

4579
  if (TSDB_CODE_SUCCESS == code) {
419,335✔
4580
    atomic_store_8(&mStreamMgmt.active, 1);
419,335✔
4581
  }
4582

4583
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
419,335✔
4584
}
4585

4586
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
614,107✔
4587
  if (tsDisableStream) {
614,107✔
UNCOV
4588
    return;
×
4589
  }
4590

4591
  mstInfo("start to process mnode become not leader");
614,107✔
4592

4593
  streamRemoveVnodeLeader(MNODE_HANDLE);
614,107✔
4594

4595
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
614,107✔
4596
    taosWLockLatch(&mStreamMgmt.runtimeLock);
419,335✔
4597
    msmDestroyRuntimeInfo(pMnode);
419,335✔
4598
    mStreamMgmt.stat.inactiveTimes++;
419,335✔
4599
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
419,335✔
4600
  }
4601
}
4602

4603

UNCOV
4604
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
UNCOV
4605
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
UNCOV
4606
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4607
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4608
  } else {
UNCOV
4609
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4610
  }
4611
}
×
4612

4613
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
13,659✔
4614
  int32_t code = TSDB_CODE_SUCCESS;
13,659✔
4615
  int32_t lino = 0;
13,659✔
4616
  SStreamObj* pStream = pObj;
13,659✔
4617
  SSnodeObj* pSnode = p1;
13,659✔
4618
  SArray** ppRes = p2;
13,659✔
4619

4620
  if (pStream->mainSnodeId == pSnode->id) {
13,659✔
4621
    if (NULL == *ppRes) {
10,663✔
4622
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,894✔
4623
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
1,894✔
4624
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
1,894✔
4625
    }
4626

4627
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
21,326✔
4628
  }
4629

4630
  return true;
13,659✔
4631

UNCOV
4632
_exit:
×
4633

UNCOV
4634
  if (code) {
×
UNCOV
4635
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4636
  }  
4637

UNCOV
4638
  *(int32_t*)p3 = code;
×
4639

UNCOV
4640
  return false;
×
4641
}
4642

4643

4644
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
60,554✔
4645
  int32_t code = TSDB_CODE_SUCCESS;
60,554✔
4646
  int32_t lino = 0;
60,554✔
4647
  
4648
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
60,554✔
4649
  TAOS_CHECK_EXIT(code);
60,554✔
4650

4651
  int32_t streamNum = taosArrayGetSize(*ppRes);
60,554✔
4652
  if (streamNum > 0 && 0 == pSnode->replicaId) {
60,554✔
4653
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
370✔
4654
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
370✔
4655
  }
4656

4657
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4658

4659
_exit:
60,554✔
4660

4661
  if (code) {
60,554✔
4662
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
370✔
4663
  }  
4664

4665
  return code;
60,554✔
4666
}
4667

4668
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
600,147✔
4669
  SStreamObj* pStream = pObj;
600,147✔
4670
  int64_t streamId = pStream->pCreate->streamId;
600,147✔
4671
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
600,147✔
4672
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
600,147✔
4673
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
600,147✔
4674
  
4675
  if ((userDropped || userStopped) && (NULL == pStatus)) {
600,147✔
4676
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
740✔
4677
    return true;
740✔
4678
  }
4679
  
4680
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
599,407✔
4681
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
440,894✔
4682
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4683
    return true;
440,894✔
4684
  }
4685

4686
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
158,513✔
4687
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
34,231✔
4688
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4689
    return true;
34,231✔
4690
  }  
4691

4692
  if (pStatus) {
124,282✔
4693
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
120,679✔
UNCOV
4694
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4695
    }
4696

4697
    return true;
120,679✔
4698
  }
4699

4700
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
3,603✔
4701

4702
  return true;
3,603✔
4703
}
4704

4705
void msmCheckLoopStreamMap(SMnode *pMnode) {
60,340✔
4706
  SStmStatus* pStatus = NULL;
60,340✔
4707
  void* pIter = NULL;
60,340✔
4708
  int8_t stopped = 0;
60,340✔
4709
  int64_t streamId = 0;
60,340✔
4710
  
4711
  while (true) {
4712
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
448,124✔
4713
    if (NULL == pIter) {
448,124✔
4714
      break;
60,340✔
4715
    }
4716

4717
    pStatus = (SStmStatus*)pIter;
387,784✔
4718

4719
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
387,784✔
4720
    stopped = atomic_load_8(&pStatus->stopped);
387,784✔
4721
    if (MST_IS_USER_STOPPED(stopped)) {
387,784✔
4722
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
761✔
4723
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
761✔
4724
      continue;
761✔
4725
    }
4726

4727
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
387,023✔
UNCOV
4728
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4729
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
UNCOV
4730
      continue;
×
4731
    }
4732

4733
    if (MST_IS_ERROR_STOPPED(stopped)) {
387,023✔
4734
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
29,035✔
4735
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
19,488✔
4736
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4737
            
4738
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
19,488✔
4739
        continue;
19,488✔
4740
      }
4741

4742
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
9,547✔
4743
      continue;
9,547✔
4744
    }
4745

4746
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
357,988✔
UNCOV
4747
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
UNCOV
4748
      continue;
×
4749
    }
4750
  }
4751
}
60,340✔
4752

4753
void msmCheckStreamsStatus(SMnode *pMnode) {
446,356✔
4754
  SStmCheckStatusCtx ctx = {0};
446,356✔
4755

4756
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
446,356✔
4757
  
4758
  if (MST_READY_FOR_SDB_LOOP()) {
446,356✔
4759
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
81,505✔
4760
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
81,505✔
4761
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
81,505✔
4762
  }
4763

4764
  if (MST_READY_FOR_MAP_LOOP()) {
446,356✔
4765
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
60,340✔
4766
    msmCheckLoopStreamMap(pMnode);
60,340✔
4767
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
60,340✔
4768
  }
4769
}
446,356✔
4770

4771
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,882,409✔
4772
  for (int32_t i = 0; i < taskNum; ++i) {
7,915,335✔
4773
    SStmTaskStatus* pTask = *(pList + i);
4,033,281✔
4774

4775
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
4,033,281✔
4776
      continue;
101,695✔
4777
    }
4778
    
4779
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,946,510✔
4780
      continue;
3,938,431✔
4781
    }
4782

4783
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
8,079✔
4784
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
8,079✔
4785
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
355✔
4786
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4787
          
4788
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
355✔
4789
      break;
355✔
4790
    }
4791

4792
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
7,724✔
4793
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4794

4795
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
7,724✔
4796
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
7,724✔
4797

4798
    SStmTaskAction task = {0};
7,724✔
4799
    task.streamId = streamId;
7,724✔
4800
    task.id = pTask->id;
7,724✔
4801
    task.flag = pTask->flags;
7,724✔
4802
    task.type = pTask->type;
7,724✔
4803
    
4804
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
7,724✔
4805
  }
4806
}
3,882,409✔
4807

4808
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
196,267✔
4809
  void* pIter = NULL;
196,267✔
4810
  SStmVgStreamStatus* pVg = NULL;
196,267✔
4811
  int64_t streamId = 0;
196,267✔
4812
  
4813
  while (true) {
1,451,946✔
4814
    pIter = taosHashIterate(pStreams, pIter);
1,648,213✔
4815
    if (NULL == pIter) {
1,648,213✔
4816
      break;
196,267✔
4817
    }
4818

4819
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,451,946✔
4820
    pVg = (SStmVgStreamStatus*)pIter;
1,451,946✔
4821

4822
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,451,946✔
4823
    if (taskNum > 0) {
1,451,946✔
4824
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
903,585✔
4825
    }
4826

4827
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,451,946✔
4828
    if (taskNum > 0) {
1,451,946✔
4829
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
902,931✔
4830
    }
4831
  }
4832
}
196,267✔
4833

UNCOV
4834
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
UNCOV
4835
  int64_t streamId = 0;
×
UNCOV
4836
  void* pIter = NULL;
×
UNCOV
4837
  SStmVgStreamStatus* pStream = NULL;
×
4838

UNCOV
4839
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
UNCOV
4840
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4841
    return;
×
4842
  }
4843

4844
  
4845
  while (true) {
4846
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4847
    if (NULL == pIter) {
×
4848
      break;
×
4849
    }
4850

UNCOV
4851
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4852
    
4853
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4854
  }
4855

UNCOV
4856
  taosHashClear(pVg->streamTasks);
×
4857
}
4858

4859

4860
void msmCheckVgroupStatus(SMnode *pMnode) {
446,356✔
4861
  void* pIter = NULL;
446,356✔
4862
  int32_t code = 0;
446,356✔
4863
  
4864
  while (true) {
1,805,963✔
4865
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
2,252,319✔
4866
    if (NULL == pIter) {
2,252,319✔
4867
      break;
446,356✔
4868
    }
4869

4870
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,805,963✔
4871
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,805,963✔
4872
      continue;
1,607,331✔
4873
    }
4874
    
4875
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
198,632✔
4876

4877
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
198,632✔
4878
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
2,365✔
4879
      if (NULL == pVgroup) {
2,365✔
4880
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
2,365✔
4881
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
2,365✔
4882
        if (code) {
2,365✔
UNCOV
4883
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4884
        }
4885
        continue;
2,365✔
4886
      }
UNCOV
4887
      mndReleaseVgroup(pMnode, pVgroup);
×
4888
      
UNCOV
4889
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4890
      
UNCOV
4891
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
UNCOV
4892
      continue;
×
4893
    }
4894

4895
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
196,267✔
4896

4897
    msmCheckVgroupStreamStatus(pVg->streamTasks);
196,267✔
4898
  }
4899
}
446,356✔
4900

4901
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
3,100✔
4902
  *deployNum = 0;
3,100✔
4903
  
4904
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
10,772✔
4905
    if (pStream->runners[i]) {
8,486✔
4906
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
3,100✔
4907
      for (int32_t t = 0; t < taskNum; ++t) {
5,386✔
4908
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
3,100✔
4909
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
3,100✔
4910
        if (stopped) {
3,100✔
4911
          mstsDebug("stream already stopped %d, ignore it", stopped);
814✔
4912
          *deployNum = 0;
814✔
4913
          return;
814✔
4914
        }
4915

4916
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
2,286✔
4917
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
2,286✔
4918
      }
4919
      
4920
      deployId[*deployNum] = i;
2,286✔
4921
      (*deployNum)++;
2,286✔
4922
    }
4923
  }
4924
}
4925

4926
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
6,148✔
4927
  pSnode->runnerThreadNum = -1;
6,148✔
4928

4929
  (void)msmSTAddSnodesToMap(pMnode);
6,148✔
4930

4931
  int64_t streamId = 0;
6,148✔
4932
  void* pIter = NULL;
6,148✔
4933
  SStmSnodeStreamStatus* pStream = NULL;
6,148✔
4934
  int32_t deployNum = 0;
6,148✔
4935
  SStmTaskAction task = {0};
6,148✔
4936
  
4937
  while (true) {
4938
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
11,638✔
4939
    if (NULL == pIter) {
11,638✔
4940
      break;
6,148✔
4941
    }
4942

4943
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
5,490✔
4944
    
4945
    task.streamId = streamId;
5,490✔
4946
    
4947
    pStream = (SStmSnodeStreamStatus*)pIter;
5,490✔
4948
    if (pStream->trigger) {
5,490✔
4949
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
2,390✔
4950
      if (stopped) {
2,390✔
UNCOV
4951
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
UNCOV
4952
        continue;
×
4953
      }
4954

4955
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
2,390✔
4956
      
4957
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
2,390✔
4958
    } else {
4959
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
3,100✔
4960
      
4961
      if (task.deployNum > 0) {
3,100✔
4962
        //task.triggerStatus = pStream->trigger;
4963
        task.multiRunner = true;
2,286✔
4964
        task.type = STREAM_RUNNER_TASK;
2,286✔
4965
        
4966
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
2,286✔
4967
        
4968
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
2,286✔
4969
      }
4970
    }
4971
  }
4972

4973
  taosHashClear(pSnode->streamTasks);
6,148✔
4974
}
6,148✔
4975

4976

4977
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
59,518✔
4978
  void* pIter = NULL;
59,518✔
4979
  SStmSnodeStreamStatus* pSnode = NULL;
59,518✔
4980
  int64_t streamId = 0;
59,518✔
4981
  
4982
  while (true) {
4983
    pIter = taosHashIterate(pStreams, pIter);
613,486✔
4984
    if (NULL == pIter) {
613,486✔
4985
      break;
59,518✔
4986
    }
4987

4988
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
553,968✔
4989
    pSnode = (SStmSnodeStreamStatus*)pIter;
553,968✔
4990

4991
    if (NULL != pSnode->trigger) {
553,968✔
4992
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
529,844✔
4993
    }
4994

4995
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,215,872✔
4996
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,661,904✔
4997
      if (taskNum > 0) {
1,661,904✔
4998
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,546,049✔
4999
      }
5000
    }
5001
  }
5002
}
59,518✔
5003

5004

5005
void msmCheckSnodeStatus(SMnode *pMnode) {
446,356✔
5006
  void* pIter = NULL;
446,356✔
5007
  
5008
  while (true) {
620,716✔
5009
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
1,067,072✔
5010
    if (NULL == pIter) {
1,067,072✔
5011
      break;
446,356✔
5012
    }
5013

5014
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
620,716✔
5015
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
620,716✔
5016
      continue;
547,121✔
5017
    }
5018

5019
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
73,595✔
5020
    
5021
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
73,595✔
5022
    if (NULL == pSnode->streamTasks) {
73,595✔
5023
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
9,098✔
5024
      continue;
9,098✔
5025
    }
5026
    
5027
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
64,497✔
5028
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
4,979✔
5029
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5030
      
5031
      msmHandleSnodeLost(pMnode, pSnode);
4,979✔
5032
      continue;
4,979✔
5033
    }
5034
    
5035
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
59,518✔
5036

5037
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
59,518✔
5038
  }
5039
}
446,356✔
5040

5041

5042
void msmCheckTasksStatus(SMnode *pMnode) {
446,356✔
5043
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
446,356✔
5044

5045
  msmCheckVgroupStatus(pMnode);
446,356✔
5046
  msmCheckSnodeStatus(pMnode);
446,356✔
5047
}
446,356✔
5048

5049
void msmCheckSnodesState(SMnode *pMnode) {
446,356✔
5050
  if (!MST_READY_FOR_SNODE_LOOP()) {
446,356✔
5051
    return;
397,809✔
5052
  }
5053

5054
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
48,547✔
5055

5056
  void* pIter = NULL;
48,547✔
5057
  int32_t snodeId = 0;
48,547✔
5058
  while (true) {
57,847✔
5059
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
106,394✔
5060
    if (NULL == pIter) {
106,394✔
5061
      break;
48,547✔
5062
    }
5063

5064
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
57,847✔
5065
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
57,847✔
5066
      continue;
56,678✔
5067
    }
5068

5069
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
1,169✔
5070
    if (NULL == pSnode->streamTasks) {
1,169✔
UNCOV
5071
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
UNCOV
5072
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
UNCOV
5073
      continue;
×
5074
    }
5075
    
5076
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,169✔
5077
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5078
    
5079
    msmHandleSnodeLost(pMnode, pSnode);
1,169✔
5080
  }
5081

5082
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
48,547✔
5083
}
5084

5085
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
8,251,121✔
5086
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
8,251,121✔
5087
  if (0 == active || MND_STM_STATE_NORMAL != state) {
8,251,121✔
5088
    mstTrace("ignore health check since active:%d state:%d", active, state);
768✔
5089
    return false;
768✔
5090
  }
5091

5092
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
8,250,353✔
5093
    mstTrace("ignore health check since no stream now");
7,357,641✔
5094
    return false;
7,357,641✔
5095
  }
5096

5097
  return true;
892,712✔
5098
}
5099

5100
void msmHealthCheck(SMnode *pMnode) {
7,804,765✔
5101
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,804,765✔
5102
    return;
7,358,409✔
5103
  }
5104

5105
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
834,304✔
5106
  
5107
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
446,356✔
5108
  if (!msmCheckNeedHealthCheck(pMnode)) {
446,356✔
UNCOV
5109
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
UNCOV
5110
    return;
×
5111
  }
5112
  
5113
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
446,356✔
5114
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
446,356✔
5115

5116
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
446,356✔
5117
  
5118
  msmCheckStreamsStatus(pMnode);
446,356✔
5119
  msmCheckTasksStatus(pMnode);
446,356✔
5120
  msmCheckSnodesState(pMnode);
446,356✔
5121

5122
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
446,356✔
5123

5124
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
446,356✔
5125
}
5126

5127
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
1,792✔
5128
  SStreamObj *pStream = pObj;
1,792✔
5129
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
1,792✔
UNCOV
5130
    return true;
×
5131
  }
5132
  
5133
  pStream->updateTime = *(int64_t*)p1;
1,792✔
5134
  
5135
  (*(int32_t*)p2)++;
1,792✔
5136
  
5137
  return true;
1,792✔
5138
}
5139

UNCOV
5140
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
UNCOV
5141
  int32_t code = 0;
×
UNCOV
5142
  int8_t  stopped = 0;
×
5143
  
UNCOV
5144
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5145
  
UNCOV
5146
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5147
  if (NULL == pStatus) {
×
5148
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5149
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
UNCOV
5150
    goto _exit;
×
5151
  }
5152

5153
  stopped = atomic_load_8(&pStatus->stopped);
×
5154
  if (stopped) {
×
5155
    mstsError("stream already stopped, stopped:%d", stopped);
×
5156
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5157
    goto _exit;
×
5158
  }
5159

5160
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5161
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5162
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5163
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5164
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
UNCOV
5165
    goto _exit;
×
5166
  }
5167

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

5171
_exit:
×
5172
  
UNCOV
5173
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5174

5175
  return code;
×
5176
}
5177

5178
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
419,335✔
5179
  int32_t code = TSDB_CODE_SUCCESS;
419,335✔
5180
  int32_t lino = 0;
419,335✔
5181
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
419,335✔
5182
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
419,335✔
5183
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
419,335✔
5184

5185
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
837,562✔
5186

5187
  mStreamMgmt.stat.activeTimes++;
419,335✔
5188
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
419,335✔
5189
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
419,335✔
5190
  if (NULL == mStreamMgmt.tCtx) {
419,335✔
UNCOV
5191
    code = terrno;
×
UNCOV
5192
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
UNCOV
5193
    goto _exit;
×
5194
  }
5195

5196
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
419,335✔
5197
  if (mStreamMgmt.actionQ == NULL) {
419,335✔
5198
    code = terrno;
×
5199
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5200
    goto _exit;
×
5201
  }
5202
  
5203
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
419,335✔
5204
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
419,335✔
5205
  
5206
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
419,335✔
5207
  
5208
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,515,640✔
5209
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,096,305✔
5210

5211
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,577,830✔
5212
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,481,525✔
5213
      if (pCtx->deployStm[m] == NULL) {
10,481,525✔
UNCOV
5214
        code = terrno;
×
UNCOV
5215
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
UNCOV
5216
        goto _exit;
×
5217
      }
5218
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
10,481,525✔
5219
      
5220
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,481,525✔
5221
      if (pCtx->actionStm[m] == NULL) {
10,481,525✔
5222
        code = terrno;
×
5223
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
UNCOV
5224
        goto _exit;
×
5225
      }
5226
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
10,481,525✔
5227
    }
5228
  }
5229
  
5230
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
419,335✔
5231
  if (mStreamMgmt.streamMap == NULL) {
419,335✔
UNCOV
5232
    code = terrno;
×
UNCOV
5233
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
UNCOV
5234
    goto _exit;
×
5235
  }
5236
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
419,335✔
5237
  
5238
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
419,335✔
5239
  if (mStreamMgmt.taskMap == NULL) {
419,335✔
5240
    code = terrno;
×
5241
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
UNCOV
5242
    goto _exit;
×
5243
  }
5244
  
5245
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
419,335✔
5246
  if (mStreamMgmt.vgroupMap == NULL) {
419,335✔
5247
    code = terrno;
×
5248
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5249
    goto _exit;
×
5250
  }
5251
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
419,335✔
5252

5253
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
419,335✔
5254
  if (mStreamMgmt.snodeMap == NULL) {
419,335✔
5255
    code = terrno;
×
5256
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
UNCOV
5257
    goto _exit;
×
5258
  }
5259
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
419,335✔
5260
  
5261
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
419,335✔
5262
  if (mStreamMgmt.dnodeMap == NULL) {
419,335✔
5263
    code = terrno;
×
5264
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
UNCOV
5265
    goto _exit;
×
5266
  }
5267

5268
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
419,335✔
5269
  if (mStreamMgmt.toDeployVgMap == NULL) {
419,335✔
5270
    code = terrno;
×
5271
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5272
    goto _exit;
×
5273
  }
5274
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
419,335✔
5275
  
5276
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
419,335✔
5277
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
419,335✔
5278
    code = terrno;
×
5279
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
UNCOV
5280
    goto _exit;
×
5281
  }
5282
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
419,335✔
5283

5284
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
419,335✔
5285
  if (mStreamMgmt.toUpdateScanMap == NULL) {
419,335✔
5286
    code = terrno;
×
5287
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
UNCOV
5288
    goto _exit;
×
5289
  }
5290
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
419,335✔
5291

5292
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
419,335✔
5293
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
419,335✔
5294

5295
  mStreamMgmt.lastTaskId = 1;
419,335✔
5296

5297
  int32_t activeStreamNum = 0;
419,335✔
5298
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
419,335✔
5299

5300
  if (activeStreamNum > 0) {
419,335✔
5301
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
256✔
5302
  } else {
5303
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
419,079✔
5304
  }
5305

5306
_exit:
419,335✔
5307

5308
  if (code) {
419,335✔
UNCOV
5309
    msmDestroyRuntimeInfo(pMnode);
×
UNCOV
5310
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5311
  } else {
5312
    mstInfo("mnode stream runtime init done");
419,335✔
5313
  }
5314

5315
  return code;
419,335✔
5316
}
5317

5318

5319

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