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

taosdata / TDengine / #4980

10 Mar 2026 08:57AM UTC coverage: 68.492% (-0.02%) from 68.512%
#4980

push

travis-ci

web-flow
fix: add retry while exec ci case test_stable_keep_compact.py. (#34729)

211901 of 309380 relevant lines covered (68.49%)

135171938.8 hits per line

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

76.23
/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() {
806,656✔
34
  SStmQNode* pQNode = NULL;
806,656✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
806,656✔
37
    return;
403,328✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,016,432✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,098,592✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,082,160✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,082,160✔
51
  }
52
}
2,016,432✔
53

54
void msmDestroyThreadCtxs() {
806,656✔
55
  if (NULL == mStreamMgmt.tCtx) {
806,656✔
56
    return;
403,328✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,419,760✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,016,432✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
403,328✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
806,656✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
806,656✔
94
}
806,656✔
95

96

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

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

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

111
    pStatus = pStream;
424✔
112
  }
113

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

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

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

136
_exit:
458✔
137

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

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

145

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

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

200
  pSnode = NULL;
429,772✔
201

202
_exit:
429,772✔
203

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

208
  return code;
429,772✔
209
}
210

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

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

235
  pDnode = NULL;
913,769✔
236

237
_exit:
913,769✔
238

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

243
  return code;
913,769✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
557,037✔
275
  int32_t code = TSDB_CODE_SUCCESS;
557,037✔
276
  int32_t lino = 0;
557,037✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
557,037✔
278
  if (NULL == pStream) {
557,037✔
279
    SStmVgStreamStatus stream = {0};
411,692✔
280
    if (trigReader) {
411,692✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
261,602✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
261,602✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
523,204✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
150,090✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
150,090✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
300,180✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
411,692✔
290
    goto _exit;
411,692✔
291
  }
292
  
293
  if (trigReader) {
145,345✔
294
    if (NULL == pStream->trigReaders) {
22,956✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
22,956✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
22,956✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
45,912✔
300
    goto _exit;
22,956✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
122,389✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
92,662✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
92,662✔
306
  }
307

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

310
_exit:
122,389✔
311

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

320
  return code;
557,037✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
70,231✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
70,231✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
70,231✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
486,806✔
339
  }
340
  
341
_exit:
556,907✔
342

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

350
  return code;
557,037✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
558,437✔
363
    if (NULL == pVg) {
558,437✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
110,377✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
110,377✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
220,754✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
110,377✔
368
      if (TSDB_CODE_SUCCESS == code) {
110,377✔
369
        goto _return;
110,377✔
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);
448,060✔
381
    if (NULL == pVg->taskList) {
448,060✔
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)) {
896,120✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
448,060✔
390
    
391
    taosHashRelease(pVgMap, pVg);
448,060✔
392
    break;
448,060✔
393
  }
394
  
395
_return:
558,437✔
396

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

404
  return code;
558,437✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
762,262✔
409
  int32_t code = TSDB_CODE_SUCCESS;
762,262✔
410
  int32_t lino = 0;
762,262✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
762,262✔
412
  if (NULL == pStream) {
762,262✔
413
    SStmSnodeStreamStatus stream = {0};
187,419✔
414
    if (deployId < 0) {
187,419✔
415
      stream.trigger = pStatus;
3,277✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
184,142✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
184,142✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
368,284✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
187,419✔
423
    goto _exit;
187,419✔
424
  }
425
  
426
  if (deployId < 0) {
574,843✔
427
    if (NULL != pStream->trigger) {
178,529✔
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;
178,529✔
433
    goto _exit;
178,529✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
396,314✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
351,620✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
351,620✔
439
  }
440

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

443
_exit:
396,314✔
444

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

453
  return code;
762,262✔
454
}
455

456

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

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

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

484
  return code;
762,262✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
181,806✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
41,759✔
568
      ext.deployed = false;
41,759✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
83,518✔
570

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

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

614
  return code;
580,456✔
615
}
616

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

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

632
_exit:
535,762✔
633

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

638
  return code;
535,762✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
13,710✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
×
682

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

687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
69,656✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
16,037,123✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
65,047,130✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
49,010,007✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
49,010,007✔
725
  }
726

727
_exit:
16,037,123✔
728

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

733
  return code;
16,037,123✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
347,905✔
739
  int32_t num = taosArrayGetSize(pList);
347,905✔
740
  for (int32_t i = 0; i < num; ++i) {
996,416✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
823,593✔
742
    if (pScan->readFromCache) {
823,593✔
743
      return pScan->scanPlan;
175,082✔
744
    }
745
  }
746

747
  return NULL;
172,823✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
558,437✔
775
}
776

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

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

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

805
_exit:
178,529✔
806

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

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

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

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

877
  SStreamTaskAddr addr;
181,351✔
878
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
181,806✔
879
  if (triggerReaderNum > 0) {
181,806✔
880
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
180,813✔
881
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
180,813✔
882
  }
883
  
884
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
406,028✔
885
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
224,222✔
886
    addr.taskId = pStatus->id.taskId;
224,222✔
887
    addr.nodeId = pStatus->id.nodeId;
224,222✔
888
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
224,222✔
889
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
448,444✔
890
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
224,222✔
891
  }
892

893
  pMsg->leaderSnodeId = pStream->mainSnodeId;
181,806✔
894
  pMsg->streamName = pInfo->streamName;
181,806✔
895

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

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

903
_exit:
178,529✔
904

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

914

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

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

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

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

959

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

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

983

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

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

1001
  return code;
717,568✔
1002
}
1003

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

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

1012

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

1019
  while (true) {
1020
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
933,242✔
1021
    if (NULL == pStatus) {
933,242✔
1022
      if (noExists) {
9,194✔
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,194✔
1028
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
9,194✔
1029
      
1030
      continue;
9,194✔
1031
    }
1032

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

1037
_exit:
924,048✔
1038

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

1043
  return code;
924,048✔
1044
}
1045

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

1057
    if (alive) {
361,008✔
1058
      return snodeId;
360,510✔
1059
    }
1060
    
1061
    if (snodeId == mainSnodeId) {
498✔
1062
      SSnodeObj* pSnode = mndAcquireSnode(pMnode, snodeId);
498✔
1063
      if (NULL == pSnode) {
498✔
1064
        stsWarn("snode %d not longer exists, ignore assign snode", snodeId);
×
1065
        return 0;
×
1066
      }
1067
      
1068
      if (pSnode->replicaId <= 0) {
498✔
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;
498✔
1075
      mndReleaseSnode(pMnode, pSnode);
498✔
1076
      
1077
      continue;
498✔
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) {
544,104✔
1094
  int32_t code = TSDB_CODE_SUCCESS;
544,104✔
1095
  int32_t lino = 0;
544,104✔
1096
  int32_t snodeIdx = 0;
544,104✔
1097
  int32_t snodeId = 0;
544,104✔
1098
  void      *pIter = NULL;
544,104✔
1099
  SSnodeObj *pObj = NULL;
544,104✔
1100
  bool alive = false;
544,104✔
1101
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
544,104✔
1102
  if (snodeNum <= 0) {
544,104✔
1103
    mstsInfo("no available snode now, num:%d", snodeNum);
2,600✔
1104
    goto _exit;
2,600✔
1105
  }
1106

1107
  int32_t snodeTarget = taosRand() % snodeNum;
541,504✔
1108

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

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

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

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

1146
_exit:
544,104✔
1147

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

1152
  if (0 == snodeId) {
544,104✔
1153
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,600✔
1154
  }
1155

1156
  return snodeId;
544,104✔
1157
}
1158

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

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

1170
_exit:
717,568✔
1171

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

1176
  return snodeId;
717,568✔
1177
}
1178

1179

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

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

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

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

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

1212
_exit:
181,806✔
1213

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

1218
  return code;
181,806✔
1219
}
1220

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

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

1247
_exit:
284,558✔
1248

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

1253
  return code;
284,558✔
1254
}
1255

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

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

1308
        sdbRelease(pSdb, pVgroup);
406,255✔
1309
      }
1310
      break;
76,124✔
1311
    }
1312
    default:
993✔
1313
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
993✔
1314
      break;
993✔
1315
  }
1316

1317
_exit:
181,806✔
1318

1319
  mndReleaseDb(pCtx->pMnode, pDb);
181,806✔
1320

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

1325
  return code;
181,806✔
1326
}
1327

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

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

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

1369
  nodesDestroyNode((SNode*)pSubplan);
270,190✔
1370

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

1375
  return code;
270,190✔
1376
}
1377

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

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

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

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

1414
  return code;
82,015✔
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) {
272,479✔
1419
  int32_t code = TSDB_CODE_SUCCESS;
272,479✔
1420
  int32_t lino = 0;
272,479✔
1421

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

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

1446
_exit:
272,479✔
1447

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

1452
  return code;
272,479✔
1453
}
1454

1455

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

1465
  
1466
  for (int32_t i = 0; i < calcTasksNum; ++i) {
533,836✔
1467
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
352,030✔
1468
    if (pScan->readFromCache) {
352,030✔
1469
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
82,015✔
1470
      continue;
82,015✔
1471
    }
1472
    
1473
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
270,015✔
1474
    for (int32_t m = 0; m < vgNum; ++m) {
540,030✔
1475
      pState = tdListReserve(pInfo->calcReaders);
270,015✔
1476
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
270,015✔
1477

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

1483
_exit:
181,806✔
1484

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

1489
  return code;
181,806✔
1490
}
1491

1492

1493

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

1521
_exit:
175✔
1522

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

1527
  return code;
175✔
1528
}
1529

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

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

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

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

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

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

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

1624
_exit:
580,456✔
1625

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

1630
  return code;
580,456✔
1631
}
1632

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

1641
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
580,456✔
1642

1643
  SNode* pTmp = NULL;
580,456✔
1644
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,681,415✔
1645
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,100,959✔
1646
      ERASE_NODE(pPlan->pChildren);
1,056,265✔
1647
      continue;
1,056,265✔
1648
    }
1649
    WHERE_NEXT;
44,694✔
1650
  }
1651
  nodesClearList(pPlan->pChildren);
580,456✔
1652
  pPlan->pChildren = NULL;
580,456✔
1653

1654
  if (NULL == pPlan->pParents) {
580,456✔
1655
    goto _exit;
535,762✔
1656
  }
1657

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

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

1677
  return code;
580,456✔
1678
}
1679

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

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

1695
_exit:
535,762✔
1696

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

1701
  return code;
535,762✔
1702
}
1703

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

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

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

1759
    taskIdx = 0;
535,587✔
1760
    
1761
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,088,397✔
1762
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
552,810✔
1763
      if (NULL == plans) {
552,810✔
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)) {
552,810✔
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);
552,810✔
1774
      if (taskNum <= 0) {
552,810✔
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;
552,810✔
1780
      if (totalTaskNum > pDag->numOfSubplans) {
552,810✔
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,133,091✔
1786
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
580,281✔
1787
        pState = taosArrayReserve(deployList, 1);
580,281✔
1788

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

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

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

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

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

1826
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
535,587✔
1827

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

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

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

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

1849
  taosArrayDestroy(deployTaskList);
178,529✔
1850
  nodesDestroyNode((SNode *)pDag);
178,529✔
1851

1852
  return code;
178,529✔
1853
}
1854

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

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

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

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

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

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

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

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

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

1915
        pRunner++;
175✔
1916
      }
1917

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

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

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

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

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

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

1933
_exit:
175✔
1934

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

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

1942
  return code;
175✔
1943
}
1944

1945

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

1954
_exit:
173,146✔
1955

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

1960
  return code;
173,146✔
1961
}
1962

1963

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

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

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

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

1986
  taosHashClear(mStreamMgmt.toUpdateScanMap);
178,529✔
1987
  mStreamMgmt.toUpdateScanNum = 0;
178,529✔
1988

1989
_exit:
178,529✔
1990

1991
  nodesDestroyNode((SNode *)pPlan);
178,529✔
1992

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

1997
  return code;
178,529✔
1998
}
1999

2000

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

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

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

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

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

2024
  return code;
181,806✔
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) {
176,423✔
2078
  int32_t code = TSDB_CODE_SUCCESS;
176,423✔
2079
  int32_t lino = 0;
176,423✔
2080
  int64_t streamId = pStream->pCreate->streamId;
176,423✔
2081

2082
  pStatus->lastActionTs = INT64_MIN;
176,423✔
2083

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

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

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

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

2121
  return code;
176,423✔
2122
}
2123

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

2130
  if (NULL == pStatus) {
181,806✔
2131
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
176,423✔
2132

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

2135
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
176,423✔
2136
  }
2137
  
2138
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
181,806✔
2139

2140
  mstLogSStmStatus("stream deployed", streamId, pStatus);
181,806✔
2141

2142
_exit:
181,806✔
2143

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

2156

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

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

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

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

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

2184
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
13,678✔
2185
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
5,664✔
2186
    (void)mstWaitLock(&pSnode->lock, true);
5,664✔
2187

2188
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
5,664✔
2189
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
5,664✔
2190
      for (int32_t i = 0; i < taskNum; ++i) {
12,004✔
2191
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
7,215✔
2192
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
7,215✔
2193
          continue;
6,966✔
2194
        }
2195
        
2196
        mstDestroySStmTaskToDeployExt(pExt);
249✔
2197
        pExt->deployed = true;
249✔
2198
      }
2199
    }
2200

2201
    taskNum = taosArrayGetSize(pSnode->runnerList);
5,664✔
2202
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
5,664✔
2203
      for (int32_t i = 0; i < taskNum; ++i) {
25,566✔
2204
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
19,902✔
2205
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
19,902✔
2206
          continue;
19,404✔
2207
        }
2208
        
2209
        mstDestroySStmTaskToDeployExt(pExt);
498✔
2210
        pExt->deployed = true;
498✔
2211
      }
2212
    }
2213

2214
    taosRUnLockLatch(&pSnode->lock);
5,664✔
2215
  }
2216

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

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

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

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

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

2265
  mstResetSStmStatus(pStatus);
5,908✔
2266

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

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

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

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

2303
  TAOS_CHECK_EXIT(code);
182,331✔
2304

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

2321
_exit:
182,331✔
2322

2323
  mndReleaseStream(pCtx->pMnode, pStream);
182,331✔
2324

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

2329
  return code;
182,331✔
2330
}
2331

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

2364
_exit:
1,400✔
2365

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

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

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

2469
  return code;
175✔
2470
}
2471

2472

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

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

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

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

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

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

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

2600
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
2,106✔
2601

2602
_exit:
2,106✔
2603

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

2610
  return code;
2,106✔
2611
}
2612

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

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

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

2629
  atomic_store_8(&pStream->stopped, 2);
28,003✔
2630

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

2633
_exit:
×
2634

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

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

2641
  return;
28,722✔
2642
}
2643

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

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

2663
_exit:
3,520✔
2664

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

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

2671
  return code;
3,520✔
2672
}
2673

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

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

2695
_exit:
38,908✔
2696

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

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

2713
    pStatus = (SStmStatus*)pIter;
×
2714

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

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

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

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

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

2732
  msmStopAllStreamsByGrant(errCode);
×
2733

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

2739
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,319,952✔
2740
  int32_t code = TSDB_CODE_SUCCESS;
1,319,952✔
2741
  int32_t lino = 0;
1,319,952✔
2742
  int64_t streamId = pDeploy->task.streamId;
1,319,952✔
2743
  
2744
  switch (pDeploy->task.type) {
1,319,952✔
2745
    case STREAM_READER_TASK:
558,437✔
2746
      if (NULL == pStream->readerTasks) {
558,437✔
2747
        pStream->streamId = streamId;
237,239✔
2748
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
237,239✔
2749
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
237,239✔
2750
      }
2751
      
2752
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,116,874✔
2753
      break;
558,437✔
2754
    case STREAM_TRIGGER_TASK:
181,557✔
2755
      pStream->streamId = streamId;
181,557✔
2756
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
181,557✔
2757
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
181,557✔
2758
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
181,557✔
2759
      break;
181,557✔
2760
    case STREAM_RUNNER_TASK:
579,958✔
2761
      if (NULL == pStream->runnerTasks) {
579,958✔
2762
        pStream->streamId = streamId;
184,305✔
2763
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
184,305✔
2764
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
184,305✔
2765
      }      
2766
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,159,916✔
2767
      break;
579,958✔
2768
    default:
×
2769
      break;
×
2770
  }
2771

2772
_exit:
1,319,952✔
2773

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

2778
  return code;
1,319,952✔
2779
}
2780

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

2813
  taosHashRelease(pHash, pStream);
1,319,952✔
2814

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

2821
  return code;
1,319,952✔
2822
}
2823

2824

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

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

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

2839
    (void)atomic_add_fetch_32(deployed, 1);
1,319,952✔
2840
  }
2841

2842
_exit:
191,136✔
2843

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

2848
  return code;
191,136✔
2849
}
2850

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

2858
  mstDebug("start to add stream vgroup tasks deploy");
55,655✔
2859
  
2860
  for (int32_t i = 0; i < vgNum; ++i) {
357,416✔
2861
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
301,761✔
2862

2863
    msmUpdateVgroupUpTs(pCtx, *vgId);
301,761✔
2864

2865
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
301,761✔
2866
    if (NULL == pVg) {
301,761✔
2867
      continue;
191,384✔
2868
    }
2869

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

2883
_exit:
55,655✔
2884

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

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

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

2909
  (void)mstWaitLock(&pSnode->lock, false);
41,739✔
2910
  
2911
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
41,739✔
2912
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
39,020✔
2913
  }
2914

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

2921
_exit:
41,739✔
2922

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

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

2945
_exit:
653,193✔
2946

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

2951
  return code;
653,193✔
2952
}
2953

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

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

2965
  while (1) {
244,716✔
2966
    pIter = taosHashIterate(pCtx->deployStm, pIter);
298,818✔
2967
    if (pIter == NULL) {
298,818✔
2968
      break;
54,102✔
2969
    }
2970
    
2971
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
244,716✔
2972
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
489,432✔
2973

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

2978
    mstClearSStmStreamDeploy(pDeploy);
244,716✔
2979
    
2980
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
244,716✔
2981
  }
2982
  
2983
_exit:
54,102✔
2984

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

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

2996
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
55,655✔
2997
  int32_t code = TSDB_CODE_SUCCESS;
55,655✔
2998
  int32_t lino = 0;
55,655✔
2999
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
55,655✔
3000
  SStmVgTasksToDeploy* pVg = NULL;
55,655✔
3001
  
3002
  for (int32_t i = 0; i < vgNum; ++i) {
357,416✔
3003
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
301,761✔
3004
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
301,761✔
3005
    if (NULL == pVg) {
301,761✔
3006
      continue;
191,384✔
3007
    }
3008

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

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

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

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

3058
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
84,357✔
3059
  if (!GOT_SNODE(snodeId)) {
84,357✔
3060
    return;
16,944✔
3061
  }
3062
  
3063
  int32_t code = TSDB_CODE_SUCCESS;
67,413✔
3064
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
67,413✔
3065
  if (NULL == pSnode) {
67,413✔
3066
    return;
25,674✔
3067
  }
3068

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

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

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

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

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

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

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

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

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

3136
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
16,344,369✔
3137
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,344,369✔
3138
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
55,655✔
3139
  }
3140

3141
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
16,344,369✔
3142
    msmCleanDeployedSnodeTasks(pHb->snodeId);
84,357✔
3143
  }
3144
}
16,344,369✔
3145

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

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

3171
_exit:
241,553✔
3172

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

3177
  return code;
241,553✔
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) {
166,924✔
3208
  int32_t code = TSDB_CODE_SUCCESS;
166,924✔
3209
  int32_t lino = 0;
166,924✔
3210
  int32_t action = STREAM_ACT_UNDEPLOY;
166,924✔
3211
  bool    dropped = false;
166,924✔
3212

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

3246
_exit:
166,924✔
3247

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

3252
  return code;
166,924✔
3253
}
3254

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

3276
_exit:
3,520✔
3277

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

3283
  return code;
3,520✔
3284
}
3285

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

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

3296
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
318,380✔
3297
  for (int32_t i = 0; i < readerNum; ++i) {
702,972✔
3298
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
384,592✔
3299
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
384,592✔
3300
      return false;
×
3301
    }
3302
  }
3303

3304
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
318,380✔
3305
  for (int32_t i = 0; i < readerNum; ++i) {
378,716✔
3306
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
60,336✔
3307
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
60,336✔
3308
      return false;
×
3309
    }
3310
  }
3311

3312
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
318,380✔
3313
  SListNode* pNode = listHead(pStream->calcReaders);
318,380✔
3314
  for (int32_t i = 0; i < readerNum; ++i) {
929,774✔
3315
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
611,394✔
3316
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
611,394✔
3317
      return false;
×
3318
    }
3319
    pNode = TD_DLIST_NODE_NEXT(pNode);
611,394✔
3320
  }
3321

3322
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,024,064✔
3323
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
782,511✔
3324
    for (int32_t m = 0; m < runnerNum; ++m) {
1,532,889✔
3325
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
827,205✔
3326
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
827,205✔
3327
        return false;
76,827✔
3328
      }
3329
    }
3330
  }
3331
  
3332
  return true;
241,553✔
3333
}
3334

3335

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

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

3353
  stopped = atomic_load_8(&pStatus->stopped);
10,497,516✔
3354
  if (stopped) {
10,497,516✔
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,497,516✔
3361
    case STREAM_STATUS_INIT:      
10,492,193✔
3362
      if (STREAM_TRIGGER_TASK != pMsg->type) {
10,492,193✔
3363
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
10,020,148✔
3364
        return;
10,020,148✔
3365
      }
3366
      
3367
      if (INT64_MIN == pStatus->lastActionTs) {
472,045✔
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) {
472,045✔
3373
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
153,665✔
3374
        return;
153,665✔
3375
      }
3376

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

3399
_exit:
323,703✔
3400

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

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

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

3417
_exit:
166,162✔
3418

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

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

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

3448
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
3,724,671✔
3449
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,879,607✔
3450
    if (NULL == pStatus->detailStatus) {
1,879,607✔
3451
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
180,089✔
3452
      if (NULL == pStatus->detailStatus) {
180,089✔
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,879,607✔
3459
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,879,607✔
3460
  }
3461

3462
_exit:
1,845,064✔
3463

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

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

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

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

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

3496
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
28,828,347✔
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,828,347✔
3505
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,268,060✔
3506
        (*ppStatus)->runningStartTs = pCtx->currTs;
953,282✔
3507
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,314,778✔
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,828,347✔
3517
    (*ppStatus)->status = pTask->status;
28,828,347✔
3518
    (*ppStatus)->lastUpTs = pCtx->currTs;
28,828,347✔
3519
    
3520
    if (STREAM_STATUS_RUNNING != pTask->status) {
28,828,347✔
3521
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
10,497,516✔
3522
    }
3523
    
3524
    if (STREAM_TRIGGER_TASK == pTask->type) {
28,828,347✔
3525
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
3,724,671✔
3526
    }
3527
  }
3528

3529
_exit:
1,011,038✔
3530

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

3535
  return code;
1,011,038✔
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) {
241,553✔
3687
  int32_t code = TSDB_CODE_SUCCESS;
241,553✔
3688
  int32_t lino = 0;
241,553✔
3689
  if (NULL == pCtx->pRsp->start.taskList) {
241,553✔
3690
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
91,966✔
3691
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
91,966✔
3692
  }
3693

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

3703
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
483,106✔
3704
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
241,553✔
3705

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

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

3725
  SStreamTaskUndeploy undeploy;
37,286✔
3726
  for (int32_t i = 0; i < dropNum; ++i) {
204,210✔
3727
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
166,924✔
3728
    undeploy.task = *pTask;
166,924✔
3729
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
166,924✔
3730
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
166,924✔
3731

3732
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
333,848✔
3733
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
166,924✔
3734

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

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

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

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

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

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

3813
_exit:
3,520✔
3814

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

3822

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

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

3831
  while (1) {
282,359✔
3832
    pIter = taosHashIterate(pCtx->actionStm, pIter);
400,338✔
3833
    if (pIter == NULL) {
400,338✔
3834
      break;
117,979✔
3835
    }
3836

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

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

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

3853
    if (STREAM_ACT_START & pAction->actions) {
245,073✔
3854
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
241,553✔
3855
    }
3856
  }
3857
  
3858
_exit:
117,979✔
3859

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

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

3868
  return code;
117,979✔
3869
}
3870

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

3877
  while (true) {
3878
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
16,771,127✔
3879
    if (NULL == lastTs) {
16,771,127✔
3880
      if (noExists) {
667,081✔
3881
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
156,640✔
3882
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
156,640✔
3883
      }
3884

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

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

3899
        continue;
×
3900
      }
3901

3902
      return code;
2,470✔
3903
    }
3904

3905
    break;
3906
  }
3907

3908
_exit:
156,640✔
3909

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

3914
  return code;  
156,640✔
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) {
11,268✔
4010
  int32_t code = TSDB_CODE_SUCCESS;
11,268✔
4011
  int32_t lino = 0;
11,268✔
4012
  SStreamHbMsg* pReq = pCtx->pReq;
11,268✔
4013

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

4020
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
11,268✔
4021
  if (GOT_SNODE(pReq->snodeId)) {
11,268✔
4022
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,763✔
4023
  }
4024

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

4034
_exit:
11,268✔
4035

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

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

4044
  return code;
11,268✔
4045
}
4046

4047
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
5,542,878✔
4048
  int32_t listSize = taosArrayGetSize(pOReaders);
5,542,878✔
4049
  int32_t totalSize = 0;
5,542,878✔
4050
  
4051
  for (int32_t i = 0; i < listSize; ++i) {
6,502,588✔
4052
    SArray* pList = taosArrayGetP(pOReaders, i);
959,710✔
4053
    totalSize += taosArrayGetSize(pList);
959,710✔
4054
  }
4055

4056
  return totalSize;
5,542,878✔
4057
}
4058

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

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

4068

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

4073
  if (NULL == pStatus->trigOReaders) {
60,336✔
4074
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
52,269✔
4075
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
52,269✔
4076
  }
4077

4078
  while (true) {
52,269✔
4079
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
112,605✔
4080

4081
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
112,605✔
4082
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
52,269✔
4083
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
52,269✔
4084

4085
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
104,538✔
4086
      continue;
52,269✔
4087
    }
4088

4089
    *ppRes = *ppOReaderList;
60,336✔
4090
    break;
60,336✔
4091
  }
4092

4093
_exit:
60,336✔
4094

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

4099
  return code;
60,336✔
4100
}
4101

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

4108
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
70,675✔
4109
  for (int32_t i = 0; i < readerNum; ++i) {
147,317✔
4110
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
86,981✔
4111
    if (pReader->id.nodeId == vgId) {
86,981✔
4112
      readerExists = true;
10,339✔
4113
      break;
10,339✔
4114
    }
4115
  }
4116

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

4127
_exit:
10,339✔
4128

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

4133
  return code;
70,675✔
4134
}
4135

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

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

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

4175
  stopped = atomic_load_8(&pStatus->stopped);
61,984✔
4176
  if (stopped) {
61,984✔
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,984✔
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,984✔
4187
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
61,984✔
4188
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
61,984✔
4189

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

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

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

4224
_exit:
61,984✔
4225

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

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

4232
  if (code) {
61,984✔
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);
123,968✔
4240
  }
4241

4242
_final:
61,984✔
4243

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

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

4267
_exit:
2,464✔
4268

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

4273
  return code;
2,464✔
4274
}
4275

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

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

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

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

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

4319
_exit:
2,464✔
4320

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

4325
  return code;
2,464✔
4326
}
4327

4328

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

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

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

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

4389
_exit:
2,464✔
4390

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

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

4403

4404
_final:
2,464✔
4405

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

4415

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

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

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

4439
  return code;
64,448✔
4440
}
4441

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

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

4467
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
64,448✔
4468
  }
4469

4470
_exit:
16,243✔
4471

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

4476
  return code;
16,243✔
4477
}
4478

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

4484
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
16,249,418✔
4485
  if (GOT_SNODE(pReq->snodeId)) {
16,092,778✔
4486
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,169,261✔
4487
  }
4488
  
4489
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
16,092,778✔
4490
    msmHandleStreamActions(pCtx);
38,908✔
4491
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
38,908✔
4492
  }
4493

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

4500
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
16,092,778✔
4501
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
55,655✔
4502
  } else {
4503
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
16,037,123✔
4504
  }
4505

4506
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
16,092,778✔
4507
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
67,413✔
4508
  }
4509

4510
  if (taosHashGetSize(pCtx->deployStm) > 0) {
16,092,778✔
4511
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
54,102✔
4512
  }
4513

4514
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
16,092,778✔
4515
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,011,038✔
4516
  }
4517

4518
  if (taosHashGetSize(pCtx->actionStm) > 0) {
16,092,778✔
4519
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
117,979✔
4520
  }
4521

4522
_exit:
16,092,778✔
4523

4524
  if (code) {
16,249,418✔
4525
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
156,640✔
4526
  }
4527

4528
  return code;
16,249,418✔
4529
}
4530

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

4536
  if (TSDB_CODE_SUCCESS != code) {
16,344,369✔
4537
    goto _exit;
156,640✔
4538
  }
4539

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

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

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

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

4566
_exit:
16,344,369✔
4567

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

4576

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

4582
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
16,344,369✔
4583

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

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

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

4613
_exit:
16,344,369✔
4614

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

4617
  msmCleanStreamGrpCtx(pHb);
16,344,369✔
4618
  msmClearStreamToDeployMaps(pHb);
16,344,369✔
4619

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

4624
  return code;
16,344,369✔
4625
}
4626

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

4632
  mstInfo("start to process mnode become leader");
403,328✔
4633

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

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

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

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

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

4656
  streamRemoveVnodeLeader(MNODE_HANDLE);
500,503✔
4657

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

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

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

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

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

4720
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4721

4722
_exit:
36,068✔
4723

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

4728
  return code;
36,068✔
4729
}
4730

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

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

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

4760
    return true;
20,235✔
4761
  }
4762

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

4765
  return true;
448✔
4766
}
4767

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

4780
    pStatus = (SStmStatus*)pIter;
249,393✔
4781

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

4790
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
247,287✔
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)) {
247,287✔
4797
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
11,720✔
4798
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
7,824✔
4799
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4800
            
4801
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
7,824✔
4802
        continue;
7,824✔
4803
      }
4804

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

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

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

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

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

4834
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
3,159,179✔
4835
  for (int32_t i = 0; i < taskNum; ++i) {
6,673,085✔
4836
    SStmTaskStatus* pTask = *(pList + i);
3,513,906✔
4837

4838
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
3,513,906✔
4839
      continue;
445,074✔
4840
    }
4841
    
4842
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
3,070,666✔
4843
      continue;
3,069,266✔
4844
    }
4845

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

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

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

4871
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
129,877✔
4872
  void* pIter = NULL;
129,877✔
4873
  SStmVgStreamStatus* pVg = NULL;
129,877✔
4874
  int64_t streamId = 0;
129,877✔
4875
  
4876
  while (true) {
1,171,428✔
4877
    pIter = taosHashIterate(pStreams, pIter);
1,301,305✔
4878
    if (NULL == pIter) {
1,301,305✔
4879
      break;
129,877✔
4880
    }
4881

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

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

4890
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,171,428✔
4891
    if (taskNum > 0) {
1,171,428✔
4892
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
780,324✔
4893
    }
4894
  }
4895
}
129,877✔
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) {
333,038✔
4924
  void* pIter = NULL;
333,038✔
4925
  int32_t code = 0;
333,038✔
4926
  
4927
  while (true) {
1,203,167✔
4928
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,536,205✔
4929
    if (NULL == pIter) {
1,536,205✔
4930
      break;
333,038✔
4931
    }
4932

4933
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,203,167✔
4934
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,203,167✔
4935
      continue;
1,068,119✔
4936
    }
4937
    
4938
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
135,048✔
4939

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

4960
    msmCheckVgroupStreamStatus(pVg->streamTasks);
129,877✔
4961
  }
4962
}
333,038✔
4963

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

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

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

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

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

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

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

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

5039

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

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

5054
    if (NULL != pSnode->trigger) {
445,701✔
5055
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
431,211✔
5056
    }
5057

5058
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,782,804✔
5059
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,337,103✔
5060
      if (taskNum > 0) {
1,337,103✔
5061
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,259,459✔
5062
      }
5063
    }
5064
  }
5065
}
39,514✔
5066

5067

5068
void msmCheckSnodeStatus(SMnode *pMnode) {
333,038✔
5069
  void* pIter = NULL;
333,038✔
5070
  
5071
  while (true) {
434,528✔
5072
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
767,566✔
5073
    if (NULL == pIter) {
767,566✔
5074
      break;
333,038✔
5075
    }
5076

5077
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
434,528✔
5078
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
434,528✔
5079
      continue;
382,928✔
5080
    }
5081

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

5100
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
39,514✔
5101
  }
5102
}
333,038✔
5103

5104

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

5108
  msmCheckVgroupStatus(pMnode);
333,038✔
5109
  msmCheckSnodeStatus(pMnode);
333,038✔
5110
}
333,038✔
5111

5112
void msmCheckSnodesState(SMnode *pMnode) {
333,038✔
5113
  if (!MST_READY_FOR_SNODE_LOOP()) {
333,038✔
5114
    return;
296,474✔
5115
  }
5116

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

5119
  void* pIter = NULL;
36,564✔
5120
  int32_t snodeId = 0;
36,564✔
5121
  while (true) {
40,629✔
5122
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
77,193✔
5123
    if (NULL == pIter) {
77,193✔
5124
      break;
36,564✔
5125
    }
5126

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

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

5145
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
36,564✔
5146
}
5147

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

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

5160
  return true;
666,076✔
5161
}
5162

5163
void msmHealthCheck(SMnode *pMnode) {
9,962,744✔
5164
  if (!msmCheckNeedHealthCheck(pMnode)) {
9,962,744✔
5165
    return;
9,629,706✔
5166
  }
5167

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

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

5185
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
333,038✔
5186

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

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

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

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

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

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

5238
  return code;
23,408✔
5239
}
5240

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

5248
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
806,577✔
5249

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

5274
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,098,592✔
5275
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,082,160✔
5276
      if (pCtx->deployStm[m] == NULL) {
10,082,160✔
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);
10,082,160✔
5282
      
5283
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,082,160✔
5284
      if (pCtx->actionStm[m] == NULL) {
10,082,160✔
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);
10,082,160✔
5290
    }
5291
  }
5292
  
5293
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
403,328✔
5294
  if (mStreamMgmt.streamMap == NULL) {
403,328✔
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);
403,328✔
5300
  
5301
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
403,328✔
5302
  if (mStreamMgmt.taskMap == NULL) {
403,328✔
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);
403,328✔
5309
  if (mStreamMgmt.vgroupMap == NULL) {
403,328✔
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);
403,328✔
5315

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

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

5355
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
403,328✔
5356
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
403,328✔
5357

5358
  mStreamMgmt.lastTaskId = 1;
403,328✔
5359

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

5363
  if (activeStreamNum > 0) {
403,328✔
5364
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
687✔
5365
  } else {
5366
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
402,641✔
5367
  }
5368

5369
_exit:
403,328✔
5370

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

5378
  return code;
403,328✔
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