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

taosdata / TDengine / #4800

16 Oct 2025 09:19AM UTC coverage: 53.935% (-7.1%) from 61.083%
#4800

push

travis-ci

web-flow
Merge b32e3a393 into a190048d5

134724 of 323629 branches covered (41.63%)

Branch coverage included in aggregate %.

184803 of 268802 relevant lines covered (68.75%)

69058627.2 hits per line

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

61.2
/source/dnode/mnode/impl/src/mndStreamMgmt.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "mndStream.h"
17
#include "mndDb.h"
18
#include "mndPrivilege.h"
19
#include "mndShow.h"
20
#include "mndStb.h"
21
#include "mndTrans.h"
22
#include "osMemory.h"
23
#include "parser.h"
24
#include "taoserror.h"
25
#include "tmisce.h"
26
#include "tname.h"
27
#include "mndDnode.h"
28
#include "mndVgroup.h"
29
#include "mndSnode.h"
30
#include "mndMnode.h"
31
#include "cmdnodes.h"
32

33
void msmDestroyActionQ() {
316,140✔
34
  SStmQNode* pQNode = NULL;
316,140✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
316,140✔
37
    return;
158,070✔
38
  }
39

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

43
  taosMemoryFreeClear(mStreamMgmt.actionQ->head);
158,070!
44
  taosMemoryFreeClear(mStreamMgmt.actionQ);
158,070!
45
}
46

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
790,350✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
4,742,100✔
49
    taosHashCleanup(pCtx->deployStm[m]);
3,951,750✔
50
    taosHashCleanup(pCtx->actionStm[m]);
3,951,750✔
51
  }
52
}
790,350✔
53

54
void msmDestroyThreadCtxs() {
316,140✔
55
  if (NULL == mStreamMgmt.tCtx) {
316,140✔
56
    return;
158,070✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
948,420✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
790,350✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
158,070!
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
316,140!
92

93
  mstInfo("mnode stream mgmt destroyed");  
316,140!
94
}
316,140✔
95

96

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

102
  mstsInfo("try to stop stream for error: %s", tstrerror(errCode));
1,153!
103

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

111
    pStatus = pStream;
1,153✔
112
  }
113

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

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

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

136
_exit:
636✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
1,153✔
139

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

145

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

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

200
  pSnode = NULL;
168,549✔
201

202
_exit:
168,549✔
203

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

208
  return code;
168,549✔
209
}
210

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

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

235
  pDnode = NULL;
381,319✔
236

237
_exit:
381,319✔
238

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

243
  return code;
381,319✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
146,532✔
275
  int32_t code = TSDB_CODE_SUCCESS;
146,532✔
276
  int32_t lino = 0;
146,532✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
146,532✔
278
  if (NULL == pStream) {
146,532✔
279
    SStmVgStreamStatus stream = {0};
115,661✔
280
    if (trigReader) {
115,661✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
78,567✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
78,567!
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
157,134!
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
37,094✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
37,094!
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
74,188!
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
115,661!
290
    goto _exit;
115,661✔
291
  }
292
  
293
  if (trigReader) {
30,871✔
294
    if (NULL == pStream->trigReaders) {
3,741!
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
3,741✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
3,741!
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
7,482!
300
    goto _exit;
3,741✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
27,130✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
20,073✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
20,073!
306
  }
307

308
  TSDB_CHECK_NULL(taosArrayPush(pStream->calcReaders, &pStatus), code, lino, _exit, terrno);
54,260!
309

310
_exit:
27,130✔
311

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

320
  return code;
146,532✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
32,210✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
32,210!
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
32,210!
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
114,322!
339
  }
340
  
341
_exit:
146,472✔
342

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

350
  return code;
146,532✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
147,258✔
363
    if (NULL == pVg) {
147,258✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
41,909✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
41,909!
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
83,818!
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
41,909✔
368
      if (TSDB_CODE_SUCCESS == code) {
41,909!
369
        goto _return;
41,909✔
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);
105,349✔
381
    if (NULL == pVg->taskList) {
105,349!
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)) {
210,698!
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
105,349✔
390
    
391
    taosHashRelease(pVgMap, pVg);
105,349✔
392
    break;
105,349✔
393
  }
394
  
395
_return:
147,258✔
396

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

404
  return code;
147,258✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
232,962✔
409
  int32_t code = TSDB_CODE_SUCCESS;
232,962✔
410
  int32_t lino = 0;
232,962✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
232,962✔
412
  if (NULL == pStream) {
232,962✔
413
    SStmSnodeStreamStatus stream = {0};
58,913✔
414
    if (deployId < 0) {
58,913✔
415
      stream.trigger = pStatus;
630✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
58,283✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
58,283!
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
116,566!
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
58,913!
423
    goto _exit;
58,913✔
424
  }
425
  
426
  if (deployId < 0) {
174,049✔
427
    if (NULL != pStream->trigger) {
55,863!
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;
55,863✔
433
    goto _exit;
55,863✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
118,186✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
109,306✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
109,306!
439
  }
440

441
  TSDB_CHECK_NULL(taosArrayPush(pStream->runners[deployId], &pStatus), code, lino, _exit, terrno);
236,372!
442

443
_exit:
118,186✔
444

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

453
  return code;
232,962✔
454
}
455

456

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

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

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

484
  return code;
232,962✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
56,493✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
14,236✔
568
      ext.deployed = false;
14,236✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
28,472!
570

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

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

614
  return code;
176,469✔
615
}
616

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

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

632
_exit:
167,589✔
633

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

638
  return code;
167,589✔
639
}
640

641

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
5,744✔
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) {
11,254,129✔
691
  int32_t  code = TSDB_CODE_SUCCESS;
11,254,129✔
692
  int32_t  lino = 0;
11,254,129✔
693
  SStmVgroupStatus* pStatus = taosHashGet(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
11,254,129✔
694
  if (NULL == pStatus) {
11,254,129✔
695
    mstDebug("vgroup %d not exists in vgroupMap, ignore update upTs", vgId);
9,797,335✔
696
    return;
9,797,335✔
697
  }
698

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

707
      continue;
×
708
    }
709

710
    return;
31,612✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
4,214,270✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
15,358,289✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
11,144,019✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
11,144,019✔
725
  }
726

727
_exit:
4,214,270✔
728

729
  if (code) {
4,214,270!
730
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
731
  }
732

733
  return code;
4,214,270✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
98,586✔
739
  int32_t num = taosArrayGetSize(pList);
98,586✔
740
  for (int32_t i = 0; i < num; ++i) {
226,341✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
184,584✔
742
    if (pScan->readFromCache) {
184,584✔
743
      return pScan->scanPlan;
56,829✔
744
    }
745
  }
746

747
  return NULL;
41,757✔
748
}
749

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

773
  return TSDB_CODE_SUCCESS;
147,258✔
774
}
775

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

780
  if (pInfo->runnerDeploys > 0) {
55,863!
781
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
55,863✔
782
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
55,863!
783
  }
784
  
785
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
223,452✔
786
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
167,589✔
787
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
167,589!
788

789
    if (!STREAM_IS_TOP_RUNNER(pStatus->flags)) {
167,589!
790
      mstsError("the last runner task %" PRIx64 " SID:%" PRId64 " tidx:%d in deploy %d is not top runner", 
×
791
          pStatus->id.taskId, pStatus->id.seriousId, pStatus->id.taskIdx, i);
792
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
793
    }
794
    
795
    SStreamRunnerTarget runner;
166,509✔
796
    runner.addr.taskId = pStatus->id.taskId;
167,589✔
797
    runner.addr.nodeId = pStatus->id.nodeId;
167,589✔
798
    runner.addr.epset = mndGetDnodeEpsetById(pMnode, pStatus->id.nodeId);
167,589✔
799
    runner.execReplica = pInfo->runnerReplica; 
167,589✔
800
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &runner), code, lino, _exit, terrno);
335,178!
801
    mstsDebug("the %dth runner target added to trigger's runnerList, TASK:%" PRIx64 , i, runner.addr.taskId);
167,589✔
802
  }
803

804
_exit:
55,863✔
805

806
  if (code) {
55,863!
807
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
808
  }
809
  
810
  return TSDB_CODE_SUCCESS;
55,863✔
811
}
812

813
int32_t msmBuildStreamSnodeInfo(SMnode* pMnode, SStreamObj* pStream, SStreamSnodeInfo* pInfo) {
×
814
  int64_t streamId = pStream->pCreate->streamId;
×
815
  int32_t leaderSnodeId = atomic_load_32(&pStream->mainSnodeId);
×
816
  SSnodeObj* pSnode = mndAcquireSnode(pMnode, leaderSnodeId);
×
817
  if (NULL == pSnode) {
×
818
    mstsError("snode %d not longer exists, ignore build stream snode info", leaderSnodeId);
×
819
    return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
820
  }
821
  
822
  pInfo->leaderSnodeId = leaderSnodeId;
×
823
  pInfo->replicaSnodeId = pSnode->replicaId;
×
824

825
  mndReleaseSnode(pMnode, pSnode);
×
826

827
  pInfo->leaderEpSet = mndGetDnodeEpsetById(pMnode, pInfo->leaderSnodeId);
×
828
  if (GOT_SNODE(pInfo->replicaSnodeId)) {
×
829
    pInfo->replicaEpSet = mndGetDnodeEpsetById(pMnode, pInfo->replicaSnodeId);
×
830
  }
831

832
  return TSDB_CODE_SUCCESS;
×
833
}
834

835
int32_t msmBuildTriggerDeployInfo(SMnode* pMnode, SStmStatus* pInfo, SStmTaskDeploy* pDeploy, SStreamObj* pStream) {
56,493✔
836
  int32_t code = TSDB_CODE_SUCCESS;
56,493✔
837
  int32_t lino = 0;
56,493✔
838
  int64_t streamId = pStream->pCreate->streamId;
56,493✔
839
  SStreamTriggerDeployMsg* pMsg = &pDeploy->msg.trigger;
56,493✔
840
  
841
  pMsg->triggerType = pStream->pCreate->triggerType;
56,493✔
842
  pMsg->igDisorder = pStream->pCreate->igDisorder;
56,493✔
843
  pMsg->fillHistory = pStream->pCreate->fillHistory;
56,493✔
844
  pMsg->fillHistoryFirst = pStream->pCreate->fillHistoryFirst;
56,493✔
845
  pMsg->lowLatencyCalc = pStream->pCreate->lowLatencyCalc;
56,493✔
846
  pMsg->igNoDataTrigger = pStream->pCreate->igNoDataTrigger;
56,493✔
847
  pMsg->isTriggerTblVirt = STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags);
56,493✔
848
  pMsg->triggerHasPF = pStream->pCreate->triggerHasPF;
56,493✔
849
  pMsg->isTriggerTblStb = (pStream->pCreate->triggerTblType == TSDB_SUPER_TABLE);
56,493✔
850
  pMsg->partitionCols = pStream->pCreate->partitionCols;
56,493✔
851

852
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
56,493✔
853
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
56,493✔
854
  pMsg->addOptions = pStream->pCreate->addOptions;
56,493✔
855
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
56,493✔
856

857
  pMsg->maxDelay = pStream->pCreate->maxDelay;
56,493✔
858
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
56,493✔
859
  pMsg->watermark = pStream->pCreate->watermark;
56,493✔
860
  pMsg->expiredTime = pStream->pCreate->expiredTime;
56,493✔
861
  pMsg->trigger = pInfo->pCreate->trigger;
56,493✔
862

863
  pMsg->eventTypes = pStream->pCreate->eventTypes;
56,493✔
864
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
56,493✔
865
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
56,493✔
866
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
56,493✔
867
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
56,493✔
868
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
56,493✔
869
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
15,915✔
870
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
15,915✔
871
  }
872

873
  SStreamTaskAddr addr;
56,133✔
874
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
56,493✔
875
  if (triggerReaderNum > 0) {
56,493✔
876
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
56,217✔
877
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
56,217!
878
  }
879
  
880
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
127,281✔
881
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
70,788✔
882
    addr.taskId = pStatus->id.taskId;
70,788✔
883
    addr.nodeId = pStatus->id.nodeId;
70,788✔
884
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
70,788✔
885
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
141,576!
886
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
70,788✔
887
  }
888

889
  pMsg->leaderSnodeId = pStream->mainSnodeId;
56,493✔
890
  pMsg->streamName = pInfo->streamName;
56,493✔
891

892
  if (0 == pInfo->runnerNum) {
56,493✔
893
    mstsDebug("no runner task, skip set trigger's runner list, deployNum:%d", pInfo->runnerDeploys);
630!
894
    return code;
630✔
895
  }
896

897
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
55,863!
898

899
_exit:
55,863✔
900

901
  if (code) {
55,863!
902
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
903
  } else {
904
    mstsDebug("trigger deploy info built, readerNum:%d, runnerNum:%d", (int32_t)taosArrayGetSize(pMsg->readerList), (int32_t)taosArrayGetSize(pMsg->runnerList));
55,863✔
905
  }
906
  
907
  return TSDB_CODE_SUCCESS;
55,863✔
908
}
909

910

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

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

942
_exit:
176,469✔
943

944
  if (code) {
176,469!
945
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
946
  }
947
  
948
  return code;
176,469✔
949
}
950

951

952
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
126,134✔
953
  int32_t code = TSDB_CODE_SUCCESS;
126,134✔
954
  int32_t lino = 0;
126,134✔
955
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
126,134!
956
  SListNode* pNode = pList ? listHead(pList) : NULL;
126,134✔
957
  
958
  for (int32_t i = 0; i < taskNum; ++i) {
272,666✔
959
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
146,532✔
960
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
146,532!
961
    if (pNode) {
146,532✔
962
      pNode = TD_DLIST_NODE_NEXT(pNode);
62,596✔
963
    }
964
  }
965
  
966
_exit:
126,134✔
967

968
  if (code) {
126,134!
969
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
970
  }
971
  
972
  return code;
126,134✔
973
}
974

975

976
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
224,082✔
977
  int32_t code = TSDB_CODE_SUCCESS;
224,082✔
978
  int32_t lino = 0;
224,082✔
979
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
224,082✔
980
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
224,082✔
981
  
982
  for (int32_t i = 0; i < rtaskNum; ++i) {
457,044✔
983
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
232,962✔
984
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
232,962!
985
  }
986
  
987
_exit:
224,082✔
988

989
  if (code) {
224,082!
990
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
991
  }
992

993
  return code;
224,082✔
994
}
995

996
int64_t msmAssignTaskId(void) {
379,494✔
997
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
379,494✔
998
}
999

1000
int64_t msmAssignTaskSeriousId(void) {
379,494✔
1001
  return taosGetTimestampNs();
379,494✔
1002
}
1003

1004

1005
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
292,958✔
1006
  int32_t code = TSDB_CODE_SUCCESS;
292,958✔
1007
  int32_t lino = 0;
292,958✔
1008
  bool     noExists = false;
292,958✔
1009
  SStmSnodeStatus* pStatus = NULL;
292,958✔
1010

1011
  while (true) {
1012
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
296,846✔
1013
    if (NULL == pStatus) {
296,846✔
1014
      if (noExists) {
3,888!
1015
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1016
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1017
      }
1018

1019
      noExists = true;
3,888✔
1020
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
3,888!
1021
      
1022
      continue;
3,888✔
1023
    }
1024

1025
    *alive = (pStatus->runnerThreadNum >= 0);
292,958✔
1026
    break;
292,958✔
1027
  }
1028

1029
_exit:
292,958✔
1030

1031
  if (code) {
292,958!
1032
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1033
  }
1034

1035
  return code;
292,958✔
1036
}
1037

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

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

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

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

1076
_exit:
×
1077

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

1082
  return 0;
×
1083
}
1084

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

1099
  int32_t snodeTarget = taosRand() % snodeNum;
175,399!
1100

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

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

1126
    snodeId = pObj->id;
180,602✔
1127
    if (snodeIdx == snodeTarget) {
180,602✔
1128
      sdbRelease(pMnode->pSdb, pObj);
175,399✔
1129
      sdbCancelFetch(pMnode->pSdb, pIter);
175,399✔
1130
      pObj = NULL;
175,399✔
1131
      goto _exit;
175,399✔
1132
    }
1133

1134
    sdbRelease(pMnode->pSdb, pObj);
5,203✔
1135
    snodeIdx++;
5,203✔
1136
  }
1137

1138
_exit:
177,129✔
1139

1140
  if (code) {
177,129!
1141
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1142
  }
1143

1144
  if (0 == snodeId) {
177,129✔
1145
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
1,730✔
1146
  }
1147

1148
  return snodeId;
177,129✔
1149
}
1150

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

1160
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
224,082✔
1161

1162
_exit:
224,082✔
1163

1164
  if (0 == snodeId) {
224,082!
1165
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1166
  }
1167

1168
  return snodeId;
224,082✔
1169
}
1170

1171

1172
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
56,493✔
1173
  int32_t code = TSDB_CODE_SUCCESS;
56,493✔
1174
  int32_t lino = 0;
56,493✔
1175
  int64_t streamId = pStream->pCreate->streamId;
56,493✔
1176

1177
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
56,493!
1178
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
56,493!
1179

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

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

1201
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
56,493!
1202
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
56,493!
1203

1204
_exit:
56,493✔
1205

1206
  if (code) {
56,493!
1207
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1208
  }
1209

1210
  return code;
56,493✔
1211
}
1212

1213
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
82,308✔
1214
  int32_t code = TSDB_CODE_SUCCESS;
82,308✔
1215
  int32_t lino = 0;
82,308✔
1216

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

1239
_exit:
82,308✔
1240

1241
  if (code) {
82,308!
1242
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1243
  }
1244

1245
  return code;
82,308✔
1246
}
1247

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

1277
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
32,899✔
1278
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
32,899!
1279
      
1280
      void *pIter = NULL;
32,899✔
1281
      while (1) {
189,836✔
1282
        SStmTaskDeploy info = {0};
222,735✔
1283
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
222,735✔
1284
        if (pIter == NULL) {
222,735✔
1285
          break;
32,899✔
1286
        }
1287
      
1288
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
189,836!
1289
          pState = taosArrayReserve(pInfo->trigReaders, 1);
47,470✔
1290

1291
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
47,470✔
1292
          if (code) {
47,470!
1293
            sdbRelease(pSdb, pVgroup);
×
1294
            sdbCancelFetch(pSdb, pIter);
×
1295
            pVgroup = NULL;
×
1296
            TAOS_CHECK_EXIT(code);
×
1297
          }
1298
        }
1299

1300
        sdbRelease(pSdb, pVgroup);
189,836✔
1301
      }
1302
      break;
32,899✔
1303
    }
1304
    default:
276✔
1305
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
276!
1306
      break;
276✔
1307
  }
1308

1309
_exit:
56,493✔
1310

1311
  mndReleaseDb(pCtx->pMnode, pDb);
56,493✔
1312

1313
  if (code) {
56,493!
1314
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1315
  }
1316

1317
  return code;
56,493✔
1318
}
1319

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

1343
  key[1] = pSubplan->id.subplanId;
62,596✔
1344

1345
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
62,596✔
1346
  if (NULL == ppRes) {
62,596!
1347
    SArray* pRes = taosArrayInit(1, sizeof(addr));
62,596✔
1348
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
62,596!
1349
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
125,192!
1350
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
62,596!
1351
  } else {
1352
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1353
  }
1354

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

1361
  nodesDestroyNode((SNode*)pSubplan);
62,596✔
1362

1363
  if (code) {
62,596!
1364
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1365
  }
1366

1367
  return code;
62,596✔
1368
}
1369

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

1378
  SStmTaskSrcAddr addr;
28,405✔
1379
  addr.isFromCache = true;
28,405✔
1380
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
28,405✔
1381
  addr.taskId = pCtx->triggerTaskId;
28,405✔
1382
  addr.vgId = pCtx->triggerNodeId;
28,405✔
1383
  addr.groupId = pSubplan->id.groupId;
28,405✔
1384

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

1400
  nodesDestroyNode((SNode*)pSubplan);
28,405✔
1401
  
1402
  if (code) {
28,405!
1403
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1404
  }
1405

1406
  return code;
28,405✔
1407
}
1408

1409

1410
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
64,224✔
1411
  int32_t code = TSDB_CODE_SUCCESS;
64,224✔
1412
  int32_t lino = 0;
64,224✔
1413

1414
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
64,224!
1415

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

1438
_exit:
64,224✔
1439

1440
  if (code) {
64,224!
1441
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1442
  }
1443

1444
  return code;
64,224✔
1445
}
1446

1447

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

1457
  
1458
  for (int32_t i = 0; i < calcTasksNum; ++i) {
147,494✔
1459
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
91,001✔
1460
    if (pScan->readFromCache) {
91,001✔
1461
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
28,405!
1462
      continue;
28,405✔
1463
    }
1464
    
1465
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
62,596✔
1466
    for (int32_t m = 0; m < vgNum; ++m) {
125,192✔
1467
      pState = tdListReserve(pInfo->calcReaders);
62,596✔
1468
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
62,596!
1469

1470
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
62,596!
1471
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
62,596!
1472
    }
1473
  }
1474

1475
_exit:
56,493✔
1476

1477
  if (code) {
56,493!
1478
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1479
  }
1480

1481
  return code;
56,493✔
1482
}
1483

1484

1485

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

1513
_exit:
×
1514

1515
  if (code) {
×
1516
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1517
  }
1518

1519
  return code;
×
1520
}
1521

1522
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
56,493✔
1523
  int32_t code = TSDB_CODE_SUCCESS;
56,493✔
1524
  int32_t lino = 0;
56,493✔
1525
  int64_t streamId = pStream->pCreate->streamId;
56,493✔
1526
  
1527
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
56,493!
1528
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
56,493!
1529

1530
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
56,493!
1531
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
56,493!
1532
  
1533
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
56,493!
1534
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
56,493!
1535
  
1536
_exit:
56,493✔
1537

1538
  if (code) {
56,493!
1539
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1540
  }
1541
  
1542
  return code;
56,493✔
1543
}
1544

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

1556
  source.addr.epSet = pSrc->epset;
281,883✔
1557
  source.addr.nodeId = pSrc->vgId;
281,883✔
1558

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

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

1578
  mstsError("subplanId %d not found in runner list", subplanId);
×
1579

1580
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1581
}
1582

1583
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
176,469✔
1584
  int32_t code = TSDB_CODE_SUCCESS;
176,469✔
1585
  int32_t lino = 0;
176,469✔
1586
  int64_t key[2] = {streamId, -1};
176,469✔
1587
  SNode* pNode = NULL;
176,469✔
1588
  SStreamTask* pTask = &pDeploy->task;
176,469✔
1589
  FOREACH(pNode, pPlan->pChildren) {
458,352!
1590
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
281,883✔
1591
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
8,880!
1592
      continue;
8,880✔
1593
    }
1594
    
1595
    SValueNode* pVal = (SValueNode*)pNode;
273,003✔
1596
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
273,003!
1597
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1598
      continue;
×
1599
    }
1600

1601
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
273,003✔
1602

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

1609
    int32_t childrenNum = taosArrayGetSize(*ppRes);
273,003✔
1610
    for (int32_t i = 0; i < childrenNum; ++i) {
546,006✔
1611
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
273,003✔
1612
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
273,003!
1613
    }
1614
  }
1615

1616
_exit:
176,469✔
1617

1618
  if (code) {
176,469!
1619
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1620
  }
1621

1622
  return code;
176,469✔
1623
}
1624

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

1633
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
176,469!
1634

1635
  SNode* pTmp = NULL;
176,469✔
1636
  WHERE_EACH(pTmp, pPlan->pChildren) {
458,352!
1637
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
281,883✔
1638
      ERASE_NODE(pPlan->pChildren);
273,003✔
1639
      continue;
273,003✔
1640
    }
1641
    WHERE_NEXT;
8,880✔
1642
  }
1643
  nodesClearList(pPlan->pChildren);
176,469✔
1644
  pPlan->pChildren = NULL;
176,469✔
1645

1646
  if (NULL == pPlan->pParents) {
176,469✔
1647
    goto _exit;
167,589✔
1648
  }
1649

1650
  SNode* pNode = NULL;
8,880✔
1651
  int64_t parentTaskId = 0;
8,880✔
1652
  SStmTaskSrcAddr addr = {0};
8,880✔
1653
  addr.taskId = pDeploy->task.taskId;
8,880✔
1654
  addr.vgId = pDeploy->task.nodeId;
8,880✔
1655
  addr.groupId = pPlan->id.groupId;
8,880✔
1656
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
8,880✔
1657
  FOREACH(pNode, pPlan->pParents) {
17,760!
1658
    SSubplan* pSubplan = (SSubplan*)pNode;
8,880✔
1659
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
8,880!
1660
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
8,880!
1661
  }
1662
  
1663
_exit:
175,389✔
1664

1665
  if (code) {
176,469!
1666
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1667
  }
1668

1669
  return code;
176,469✔
1670
}
1671

1672
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
167,589✔
1673
  int32_t code = TSDB_CODE_SUCCESS;
167,589✔
1674
  int32_t lino = 0;
167,589✔
1675
  int64_t streamId = pStream->pCreate->streamId;
167,589✔
1676
  int32_t runnerNum = taosArrayGetSize(pRunners);
167,589✔
1677
  
1678
  for (int32_t i = 0; i < runnerNum; ++i) {
344,058✔
1679
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
176,469✔
1680
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
176,469!
1681
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
176,469!
1682

1683
    SStreamTask* pTask = &pDeploy->task;
176,469✔
1684
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
176,469✔
1685
  }
1686

1687
_exit:
167,589✔
1688

1689
  if (code) {
167,589!
1690
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1691
  }
1692

1693
  return code;
167,589✔
1694
}
1695

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

1709
  if (pDag->numOfSubplans <= 0) {
55,863!
1710
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1711
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1712
  }
1713

1714
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
55,863!
1715
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1716
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1717
  }
1718

1719
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
55,863!
1720
  if (levelNum <= 0) {
55,863!
1721
    mstsError("invalid level num:%d", levelNum);
×
1722
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1723
  }
1724

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

1739
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
55,863✔
1740
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
55,863!
1741
  
1742
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
223,452✔
1743
    totalTaskNum = 0;
167,589✔
1744

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

1751
    taskIdx = 0;
167,589✔
1752
    
1753
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
339,618✔
1754
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
172,029✔
1755
      if (NULL == plans) {
172,029!
1756
        mstsError("empty level plan, level:%d", i);
×
1757
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1758
      }
1759

1760
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
172,029!
1761
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1762
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1763
      }
1764

1765
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
172,029!
1766
      if (taskNum <= 0) {
172,029!
1767
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1768
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1769
      }
1770

1771
      totalTaskNum += taskNum;
172,029✔
1772
      if (totalTaskNum > pDag->numOfSubplans) {
172,029!
1773
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1774
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1775
      }
1776

1777
      for (int32_t n = 0; n < taskNum; ++n) {
348,498✔
1778
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
176,469✔
1779
        pState = taosArrayReserve(deployList, 1);
176,469✔
1780

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

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

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

1808
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
172,029✔
1809
    }
1810

1811
    if (totalTaskNum != pDag->numOfSubplans) {
167,589!
1812
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1813
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1814
    }
1815

1816
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
167,589!
1817

1818
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
167,589!
1819

1820
    nodesDestroyNode((SNode *)pDag);
167,589✔
1821
    pDag = NULL;
167,589✔
1822
    
1823
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
167,589!
1824

1825
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
167,589✔
1826
  }
1827

1828
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
223,452✔
1829
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
167,589!
1830
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
167,589!
1831
  }
1832
  
1833
  pInfo->runnerNum = totalTaskNum;
55,863✔
1834
  
1835
_exit:
55,863✔
1836

1837
  if (code) {
55,863!
1838
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1839
  }
1840

1841
  taosArrayDestroy(deployTaskList);
55,863✔
1842
  nodesDestroyNode((SNode *)pDag);
55,863✔
1843

1844
  return code;
55,863✔
1845
}
1846

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

1864
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
×
1865
    deployId = pAction->deployId[r];
×
1866

1867
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
×
1868

1869
    pStartRunner = pRunner;
×
1870
    totalTaskNum = 0;
×
1871

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

1877
    taskIdx = 0;
×
1878
    
1879
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
×
1880
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
×
1881
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
×
1882
      totalTaskNum += taskNum;
×
1883

1884
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
×
1885
      
1886
      for (int32_t n = 0; n < taskNum; ++n) {
×
1887
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
×
1888

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

1895
        pRunner->id.nodeId = newNodeId;
×
1896

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

1907
        pRunner++;
×
1908
      }
1909

1910
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
×
1911
    }
1912

1913
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
×
1914

1915
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
×
1916

1917
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
×
1918

1919
    nodesDestroyNode((SNode *)pDag);
×
1920
    pDag = NULL;
×
1921

1922
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
×
1923
  }
1924

1925
_exit:
×
1926

1927
  if (code) {
×
1928
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1929
  }
1930

1931
  nodesDestroyNode((SNode *)pDag);
×
1932
  taosArrayDestroy(deployTaskList);
×
1933

1934
  return code;
×
1935
}
1936

1937

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

1946
_exit:
53,462✔
1947

1948
  if (code) {
53,462!
1949
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1950
  }
1951

1952
  return code;
53,462✔
1953
}
1954

1955

1956
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
56,493✔
1957
  if (NULL == pStream->pCreate->calcPlan) {
56,493✔
1958
    return TSDB_CODE_SUCCESS;
630✔
1959
  }
1960
  
1961
  int32_t code = TSDB_CODE_SUCCESS;
55,863✔
1962
  int32_t lino = 0;
55,863✔
1963
  int64_t streamId = pStream->pCreate->streamId;
55,863✔
1964
  SQueryPlan* pPlan = NULL;
55,863✔
1965

1966
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
55,863!
1967

1968
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
223,452✔
1969
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
167,589✔
1970
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
167,589!
1971
  }
1972

1973
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
55,863✔
1974
  pPlan = NULL;
55,863✔
1975
  
1976
  TAOS_CHECK_EXIT(code);
55,863!
1977

1978
  taosHashClear(mStreamMgmt.toUpdateScanMap);
55,863✔
1979
  mStreamMgmt.toUpdateScanNum = 0;
55,863✔
1980

1981
_exit:
55,863✔
1982

1983
  nodesDestroyNode((SNode *)pPlan);
55,863✔
1984

1985
  if (code) {
55,863!
1986
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1987
  }
1988

1989
  return code;
55,863✔
1990
}
1991

1992

1993
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
56,493✔
1994
  int32_t code = TSDB_CODE_SUCCESS;
56,493✔
1995
  int32_t lino = 0;
56,493✔
1996
  int64_t streamId = pStream->pCreate->streamId;
56,493✔
1997

1998
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
56,493!
1999

2000
  pCtx->triggerTaskId = msmAssignTaskId();
56,493✔
2001
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
56,493✔
2002
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
56,493!
2003
    TAOS_CHECK_EXIT(terrno);
×
2004
  }
2005

2006
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
56,493!
2007
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
56,493!
2008
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
56,493!
2009
  
2010
_exit:
56,493✔
2011

2012
  if (code) {
56,493!
2013
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2014
  }
2015

2016
  return code;
56,493✔
2017
}
2018

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

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

2058
_exit:
×
2059

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

2065
  return code;
×
2066
}
2067

2068

2069
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
54,092✔
2070
  int32_t code = TSDB_CODE_SUCCESS;
54,092✔
2071
  int32_t lino = 0;
54,092✔
2072
  int64_t streamId = pStream->pCreate->streamId;
54,092✔
2073

2074
  pStatus->lastActionTs = INT64_MIN;
54,092✔
2075

2076
  if (NULL == pStatus->streamName) {
54,092!
2077
    pStatus->streamName = taosStrdup(pStream->name);
54,092!
2078
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
54,092!
2079
  }
2080

2081
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
54,092!
2082
  
2083
  if (pStream->pCreate->numOfCalcSubplan > 0) {
54,092✔
2084
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
53,462✔
2085
    
2086
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
53,462!
2087
  }
2088

2089
  if (initList) {
54,092!
2090
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2091

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

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

2109
  if (code) {
54,092!
2110
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2111
  }
2112

2113
  return code;
54,092✔
2114
}
2115

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

2122
  if (NULL == pStatus) {
56,493✔
2123
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
54,092!
2124

2125
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
54,092!
2126

2127
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
54,092✔
2128
  }
2129
  
2130
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
56,493!
2131

2132
  mstLogSStmStatus("stream deployed", streamId, pStatus);
56,493✔
2133

2134
_exit:
56,493✔
2135

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

2145
  return code;
56,493✔
2146
}
2147

2148

2149
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
2,560✔
2150
  int32_t code = TSDB_CODE_SUCCESS;
2,560✔
2151
  void* pIter = NULL;
2,560✔
2152

2153
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
4,100✔
2154
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
1,540✔
2155
    (void)mstWaitLock(&pVg->lock, true);
1,540✔
2156

2157
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
1,540✔
2158
    if (atomic_load_32(&pVg->deployed) == taskNum) {
1,540!
2159
      taosRUnLockLatch(&pVg->lock);
×
2160
      continue;
×
2161
    }
2162

2163
    for (int32_t i = 0; i < taskNum; ++i) {
7,849✔
2164
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
6,309✔
2165
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
6,309!
2166
        continue;
5,241✔
2167
      }
2168

2169
      mstDestroySStmTaskToDeployExt(pExt);
1,068✔
2170
      pExt->deployed = true;
1,068✔
2171
    }
2172
    
2173
    taosRUnLockLatch(&pVg->lock);
1,540✔
2174
  }
2175

2176
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
5,794✔
2177
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
3,234✔
2178
    (void)mstWaitLock(&pSnode->lock, true);
3,234✔
2179

2180
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
3,234✔
2181
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
3,234✔
2182
      for (int32_t i = 0; i < taskNum; ++i) {
4,774✔
2183
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
3,234✔
2184
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
3,234!
2185
          continue;
2,700✔
2186
        }
2187
        
2188
        mstDestroySStmTaskToDeployExt(pExt);
534✔
2189
        pExt->deployed = true;
534✔
2190
      }
2191
    }
2192

2193
    taskNum = taosArrayGetSize(pSnode->runnerList);
3,234✔
2194
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
3,234!
2195
      for (int32_t i = 0; i < taskNum; ++i) {
12,936✔
2196
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
9,702✔
2197
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
9,702!
2198
          continue;
8,100✔
2199
        }
2200
        
2201
        mstDestroySStmTaskToDeployExt(pExt);
1,602✔
2202
        pExt->deployed = true;
1,602✔
2203
      }
2204
    }
2205

2206
    taosRUnLockLatch(&pSnode->lock);
3,234✔
2207
  }
2208

2209
  
2210
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
9,839✔
2211
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
7,279✔
2212
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
7,279✔
2213
    if (TSDB_CODE_SUCCESS == code) {
7,279✔
2214
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
4,254✔
2215
    }
2216
  }
2217

2218
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
8,772✔
2219
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
6,212✔
2220
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
6,212✔
2221
    if (TSDB_CODE_SUCCESS == code) {
6,212✔
2222
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
3,986✔
2223
    }
2224
  }
2225

2226
  size_t keyLen = 0;
2,560✔
2227
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
96,285✔
2228
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
93,725✔
2229
    if (*pStreamId == streamId) {
93,725✔
2230
      int64_t taskId = *(pStreamId + 1);
16,950✔
2231
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
16,950✔
2232
      if (code) {
16,950!
2233
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2234
      } else {
2235
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
16,950✔
2236
      }
2237
    }
2238
  }
2239

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

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

2257
  mstResetSStmStatus(pStatus);
2,401✔
2258

2259
  pStatus->deployTimes++;
2,401✔
2260
}
2,401✔
2261

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

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

2289
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
56,781✔
2290
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
56,781✔
2291
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
288!
2292
    return TSDB_CODE_SUCCESS;
288✔
2293
  }
2294

2295
  TAOS_CHECK_EXIT(code);
56,493!
2296

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

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

2313
_exit:
56,493✔
2314

2315
  mndReleaseStream(pCtx->pMnode, pStream);
56,493✔
2316

2317
  if (code) {
56,493!
2318
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2319
  }
2320

2321
  return code;
56,493✔
2322
}
2323

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

2356
_exit:
726✔
2357

2358
  if (code) {
726!
2359
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2360
  }
2361

2362
  return code;
726✔
2363
}
2364

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

2396
_exit:
2397

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

2402
  return code;
2403
}
2404
*/
2405

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

2455
_exit:
×
2456

2457
  if (code) {
×
2458
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2459
  }
2460

2461
  return code;
×
2462
}
2463

2464

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

2472
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
726!
2473

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

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

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

2492
  TAOS_CHECK_EXIT(code);
726!
2493

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

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

2524
_exit:
726✔
2525

2526
  if (pStream) {
726!
2527
    mndReleaseStream(pCtx->pMnode, pStream);
726✔
2528
  }
2529

2530
  if (code) {
726!
2531
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2532
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2533
  }
2534

2535
  return code;
726✔
2536
}
2537

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

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

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

2583
  return TSDB_CODE_SUCCESS;
×
2584
}
2585

2586
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
159✔
2587
  int32_t code = TSDB_CODE_SUCCESS;
159✔
2588
  int32_t lino = 0;
159✔
2589

2590
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
159!
2591

2592
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
159!
2593

2594
_exit:
159✔
2595

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

2602
  return code;
159✔
2603
}
2604

2605
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
5,331✔
2606
  int32_t code = TSDB_CODE_SUCCESS;
5,331✔
2607
  int32_t lino = 0;
5,331✔
2608

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

2615
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
5,331✔
2616
  if (NULL == pStream) {
5,331✔
2617
    mstsInfo("stream %s already not in streamMap", streamName);
302!
2618
    goto _exit;
302✔
2619
  }
2620

2621
  atomic_store_8(&pStream->stopped, 2);
5,029✔
2622

2623
  mstsInfo("set stream %s stopped by user", streamName);
5,029!
2624

2625
_exit:
×
2626

2627
  taosHashRelease(mStreamMgmt.streamMap, pStream);
5,331✔
2628

2629
  if (code) {
5,331!
2630
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2631
  }
2632

2633
  return;
5,331✔
2634
}
2635

2636
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
1,950✔
2637
  int32_t code = TSDB_CODE_SUCCESS;
1,950✔
2638
  int32_t lino = 0;
1,950✔
2639

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

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

2653
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
1,950!
2654

2655
_exit:
1,950✔
2656

2657
  taosHashRelease(mStreamMgmt.streamMap, pStream);
1,950✔
2658

2659
  if (code) {
1,950!
2660
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2661
  }
2662

2663
  return code;
1,950✔
2664
}
2665

2666
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
13,710✔
2667
  int32_t code = TSDB_CODE_SUCCESS;
13,710✔
2668
  int32_t lino = 0;
13,710✔
2669
  SStmQNode* pQNode = NULL;
13,710✔
2670

2671
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
71,217✔
2672
    switch (pQNode->type) {
57,507!
2673
      case STREAM_ACT_DEPLOY:
57,507✔
2674
        if (pQNode->streamAct) {
57,507✔
2675
          mstDebug("start to handle stream deploy action");
56,781✔
2676
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
56,781!
2677
        } else {
2678
          mstDebug("start to handle task deploy action");
726!
2679
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
726!
2680
        }
2681
        break;
57,507✔
2682
      default:
×
2683
        break;
×
2684
    }
2685
  }
2686

2687
_exit:
13,710✔
2688

2689
  if (code) {
13,710!
2690
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2691
  }
2692
}
13,710✔
2693

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

2705
    pStatus = (SStmStatus*)pIter;
×
2706

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

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

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

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

2722
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2723

2724
  msmStopAllStreamsByGrant(errCode);
×
2725

2726
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2727
  
2728
  return errCode;
×
2729
}
2730

2731
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
377,016✔
2732
  int32_t code = TSDB_CODE_SUCCESS;
377,016✔
2733
  int32_t lino = 0;
377,016✔
2734
  int64_t streamId = pDeploy->task.streamId;
377,016✔
2735
  
2736
  switch (pDeploy->task.type) {
377,016!
2737
    case STREAM_READER_TASK:
146,190✔
2738
      if (NULL == pStream->readerTasks) {
146,190✔
2739
        pStream->streamId = streamId;
68,135✔
2740
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
68,135✔
2741
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
68,135!
2742
      }
2743
      
2744
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
292,380!
2745
      break;
146,190✔
2746
    case STREAM_TRIGGER_TASK:
55,959✔
2747
      pStream->streamId = streamId;
55,959✔
2748
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
55,959!
2749
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
55,959!
2750
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
55,959!
2751
      break;
55,959✔
2752
    case STREAM_RUNNER_TASK:
174,867✔
2753
      if (NULL == pStream->runnerTasks) {
174,867✔
2754
        pStream->streamId = streamId;
57,386✔
2755
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
57,386✔
2756
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
57,386!
2757
      }      
2758
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
349,734!
2759
      break;
174,867✔
2760
    default:
×
2761
      break;
×
2762
  }
2763

2764
_exit:
377,016✔
2765

2766
  if (code) {
377,016!
2767
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2768
  }
2769

2770
  return code;
377,016✔
2771
}
2772

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

2790
      if (TSDB_CODE_DUP_KEY != code) {
×
2791
        goto _exit;
×
2792
      }    
2793

2794
      tFreeSStmStreamDeploy(&streamDeploy);
×
2795
      continue;
×
2796
    }
2797

2798
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
305,144!
2799
    
2800
    break;
305,144✔
2801
  }
2802
  
2803
_exit:
377,016✔
2804

2805
  taosHashRelease(pHash, pStream);
377,016✔
2806

2807
  if (code) {
377,016!
2808
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2809
  } else {
2810
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
377,016✔
2811
  }
2812

2813
  return code;
377,016✔
2814
}
2815

2816

2817
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
69,842✔
2818
  int32_t code = TSDB_CODE_SUCCESS;
69,842✔
2819
  int32_t lino = 0;
69,842✔
2820
  int32_t taskNum = taosArrayGetSize(pTasks);
69,842✔
2821

2822
  for (int32_t i = 0; i < taskNum; ++i) {
450,062✔
2823
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
380,220✔
2824
    if (pExt->deployed) {
380,220!
2825
      continue;
3,204✔
2826
    }
2827

2828
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
377,016!
2829
    pExt->deployed = true;
377,016✔
2830

2831
    (void)atomic_add_fetch_32(deployed, 1);
377,016✔
2832
  }
2833

2834
_exit:
69,842✔
2835

2836
  if (code) {
69,842!
2837
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2838
  }
2839

2840
  return code;
69,842✔
2841
}
2842

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

2850
  mstDebug("start to add stream vgroup tasks deploy");
21,049✔
2851
  
2852
  for (int32_t i = 0; i < vgNum; ++i) {
131,159✔
2853
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
110,110✔
2854

2855
    msmUpdateVgroupUpTs(pCtx, *vgId);
110,110✔
2856

2857
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
110,110✔
2858
    if (NULL == pVg) {
110,110✔
2859
      continue;
68,201✔
2860
    }
2861

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

2875
_exit:
21,049✔
2876

2877
  if (code) {
21,049!
2878
    if (pVg) {
×
2879
      taosRUnLockLatch(&pVg->lock);
×
2880
    }
2881

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

2885
  return code;
21,049✔
2886
}
2887

2888
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
15,237✔
2889
  int32_t code = TSDB_CODE_SUCCESS;
15,237✔
2890
  int32_t lino = 0;
15,237✔
2891
  SStmSnodeTasksDeploy* pSnode = NULL;
15,237✔
2892
  SStreamHbMsg* pReq = pCtx->pReq;
15,237✔
2893

2894
  mstDebug("start to add stream snode tasks deploy");
15,237✔
2895
  
2896
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
15,237✔
2897
  if (NULL == pSnode) {
15,237✔
2898
    return TSDB_CODE_SUCCESS;
605✔
2899
  }
2900

2901
  (void)mstWaitLock(&pSnode->lock, false);
14,632✔
2902
  
2903
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
14,632✔
2904
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
13,543!
2905
  }
2906

2907
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
14,632✔
2908
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
14,390!
2909
  }
2910
  
2911
  taosWUnLockLatch(&pSnode->lock);
14,632✔
2912

2913
_exit:
14,632✔
2914

2915
  if (code) {
14,632!
2916
    if (pSnode) {
×
2917
      taosWUnLockLatch(&pSnode->lock);
×
2918
    }
2919

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

2923
  return code;
14,632✔
2924
}
2925

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

2937
_exit:
163,749✔
2938

2939
  if (code) {
163,749!
2940
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2941
  }
2942

2943
  return code;
163,749✔
2944
}
2945

2946
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
18,024✔
2947
  int32_t code = TSDB_CODE_SUCCESS;
18,024✔
2948
  int32_t lino = 0;
18,024✔
2949
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
18,024✔
2950
  void* pIter = NULL;
18,024✔
2951

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

2957
  while (1) {
71,872✔
2958
    pIter = taosHashIterate(pCtx->deployStm, pIter);
89,896✔
2959
    if (pIter == NULL) {
89,896✔
2960
      break;
18,024✔
2961
    }
2962
    
2963
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
71,872✔
2964
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
143,744!
2965

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

2970
    mstClearSStmStreamDeploy(pDeploy);
71,872✔
2971
    
2972
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
71,872!
2973
  }
2974
  
2975
_exit:
18,024✔
2976

2977
  if (pIter) {
18,024!
2978
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2979
  }
2980

2981
  if (code) {
18,024!
2982
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2983
  }
2984
  
2985
  return code;
18,024✔
2986
}
2987

2988
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
21,049✔
2989
  int32_t code = TSDB_CODE_SUCCESS;
21,049✔
2990
  int32_t lino = 0;
21,049✔
2991
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
21,049✔
2992
  SStmVgTasksToDeploy* pVg = NULL;
21,049✔
2993
  
2994
  for (int32_t i = 0; i < vgNum; ++i) {
131,159✔
2995
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
110,110✔
2996
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
110,110✔
2997
    if (NULL == pVg) {
110,110✔
2998
      continue;
68,201✔
2999
    }
3000

3001
    if (taosWTryLockLatch(&pVg->lock)) {
41,909!
3002
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3003
      continue;
×
3004
    }
3005
    
3006
    if (atomic_load_32(&pVg->deployed) <= 0) {
41,909!
3007
      taosWUnLockLatch(&pVg->lock);
×
3008
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3009
      continue;
×
3010
    }
3011

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

3023
    for (int32_t m = taskNum - 1; m >= 0; --m) {
2,428✔
3024
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
2,136✔
3025
      if (!pExt->deployed) {
2,136!
3026
        continue;
×
3027
      }
3028

3029
      mstDestroySStmTaskToDeployExt(pExt);
2,136✔
3030

3031
      taosArrayRemove(pVg->taskList, m);
2,136✔
3032
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
2,136✔
3033
    }
3034
    atomic_store_32(&pVg->deployed, 0);
292✔
3035
    taosWUnLockLatch(&pVg->lock);
292✔
3036
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
292✔
3037
  }
3038

3039
_exit:
21,049✔
3040

3041
  if (code) {
21,049!
3042
    if (pVg) {
×
3043
      taosWUnLockLatch(&pVg->lock);
×
3044
    }
3045

3046
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3047
  }
3048
}
21,049✔
3049

3050
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
15,721✔
3051
  if (!GOT_SNODE(snodeId)) {
15,721✔
3052
    return;
484✔
3053
  }
3054
  
3055
  int32_t code = TSDB_CODE_SUCCESS;
15,237✔
3056
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
15,237✔
3057
  if (NULL == pSnode) {
15,237✔
3058
    return;
605✔
3059
  }
3060

3061
  if (taosWTryLockLatch(&pSnode->lock)) {
14,632!
3062
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3063
    return;
×
3064
  }
3065

3066
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
14,632✔
3067
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
14,632✔
3068
  
3069
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
14,632✔
3070
    taosWUnLockLatch(&pSnode->lock);
242✔
3071
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
242✔
3072
    return;
242✔
3073
  }
3074

3075
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
14,390✔
3076
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
14,098✔
3077
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
14,098✔
3078
    pSnode->triggerList = NULL;
14,098✔
3079
  }
3080

3081
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
14,390✔
3082
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
13,977✔
3083
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
13,977✔
3084
    pSnode->runnerList = NULL;
13,977✔
3085
  }
3086

3087
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
14,390✔
3088
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
13,977✔
3089
    taosWUnLockLatch(&pSnode->lock);
13,977✔
3090
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
13,977✔
3091
    return;
13,977✔
3092
  }
3093

3094
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
413!
3095
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
1,360✔
3096
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
1,068✔
3097
      if (!pExt->deployed) {
1,068!
3098
        continue;
×
3099
      }
3100

3101
      mstDestroySStmTaskToDeployExt(pExt);
1,068✔
3102
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
1,068✔
3103
      taosArrayRemove(pSnode->triggerList, m);
1,068✔
3104
    }
3105
    
3106
    pSnode->triggerDeployed = 0;
292✔
3107
  }
3108

3109
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
413!
3110
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
3,617✔
3111
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
3,204✔
3112
      if (!pExt->deployed) {
3,204!
3113
        continue;
×
3114
      }
3115

3116
      mstDestroySStmTaskToDeployExt(pExt);
3,204✔
3117
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
3,204✔
3118
      taosArrayRemove(pSnode->runnerList, m);
3,204✔
3119
    }
3120
    
3121
    pSnode->runnerDeployed = 0;
413✔
3122
  }
3123
  
3124
  taosWUnLockLatch(&pSnode->lock);
413✔
3125
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
413✔
3126
}
3127

3128
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
4,334,956✔
3129
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
4,334,956✔
3130
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
21,049✔
3131
  }
3132

3133
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
4,334,956✔
3134
    msmCleanDeployedSnodeTasks(pHb->snodeId);
15,721✔
3135
  }
3136
}
4,334,956✔
3137

3138
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
4,334,956✔
3139
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
4,334,956✔
3140
  if (mStreamMgmt.tCtx) {
4,334,956✔
3141
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
4,329,165✔
3142
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
4,329,165✔
3143
  }
3144
}
4,334,956✔
3145

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

3163
_exit:
70,977✔
3164

3165
  if (code) {
70,977!
3166
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3167
  }
3168

3169
  return code;
70,977✔
3170
}
3171

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

3188
_exit:
×
3189

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

3194
  return code;
×
3195
}
3196

3197

3198

3199
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
20,900✔
3200
  int32_t code = TSDB_CODE_SUCCESS;
20,900✔
3201
  int32_t lino = 0;
20,900✔
3202
  int32_t action = STREAM_ACT_UNDEPLOY;
20,900✔
3203
  bool    dropped = false;
20,900✔
3204

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

3216
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
26,970!
3217
    if (pAction->undeploy.doCheckpoint) {
13,485✔
3218
      pAction->undeploy.doCheckpoint = dropped ? false : true;
6,142!
3219
    }
3220
    if (!pAction->undeploy.doCleanup) {
13,485✔
3221
      pAction->undeploy.doCleanup = dropped ? true : false;
6,142!
3222
    }
3223
    
3224
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
13,485✔
3225
  } else {
3226
    SStmAction newAction = {0};
7,415✔
3227
    newAction.actions = action;
7,415✔
3228
    newAction.undeploy.doCheckpoint = dropped ? false : true;
7,415!
3229
    newAction.undeploy.doCleanup = dropped ? true : false;
7,415!
3230
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
7,415✔
3231
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
7,415!
3232
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
14,830!
3233
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
7,415!
3234
    
3235
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
7,415✔
3236
  }
3237

3238
_exit:
20,900✔
3239

3240
  if (code) {
20,900!
3241
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3242
  }
3243

3244
  return code;
20,900✔
3245
}
3246

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

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

3268
_exit:
1,764✔
3269

3270
  if (code) {
1,764!
3271
    mstDestroySStmAction(&newAction);
×
3272
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3273
  }
3274

3275
  return code;
1,764✔
3276
}
3277

3278
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
88,393✔
3279
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
88,393✔
3280
  if (NULL == pStream) {
88,393!
3281
    return false;
×
3282
  }
3283

3284
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
88,393!
3285
    return false;
×
3286
  }
3287

3288
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
88,393✔
3289
  for (int32_t i = 0; i < readerNum; ++i) {
195,900✔
3290
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
107,507✔
3291
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
107,507!
3292
      return false;
×
3293
    }
3294
  }
3295

3296
  readerNum = taosArrayGetSize(pStream->trigOReaders);
88,393✔
3297
  for (int32_t i = 0; i < readerNum; ++i) {
99,913✔
3298
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
11,520✔
3299
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
11,520!
3300
      return false;
×
3301
    }
3302
  }
3303

3304
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
88,393!
3305
  SListNode* pNode = listHead(pStream->calcReaders);
88,393✔
3306
  for (int32_t i = 0; i < readerNum; ++i) {
216,820✔
3307
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
128,427✔
3308
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
128,427!
3309
      return false;
×
3310
    }
3311
    pNode = TD_DLIST_NODE_NEXT(pNode);
128,427✔
3312
  }
3313

3314
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
298,006✔
3315
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
227,029✔
3316
    for (int32_t m = 0; m < runnerNum; ++m) {
445,522✔
3317
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
235,909✔
3318
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
235,909!
3319
        return false;
17,416✔
3320
      }
3321
    }
3322
  }
3323
  
3324
  return true;
70,977✔
3325
}
3326

3327

3328
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
2,502,845✔
3329
  int32_t code = TSDB_CODE_SUCCESS;
2,502,845✔
3330
  int32_t lino = 0;
2,502,845✔
3331
  int32_t action = 0;
2,502,845✔
3332
  int64_t streamId = pMsg->streamId;
2,502,845✔
3333
  SStreamTask* pTask = (SStreamTask*)pMsg;
2,502,845✔
3334
  int8_t  stopped = 0;
2,502,845✔
3335

3336
  msttDebug("start to handle task abnormal status %d", pTask->status);
2,502,845✔
3337
  
3338
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,502,845✔
3339
  if (NULL == pStatus) {
2,502,845!
3340
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3341
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3342
    return;
7,500✔
3343
  }
3344

3345
  stopped = atomic_load_8(&pStatus->stopped);
2,502,845✔
3346
  if (stopped) {
2,502,845!
3347
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3348
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3349
    return;
×
3350
  }
3351
  
3352
  switch (pMsg->status) {
2,502,845!
3353
    case STREAM_STATUS_INIT:      
2,500,972✔
3354
      if (STREAM_TRIGGER_TASK != pMsg->type) {
2,500,972✔
3355
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
2,364,894!
3356
        return;
2,364,894✔
3357
      }
3358
      
3359
      if (INT64_MIN == pStatus->lastActionTs) {
136,078!
3360
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3361
        return;
×
3362
      }
3363
      
3364
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
136,078✔
3365
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
47,685✔
3366
        return;
47,685✔
3367
      }
3368

3369
      if (STREAM_IS_RUNNING(pStatus)) {
88,393!
3370
        msttDebug("stream already running, ignore status: %s", gStreamStatusStr[pTask->status]);
×
3371
      } else if (GOT_SNODE(pCtx->pReq->snodeId) && msmCheckStreamStartCond(streamId, pCtx->pReq->snodeId)) {
88,393!
3372
        TAOS_CHECK_EXIT(msmGrpAddActionStart(pCtx->actionStm, streamId, &pStatus->triggerTask->id));
70,977!
3373
      }
3374
      break;
88,393✔
3375
    case STREAM_STATUS_FAILED:
1,873✔
3376
      //STREAMTODO ADD ERRCODE HANDLE
3377
      msttInfo("task failed with error:%s, try to undeploy current task, idx:%d", tstrerror(pMsg->errorCode), pMsg->taskIdx);
1,873!
3378
      TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
1,873!
3379
      break;
1,873✔
3380
    default:
×
3381
      break;
×
3382
  }
3383

3384
_exit:
90,266✔
3385

3386
  if (code) {
90,266!
3387
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3388
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3389
  }
3390
}
3391

3392
void msmHandleStatusUpdateErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
19,027✔
3393
  int32_t code = TSDB_CODE_SUCCESS;
19,027✔
3394
  int32_t lino = 0;
19,027✔
3395
  SStreamTask* pTask = (SStreamTask*)pStatus;
19,027✔
3396
  int64_t streamId = pStatus->streamId;
19,027✔
3397

3398
  msttInfo("start to handle task status update exception, type: %d", err);
19,027!
3399
  
3400
  // STREAMTODO
3401

3402
  if (STM_ERR_TASK_NOT_EXISTS == err || STM_ERR_STREAM_STOPPED == err) {
19,027!
3403
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
19,027!
3404
  }
3405

3406
_exit:
19,027✔
3407

3408
  if (code) {
19,027!
3409
    // IGNORE STOP STREAM BY ERROR  
3410
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3411
  }
3412
}
19,027✔
3413

3414
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
899,841✔
3415
  int32_t code = TSDB_CODE_SUCCESS;
899,841✔
3416
  int32_t lino = 0;
899,841✔
3417
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
899,841✔
3418

3419
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
899,841!
3420
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3421
  }
3422
  
3423
  SArray* userRecalcList = NULL;
899,841✔
3424
  if (atomic_load_ptr(&pStream->userRecalcList)) {
899,841✔
3425
    taosWLockLatch(&pStream->userRecalcLock);
1,764✔
3426
    if (pStream->userRecalcList) {
1,764!
3427
      userRecalcList = pStream->userRecalcList;
1,764✔
3428
      pStream->userRecalcList = NULL;
1,764✔
3429
    }
3430
    taosWUnLockLatch(&pStream->userRecalcLock);
1,764✔
3431
    
3432
    if (userRecalcList) {
1,764!
3433
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
1,764!
3434
    }
3435
  }
3436

3437
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
899,841!
3438
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
446,129✔
3439
    if (NULL == pStatus->detailStatus) {
446,129✔
3440
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
55,062!
3441
      if (NULL == pStatus->detailStatus) {
55,062!
3442
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3443
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3444
      }
3445
    }
3446
    
3447
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
446,129!
3448
    taosWUnLockLatch(&pStatus->detailStatusLock);
446,129✔
3449
  }
3450

3451
_exit:
453,712✔
3452

3453
  if (code) {
899,841!
3454
    // IGNORE STOP STREAM BY ERROR
3455
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3456
  }
3457
}
899,841✔
3458

3459
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
344,750✔
3460
  int32_t code = TSDB_CODE_SUCCESS;
344,750✔
3461
  int32_t lino = 0;
344,750✔
3462
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
344,750✔
3463

3464
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
344,750✔
3465

3466
  for (int32_t i = 0; i < num; ++i) {
6,260,254✔
3467
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
5,915,504✔
3468
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
5,915,504✔
3469
    
3470
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
5,915,504✔
3471
    if (NULL == ppStatus) {
5,915,504✔
3472
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
4,377!
3473
      msmHandleStatusUpdateErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
4,377✔
3474
      continue;
4,377✔
3475
    }
3476

3477
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
5,911,127✔
3478
    int8_t stopped = atomic_load_8(&pStream->stopped);
5,911,127✔
3479
    if (stopped) {
5,911,127✔
3480
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
14,650!
3481
      msmHandleStatusUpdateErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
14,650✔
3482
      continue;
14,650✔
3483
    }
3484

3485
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
5,896,477!
3486
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3487
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3488
          
3489
      msmHandleStatusUpdateErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
3490
      continue;
×
3491
    }
3492

3493
    if ((*ppStatus)->status != pTask->status) {
5,896,477✔
3494
      if (STREAM_STATUS_RUNNING == pTask->status) {
657,864✔
3495
        (*ppStatus)->runningStartTs = pCtx->currTs;
283,202✔
3496
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
374,662!
3497
        if (pStream->triggerTask) {
×
3498
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3499
        }
3500
        
3501
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3502
      }
3503
    }
3504
    
3505
    (*ppStatus)->errCode = pTask->errorCode;
5,896,477✔
3506
    (*ppStatus)->status = pTask->status;
5,896,477✔
3507
    (*ppStatus)->lastUpTs = pCtx->currTs;
5,896,477✔
3508
    
3509
    if (STREAM_STATUS_RUNNING != pTask->status) {
5,896,477✔
3510
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
2,502,845✔
3511
    }
3512
    
3513
    if (STREAM_TRIGGER_TASK == pTask->type) {
5,896,477✔
3514
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
899,841✔
3515
    }
3516
  }
3517

3518
_exit:
344,750✔
3519

3520
  if (code) {
344,750!
3521
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3522
  }
3523

3524
  return code;
344,750✔
3525
}
3526

3527
int32_t msmWatchRecordNewTask(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
×
3528
  int32_t code = TSDB_CODE_SUCCESS;
×
3529
  int32_t lino = 0;
×
3530
  int64_t streamId = pTask->streamId;
×
3531
  SStreamObj* pStream = NULL;
×
3532

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

3545
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &status, pStream, true));
×
3546
    mndReleaseStream(pCtx->pMnode, pStream);
×
3547

3548
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &status, sizeof(status)));
×
3549
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3550
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
×
3551
    msttDebug("stream added to streamMap cause of new task status:%s", gStreamStatusStr[pTask->status]);
×
3552
  }
3553

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

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

3613
      TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pNewTask));
×
3614
      TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pNewTask, pTask->deployId));
×
3615
      break;
×
3616
    }
3617
    default: {
×
3618
      msttError("invalid task type:%d in task status", pTask->type);
×
3619
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
3620
      break;
×
3621
    }
3622
  }
3623

3624
_exit:
×
3625

3626
  if (code) {
×
3627
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3628
  } else {
3629
    msttDebug("new task recored to taskMap/streamMap, task status:%s", gStreamStatusStr[pTask->status]);
×
3630
  }
3631

3632
  return code;
×
3633
}
3634

3635
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3636
  int32_t code = TSDB_CODE_SUCCESS;
×
3637
  int32_t lino = 0;
×
3638
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3639

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

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

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

3663
_exit:
×
3664

3665
  if (code) {
×
3666
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3667
  }
3668

3669
  return code;
×
3670
}
3671

3672
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
70,977✔
3673
  int32_t code = TSDB_CODE_SUCCESS;
70,977✔
3674
  int32_t lino = 0;
70,977✔
3675
  if (NULL == pCtx->pRsp->start.taskList) {
70,977✔
3676
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
29,760✔
3677
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
29,760!
3678
  }
3679

3680
  SStmTaskId* pId = &pAction->start.triggerId;
70,977✔
3681
  SStreamTaskStart start = {0};
70,977✔
3682
  start.task.type = STREAM_TRIGGER_TASK;
70,977✔
3683
  start.task.streamId = streamId;
70,977✔
3684
  start.task.taskId = pId->taskId;
70,977✔
3685
  start.task.seriousId = pId->seriousId;
70,977✔
3686
  start.task.nodeId = pId->nodeId;
70,977✔
3687
  start.task.taskIdx = pId->taskIdx;
70,977✔
3688

3689
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
141,954!
3690
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
70,977!
3691

3692
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
70,977✔
3693

3694
  return;
70,977✔
3695

3696
_exit:
×
3697

3698
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3699
}
3700

3701

3702
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
7,415✔
3703
  int32_t code = TSDB_CODE_SUCCESS;
7,415✔
3704
  int32_t lino = 0;
7,415✔
3705
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
7,415✔
3706
  if (NULL == pCtx->pRsp->undeploy.taskList) {
7,415✔
3707
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
6,288✔
3708
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
6,288!
3709
  }
3710

3711
  SStreamTaskUndeploy undeploy;
7,415✔
3712
  for (int32_t i = 0; i < dropNum; ++i) {
28,315✔
3713
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
20,900✔
3714
    undeploy.task = *pTask;
20,900✔
3715
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
20,900✔
3716
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
20,900✔
3717

3718
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
41,800!
3719
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
20,900!
3720

3721
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
20,900✔
3722
  }
3723

3724
  return;
7,415✔
3725

3726
_exit:
×
3727

3728
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3729
}
3730

3731
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3732
  int32_t code = TSDB_CODE_SUCCESS;
×
3733
  int32_t lino = 0;
×
3734

3735
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
3736
  if (NULL == pStream) {
×
3737
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3738
    return;
×
3739
  }
3740

3741
  if (NULL == pStream->triggerTask) {
×
3742
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3743
    return;
×
3744
  }
3745

3746
  SStreamMgmtRsp rsp = {0};
×
3747
  rsp.reqId = INT64_MIN;
×
3748
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3749
  rsp.task.streamId = streamId;
×
3750
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3751

3752
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3753

3754
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3755
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3756
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3757
  }
3758

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

3761
_exit:
×
3762

3763
  if (code) {
×
3764
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3765
  } else {
3766
    mstsDebug("trigger update rsp added, runnerNum:%d", (int32_t)taosArrayGetSize(rsp.cont.runnerList));
×
3767
  }
3768
}
3769

3770
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
1,764✔
3771
  int32_t code = TSDB_CODE_SUCCESS;
1,764✔
3772
  int32_t lino = 0;
1,764✔
3773

3774
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
1,764✔
3775
  if (NULL == pStream) {
1,764!
3776
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3777
    return;
×
3778
  }
3779

3780
  if (NULL == pStream->triggerTask) {
1,764!
3781
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3782
    return;
×
3783
  }
3784

3785
  SStreamMgmtRsp rsp = {0};
1,764✔
3786
  rsp.reqId = INT64_MIN;
1,764✔
3787
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
1,764✔
3788
  rsp.task.streamId = streamId;
1,764✔
3789
  rsp.task.taskId = pStream->triggerTask->id.taskId;
1,764✔
3790
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
1,764✔
3791

3792
  if (NULL == pCtx->pRsp->rsps.rspList) {
1,764!
3793
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
1,764✔
3794
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
1,764!
3795
  }
3796

3797
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
3,528!
3798

3799
_exit:
1,764✔
3800

3801
  if (code) {
1,764!
3802
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3803
  } else {
3804
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
1,764!
3805
  }
3806
}
3807

3808

3809
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
37,812✔
3810
  int32_t code = TSDB_CODE_SUCCESS;
37,812✔
3811
  int32_t lino = 0;
37,812✔
3812
  void* pIter = NULL;
37,812✔
3813
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
37,812✔
3814

3815
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
37,812✔
3816

3817
  while (1) {
80,156✔
3818
    pIter = taosHashIterate(pCtx->actionStm, pIter);
117,968✔
3819
    if (pIter == NULL) {
117,968✔
3820
      break;
37,812✔
3821
    }
3822

3823
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
80,156✔
3824
    SStmAction *pAction = (SStmAction *)pIter;
80,156✔
3825
    
3826
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
80,156✔
3827
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
7,415✔
3828
      continue;
7,415✔
3829
    }
3830

3831
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
72,741!
3832
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3833
    }
3834

3835
    if (STREAM_ACT_RECALC & pAction->actions) {
72,741✔
3836
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
1,764✔
3837
    }
3838

3839
    if (STREAM_ACT_START & pAction->actions) {
72,741✔
3840
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
70,977✔
3841
    }
3842
  }
3843
  
3844
_exit:
37,812✔
3845

3846
  if (pIter) {
37,812!
3847
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3848
  }
3849

3850
  if (code) {
37,812!
3851
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3852
  }
3853

3854
  return code;
37,812✔
3855
}
3856

3857
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
4,329,165✔
3858
  int32_t  code = TSDB_CODE_SUCCESS;
4,329,165✔
3859
  int32_t  lino = 0;
4,329,165✔
3860
  int64_t* lastTs = NULL;
4,329,165✔
3861
  bool     noExists = false;
4,329,165✔
3862

3863
  while (true) {
3864
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
4,552,414✔
3865
    if (NULL == lastTs) {
4,552,414✔
3866
      if (noExists) {
316,975✔
3867
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
93,726!
3868
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
93,726!
3869
      }
3870

3871
      noExists = true;
223,249✔
3872
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
223,249!
3873
      
3874
      continue;
223,249✔
3875
    }
3876

3877
    while (true) {
×
3878
      int64_t lastTsValue = atomic_load_64(lastTs);
4,235,439✔
3879
      if (pCtx->currTs > lastTsValue) {
4,235,439!
3880
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
4,235,439!
3881
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
4,235,439✔
3882
          return code;
4,235,439✔
3883
        }
3884

3885
        continue;
×
3886
      }
3887

3888
      return code;
×
3889
    }
3890

3891
    break;
3892
  }
3893

3894
_exit:
93,726✔
3895

3896
  if (code) {
93,726!
3897
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
93,726!
3898
  }
3899

3900
  return code;  
93,726✔
3901
}
3902

3903
void msmWatchCheckStreamMap(SStmGrpCtx* pCtx) {
×
3904
  SStmStatus* pStatus = NULL;
×
3905
  int32_t trigReaderNum = 0;
×
3906
  int32_t calcReaderNum = 0;
×
3907
  int32_t runnerNum = 0;
×
3908
  int64_t streamId = 0;
×
3909
  void* pIter = NULL;
×
3910
  while (true) {
3911
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
×
3912
    if (NULL == pIter) {
×
3913
      return;
×
3914
    }
3915

3916
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3917
    pStatus = (SStmStatus*)pIter;
×
3918

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

3932
    calcReaderNum = MST_LIST_SIZE(pStatus->calcReaders);
×
3933
    if (pStatus->calcReaderNum != calcReaderNum) {
×
3934
      mstsWarn("calcReaderNum %d mis-match with expected %d", calcReaderNum, pStatus->calcReaderNum);
×
3935
      msmStopStreamByError(streamId, pStatus, TSDB_CODE_MND_STREAM_TASK_LOST, pCtx->currTs);
×
3936
      continue;
×
3937
    }
3938

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

3950
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
30✔
3951
  int32_t code = TSDB_CODE_SUCCESS;
30✔
3952
  int32_t lino = 0;
30✔
3953
  int32_t minVal = watchError ? 0 : 1;
30✔
3954

3955
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
30!
3956
    return code;
×
3957
  }
3958

3959
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
30!
3960
    (void)sched_yield();
×
3961
  }
3962

3963
  if (watchError) {
30!
3964
    taosHashClear(mStreamMgmt.vgroupMap);
×
3965
    taosHashClear(mStreamMgmt.snodeMap);
×
3966
    taosHashClear(mStreamMgmt.taskMap);
×
3967
    taosHashClear(mStreamMgmt.streamMap);
×
3968
    mstInfo("watch error happends, clear all maps");
×
3969
    goto _exit;
×
3970
  }
3971

3972
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
30!
3973
    mstInfo("no stream tasks remain during watch state");
30!
3974
    goto _exit;
30✔
3975
  }
3976

3977
  msmWatchCheckStreamMap(pCtx);
×
3978

3979
_exit:
30✔
3980

3981
  mStreamMgmt.lastTaskId += 100000;
30✔
3982

3983
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
30!
3984

3985
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
30✔
3986

3987
  if (code) {
30!
3988
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3989
  }
3990

3991
  return code;
30✔
3992
}
3993

3994

3995
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
120✔
3996
  int32_t code = TSDB_CODE_SUCCESS;
120✔
3997
  int32_t lino = 0;
120✔
3998
  SStreamHbMsg* pReq = pCtx->pReq;
120✔
3999

4000
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
120✔
4001
  
4002
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
120!
4003
    goto _exit;
×
4004
  }
4005

4006
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
120!
4007
  if (GOT_SNODE(pReq->snodeId)) {
120!
4008
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
120!
4009
  }
4010

4011
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
120!
4012
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4013
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4014
  }
4015

4016
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
120✔
4017
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
30!
4018
  }
4019

4020
_exit:
120✔
4021

4022
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
120✔
4023
  
4024
  if (code) {
120!
4025
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4026

4027
    (void)msmWatchHandleEnding(pCtx, true);
×
4028
  }
4029

4030
  return code;
120✔
4031
}
4032

4033
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
17,250✔
4034
  int32_t code = TSDB_CODE_SUCCESS;
17,250✔
4035
  int32_t lino = 0;
17,250✔
4036
  bool    readerExists = false;
17,250✔
4037
  int64_t streamId = pTask->streamId;
17,250✔
4038

4039
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
17,250✔
4040
  for (int32_t i = 0; i < readerNum; ++i) {
31,440✔
4041
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
19,920✔
4042
    if (pReader->id.nodeId == vgId) {
19,920✔
4043
      readerExists = true;
5,730✔
4044
      break;
5,730✔
4045
    }
4046
  }
4047

4048
  if (!readerExists) {
17,250✔
4049
    if (NULL == pStatus->trigOReaders) {
11,520✔
4050
      pStatus->trigOReaders = taosArrayInit(vgNum, sizeof(SStmTaskStatus));
10,185✔
4051
      TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
10,185!
4052
    }
4053
    
4054
    SStmTaskStatus* pState = taosArrayReserve(pStatus->trigOReaders, 1);
11,520✔
4055
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
11,520!
4056
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
11,520!
4057
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
11,520!
4058
  }
4059

4060
_exit:
17,250✔
4061

4062
  if (code) {
17,250!
4063
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4064
  }
4065

4066
  return code;
17,250✔
4067
}
4068

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

4090
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
15,915✔
4091
  if (NULL == pStatus) {
15,915!
4092
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4093
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4094
  }
4095

4096
  stopped = atomic_load_8(&pStatus->stopped);
15,915✔
4097
  if (stopped) {
15,915!
4098
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4099
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4100
  }
4101

4102
  if (tbNum <= 0) {
15,915!
4103
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4104
    goto _exit;
×
4105
  }
4106

4107
  int32_t oReaderNum = taosArrayGetSize(pStatus->trigOReaders);
15,915✔
4108
  if (oReaderNum > 0) {
15,915!
4109
    mstsWarn("origReaders already exits, num:%d", oReaderNum);
×
4110
    goto _exit;
×
4111
  }
4112

4113
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
15,915!
4114
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
15,915✔
4115
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
15,915!
4116

4117
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
15,915✔
4118
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
15,915!
4119
  
4120
  for (int32_t i = 0; i < tbNum; ++i) {
56,448✔
4121
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
40,533✔
4122
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
40,533!
4123
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
81,066!
4124
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
40,533!
4125
  }
4126

4127
  int32_t vgNum = tSimpleHashGetSize(pVgs);
15,915✔
4128
  while (true) {
4129
    p = tSimpleHashIterate(pVgs, p, &iter);
33,165✔
4130
    if (NULL == p) {
33,165✔
4131
      break;
15,915✔
4132
    }
4133
    
4134
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
17,250!
4135
  }
4136
  
4137
  vgNum = taosArrayGetSize(pStatus->trigOReaders);
15,915✔
4138
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
15,915✔
4139
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
15,915!
4140

4141
  SStreamTaskAddr addr;
15,915✔
4142
  for (int32_t i = 0; i < vgNum; ++i) {
27,435✔
4143
    SStmTaskStatus* pOTask = taosArrayGet(pStatus->trigOReaders, i);
11,520✔
4144
    addr.taskId = pOTask->id.taskId;
11,520✔
4145
    addr.nodeId = pOTask->id.nodeId;
11,520✔
4146
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
11,520✔
4147
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
23,040!
4148
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
11,520!
4149
  }
4150

4151
  if (NULL == pCtx->pRsp->rsps.rspList) {
15,915!
4152
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4153
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
4154
  }
4155

4156
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
31,830!
4157

4158
_exit:
15,915✔
4159

4160
  tFreeSStreamMgmtReq(pMgmtReq);
15,915✔
4161
  taosMemoryFree(pMgmtReq);
15,915!
4162

4163
  tSimpleHashCleanup(pVgs);
15,915✔
4164

4165
  if (code) {
15,915!
4166
    tFreeSStreamMgmtRsp(&rsp);
×
4167
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4168
  }
4169

4170
  mstDestroyDbVgroupsHash(pDbVgroups);
15,915✔
4171

4172
  return code;
15,915✔
4173
}
4174

4175
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
1,628✔
4176
  int32_t num = taosArrayGetSize(pList);
1,628✔
4177
  SStreamCalcScan* pScan = NULL;
1,628✔
4178
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
1,628✔
4179
  int64_t planUid = 0;
1,628✔
4180
  for (int32_t i = 0; i < num; ++i) {
1,628!
4181
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
1,628✔
4182
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
1,628!
4183
    if (0 != planUid && planUid == uid) {
1,628!
4184
      *ppRes = pScan;
1,628✔
4185
      break;
1,628✔
4186
    }
4187
  }
4188

4189
_exit:
1,628✔
4190

4191
  if (code) {
1,628!
4192
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4193
  }
4194

4195
  return code;
1,628✔
4196
}
4197

4198
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,960✔
4199
  int32_t code = TSDB_CODE_SUCCESS;
2,960✔
4200
  int32_t lino = 0;
2,960✔
4201
  bool    readerExists = false;
2,960✔
4202
  int64_t streamId = pTask->streamId;
2,960✔
4203
  SListNode* pNode = listHead(pStatus->calcReaders);
2,960✔
4204
  SStmTaskStatus* pReader = NULL;
2,960✔
4205
  int32_t taskIdx = 0;
2,960✔
4206

4207
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,960!
4208
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
26,640✔
4209
    pReader = (SStmTaskStatus*)pNode->data;
25,012✔
4210
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
25,012✔
4211
      readerExists = true;
1,332✔
4212
      pAddr->taskId = pReader->id.taskId;
1,332✔
4213
      break;
1,332✔
4214
    }
4215
  }
4216

4217
  if (!readerExists) {
2,960✔
4218
    if (NULL == pStatus->calcReaders) {
1,628!
4219
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4220
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4221
      taskIdx = 0;
×
4222
    } else {
4223
      pNode = listTail(pStatus->calcReaders);
1,628✔
4224
      pReader = (SStmTaskStatus*)pNode->data;
1,628✔
4225
      taskIdx = pReader->id.taskIdx + 1;
1,628✔
4226
    }
4227

4228
    SStreamCalcScan* pScan = NULL;
1,628✔
4229
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
1,628!
4230
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
1,628!
4231
    pReader = tdListReserve(pStatus->calcReaders);
1,628✔
4232
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
1,628!
4233
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
1,628!
4234
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
1,628!
4235
    pAddr->taskId = pReader->id.taskId;
1,628✔
4236
  }
4237

4238
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,960✔
4239
  pAddr->nodeId = vgId;
2,960✔
4240

4241
_exit:
2,960✔
4242

4243
  if (code) {
2,960!
4244
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4245
  }
4246

4247
  return code;
2,960✔
4248
}
4249

4250

4251
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
2,220✔
4252
  int32_t code = TSDB_CODE_SUCCESS;
2,220✔
4253
  int32_t lino = 0;
2,220✔
4254
  int32_t vgId = 0;
2,220✔
4255
  int64_t streamId = pTask->streamId;
2,220✔
4256
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
2,220✔
4257
  int32_t reqNum = taosArrayGetSize(pReqs);
2,220✔
4258
  SStreamOReaderDeployReq* pReq = NULL;
2,220✔
4259
  SStreamOReaderDeployRsp* pRsp = NULL;
2,220✔
4260
  SStreamMgmtRsp rsp = {0};
2,220✔
4261
  rsp.reqId = pTask->pMgmtReq->reqId;
2,220✔
4262
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
2,220✔
4263
  SStreamMgmtReq* pMgmtReq = NULL;
2,220✔
4264
  int8_t stopped = 0;
2,220✔
4265
  int32_t vgNum = 0;
2,220✔
4266
  SStreamTaskAddr* pAddr = NULL;
2,220✔
4267
  
4268
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,220✔
4269
  rsp.task = *(SStreamTask*)pTask;
2,220✔
4270

4271
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,220✔
4272
  if (NULL == pStatus) {
2,220!
4273
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4274
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4275
  }
4276

4277
  stopped = atomic_load_8(&pStatus->stopped);
2,220✔
4278
  if (stopped) {
2,220!
4279
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4280
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4281
  }
4282

4283
  if (reqNum <= 0) {
2,220!
4284
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4285
    goto _exit;
×
4286
  }
4287

4288
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,220✔
4289
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,220!
4290

4291
  for (int32_t i = 0; i < reqNum; ++i) {
4,440✔
4292
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
2,220✔
4293
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
2,220✔
4294
    pRsp->execId = pReq->execId;
2,220✔
4295
    vgNum = taosArrayGetSize(pReq->vgIds);
2,220✔
4296
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
2,220✔
4297
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
2,220!
4298
    
4299
    for (int32_t n = 0; n < vgNum; ++n) {
5,180✔
4300
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
2,960✔
4301
      pAddr = taosArrayGet(pRsp->vgList, n);
2,960✔
4302
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
2,960!
4303
    }
4304
  }
4305

4306
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,220!
4307
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4308
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
4309
  }
4310

4311
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
4,440!
4312

4313
_exit:
2,220✔
4314

4315
  tFreeSStreamMgmtReq(pMgmtReq);
2,220✔
4316
  taosMemoryFree(pMgmtReq);
2,220!
4317

4318
  if (code) {
2,220!
4319
    tFreeSStreamMgmtRsp(&rsp);
×
4320
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4321
  }
4322

4323
  return code;
2,220✔
4324
}
4325

4326

4327
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
18,135✔
4328
  int32_t code = TSDB_CODE_SUCCESS;
18,135✔
4329
  int32_t lino = 0;
18,135✔
4330

4331
  switch (pTask->pMgmtReq->type) {
18,135!
4332
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
15,915✔
4333
      TAOS_CHECK_EXIT(msmDeployTriggerOrigReader(pCtx, pTask));
15,915!
4334
      break;
15,915✔
4335
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,220✔
4336
      TAOS_CHECK_EXIT(msmDeployRunnerOrigReader(pCtx, pTask));
2,220!
4337
      break;
2,220✔
4338
    default:
×
4339
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4340
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4341
      break;
×
4342
  }
4343

4344
_exit:
18,135✔
4345

4346
  if (code) {
18,135!
4347
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4348
  }
4349

4350
  return code;
18,135✔
4351
}
4352

4353
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
6,693✔
4354
  int32_t code = TSDB_CODE_SUCCESS;
6,693✔
4355
  int32_t lino = 0;
6,693✔
4356
  SStreamHbMsg* pReq = pCtx->pReq;
6,693✔
4357
  SStmTaskStatusMsg* pTask = NULL;
6,693✔
4358
  
4359
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
6,693✔
4360
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
6,693!
4361
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
6,693✔
4362
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
6,693!
4363
  }
4364
  
4365
  for (int32_t i = 0; i < reqNum; ++i) {
24,828✔
4366
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
18,135✔
4367
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
18,135✔
4368
    if (NULL == pTask) {
18,135!
4369
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4370
      continue;
×
4371
    }
4372

4373
    if (NULL == pTask->pMgmtReq) {
18,135!
4374
      msttError("idx %d without mgmtReq", idx);
×
4375
      continue;
×
4376
    }
4377

4378
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
18,135!
4379
  }
4380

4381
_exit:
6,693✔
4382

4383
  if (code) {
6,693!
4384
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4385
  }
4386

4387
  return code;
6,693✔
4388
}
4389

4390
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
4,329,045✔
4391
  int32_t code = TSDB_CODE_SUCCESS;
4,329,045✔
4392
  int32_t lino = 0;
4,329,045✔
4393
  SStreamHbMsg* pReq = pCtx->pReq;
4,329,045✔
4394

4395
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
4,329,045✔
4396
  if (GOT_SNODE(pReq->snodeId)) {
4,235,319✔
4397
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
371,175!
4398
  }
4399
  
4400
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
4,235,319!
4401
    msmHandleStreamActions(pCtx);
13,710✔
4402
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
13,710✔
4403
  }
4404

4405
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
4,235,319!
4406
    code = msmHandleStreamRequests(pCtx);
6,693✔
4407
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
6,693✔
4408
    TAOS_CHECK_EXIT(code);
6,693!
4409
  }
4410

4411
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
4,235,319✔
4412
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
21,049!
4413
  } else {
4414
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
4,214,270!
4415
  }
4416

4417
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
4,235,319✔
4418
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
15,237!
4419
  }
4420

4421
  if (taosHashGetSize(pCtx->deployStm) > 0) {
4,235,319✔
4422
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
18,024!
4423
  }
4424

4425
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
4,235,319✔
4426
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
344,750!
4427
  }
4428

4429
  if (taosHashGetSize(pCtx->actionStm) > 0) {
4,235,319✔
4430
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
37,812!
4431
  }
4432

4433
_exit:
4,235,319✔
4434

4435
  if (code) {
4,329,045✔
4436
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
93,726!
4437
  }
4438

4439
  return code;
4,329,045✔
4440
}
4441

4442
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
4,334,956✔
4443
  int32_t lino = 0;
4,334,956✔
4444
  int32_t tlen = 0;
4,334,956✔
4445
  void   *buf = NULL;
4,334,956✔
4446

4447
  if (TSDB_CODE_SUCCESS != code) {
4,334,956✔
4448
    goto _exit;
93,726✔
4449
  }
4450

4451
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
4,241,230!
4452
  if (code < 0) {
4,241,230!
4453
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4454
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4455
  }
4456

4457
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
4,241,230✔
4458
  if (buf == NULL) {
4,241,230!
4459
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4460
    TAOS_CHECK_EXIT(terrno);    
×
4461
  }
4462

4463
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
4,241,230✔
4464
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
4,241,230✔
4465

4466
  SEncoder encoder;
4,238,957✔
4467
  tEncoderInit(&encoder, abuf, tlen);
4,241,230✔
4468
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
4,241,230!
4469
    rpcFreeCont(buf);
×
4470
    buf = NULL;
×
4471
    tEncoderClear(&encoder);
×
4472
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4473
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4474
  }
4475
  tEncoderClear(&encoder);
4,241,230✔
4476

4477
_exit:
4,334,956✔
4478

4479
  pMsg->code = code;
4,334,956✔
4480
  pMsg->info = *pRpcInfo;
4,334,956✔
4481
  if (TSDB_CODE_SUCCESS == code) {
4,334,956✔
4482
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
4,241,230✔
4483
    pMsg->pCont = buf;
4,241,230✔
4484
  }
4485
}
4,334,956✔
4486

4487

4488
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
4,334,956✔
4489
  int32_t code = TSDB_CODE_SUCCESS;
4,334,956✔
4490
  SMStreamHbRspMsg rsp = {0};
4,334,956✔
4491
  rsp.streamGId = pHb->streamGId;
4,334,956✔
4492

4493
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
4,334,956✔
4494

4495
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
4,334,956✔
4496
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
5,791!
4497
    goto _exit;
5,791✔
4498
  }
4499

4500
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
4,329,165✔
4501
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
4,329,165✔
4502

4503
  pCtx->tidx = tidx;
4,329,165✔
4504
  pCtx->pMnode = pMnode;
4,329,165✔
4505
  pCtx->currTs = currTs;
4,329,165✔
4506
  pCtx->pReq = pHb;
4,329,165✔
4507
  pCtx->pRsp = &rsp;
4,329,165✔
4508
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
4,329,165✔
4509
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
4,329,165✔
4510
  
4511
  switch (atomic_load_8(&mStreamMgmt.state)) {
4,329,165!
4512
    case MND_STM_STATE_WATCH:
120✔
4513
      code = msmWatchHandleHbMsg(pCtx);
120✔
4514
      break;
120✔
4515
    case MND_STM_STATE_NORMAL:
4,329,045✔
4516
      code = msmNormalHandleHbMsg(pCtx);
4,329,045✔
4517
      break;
4,329,045✔
4518
    default:
×
4519
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4520
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4521
      break;
×
4522
  }
4523

4524
_exit:
4,334,956✔
4525

4526
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
4,334,956✔
4527

4528
  msmCleanStreamGrpCtx(pHb);
4,334,956✔
4529
  msmClearStreamToDeployMaps(pHb);
4,334,956✔
4530

4531
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
4,334,956✔
4532
  
4533
  tFreeSMStreamHbRspMsg(&rsp);
4,334,956✔
4534

4535
  return code;
4,334,956✔
4536
}
4537

4538
void msmHandleBecomeLeader(SMnode *pMnode) {
158,070✔
4539
  if (tsDisableStream) {
158,070!
4540
    return;
×
4541
  }
4542

4543
  mstInfo("start to process mnode become leader");
158,070!
4544

4545
  int32_t code = 0;
158,070✔
4546
  streamAddVnodeLeader(MNODE_HANDLE);
158,070✔
4547
  
4548
  taosWLockLatch(&mStreamMgmt.runtimeLock);
158,070✔
4549
  msmDestroyRuntimeInfo(pMnode);
158,070✔
4550
  code = msmInitRuntimeInfo(pMnode);
158,070✔
4551
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
158,070✔
4552

4553
  if (TSDB_CODE_SUCCESS == code) {
158,070!
4554
    atomic_store_8(&mStreamMgmt.active, 1);
158,070✔
4555
  }
4556

4557
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
158,070!
4558
}
4559

4560
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
174,785✔
4561
  if (tsDisableStream) {
174,785!
4562
    return;
×
4563
  }
4564

4565
  mstInfo("start to process mnode become not leader");
174,785!
4566

4567
  streamRemoveVnodeLeader(MNODE_HANDLE);
174,785✔
4568

4569
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
174,785✔
4570
    taosWLockLatch(&mStreamMgmt.runtimeLock);
158,070✔
4571
    msmDestroyRuntimeInfo(pMnode);
158,070✔
4572
    mStreamMgmt.stat.inactiveTimes++;
158,070✔
4573
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
158,070✔
4574
  }
4575
}
4576

4577

4578
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
4579
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4580
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
4581
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4582
  } else {
4583
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4584
  }
4585
}
×
4586

4587
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
847✔
4588
  int32_t code = TSDB_CODE_SUCCESS;
847✔
4589
  int32_t lino = 0;
847✔
4590
  SStreamObj* pStream = pObj;
847✔
4591
  SSnodeObj* pSnode = p1;
847✔
4592
  SArray** ppRes = p2;
847✔
4593

4594
  if (pStream->mainSnodeId == pSnode->id) {
847✔
4595
    if (NULL == *ppRes) {
484✔
4596
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
242✔
4597
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
242✔
4598
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
242!
4599
    }
4600

4601
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
968!
4602
  }
4603

4604
  return true;
847✔
4605

4606
_exit:
×
4607

4608
  if (code) {
×
4609
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4610
  }  
4611

4612
  *(int32_t*)p3 = code;
×
4613

4614
  return false;
×
4615
}
4616

4617

4618
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
26,570✔
4619
  int32_t code = TSDB_CODE_SUCCESS;
26,570✔
4620
  int32_t lino = 0;
26,570✔
4621
  
4622
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
26,570✔
4623
  TAOS_CHECK_EXIT(code);
26,570!
4624

4625
  int32_t streamNum = taosArrayGetSize(*ppRes);
26,570✔
4626
  if (streamNum > 0 && 0 == pSnode->replicaId) {
26,570!
4627
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
×
4628
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
×
4629
  }
4630

4631
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4632

4633
_exit:
26,570✔
4634

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

4639
  return code;
26,570✔
4640
}
4641

4642
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
171,779✔
4643
  SStreamObj* pStream = pObj;
171,779✔
4644
  int64_t streamId = pStream->pCreate->streamId;
171,779✔
4645
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
171,779✔
4646
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
171,779✔
4647
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
171,779✔
4648
  
4649
  if ((userDropped || userStopped) && (NULL == pStatus)) {
171,779!
4650
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
×
4651
    return true;
×
4652
  }
4653
  
4654
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
171,779✔
4655
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
136,143✔
4656
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4657
    return true;
136,143✔
4658
  }
4659

4660
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
35,636✔
4661
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
7,778!
4662
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4663
    return true;
7,778✔
4664
  }  
4665

4666
  if (pStatus) {
27,858✔
4667
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
27,521!
4668
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4669
    }
4670

4671
    return true;
27,521✔
4672
  }
4673

4674
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
337✔
4675

4676
  return true;
337✔
4677
}
4678

4679
void msmCheckLoopStreamMap(SMnode *pMnode) {
17,629✔
4680
  SStmStatus* pStatus = NULL;
17,629✔
4681
  void* pIter = NULL;
17,629✔
4682
  int8_t stopped = 0;
17,629✔
4683
  int64_t streamId = 0;
17,629✔
4684
  
4685
  while (true) {
4686
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
119,856✔
4687
    if (NULL == pIter) {
119,856✔
4688
      break;
17,629✔
4689
    }
4690

4691
    pStatus = (SStmStatus*)pIter;
102,227✔
4692

4693
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
102,227✔
4694
    stopped = atomic_load_8(&pStatus->stopped);
102,227✔
4695
    if (MST_IS_USER_STOPPED(stopped)) {
102,227!
4696
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
159!
4697
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
159✔
4698
      continue;
159✔
4699
    }
4700

4701
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
102,068!
4702
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4703
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4704
      continue;
×
4705
    }
4706

4707
    if (MST_IS_ERROR_STOPPED(stopped)) {
102,068✔
4708
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
3,141✔
4709
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
2,306✔
4710
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4711
            
4712
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
2,306✔
4713
        continue;
2,306✔
4714
      }
4715

4716
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
835✔
4717
      continue;
835✔
4718
    }
4719

4720
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
98,927!
4721
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4722
      continue;
×
4723
    }
4724
  }
4725
}
17,629✔
4726

4727
void msmCheckStreamsStatus(SMnode *pMnode) {
117,854✔
4728
  SStmCheckStatusCtx ctx = {0};
117,854✔
4729

4730
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
117,854✔
4731
  
4732
  if (MST_READY_FOR_SDB_LOOP()) {
117,854!
4733
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
26,620✔
4734
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
26,620✔
4735
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
26,620✔
4736
  }
4737

4738
  if (MST_READY_FOR_MAP_LOOP()) {
117,854!
4739
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
17,629✔
4740
    msmCheckLoopStreamMap(pMnode);
17,629✔
4741
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
17,629✔
4742
  }
4743
}
117,854✔
4744

4745
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
645,651✔
4746
  for (int32_t i = 0; i < taskNum; ++i) {
1,313,540✔
4747
    SStmTaskStatus* pTask = *(pList + i);
668,679✔
4748

4749
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
668,679✔
4750
      continue;
27,502✔
4751
    }
4752
    
4753
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
642,617✔
4754
      continue;
641,101✔
4755
    }
4756

4757
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
1,516✔
4758
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
1,516✔
4759
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
790!
4760
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4761
          
4762
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
790✔
4763
      break;
790✔
4764
    }
4765

4766
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
726!
4767
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4768

4769
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
726✔
4770
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
726!
4771

4772
    SStmTaskAction task = {0};
726✔
4773
    task.streamId = streamId;
726✔
4774
    task.id = pTask->id;
726✔
4775
    task.flag = pTask->flags;
726✔
4776
    task.type = pTask->type;
726✔
4777
    
4778
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
726✔
4779
  }
4780
}
645,651✔
4781

4782
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
51,142✔
4783
  void* pIter = NULL;
51,142✔
4784
  SStmVgStreamStatus* pVg = NULL;
51,142✔
4785
  int64_t streamId = 0;
51,142✔
4786
  
4787
  while (true) {
211,095✔
4788
    pIter = taosHashIterate(pStreams, pIter);
262,237✔
4789
    if (NULL == pIter) {
262,237✔
4790
      break;
51,142✔
4791
    }
4792

4793
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
211,095✔
4794
    pVg = (SStmVgStreamStatus*)pIter;
211,095✔
4795

4796
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
211,095✔
4797
    if (taskNum > 0) {
211,095✔
4798
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
134,817✔
4799
    }
4800

4801
    taskNum = taosArrayGetSize(pVg->calcReaders);
211,095✔
4802
    if (taskNum > 0) {
211,095✔
4803
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
112,722✔
4804
    }
4805
  }
4806
}
51,142✔
4807

4808
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4809
  int64_t streamId = 0;
×
4810
  void* pIter = NULL;
×
4811
  SStmVgStreamStatus* pStream = NULL;
×
4812

4813
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4814
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4815
    return;
×
4816
  }
4817

4818
  
4819
  while (true) {
4820
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4821
    if (NULL == pIter) {
×
4822
      break;
×
4823
    }
4824

4825
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4826
    
4827
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4828
  }
4829

4830
  taosHashClear(pVg->streamTasks);
×
4831
}
4832

4833

4834
void msmCheckVgroupStatus(SMnode *pMnode) {
117,854✔
4835
  void* pIter = NULL;
117,854✔
4836
  int32_t code = 0;
117,854✔
4837
  
4838
  while (true) {
479,800✔
4839
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
597,654✔
4840
    if (NULL == pIter) {
597,654✔
4841
      break;
117,854✔
4842
    }
4843

4844
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
479,800✔
4845
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
479,800✔
4846
      continue;
427,372✔
4847
    }
4848
    
4849
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
52,428✔
4850

4851
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
52,428✔
4852
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
1,286✔
4853
      if (NULL == pVgroup) {
1,286!
4854
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
1,286✔
4855
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
1,286✔
4856
        if (code) {
1,286!
4857
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4858
        }
4859
        continue;
1,286✔
4860
      }
4861
      mndReleaseVgroup(pMnode, pVgroup);
×
4862
      
4863
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4864
      
4865
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
4866
      continue;
×
4867
    }
4868

4869
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
51,142✔
4870

4871
    msmCheckVgroupStreamStatus(pVg->streamTasks);
51,142✔
4872
  }
4873
}
117,854✔
4874

4875
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
×
4876
  *deployNum = 0;
×
4877
  
4878
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
×
4879
    if (pStream->runners[i]) {
×
4880
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
×
4881
      for (int32_t t = 0; t < taskNum; ++t) {
×
4882
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
×
4883
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
×
4884
        if (stopped) {
×
4885
          mstsDebug("stream already stopped %d, ignore it", stopped);
×
4886
          *deployNum = 0;
×
4887
          return;
×
4888
        }
4889

4890
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
×
4891
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
×
4892
      }
4893
      
4894
      deployId[*deployNum] = i;
×
4895
      (*deployNum)++;
×
4896
    }
4897
  }
4898
}
4899

4900
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
847✔
4901
  pSnode->runnerThreadNum = -1;
847✔
4902

4903
  (void)msmSTAddSnodesToMap(pMnode);
847✔
4904

4905
  int64_t streamId = 0;
847✔
4906
  void* pIter = NULL;
847✔
4907
  SStmSnodeStreamStatus* pStream = NULL;
847✔
4908
  int32_t deployNum = 0;
847✔
4909
  SStmTaskAction task = {0};
847✔
4910
  
4911
  while (true) {
4912
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
1,210✔
4913
    if (NULL == pIter) {
1,210✔
4914
      break;
847✔
4915
    }
4916

4917
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
363✔
4918
    
4919
    task.streamId = streamId;
363✔
4920
    
4921
    pStream = (SStmSnodeStreamStatus*)pIter;
363✔
4922
    if (pStream->trigger) {
363!
4923
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
363✔
4924
      if (stopped) {
363!
4925
        mstsDebug("stream already stopped %d, ignore it", stopped);
×
4926
        continue;
×
4927
      }
4928

4929
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
363!
4930
      
4931
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
363✔
4932
    } else {
4933
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
×
4934
      
4935
      if (task.deployNum > 0) {
×
4936
        //task.triggerStatus = pStream->trigger;
4937
        task.multiRunner = true;
×
4938
        task.type = STREAM_RUNNER_TASK;
×
4939
        
4940
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
×
4941
        
4942
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
×
4943
      }
4944
    }
4945
  }
4946

4947
  taosHashClear(pSnode->streamTasks);
847✔
4948
}
847✔
4949

4950

4951
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
14,755✔
4952
  void* pIter = NULL;
14,755✔
4953
  SStmSnodeStreamStatus* pSnode = NULL;
14,755✔
4954
  int64_t streamId = 0;
14,755✔
4955
  
4956
  while (true) {
4957
    pIter = taosHashIterate(pStreams, pIter);
119,034✔
4958
    if (NULL == pIter) {
119,034✔
4959
      break;
14,755✔
4960
    }
4961

4962
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
104,279✔
4963
    pSnode = (SStmSnodeStreamStatus*)pIter;
104,279✔
4964

4965
    if (NULL != pSnode->trigger) {
104,279✔
4966
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
100,286✔
4967
    }
4968

4969
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
417,116✔
4970
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
312,837✔
4971
      if (taskNum > 0) {
312,837✔
4972
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
297,826✔
4973
      }
4974
    }
4975
  }
4976
}
14,755✔
4977

4978

4979
void msmCheckSnodeStatus(SMnode *pMnode) {
117,854✔
4980
  void* pIter = NULL;
117,854✔
4981
  
4982
  while (true) {
140,723✔
4983
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
258,577✔
4984
    if (NULL == pIter) {
258,577✔
4985
      break;
117,854✔
4986
    }
4987

4988
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
140,723✔
4989
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
140,723✔
4990
      continue;
122,991✔
4991
    }
4992

4993
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
17,732✔
4994
    
4995
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
17,732✔
4996
    if (NULL == pSnode->streamTasks) {
17,732✔
4997
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
2,251!
4998
      continue;
2,251✔
4999
    }
5000
    
5001
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
15,481✔
5002
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
726!
5003
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5004
      
5005
      msmHandleSnodeLost(pMnode, pSnode);
726✔
5006
      continue;
726✔
5007
    }
5008
    
5009
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
14,755✔
5010

5011
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
14,755✔
5012
  }
5013
}
117,854✔
5014

5015

5016
void msmCheckTasksStatus(SMnode *pMnode) {
117,854✔
5017
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
117,854✔
5018

5019
  msmCheckVgroupStatus(pMnode);
117,854✔
5020
  msmCheckSnodeStatus(pMnode);
117,854✔
5021
}
117,854✔
5022

5023
void msmCheckSnodesState(SMnode *pMnode) {
117,854✔
5024
  if (!MST_READY_FOR_SNODE_LOOP()) {
117,854!
5025
    return;
101,686✔
5026
  }
5027

5028
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
16,168✔
5029

5030
  void* pIter = NULL;
16,168✔
5031
  int32_t snodeId = 0;
16,168✔
5032
  while (true) {
17,257✔
5033
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
33,425✔
5034
    if (NULL == pIter) {
33,425✔
5035
      break;
16,168✔
5036
    }
5037

5038
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
17,257✔
5039
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
17,257✔
5040
      continue;
17,136✔
5041
    }
5042

5043
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
121✔
5044
    if (NULL == pSnode->streamTasks) {
121!
5045
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
5046
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
5047
      continue;
×
5048
    }
5049
    
5050
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
121!
5051
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5052
    
5053
    msmHandleSnodeLost(pMnode, pSnode);
121✔
5054
  }
5055

5056
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
16,168✔
5057
}
5058

5059
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
2,360,325✔
5060
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
2,360,325✔
5061
  if (0 == active || MND_STM_STATE_NORMAL != state) {
2,360,325!
5062
    mstTrace("ignore health check since active:%d state:%d", active, state);
90!
5063
    return false;
90✔
5064
  }
5065

5066
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
2,360,235✔
5067
    mstTrace("ignore health check since no stream now");
2,124,527!
5068
    return false;
2,124,527✔
5069
  }
5070

5071
  return true;
235,708✔
5072
}
5073

5074
void msmHealthCheck(SMnode *pMnode) {
2,242,471✔
5075
  if (!msmCheckNeedHealthCheck(pMnode)) {
2,242,471✔
5076
    return;
2,124,617✔
5077
  }
5078

5079
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
216,226✔
5080
  
5081
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
117,854✔
5082
  if (!msmCheckNeedHealthCheck(pMnode)) {
117,854!
5083
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5084
    return;
×
5085
  }
5086
  
5087
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
117,854✔
5088
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
117,854✔
5089

5090
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
117,854✔
5091
  
5092
  msmCheckStreamsStatus(pMnode);
117,854✔
5093
  msmCheckTasksStatus(pMnode);
117,854✔
5094
  msmCheckSnodesState(pMnode);
117,854✔
5095

5096
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
117,854✔
5097

5098
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
117,854✔
5099
}
5100

5101
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
180✔
5102
  SStreamObj *pStream = pObj;
180✔
5103
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
180!
5104
    return true;
×
5105
  }
5106
  
5107
  pStream->updateTime = *(int64_t*)p1;
180✔
5108
  
5109
  (*(int32_t*)p2)++;
180✔
5110
  
5111
  return true;
180✔
5112
}
5113

5114
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5115
  int32_t code = 0;
×
5116
  int8_t  stopped = 0;
×
5117
  
5118
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5119
  
5120
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5121
  if (NULL == pStatus) {
×
5122
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5123
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5124
    goto _exit;
×
5125
  }
5126

5127
  stopped = atomic_load_8(&pStatus->stopped);
×
5128
  if (stopped) {
×
5129
    mstsError("stream already stopped, stopped:%d", stopped);
×
5130
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5131
    goto _exit;
×
5132
  }
5133

5134
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5135
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5136
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5137
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5138
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5139
    goto _exit;
×
5140
  }
5141

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

5145
_exit:
×
5146
  
5147
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5148

5149
  return code;
×
5150
}
5151

5152
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
158,070✔
5153
  int32_t code = TSDB_CODE_SUCCESS;
158,070✔
5154
  int32_t lino = 0;
158,070✔
5155
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
158,070✔
5156
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
158,070✔
5157
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
158,070✔
5158

5159
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
316,017✔
5160

5161
  mStreamMgmt.stat.activeTimes++;
158,070✔
5162
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
158,070✔
5163
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
158,070!
5164
  if (NULL == mStreamMgmt.tCtx) {
158,070!
5165
    code = terrno;
×
5166
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5167
    goto _exit;
×
5168
  }
5169

5170
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
158,070!
5171
  if (mStreamMgmt.actionQ == NULL) {
158,070!
5172
    code = terrno;
×
5173
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5174
    goto _exit;
×
5175
  }
5176
  
5177
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
158,070!
5178
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
158,070!
5179
  
5180
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
158,070✔
5181
  
5182
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
948,420✔
5183
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
790,350✔
5184

5185
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
4,742,100✔
5186
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
3,951,750✔
5187
      if (pCtx->deployStm[m] == NULL) {
3,951,750!
5188
        code = terrno;
×
5189
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5190
        goto _exit;
×
5191
      }
5192
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
3,951,750✔
5193
      
5194
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
3,951,750✔
5195
      if (pCtx->actionStm[m] == NULL) {
3,951,750!
5196
        code = terrno;
×
5197
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5198
        goto _exit;
×
5199
      }
5200
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
3,951,750✔
5201
    }
5202
  }
5203
  
5204
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
158,070✔
5205
  if (mStreamMgmt.streamMap == NULL) {
158,070!
5206
    code = terrno;
×
5207
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5208
    goto _exit;
×
5209
  }
5210
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
158,070✔
5211
  
5212
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
158,070✔
5213
  if (mStreamMgmt.taskMap == NULL) {
158,070!
5214
    code = terrno;
×
5215
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5216
    goto _exit;
×
5217
  }
5218
  
5219
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
158,070✔
5220
  if (mStreamMgmt.vgroupMap == NULL) {
158,070!
5221
    code = terrno;
×
5222
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
5223
    goto _exit;
×
5224
  }
5225
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
158,070✔
5226

5227
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
158,070✔
5228
  if (mStreamMgmt.snodeMap == NULL) {
158,070!
5229
    code = terrno;
×
5230
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5231
    goto _exit;
×
5232
  }
5233
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
158,070✔
5234
  
5235
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
158,070✔
5236
  if (mStreamMgmt.dnodeMap == NULL) {
158,070!
5237
    code = terrno;
×
5238
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5239
    goto _exit;
×
5240
  }
5241

5242
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
158,070✔
5243
  if (mStreamMgmt.toDeployVgMap == NULL) {
158,070!
5244
    code = terrno;
×
5245
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5246
    goto _exit;
×
5247
  }
5248
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
158,070✔
5249
  
5250
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
158,070✔
5251
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
158,070!
5252
    code = terrno;
×
5253
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5254
    goto _exit;
×
5255
  }
5256
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
158,070✔
5257

5258
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
158,070✔
5259
  if (mStreamMgmt.toUpdateScanMap == NULL) {
158,070!
5260
    code = terrno;
×
5261
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5262
    goto _exit;
×
5263
  }
5264
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
158,070✔
5265

5266
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
158,070!
5267
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
158,070!
5268

5269
  mStreamMgmt.lastTaskId = 1;
158,070✔
5270

5271
  int32_t activeStreamNum = 0;
158,070✔
5272
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
158,070✔
5273

5274
  if (activeStreamNum > 0) {
158,070✔
5275
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
30✔
5276
  } else {
5277
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
158,040✔
5278
  }
5279

5280
_exit:
158,070✔
5281

5282
  if (code) {
158,070!
5283
    msmDestroyRuntimeInfo(pMnode);
×
5284
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5285
  } else {
5286
    mstInfo("mnode stream runtime init done");
158,070!
5287
  }
5288

5289
  return code;
158,070✔
5290
}
5291

5292

5293

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