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

taosdata / TDengine / #4983

13 Mar 2026 03:38AM UTC coverage: 68.653% (+0.07%) from 68.587%
#4983

push

travis-ci

web-flow
feat/6641435300-save-audit-in-self (#34738)

434 of 584 new or added lines in 10 files covered. (74.32%)

434 existing lines in 121 files now uncovered.

212745 of 309883 relevant lines covered (68.65%)

134272959.11 hits per line

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

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

36
  if (NULL == mStreamMgmt.actionQ) {
795,732✔
37
    return;
397,866✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,989,135✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
11,934,810✔
49
    taosHashCleanup(pCtx->deployStm[m]);
9,945,675✔
50
    taosHashCleanup(pCtx->actionStm[m]);
9,945,675✔
51
  }
52
}
1,989,135✔
53

54
void msmDestroyThreadCtxs() {
795,732✔
55
  if (NULL == mStreamMgmt.tCtx) {
795,732✔
56
    return;
397,866✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,387,001✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,989,135✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
397,866✔
63
}
64

65

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

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

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

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

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

96

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

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

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

111
    pStatus = pStream;
175✔
112
  }
113

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

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

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

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

136
_exit:
446✔
137

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

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

145

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

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

200
  pSnode = NULL;
423,152✔
201

202
_exit:
423,152✔
203

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

208
  return code;
423,152✔
209
}
210

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

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

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

235
  pDnode = NULL;
968,985✔
236

237
_exit:
968,985✔
238

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

243
  return code;
968,985✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
545,898✔
275
  int32_t code = TSDB_CODE_SUCCESS;
545,898✔
276
  int32_t lino = 0;
545,898✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
545,898✔
278
  if (NULL == pStream) {
545,898✔
279
    SStmVgStreamStatus stream = {0};
402,014✔
280
    if (trigReader) {
402,014✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
255,908✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
255,908✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
511,816✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
146,106✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
146,106✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
292,212✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
402,014✔
290
    goto _exit;
402,014✔
291
  }
292
  
293
  if (trigReader) {
143,884✔
294
    if (NULL == pStream->trigReaders) {
22,382✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
22,382✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
22,382✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
44,764✔
300
    goto _exit;
22,382✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
121,502✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
92,640✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
92,640✔
306
  }
307

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

310
_exit:
121,502✔
311

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

320
  return code;
545,898✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
68,636✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
68,636✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
68,636✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
477,262✔
339
  }
340
  
341
_exit:
545,792✔
342

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

350
  return code;
545,898✔
351
}
352

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

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

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

404
  return code;
546,948✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
745,854✔
409
  int32_t code = TSDB_CODE_SUCCESS;
745,854✔
410
  int32_t lino = 0;
745,854✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
745,854✔
412
  if (NULL == pStream) {
745,854✔
413
    SStmSnodeStreamStatus stream = {0};
183,202✔
414
    if (deployId < 0) {
183,202✔
415
      stream.trigger = pStatus;
3,199✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
180,003✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
180,003✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
360,006✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
183,202✔
423
    goto _exit;
183,202✔
424
  }
425
  
426
  if (deployId < 0) {
562,652✔
427
    if (NULL != pStream->trigger) {
174,372✔
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;
174,372✔
433
    goto _exit;
174,372✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
388,280✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
344,297✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
344,297✔
439
  }
440

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

443
_exit:
388,280✔
444

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

453
  return code;
745,854✔
454
}
455

456

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

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

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

484
  return code;
745,854✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
177,571✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
42,266✔
568
      ext.deployed = false;
42,266✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
84,532✔
570

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

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

614
  return code;
568,283✔
615
}
616

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

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

632
_exit:
524,300✔
633

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

638
  return code;
524,300✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
14,186✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
264✔
682

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

687
  return code;  
264✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
67,819✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,441,706✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
65,711,730✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
49,270,024✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
49,270,024✔
725
  }
726

727
_exit:
16,441,706✔
728

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

733
  return code;
16,441,706✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
339,738✔
739
  int32_t num = taosArrayGetSize(pList);
339,738✔
740
  for (int32_t i = 0; i < num; ++i) {
974,283✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
803,830✔
742
    if (pScan->readFromCache) {
803,830✔
743
      return pScan->scanPlan;
169,285✔
744
    }
745
  }
746

747
  return NULL;
170,453✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
546,948✔
775
}
776

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

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

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

805
_exit:
174,372✔
806

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

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

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

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

877
  SStreamTaskAddr addr;
177,200✔
878
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
177,571✔
879
  if (triggerReaderNum > 0) {
177,571✔
880
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
176,600✔
881
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
176,600✔
882
  }
883
  
884
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
397,171✔
885
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
219,600✔
886
    addr.taskId = pStatus->id.taskId;
219,600✔
887
    addr.nodeId = pStatus->id.nodeId;
219,600✔
888
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
219,600✔
889
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
439,200✔
890
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
219,600✔
891
  }
892

893
  pMsg->leaderSnodeId = pStream->mainSnodeId;
177,571✔
894
  pMsg->streamName = pInfo->streamName;
177,571✔
895

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

901
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
174,372✔
902

903
_exit:
174,372✔
904

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

914

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

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

948
  pMsg->colCids = pInfo->pCreate->colCids;
568,283✔
949
  pMsg->tagCids = pInfo->pCreate->tagCids;
568,283✔
950
_exit:
568,283✔
951

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

959

960
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
416,153✔
961
  int32_t code = TSDB_CODE_SUCCESS;
416,153✔
962
  int32_t lino = 0;
416,153✔
963
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
416,153✔
964
  SListNode* pNode = pList ? listHead(pList) : NULL;
416,153✔
965
  
966
  for (int32_t i = 0; i < taskNum; ++i) {
962,051✔
967
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
545,898✔
968
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
545,898✔
969
    if (pNode) {
545,898✔
970
      pNode = TD_DLIST_NODE_NEXT(pNode);
265,287✔
971
    }
972
  }
973
  
974
_exit:
416,153✔
975

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

983

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

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

1001
  return code;
701,871✔
1002
}
1003

1004
int64_t msmAssignTaskId(void) {
1,290,568✔
1005
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,290,568✔
1006
}
1007

1008
int64_t msmAssignTaskSeriousId(void) {
1,290,568✔
1009
  return taosGetTimestampNs();
1,290,568✔
1010
}
1011

1012

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

1019
  while (true) {
1020
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
907,832✔
1021
    if (NULL == pStatus) {
907,832✔
1022
      if (noExists) {
7,742✔
1023
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1024
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1025
      }
1026

1027
      noExists = true;
7,742✔
1028
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
7,742✔
1029
      
1030
      continue;
7,742✔
1031
    }
1032

1033
    *alive = (pStatus->runnerThreadNum >= 0);
900,090✔
1034
    break;
900,090✔
1035
  }
1036

1037
_exit:
900,090✔
1038

1039
  if (code) {
900,090✔
1040
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1041
  }
1042

1043
  return code;
900,090✔
1044
}
1045

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

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

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

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

1084
_exit:
×
1085

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

1090
  return 0;
×
1091
}
1092

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

1107
  int32_t snodeTarget = taosRand() % snodeNum;
528,951✔
1108

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

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

1134
    snodeId = pObj->id;
547,555✔
1135
    if (snodeIdx == snodeTarget) {
547,555✔
1136
      sdbRelease(pMnode->pSdb, pObj);
528,951✔
1137
      sdbCancelFetch(pMnode->pSdb, pIter);
528,951✔
1138
      pObj = NULL;
528,951✔
1139
      goto _exit;
528,951✔
1140
    }
1141

1142
    sdbRelease(pMnode->pSdb, pObj);
18,604✔
1143
    snodeIdx++;
18,604✔
1144
  }
1145

1146
_exit:
531,461✔
1147

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

1152
  if (0 == snodeId) {
531,461✔
1153
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,510✔
1154
  }
1155

1156
  return snodeId;
531,461✔
1157
}
1158

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

1168
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
701,871✔
1169

1170
_exit:
701,871✔
1171

1172
  if (0 == snodeId) {
701,871✔
1173
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1174
  }
1175

1176
  return snodeId;
701,871✔
1177
}
1178

1179

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

1185
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
177,571✔
1186
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
177,571✔
1187

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

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

1209
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
177,571✔
1210
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
177,571✔
1211

1212
_exit:
177,571✔
1213

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

1218
  return code;
177,571✔
1219
}
1220

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

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

1247
_exit:
278,290✔
1248

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

1253
  return code;
278,290✔
1254
}
1255

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

1285
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
75,467✔
1286
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
75,467✔
1287
      
1288
      void *pIter = NULL;
75,467✔
1289
      while (1) {
400,716✔
1290
        SStmTaskDeploy info = {0};
476,183✔
1291
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
476,183✔
1292
        if (pIter == NULL) {
476,183✔
1293
          break;
75,467✔
1294
        }
1295
      
1296
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
400,716✔
1297
          pState = taosArrayReserve(pInfo->trigReaders, 1);
118,467✔
1298

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

1308
        sdbRelease(pSdb, pVgroup);
400,716✔
1309
      }
1310
      break;
75,467✔
1311
    }
1312
    default:
971✔
1313
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
971✔
1314
      break;
971✔
1315
  }
1316

1317
_exit:
177,571✔
1318

1319
  mndReleaseDb(pCtx->pMnode, pDb);
177,571✔
1320

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

1325
  return code;
177,571✔
1326
}
1327

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

1351
  key[1] = pSubplan->id.subplanId;
265,879✔
1352

1353
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
265,879✔
1354
  if (NULL == ppRes) {
265,879✔
1355
    SArray* pRes = taosArrayInit(1, sizeof(addr));
265,879✔
1356
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
265,879✔
1357
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
531,758✔
1358
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
265,879✔
1359
  } else {
1360
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1361
  }
1362

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

1369
  nodesDestroyNode((SNode*)pSubplan);
265,879✔
1370

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

1375
  return code;
265,879✔
1376
}
1377

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

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

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

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

1414
  return code;
79,225✔
1415
}
1416

1417

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

1422
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
267,608✔
1423

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

1446
_exit:
267,608✔
1447

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

1452
  return code;
267,608✔
1453
}
1454

1455

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

1465
  
1466
  for (int32_t i = 0; i < calcTasksNum; ++i) {
522,083✔
1467
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
344,512✔
1468
    if (pScan->readFromCache) {
344,512✔
1469
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
79,225✔
1470
      continue;
79,225✔
1471
    }
1472
    
1473
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
265,287✔
1474
    for (int32_t m = 0; m < vgNum; ++m) {
530,574✔
1475
      pState = tdListReserve(pInfo->calcReaders);
265,287✔
1476
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
265,287✔
1477

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

1483
_exit:
177,571✔
1484

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

1489
  return code;
177,571✔
1490
}
1491

1492

1493

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

1521
_exit:
592✔
1522

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

1527
  return code;
592✔
1528
}
1529

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

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

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

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

1564
  source.addr.epSet = pSrc->epset;
1,078,703✔
1565
  source.addr.nodeId = pSrc->vgId;
1,078,703✔
1566

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

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

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

1588
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1589
}
1590

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

1609
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,034,720✔
1610

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

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

1624
_exit:
568,283✔
1625

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

1630
  return code;
568,283✔
1631
}
1632

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

1641
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
568,283✔
1642

1643
  SNode* pTmp = NULL;
568,283✔
1644
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,646,986✔
1645
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,078,703✔
1646
      ERASE_NODE(pPlan->pChildren);
1,034,720✔
1647
      continue;
1,034,720✔
1648
    }
1649
    WHERE_NEXT;
43,983✔
1650
  }
1651
  nodesClearList(pPlan->pChildren);
568,283✔
1652
  pPlan->pChildren = NULL;
568,283✔
1653

1654
  if (NULL == pPlan->pParents) {
568,283✔
1655
    goto _exit;
524,300✔
1656
  }
1657

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

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

1677
  return code;
568,283✔
1678
}
1679

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

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

1695
_exit:
524,300✔
1696

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

1701
  return code;
524,300✔
1702
}
1703

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

1717
  if (pDag->numOfSubplans <= 0) {
174,372✔
1718
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1719
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1720
  }
1721

1722
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
174,372✔
1723
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1724
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1725
  }
1726

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

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

1747
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
174,372✔
1748
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
174,372✔
1749
  
1750
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
697,488✔
1751
    totalTaskNum = 0;
523,116✔
1752

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

1759
    taskIdx = 0;
523,116✔
1760
    
1761
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,063,911✔
1762
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
540,795✔
1763
      if (NULL == plans) {
540,795✔
1764
        mstsError("empty level plan, level:%d", i);
×
1765
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1766
      }
1767

1768
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
540,795✔
1769
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1770
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1771
      }
1772

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

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

1785
      for (int32_t n = 0; n < taskNum; ++n) {
1,107,894✔
1786
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
567,099✔
1787
        pState = taosArrayReserve(deployList, 1);
567,099✔
1788

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

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

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

1816
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
540,795✔
1817
    }
1818

1819
    if (totalTaskNum != pDag->numOfSubplans) {
523,116✔
1820
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1821
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1822
    }
1823

1824
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
523,116✔
1825

1826
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
523,116✔
1827

1828
    nodesDestroyNode((SNode *)pDag);
523,116✔
1829
    pDag = NULL;
523,116✔
1830
    
1831
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
523,116✔
1832

1833
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
523,116✔
1834
  }
1835

1836
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
697,488✔
1837
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
523,116✔
1838
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
523,116✔
1839
  }
1840
  
1841
  pInfo->runnerNum = totalTaskNum;
174,372✔
1842
  
1843
_exit:
174,372✔
1844

1845
  if (code) {
174,372✔
1846
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1847
  }
1848

1849
  taosArrayDestroy(deployTaskList);
174,372✔
1850
  nodesDestroyNode((SNode *)pDag);
174,372✔
1851

1852
  return code;
174,372✔
1853
}
1854

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

1872
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
1,776✔
1873
    deployId = pAction->deployId[r];
1,184✔
1874

1875
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
1,184✔
1876

1877
    pStartRunner = pRunner;
1,184✔
1878
    totalTaskNum = 0;
1,184✔
1879

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

1885
    taskIdx = 0;
1,184✔
1886
    
1887
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
2,368✔
1888
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
1,184✔
1889
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
1,184✔
1890
      totalTaskNum += taskNum;
1,184✔
1891

1892
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
1,184✔
1893
      
1894
      for (int32_t n = 0; n < taskNum; ++n) {
2,368✔
1895
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
1,184✔
1896

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

1903
        pRunner->id.nodeId = newNodeId;
1,184✔
1904

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

1915
        pRunner++;
1,184✔
1916
      }
1917

1918
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
1,184✔
1919
    }
1920

1921
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
1,184✔
1922

1923
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
1,184✔
1924

1925
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
1,184✔
1926

1927
    nodesDestroyNode((SNode *)pDag);
1,184✔
1928
    pDag = NULL;
1,184✔
1929

1930
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
1,184✔
1931
  }
1932

1933
_exit:
592✔
1934

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

1939
  nodesDestroyNode((SNode *)pDag);
592✔
1940
  taosArrayDestroy(deployTaskList);
592✔
1941

1942
  return code;
592✔
1943
}
1944

1945

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

1954
_exit:
169,131✔
1955

1956
  if (code) {
169,131✔
1957
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1958
  }
1959

1960
  return code;
169,131✔
1961
}
1962

1963

1964
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
177,571✔
1965
  if (NULL == pStream->pCreate->calcPlan) {
177,571✔
1966
    return TSDB_CODE_SUCCESS;
3,199✔
1967
  }
1968
  
1969
  int32_t code = TSDB_CODE_SUCCESS;
174,372✔
1970
  int32_t lino = 0;
174,372✔
1971
  int64_t streamId = pStream->pCreate->streamId;
174,372✔
1972
  SQueryPlan* pPlan = NULL;
174,372✔
1973

1974
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
174,372✔
1975

1976
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
697,488✔
1977
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
523,116✔
1978
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
523,116✔
1979
  }
1980

1981
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
174,372✔
1982
  pPlan = NULL;
174,372✔
1983
  
1984
  TAOS_CHECK_EXIT(code);
174,372✔
1985

1986
  taosHashClear(mStreamMgmt.toUpdateScanMap);
174,372✔
1987
  mStreamMgmt.toUpdateScanNum = 0;
174,372✔
1988

1989
_exit:
174,372✔
1990

1991
  nodesDestroyNode((SNode *)pPlan);
174,372✔
1992

1993
  if (code) {
174,372✔
1994
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1995
  }
1996

1997
  return code;
174,372✔
1998
}
1999

2000

2001
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
177,571✔
2002
  int32_t code = TSDB_CODE_SUCCESS;
177,571✔
2003
  int32_t lino = 0;
177,571✔
2004
  int64_t streamId = pStream->pCreate->streamId;
177,571✔
2005

2006
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
177,571✔
2007

2008
  pCtx->triggerTaskId = msmAssignTaskId();
177,571✔
2009
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
177,571✔
2010
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
177,571✔
2011
    TAOS_CHECK_EXIT(terrno);
×
2012
  }
2013

2014
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
177,571✔
2015
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
177,571✔
2016
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
177,571✔
2017
  
2018
_exit:
177,571✔
2019

2020
  if (code) {
177,571✔
2021
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2022
  }
2023

2024
  return code;
177,571✔
2025
}
2026

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

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

2066
_exit:
×
2067

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

2073
  return code;
×
2074
}
2075

2076

2077
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
172,330✔
2078
  int32_t code = TSDB_CODE_SUCCESS;
172,330✔
2079
  int32_t lino = 0;
172,330✔
2080
  int64_t streamId = pStream->pCreate->streamId;
172,330✔
2081

2082
  pStatus->lastActionTs = INT64_MIN;
172,330✔
2083

2084
  if (NULL == pStatus->streamName) {
172,330✔
2085
    pStatus->streamName = taosStrdup(pStream->name);
172,330✔
2086
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
172,330✔
2087
  }
2088

2089
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
172,330✔
2090
  
2091
  if (pStream->pCreate->numOfCalcSubplan > 0) {
172,330✔
2092
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
169,131✔
2093
    
2094
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
169,131✔
2095
  }
2096

2097
  if (initList) {
172,330✔
2098
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2099

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

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

2117
  if (code) {
172,330✔
2118
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2119
  }
2120

2121
  return code;
172,330✔
2122
}
2123

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

2130
  if (NULL == pStatus) {
177,571✔
2131
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
172,330✔
2132

2133
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
172,330✔
2134

2135
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
172,330✔
2136
  }
2137
  
2138
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
177,571✔
2139

2140
  mstLogSStmStatus("stream deployed", streamId, pStatus);
177,571✔
2141

2142
_exit:
177,571✔
2143

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

2153
  return code;
177,571✔
2154
}
2155

2156

2157
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
6,009✔
2158
  int32_t code = TSDB_CODE_SUCCESS;
6,009✔
2159
  void* pIter = NULL;
6,009✔
2160

2161
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
8,889✔
2162
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
2,880✔
2163
    (void)mstWaitLock(&pVg->lock, true);
2,880✔
2164

2165
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
2,880✔
2166
    if (atomic_load_32(&pVg->deployed) == taskNum) {
2,880✔
2167
      taosRUnLockLatch(&pVg->lock);
×
2168
      continue;
×
2169
    }
2170

2171
    for (int32_t i = 0; i < taskNum; ++i) {
13,798✔
2172
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
10,918✔
2173
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
10,918✔
2174
        continue;
10,918✔
2175
      }
2176

UNCOV
2177
      mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2178
      pExt->deployed = true;
×
2179
    }
2180
    
2181
    taosRUnLockLatch(&pVg->lock);
2,880✔
2182
  }
2183

2184
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
10,464✔
2185
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
4,455✔
2186
    (void)mstWaitLock(&pSnode->lock, true);
4,455✔
2187

2188
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
4,455✔
2189
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
4,455✔
2190
      for (int32_t i = 0; i < taskNum; ++i) {
9,268✔
2191
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
5,863✔
2192
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
5,863✔
2193
          continue;
5,863✔
2194
        }
2195
        
UNCOV
2196
        mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2197
        pExt->deployed = true;
×
2198
      }
2199
    }
2200

2201
    taskNum = taosArrayGetSize(pSnode->runnerList);
4,455✔
2202
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
4,455✔
2203
      for (int32_t i = 0; i < taskNum; ++i) {
22,044✔
2204
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
17,589✔
2205
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
17,589✔
2206
          continue;
17,589✔
2207
        }
2208
        
UNCOV
2209
        mstDestroySStmTaskToDeployExt(pExt);
×
UNCOV
2210
        pExt->deployed = true;
×
2211
      }
2212
    }
2213

2214
    taosRUnLockLatch(&pSnode->lock);
4,455✔
2215
  }
2216

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

2226
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
26,831✔
2227
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
20,822✔
2228
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
20,822✔
2229
    if (TSDB_CODE_SUCCESS == code) {
20,822✔
2230
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
8,474✔
2231
    }
2232
  }
2233

2234
  size_t keyLen = 0;
6,009✔
2235
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
407,740✔
2236
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
401,731✔
2237
    if (*pStreamId == streamId) {
401,731✔
2238
      int64_t taskId = *(pStreamId + 1);
35,348✔
2239
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
35,348✔
2240
      if (code) {
35,348✔
2241
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2242
      } else {
2243
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
35,348✔
2244
      }
2245
    }
2246
  }
2247

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

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

2265
  mstResetSStmStatus(pStatus);
5,591✔
2266

2267
  pStatus->deployTimes++;
5,591✔
2268
}
5,591✔
2269

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

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

2297
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
180,210✔
2298
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
180,210✔
2299
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
2,289✔
2300
    return TSDB_CODE_SUCCESS;
2,289✔
2301
  }
2302

2303
  TAOS_CHECK_EXIT(code);
177,921✔
2304

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

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

2321
_exit:
177,921✔
2322

2323
  mndReleaseStream(pCtx->pMnode, pStream);
177,921✔
2324

2325
  if (code) {
177,921✔
2326
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2327
  }
2328

2329
  return code;
177,921✔
2330
}
2331

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

2364
_exit:
1,050✔
2365

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

2370
  return code;
1,050✔
2371
}
2372

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

2404
_exit:
2405

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

2410
  return code;
2411
}
2412
*/
2413

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

2463
_exit:
592✔
2464

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

2469
  return code;
592✔
2470
}
2471

2472

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

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

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

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

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

2500
  TAOS_CHECK_EXIT(code);
1,642✔
2501

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

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

2532
_exit:
1,642✔
2533

2534
  if (pStream) {
1,642✔
2535
    mndReleaseStream(pCtx->pMnode, pStream);
1,642✔
2536
  }
2537

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

2543
  return code;
1,642✔
2544
}
2545

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

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

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

2591
  return TSDB_CODE_SUCCESS;
×
2592
}
2593

2594
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
418✔
2595
  int32_t code = TSDB_CODE_SUCCESS;
418✔
2596
  int32_t lino = 0;
418✔
2597

2598
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
418✔
2599

2600
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
418✔
2601

2602
_exit:
418✔
2603

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

2610
  return code;
418✔
2611
}
2612

2613
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
29,382✔
2614
  int32_t code = TSDB_CODE_SUCCESS;
29,382✔
2615
  int32_t lino = 0;
29,382✔
2616

2617
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
29,382✔
2618
  if (0 == active || MND_STM_STATE_NORMAL != state) {
29,382✔
2619
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
305✔
2620
    return;
305✔
2621
  }
2622

2623
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
29,077✔
2624
  if (NULL == pStream) {
29,077✔
2625
    mstsInfo("stream %s already not in streamMap", streamName);
1,821✔
2626
    goto _exit;
1,821✔
2627
  }
2628

2629
  atomic_store_8(&pStream->stopped, 2);
27,256✔
2630

2631
  mstsInfo("set stream %s stopped by user", streamName);
27,256✔
2632

2633
_exit:
×
2634

2635
  taosHashRelease(mStreamMgmt.streamMap, pStream);
29,077✔
2636

2637
  if (code) {
29,077✔
2638
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2639
  }
2640

2641
  return;
29,077✔
2642
}
2643

2644
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,308✔
2645
  int32_t code = TSDB_CODE_SUCCESS;
3,308✔
2646
  int32_t lino = 0;
3,308✔
2647

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

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

2661
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,308✔
2662

2663
_exit:
3,308✔
2664

2665
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,308✔
2666

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

2671
  return code;
3,308✔
2672
}
2673

2674
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
39,908✔
2675
  int32_t code = TSDB_CODE_SUCCESS;
39,908✔
2676
  int32_t lino = 0;
39,908✔
2677
  SStmQNode* pQNode = NULL;
39,908✔
2678

2679
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
221,760✔
2680
    switch (pQNode->type) {
181,852✔
2681
      case STREAM_ACT_DEPLOY:
181,852✔
2682
        if (pQNode->streamAct) {
181,852✔
2683
          mstDebug("start to handle stream deploy action");
180,210✔
2684
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
180,210✔
2685
        } else {
2686
          mstDebug("start to handle task deploy action");
1,642✔
2687
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
1,642✔
2688
        }
2689
        break;
181,852✔
2690
      default:
×
2691
        break;
×
2692
    }
2693
  }
2694

2695
_exit:
39,908✔
2696

2697
  if (code) {
39,908✔
2698
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2699
  }
2700
}
39,908✔
2701

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

2713
    pStatus = (SStmStatus*)pIter;
×
2714

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

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

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

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

2730
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2731

2732
  msmStopAllStreamsByGrant(errCode);
×
2733

2734
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2735
  
2736
  return errCode;
×
2737
}
2738

2739
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,292,318✔
2740
  int32_t code = TSDB_CODE_SUCCESS;
1,292,318✔
2741
  int32_t lino = 0;
1,292,318✔
2742
  int64_t streamId = pDeploy->task.streamId;
1,292,318✔
2743
  
2744
  switch (pDeploy->task.type) {
1,292,318✔
2745
    case STREAM_READER_TASK:
546,948✔
2746
      if (NULL == pStream->readerTasks) {
546,948✔
2747
        pStream->streamId = streamId;
231,647✔
2748
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
231,647✔
2749
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
231,647✔
2750
      }
2751
      
2752
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,093,896✔
2753
      break;
546,948✔
2754
    case STREAM_TRIGGER_TASK:
177,571✔
2755
      pStream->streamId = streamId;
177,571✔
2756
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
177,571✔
2757
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
177,571✔
2758
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
177,571✔
2759
      break;
177,571✔
2760
    case STREAM_RUNNER_TASK:
567,799✔
2761
      if (NULL == pStream->runnerTasks) {
567,799✔
2762
        pStream->streamId = streamId;
180,353✔
2763
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
180,353✔
2764
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
180,353✔
2765
      }      
2766
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,135,598✔
2767
      break;
567,799✔
2768
    default:
×
2769
      break;
×
2770
  }
2771

2772
_exit:
1,292,318✔
2773

2774
  if (code) {
1,292,318✔
2775
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2776
  }
2777

2778
  return code;
1,292,318✔
2779
}
2780

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

2798
      if (TSDB_CODE_DUP_KEY != code) {
×
2799
        goto _exit;
×
2800
      }    
2801

2802
      tFreeSStmStreamDeploy(&streamDeploy);
×
2803
      continue;
×
2804
    }
2805

2806
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,051,904✔
2807
    
2808
    break;
1,051,904✔
2809
  }
2810
  
2811
_exit:
1,292,318✔
2812

2813
  taosHashRelease(pHash, pStream);
1,292,318✔
2814

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

2821
  return code;
1,292,318✔
2822
}
2823

2824

2825
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
190,272✔
2826
  int32_t code = TSDB_CODE_SUCCESS;
190,272✔
2827
  int32_t lino = 0;
190,272✔
2828
  int32_t taskNum = taosArrayGetSize(pTasks);
190,272✔
2829

2830
  for (int32_t i = 0; i < taskNum; ++i) {
1,482,590✔
2831
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,292,318✔
2832
    if (pExt->deployed) {
1,292,318✔
UNCOV
2833
      continue;
×
2834
    }
2835

2836
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,292,318✔
2837
    pExt->deployed = true;
1,292,318✔
2838

2839
    (void)atomic_add_fetch_32(deployed, 1);
1,292,318✔
2840
  }
2841

2842
_exit:
190,272✔
2843

2844
  if (code) {
190,272✔
2845
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2846
  }
2847

2848
  return code;
190,272✔
2849
}
2850

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

2858
  mstDebug("start to add stream vgroup tasks deploy");
53,253✔
2859
  
2860
  for (int32_t i = 0; i < vgNum; ++i) {
355,432✔
2861
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
302,179✔
2862

2863
    msmUpdateVgroupUpTs(pCtx, *vgId);
302,179✔
2864

2865
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
302,179✔
2866
    if (NULL == pVg) {
302,179✔
2867
      continue;
194,421✔
2868
    }
2869

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

2883
_exit:
53,253✔
2884

2885
  if (code) {
53,253✔
2886
    if (pVg) {
×
2887
      taosRUnLockLatch(&pVg->lock);
×
2888
    }
2889

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

2893
  return code;
53,253✔
2894
}
2895

2896
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
60,829✔
2897
  int32_t code = TSDB_CODE_SUCCESS;
60,829✔
2898
  int32_t lino = 0;
60,829✔
2899
  SStmSnodeTasksDeploy* pSnode = NULL;
60,829✔
2900
  SStreamHbMsg* pReq = pCtx->pReq;
60,829✔
2901

2902
  mstDebug("start to add stream snode tasks deploy");
60,829✔
2903
  
2904
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
60,829✔
2905
  if (NULL == pSnode) {
60,829✔
2906
    return TSDB_CODE_SUCCESS;
18,582✔
2907
  }
2908

2909
  (void)mstWaitLock(&pSnode->lock, false);
42,247✔
2910
  
2911
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
42,247✔
2912
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
40,267✔
2913
  }
2914

2915
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
42,247✔
2916
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
42,247✔
2917
  }
2918
  
2919
  taosWUnLockLatch(&pSnode->lock);
42,247✔
2920

2921
_exit:
42,247✔
2922

2923
  if (code) {
42,247✔
2924
    if (pSnode) {
×
2925
      taosWUnLockLatch(&pSnode->lock);
×
2926
    }
2927

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

2931
  return code;
42,247✔
2932
}
2933

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

2945
_exit:
639,090✔
2946

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

2951
  return code;
639,090✔
2952
}
2953

2954
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
55,364✔
2955
  int32_t code = TSDB_CODE_SUCCESS;
55,364✔
2956
  int32_t lino = 0;
55,364✔
2957
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
55,364✔
2958
  void* pIter = NULL;
55,364✔
2959

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

2965
  while (1) {
240,414✔
2966
    pIter = taosHashIterate(pCtx->deployStm, pIter);
295,778✔
2967
    if (pIter == NULL) {
295,778✔
2968
      break;
55,364✔
2969
    }
2970
    
2971
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
240,414✔
2972
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
480,828✔
2973

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

2978
    mstClearSStmStreamDeploy(pDeploy);
240,414✔
2979
    
2980
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
240,414✔
2981
  }
2982
  
2983
_exit:
55,364✔
2984

2985
  if (pIter) {
55,364✔
2986
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2987
  }
2988

2989
  if (code) {
55,364✔
2990
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2991
  }
2992
  
2993
  return code;
55,364✔
2994
}
2995

2996
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
53,116✔
2997
  int32_t code = TSDB_CODE_SUCCESS;
53,116✔
2998
  int32_t lino = 0;
53,116✔
2999
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
53,116✔
3000
  SStmVgTasksToDeploy* pVg = NULL;
53,116✔
3001
  
3002
  for (int32_t i = 0; i < vgNum; ++i) {
355,158✔
3003
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
302,042✔
3004
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
302,042✔
3005
    if (NULL == pVg) {
302,042✔
3006
      continue;
194,284✔
3007
    }
3008

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

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

3031
    for (int32_t m = taskNum - 1; m >= 0; --m) {
×
3032
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
×
3033
      if (!pExt->deployed) {
×
3034
        continue;
×
3035
      }
3036

3037
      mstDestroySStmTaskToDeployExt(pExt);
×
3038

3039
      taosArrayRemove(pVg->taskList, m);
×
3040
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
×
3041
    }
3042
    atomic_store_32(&pVg->deployed, 0);
×
3043
    taosWUnLockLatch(&pVg->lock);
×
3044
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3045
  }
3046

3047
_exit:
53,116✔
3048

3049
  if (code) {
53,116✔
3050
    if (pVg) {
×
3051
      taosWUnLockLatch(&pVg->lock);
×
3052
    }
3053

3054
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3055
  }
3056
}
53,116✔
3057

3058
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
66,952✔
3059
  if (!GOT_SNODE(snodeId)) {
66,952✔
3060
    return;
6,123✔
3061
  }
3062
  
3063
  int32_t code = TSDB_CODE_SUCCESS;
60,829✔
3064
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
60,829✔
3065
  if (NULL == pSnode) {
60,829✔
3066
    return;
18,582✔
3067
  }
3068

3069
  if (taosWTryLockLatch(&pSnode->lock)) {
42,247✔
3070
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3071
    return;
×
3072
  }
3073

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

3083
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
42,247✔
3084
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
42,247✔
3085
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
42,247✔
3086
    pSnode->triggerList = NULL;
42,247✔
3087
  }
3088

3089
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
42,247✔
3090
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
42,247✔
3091
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
42,247✔
3092
    pSnode->runnerList = NULL;
42,247✔
3093
  }
3094

3095
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
42,247✔
3096
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
42,247✔
3097
    taosWUnLockLatch(&pSnode->lock);
42,247✔
3098
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
42,247✔
3099
    return;
42,247✔
3100
  }
3101

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

3109
      mstDestroySStmTaskToDeployExt(pExt);
×
3110
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
×
3111
      taosArrayRemove(pSnode->triggerList, m);
×
3112
    }
3113
    
3114
    pSnode->triggerDeployed = 0;
×
3115
  }
3116

3117
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
×
3118
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
×
3119
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
×
3120
      if (!pExt->deployed) {
×
3121
        continue;
×
3122
      }
3123

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

3136
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,795,935✔
3137
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,795,935✔
3138
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
53,116✔
3139
  }
3140

3141
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,795,935✔
3142
    msmCleanDeployedSnodeTasks(pHb->snodeId);
66,952✔
3143
  }
3144
}
16,795,935✔
3145

3146
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
16,795,935✔
3147
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,795,935✔
3148
  if (mStreamMgmt.tCtx) {
16,795,935✔
3149
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
16,721,697✔
3150
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
16,721,697✔
3151
  }
3152
}
16,795,935✔
3153

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

3171
_exit:
234,165✔
3172

3173
  if (code) {
234,165✔
3174
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3175
  }
3176

3177
  return code;
234,165✔
3178
}
3179

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

3196
_exit:
×
3197

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

3202
  return code;
×
3203
}
3204

3205

3206

3207
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
164,511✔
3208
  int32_t code = TSDB_CODE_SUCCESS;
164,511✔
3209
  int32_t lino = 0;
164,511✔
3210
  int32_t action = STREAM_ACT_UNDEPLOY;
164,511✔
3211
  bool    dropped = false;
164,511✔
3212

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

3224
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
255,324✔
3225
    if (pAction->undeploy.doCheckpoint) {
127,662✔
3226
      pAction->undeploy.doCheckpoint = dropped ? false : true;
22,853✔
3227
    }
3228
    if (!pAction->undeploy.doCleanup) {
127,662✔
3229
      pAction->undeploy.doCleanup = dropped ? true : false;
22,853✔
3230
    }
3231
    
3232
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
127,662✔
3233
  } else {
3234
    SStmAction newAction = {0};
36,849✔
3235
    newAction.actions = action;
36,849✔
3236
    newAction.undeploy.doCheckpoint = dropped ? false : true;
36,849✔
3237
    newAction.undeploy.doCleanup = dropped ? true : false;
36,849✔
3238
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
36,849✔
3239
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
36,849✔
3240
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
73,698✔
3241
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
36,849✔
3242
    
3243
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
36,849✔
3244
  }
3245

3246
_exit:
164,511✔
3247

3248
  if (code) {
164,511✔
3249
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3250
  }
3251

3252
  return code;
164,511✔
3253
}
3254

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

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

3276
_exit:
3,308✔
3277

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

3283
  return code;
3,308✔
3284
}
3285

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

3292
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
308,023✔
3293
    return false;
×
3294
  }
3295

3296
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
308,023✔
3297
  for (int32_t i = 0; i < readerNum; ++i) {
675,952✔
3298
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
367,929✔
3299
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
367,929✔
3300
      return false;
×
3301
    }
3302
  }
3303

3304
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
308,023✔
3305
  for (int32_t i = 0; i < readerNum; ++i) {
366,713✔
3306
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
58,690✔
3307
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
58,690✔
3308
      return false;
×
3309
    }
3310
  }
3311

3312
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
308,023✔
3313
  SListNode* pNode = listHead(pStream->calcReaders);
308,023✔
3314
  for (int32_t i = 0; i < readerNum; ++i) {
903,683✔
3315
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
595,660✔
3316
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
595,660✔
3317
      return false;
×
3318
    }
3319
    pNode = TD_DLIST_NODE_NEXT(pNode);
595,660✔
3320
  }
3321

3322
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
996,107✔
3323
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
761,942✔
3324
    for (int32_t m = 0; m < runnerNum; ++m) {
1,490,379✔
3325
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
802,295✔
3326
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
802,295✔
3327
        return false;
73,858✔
3328
      }
3329
    }
3330
  }
3331
  
3332
  return true;
234,165✔
3333
}
3334

3335

3336
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
11,039,088✔
3337
  int32_t code = TSDB_CODE_SUCCESS;
11,039,088✔
3338
  int32_t lino = 0;
11,039,088✔
3339
  int32_t action = 0;
11,039,088✔
3340
  int64_t streamId = pMsg->streamId;
11,039,088✔
3341
  SStreamTask* pTask = (SStreamTask*)pMsg;
11,039,088✔
3342
  int8_t  stopped = 0;
11,039,088✔
3343

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

3353
  stopped = atomic_load_8(&pStatus->stopped);
11,039,088✔
3354
  if (stopped) {
11,039,088✔
3355
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3356
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3357
    return;
×
3358
  }
3359
  
3360
  switch (pMsg->status) {
11,039,088✔
3361
    case STREAM_STATUS_INIT:      
11,033,846✔
3362
      if (STREAM_TRIGGER_TASK != pMsg->type) {
11,033,846✔
3363
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
10,571,814✔
3364
        return;
10,571,814✔
3365
      }
3366
      
3367
      if (INT64_MIN == pStatus->lastActionTs) {
462,032✔
3368
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3369
        return;
×
3370
      }
3371
      
3372
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
462,032✔
3373
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
154,009✔
3374
        return;
154,009✔
3375
      }
3376

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

3399
_exit:
313,265✔
3400

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

3407
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
163,767✔
3408
  int32_t code = TSDB_CODE_SUCCESS;
163,767✔
3409
  int32_t lino = 0;
163,767✔
3410
  SStreamTask* pTask = (SStreamTask*)pStatus;
163,767✔
3411
  int64_t streamId = pStatus->streamId;
163,767✔
3412

3413
  msttInfo("start to handle task error, type: %d", err);
163,767✔
3414

3415
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
163,767✔
3416

3417
_exit:
163,767✔
3418

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

3425
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
3,772,469✔
3426
  int32_t code = TSDB_CODE_SUCCESS;
3,772,469✔
3427
  int32_t lino = 0;
3,772,469✔
3428
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
3,772,469✔
3429

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

3448
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
3,772,469✔
3449
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,897,164✔
3450
    if (NULL == pStatus->detailStatus) {
1,897,164✔
3451
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
174,901✔
3452
      if (NULL == pStatus->detailStatus) {
174,901✔
3453
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3454
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3455
      }
3456
    }
3457
    
3458
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,897,164✔
3459
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,897,164✔
3460
  }
3461

3462
_exit:
1,875,305✔
3463

3464
  if (code) {
3,772,469✔
3465
    // IGNORE STOP STREAM BY ERROR
3466
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3467
  }
3468
}
3,772,469✔
3469

3470
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
997,781✔
3471
  int32_t code = TSDB_CODE_SUCCESS;
997,781✔
3472
  int32_t lino = 0;
997,781✔
3473
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
997,781✔
3474

3475
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
997,781✔
3476

3477
  for (int32_t i = 0; i < num; ++i) {
30,549,438✔
3478
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
29,551,657✔
3479
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
29,551,657✔
3480
    
3481
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
29,551,657✔
3482
    if (NULL == ppStatus) {
29,551,657✔
3483
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
1,524✔
3484
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
1,524✔
3485
      continue;
1,524✔
3486
    }
3487

3488
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
29,550,133✔
3489
    int8_t stopped = atomic_load_8(&pStream->stopped);
29,550,133✔
3490
    if (stopped) {
29,550,133✔
3491
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
162,243✔
3492
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
162,243✔
3493
      continue;
162,243✔
3494
    }
3495

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

3504
    if ((*ppStatus)->status != pTask->status) {
29,387,890✔
3505
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,195,435✔
3506
        (*ppStatus)->runningStartTs = pCtx->currTs;
922,159✔
3507
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,273,276✔
3508
        if (pStream->triggerTask) {
×
3509
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3510
        }
3511
        
3512
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3513
      }
3514
    }
3515
    
3516
    (*ppStatus)->errCode = pTask->errorCode;
29,387,890✔
3517
    (*ppStatus)->status = pTask->status;
29,387,890✔
3518
    (*ppStatus)->lastUpTs = pCtx->currTs;
29,387,890✔
3519
    
3520
    if (STREAM_STATUS_RUNNING != pTask->status) {
29,387,890✔
3521
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
11,039,088✔
3522
    }
3523
    
3524
    if (STREAM_TRIGGER_TASK == pTask->type) {
29,387,890✔
3525
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
3,772,469✔
3526
    }
3527
  }
3528

3529
_exit:
997,781✔
3530

3531
  if (code) {
997,781✔
3532
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3533
  }
3534

3535
  return code;
997,781✔
3536
}
3537

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

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

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

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

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

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

3627
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3628
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3629
      break;
×
3630
    }
3631
    default: {
×
3632
      msttError("invalid task type:%d in task status", pTask->type);
×
3633
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3634
      break;
×
3635
    }
3636
  }
3637

3638
_exit:
×
3639

3640
  if (code) {
×
3641
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3642
  } else {
3643
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3644
  }
3645

3646
  return code;
×
3647
}
3648

3649
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3650
  int32_t code = TSDB_CODE_SUCCESS;
×
3651
  int32_t lino = 0;
×
3652
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3653

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

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

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

3677
_exit:
×
3678

3679
  if (code) {
×
3680
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3681
  }
3682

3683
  return code;
×
3684
}
3685

3686
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
234,165✔
3687
  int32_t code = TSDB_CODE_SUCCESS;
234,165✔
3688
  int32_t lino = 0;
234,165✔
3689
  if (NULL == pCtx->pRsp->start.taskList) {
234,165✔
3690
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
91,140✔
3691
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
91,140✔
3692
  }
3693

3694
  SStmTaskId* pId = &pAction->start.triggerId;
234,165✔
3695
  SStreamTaskStart start = {0};
234,165✔
3696
  start.task.type = STREAM_TRIGGER_TASK;
234,165✔
3697
  start.task.streamId = streamId;
234,165✔
3698
  start.task.taskId = pId->taskId;
234,165✔
3699
  start.task.seriousId = pId->seriousId;
234,165✔
3700
  start.task.nodeId = pId->nodeId;
234,165✔
3701
  start.task.taskIdx = pId->taskIdx;
234,165✔
3702

3703
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
468,330✔
3704
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
234,165✔
3705

3706
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
234,165✔
3707

3708
  return;
234,165✔
3709

3710
_exit:
×
3711

3712
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3713
}
3714

3715

3716
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
36,849✔
3717
  int32_t code = TSDB_CODE_SUCCESS;
36,849✔
3718
  int32_t lino = 0;
36,849✔
3719
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
36,849✔
3720
  if (NULL == pCtx->pRsp->undeploy.taskList) {
36,849✔
3721
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
24,508✔
3722
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
24,508✔
3723
  }
3724

3725
  SStreamTaskUndeploy undeploy;
36,849✔
3726
  for (int32_t i = 0; i < dropNum; ++i) {
201,360✔
3727
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
164,511✔
3728
    undeploy.task = *pTask;
164,511✔
3729
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
164,511✔
3730
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
164,511✔
3731

3732
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
329,022✔
3733
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
164,511✔
3734

3735
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
164,511✔
3736
  }
3737

3738
  return;
36,849✔
3739

3740
_exit:
×
3741

3742
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3743
}
3744

3745
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3746
  int32_t code = TSDB_CODE_SUCCESS;
×
3747
  int32_t lino = 0;
×
3748

3749
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3750
  if (NULL == pStream) {
×
3751
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3752
    return;
×
3753
  }
3754

3755
  if (NULL == pStream->triggerTask) {
×
3756
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3757
    return;
×
3758
  }
3759

3760
  SStreamMgmtRsp rsp = {0};
×
3761
  rsp.reqId = INT64_MIN;
×
3762
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3763
  rsp.task.streamId = streamId;
×
3764
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3765

3766
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3767

3768
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3769
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3770
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3771
  }
3772

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

3775
_exit:
×
3776

3777
  if (code) {
×
3778
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3779
  } else {
3780
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3781
  }
3782
}
3783

3784
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,308✔
3785
  int32_t code = TSDB_CODE_SUCCESS;
3,308✔
3786
  int32_t lino = 0;
3,308✔
3787

3788
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,308✔
3789
  if (NULL == pStream) {
3,308✔
3790
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3791
    return;
×
3792
  }
3793

3794
  if (NULL == pStream->triggerTask) {
3,308✔
3795
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3796
    return;
×
3797
  }
3798

3799
  SStreamMgmtRsp rsp = {0};
3,308✔
3800
  rsp.reqId = INT64_MIN;
3,308✔
3801
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,308✔
3802
  rsp.task.streamId = streamId;
3,308✔
3803
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,308✔
3804
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,308✔
3805

3806
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,308✔
3807
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,113✔
3808
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,113✔
3809
  }
3810

3811
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,616✔
3812

3813
_exit:
3,308✔
3814

3815
  if (code) {
3,308✔
3816
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3817
  } else {
3818
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,308✔
3819
  }
3820
}
3821

3822

3823
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
118,218✔
3824
  int32_t code = TSDB_CODE_SUCCESS;
118,218✔
3825
  int32_t lino = 0;
118,218✔
3826
  void* pIter = NULL;
118,218✔
3827
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
118,218✔
3828

3829
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
118,218✔
3830

3831
  while (1) {
274,322✔
3832
    pIter = taosHashIterate(pCtx->actionStm, pIter);
392,540✔
3833
    if (pIter == NULL) {
392,540✔
3834
      break;
118,218✔
3835
    }
3836

3837
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
274,322✔
3838
    SStmAction *pAction = (SStmAction *)pIter;
274,322✔
3839
    
3840
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
274,322✔
3841
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
36,849✔
3842
      continue;
36,849✔
3843
    }
3844

3845
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
237,473✔
3846
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3847
    }
3848

3849
    if (STREAM_ACT_RECALC & pAction->actions) {
237,473✔
3850
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,308✔
3851
    }
3852

3853
    if (STREAM_ACT_START & pAction->actions) {
237,473✔
3854
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
234,165✔
3855
    }
3856
  }
3857
  
3858
_exit:
118,218✔
3859

3860
  if (pIter) {
118,218✔
3861
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3862
  }
3863

3864
  if (code) {
118,218✔
3865
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3866
  }
3867

3868
  return code;
118,218✔
3869
}
3870

3871
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
16,721,697✔
3872
  int32_t  code = TSDB_CODE_SUCCESS;
16,721,697✔
3873
  int32_t  lino = 0;
16,721,697✔
3874
  int64_t* lastTs = NULL;
16,721,697✔
3875
  bool     noExists = false;
16,721,697✔
3876

3877
  while (true) {
3878
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
17,292,816✔
3879
    if (NULL == lastTs) {
17,292,816✔
3880
      if (noExists) {
787,870✔
3881
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
216,751✔
3882
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
216,751✔
3883
      }
3884

3885
      noExists = true;
571,119✔
3886
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
571,119✔
3887
      
3888
      continue;
571,119✔
3889
    }
3890

3891
    while (true) {
×
3892
      int64_t lastTsValue = atomic_load_64(lastTs);
16,504,946✔
3893
      if (pCtx->currTs > lastTsValue) {
16,504,946✔
3894
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
16,501,836✔
3895
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
16,501,836✔
3896
          return code;
16,501,836✔
3897
        }
3898

3899
        continue;
×
3900
      }
3901

3902
      return code;
3,110✔
3903
    }
3904

3905
    break;
3906
  }
3907

3908
_exit:
216,751✔
3909

3910
  if (code) {
216,751✔
3911
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
216,751✔
3912
  }
3913

3914
  return code;  
216,751✔
3915
}
3916

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

3930
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3931
    pStatus = (SStmStatus*)pIter;
×
3932

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

3946
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
3947
    if (pStatus->calcReaderNum != calcReaderNum) {
×
3948
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
3949
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3950
      continue;
×
3951
    }
3952

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

3964
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
330✔
3965
  int32_t code = TSDB_CODE_SUCCESS;
330✔
3966
  int32_t lino = 0;
330✔
3967
  int32_t minVal = watchError ? 0 : 1;
330✔
3968

3969
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
330✔
3970
    return code;
×
3971
  }
3972

3973
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
330✔
3974
    (void)sched_yield();
×
3975
  }
3976

3977
  if (watchError) {
330✔
3978
    taosHashClear(mStreamMgmt.vgroupMap);
×
3979
    taosHashClear(mStreamMgmt.snodeMap);
×
3980
    taosHashClear(mStreamMgmt.taskMap);
×
3981
    taosHashClear(mStreamMgmt.streamMap);
×
3982
    mstInfo("watch error happends, clear all maps");
×
3983
    goto _exit;
×
3984
  }
3985

3986
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
330✔
3987
    mstInfo("no stream tasks remain during watch state");
330✔
3988
    goto _exit;
330✔
3989
  }
3990

3991
  msmWatchCheckStreamMap(pCtx);
×
3992

3993
_exit:
330✔
3994

3995
  mStreamMgmt.lastTaskId += 100000;
330✔
3996

3997
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
330✔
3998

3999
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
330✔
4000

4001
  if (code) {
330✔
4002
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4003
  }
4004

4005
  return code;
330✔
4006
}
4007

4008

4009
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
9,723✔
4010
  int32_t code = TSDB_CODE_SUCCESS;
9,723✔
4011
  int32_t lino = 0;
9,723✔
4012
  SStreamHbMsg* pReq = pCtx->pReq;
9,723✔
4013

4014
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
9,723✔
4015
  
4016
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
9,723✔
4017
    goto _exit;
×
4018
  }
4019

4020
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
9,723✔
4021
  if (GOT_SNODE(pReq->snodeId)) {
9,723✔
4022
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,954✔
4023
  }
4024

4025
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
9,723✔
4026
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4027
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4028
  }
4029

4030
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
9,723✔
4031
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
330✔
4032
  }
4033

4034
_exit:
9,723✔
4035

4036
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
9,723✔
4037
  
4038
  if (code) {
9,723✔
4039
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4040

4041
    (void)msmWatchHandleEnding(pCtx, true);
×
4042
  }
4043

4044
  return code;
9,723✔
4045
}
4046

4047
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,360,962✔
4048
  int32_t listSize = taosArrayGetSize(pOReaders);
5,360,962✔
4049
  int32_t totalSize = 0;
5,360,962✔
4050
  
4051
  for (int32_t i = 0; i < listSize; ++i) {
6,382,234✔
4052
    SArray* pList = taosArrayGetP(pOReaders, i);
1,021,272✔
4053
    totalSize += taosArrayGetSize(pList);
1,021,272✔
4054
  }
4055

4056
  return totalSize;
5,360,962✔
4057
}
4058

4059
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
641,357✔
4060
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
641,357✔
4061
  if (NULL == pList) {
641,357✔
4062
    return NULL;
×
4063
  }
4064

4065
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
641,357✔
4066
}
4067

4068

4069
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
58,690✔
4070
  int32_t code = TSDB_CODE_SUCCESS;
58,690✔
4071
  int32_t lino = 0;
58,690✔
4072

4073
  if (NULL == pStatus->trigOReaders) {
58,690✔
4074
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
50,819✔
4075
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
50,819✔
4076
  }
4077

4078
  while (true) {
50,819✔
4079
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
109,509✔
4080

4081
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
109,509✔
4082
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
50,819✔
4083
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
50,819✔
4084

4085
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
101,638✔
4086
      continue;
50,819✔
4087
    }
4088

4089
    *ppRes = *ppOReaderList;
58,690✔
4090
    break;
58,690✔
4091
  }
4092

4093
_exit:
58,690✔
4094

4095
  if (code) {
58,690✔
4096
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4097
  }
4098

4099
  return code;
58,690✔
4100
}
4101

4102
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
68,738✔
4103
  int32_t code = TSDB_CODE_SUCCESS;
68,738✔
4104
  int32_t lino = 0;
68,738✔
4105
  bool    readerExists = false;
68,738✔
4106
  int64_t streamId = pTask->streamId;
68,738✔
4107

4108
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
68,738✔
4109
  for (int32_t i = 0; i < readerNum; ++i) {
143,309✔
4110
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
84,619✔
4111
    if (pReader->id.nodeId == vgId) {
84,619✔
4112
      readerExists = true;
10,048✔
4113
      break;
10,048✔
4114
    }
4115
  }
4116

4117
  if (!readerExists) {
68,738✔
4118
    SArray* pReaderList = NULL;
58,690✔
4119
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
58,690✔
4120
    
4121
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
58,690✔
4122
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
58,690✔
4123
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
58,690✔
4124
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
58,690✔
4125
  }
4126

4127
_exit:
10,048✔
4128

4129
  if (code) {
68,738✔
4130
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4131
  }
4132

4133
  return code;
68,738✔
4134
}
4135

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

4154
  if (NULL == pCtx->pRsp->rsps.rspList) {
60,282✔
4155
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4156
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4157
  }
4158
  
4159
  TSWAP(pTask->pMgmtReq, pMgmtReq);
60,282✔
4160
  rsp.task = *(SStreamTask*)pTask;
60,282✔
4161

4162
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
60,282✔
4163
  if (NULL == pStatus) {
60,282✔
4164
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4165
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4166
  }
4167

4168
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
60,282✔
4169
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4170
    goto _exit;
×
4171
  }
4172

4173
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
60,282✔
4174

4175
  stopped = atomic_load_8(&pStatus->stopped);
60,282✔
4176
  if (stopped) {
60,282✔
4177
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4178
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4179
  }
4180

4181
  if (tbNum <= 0) {
60,282✔
4182
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4183
    goto _exit;
×
4184
  }
4185

4186
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
60,282✔
4187
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
60,282✔
4188
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
60,282✔
4189

4190
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
60,282✔
4191
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
60,282✔
4192
  
4193
  for (int32_t i = 0; i < tbNum; ++i) {
239,707✔
4194
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
179,425✔
4195
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
179,425✔
4196
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
358,850✔
4197
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
179,425✔
4198
  }
4199

4200
  int32_t vgNum = tSimpleHashGetSize(pVgs);
60,282✔
4201
  while (true) {
4202
    p = tSimpleHashIterate(pVgs, p, &iter);
129,020✔
4203
    if (NULL == p) {
129,020✔
4204
      break;
60,282✔
4205
    }
4206
    
4207
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
68,738✔
4208
  }
4209
  
4210
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
60,282✔
4211
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
60,282✔
4212
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
60,282✔
4213

4214
  SStreamTaskAddr addr;
60,282✔
4215
  for (int32_t i = 0; i < vgNum; ++i) {
118,972✔
4216
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
58,690✔
4217
    addr.taskId = pOTask->id.taskId;
58,690✔
4218
    addr.nodeId = pOTask->id.nodeId;
58,690✔
4219
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
58,690✔
4220
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
117,380✔
4221
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
58,690✔
4222
  }
4223

4224
_exit:
60,282✔
4225

4226
  tFreeSStreamMgmtReq(pMgmtReq);
60,282✔
4227
  taosMemoryFree(pMgmtReq);
60,282✔
4228

4229
  tSimpleHashCleanup(pVgs);
60,282✔
4230
  mstDestroyDbVgroupsHash(pDbVgroups);
60,282✔
4231

4232
  if (code) {
60,282✔
4233
    rsp.code = code;
×
4234
    
4235
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4236

4237
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4238
  } else {
4239
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
120,564✔
4240
  }
4241

4242
_final:
60,282✔
4243

4244
  if (finalCode) {
60,282✔
4245
    tFreeSStreamMgmtRsp(&rsp);
×
4246
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4247
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4248
  }
4249

4250
  return finalCode;
60,282✔
4251
}
4252

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

4267
_exit:
2,321✔
4268

4269
  if (code) {
2,321✔
4270
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4271
  }
4272

4273
  return code;
2,321✔
4274
}
4275

4276
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,321✔
4277
  int32_t code = TSDB_CODE_SUCCESS;
2,321✔
4278
  int32_t lino = 0;
2,321✔
4279
  bool    readerExists = false;
2,321✔
4280
  int64_t streamId = pTask->streamId;
2,321✔
4281
  SListNode* pNode = listHead(pStatus->calcReaders);
2,321✔
4282
  SStmTaskStatus* pReader = NULL;
2,321✔
4283
  int32_t taskIdx = 0;
2,321✔
4284

4285
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,321✔
4286
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
21,522✔
4287
    pReader = (SStmTaskStatus*)pNode->data;
19,201✔
4288
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
19,201✔
4289
      readerExists = true;
×
4290
      pAddr->taskId = pReader->id.taskId;
×
4291
      break;
×
4292
    }
4293
  }
4294

4295
  if (!readerExists) {
2,321✔
4296
    if (NULL == pStatus->calcReaders) {
2,321✔
4297
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4298
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4299
      taskIdx = 0;
×
4300
    } else {
4301
      pNode = listTail(pStatus->calcReaders);
2,321✔
4302
      pReader = (SStmTaskStatus*)pNode->data;
2,321✔
4303
      taskIdx = pReader->id.taskIdx + 1;
2,321✔
4304
    }
4305

4306
    SStreamCalcScan* pScan = NULL;
2,321✔
4307
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,321✔
4308
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,321✔
4309
    pReader = tdListReserve(pStatus->calcReaders);
2,321✔
4310
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,321✔
4311
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,321✔
4312
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,321✔
4313
    pAddr->taskId = pReader->id.taskId;
2,321✔
4314
  }
4315

4316
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,321✔
4317
  pAddr->nodeId = vgId;
2,321✔
4318

4319
_exit:
2,321✔
4320

4321
  if (code) {
2,321✔
4322
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4323
  }
4324

4325
  return code;
2,321✔
4326
}
4327

4328

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

4346
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,321✔
4347
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4348
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4349
  }
4350
  
4351
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,321✔
4352
  rsp.task = *(SStreamTask*)pTask;
2,321✔
4353

4354
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,321✔
4355
  if (NULL == pStatus) {
2,321✔
4356
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4357
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4358
  }
4359

4360
  stopped = atomic_load_8(&pStatus->stopped);
2,321✔
4361
  if (stopped) {
2,321✔
4362
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4363
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4364
  }
4365

4366
  if (reqNum <= 0) {
2,321✔
4367
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4368
    goto _exit;
×
4369
  }
4370

4371
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,321✔
4372
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,321✔
4373

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

4389
_exit:
2,321✔
4390

4391
  tFreeSStreamMgmtReq(pMgmtReq);
2,321✔
4392
  taosMemoryFree(pMgmtReq);
2,321✔
4393

4394
  if (code) {
2,321✔
4395
    rsp.code = code;
×
4396
    
4397
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4398
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4399
  } else {
4400
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,642✔
4401
  }
4402

4403

4404
_final:
2,321✔
4405

4406
  if (finalCode) {
2,321✔
4407
    tFreeSStreamMgmtRsp(&rsp);
×
4408
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4409
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4410
  }
4411

4412
  return finalCode;
2,321✔
4413
}
4414

4415

4416
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
62,603✔
4417
  int32_t code = TSDB_CODE_SUCCESS;
62,603✔
4418
  int32_t lino = 0;
62,603✔
4419

4420
  switch (pTask->pMgmtReq->type) {
62,603✔
4421
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
60,282✔
4422
      msmDeployTriggerOrigReader(pCtx, pTask);
60,282✔
4423
      break;
60,282✔
4424
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,321✔
4425
      msmDeployRunnerOrigReader(pCtx, pTask);
2,321✔
4426
      break;
2,321✔
4427
    default:
×
4428
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4429
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4430
      break;
×
4431
  }
4432

4433
_exit:
62,603✔
4434

4435
  if (code) {
62,603✔
4436
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4437
  }
4438

4439
  return code;
62,603✔
4440
}
4441

4442
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
16,187✔
4443
  int32_t code = TSDB_CODE_SUCCESS;
16,187✔
4444
  int32_t lino = 0;
16,187✔
4445
  SStreamHbMsg* pReq = pCtx->pReq;
16,187✔
4446
  SStmTaskStatusMsg* pTask = NULL;
16,187✔
4447
  
4448
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
16,187✔
4449
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
16,187✔
4450
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
16,187✔
4451
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
16,187✔
4452
  }
4453
  
4454
  for (int32_t i = 0; i < reqNum; ++i) {
78,790✔
4455
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
62,603✔
4456
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
62,603✔
4457
    if (NULL == pTask) {
62,603✔
4458
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4459
      continue;
×
4460
    }
4461

4462
    if (NULL == pTask->pMgmtReq) {
62,603✔
4463
      msttError("idx %d without mgmtReq", idx);
×
4464
      continue;
×
4465
    }
4466

4467
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
62,603✔
4468
  }
4469

4470
_exit:
16,187✔
4471

4472
  if (code) {
16,187✔
4473
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4474
  }
4475

4476
  return code;
16,187✔
4477
}
4478

4479
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
16,711,974✔
4480
  int32_t code = TSDB_CODE_SUCCESS;
16,711,974✔
4481
  int32_t lino = 0;
16,711,974✔
4482
  SStreamHbMsg* pReq = pCtx->pReq;
16,711,974✔
4483

4484
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,711,974✔
4485
  if (GOT_SNODE(pReq->snodeId)) {
16,495,223✔
4486
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,169,669✔
4487
  }
4488
  
4489
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,494,959✔
4490
    msmHandleStreamActions(pCtx);
39,908✔
4491
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
39,908✔
4492
  }
4493

4494
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
16,494,959✔
4495
    code = msmHandleStreamRequests(pCtx);
16,187✔
4496
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
16,187✔
4497
    TAOS_CHECK_EXIT(code);
16,187✔
4498
  }
4499

4500
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,494,959✔
4501
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
53,253✔
4502
  } else {
4503
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,441,706✔
4504
  }
4505

4506
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,494,959✔
4507
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
60,829✔
4508
  }
4509

4510
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,494,959✔
4511
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
55,364✔
4512
  }
4513

4514
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,494,959✔
4515
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
997,781✔
4516
  }
4517

4518
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,494,959✔
4519
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
118,218✔
4520
  }
4521

4522
_exit:
16,494,959✔
4523

4524
  if (code) {
16,711,974✔
4525
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
217,015✔
4526
  }
4527

4528
  return code;
16,711,974✔
4529
}
4530

4531
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
16,795,935✔
4532
  int32_t lino = 0;
16,795,935✔
4533
  int32_t tlen = 0;
16,795,935✔
4534
  void   *buf = NULL;
16,795,935✔
4535

4536
  if (TSDB_CODE_SUCCESS != code) {
16,795,935✔
4537
    goto _exit;
217,015✔
4538
  }
4539

4540
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
16,578,920✔
4541
  if (code < 0) {
16,578,920✔
4542
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4543
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4544
  }
4545

4546
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
16,578,920✔
4547
  if (buf == NULL) {
16,578,920✔
4548
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4549
    TAOS_CHECK_EXIT(terrno);    
×
4550
  }
4551

4552
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
16,578,920✔
4553
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
16,578,920✔
4554

4555
  SEncoder encoder;
16,576,255✔
4556
  tEncoderInit(&encoder, abuf, tlen);
16,578,920✔
4557
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
16,578,920✔
4558
    rpcFreeCont(buf);
×
4559
    buf = NULL;
×
4560
    tEncoderClear(&encoder);
×
4561
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4562
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4563
  }
4564
  tEncoderClear(&encoder);
16,578,920✔
4565

4566
_exit:
16,795,935✔
4567

4568
  pMsg->code = code;
16,795,935✔
4569
  pMsg->info = *pRpcInfo;
16,795,935✔
4570
  if (TSDB_CODE_SUCCESS == code) {
16,795,935✔
4571
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
16,578,920✔
4572
    pMsg->pCont = buf;
16,578,920✔
4573
  }
4574
}
16,795,935✔
4575

4576

4577
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
16,795,935✔
4578
  int32_t code = TSDB_CODE_SUCCESS;
16,795,935✔
4579
  SMStreamHbRspMsg rsp = {0};
16,795,935✔
4580
  rsp.streamGId = pHb->streamGId;
16,795,935✔
4581

4582
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,795,935✔
4583

4584
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
16,795,935✔
4585
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
74,238✔
4586
    goto _exit;
74,238✔
4587
  }
4588

4589
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
16,721,697✔
4590
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
16,721,697✔
4591

4592
  pCtx->tidx = tidx;
16,721,697✔
4593
  pCtx->pMnode = pMnode;
16,721,697✔
4594
  pCtx->currTs = currTs;
16,721,697✔
4595
  pCtx->pReq = pHb;
16,721,697✔
4596
  pCtx->pRsp = &rsp;
16,721,697✔
4597
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
16,721,697✔
4598
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
16,721,697✔
4599
  
4600
  switch (atomic_load_8(&mStreamMgmt.state)) {
16,721,697✔
4601
    case MND_STM_STATE_WATCH:
9,723✔
4602
      code = msmWatchHandleHbMsg(pCtx);
9,723✔
4603
      break;
9,723✔
4604
    case MND_STM_STATE_NORMAL:
16,711,974✔
4605
      code = msmNormalHandleHbMsg(pCtx);
16,711,974✔
4606
      break;
16,711,974✔
4607
    default:
×
4608
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4609
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4610
      break;
×
4611
  }
4612

4613
_exit:
16,795,935✔
4614

4615
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
16,795,935✔
4616

4617
  msmCleanStreamGrpCtx(pHb);
16,795,935✔
4618
  msmClearStreamToDeployMaps(pHb);
16,795,935✔
4619

4620
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
16,795,935✔
4621
  
4622
  tFreeSMStreamHbRspMsg(&rsp);
16,795,935✔
4623

4624
  return code;
16,795,935✔
4625
}
4626

4627
void msmHandleBecomeLeader(SMnode *pMnode) {
397,866✔
4628
  if (tsDisableStream) {
397,866✔
4629
    return;
×
4630
  }
4631

4632
  mstInfo("start to process mnode become leader");
397,866✔
4633

4634
  int32_t code = 0;
397,866✔
4635
  streamAddVnodeLeader(MNODE_HANDLE);
397,866✔
4636
  
4637
  taosWLockLatch(&mStreamMgmt.runtimeLock);
397,866✔
4638
  msmDestroyRuntimeInfo(pMnode);
397,866✔
4639
  code = msmInitRuntimeInfo(pMnode);
397,866✔
4640
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
397,866✔
4641

4642
  if (TSDB_CODE_SUCCESS == code) {
397,866✔
4643
    atomic_store_8(&mStreamMgmt.active, 1);
397,866✔
4644
  }
4645

4646
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
397,866✔
4647
}
4648

4649
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
488,011✔
4650
  if (tsDisableStream) {
488,011✔
4651
    return;
×
4652
  }
4653

4654
  mstInfo("start to process mnode become not leader");
488,011✔
4655

4656
  streamRemoveVnodeLeader(MNODE_HANDLE);
488,011✔
4657

4658
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
488,011✔
4659
    taosWLockLatch(&mStreamMgmt.runtimeLock);
397,866✔
4660
    msmDestroyRuntimeInfo(pMnode);
397,866✔
4661
    mStreamMgmt.stat.inactiveTimes++;
397,866✔
4662
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
397,866✔
4663
  }
4664
}
4665

4666

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

4676
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
7,444✔
4677
  int32_t code = TSDB_CODE_SUCCESS;
7,444✔
4678
  int32_t lino = 0;
7,444✔
4679
  SStreamObj* pStream = pObj;
7,444✔
4680
  SSnodeObj* pSnode = p1;
7,444✔
4681
  SArray** ppRes = p2;
7,444✔
4682

4683
  if (pStream->mainSnodeId == pSnode->id) {
7,444✔
4684
    if (NULL == *ppRes) {
3,907✔
4685
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
787✔
4686
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
787✔
4687
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
787✔
4688
    }
4689

4690
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
7,814✔
4691
  }
4692

4693
  return true;
7,444✔
4694

4695
_exit:
×
4696

4697
  if (code) {
×
4698
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4699
  }  
4700

4701
  *(int32_t*)p3 = code;
×
4702

4703
  return false;
×
4704
}
4705

4706

4707
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
41,316✔
4708
  int32_t code = TSDB_CODE_SUCCESS;
41,316✔
4709
  int32_t lino = 0;
41,316✔
4710
  
4711
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
41,316✔
4712
  TAOS_CHECK_EXIT(code);
41,316✔
4713

4714
  int32_t streamNum = taosArrayGetSize(*ppRes);
41,316✔
4715
  if (streamNum > 0 && 0 == pSnode->replicaId) {
41,316✔
4716
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
195✔
4717
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
195✔
4718
  }
4719

4720
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4721

4722
_exit:
41,316✔
4723

4724
  if (code) {
41,316✔
4725
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
195✔
4726
  }  
4727

4728
  return code;
41,316✔
4729
}
4730

4731
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
582,248✔
4732
  SStreamObj* pStream = pObj;
582,248✔
4733
  int64_t streamId = pStream->pCreate->streamId;
582,248✔
4734
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
582,248✔
4735
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
582,248✔
4736
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
582,248✔
4737
  
4738
  if ((userDropped || userStopped) && (NULL == pStatus)) {
582,248✔
4739
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
195✔
4740
    return true;
195✔
4741
  }
4742
  
4743
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
582,053✔
4744
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
510,892✔
4745
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4746
    return true;
510,892✔
4747
  }
4748

4749
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
71,161✔
4750
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
51,543✔
4751
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4752
    return true;
51,543✔
4753
  }  
4754

4755
  if (pStatus) {
19,618✔
4756
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
18,566✔
4757
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4758
    }
4759

4760
    return true;
18,566✔
4761
  }
4762

4763
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
1,052✔
4764

4765
  return true;
1,052✔
4766
}
4767

4768
void msmCheckLoopStreamMap(SMnode *pMnode) {
40,451✔
4769
  SStmStatus* pStatus = NULL;
40,451✔
4770
  void* pIter = NULL;
40,451✔
4771
  int8_t stopped = 0;
40,451✔
4772
  int64_t streamId = 0;
40,451✔
4773
  
4774
  while (true) {
4775
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
269,986✔
4776
    if (NULL == pIter) {
269,986✔
4777
      break;
40,451✔
4778
    }
4779

4780
    pStatus = (SStmStatus*)pIter;
229,535✔
4781

4782
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
229,535✔
4783
    stopped = atomic_load_8(&pStatus->stopped);
229,535✔
4784
    if (MST_IS_USER_STOPPED(stopped)) {
229,535✔
4785
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
418✔
4786
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
418✔
4787
      continue;
418✔
4788
    }
4789

4790
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
229,117✔
4791
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4792
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4793
      continue;
×
4794
    }
4795

4796
    if (MST_IS_ERROR_STOPPED(stopped)) {
229,117✔
4797
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
11,193✔
4798
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
7,606✔
4799
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4800
            
4801
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
7,606✔
4802
        continue;
7,606✔
4803
      }
4804

4805
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
3,587✔
4806
      continue;
3,587✔
4807
    }
4808

4809
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
217,924✔
4810
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4811
      continue;
×
4812
    }
4813
  }
4814
}
40,451✔
4815

4816
void msmCheckStreamsStatus(SMnode *pMnode) {
329,594✔
4817
  SStmCheckStatusCtx ctx = {0};
329,594✔
4818

4819
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
329,594✔
4820
  
4821
  if (MST_READY_FOR_SDB_LOOP()) {
329,594✔
4822
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
80,718✔
4823
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
80,718✔
4824
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
80,718✔
4825
  }
4826

4827
  if (MST_READY_FOR_MAP_LOOP()) {
329,594✔
4828
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
40,451✔
4829
    msmCheckLoopStreamMap(pMnode);
40,451✔
4830
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
40,451✔
4831
  }
4832
}
329,594✔
4833

4834
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,179,130✔
4835
  for (int32_t i = 0; i < taskNum; ++i) {
6,705,611✔
4836
    SStmTaskStatus* pTask = *(pList + i);
3,526,481✔
4837

4838
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
3,526,481✔
4839
      continue;
392,826✔
4840
    }
4841
    
4842
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,135,181✔
4843
      continue;
3,134,131✔
4844
    }
4845

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

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

4858
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,050✔
4859
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,050✔
4860

4861
    SStmTaskAction task = {0};
1,050✔
4862
    task.streamId = streamId;
1,050✔
4863
    task.id = pTask->id;
1,050✔
4864
    task.flag = pTask->flags;
1,050✔
4865
    task.type = pTask->type;
1,050✔
4866
    
4867
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
1,050✔
4868
  }
4869
}
3,179,130✔
4870

4871
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
127,364✔
4872
  void* pIter = NULL;
127,364✔
4873
  SStmVgStreamStatus* pVg = NULL;
127,364✔
4874
  int64_t streamId = 0;
127,364✔
4875
  
4876
  while (true) {
1,285,600✔
4877
    pIter = taosHashIterate(pStreams, pIter);
1,412,964✔
4878
    if (NULL == pIter) {
1,412,964✔
4879
      break;
127,364✔
4880
    }
4881

4882
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,285,600✔
4883
    pVg = (SStmVgStreamStatus*)pIter;
1,285,600✔
4884

4885
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,285,600✔
4886
    if (taskNum > 0) {
1,285,600✔
4887
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
768,977✔
4888
    }
4889

4890
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,285,600✔
4891
    if (taskNum > 0) {
1,285,600✔
4892
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
847,988✔
4893
    }
4894
  }
4895
}
127,364✔
4896

4897
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4898
  int64_t streamId = 0;
×
4899
  void* pIter = NULL;
×
4900
  SStmVgStreamStatus* pStream = NULL;
×
4901

4902
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4903
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4904
    return;
×
4905
  }
4906

4907
  
4908
  while (true) {
4909
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4910
    if (NULL == pIter) {
×
4911
      break;
×
4912
    }
4913

4914
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4915
    
4916
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4917
  }
4918

4919
  taosHashClear(pVg->streamTasks);
×
4920
}
4921

4922

4923
void msmCheckVgroupStatus(SMnode *pMnode) {
329,594✔
4924
  void* pIter = NULL;
329,594✔
4925
  int32_t code = 0;
329,594✔
4926
  
4927
  while (true) {
1,177,832✔
4928
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,507,426✔
4929
    if (NULL == pIter) {
1,507,426✔
4930
      break;
329,594✔
4931
    }
4932

4933
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,177,832✔
4934
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,177,832✔
4935
      continue;
1,045,763✔
4936
    }
4937
    
4938
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
132,069✔
4939

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

4958
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
127,364✔
4959

4960
    msmCheckVgroupStreamStatus(pVg->streamTasks);
127,364✔
4961
  }
4962
}
329,594✔
4963

4964
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
1,426✔
4965
  *deployNum = 0;
1,426✔
4966
  
4967
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
4,628✔
4968
    if (pStream->runners[i]) {
4,036✔
4969
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
2,018✔
4970
      for (int32_t t = 0; t < taskNum; ++t) {
3,202✔
4971
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
2,018✔
4972
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
2,018✔
4973
        if (stopped) {
2,018✔
4974
          mstsDebug("stream already stopped %d, ignore it", stopped);
834✔
4975
          *deployNum = 0;
834✔
4976
          return;
834✔
4977
        }
4978

4979
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
1,184✔
4980
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
1,184✔
4981
      }
4982
      
4983
      deployId[*deployNum] = i;
1,184✔
4984
      (*deployNum)++;
1,184✔
4985
    }
4986
  }
4987
}
4988

4989
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
3,094✔
4990
  pSnode->runnerThreadNum = -1;
3,094✔
4991

4992
  (void)msmSTAddSnodesToMap(pMnode);
3,094✔
4993

4994
  int64_t streamId = 0;
3,094✔
4995
  void* pIter = NULL;
3,094✔
4996
  SStmSnodeStreamStatus* pStream = NULL;
3,094✔
4997
  int32_t deployNum = 0;
3,094✔
4998
  SStmTaskAction task = {0};
3,094✔
4999
  
5000
  while (true) {
5001
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
5,354✔
5002
    if (NULL == pIter) {
5,354✔
5003
      break;
3,094✔
5004
    }
5005

5006
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
2,260✔
5007
    
5008
    task.streamId = streamId;
2,260✔
5009
    
5010
    pStream = (SStmSnodeStreamStatus*)pIter;
2,260✔
5011
    if (pStream->trigger) {
2,260✔
5012
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
834✔
5013
      if (stopped) {
834✔
5014
        mstsDebug("stream already stopped %d, ignore it", stopped);
659✔
5015
        continue;
659✔
5016
      }
5017

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

5036
  taosHashClear(pSnode->streamTasks);
3,094✔
5037
}
3,094✔
5038

5039

5040
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
35,838✔
5041
  void* pIter = NULL;
35,838✔
5042
  SStmSnodeStreamStatus* pSnode = NULL;
35,838✔
5043
  int64_t streamId = 0;
35,838✔
5044
  
5045
  while (true) {
5046
    pIter = taosHashIterate(pStreams, pIter);
446,940✔
5047
    if (NULL == pIter) {
446,940✔
5048
      break;
35,838✔
5049
    }
5050

5051
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
411,102✔
5052
    pSnode = (SStmSnodeStreamStatus*)pIter;
411,102✔
5053

5054
    if (NULL != pSnode->trigger) {
411,102✔
5055
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
399,397✔
5056
    }
5057

5058
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,644,408✔
5059
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,233,306✔
5060
      if (taskNum > 0) {
1,233,306✔
5061
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,162,768✔
5062
      }
5063
    }
5064
  }
5065
}
35,838✔
5066

5067

5068
void msmCheckSnodeStatus(SMnode *pMnode) {
329,594✔
5069
  void* pIter = NULL;
329,594✔
5070
  
5071
  while (true) {
415,543✔
5072
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
745,137✔
5073
    if (NULL == pIter) {
745,137✔
5074
      break;
329,594✔
5075
    }
5076

5077
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
415,543✔
5078
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
415,543✔
5079
      continue;
367,150✔
5080
    }
5081

5082
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
48,393✔
5083
    
5084
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
48,393✔
5085
    if (NULL == pSnode->streamTasks) {
48,393✔
5086
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
10,120✔
5087
      continue;
10,120✔
5088
    }
5089
    
5090
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
38,273✔
5091
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
2,435✔
5092
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5093
      
5094
      msmHandleSnodeLost(pMnode, pSnode);
2,435✔
5095
      continue;
2,435✔
5096
    }
5097
    
5098
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
35,838✔
5099

5100
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
35,838✔
5101
  }
5102
}
329,594✔
5103

5104

5105
void msmCheckTasksStatus(SMnode *pMnode) {
329,594✔
5106
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
329,594✔
5107

5108
  msmCheckVgroupStatus(pMnode);
329,594✔
5109
  msmCheckSnodeStatus(pMnode);
329,594✔
5110
}
329,594✔
5111

5112
void msmCheckSnodesState(SMnode *pMnode) {
329,594✔
5113
  if (!MST_READY_FOR_SNODE_LOOP()) {
329,594✔
5114
    return;
294,146✔
5115
  }
5116

5117
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
35,448✔
5118

5119
  void* pIter = NULL;
35,448✔
5120
  int32_t snodeId = 0;
35,448✔
5121
  while (true) {
39,717✔
5122
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
75,165✔
5123
    if (NULL == pIter) {
75,165✔
5124
      break;
35,448✔
5125
    }
5126

5127
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
39,717✔
5128
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
39,717✔
5129
      continue;
39,058✔
5130
    }
5131

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

5145
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
35,448✔
5146
}
5147

5148
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
10,599,733✔
5149
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
10,599,733✔
5150
  if (0 == active || MND_STM_STATE_NORMAL != state) {
10,599,733✔
5151
    mstTrace("ignore health check since active:%d state:%d", active, state);
2,296✔
5152
    return false;
2,296✔
5153
  }
5154

5155
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
10,597,437✔
5156
    mstTrace("ignore health check since no stream now");
9,938,249✔
5157
    return false;
9,938,249✔
5158
  }
5159

5160
  return true;
659,188✔
5161
}
5162

5163
void msmHealthCheck(SMnode *pMnode) {
10,270,139✔
5164
  if (!msmCheckNeedHealthCheck(pMnode)) {
10,270,139✔
5165
    return;
9,940,545✔
5166
  }
5167

5168
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
634,038✔
5169
  
5170
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
329,594✔
5171
  if (!msmCheckNeedHealthCheck(pMnode)) {
329,594✔
5172
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5173
    return;
×
5174
  }
5175
  
5176
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
329,594✔
5177
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
329,594✔
5178

5179
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
329,594✔
5180
  
5181
  msmCheckStreamsStatus(pMnode);
329,594✔
5182
  msmCheckTasksStatus(pMnode);
329,594✔
5183
  msmCheckSnodesState(pMnode);
329,594✔
5184

5185
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
329,594✔
5186

5187
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
329,594✔
5188
}
5189

5190
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
797✔
5191
  SStreamObj *pStream = pObj;
797✔
5192
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
797✔
5193
    return true;
×
5194
  }
5195
  
5196
  pStream->updateTime = *(int64_t*)p1;
797✔
5197
  
5198
  (*(int32_t*)p2)++;
797✔
5199
  
5200
  return true;
797✔
5201
}
5202

5203
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
20,424✔
5204
  int32_t code = 0;
20,424✔
5205
  int8_t  stopped = 0;
20,424✔
5206
  
5207
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
20,424✔
5208
  
5209
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
20,424✔
5210
  if (NULL == pStatus) {
20,424✔
5211
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
2,664✔
5212
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
2,664✔
5213
    goto _exit;
2,664✔
5214
  }
5215

5216
  stopped = atomic_load_8(&pStatus->stopped);
17,760✔
5217
  if (stopped) {
17,760✔
5218
    mstsError("stream already stopped, stopped:%d", stopped);
×
5219
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5220
    goto _exit;
×
5221
  }
5222

5223
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
17,760✔
5224
    pAddr->taskId = pStatus->triggerTask->id.taskId;
13,764✔
5225
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
13,764✔
5226
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
13,764✔
5227
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
13,764✔
5228
    goto _exit;
13,764✔
5229
  }
5230

5231
  mstsError("trigger task %p not running, status:%s", pStatus->triggerTask, pStatus->triggerTask ? gStreamStatusStr[pStatus->triggerTask->status] : "unknown");
3,996✔
5232
  code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
3,996✔
5233

5234
_exit:
20,424✔
5235
  
5236
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
20,424✔
5237

5238
  return code;
20,424✔
5239
}
5240

5241
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
397,866✔
5242
  int32_t code = TSDB_CODE_SUCCESS;
397,866✔
5243
  int32_t lino = 0;
397,866✔
5244
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
397,866✔
5245
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
397,866✔
5246
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
397,866✔
5247

5248
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
795,598✔
5249

5250
  mStreamMgmt.stat.activeTimes++;
397,866✔
5251
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
397,866✔
5252
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
397,866✔
5253
  if (NULL == mStreamMgmt.tCtx) {
397,866✔
5254
    code = terrno;
×
5255
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5256
    goto _exit;
×
5257
  }
5258

5259
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
397,866✔
5260
  if (mStreamMgmt.actionQ == NULL) {
397,866✔
5261
    code = terrno;
×
5262
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5263
    goto _exit;
×
5264
  }
5265
  
5266
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
397,866✔
5267
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
397,866✔
5268
  
5269
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
397,866✔
5270
  
5271
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,387,001✔
5272
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,989,135✔
5273

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

5316
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
397,866✔
5317
  if (mStreamMgmt.snodeMap == NULL) {
397,866✔
5318
    code = terrno;
×
5319
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5320
    goto _exit;
×
5321
  }
5322
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
397,866✔
5323
  
5324
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
397,866✔
5325
  if (mStreamMgmt.dnodeMap == NULL) {
397,866✔
5326
    code = terrno;
×
5327
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5328
    goto _exit;
×
5329
  }
5330

5331
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
397,866✔
5332
  if (mStreamMgmt.toDeployVgMap == NULL) {
397,866✔
5333
    code = terrno;
×
5334
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5335
    goto _exit;
×
5336
  }
5337
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
397,866✔
5338
  
5339
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
397,866✔
5340
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
397,866✔
5341
    code = terrno;
×
5342
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5343
    goto _exit;
×
5344
  }
5345
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
397,866✔
5346

5347
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
397,866✔
5348
  if (mStreamMgmt.toUpdateScanMap == NULL) {
397,866✔
5349
    code = terrno;
×
5350
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5351
    goto _exit;
×
5352
  }
5353
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
397,866✔
5354

5355
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
397,866✔
5356
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
397,866✔
5357

5358
  mStreamMgmt.lastTaskId = 1;
397,866✔
5359

5360
  int32_t activeStreamNum = 0;
397,866✔
5361
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
397,866✔
5362

5363
  if (activeStreamNum > 0) {
397,866✔
5364
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
647✔
5365
  } else {
5366
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
397,219✔
5367
  }
5368

5369
_exit:
397,866✔
5370

5371
  if (code) {
397,866✔
5372
    msmDestroyRuntimeInfo(pMnode);
×
5373
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5374
  } else {
5375
    mstInfo("mnode stream runtime init done");
397,866✔
5376
  }
5377

5378
  return code;
397,866✔
5379
}
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