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

taosdata / TDengine / #4971

28 Feb 2026 08:05AM UTC coverage: 67.671% (-0.04%) from 67.707%
#4971

push

travis-ci

web-flow
fix(planner): disable project block merge in non-top-level subplans (#34617)

208281 of 307783 relevant lines covered (67.67%)

130135765.28 hits per line

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

75.78
/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() {
782,812✔
34
  SStmQNode* pQNode = NULL;
782,812✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
782,812✔
37
    return;
391,406✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,956,821✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
11,740,926✔
49
    taosHashCleanup(pCtx->deployStm[m]);
9,784,105✔
50
    taosHashCleanup(pCtx->actionStm[m]);
9,784,105✔
51
  }
52
}
1,956,821✔
53

54
void msmDestroyThreadCtxs() {
782,812✔
55
  if (NULL == mStreamMgmt.tCtx) {
782,812✔
56
    return;
391,406✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,348,227✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,956,821✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
391,406✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
782,812✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
782,812✔
94
}
782,812✔
95

96

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

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

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

111
    pStatus = pStream;
354✔
112
  }
113

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

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

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

136
_exit:
456✔
137

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

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

145

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

186
    tasks.lastUpTs = taosGetTimestampMs();
34,468✔
187
    code = taosHashPut(mStreamMgmt.snodeMap, &pSnode->id, sizeof(pSnode->id), &tasks, sizeof(tasks));
34,468✔
188
    if (code && TSDB_CODE_DUP_KEY != code) {
34,468✔
189
      sdbRelease(pMnode->pSdb, pSnode);
×
190
      sdbCancelFetch(pMnode->pSdb, pIter);
×
191
      pSnode = NULL;
×
192
      TAOS_CHECK_EXIT(code);
×
193
    }
194

195
    code = TSDB_CODE_SUCCESS;
34,468✔
196
  
197
    sdbRelease(pMnode->pSdb, pSnode);
34,468✔
198
  }
199

200
  pSnode = NULL;
413,516✔
201

202
_exit:
413,516✔
203

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

208
  return code;
413,516✔
209
}
210

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

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

235
  pDnode = NULL;
896,095✔
236

237
_exit:
896,095✔
238

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

243
  return code;
896,095✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
547,174✔
275
  int32_t code = TSDB_CODE_SUCCESS;
547,174✔
276
  int32_t lino = 0;
547,174✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
547,174✔
278
  if (NULL == pStream) {
547,174✔
279
    SStmVgStreamStatus stream = {0};
405,786✔
280
    if (trigReader) {
405,786✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
257,589✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
257,589✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
515,178✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
148,197✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
148,197✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
296,394✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
405,786✔
290
    goto _exit;
405,786✔
291
  }
292
  
293
  if (trigReader) {
141,388✔
294
    if (NULL == pStream->trigReaders) {
22,612✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
22,612✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
22,612✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
45,224✔
300
    goto _exit;
22,612✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
118,776✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
89,648✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
89,648✔
306
  }
307

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

310
_exit:
118,776✔
311

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

320
  return code;
547,174✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
69,131✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
69,131✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
69,131✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
478,043✔
339
  }
340
  
341
_exit:
547,052✔
342

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

350
  return code;
547,174✔
351
}
352

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

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

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

404
  return code;
548,236✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
750,230✔
409
  int32_t code = TSDB_CODE_SUCCESS;
750,230✔
410
  int32_t lino = 0;
750,230✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
750,230✔
412
  if (NULL == pStream) {
750,230✔
413
    SStmSnodeStreamStatus stream = {0};
185,100✔
414
    if (deployId < 0) {
185,100✔
415
      stream.trigger = pStatus;
3,228✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
181,872✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
181,872✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
363,744✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
185,100✔
423
    goto _exit;
185,100✔
424
  }
425
  
426
  if (deployId < 0) {
565,130✔
427
    if (NULL != pStream->trigger) {
175,496✔
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;
175,496✔
433
    goto _exit;
175,496✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
389,634✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
344,793✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
344,793✔
439
  }
440

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

443
_exit:
389,634✔
444

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

453
  return code;
750,230✔
454
}
455

456

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

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

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

484
  return code;
750,230✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
178,724✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
40,578✔
568
      ext.deployed = false;
40,578✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
81,156✔
570

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

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

614
  return code;
571,506✔
615
}
616

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

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

632
_exit:
526,665✔
633

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

638
  return code;
526,665✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
11,458✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
259✔
682

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

687
  return code;  
259✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
68,536✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
14,856,398✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
59,690,647✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
44,834,249✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
44,834,249✔
725
  }
726

727
_exit:
14,856,398✔
728

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

733
  return code;
14,856,398✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
342,690✔
739
  int32_t num = taosArrayGetSize(pList);
342,690✔
740
  for (int32_t i = 0; i < num; ++i) {
978,600✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
809,902✔
742
    if (pScan->readFromCache) {
809,902✔
743
      return pScan->scanPlan;
173,992✔
744
    }
745
  }
746

747
  return NULL;
168,698✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
548,236✔
775
}
776

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

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

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

805
_exit:
175,496✔
806

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

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

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

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

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

893
  pMsg->leaderSnodeId = pStream->mainSnodeId;
178,724✔
894
  pMsg->streamName = pInfo->streamName;
178,724✔
895

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

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

903
_exit:
175,496✔
904

905
  if (code) {
175,496✔
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));
175,496✔
909
  }
910
  
911
  return TSDB_CODE_SUCCESS;
175,496✔
912
}
913

914

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

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

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

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

959

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

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

983

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

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

1001
  return code;
705,389✔
1002
}
1003

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

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

1012

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

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

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

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

1037
_exit:
907,586✔
1038

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

1043
  return code;
907,586✔
1044
}
1045

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

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

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

1107
  int32_t snodeTarget = taosRand() % snodeNum;
532,641✔
1108

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

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

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

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

1146
_exit:
535,251✔
1147

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

1152
  if (0 == snodeId) {
535,251✔
1153
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,610✔
1154
  }
1155

1156
  return snodeId;
535,251✔
1157
}
1158

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

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

1170
_exit:
705,389✔
1171

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

1176
  return snodeId;
705,389✔
1177
}
1178

1179

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

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

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

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

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

1212
_exit:
178,724✔
1213

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

1218
  return code;
178,724✔
1219
}
1220

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

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

1247
_exit:
280,201✔
1248

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

1253
  return code;
280,201✔
1254
}
1255

1256
static int32_t msmTDAddTrigReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
178,724✔
1257
  int32_t code = TSDB_CODE_SUCCESS;
178,724✔
1258
  int32_t lino = 0;
178,724✔
1259
  int64_t streamId = pStream->pCreate->streamId;
178,724✔
1260
  SSdb   *pSdb = pCtx->pMnode->pSdb;
178,724✔
1261
  SStmTaskStatus* pState = NULL;
178,724✔
1262
  SVgObj *pVgroup = NULL;
178,724✔
1263
  SDbObj* pDb = NULL;
178,724✔
1264
  
1265
  switch (pStream->pCreate->triggerTblType) {
178,724✔
1266
    case TSDB_NORMAL_TABLE:
103,128✔
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);
103,128✔
1271
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
103,128✔
1272
      pState = taosArrayGet(pInfo->trigReaders, 0);
103,128✔
1273
      
1274
      TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, pStream->pCreate->triggerTblVgId, pInfo, streamId));
103,128✔
1275
      break;
103,128✔
1276
    }
1277
    case TSDB_SUPER_TABLE: {
74,617✔
1278
      pDb = mndAcquireDb(pCtx->pMnode, pStream->pCreate->triggerDB);
74,617✔
1279
      if (NULL == pDb) {
74,617✔
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));
74,617✔
1286
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
74,617✔
1287
      
1288
      void *pIter = NULL;
74,617✔
1289
      while (1) {
385,902✔
1290
        SStmTaskDeploy info = {0};
460,519✔
1291
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
460,519✔
1292
        if (pIter == NULL) {
460,519✔
1293
          break;
74,617✔
1294
        }
1295
      
1296
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
385,902✔
1297
          pState = taosArrayReserve(pInfo->trigReaders, 1);
117,548✔
1298

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

1308
        sdbRelease(pSdb, pVgroup);
385,902✔
1309
      }
1310
      break;
74,617✔
1311
    }
1312
    default:
979✔
1313
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
979✔
1314
      break;
979✔
1315
  }
1316

1317
_exit:
178,724✔
1318

1319
  mndReleaseDb(pCtx->pMnode, pDb);
178,724✔
1320

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

1325
  return code;
178,724✔
1326
}
1327

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

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

1353
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
264,697✔
1354
  if (NULL == ppRes) {
264,697✔
1355
    SArray* pRes = taosArrayInit(1, sizeof(addr));
264,697✔
1356
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
264,697✔
1357
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
529,394✔
1358
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
264,697✔
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);
264,697✔
1364
  
1365
  (void)atomic_add_fetch_32(&mStreamMgmt.toUpdateScanNum, 1);
264,697✔
1366
  
1367
_exit:
264,697✔
1368

1369
  nodesDestroyNode((SNode*)pSubplan);
264,697✔
1370

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

1375
  return code;
264,697✔
1376
}
1377

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

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

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

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

1414
  return code;
81,471✔
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) {
266,973✔
1419
  int32_t code = TSDB_CODE_SUCCESS;
266,973✔
1420
  int32_t lino = 0;
266,973✔
1421

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

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

1446
_exit:
266,973✔
1447

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

1452
  return code;
266,973✔
1453
}
1454

1455

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

1465
  
1466
  for (int32_t i = 0; i < calcTasksNum; ++i) {
524,715✔
1467
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
345,991✔
1468
    if (pScan->readFromCache) {
345,991✔
1469
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
81,471✔
1470
      continue;
81,471✔
1471
    }
1472
    
1473
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
264,520✔
1474
    for (int32_t m = 0; m < vgNum; ++m) {
529,040✔
1475
      pState = tdListReserve(pInfo->calcReaders);
264,520✔
1476
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
264,520✔
1477

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

1483
_exit:
178,724✔
1484

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

1489
  return code;
178,724✔
1490
}
1491

1492

1493

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

1521
_exit:
177✔
1522

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

1527
  return code;
177✔
1528
}
1529

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

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

1546
  if (code) {
178,724✔
1547
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1548
  }
1549
  
1550
  return code;
178,724✔
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,082,991✔
1554
  SDownstreamSourceNode source = {
1,082,991✔
1555
      .type = QUERY_NODE_DOWNSTREAM_SOURCE,
1556
      .clientId = clientId,
1557
      .taskId = pSrc->taskId,
1,082,991✔
1558
      .sId = 0,
1559
      .execId = 0,
1560
      .fetchMsgType = msgType,
1561
      .localExec = false,
1562
  };
1563

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

1567
  msttDebug("try to update subplan %d grp %d sourceAddr from subplan %" PRId64 ", clientId:%" PRIx64 ", srcTaskId:%" PRIx64 ", srcNodeId:%d, msgType:%s", 
1,082,991✔
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,082,991✔
1571
}
1572

1573
int32_t msmGetTaskIdFromSubplanId(SStreamObj* pStream, SArray* pRunners, int32_t beginIdx, int32_t subplanId, int64_t* taskId, SStreamTask** ppParent) {
44,841✔
1574
  int64_t streamId = pStream->pCreate->streamId;
44,841✔
1575
  int32_t runnerNum = taosArrayGetSize(pRunners);
44,841✔
1576
  for (int32_t i = beginIdx; i < runnerNum; ++i) {
130,962✔
1577
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
130,962✔
1578
    SSubplan* pPlan = pDeploy->msg.runner.pPlan;
130,962✔
1579
    if (pPlan->id.subplanId == subplanId) {
130,962✔
1580
      *taskId = pDeploy->task.taskId;
44,841✔
1581
      *ppParent = &pDeploy->task;
44,841✔
1582
      return TSDB_CODE_SUCCESS;
44,841✔
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) {
571,506✔
1592
  int32_t code = TSDB_CODE_SUCCESS;
571,506✔
1593
  int32_t lino = 0;
571,506✔
1594
  int64_t key[2] = {streamId, -1};
571,506✔
1595
  SNode* pNode = NULL;
571,506✔
1596
  SStreamTask* pTask = &pDeploy->task;
571,506✔
1597
  FOREACH(pNode, pPlan->pChildren) {
1,654,497✔
1598
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,082,991✔
1599
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
44,841✔
1600
      continue;
44,841✔
1601
    }
1602
    
1603
    SValueNode* pVal = (SValueNode*)pNode;
1,038,150✔
1604
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,038,150✔
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,038,150✔
1610

1611
    SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
1,038,150✔
1612
    if (NULL == ppRes) {
1,038,150✔
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,038,150✔
1618
    for (int32_t i = 0; i < childrenNum; ++i) {
2,076,300✔
1619
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,038,150✔
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,038,150✔
1621
    }
1622
  }
1623

1624
_exit:
571,506✔
1625

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

1630
  return code;
571,506✔
1631
}
1632

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

1641
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
571,506✔
1642

1643
  SNode* pTmp = NULL;
571,506✔
1644
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,654,497✔
1645
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,082,991✔
1646
      ERASE_NODE(pPlan->pChildren);
1,038,150✔
1647
      continue;
1,038,150✔
1648
    }
1649
    WHERE_NEXT;
44,841✔
1650
  }
1651
  nodesClearList(pPlan->pChildren);
571,506✔
1652
  pPlan->pChildren = NULL;
571,506✔
1653

1654
  if (NULL == pPlan->pParents) {
571,506✔
1655
    goto _exit;
526,665✔
1656
  }
1657

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

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

1677
  return code;
571,506✔
1678
}
1679

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

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

1695
_exit:
526,665✔
1696

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

1701
  return code;
526,665✔
1702
}
1703

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

1717
  if (pDag->numOfSubplans <= 0) {
175,496✔
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) {
175,496✔
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);
175,496✔
1728
  if (levelNum <= 0) {
175,496✔
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;
175,496✔
1734
  
1735
  plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, 0);
175,496✔
1736
  if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
175,496✔
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);
175,496✔
1742
  if (taskNum != 1) {
175,496✔
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);
175,496✔
1748
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
175,496✔
1749
  
1750
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
701,984✔
1751
    totalTaskNum = 0;
526,488✔
1752

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

1759
    taskIdx = 0;
526,488✔
1760
    
1761
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,070,598✔
1762
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
544,110✔
1763
      if (NULL == plans) {
544,110✔
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)) {
544,110✔
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);
544,110✔
1774
      if (taskNum <= 0) {
544,110✔
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;
544,110✔
1780
      if (totalTaskNum > pDag->numOfSubplans) {
544,110✔
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,115,439✔
1786
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
571,329✔
1787
        pState = taosArrayReserve(deployList, 1);
571,329✔
1788

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

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

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

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

1819
    if (totalTaskNum != pDag->numOfSubplans) {
526,488✔
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));
526,488✔
1825

1826
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
526,488✔
1827

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

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

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

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

1849
  taosArrayDestroy(deployTaskList);
175,496✔
1850
  nodesDestroyNode((SNode *)pDag);
175,496✔
1851

1852
  return code;
175,496✔
1853
}
1854

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

1872
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
354✔
1873
    deployId = pAction->deployId[r];
177✔
1874

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

1877
    pStartRunner = pRunner;
177✔
1878
    totalTaskNum = 0;
177✔
1879

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

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

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

1897
        int32_t newTaskIdx = MND_SET_RUNNER_TASKIDX(i, n);
177✔
1898
        if (pRunner->id.taskIdx != newTaskIdx) {
177✔
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;
177✔
1904

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

1915
        pRunner++;
177✔
1916
      }
1917

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

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

1923
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
177✔
1924

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

1927
    nodesDestroyNode((SNode *)pDag);
177✔
1928
    pDag = NULL;
177✔
1929

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

1933
_exit:
177✔
1934

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

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

1942
  return code;
177✔
1943
}
1944

1945

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

1954
_exit:
170,158✔
1955

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

1960
  return code;
170,158✔
1961
}
1962

1963

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

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

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

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

1986
  taosHashClear(mStreamMgmt.toUpdateScanMap);
175,496✔
1987
  mStreamMgmt.toUpdateScanNum = 0;
175,496✔
1988

1989
_exit:
175,496✔
1990

1991
  nodesDestroyNode((SNode *)pPlan);
175,496✔
1992

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

1997
  return code;
175,496✔
1998
}
1999

2000

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

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

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

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

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

2024
  return code;
178,724✔
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) {
173,386✔
2078
  int32_t code = TSDB_CODE_SUCCESS;
173,386✔
2079
  int32_t lino = 0;
173,386✔
2080
  int64_t streamId = pStream->pCreate->streamId;
173,386✔
2081

2082
  pStatus->lastActionTs = INT64_MIN;
173,386✔
2083

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

2089
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
173,386✔
2090
  
2091
  if (pStream->pCreate->numOfCalcSubplan > 0) {
173,386✔
2092
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
170,158✔
2093
    
2094
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
170,158✔
2095
  }
2096

2097
  if (initList) {
173,386✔
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:
173,386✔
2116

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

2121
  return code;
173,386✔
2122
}
2123

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

2130
  if (NULL == pStatus) {
178,724✔
2131
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
173,386✔
2132

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

2135
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
173,386✔
2136
  }
2137
  
2138
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
178,724✔
2139

2140
  mstLogSStmStatus("stream deployed", streamId, pStatus);
178,724✔
2141

2142
_exit:
178,724✔
2143

2144
  if (code) {
178,724✔
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;
178,724✔
2154
}
2155

2156

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

2161
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
9,406✔
2162
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
3,100✔
2163
    (void)mstWaitLock(&pVg->lock, true);
3,100✔
2164

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

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

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

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

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

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

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

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

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

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

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

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

2265
  mstResetSStmStatus(pStatus);
5,869✔
2266

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

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

2278
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
180,184✔
2279
  if (pStatus) {
180,184✔
2280
    stopped = atomic_load_8(&pStatus->stopped);
5,869✔
2281
    if (0 == stopped) {
5,869✔
2282
      mstsDebug("stream %s will try to reset and redeploy it", pAction->streamName);
531✔
2283
      msmResetStreamForRedeploy(streamId, pStatus);
531✔
2284
    } else {
2285
      if (MST_IS_USER_STOPPED(stopped) && !pAction->userAction) {
5,338✔
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,338✔
2291
        mstsDebug("stream %s will try to reset and redeploy it from stopped %d", pAction->streamName, stopped);
5,338✔
2292
        msmResetStreamForRedeploy(streamId, pStatus);
5,338✔
2293
      }
2294
    }
2295
  }
2296

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

2303
  TAOS_CHECK_EXIT(code);
179,255✔
2304

2305
  if (pStatus && pStream->pCreate->streamId != streamId) {
179,255✔
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);
179,255✔
2313
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
179,255✔
2314
  if (userStopped || userDropped) {
179,255✔
2315
    mstsWarn("stream %s is stopped %d or removing %d, ignore deploy", streamName, userStopped, userDropped);
531✔
2316
    goto _exit;
531✔
2317
  }
2318
  
2319
  TAOS_CHECK_EXIT(msmDeployStreamTasks(pCtx, pStream, pStatus));
178,724✔
2320

2321
_exit:
179,255✔
2322

2323
  mndReleaseStream(pCtx->pMnode, pStream);
179,255✔
2324

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

2329
  return code;
179,255✔
2330
}
2331

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

2364
_exit:
1,062✔
2365

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

2370
  return code;
1,062✔
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) {
177✔
2415
  int32_t code = TSDB_CODE_SUCCESS;
177✔
2416
  int32_t lino = 0;
177✔
2417
  int64_t streamId = pAction->streamId;
177✔
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;
177✔
2432
  pCtx->triggerNodeId = pStatus->triggerTask->id.nodeId;
177✔
2433
//  }
2434
  
2435
  TAOS_CHECK_EXIT(msmUPPrepareReaderTasks(pCtx, pStatus, pStream));
177✔
2436
  
2437
  SQueryPlan* pPlan = NULL;
177✔
2438
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
177✔
2439
  
2440
  TAOS_CHECK_EXIT(msmReBuildRunnerTasks(pCtx, pPlan, pStatus, pStream, pAction));
177✔
2441
  
2442
  taosHashClear(mStreamMgmt.toUpdateScanMap);
177✔
2443
  mStreamMgmt.toUpdateScanNum = 0;
177✔
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:
177✔
2464

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

2469
  return code;
177✔
2470
}
2471

2472

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

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

2482
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &pAction->streamId, sizeof(pAction->streamId));
1,239✔
2483
  if (NULL == pStatus) {
1,239✔
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,239✔
2489
  if (stopped) {
1,239✔
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,239✔
2495
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
1,239✔
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,239✔
2501

2502
  int8_t userStopped = atomic_load_8(&pStream->userStopped);
1,239✔
2503
  int8_t userDropped = atomic_load_8(&pStream->userDropped);
1,239✔
2504
  if (userStopped || userDropped) {
1,239✔
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,239✔
2510
    case STREAM_READER_TASK:
1,062✔
2511
      TAOS_CHECK_EXIT(msmReLaunchReaderTask(pStream, pAction, pStatus));
1,062✔
2512
      break;
1,062✔
2513
/*
2514
    case STREAM_TRIGGER_TASK:
2515
      TAOS_CHECK_EXIT(msmReLaunchTriggerTask(pCtx, pStream, pAction, pStatus));
2516
      break;
2517
*/
2518
    case STREAM_RUNNER_TASK:
177✔
2519
      if (pAction->multiRunner) {
177✔
2520
        TAOS_CHECK_EXIT(msmReLaunchRunnerDeploy(pCtx, pStream, pAction, pStatus));
177✔
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;
177✔
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,239✔
2533

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

2538
  if (code) {
1,239✔
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,239✔
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) {
437✔
2595
  int32_t code = TSDB_CODE_SUCCESS;
437✔
2596
  int32_t lino = 0;
437✔
2597

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

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

2602
_exit:
437✔
2603

2604
  if (code) {
437✔
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));
437✔
2608
  }
2609

2610
  return code;
437✔
2611
}
2612

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

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

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

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

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

2633
_exit:
×
2634

2635
  taosHashRelease(mStreamMgmt.streamMap, pStream);
28,562✔
2636

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

2641
  return;
28,562✔
2642
}
2643

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

2648
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
3,504✔
2649
  if (0 == active || MND_STM_STATE_NORMAL != state) {
3,504✔
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,504✔
2655
  if (NULL == pStream || !STREAM_IS_RUNNING(pStream)) {
3,504✔
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,504✔
2662

2663
_exit:
3,504✔
2664

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

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

2671
  return code;
3,504✔
2672
}
2673

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

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

2695
_exit:
38,778✔
2696

2697
  if (code) {
38,778✔
2698
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2699
  }
2700
}
38,778✔
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,298,466✔
2740
  int32_t code = TSDB_CODE_SUCCESS;
1,298,466✔
2741
  int32_t lino = 0;
1,298,466✔
2742
  int64_t streamId = pDeploy->task.streamId;
1,298,466✔
2743
  
2744
  switch (pDeploy->task.type) {
1,298,466✔
2745
    case STREAM_READER_TASK:
548,236✔
2746
      if (NULL == pStream->readerTasks) {
548,236✔
2747
        pStream->streamId = streamId;
233,285✔
2748
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
233,285✔
2749
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
233,285✔
2750
      }
2751
      
2752
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,096,472✔
2753
      break;
548,236✔
2754
    case STREAM_TRIGGER_TASK:
178,724✔
2755
      pStream->streamId = streamId;
178,724✔
2756
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
178,724✔
2757
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
178,724✔
2758
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
178,724✔
2759
      break;
178,724✔
2760
    case STREAM_RUNNER_TASK:
571,506✔
2761
      if (NULL == pStream->runnerTasks) {
571,506✔
2762
        pStream->streamId = streamId;
182,049✔
2763
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
182,049✔
2764
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
182,049✔
2765
      }      
2766
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,143,012✔
2767
      break;
571,506✔
2768
    default:
×
2769
      break;
×
2770
  }
2771

2772
_exit:
1,298,466✔
2773

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

2778
  return code;
1,298,466✔
2779
}
2780

2781
static int32_t msmGrpAddDeployTask(SHashObj* pHash, SStmTaskDeploy* pDeploy) {
1,298,466✔
2782
  int32_t code = TSDB_CODE_SUCCESS;
1,298,466✔
2783
  int32_t lino = 0;
1,298,466✔
2784
  int64_t streamId = pDeploy->task.streamId;
1,298,466✔
2785
  SStreamTask* pTask = &pDeploy->task;
1,298,466✔
2786
  SStmStreamDeploy streamDeploy = {0};
1,298,466✔
2787
  SStmStreamDeploy* pStream = NULL;
1,298,466✔
2788
   
2789
  while (true) {
2790
    pStream = taosHashAcquire(pHash, &streamId, sizeof(streamId));
1,298,466✔
2791
    if (NULL == pStream) {
1,298,466✔
2792
      TAOS_CHECK_EXIT(msmInitStreamDeploy(&streamDeploy, pDeploy));
241,854✔
2793
      code = taosHashPut(pHash, &streamId, sizeof(streamId), &streamDeploy, sizeof(streamDeploy));
241,854✔
2794
      if (TSDB_CODE_SUCCESS == code) {
241,854✔
2795
        goto _exit;
241,854✔
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,056,612✔
2807
    
2808
    break;
1,056,612✔
2809
  }
2810
  
2811
_exit:
1,298,466✔
2812

2813
  taosHashRelease(pHash, pStream);
1,298,466✔
2814

2815
  if (code) {
1,298,466✔
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,298,466✔
2819
  }
2820

2821
  return code;
1,298,466✔
2822
}
2823

2824

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

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

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

2839
    (void)atomic_add_fetch_32(deployed, 1);
1,298,466✔
2840
  }
2841

2842
_exit:
184,229✔
2843

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

2848
  return code;
184,229✔
2849
}
2850

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

2858
  mstDebug("start to add stream vgroup tasks deploy");
51,861✔
2859
  
2860
  for (int32_t i = 0; i < vgNum; ++i) {
342,143✔
2861
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
290,282✔
2862

2863
    msmUpdateVgroupUpTs(pCtx, *vgId);
290,282✔
2864

2865
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
290,282✔
2866
    if (NULL == pVg) {
290,282✔
2867
      continue;
185,826✔
2868
    }
2869

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

2883
_exit:
51,861✔
2884

2885
  if (code) {
51,861✔
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;
51,861✔
2894
}
2895

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

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

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

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

2921
_exit:
40,806✔
2922

2923
  if (code) {
40,806✔
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;
40,806✔
2932
}
2933

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

2945
_exit:
643,191✔
2946

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

2951
  return code;
643,191✔
2952
}
2953

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

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

2965
  while (1) {
241,854✔
2966
    pIter = taosHashIterate(pCtx->deployStm, pIter);
293,677✔
2967
    if (pIter == NULL) {
293,677✔
2968
      break;
51,823✔
2969
    }
2970
    
2971
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
241,854✔
2972
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
483,708✔
2973

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

2978
    mstClearSStmStreamDeploy(pDeploy);
241,854✔
2979
    
2980
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
241,854✔
2981
  }
2982
  
2983
_exit:
51,823✔
2984

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

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

2996
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
51,861✔
2997
  int32_t code = TSDB_CODE_SUCCESS;
51,861✔
2998
  int32_t lino = 0;
51,861✔
2999
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
51,861✔
3000
  SStmVgTasksToDeploy* pVg = NULL;
51,861✔
3001
  
3002
  for (int32_t i = 0; i < vgNum; ++i) {
342,143✔
3003
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
290,282✔
3004
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
290,282✔
3005
    if (NULL == pVg) {
290,282✔
3006
      continue;
185,826✔
3007
    }
3008

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

3020
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
104,456✔
3021
    if (atomic_load_32(&pVg->deployed) == taskNum) {
104,456✔
3022
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, taskNum);
104,456✔
3023
      taosArrayDestroyEx(pVg->taskList, mstDestroySStmTaskToDeployExt);
104,456✔
3024
      pVg->taskList = NULL;
104,456✔
3025
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId)));
104,456✔
3026
      taosWUnLockLatch(&pVg->lock);
104,456✔
3027
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
104,456✔
3028
      continue;
104,456✔
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:
51,861✔
3048

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

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

3058
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
44,984✔
3059
  if (!GOT_SNODE(snodeId)) {
44,984✔
3060
    return;
1,480✔
3061
  }
3062
  
3063
  int32_t code = TSDB_CODE_SUCCESS;
43,504✔
3064
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
43,504✔
3065
  if (NULL == pSnode) {
43,504✔
3066
    return;
2,698✔
3067
  }
3068

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

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

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

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

3095
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
40,806✔
3096
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
40,806✔
3097
    taosWUnLockLatch(&pSnode->lock);
40,806✔
3098
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
40,806✔
3099
    return;
40,806✔
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) {
15,177,026✔
3137
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
15,177,026✔
3138
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
51,861✔
3139
  }
3140

3141
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
15,177,026✔
3142
    msmCleanDeployedSnodeTasks(pHb->snodeId);
44,984✔
3143
  }
3144
}
15,177,026✔
3145

3146
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
15,177,026✔
3147
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
15,177,026✔
3148
  if (mStreamMgmt.tCtx) {
15,177,026✔
3149
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
15,086,075✔
3150
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
15,086,075✔
3151
  }
3152
}
15,177,026✔
3153

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

3171
_exit:
238,365✔
3172

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

3177
  return code;
238,365✔
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) {
162,972✔
3208
  int32_t code = TSDB_CODE_SUCCESS;
162,972✔
3209
  int32_t lino = 0;
162,972✔
3210
  int32_t action = STREAM_ACT_UNDEPLOY;
162,972✔
3211
  bool    dropped = false;
162,972✔
3212

3213
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
162,972✔
3214
  mstsDebug("stream dropped: %d", dropped);
162,972✔
3215
  
3216
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
162,972✔
3217
  if (pAction) {
162,972✔
3218
    pAction->actions |= action;
125,933✔
3219
    if (NULL == pAction->undeploy.taskList) {
125,933✔
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);
251,866✔
3225
    if (pAction->undeploy.doCheckpoint) {
125,933✔
3226
      pAction->undeploy.doCheckpoint = dropped ? false : true;
22,390✔
3227
    }
3228
    if (!pAction->undeploy.doCleanup) {
125,933✔
3229
      pAction->undeploy.doCleanup = dropped ? true : false;
22,390✔
3230
    }
3231
    
3232
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
125,933✔
3233
  } else {
3234
    SStmAction newAction = {0};
37,039✔
3235
    newAction.actions = action;
37,039✔
3236
    newAction.undeploy.doCheckpoint = dropped ? false : true;
37,039✔
3237
    newAction.undeploy.doCleanup = dropped ? true : false;
37,039✔
3238
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
37,039✔
3239
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
37,039✔
3240
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
74,078✔
3241
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
37,039✔
3242
    
3243
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
37,039✔
3244
  }
3245

3246
_exit:
162,972✔
3247

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

3252
  return code;
162,972✔
3253
}
3254

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

3276
_exit:
3,504✔
3277

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

3283
  return code;
3,504✔
3284
}
3285

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

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

3296
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
311,393✔
3297
  for (int32_t i = 0; i < readerNum; ++i) {
687,815✔
3298
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
376,422✔
3299
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
376,422✔
3300
      return false;
×
3301
    }
3302
  }
3303

3304
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
311,393✔
3305
  for (int32_t i = 0; i < readerNum; ++i) {
370,918✔
3306
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
59,525✔
3307
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
59,525✔
3308
      return false;
×
3309
    }
3310
  }
3311

3312
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
311,393✔
3313
  SListNode* pNode = listHead(pStream->calcReaders);
311,393✔
3314
  for (int32_t i = 0; i < readerNum; ++i) {
909,914✔
3315
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
598,521✔
3316
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
598,521✔
3317
      return false;
×
3318
    }
3319
    pNode = TD_DLIST_NODE_NEXT(pNode);
598,521✔
3320
  }
3321

3322
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,012,760✔
3323
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
774,395✔
3324
    for (int32_t m = 0; m < runnerNum; ++m) {
1,519,019✔
3325
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
817,652✔
3326
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
817,652✔
3327
        return false;
73,028✔
3328
      }
3329
    }
3330
  }
3331
  
3332
  return true;
238,365✔
3333
}
3334

3335

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

3344
  msttDebug("start to handle task abnormal status %d", pTask->status);
10,701,223✔
3345
  
3346
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
10,701,223✔
3347
  if (NULL == pStatus) {
10,701,223✔
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;
11,635✔
3351
  }
3352

3353
  stopped = atomic_load_8(&pStatus->stopped);
10,701,223✔
3354
  if (stopped) {
10,701,223✔
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) {
10,701,223✔
3361
    case STREAM_STATUS_INIT:      
10,696,646✔
3362
      if (STREAM_TRIGGER_TASK != pMsg->type) {
10,696,646✔
3363
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
10,229,720✔
3364
        return;
10,229,720✔
3365
      }
3366
      
3367
      if (INT64_MIN == pStatus->lastActionTs) {
466,926✔
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) {
466,926✔
3373
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
155,533✔
3374
        return;
155,533✔
3375
      }
3376

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

3399
_exit:
315,970✔
3400

3401
  if (code) {
315,970✔
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) {
162,972✔
3408
  int32_t code = TSDB_CODE_SUCCESS;
162,972✔
3409
  int32_t lino = 0;
162,972✔
3410
  SStreamTask* pTask = (SStreamTask*)pStatus;
162,972✔
3411
  int64_t streamId = pStatus->streamId;
162,972✔
3412

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

3415
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
162,972✔
3416

3417
_exit:
162,972✔
3418

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

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

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

3448
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
3,675,622✔
3449
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,834,152✔
3450
    if (NULL == pStatus->detailStatus) {
1,834,152✔
3451
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
177,070✔
3452
      if (NULL == pStatus->detailStatus) {
177,070✔
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,834,152✔
3459
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,834,152✔
3460
  }
3461

3462
_exit:
1,841,470✔
3463

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

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

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

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

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

3496
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
28,590,855✔
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) {
28,590,855✔
3505
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,225,762✔
3506
        (*ppStatus)->runningStartTs = pCtx->currTs;
936,677✔
3507
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,289,085✔
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;
28,590,855✔
3517
    (*ppStatus)->status = pTask->status;
28,590,855✔
3518
    (*ppStatus)->lastUpTs = pCtx->currTs;
28,590,855✔
3519
    
3520
    if (STREAM_STATUS_RUNNING != pTask->status) {
28,590,855✔
3521
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
10,701,223✔
3522
    }
3523
    
3524
    if (STREAM_TRIGGER_TASK == pTask->type) {
28,590,855✔
3525
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
3,675,622✔
3526
    }
3527
  }
3528

3529
_exit:
963,760✔
3530

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

3535
  return code;
963,760✔
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) {
238,365✔
3687
  int32_t code = TSDB_CODE_SUCCESS;
238,365✔
3688
  int32_t lino = 0;
238,365✔
3689
  if (NULL == pCtx->pRsp->start.taskList) {
238,365✔
3690
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
92,008✔
3691
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
92,008✔
3692
  }
3693

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

3703
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
476,730✔
3704
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
238,365✔
3705

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

3708
  return;
238,365✔
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) {
37,039✔
3717
  int32_t code = TSDB_CODE_SUCCESS;
37,039✔
3718
  int32_t lino = 0;
37,039✔
3719
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
37,039✔
3720
  if (NULL == pCtx->pRsp->undeploy.taskList) {
37,039✔
3721
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
23,870✔
3722
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
23,870✔
3723
  }
3724

3725
  SStreamTaskUndeploy undeploy;
37,039✔
3726
  for (int32_t i = 0; i < dropNum; ++i) {
200,011✔
3727
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
162,972✔
3728
    undeploy.task = *pTask;
162,972✔
3729
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
162,972✔
3730
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
162,972✔
3731

3732
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
325,944✔
3733
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
162,972✔
3734

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

3738
  return;
37,039✔
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,504✔
3785
  int32_t code = TSDB_CODE_SUCCESS;
3,504✔
3786
  int32_t lino = 0;
3,504✔
3787

3788
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,504✔
3789
  if (NULL == pStream) {
3,504✔
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,504✔
3795
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3796
    return;
×
3797
  }
3798

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

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

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

3813
_exit:
3,504✔
3814

3815
  if (code) {
3,504✔
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,504✔
3819
  }
3820
}
3821

3822

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

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

3831
  while (1) {
278,908✔
3832
    pIter = taosHashIterate(pCtx->actionStm, pIter);
397,698✔
3833
    if (pIter == NULL) {
397,698✔
3834
      break;
118,790✔
3835
    }
3836

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

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

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

3853
    if (STREAM_ACT_START & pAction->actions) {
241,869✔
3854
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
238,365✔
3855
    }
3856
  }
3857
  
3858
_exit:
118,790✔
3859

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

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

3868
  return code;
118,790✔
3869
}
3870

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

3877
  while (true) {
3878
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
15,590,764✔
3879
    if (NULL == lastTs) {
15,590,764✔
3880
      if (noExists) {
669,943✔
3881
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
165,254✔
3882
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
165,254✔
3883
      }
3884

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

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

3899
        continue;
×
3900
      }
3901

3902
      return code;
5,080✔
3903
    }
3904

3905
    break;
3906
  }
3907

3908
_exit:
165,254✔
3909

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

3914
  return code;  
165,254✔
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) {
357✔
3965
  int32_t code = TSDB_CODE_SUCCESS;
357✔
3966
  int32_t lino = 0;
357✔
3967
  int32_t minVal = watchError ? 0 : 1;
357✔
3968

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

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

3977
  if (watchError) {
357✔
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)) {
357✔
3987
    mstInfo("no stream tasks remain during watch state");
357✔
3988
    goto _exit;
357✔
3989
  }
3990

3991
  msmWatchCheckStreamMap(pCtx);
×
3992

3993
_exit:
357✔
3994

3995
  mStreamMgmt.lastTaskId += 100000;
357✔
3996

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

3999
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
357✔
4000

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

4005
  return code;
357✔
4006
}
4007

4008

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

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

4020
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
12,303✔
4021
  if (GOT_SNODE(pReq->snodeId)) {
12,303✔
4022
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,421✔
4023
  }
4024

4025
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
12,303✔
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) {
12,303✔
4031
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
357✔
4032
  }
4033

4034
_exit:
12,303✔
4035

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

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

4044
  return code;
12,303✔
4045
}
4046

4047
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,411,474✔
4048
  int32_t listSize = taosArrayGetSize(pOReaders);
5,411,474✔
4049
  int32_t totalSize = 0;
5,411,474✔
4050
  
4051
  for (int32_t i = 0; i < listSize; ++i) {
6,404,186✔
4052
    SArray* pList = taosArrayGetP(pOReaders, i);
992,712✔
4053
    totalSize += taosArrayGetSize(pList);
992,712✔
4054
  }
4055

4056
  return totalSize;
5,411,474✔
4057
}
4058

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

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

4068

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

4073
  if (NULL == pStatus->trigOReaders) {
59,525✔
4074
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
51,577✔
4075
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
51,577✔
4076
  }
4077

4078
  while (true) {
51,577✔
4079
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
111,102✔
4080

4081
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
111,102✔
4082
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
51,577✔
4083
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
51,577✔
4084

4085
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
103,154✔
4086
      continue;
51,577✔
4087
    }
4088

4089
    *ppRes = *ppOReaderList;
59,525✔
4090
    break;
59,525✔
4091
  }
4092

4093
_exit:
59,525✔
4094

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

4099
  return code;
59,525✔
4100
}
4101

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

4108
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
69,870✔
4109
  for (int32_t i = 0; i < readerNum; ++i) {
145,466✔
4110
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
85,941✔
4111
    if (pReader->id.nodeId == vgId) {
85,941✔
4112
      readerExists = true;
10,345✔
4113
      break;
10,345✔
4114
    }
4115
  }
4116

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

4127
_exit:
10,345✔
4128

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

4133
  return code;
69,870✔
4134
}
4135

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

4154
  if (NULL == pCtx->pRsp->rsps.rspList) {
61,295✔
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);
61,295✔
4160
  rsp.task = *(SStreamTask*)pTask;
61,295✔
4161

4162
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
61,295✔
4163
  if (NULL == pStatus) {
61,295✔
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) {
61,295✔
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); 
61,295✔
4174

4175
  stopped = atomic_load_8(&pStatus->stopped);
61,295✔
4176
  if (stopped) {
61,295✔
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) {
61,295✔
4182
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4183
    goto _exit;
×
4184
  }
4185

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

4190
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
61,295✔
4191
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
61,295✔
4192
  
4193
  for (int32_t i = 0; i < tbNum; ++i) {
243,177✔
4194
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
181,882✔
4195
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
181,882✔
4196
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
363,764✔
4197
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
181,882✔
4198
  }
4199

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

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

4224
_exit:
61,295✔
4225

4226
  tFreeSStreamMgmtReq(pMgmtReq);
61,295✔
4227
  taosMemoryFree(pMgmtReq);
61,295✔
4228

4229
  tSimpleHashCleanup(pVgs);
61,295✔
4230
  mstDestroyDbVgroupsHash(pDbVgroups);
61,295✔
4231

4232
  if (code) {
61,295✔
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);
122,590✔
4240
  }
4241

4242
_final:
61,295✔
4243

4244
  if (finalCode) {
61,295✔
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;
61,295✔
4251
}
4252

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

4267
_exit:
2,453✔
4268

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

4273
  return code;
2,453✔
4274
}
4275

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

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

4295
  if (!readerExists) {
2,453✔
4296
    if (NULL == pStatus->calcReaders) {
2,453✔
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,453✔
4302
      pReader = (SStmTaskStatus*)pNode->data;
2,453✔
4303
      taskIdx = pReader->id.taskIdx + 1;
2,453✔
4304
    }
4305

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

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

4319
_exit:
2,453✔
4320

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

4325
  return code;
2,453✔
4326
}
4327

4328

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

4346
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,453✔
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,453✔
4352
  rsp.task = *(SStreamTask*)pTask;
2,453✔
4353

4354
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,453✔
4355
  if (NULL == pStatus) {
2,453✔
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,453✔
4361
  if (stopped) {
2,453✔
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,453✔
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,453✔
4372
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,453✔
4373

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

4389
_exit:
2,453✔
4390

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

4394
  if (code) {
2,453✔
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,906✔
4401
  }
4402

4403

4404
_final:
2,453✔
4405

4406
  if (finalCode) {
2,453✔
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,453✔
4413
}
4414

4415

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

4420
  switch (pTask->pMgmtReq->type) {
63,748✔
4421
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
61,295✔
4422
      msmDeployTriggerOrigReader(pCtx, pTask);
61,295✔
4423
      break;
61,295✔
4424
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,453✔
4425
      msmDeployRunnerOrigReader(pCtx, pTask);
2,453✔
4426
      break;
2,453✔
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:
63,748✔
4434

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

4439
  return code;
63,748✔
4440
}
4441

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

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

4467
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
63,748✔
4468
  }
4469

4470
_exit:
15,937✔
4471

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

4476
  return code;
15,937✔
4477
}
4478

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

4484
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
15,073,772✔
4485
  if (GOT_SNODE(pReq->snodeId)) {
14,908,518✔
4486
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,082,638✔
4487
  }
4488
  
4489
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
14,908,259✔
4490
    msmHandleStreamActions(pCtx);
38,778✔
4491
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
38,778✔
4492
  }
4493

4494
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
14,908,259✔
4495
    code = msmHandleStreamRequests(pCtx);
15,937✔
4496
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
15,937✔
4497
    TAOS_CHECK_EXIT(code);
15,937✔
4498
  }
4499

4500
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,908,259✔
4501
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
51,861✔
4502
  } else {
4503
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
14,856,398✔
4504
  }
4505

4506
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
14,908,259✔
4507
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
43,504✔
4508
  }
4509

4510
  if (taosHashGetSize(pCtx->deployStm) > 0) {
14,908,259✔
4511
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
51,823✔
4512
  }
4513

4514
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
14,908,259✔
4515
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
963,760✔
4516
  }
4517

4518
  if (taosHashGetSize(pCtx->actionStm) > 0) {
14,908,259✔
4519
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
118,790✔
4520
  }
4521

4522
_exit:
14,908,259✔
4523

4524
  if (code) {
15,073,772✔
4525
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
165,513✔
4526
  }
4527

4528
  return code;
15,073,772✔
4529
}
4530

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

4536
  if (TSDB_CODE_SUCCESS != code) {
15,177,026✔
4537
    goto _exit;
165,513✔
4538
  }
4539

4540
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
15,011,513✔
4541
  if (code < 0) {
15,011,513✔
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));
15,011,513✔
4547
  if (buf == NULL) {
15,011,513✔
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;
15,011,513✔
4553
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
15,011,513✔
4554

4555
  SEncoder encoder;
15,009,095✔
4556
  tEncoderInit(&encoder, abuf, tlen);
15,011,513✔
4557
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
15,011,513✔
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);
15,011,513✔
4565

4566
_exit:
15,177,026✔
4567

4568
  pMsg->code = code;
15,177,026✔
4569
  pMsg->info = *pRpcInfo;
15,177,026✔
4570
  if (TSDB_CODE_SUCCESS == code) {
15,177,026✔
4571
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
15,011,513✔
4572
    pMsg->pCont = buf;
15,011,513✔
4573
  }
4574
}
15,177,026✔
4575

4576

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

4582
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
15,177,026✔
4583

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

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

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

4613
_exit:
15,177,026✔
4614

4615
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
15,177,026✔
4616

4617
  msmCleanStreamGrpCtx(pHb);
15,177,026✔
4618
  msmClearStreamToDeployMaps(pHb);
15,177,026✔
4619

4620
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
15,177,026✔
4621
  
4622
  tFreeSMStreamHbRspMsg(&rsp);
15,177,026✔
4623

4624
  return code;
15,177,026✔
4625
}
4626

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

4632
  mstInfo("start to process mnode become leader");
391,406✔
4633

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

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

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

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

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

4656
  streamRemoveVnodeLeader(MNODE_HANDLE);
488,185✔
4657

4658
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
488,185✔
4659
    taosWLockLatch(&mStreamMgmt.runtimeLock);
391,406✔
4660
    msmDestroyRuntimeInfo(pMnode);
391,406✔
4661
    mStreamMgmt.stat.inactiveTimes++;
391,406✔
4662
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
391,406✔
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,900✔
4677
  int32_t code = TSDB_CODE_SUCCESS;
7,900✔
4678
  int32_t lino = 0;
7,900✔
4679
  SStreamObj* pStream = pObj;
7,900✔
4680
  SSnodeObj* pSnode = p1;
7,900✔
4681
  SArray** ppRes = p2;
7,900✔
4682

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

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

4693
  return true;
7,900✔
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) {
34,246✔
4708
  int32_t code = TSDB_CODE_SUCCESS;
34,246✔
4709
  int32_t lino = 0;
34,246✔
4710
  
4711
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
34,246✔
4712
  TAOS_CHECK_EXIT(code);
34,246✔
4713

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

4720
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4721

4722
_exit:
34,246✔
4723

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

4728
  return code;
34,246✔
4729
}
4730

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

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

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

4760
    return true;
19,062✔
4761
  }
4762

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

4765
  return true;
419✔
4766
}
4767

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

4780
    pStatus = (SStmStatus*)pIter;
242,603✔
4781

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

4790
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
242,166✔
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)) {
242,166✔
4797
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
11,960✔
4798
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
8,122✔
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);
8,122✔
4802
        continue;
8,122✔
4803
      }
4804

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

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

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

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

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

4834
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,104,448✔
4835
  for (int32_t i = 0; i < taskNum; ++i) {
6,573,171✔
4836
    SStmTaskStatus* pTask = *(pList + i);
3,468,723✔
4837

4838
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
3,468,723✔
4839
      continue;
445,588✔
4840
    }
4841
    
4842
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,023,989✔
4843
      continue;
3,022,927✔
4844
    }
4845

4846
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,062✔
4847
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,062✔
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,062✔
4856
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4857

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

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

4871
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
124,602✔
4872
  void* pIter = NULL;
124,602✔
4873
  SStmVgStreamStatus* pVg = NULL;
124,602✔
4874
  int64_t streamId = 0;
124,602✔
4875
  
4876
  while (true) {
1,185,154✔
4877
    pIter = taosHashIterate(pStreams, pIter);
1,309,756✔
4878
    if (NULL == pIter) {
1,309,756✔
4879
      break;
124,602✔
4880
    }
4881

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

4885
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,185,154✔
4886
    if (taskNum > 0) {
1,185,154✔
4887
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
706,056✔
4888
    }
4889

4890
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,185,154✔
4891
    if (taskNum > 0) {
1,185,154✔
4892
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
781,225✔
4893
    }
4894
  }
4895
}
124,602✔
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) {
312,633✔
4924
  void* pIter = NULL;
312,633✔
4925
  int32_t code = 0;
312,633✔
4926
  
4927
  while (true) {
1,173,811✔
4928
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,486,444✔
4929
    if (NULL == pIter) {
1,486,444✔
4930
      break;
312,633✔
4931
    }
4932

4933
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,173,811✔
4934
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,173,811✔
4935
      continue;
1,043,886✔
4936
    }
4937
    
4938
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
129,925✔
4939

4940
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
129,925✔
4941
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
5,323✔
4942
      if (NULL == pVgroup) {
5,323✔
4943
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
5,323✔
4944
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
5,323✔
4945
        if (code) {
5,323✔
4946
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4947
        }
4948
        continue;
5,323✔
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);
124,602✔
4959

4960
    msmCheckVgroupStreamStatus(pVg->streamTasks);
124,602✔
4961
  }
4962
}
312,633✔
4963

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

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

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

4992
  (void)msmSTAddSnodesToMap(pMnode);
1,239✔
4993

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

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

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

5036
  taosHashClear(pSnode->streamTasks);
1,239✔
5037
}
1,239✔
5038

5039

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

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

5054
    if (NULL != pSnode->trigger) {
424,148✔
5055
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
412,976✔
5056
    }
5057

5058
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,696,592✔
5059
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,272,444✔
5060
      if (taskNum > 0) {
1,272,444✔
5061
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,204,191✔
5062
      }
5063
    }
5064
  }
5065
}
35,728✔
5066

5067

5068
void msmCheckSnodeStatus(SMnode *pMnode) {
312,633✔
5069
  void* pIter = NULL;
312,633✔
5070
  
5071
  while (true) {
375,638✔
5072
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
688,271✔
5073
    if (NULL == pIter) {
688,271✔
5074
      break;
312,633✔
5075
    }
5076

5077
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
375,638✔
5078
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
375,638✔
5079
      continue;
329,410✔
5080
    }
5081

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

5100
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
35,728✔
5101
  }
5102
}
312,633✔
5103

5104

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

5108
  msmCheckVgroupStatus(pMnode);
312,633✔
5109
  msmCheckSnodeStatus(pMnode);
312,633✔
5110
}
312,633✔
5111

5112
void msmCheckSnodesState(SMnode *pMnode) {
312,633✔
5113
  if (!MST_READY_FOR_SNODE_LOOP()) {
312,633✔
5114
    return;
277,634✔
5115
  }
5116

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

5119
  void* pIter = NULL;
34,999✔
5120
  int32_t snodeId = 0;
34,999✔
5121
  while (true) {
37,887✔
5122
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
72,886✔
5123
    if (NULL == pIter) {
72,886✔
5124
      break;
34,999✔
5125
    }
5126

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

5132
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
177✔
5133
    if (NULL == pSnode->streamTasks) {
177✔
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", 
177✔
5140
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5141
    
5142
    msmHandleSnodeLost(pMnode, pSnode);
177✔
5143
  }
5144

5145
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
34,999✔
5146
}
5147

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

5155
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
9,302,737✔
5156
    mstTrace("ignore health check since no stream now");
8,677,471✔
5157
    return false;
8,677,471✔
5158
  }
5159

5160
  return true;
625,266✔
5161
}
5162

5163
void msmHealthCheck(SMnode *pMnode) {
8,992,856✔
5164
  if (!msmCheckNeedHealthCheck(pMnode)) {
8,992,856✔
5165
    return;
8,680,223✔
5166
  }
5167

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

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

5185
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
312,633✔
5186

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

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

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

5216
  stopped = atomic_load_8(&pStatus->stopped);
18,476✔
5217
  if (stopped) {
18,476✔
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) {
18,476✔
5224
    pAddr->taskId = pStatus->triggerTask->id.taskId;
14,880✔
5225
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
14,880✔
5226
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
14,880✔
5227
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
14,880✔
5228
    goto _exit;
14,880✔
5229
  }
5230

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

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

5238
  return code;
21,700✔
5239
}
5240

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

5248
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
782,731✔
5249

5250
  mStreamMgmt.stat.activeTimes++;
391,406✔
5251
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
391,406✔
5252
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
391,406✔
5253
  if (NULL == mStreamMgmt.tCtx) {
391,406✔
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));
391,406✔
5260
  if (mStreamMgmt.actionQ == NULL) {
391,406✔
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));
391,406✔
5267
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
391,406✔
5268
  
5269
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
391,406✔
5270
  
5271
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,348,227✔
5272
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,956,821✔
5273

5274
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
11,740,926✔
5275
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,784,105✔
5276
      if (pCtx->deployStm[m] == NULL) {
9,784,105✔
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,784,105✔
5282
      
5283
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
9,784,105✔
5284
      if (pCtx->actionStm[m] == NULL) {
9,784,105✔
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,784,105✔
5290
    }
5291
  }
5292
  
5293
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
391,406✔
5294
  if (mStreamMgmt.streamMap == NULL) {
391,406✔
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);
391,406✔
5300
  
5301
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
391,406✔
5302
  if (mStreamMgmt.taskMap == NULL) {
391,406✔
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);
391,406✔
5309
  if (mStreamMgmt.vgroupMap == NULL) {
391,406✔
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);
391,406✔
5315

5316
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
391,406✔
5317
  if (mStreamMgmt.snodeMap == NULL) {
391,406✔
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);
391,406✔
5323
  
5324
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
391,406✔
5325
  if (mStreamMgmt.dnodeMap == NULL) {
391,406✔
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);
391,406✔
5332
  if (mStreamMgmt.toDeployVgMap == NULL) {
391,406✔
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);
391,406✔
5338
  
5339
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
391,406✔
5340
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
391,406✔
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);
391,406✔
5346

5347
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
391,406✔
5348
  if (mStreamMgmt.toUpdateScanMap == NULL) {
391,406✔
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);
391,406✔
5354

5355
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
391,406✔
5356
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
391,406✔
5357

5358
  mStreamMgmt.lastTaskId = 1;
391,406✔
5359

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

5363
  if (activeStreamNum > 0) {
391,406✔
5364
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
688✔
5365
  } else {
5366
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
390,718✔
5367
  }
5368

5369
_exit:
391,406✔
5370

5371
  if (code) {
391,406✔
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");
391,406✔
5376
  }
5377

5378
  return code;
391,406✔
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