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

taosdata / TDengine / #4913

06 Jan 2026 01:30AM UTC coverage: 64.884% (-0.004%) from 64.888%
#4913

push

travis-ci

web-flow
merge: from main to 3.0 branch #34167

180 of 319 new or added lines in 14 files covered. (56.43%)

571 existing lines in 128 files now uncovered.

195016 of 300563 relevant lines covered (64.88%)

117540852.85 hits per line

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

76.71
/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() {
744,056✔
34
  SStmQNode* pQNode = NULL;
744,056✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
744,056✔
37
    return;
372,028✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
1,859,946✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
11,159,676✔
49
    taosHashCleanup(pCtx->deployStm[m]);
9,299,730✔
50
    taosHashCleanup(pCtx->actionStm[m]);
9,299,730✔
51
  }
52
}
1,859,946✔
53

54
void msmDestroyThreadCtxs() {
744,056✔
55
  if (NULL == mStreamMgmt.tCtx) {
744,056✔
56
    return;
372,028✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,231,974✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
1,859,946✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
372,028✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
744,056✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
744,056✔
94
}
744,056✔
95

96

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

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

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

111
    pStatus = pStream;
752✔
112
  }
113

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

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

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

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

136
_exit:
424✔
137

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

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

145

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

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

200
  pSnode = NULL;
392,224✔
201

202
_exit:
392,224✔
203

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

208
  return code;
392,224✔
209
}
210

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

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

235
  pDnode = NULL;
834,028✔
236

237
_exit:
834,028✔
238

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

243
  return code;
834,028✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
458,331✔
275
  int32_t code = TSDB_CODE_SUCCESS;
458,331✔
276
  int32_t lino = 0;
458,331✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
458,331✔
278
  if (NULL == pStream) {
458,331✔
279
    SStmVgStreamStatus stream = {0};
345,430✔
280
    if (trigReader) {
345,430✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
209,865✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
209,865✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
419,730✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
135,565✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
135,565✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
271,130✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
345,430✔
290
    goto _exit;
345,430✔
291
  }
292
  
293
  if (trigReader) {
112,901✔
294
    if (NULL == pStream->trigReaders) {
21,999✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
21,999✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
21,999✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
43,998✔
300
    goto _exit;
21,999✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
90,902✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
62,853✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
62,853✔
306
  }
307

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

310
_exit:
90,902✔
311

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

320
  return code;
458,331✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
56,762✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
56,762✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
56,762✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
401,569✔
339
  }
340
  
341
_exit:
458,205✔
342

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

350
  return code;
458,331✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
458,679✔
363
    if (NULL == pVg) {
458,679✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
105,537✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
105,537✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
211,074✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
105,537✔
368
      if (TSDB_CODE_SUCCESS == code) {
105,537✔
369
        goto _return;
105,537✔
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);
353,142✔
381
    if (NULL == pVg->taskList) {
353,142✔
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)) {
706,284✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
353,142✔
390
    
391
    taosHashRelease(pVgMap, pVg);
353,142✔
392
    break;
353,142✔
393
  }
394
  
395
_return:
458,679✔
396

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

404
  return code;
458,679✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
576,618✔
409
  int32_t code = TSDB_CODE_SUCCESS;
576,618✔
410
  int32_t lino = 0;
576,618✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
576,618✔
412
  if (NULL == pStream) {
576,618✔
413
    SStmSnodeStreamStatus stream = {0};
147,025✔
414
    if (deployId < 0) {
147,025✔
415
      stream.trigger = pStatus;
3,136✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
143,889✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
143,889✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
287,778✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
147,025✔
423
    goto _exit;
147,025✔
424
  }
425
  
426
  if (deployId < 0) {
429,593✔
427
    if (NULL != pStream->trigger) {
138,923✔
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;
138,923✔
433
    goto _exit;
138,923✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
290,670✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
273,054✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
273,054✔
439
  }
440

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

443
_exit:
290,670✔
444

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

453
  return code;
576,618✔
454
}
455

456

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

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

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

484
  return code;
576,618✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
142,059✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
33,406✔
568
      ext.deployed = false;
33,406✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
66,812✔
570

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

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

614
  return code;
434,559✔
615
}
616

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

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

632
_exit:
416,943✔
633

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

638
  return code;
416,943✔
639
}
640

641

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
10,150✔
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) {
39,289,627✔
691
  int32_t  code = TSDB_CODE_SUCCESS;
39,289,627✔
692
  int32_t  lino = 0;
39,289,627✔
693
  SStmVgroupStatus* pStatus = taosHashGet(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
39,289,627✔
694
  if (NULL == pStatus) {
39,289,627✔
695
    mstDebug("vgroup %d not exists in vgroupMap, ignore update upTs", vgId);
35,989,623✔
696
    return;
35,989,623✔
697
  }
698

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

707
      continue;
×
708
    }
709

710
    return;
60,402✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
13,797,659✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
52,819,965✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
39,022,306✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
39,022,306✔
725
  }
726

727
_exit:
13,797,659✔
728

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

733
  return code;
13,797,659✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
291,657✔
739
  int32_t num = taosArrayGetSize(pList);
291,657✔
740
  for (int32_t i = 0; i < num; ++i) {
846,594✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
713,375✔
742
    if (pScan->readFromCache) {
713,375✔
743
      return pScan->scanPlan;
158,438✔
744
    }
745
  }
746

747
  return NULL;
133,219✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
458,679✔
775
}
776

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

781
  if (pInfo->runnerDeploys > 0) {
138,923✔
782
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
138,923✔
783
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
138,923✔
784
  }
785
  
786
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
555,692✔
787
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
416,769✔
788
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
416,769✔
789

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

805
_exit:
138,923✔
806

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

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

826
  mndReleaseSnode(pMnode, pSnode);
×
827

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

833
  return TSDB_CODE_SUCCESS;
×
834
}
835

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

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

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

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

875
  SStreamTaskAddr addr;
141,618✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
142,059✔
877
  if (triggerReaderNum > 0) {
142,059✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
141,115✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
141,115✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
316,230✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
174,171✔
884
    addr.taskId = pStatus->id.taskId;
174,171✔
885
    addr.nodeId = pStatus->id.nodeId;
174,171✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
174,171✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
348,342✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
174,171✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
142,059✔
892
  pMsg->streamName = pInfo->streamName;
142,059✔
893

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

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

901
_exit:
138,923✔
902

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

912

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

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

946
  pMsg->colCids = pInfo->pCreate->colCids;
434,559✔
947
  pMsg->tagCids = pInfo->pCreate->tagCids;
434,559✔
948
_exit:
434,559✔
949

950
  if (code) {
434,559✔
951
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
952
  }
953
  
954
  return code;
434,559✔
955
}
956

957

958
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
344,088✔
959
  int32_t code = TSDB_CODE_SUCCESS;
344,088✔
960
  int32_t lino = 0;
344,088✔
961
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
344,088✔
962
  SListNode* pNode = pList ? listHead(pList) : NULL;
344,088✔
963
  
964
  for (int32_t i = 0; i < taskNum; ++i) {
802,419✔
965
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
458,331✔
966
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
458,331✔
967
    if (pNode) {
458,331✔
968
      pNode = TD_DLIST_NODE_NEXT(pNode);
224,190✔
969
    }
970
  }
971
  
972
_exit:
344,088✔
973

974
  if (code) {
344,088✔
975
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
976
  }
977
  
978
  return code;
344,088✔
979
}
980

981

982
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
559,002✔
983
  int32_t code = TSDB_CODE_SUCCESS;
559,002✔
984
  int32_t lino = 0;
559,002✔
985
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
559,002✔
986
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
559,002✔
987
  
988
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,135,620✔
989
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
576,618✔
990
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
576,618✔
991
  }
992
  
993
_exit:
559,002✔
994

995
  if (code) {
559,002✔
996
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
997
  }
998

999
  return code;
559,002✔
1000
}
1001

1002
int64_t msmAssignTaskId(void) {
1,034,775✔
1003
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,034,775✔
1004
}
1005

1006
int64_t msmAssignTaskSeriousId(void) {
1,034,775✔
1007
  return taosGetTimestampNs();
1,034,775✔
1008
}
1009

1010

1011
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
723,728✔
1012
  int32_t code = TSDB_CODE_SUCCESS;
723,728✔
1013
  int32_t lino = 0;
723,728✔
1014
  bool     noExists = false;
723,728✔
1015
  SStmSnodeStatus* pStatus = NULL;
723,728✔
1016

1017
  while (true) {
1018
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
730,896✔
1019
    if (NULL == pStatus) {
730,896✔
1020
      if (noExists) {
7,168✔
1021
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1022
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1023
      }
1024

1025
      noExists = true;
7,168✔
1026
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
7,168✔
1027
      
1028
      continue;
7,168✔
1029
    }
1030

1031
    *alive = (pStatus->runnerThreadNum >= 0);
723,728✔
1032
    break;
723,728✔
1033
  }
1034

1035
_exit:
723,728✔
1036

1037
  if (code) {
723,728✔
1038
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1039
  }
1040

1041
  return code;
723,728✔
1042
}
1043

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

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

1072
      snodeId = pSnode->replicaId;
460✔
1073
      mndReleaseSnode(pMnode, pSnode);
460✔
1074
      
1075
      continue;
460✔
1076
    }
1077

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

1082
_exit:
×
1083

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

1088
  return 0;
×
1089
}
1090

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

1105
  int32_t snodeTarget = taosRand() % snodeNum;
422,160✔
1106

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

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

1132
    snodeId = pObj->id;
441,882✔
1133
    if (snodeIdx == snodeTarget) {
441,882✔
1134
      sdbRelease(pMnode->pSdb, pObj);
422,160✔
1135
      sdbCancelFetch(pMnode->pSdb, pIter);
422,160✔
1136
      pObj = NULL;
422,160✔
1137
      goto _exit;
422,160✔
1138
    }
1139

1140
    sdbRelease(pMnode->pSdb, pObj);
19,722✔
1141
    snodeIdx++;
19,722✔
1142
  }
1143

1144
_exit:
424,580✔
1145

1146
  if (code) {
424,580✔
1147
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1148
  }
1149

1150
  if (0 == snodeId) {
424,580✔
1151
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
2,420✔
1152
  }
1153

1154
  return snodeId;
424,580✔
1155
}
1156

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

1166
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
559,002✔
1167

1168
_exit:
559,002✔
1169

1170
  if (0 == snodeId) {
559,002✔
1171
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1172
  }
1173

1174
  return snodeId;
559,002✔
1175
}
1176

1177

1178
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
142,059✔
1179
  int32_t code = TSDB_CODE_SUCCESS;
142,059✔
1180
  int32_t lino = 0;
142,059✔
1181
  int64_t streamId = pStream->pCreate->streamId;
142,059✔
1182

1183
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
142,059✔
1184
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
142,059✔
1185

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

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

1207
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
142,059✔
1208
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
142,059✔
1209

1210
_exit:
142,059✔
1211

1212
  if (code) {
142,059✔
1213
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1214
  }
1215

1216
  return code;
142,059✔
1217
}
1218

1219
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
231,864✔
1220
  int32_t code = TSDB_CODE_SUCCESS;
231,864✔
1221
  int32_t lino = 0;
231,864✔
1222

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

1245
_exit:
231,864✔
1246

1247
  if (code) {
231,864✔
1248
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1249
  }
1250

1251
  return code;
231,864✔
1252
}
1253

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

1283
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
67,050✔
1284
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
67,050✔
1285
      
1286
      void *pIter = NULL;
67,050✔
1287
      while (1) {
370,895✔
1288
        SStmTaskDeploy info = {0};
437,945✔
1289
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
437,945✔
1290
        if (pIter == NULL) {
437,945✔
1291
          break;
67,050✔
1292
        }
1293
      
1294
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
370,895✔
1295
          pState = taosArrayReserve(pInfo->trigReaders, 1);
100,106✔
1296

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

1306
        sdbRelease(pSdb, pVgroup);
370,895✔
1307
      }
1308
      break;
67,050✔
1309
    }
1310
    default:
944✔
1311
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
944✔
1312
      break;
944✔
1313
  }
1314

1315
_exit:
142,059✔
1316

1317
  mndReleaseDb(pCtx->pMnode, pDb);
142,059✔
1318

1319
  if (code) {
142,059✔
1320
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1321
  }
1322

1323
  return code;
142,059✔
1324
}
1325

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

1349
  key[1] = pSubplan->id.subplanId;
224,364✔
1350

1351
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
224,364✔
1352
  if (NULL == ppRes) {
224,364✔
1353
    SArray* pRes = taosArrayInit(1, sizeof(addr));
224,364✔
1354
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
224,364✔
1355
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
448,728✔
1356
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
224,364✔
1357
  } else {
1358
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1359
  }
1360

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

1367
  nodesDestroyNode((SNode*)pSubplan);
224,364✔
1368

1369
  if (code) {
224,364✔
1370
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1371
  }
1372

1373
  return code;
224,364✔
1374
}
1375

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

1384
  SStmTaskSrcAddr addr;
69,771✔
1385
  addr.isFromCache = true;
69,771✔
1386
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
69,771✔
1387
  addr.taskId = pCtx->triggerTaskId;
69,771✔
1388
  addr.vgId = pCtx->triggerNodeId;
69,771✔
1389
  addr.groupId = pSubplan->id.groupId;
69,771✔
1390

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

1406
  nodesDestroyNode((SNode*)pSubplan);
69,771✔
1407
  
1408
  if (code) {
69,771✔
1409
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1410
  }
1411

1412
  return code;
69,771✔
1413
}
1414

1415

1416
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
226,467✔
1417
  int32_t code = TSDB_CODE_SUCCESS;
226,467✔
1418
  int32_t lino = 0;
226,467✔
1419

1420
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
226,467✔
1421

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

1444
_exit:
226,467✔
1445

1446
  if (code) {
226,467✔
1447
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1448
  }
1449

1450
  return code;
226,467✔
1451
}
1452

1453

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

1463
  
1464
  for (int32_t i = 0; i < calcTasksNum; ++i) {
436,020✔
1465
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
293,961✔
1466
    if (pScan->readFromCache) {
293,961✔
1467
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
69,771✔
1468
      continue;
69,771✔
1469
    }
1470
    
1471
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
224,190✔
1472
    for (int32_t m = 0; m < vgNum; ++m) {
448,380✔
1473
      pState = tdListReserve(pInfo->calcReaders);
224,190✔
1474
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
224,190✔
1475

1476
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
224,190✔
1477
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
224,190✔
1478
    }
1479
  }
1480

1481
_exit:
142,059✔
1482

1483
  if (code) {
142,059✔
1484
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1485
  }
1486

1487
  return code;
142,059✔
1488
}
1489

1490

1491

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

1519
_exit:
174✔
1520

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

1525
  return code;
174✔
1526
}
1527

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

1536
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
142,059✔
1537
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
142,059✔
1538
  
1539
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
142,059✔
1540
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
142,059✔
1541
  
1542
_exit:
142,059✔
1543

1544
  if (code) {
142,059✔
1545
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1546
  }
1547
  
1548
  return code;
142,059✔
1549
}
1550

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

1562
  source.addr.epSet = pSrc->epset;
899,673✔
1563
  source.addr.nodeId = pSrc->vgId;
899,673✔
1564

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

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

1584
  mstsError("subplanId %d not found in runner list", subplanId);
×
1585

1586
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1587
}
1588

1589
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
434,559✔
1590
  int32_t code = TSDB_CODE_SUCCESS;
434,559✔
1591
  int32_t lino = 0;
434,559✔
1592
  int64_t key[2] = {streamId, -1};
434,559✔
1593
  SNode* pNode = NULL;
434,559✔
1594
  SStreamTask* pTask = &pDeploy->task;
434,559✔
1595
  FOREACH(pNode, pPlan->pChildren) {
1,334,232✔
1596
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
899,673✔
1597
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
17,616✔
1598
      continue;
17,616✔
1599
    }
1600
    
1601
    SValueNode* pVal = (SValueNode*)pNode;
882,057✔
1602
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
882,057✔
1603
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1604
      continue;
×
1605
    }
1606

1607
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
882,057✔
1608

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

1615
    int32_t childrenNum = taosArrayGetSize(*ppRes);
882,057✔
1616
    for (int32_t i = 0; i < childrenNum; ++i) {
1,764,114✔
1617
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
882,057✔
1618
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
882,057✔
1619
    }
1620
  }
1621

1622
_exit:
434,559✔
1623

1624
  if (code) {
434,559✔
1625
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1626
  }
1627

1628
  return code;
434,559✔
1629
}
1630

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

1639
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
434,559✔
1640

1641
  SNode* pTmp = NULL;
434,559✔
1642
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,334,232✔
1643
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
899,673✔
1644
      ERASE_NODE(pPlan->pChildren);
882,057✔
1645
      continue;
882,057✔
1646
    }
1647
    WHERE_NEXT;
17,616✔
1648
  }
1649
  nodesClearList(pPlan->pChildren);
434,559✔
1650
  pPlan->pChildren = NULL;
434,559✔
1651

1652
  if (NULL == pPlan->pParents) {
434,559✔
1653
    goto _exit;
416,943✔
1654
  }
1655

1656
  SNode* pNode = NULL;
17,616✔
1657
  int64_t parentTaskId = 0;
17,616✔
1658
  SStmTaskSrcAddr addr = {0};
17,616✔
1659
  addr.taskId = pDeploy->task.taskId;
17,616✔
1660
  addr.vgId = pDeploy->task.nodeId;
17,616✔
1661
  addr.groupId = pPlan->id.groupId;
17,616✔
1662
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
17,616✔
1663
  FOREACH(pNode, pPlan->pParents) {
35,232✔
1664
    SSubplan* pSubplan = (SSubplan*)pNode;
17,616✔
1665
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
17,616✔
1666
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
17,616✔
1667
  }
1668
  
1669
_exit:
433,236✔
1670

1671
  if (code) {
434,559✔
1672
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1673
  }
1674

1675
  return code;
434,559✔
1676
}
1677

1678
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
416,943✔
1679
  int32_t code = TSDB_CODE_SUCCESS;
416,943✔
1680
  int32_t lino = 0;
416,943✔
1681
  int64_t streamId = pStream->pCreate->streamId;
416,943✔
1682
  int32_t runnerNum = taosArrayGetSize(pRunners);
416,943✔
1683
  
1684
  for (int32_t i = 0; i < runnerNum; ++i) {
851,502✔
1685
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
434,559✔
1686
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
434,559✔
1687
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
434,559✔
1688

1689
    SStreamTask* pTask = &pDeploy->task;
434,559✔
1690
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
434,559✔
1691
  }
1692

1693
_exit:
416,943✔
1694

1695
  if (code) {
416,943✔
1696
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1697
  }
1698

1699
  return code;
416,943✔
1700
}
1701

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

1715
  if (pDag->numOfSubplans <= 0) {
138,923✔
1716
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1717
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1718
  }
1719

1720
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
138,923✔
1721
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1722
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1723
  }
1724

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

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

1745
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
138,923✔
1746
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
138,923✔
1747
  
1748
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
555,692✔
1749
    totalTaskNum = 0;
416,769✔
1750

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

1757
    taskIdx = 0;
416,769✔
1758
    
1759
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
842,346✔
1760
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
425,577✔
1761
      if (NULL == plans) {
425,577✔
1762
        mstsError("empty level plan, level:%d", i);
×
1763
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1764
      }
1765

1766
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
425,577✔
1767
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1768
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1769
      }
1770

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

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

1783
      for (int32_t n = 0; n < taskNum; ++n) {
859,962✔
1784
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
434,385✔
1785
        pState = taosArrayReserve(deployList, 1);
434,385✔
1786

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

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

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

1814
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
425,577✔
1815
    }
1816

1817
    if (totalTaskNum != pDag->numOfSubplans) {
416,769✔
1818
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1819
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1820
    }
1821

1822
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
416,769✔
1823

1824
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
416,769✔
1825

1826
    nodesDestroyNode((SNode *)pDag);
416,769✔
1827
    pDag = NULL;
416,769✔
1828
    
1829
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
416,769✔
1830

1831
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
416,769✔
1832
  }
1833

1834
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
555,692✔
1835
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
416,769✔
1836
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
416,769✔
1837
  }
1838
  
1839
  pInfo->runnerNum = totalTaskNum;
138,923✔
1840
  
1841
_exit:
138,923✔
1842

1843
  if (code) {
138,923✔
1844
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1845
  }
1846

1847
  taosArrayDestroy(deployTaskList);
138,923✔
1848
  nodesDestroyNode((SNode *)pDag);
138,923✔
1849

1850
  return code;
138,923✔
1851
}
1852

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

1870
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
348✔
1871
    deployId = pAction->deployId[r];
174✔
1872

1873
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
174✔
1874

1875
    pStartRunner = pRunner;
174✔
1876
    totalTaskNum = 0;
174✔
1877

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

1883
    taskIdx = 0;
174✔
1884
    
1885
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
348✔
1886
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
174✔
1887
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
174✔
1888
      totalTaskNum += taskNum;
174✔
1889

1890
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
174✔
1891
      
1892
      for (int32_t n = 0; n < taskNum; ++n) {
348✔
1893
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
174✔
1894

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

1901
        pRunner->id.nodeId = newNodeId;
174✔
1902

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

1913
        pRunner++;
174✔
1914
      }
1915

1916
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
174✔
1917
    }
1918

1919
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
174✔
1920

1921
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
174✔
1922

1923
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
174✔
1924

1925
    nodesDestroyNode((SNode *)pDag);
174✔
1926
    pDag = NULL;
174✔
1927

1928
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
174✔
1929
  }
1930

1931
_exit:
174✔
1932

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

1937
  nodesDestroyNode((SNode *)pDag);
174✔
1938
  taosArrayDestroy(deployTaskList);
174✔
1939

1940
  return code;
174✔
1941
}
1942

1943

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

1952
_exit:
133,731✔
1953

1954
  if (code) {
133,731✔
1955
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1956
  }
1957

1958
  return code;
133,731✔
1959
}
1960

1961

1962
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
142,059✔
1963
  if (NULL == pStream->pCreate->calcPlan) {
142,059✔
1964
    return TSDB_CODE_SUCCESS;
3,136✔
1965
  }
1966
  
1967
  int32_t code = TSDB_CODE_SUCCESS;
138,923✔
1968
  int32_t lino = 0;
138,923✔
1969
  int64_t streamId = pStream->pCreate->streamId;
138,923✔
1970
  SQueryPlan* pPlan = NULL;
138,923✔
1971

1972
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
138,923✔
1973

1974
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
555,692✔
1975
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
416,769✔
1976
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
416,769✔
1977
  }
1978

1979
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
138,923✔
1980
  pPlan = NULL;
138,923✔
1981
  
1982
  TAOS_CHECK_EXIT(code);
138,923✔
1983

1984
  taosHashClear(mStreamMgmt.toUpdateScanMap);
138,923✔
1985
  mStreamMgmt.toUpdateScanNum = 0;
138,923✔
1986

1987
_exit:
138,923✔
1988

1989
  nodesDestroyNode((SNode *)pPlan);
138,923✔
1990

1991
  if (code) {
138,923✔
1992
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1993
  }
1994

1995
  return code;
138,923✔
1996
}
1997

1998

1999
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
142,059✔
2000
  int32_t code = TSDB_CODE_SUCCESS;
142,059✔
2001
  int32_t lino = 0;
142,059✔
2002
  int64_t streamId = pStream->pCreate->streamId;
142,059✔
2003

2004
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
142,059✔
2005

2006
  pCtx->triggerTaskId = msmAssignTaskId();
142,059✔
2007
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
142,059✔
2008
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
142,059✔
2009
    TAOS_CHECK_EXIT(terrno);
×
2010
  }
2011

2012
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
142,059✔
2013
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
142,059✔
2014
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
142,059✔
2015
  
2016
_exit:
142,059✔
2017

2018
  if (code) {
142,059✔
2019
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2020
  }
2021

2022
  return code;
142,059✔
2023
}
2024

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

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

2064
_exit:
×
2065

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

2071
  return code;
×
2072
}
2073

2074

2075
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
136,867✔
2076
  int32_t code = TSDB_CODE_SUCCESS;
136,867✔
2077
  int32_t lino = 0;
136,867✔
2078
  int64_t streamId = pStream->pCreate->streamId;
136,867✔
2079

2080
  pStatus->lastActionTs = INT64_MIN;
136,867✔
2081

2082
  if (NULL == pStatus->streamName) {
136,867✔
2083
    pStatus->streamName = taosStrdup(pStream->name);
136,867✔
2084
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
136,867✔
2085
  }
2086

2087
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
136,867✔
2088
  
2089
  if (pStream->pCreate->numOfCalcSubplan > 0) {
136,867✔
2090
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
133,731✔
2091
    
2092
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
133,731✔
2093
  }
2094

2095
  if (initList) {
136,867✔
2096
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2097

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

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

2115
  if (code) {
136,867✔
2116
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2117
  }
2118

2119
  return code;
136,867✔
2120
}
2121

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

2128
  if (NULL == pStatus) {
142,059✔
2129
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
136,867✔
2130

2131
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
136,867✔
2132

2133
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
136,867✔
2134
  }
2135
  
2136
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
142,059✔
2137

2138
  mstLogSStmStatus("stream deployed", streamId, pStatus);
142,059✔
2139

2140
_exit:
142,059✔
2141

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

2151
  return code;
142,059✔
2152
}
2153

2154

2155
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
7,152✔
2156
  int32_t code = TSDB_CODE_SUCCESS;
7,152✔
2157
  void* pIter = NULL;
7,152✔
2158

2159
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
10,812✔
2160
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
3,660✔
2161
    (void)mstWaitLock(&pVg->lock, true);
3,660✔
2162

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

2169
    for (int32_t i = 0; i < taskNum; ++i) {
18,550✔
2170
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
14,890✔
2171
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
14,890✔
2172
        continue;
13,846✔
2173
      }
2174

2175
      mstDestroySStmTaskToDeployExt(pExt);
1,044✔
2176
      pExt->deployed = true;
1,044✔
2177
    }
2178
    
2179
    taosRUnLockLatch(&pVg->lock);
3,660✔
2180
  }
2181

2182
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
13,948✔
2183
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
6,796✔
2184
    (void)mstWaitLock(&pSnode->lock, true);
6,796✔
2185

2186
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
6,796✔
2187
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
6,796✔
2188
      for (int32_t i = 0; i < taskNum; ++i) {
14,662✔
2189
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
9,258✔
2190
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
9,258✔
2191
          continue;
8,506✔
2192
        }
2193
        
2194
        mstDestroySStmTaskToDeployExt(pExt);
752✔
2195
        pExt->deployed = true;
752✔
2196
      }
2197
    }
2198

2199
    taskNum = taosArrayGetSize(pSnode->runnerList);
6,796✔
2200
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
6,796✔
2201
      for (int32_t i = 0; i < taskNum; ++i) {
32,960✔
2202
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
26,164✔
2203
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
26,164✔
2204
          continue;
24,138✔
2205
        }
2206
        
2207
        mstDestroySStmTaskToDeployExt(pExt);
2,026✔
2208
        pExt->deployed = true;
2,026✔
2209
      }
2210
    }
2211

2212
    taosRUnLockLatch(&pSnode->lock);
6,796✔
2213
  }
2214

2215
  
2216
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
28,096✔
2217
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
20,944✔
2218
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
20,944✔
2219
    if (TSDB_CODE_SUCCESS == code) {
20,944✔
2220
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
9,812✔
2221
    }
2222
  }
2223

2224
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
28,749✔
2225
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
21,597✔
2226
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
21,597✔
2227
    if (TSDB_CODE_SUCCESS == code) {
21,597✔
2228
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
9,853✔
2229
    }
2230
  }
2231

2232
  size_t keyLen = 0;
7,152✔
2233
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
426,467✔
2234
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
419,315✔
2235
    if (*pStreamId == streamId) {
419,315✔
2236
      int64_t taskId = *(pStreamId + 1);
44,221✔
2237
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
44,221✔
2238
      if (code) {
44,221✔
2239
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2240
      } else {
2241
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
44,221✔
2242
      }
2243
    }
2244
  }
2245

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

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

2263
  mstResetSStmStatus(pStatus);
5,192✔
2264

2265
  pStatus->deployTimes++;
5,192✔
2266
}
5,192✔
2267

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

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

2295
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
142,938✔
2296
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
142,938✔
2297
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
879✔
2298
    return TSDB_CODE_SUCCESS;
879✔
2299
  }
2300

2301
  TAOS_CHECK_EXIT(code);
142,059✔
2302

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

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

2319
_exit:
142,059✔
2320

2321
  mndReleaseStream(pCtx->pMnode, pStream);
142,059✔
2322

2323
  if (code) {
142,059✔
2324
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2325
  }
2326

2327
  return code;
142,059✔
2328
}
2329

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

2362
_exit:
348✔
2363

2364
  if (code) {
348✔
2365
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2366
  }
2367

2368
  return code;
348✔
2369
}
2370

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

2402
_exit:
2403

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

2408
  return code;
2409
}
2410
*/
2411

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

2461
_exit:
174✔
2462

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

2467
  return code;
174✔
2468
}
2469

2470

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

2478
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
870✔
2479

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

2486
  int8_t stopped = atomic_load_8(&pStatus->stopped);
870✔
2487
  if (stopped) {
870✔
2488
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
348✔
2489
    return TSDB_CODE_SUCCESS;
348✔
2490
  }
2491

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

2498
  TAOS_CHECK_EXIT(code);
522✔
2499

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

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

2530
_exit:
522✔
2531

2532
  if (pStream) {
522✔
2533
    mndReleaseStream(pCtx->pMnode, pStream);
522✔
2534
  }
2535

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

2541
  return code;
522✔
2542
}
2543

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

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

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

2589
  return TSDB_CODE_SUCCESS;
×
2590
}
2591

2592
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
1,960✔
2593
  int32_t code = TSDB_CODE_SUCCESS;
1,960✔
2594
  int32_t lino = 0;
1,960✔
2595

2596
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
1,960✔
2597

2598
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
1,960✔
2599

2600
_exit:
1,960✔
2601

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

2608
  return code;
1,960✔
2609
}
2610

2611
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
11,009✔
2612
  int32_t code = TSDB_CODE_SUCCESS;
11,009✔
2613
  int32_t lino = 0;
11,009✔
2614

2615
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
11,009✔
2616
  if (0 == active || MND_STM_STATE_NORMAL != state) {
11,009✔
2617
    mstsError("stream mgmt not available since active:%d state:%d", active, state);
304✔
2618
    return;
304✔
2619
  }
2620

2621
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
10,705✔
2622
  if (NULL == pStream) {
10,705✔
2623
    mstsInfo("stream %s already not in streamMap", streamName);
673✔
2624
    goto _exit;
673✔
2625
  }
2626

2627
  atomic_store_8(&pStream->stopped, 2);
10,032✔
2628

2629
  mstsInfo("set stream %s stopped by user", streamName);
10,032✔
2630

2631
_exit:
×
2632

2633
  taosHashRelease(mStreamMgmt.streamMap, pStream);
10,705✔
2634

2635
  if (code) {
10,705✔
2636
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2637
  }
2638

2639
  return;
10,705✔
2640
}
2641

2642
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
3,252✔
2643
  int32_t code = TSDB_CODE_SUCCESS;
3,252✔
2644
  int32_t lino = 0;
3,252✔
2645

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

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

2659
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
3,252✔
2660

2661
_exit:
3,252✔
2662

2663
  taosHashRelease(mStreamMgmt.streamMap, pStream);
3,252✔
2664

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

2669
  return code;
3,252✔
2670
}
2671

2672
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
28,730✔
2673
  int32_t code = TSDB_CODE_SUCCESS;
28,730✔
2674
  int32_t lino = 0;
28,730✔
2675
  SStmQNode* pQNode = NULL;
28,730✔
2676

2677
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
172,538✔
2678
    switch (pQNode->type) {
143,808✔
2679
      case STREAM_ACT_DEPLOY:
143,808✔
2680
        if (pQNode->streamAct) {
143,808✔
2681
          mstDebug("start to handle stream deploy action");
142,938✔
2682
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
142,938✔
2683
        } else {
2684
          mstDebug("start to handle task deploy action");
870✔
2685
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
870✔
2686
        }
2687
        break;
143,808✔
2688
      default:
×
2689
        break;
×
2690
    }
2691
  }
2692

2693
_exit:
28,730✔
2694

2695
  if (code) {
28,730✔
2696
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2697
  }
2698
}
28,730✔
2699

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

2711
    pStatus = (SStmStatus*)pIter;
×
2712

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

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

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

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

2728
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2729

2730
  msmStopAllStreamsByGrant(errCode);
×
2731

2732
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2733
  
2734
  return errCode;
×
2735
}
2736

2737
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,031,475✔
2738
  int32_t code = TSDB_CODE_SUCCESS;
1,031,475✔
2739
  int32_t lino = 0;
1,031,475✔
2740
  int64_t streamId = pDeploy->task.streamId;
1,031,475✔
2741
  
2742
  switch (pDeploy->task.type) {
1,031,475✔
2743
    case STREAM_READER_TASK:
457,635✔
2744
      if (NULL == pStream->readerTasks) {
457,635✔
2745
        pStream->streamId = streamId;
194,685✔
2746
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
194,685✔
2747
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
194,685✔
2748
      }
2749
      
2750
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
915,270✔
2751
      break;
457,635✔
2752
    case STREAM_TRIGGER_TASK:
141,307✔
2753
      pStream->streamId = streamId;
141,307✔
2754
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
141,307✔
2755
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
141,307✔
2756
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
141,307✔
2757
      break;
141,307✔
2758
    case STREAM_RUNNER_TASK:
432,533✔
2759
      if (NULL == pStream->runnerTasks) {
432,533✔
2760
        pStream->streamId = streamId;
143,520✔
2761
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
143,520✔
2762
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
143,520✔
2763
      }      
2764
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
865,066✔
2765
      break;
432,533✔
2766
    default:
×
2767
      break;
×
2768
  }
2769

2770
_exit:
1,031,475✔
2771

2772
  if (code) {
1,031,475✔
2773
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2774
  }
2775

2776
  return code;
1,031,475✔
2777
}
2778

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

2796
      if (TSDB_CODE_DUP_KEY != code) {
×
2797
        goto _exit;
×
2798
      }    
2799

2800
      tFreeSStmStreamDeploy(&streamDeploy);
×
2801
      continue;
×
2802
    }
2803

2804
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
828,266✔
2805
    
2806
    break;
828,266✔
2807
  }
2808
  
2809
_exit:
1,031,475✔
2810

2811
  taosHashRelease(pHash, pStream);
1,031,475✔
2812

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

2819
  return code;
1,031,475✔
2820
}
2821

2822

2823
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
171,198✔
2824
  int32_t code = TSDB_CODE_SUCCESS;
171,198✔
2825
  int32_t lino = 0;
171,198✔
2826
  int32_t taskNum = taosArrayGetSize(pTasks);
171,198✔
2827

2828
  for (int32_t i = 0; i < taskNum; ++i) {
1,205,805✔
2829
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,034,607✔
2830
    if (pExt->deployed) {
1,034,607✔
2831
      continue;
3,132✔
2832
    }
2833

2834
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,031,475✔
2835
    pExt->deployed = true;
1,031,475✔
2836

2837
    (void)atomic_add_fetch_32(deployed, 1);
1,031,475✔
2838
  }
2839

2840
_exit:
171,198✔
2841

2842
  if (code) {
171,198✔
2843
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2844
  }
2845

2846
  return code;
171,198✔
2847
}
2848

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

2856
  mstDebug("start to add stream vgroup tasks deploy");
45,274✔
2857
  
2858
  for (int32_t i = 0; i < vgNum; ++i) {
312,595✔
2859
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
267,321✔
2860

2861
    msmUpdateVgroupUpTs(pCtx, *vgId);
267,321✔
2862

2863
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
267,321✔
2864
    if (NULL == pVg) {
267,321✔
2865
      continue;
161,053✔
2866
    }
2867

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

2881
_exit:
45,274✔
2882

2883
  if (code) {
45,274✔
2884
    if (pVg) {
×
2885
      taosRUnLockLatch(&pVg->lock);
×
2886
    }
2887

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

2891
  return code;
45,274✔
2892
}
2893

2894
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
57,836✔
2895
  int32_t code = TSDB_CODE_SUCCESS;
57,836✔
2896
  int32_t lino = 0;
57,836✔
2897
  SStmSnodeTasksDeploy* pSnode = NULL;
57,836✔
2898
  SStreamHbMsg* pReq = pCtx->pReq;
57,836✔
2899

2900
  mstDebug("start to add stream snode tasks deploy");
57,836✔
2901
  
2902
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
57,836✔
2903
  if (NULL == pSnode) {
57,836✔
2904
    return TSDB_CODE_SUCCESS;
24,448✔
2905
  }
2906

2907
  (void)mstWaitLock(&pSnode->lock, false);
33,388✔
2908
  
2909
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
33,388✔
2910
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
31,542✔
2911
  }
2912

2913
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
33,388✔
2914
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
33,388✔
2915
  }
2916
  
2917
  taosWUnLockLatch(&pSnode->lock);
33,388✔
2918

2919
_exit:
33,388✔
2920

2921
  if (code) {
33,388✔
2922
    if (pSnode) {
×
2923
      taosWUnLockLatch(&pSnode->lock);
×
2924
    }
2925

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

2929
  return code;
33,388✔
2930
}
2931

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

2943
_exit:
458,864✔
2944

2945
  if (code) {
458,864✔
2946
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2947
  }
2948

2949
  return code;
458,864✔
2950
}
2951

2952
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
46,061✔
2953
  int32_t code = TSDB_CODE_SUCCESS;
46,061✔
2954
  int32_t lino = 0;
46,061✔
2955
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
46,061✔
2956
  void* pIter = NULL;
46,061✔
2957

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

2963
  while (1) {
203,209✔
2964
    pIter = taosHashIterate(pCtx->deployStm, pIter);
249,270✔
2965
    if (pIter == NULL) {
249,270✔
2966
      break;
46,061✔
2967
    }
2968
    
2969
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
203,209✔
2970
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
406,418✔
2971

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

2976
    mstClearSStmStreamDeploy(pDeploy);
203,209✔
2977
    
2978
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
203,209✔
2979
  }
2980
  
2981
_exit:
46,061✔
2982

2983
  if (pIter) {
46,061✔
2984
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2985
  }
2986

2987
  if (code) {
46,061✔
2988
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2989
  }
2990
  
2991
  return code;
46,061✔
2992
}
2993

2994
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
45,274✔
2995
  int32_t code = TSDB_CODE_SUCCESS;
45,274✔
2996
  int32_t lino = 0;
45,274✔
2997
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
45,274✔
2998
  SStmVgTasksToDeploy* pVg = NULL;
45,274✔
2999
  
3000
  for (int32_t i = 0; i < vgNum; ++i) {
312,595✔
3001
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
267,321✔
3002
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
267,321✔
3003
    if (NULL == pVg) {
267,321✔
3004
      continue;
161,053✔
3005
    }
3006

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

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

3029
    for (int32_t m = taskNum - 1; m >= 0; --m) {
7,379✔
3030
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
6,474✔
3031
      if (!pExt->deployed) {
6,474✔
3032
        continue;
731✔
3033
      }
3034

3035
      mstDestroySStmTaskToDeployExt(pExt);
5,743✔
3036

3037
      taosArrayRemove(pVg->taskList, m);
5,743✔
3038
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
5,743✔
3039
    }
3040
    atomic_store_32(&pVg->deployed, 0);
905✔
3041
    taosWUnLockLatch(&pVg->lock);
905✔
3042
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
905✔
3043
  }
3044

3045
_exit:
45,274✔
3046

3047
  if (code) {
45,274✔
3048
    if (pVg) {
×
3049
      taosWUnLockLatch(&pVg->lock);
×
3050
    }
3051

3052
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3053
  }
3054
}
45,274✔
3055

3056
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
73,037✔
3057
  if (!GOT_SNODE(snodeId)) {
73,037✔
3058
    return;
15,201✔
3059
  }
3060
  
3061
  int32_t code = TSDB_CODE_SUCCESS;
57,836✔
3062
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
57,836✔
3063
  if (NULL == pSnode) {
57,836✔
3064
    return;
24,448✔
3065
  }
3066

3067
  if (taosWTryLockLatch(&pSnode->lock)) {
33,388✔
3068
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3069
    return;
×
3070
  }
3071

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

3081
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
33,388✔
3082
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
33,214✔
3083
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
33,214✔
3084
    pSnode->triggerList = NULL;
33,214✔
3085
  }
3086

3087
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
33,388✔
3088
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
33,040✔
3089
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
33,040✔
3090
    pSnode->runnerList = NULL;
33,040✔
3091
  }
3092

3093
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
33,388✔
3094
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
33,040✔
3095
    taosWUnLockLatch(&pSnode->lock);
33,040✔
3096
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
33,040✔
3097
    return;
33,040✔
3098
  }
3099

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

3107
      mstDestroySStmTaskToDeployExt(pExt);
1,044✔
3108
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
1,044✔
3109
      taosArrayRemove(pSnode->triggerList, m);
1,044✔
3110
    }
3111
    
3112
    pSnode->triggerDeployed = 0;
174✔
3113
  }
3114

3115
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
348✔
3116
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
3,480✔
3117
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
3,132✔
3118
      if (!pExt->deployed) {
3,132✔
3119
        continue;
×
3120
      }
3121

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

3134
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
14,082,679✔
3135
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,082,679✔
3136
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
45,274✔
3137
  }
3138

3139
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
14,082,679✔
3140
    msmCleanDeployedSnodeTasks(pHb->snodeId);
73,037✔
3141
  }
3142
}
14,082,679✔
3143

3144
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
14,082,679✔
3145
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,082,679✔
3146
  if (mStreamMgmt.tCtx) {
14,082,679✔
3147
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
14,001,157✔
3148
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
14,001,157✔
3149
  }
3150
}
14,082,679✔
3151

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

3169
_exit:
198,341✔
3170

3171
  if (code) {
198,341✔
3172
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3173
  }
3174

3175
  return code;
198,341✔
3176
}
3177

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

3194
_exit:
×
3195

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

3200
  return code;
×
3201
}
3202

3203

3204

3205
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
57,314✔
3206
  int32_t code = TSDB_CODE_SUCCESS;
57,314✔
3207
  int32_t lino = 0;
57,314✔
3208
  int32_t action = STREAM_ACT_UNDEPLOY;
57,314✔
3209
  bool    dropped = false;
57,314✔
3210

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

3222
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
78,870✔
3223
    if (pAction->undeploy.doCheckpoint) {
39,435✔
3224
      pAction->undeploy.doCheckpoint = dropped ? false : true;
19,585✔
3225
    }
3226
    if (!pAction->undeploy.doCleanup) {
39,435✔
3227
      pAction->undeploy.doCleanup = dropped ? true : false;
19,585✔
3228
    }
3229
    
3230
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
39,435✔
3231
  } else {
3232
    SStmAction newAction = {0};
17,879✔
3233
    newAction.actions = action;
17,879✔
3234
    newAction.undeploy.doCheckpoint = dropped ? false : true;
17,879✔
3235
    newAction.undeploy.doCleanup = dropped ? true : false;
17,879✔
3236
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
17,879✔
3237
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
17,879✔
3238
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
35,758✔
3239
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
17,879✔
3240
    
3241
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
17,879✔
3242
  }
3243

3244
_exit:
57,314✔
3245

3246
  if (code) {
57,314✔
3247
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3248
  }
3249

3250
  return code;
57,314✔
3251
}
3252

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

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

3274
_exit:
3,252✔
3275

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

3281
  return code;
3,252✔
3282
}
3283

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

3290
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
236,034✔
3291
    return false;
×
3292
  }
3293

3294
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
236,034✔
3295
  for (int32_t i = 0; i < readerNum; ++i) {
518,663✔
3296
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
282,629✔
3297
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
282,629✔
3298
      return false;
×
3299
    }
3300
  }
3301

3302
  readerNum = msmGetTrigOReaderSize(pStream->trigOReaders);
236,034✔
3303
  for (int32_t i = 0; i < readerNum; ++i) {
293,727✔
3304
    SStmTaskStatus* pStatus = msmGetTrigOReader(pStream->trigOReaders, i);
57,693✔
3305
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
57,693✔
3306
      return false;
×
3307
    }
3308
  }
3309

3310
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
236,034✔
3311
  SListNode* pNode = listHead(pStream->calcReaders);
236,034✔
3312
  for (int32_t i = 0; i < readerNum; ++i) {
688,220✔
3313
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
452,186✔
3314
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
452,186✔
3315
      return false;
×
3316
    }
3317
    pNode = TD_DLIST_NODE_NEXT(pNode);
452,186✔
3318
  }
3319

3320
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
812,877✔
3321
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
614,536✔
3322
    for (int32_t m = 0; m < runnerNum; ++m) {
1,208,995✔
3323
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
632,152✔
3324
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
632,152✔
3325
        return false;
37,693✔
3326
      }
3327
    }
3328
  }
3329
  
3330
  return true;
198,341✔
3331
}
3332

3333

3334
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
8,457,618✔
3335
  int32_t code = TSDB_CODE_SUCCESS;
8,457,618✔
3336
  int32_t lino = 0;
8,457,618✔
3337
  int32_t action = 0;
8,457,618✔
3338
  int64_t streamId = pMsg->streamId;
8,457,618✔
3339
  SStreamTask* pTask = (SStreamTask*)pMsg;
8,457,618✔
3340
  int8_t  stopped = 0;
8,457,618✔
3341

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

3351
  stopped = atomic_load_8(&pStatus->stopped);
8,457,618✔
3352
  if (stopped) {
8,457,618✔
3353
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3354
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3355
    return;
×
3356
  }
3357
  
3358
  switch (pMsg->status) {
8,457,618✔
3359
    case STREAM_STATUS_INIT:      
8,452,658✔
3360
      if (STREAM_TRIGGER_TASK != pMsg->type) {
8,452,658✔
3361
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
8,066,143✔
3362
        return;
8,066,143✔
3363
      }
3364
      
3365
      if (INT64_MIN == pStatus->lastActionTs) {
386,515✔
3366
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3367
        return;
×
3368
      }
3369
      
3370
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
386,515✔
3371
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
150,481✔
3372
        return;
150,481✔
3373
      }
3374

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

3397
_exit:
240,994✔
3398

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

3405
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
56,583✔
3406
  int32_t code = TSDB_CODE_SUCCESS;
56,583✔
3407
  int32_t lino = 0;
56,583✔
3408
  SStreamTask* pTask = (SStreamTask*)pStatus;
56,583✔
3409
  int64_t streamId = pStatus->streamId;
56,583✔
3410

3411
  msttInfo("start to handle task error, type: %d", err);
56,583✔
3412

3413
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
56,583✔
3414

3415
_exit:
56,583✔
3416

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

3423
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
2,868,370✔
3424
  int32_t code = TSDB_CODE_SUCCESS;
2,868,370✔
3425
  int32_t lino = 0;
2,868,370✔
3426
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
2,868,370✔
3427

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

3446
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
2,868,370✔
3447
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,453,843✔
3448
    if (NULL == pStatus->detailStatus) {
1,453,843✔
3449
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
139,823✔
3450
      if (NULL == pStatus->detailStatus) {
139,823✔
3451
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3452
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3453
      }
3454
    }
3455
    
3456
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,453,843✔
3457
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,453,843✔
3458
  }
3459

3460
_exit:
1,414,527✔
3461

3462
  if (code) {
2,868,370✔
3463
    // IGNORE STOP STREAM BY ERROR
3464
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3465
  }
3466
}
2,868,370✔
3467

3468
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
709,903✔
3469
  int32_t code = TSDB_CODE_SUCCESS;
709,903✔
3470
  int32_t lino = 0;
709,903✔
3471
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
709,903✔
3472

3473
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
709,903✔
3474

3475
  for (int32_t i = 0; i < num; ++i) {
22,599,564✔
3476
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
21,889,661✔
3477
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
21,889,661✔
3478
    
3479
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
21,889,661✔
3480
    if (NULL == ppStatus) {
21,889,661✔
3481
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
3,828✔
3482
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
3,828✔
3483
      continue;
3,828✔
3484
    }
3485

3486
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
21,885,833✔
3487
    int8_t stopped = atomic_load_8(&pStream->stopped);
21,885,833✔
3488
    if (stopped) {
21,885,833✔
3489
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
52,755✔
3490
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
52,755✔
3491
      continue;
52,755✔
3492
    }
3493

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

3502
    if ((*ppStatus)->status != pTask->status) {
21,833,078✔
3503
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,782,858✔
3504
        (*ppStatus)->runningStartTs = pCtx->currTs;
757,634✔
3505
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,025,224✔
3506
        if (pStream->triggerTask) {
×
3507
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3508
        }
3509
        
3510
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3511
      }
3512
    }
3513
    
3514
    (*ppStatus)->errCode = pTask->errorCode;
21,833,078✔
3515
    (*ppStatus)->status = pTask->status;
21,833,078✔
3516
    (*ppStatus)->lastUpTs = pCtx->currTs;
21,833,078✔
3517
    
3518
    if (STREAM_STATUS_RUNNING != pTask->status) {
21,833,078✔
3519
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
8,457,618✔
3520
    }
3521
    
3522
    if (STREAM_TRIGGER_TASK == pTask->type) {
21,833,078✔
3523
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
2,868,370✔
3524
    }
3525
  }
3526

3527
_exit:
709,903✔
3528

3529
  if (code) {
709,903✔
3530
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3531
  }
3532

3533
  return code;
709,903✔
3534
}
3535

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

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

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

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

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

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

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

3633
_exit:
×
3634

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

3641
  return code;
×
3642
}
3643

3644
int32_t msmWatchHandleStatusUpdate(SStmGrpCtx* pCtx) {
×
3645
  int32_t code = TSDB_CODE_SUCCESS;
×
3646
  int32_t lino = 0;
×
3647
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
×
3648

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

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

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

3672
_exit:
×
3673

3674
  if (code) {
×
3675
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3676
  }
3677

3678
  return code;
×
3679
}
3680

3681
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
198,341✔
3682
  int32_t code = TSDB_CODE_SUCCESS;
198,341✔
3683
  int32_t lino = 0;
198,341✔
3684
  if (NULL == pCtx->pRsp->start.taskList) {
198,341✔
3685
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
71,425✔
3686
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
71,425✔
3687
  }
3688

3689
  SStmTaskId* pId = &pAction->start.triggerId;
198,341✔
3690
  SStreamTaskStart start = {0};
198,341✔
3691
  start.task.type = STREAM_TRIGGER_TASK;
198,341✔
3692
  start.task.streamId = streamId;
198,341✔
3693
  start.task.taskId = pId->taskId;
198,341✔
3694
  start.task.seriousId = pId->seriousId;
198,341✔
3695
  start.task.nodeId = pId->nodeId;
198,341✔
3696
  start.task.taskIdx = pId->taskIdx;
198,341✔
3697

3698
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
396,682✔
3699
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
198,341✔
3700

3701
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
198,341✔
3702

3703
  return;
198,341✔
3704

3705
_exit:
×
3706

3707
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3708
}
3709

3710

3711
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
17,879✔
3712
  int32_t code = TSDB_CODE_SUCCESS;
17,879✔
3713
  int32_t lino = 0;
17,879✔
3714
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
17,879✔
3715
  if (NULL == pCtx->pRsp->undeploy.taskList) {
17,879✔
3716
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
15,677✔
3717
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
15,677✔
3718
  }
3719

3720
  SStreamTaskUndeploy undeploy;
17,879✔
3721
  for (int32_t i = 0; i < dropNum; ++i) {
75,193✔
3722
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
57,314✔
3723
    undeploy.task = *pTask;
57,314✔
3724
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
57,314✔
3725
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
57,314✔
3726

3727
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
114,628✔
3728
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
57,314✔
3729

3730
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
57,314✔
3731
  }
3732

3733
  return;
17,879✔
3734

3735
_exit:
×
3736

3737
  mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3738
}
3739

3740
void msmRspAddTriggerUpdate(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
×
3741
  int32_t code = TSDB_CODE_SUCCESS;
×
3742
  int32_t lino = 0;
×
3743

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

3750
  if (NULL == pStream->triggerTask) {
×
3751
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3752
    return;
×
3753
  }
3754

3755
  SStreamMgmtRsp rsp = {0};
×
3756
  rsp.reqId = INT64_MIN;
×
3757
  rsp.header.msgType = STREAM_MSG_UPDATE_RUNNER;
×
3758
  rsp.task.streamId = streamId;
×
3759
  rsp.task.taskId = pStream->triggerTask->id.taskId;
×
3760

3761
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3762

3763
  if (NULL == pCtx->pRsp->rsps.rspList) {
×
3764
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
3765
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
3766
  }
3767

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

3770
_exit:
×
3771

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

3779
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
3,252✔
3780
  int32_t code = TSDB_CODE_SUCCESS;
3,252✔
3781
  int32_t lino = 0;
3,252✔
3782

3783
  SStmStatus* pStream = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,252✔
3784
  if (NULL == pStream) {
3,252✔
3785
    mstsDebug("stream already not exists in streamMap, ignore trigger update, streamRemain:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
3786
    return;
×
3787
  }
3788

3789
  if (NULL == pStream->triggerTask) {
3,252✔
3790
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
3791
    return;
×
3792
  }
3793

3794
  SStreamMgmtRsp rsp = {0};
3,252✔
3795
  rsp.reqId = INT64_MIN;
3,252✔
3796
  rsp.header.msgType = STREAM_MSG_USER_RECALC;
3,252✔
3797
  rsp.task.streamId = streamId;
3,252✔
3798
  rsp.task.taskId = pStream->triggerTask->id.taskId;
3,252✔
3799
  TSWAP(rsp.cont.recalcList, pAction->recalc.recalcList);
3,252✔
3800

3801
  if (NULL == pCtx->pRsp->rsps.rspList) {
3,252✔
3802
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
3,058✔
3803
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
3,058✔
3804
  }
3805

3806
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
6,504✔
3807

3808
_exit:
3,252✔
3809

3810
  if (code) {
3,252✔
3811
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3812
  } else {
3813
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
3,252✔
3814
  }
3815
}
3816

3817

3818
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
89,646✔
3819
  int32_t code = TSDB_CODE_SUCCESS;
89,646✔
3820
  int32_t lino = 0;
89,646✔
3821
  void* pIter = NULL;
89,646✔
3822
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
89,646✔
3823

3824
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
89,646✔
3825

3826
  while (1) {
219,472✔
3827
    pIter = taosHashIterate(pCtx->actionStm, pIter);
309,118✔
3828
    if (pIter == NULL) {
309,118✔
3829
      break;
89,646✔
3830
    }
3831

3832
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
219,472✔
3833
    SStmAction *pAction = (SStmAction *)pIter;
219,472✔
3834
    
3835
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
219,472✔
3836
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
17,879✔
3837
      continue;
17,879✔
3838
    }
3839

3840
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
201,593✔
3841
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3842
    }
3843

3844
    if (STREAM_ACT_RECALC & pAction->actions) {
201,593✔
3845
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
3,252✔
3846
    }
3847

3848
    if (STREAM_ACT_START & pAction->actions) {
201,593✔
3849
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
198,341✔
3850
    }
3851
  }
3852
  
3853
_exit:
89,646✔
3854

3855
  if (pIter) {
89,646✔
3856
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3857
  }
3858

3859
  if (code) {
89,646✔
3860
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3861
  }
3862

3863
  return code;
89,646✔
3864
}
3865

3866
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
14,001,157✔
3867
  int32_t  code = TSDB_CODE_SUCCESS;
14,001,157✔
3868
  int32_t  lino = 0;
14,001,157✔
3869
  int64_t* lastTs = NULL;
14,001,157✔
3870
  bool     noExists = false;
14,001,157✔
3871

3872
  while (true) {
3873
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
14,463,157✔
3874
    if (NULL == lastTs) {
14,463,157✔
3875
      if (noExists) {
608,812✔
3876
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
146,812✔
3877
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
146,812✔
3878
      }
3879

3880
      noExists = true;
462,000✔
3881
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
462,000✔
3882
      
3883
      continue;
462,000✔
3884
    }
3885

3886
    while (true) {
×
3887
      int64_t lastTsValue = atomic_load_64(lastTs);
13,854,345✔
3888
      if (pCtx->currTs > lastTsValue) {
13,854,345✔
3889
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
13,853,544✔
3890
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
13,853,544✔
3891
          return code;
13,853,544✔
3892
        }
3893

3894
        continue;
×
3895
      }
3896

3897
      return code;
801✔
3898
    }
3899

3900
    break;
3901
  }
3902

3903
_exit:
146,812✔
3904

3905
  if (code) {
146,812✔
3906
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
146,812✔
3907
  }
3908

3909
  return code;  
146,812✔
3910
}
3911

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

3925
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3926
    pStatus = (SStmStatus*)pIter;
×
3927

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

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

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

3959
int32_t msmWatchHandleEnding(SStmGrpCtx* pCtx, bool watchError) {
641✔
3960
  int32_t code = TSDB_CODE_SUCCESS;
641✔
3961
  int32_t lino = 0;
641✔
3962
  int32_t minVal = watchError ? 0 : 1;
641✔
3963

3964
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
641✔
3965
    return code;
×
3966
  }
3967

3968
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
641✔
3969
    (void)sched_yield();
×
3970
  }
3971

3972
  if (watchError) {
641✔
3973
    taosHashClear(mStreamMgmt.vgroupMap);
×
3974
    taosHashClear(mStreamMgmt.snodeMap);
×
3975
    taosHashClear(mStreamMgmt.taskMap);
×
3976
    taosHashClear(mStreamMgmt.streamMap);
×
3977
    mstInfo("watch error happends, clear all maps");
×
3978
    goto _exit;
×
3979
  }
3980

3981
  if (0 == atomic_load_8(&mStreamMgmt.watch.taskRemains)) {
641✔
3982
    mstInfo("no stream tasks remain during watch state");
641✔
3983
    goto _exit;
641✔
3984
  }
3985

3986
  msmWatchCheckStreamMap(pCtx);
×
3987

3988
_exit:
641✔
3989

3990
  mStreamMgmt.lastTaskId += 100000;
641✔
3991

3992
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
641✔
3993

3994
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
641✔
3995

3996
  if (code) {
641✔
3997
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3998
  }
3999

4000
  return code;
641✔
4001
}
4002

4003

4004
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
11,412✔
4005
  int32_t code = TSDB_CODE_SUCCESS;
11,412✔
4006
  int32_t lino = 0;
11,412✔
4007
  SStreamHbMsg* pReq = pCtx->pReq;
11,412✔
4008

4009
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
11,412✔
4010
  
4011
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
11,412✔
4012
    goto _exit;
×
4013
  }
4014

4015
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
11,412✔
4016
  if (GOT_SNODE(pReq->snodeId)) {
11,412✔
4017
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
2,258✔
4018
  }
4019

4020
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
11,412✔
4021
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
4022
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4023
  }
4024

4025
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
11,412✔
4026
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
641✔
4027
  }
4028

4029
_exit:
11,412✔
4030

4031
  atomic_sub_fetch_32(&mStreamMgmt.watch.processing, 1);
11,412✔
4032
  
4033
  if (code) {
11,412✔
4034
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4035

4036
    (void)msmWatchHandleEnding(pCtx, true);
×
4037
  }
4038

4039
  return code;
11,412✔
4040
}
4041

4042
int32_t msmGetTrigOReaderSize(SArray* pOReaders) {
4,761,701✔
4043
  int32_t listSize = taosArrayGetSize(pOReaders);
4,761,701✔
4044
  int32_t totalSize = 0;
4,761,701✔
4045
  
4046
  for (int32_t i = 0; i < listSize; ++i) {
5,828,639✔
4047
    SArray* pList = taosArrayGetP(pOReaders, i);
1,066,938✔
4048
    totalSize += taosArrayGetSize(pList);
1,066,938✔
4049
  }
4050

4051
  return totalSize;
4,761,701✔
4052
}
4053

4054
SStmTaskStatus* msmGetTrigOReader(SArray* pOReaders, int32_t idx) {
669,776✔
4055
  SArray* pList = taosArrayGetP(pOReaders, idx / MST_ORIGINAL_READER_LIST_SIZE);
669,776✔
4056
  if (NULL == pList) {
669,776✔
4057
    return NULL;
×
4058
  }
4059

4060
  return (SStmTaskStatus*)taosArrayGet(pList, idx % MST_ORIGINAL_READER_LIST_SIZE);
669,776✔
4061
}
4062

4063

4064
int32_t msmEnsureGetOReaderList(int64_t streamId, SStmStatus* pStatus, SArray** ppRes) {
57,693✔
4065
  int32_t code = TSDB_CODE_SUCCESS;
57,693✔
4066
  int32_t lino = 0;
57,693✔
4067

4068
  if (NULL == pStatus->trigOReaders) {
57,693✔
4069
    pStatus->trigOReaders = taosArrayInit(10, POINTER_BYTES);
49,966✔
4070
    TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
49,966✔
4071
  }
4072

4073
  while (true) {
49,966✔
4074
    SArray** ppOReaderList = taosArrayGetLast(pStatus->trigOReaders);
107,659✔
4075

4076
    if (NULL == ppOReaderList || (*ppOReaderList)->size >= (*ppOReaderList)->capacity) {
107,659✔
4077
      SArray* pOReaderList = taosArrayInit(MST_ORIGINAL_READER_LIST_SIZE, sizeof(SStmTaskStatus));
49,966✔
4078
      TSDB_CHECK_NULL(pOReaderList, code, lino, _exit, terrno);
49,966✔
4079

4080
      TSDB_CHECK_NULL(taosArrayPush(pStatus->trigOReaders, &pOReaderList), code, lino, _exit, terrno);
99,932✔
4081
      continue;
49,966✔
4082
    }
4083

4084
    *ppRes = *ppOReaderList;
57,693✔
4085
    break;
57,693✔
4086
  }
4087

4088
_exit:
57,693✔
4089

4090
  if (code) {
57,693✔
4091
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4092
  }
4093

4094
  return code;
57,693✔
4095
}
4096

4097
int32_t msmCheckDeployTrigReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int32_t vgNum) {
67,311✔
4098
  int32_t code = TSDB_CODE_SUCCESS;
67,311✔
4099
  int32_t lino = 0;
67,311✔
4100
  bool    readerExists = false;
67,311✔
4101
  int64_t streamId = pTask->streamId;
67,311✔
4102

4103
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
67,311✔
4104
  for (int32_t i = 0; i < readerNum; ++i) {
140,618✔
4105
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
82,925✔
4106
    if (pReader->id.nodeId == vgId) {
82,925✔
4107
      readerExists = true;
9,618✔
4108
      break;
9,618✔
4109
    }
4110
  }
4111

4112
  if (!readerExists) {
67,311✔
4113
    SArray* pReaderList = NULL;
57,693✔
4114
    TAOS_CHECK_EXIT(msmEnsureGetOReaderList(streamId, pStatus, &pReaderList));
57,693✔
4115
    
4116
    SStmTaskStatus* pState = taosArrayReserve(pReaderList, 1);
57,693✔
4117
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
57,693✔
4118
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
57,693✔
4119
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
57,693✔
4120
  }
4121

4122
_exit:
9,618✔
4123

4124
  if (code) {
67,311✔
4125
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4126
  }
4127

4128
  return code;
67,311✔
4129
}
4130

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

4149
  if (NULL == pCtx->pRsp->rsps.rspList) {
59,002✔
4150
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4151
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4152
  }
4153
  
4154
  TSWAP(pTask->pMgmtReq, pMgmtReq);
59,002✔
4155
  rsp.task = *(SStreamTask*)pTask;
59,002✔
4156

4157
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
59,002✔
4158
  if (NULL == pStatus) {
59,002✔
4159
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4160
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4161
  }
4162

4163
  if (rsp.reqId == pStatus->lastTrigMgmtReqId) {
59,002✔
4164
    mstsDebug("duplicated trigger oreader deploy msg, will ignore it, reqId %" PRId64, rsp.reqId);
×
4165
    goto _exit;
×
4166
  }
4167

4168
  atomic_store_64(&pStatus->lastTrigMgmtReqId, rsp.reqId); 
59,002✔
4169

4170
  stopped = atomic_load_8(&pStatus->stopped);
59,002✔
4171
  if (stopped) {
59,002✔
4172
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4173
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4174
  }
4175

4176
  if (tbNum <= 0) {
59,002✔
4177
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4178
    goto _exit;
×
4179
  }
4180

4181
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
59,002✔
4182
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
59,002✔
4183
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
59,002✔
4184

4185
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
59,002✔
4186
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
59,002✔
4187
  
4188
  for (int32_t i = 0; i < tbNum; ++i) {
234,829✔
4189
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
175,827✔
4190
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
175,827✔
4191
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
351,654✔
4192
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
175,827✔
4193
  }
4194

4195
  int32_t vgNum = tSimpleHashGetSize(pVgs);
59,002✔
4196
  while (true) {
4197
    p = tSimpleHashIterate(pVgs, p, &iter);
126,313✔
4198
    if (NULL == p) {
126,313✔
4199
      break;
59,002✔
4200
    }
4201
    
4202
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
67,311✔
4203
  }
4204
  
4205
  vgNum = msmGetTrigOReaderSize(pStatus->trigOReaders);
59,002✔
4206
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
59,002✔
4207
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
59,002✔
4208

4209
  SStreamTaskAddr addr;
59,002✔
4210
  for (int32_t i = 0; i < vgNum; ++i) {
116,695✔
4211
    SStmTaskStatus* pOTask = msmGetTrigOReader(pStatus->trigOReaders, i);
57,693✔
4212
    addr.taskId = pOTask->id.taskId;
57,693✔
4213
    addr.nodeId = pOTask->id.nodeId;
57,693✔
4214
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
57,693✔
4215
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
115,386✔
4216
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
57,693✔
4217
  }
4218

4219
_exit:
59,002✔
4220

4221
  tFreeSStreamMgmtReq(pMgmtReq);
59,002✔
4222
  taosMemoryFree(pMgmtReq);
59,002✔
4223

4224
  tSimpleHashCleanup(pVgs);
59,002✔
4225
  mstDestroyDbVgroupsHash(pDbVgroups);
59,002✔
4226

4227
  if (code) {
59,002✔
4228
    rsp.code = code;
×
4229
    
4230
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4231

4232
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4233
  } else {
4234
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
118,004✔
4235
  }
4236

4237
_final:
59,002✔
4238

4239
  if (finalCode) {
59,002✔
4240
    tFreeSStreamMgmtRsp(&rsp);
×
4241
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4242
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4243
  }
4244

4245
  return finalCode;
59,002✔
4246
}
4247

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

4262
_exit:
2,277✔
4263

4264
  if (code) {
2,277✔
4265
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4266
  }
4267

4268
  return code;
2,277✔
4269
}
4270

4271
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
2,277✔
4272
  int32_t code = TSDB_CODE_SUCCESS;
2,277✔
4273
  int32_t lino = 0;
2,277✔
4274
  bool    readerExists = false;
2,277✔
4275
  int64_t streamId = pTask->streamId;
2,277✔
4276
  SListNode* pNode = listHead(pStatus->calcReaders);
2,277✔
4277
  SStmTaskStatus* pReader = NULL;
2,277✔
4278
  int32_t taskIdx = 0;
2,277✔
4279

4280
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
2,277✔
4281
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
21,114✔
4282
    pReader = (SStmTaskStatus*)pNode->data;
18,837✔
4283
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
18,837✔
4284
      readerExists = true;
×
4285
      pAddr->taskId = pReader->id.taskId;
×
4286
      break;
×
4287
    }
4288
  }
4289

4290
  if (!readerExists) {
2,277✔
4291
    if (NULL == pStatus->calcReaders) {
2,277✔
4292
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4293
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
4294
      taskIdx = 0;
×
4295
    } else {
4296
      pNode = listTail(pStatus->calcReaders);
2,277✔
4297
      pReader = (SStmTaskStatus*)pNode->data;
2,277✔
4298
      taskIdx = pReader->id.taskIdx + 1;
2,277✔
4299
    }
4300

4301
    SStreamCalcScan* pScan = NULL;
2,277✔
4302
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
2,277✔
4303
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
2,277✔
4304
    pReader = tdListReserve(pStatus->calcReaders);
2,277✔
4305
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
2,277✔
4306
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
2,277✔
4307
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
2,277✔
4308
    pAddr->taskId = pReader->id.taskId;
2,277✔
4309
  }
4310

4311
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
2,277✔
4312
  pAddr->nodeId = vgId;
2,277✔
4313

4314
_exit:
2,277✔
4315

4316
  if (code) {
2,277✔
4317
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4318
  }
4319

4320
  return code;
2,277✔
4321
}
4322

4323

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

4341
  if (NULL == pCtx->pRsp->rsps.rspList) {
2,277✔
4342
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4343
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4344
  }
4345
  
4346
  TSWAP(pTask->pMgmtReq, pMgmtReq);
2,277✔
4347
  rsp.task = *(SStreamTask*)pTask;
2,277✔
4348

4349
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
2,277✔
4350
  if (NULL == pStatus) {
2,277✔
4351
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
4352
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4353
  }
4354

4355
  stopped = atomic_load_8(&pStatus->stopped);
2,277✔
4356
  if (stopped) {
2,277✔
4357
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
4358
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4359
  }
4360

4361
  if (reqNum <= 0) {
2,277✔
4362
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4363
    goto _exit;
×
4364
  }
4365

4366
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
2,277✔
4367
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
2,277✔
4368

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

4384
_exit:
2,277✔
4385

4386
  tFreeSStreamMgmtReq(pMgmtReq);
2,277✔
4387
  taosMemoryFree(pMgmtReq);
2,277✔
4388

4389
  if (code) {
2,277✔
4390
    rsp.code = code;
×
4391
    
4392
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4393
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4394
  } else {
4395
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
4,554✔
4396
  }
4397

4398

4399
_final:
2,277✔
4400

4401
  if (finalCode) {
2,277✔
4402
    tFreeSStreamMgmtRsp(&rsp);
×
4403
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
4404
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4405
  }
4406

4407
  return finalCode;
2,277✔
4408
}
4409

4410

4411
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
61,279✔
4412
  int32_t code = TSDB_CODE_SUCCESS;
61,279✔
4413
  int32_t lino = 0;
61,279✔
4414

4415
  switch (pTask->pMgmtReq->type) {
61,279✔
4416
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
59,002✔
4417
      msmDeployTriggerOrigReader(pCtx, pTask);
59,002✔
4418
      break;
59,002✔
4419
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
2,277✔
4420
      msmDeployRunnerOrigReader(pCtx, pTask);
2,277✔
4421
      break;
2,277✔
4422
    default:
×
4423
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4424
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4425
      break;
×
4426
  }
4427

4428
_exit:
61,279✔
4429

4430
  if (code) {
61,279✔
4431
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4432
  }
4433

4434
  return code;
61,279✔
4435
}
4436

4437
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
16,988✔
4438
  int32_t code = TSDB_CODE_SUCCESS;
16,988✔
4439
  int32_t lino = 0;
16,988✔
4440
  SStreamHbMsg* pReq = pCtx->pReq;
16,988✔
4441
  SStmTaskStatusMsg* pTask = NULL;
16,988✔
4442
  
4443
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
16,988✔
4444
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
16,988✔
4445
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
16,988✔
4446
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
16,988✔
4447
  }
4448
  
4449
  for (int32_t i = 0; i < reqNum; ++i) {
78,267✔
4450
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
61,279✔
4451
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
61,279✔
4452
    if (NULL == pTask) {
61,279✔
4453
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
4454
      continue;
×
4455
    }
4456

4457
    if (NULL == pTask->pMgmtReq) {
61,279✔
4458
      msttError("idx %d without mgmtReq", idx);
×
4459
      continue;
×
4460
    }
4461

4462
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
61,279✔
4463
  }
4464

4465
_exit:
16,988✔
4466

4467
  if (code) {
16,988✔
4468
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4469
  }
4470

4471
  return code;
16,988✔
4472
}
4473

4474
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
13,989,745✔
4475
  int32_t code = TSDB_CODE_SUCCESS;
13,989,745✔
4476
  int32_t lino = 0;
13,989,745✔
4477
  SStreamHbMsg* pReq = pCtx->pReq;
13,989,745✔
4478

4479
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
13,989,745✔
4480
  if (GOT_SNODE(pReq->snodeId)) {
13,842,933✔
4481
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
790,870✔
4482
  }
4483
  
4484
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
13,842,933✔
4485
    msmHandleStreamActions(pCtx);
28,730✔
4486
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
28,730✔
4487
  }
4488

4489
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
13,842,933✔
4490
    code = msmHandleStreamRequests(pCtx);
16,988✔
4491
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
16,988✔
4492
    TAOS_CHECK_EXIT(code);
16,988✔
4493
  }
4494

4495
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
13,842,933✔
4496
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
45,274✔
4497
  } else {
4498
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
13,797,659✔
4499
  }
4500

4501
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
13,842,933✔
4502
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
57,836✔
4503
  }
4504

4505
  if (taosHashGetSize(pCtx->deployStm) > 0) {
13,842,933✔
4506
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
46,061✔
4507
  }
4508

4509
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
13,842,933✔
4510
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
709,903✔
4511
  }
4512

4513
  if (taosHashGetSize(pCtx->actionStm) > 0) {
13,842,933✔
4514
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
89,646✔
4515
  }
4516

4517
_exit:
13,842,933✔
4518

4519
  if (code) {
13,989,745✔
4520
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
146,812✔
4521
  }
4522

4523
  return code;
13,989,745✔
4524
}
4525

4526
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
14,082,679✔
4527
  int32_t lino = 0;
14,082,679✔
4528
  int32_t tlen = 0;
14,082,679✔
4529
  void   *buf = NULL;
14,082,679✔
4530

4531
  if (TSDB_CODE_SUCCESS != code) {
14,082,679✔
4532
    goto _exit;
146,812✔
4533
  }
4534

4535
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
13,935,867✔
4536
  if (code < 0) {
13,935,867✔
4537
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4538
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4539
  }
4540

4541
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
13,935,867✔
4542
  if (buf == NULL) {
13,935,867✔
4543
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
4544
    TAOS_CHECK_EXIT(terrno);    
×
4545
  }
4546

4547
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
13,935,867✔
4548
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
13,935,867✔
4549

4550
  SEncoder encoder;
13,934,048✔
4551
  tEncoderInit(&encoder, abuf, tlen);
13,935,867✔
4552
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
13,935,867✔
4553
    rpcFreeCont(buf);
×
4554
    buf = NULL;
×
4555
    tEncoderClear(&encoder);
×
4556
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4557
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4558
  }
4559
  tEncoderClear(&encoder);
13,935,867✔
4560

4561
_exit:
14,082,679✔
4562

4563
  pMsg->code = code;
14,082,679✔
4564
  pMsg->info = *pRpcInfo;
14,082,679✔
4565
  if (TSDB_CODE_SUCCESS == code) {
14,082,679✔
4566
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
13,935,867✔
4567
    pMsg->pCont = buf;
13,935,867✔
4568
  }
4569
}
14,082,679✔
4570

4571

4572
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
14,082,679✔
4573
  int32_t code = TSDB_CODE_SUCCESS;
14,082,679✔
4574
  SMStreamHbRspMsg rsp = {0};
14,082,679✔
4575
  rsp.streamGId = pHb->streamGId;
14,082,679✔
4576

4577
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
14,082,679✔
4578

4579
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
14,082,679✔
4580
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
81,522✔
4581
    goto _exit;
81,522✔
4582
  }
4583

4584
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,001,157✔
4585
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
14,001,157✔
4586

4587
  pCtx->tidx = tidx;
14,001,157✔
4588
  pCtx->pMnode = pMnode;
14,001,157✔
4589
  pCtx->currTs = currTs;
14,001,157✔
4590
  pCtx->pReq = pHb;
14,001,157✔
4591
  pCtx->pRsp = &rsp;
14,001,157✔
4592
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
14,001,157✔
4593
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
14,001,157✔
4594
  
4595
  switch (atomic_load_8(&mStreamMgmt.state)) {
14,001,157✔
4596
    case MND_STM_STATE_WATCH:
11,412✔
4597
      code = msmWatchHandleHbMsg(pCtx);
11,412✔
4598
      break;
11,412✔
4599
    case MND_STM_STATE_NORMAL:
13,989,745✔
4600
      code = msmNormalHandleHbMsg(pCtx);
13,989,745✔
4601
      break;
13,989,745✔
4602
    default:
×
4603
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
4604
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4605
      break;
×
4606
  }
4607

4608
_exit:
14,082,679✔
4609

4610
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
14,082,679✔
4611

4612
  msmCleanStreamGrpCtx(pHb);
14,082,679✔
4613
  msmClearStreamToDeployMaps(pHb);
14,082,679✔
4614

4615
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
14,082,679✔
4616
  
4617
  tFreeSMStreamHbRspMsg(&rsp);
14,082,679✔
4618

4619
  return code;
14,082,679✔
4620
}
4621

4622
void msmHandleBecomeLeader(SMnode *pMnode) {
372,028✔
4623
  if (tsDisableStream) {
372,028✔
4624
    return;
×
4625
  }
4626

4627
  mstInfo("start to process mnode become leader");
372,028✔
4628

4629
  int32_t code = 0;
372,028✔
4630
  streamAddVnodeLeader(MNODE_HANDLE);
372,028✔
4631
  
4632
  taosWLockLatch(&mStreamMgmt.runtimeLock);
372,028✔
4633
  msmDestroyRuntimeInfo(pMnode);
372,028✔
4634
  code = msmInitRuntimeInfo(pMnode);
372,028✔
4635
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
372,028✔
4636

4637
  if (TSDB_CODE_SUCCESS == code) {
372,028✔
4638
    atomic_store_8(&mStreamMgmt.active, 1);
372,028✔
4639
  }
4640

4641
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
372,028✔
4642
}
4643

4644
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
458,712✔
4645
  if (tsDisableStream) {
458,712✔
4646
    return;
×
4647
  }
4648

4649
  mstInfo("start to process mnode become not leader");
458,712✔
4650

4651
  streamRemoveVnodeLeader(MNODE_HANDLE);
458,712✔
4652

4653
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
458,712✔
4654
    taosWLockLatch(&mStreamMgmt.runtimeLock);
372,028✔
4655
    msmDestroyRuntimeInfo(pMnode);
372,028✔
4656
    mStreamMgmt.stat.inactiveTimes++;
372,028✔
4657
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
372,028✔
4658
  }
4659
}
4660

4661

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

4671
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
7,276✔
4672
  int32_t code = TSDB_CODE_SUCCESS;
7,276✔
4673
  int32_t lino = 0;
7,276✔
4674
  SStreamObj* pStream = pObj;
7,276✔
4675
  SSnodeObj* pSnode = p1;
7,276✔
4676
  SArray** ppRes = p2;
7,276✔
4677

4678
  if (pStream->mainSnodeId == pSnode->id) {
7,276✔
4679
    if (NULL == *ppRes) {
4,970✔
4680
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,002✔
4681
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
1,002✔
4682
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
1,002✔
4683
    }
4684

4685
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
9,940✔
4686
  }
4687

4688
  return true;
7,276✔
4689

4690
_exit:
×
4691

4692
  if (code) {
×
4693
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4694
  }  
4695

4696
  *(int32_t*)p3 = code;
×
4697

4698
  return false;
×
4699
}
4700

4701

4702
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
39,472✔
4703
  int32_t code = TSDB_CODE_SUCCESS;
39,472✔
4704
  int32_t lino = 0;
39,472✔
4705
  
4706
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
39,472✔
4707
  TAOS_CHECK_EXIT(code);
39,472✔
4708

4709
  int32_t streamNum = taosArrayGetSize(*ppRes);
39,472✔
4710
  if (streamNum > 0 && 0 == pSnode->replicaId) {
39,472✔
4711
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
194✔
4712
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
194✔
4713
  }
4714

4715
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4716

4717
_exit:
39,472✔
4718

4719
  if (code) {
39,472✔
4720
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
194✔
4721
  }  
4722

4723
  return code;
39,472✔
4724
}
4725

4726
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
399,633✔
4727
  SStreamObj* pStream = pObj;
399,633✔
4728
  int64_t streamId = pStream->pCreate->streamId;
399,633✔
4729
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
399,633✔
4730
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
399,633✔
4731
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
399,633✔
4732
  
4733
  if ((userDropped || userStopped) && (NULL == pStatus)) {
399,633✔
4734
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
388✔
4735
    return true;
388✔
4736
  }
4737
  
4738
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
399,245✔
4739
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
302,492✔
4740
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4741
    return true;
302,492✔
4742
  }
4743

4744
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
96,753✔
4745
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
11,495✔
4746
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4747
    return true;
11,495✔
4748
  }  
4749

4750
  if (pStatus) {
85,258✔
4751
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
84,835✔
4752
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
×
4753
    }
4754

4755
    return true;
84,835✔
4756
  }
4757

4758
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
423✔
4759

4760
  return true;
423✔
4761
}
4762

4763
void msmCheckLoopStreamMap(SMnode *pMnode) {
34,739✔
4764
  SStmStatus* pStatus = NULL;
34,739✔
4765
  void* pIter = NULL;
34,739✔
4766
  int8_t stopped = 0;
34,739✔
4767
  int64_t streamId = 0;
34,739✔
4768
  
4769
  while (true) {
4770
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
261,401✔
4771
    if (NULL == pIter) {
261,401✔
4772
      break;
34,739✔
4773
    }
4774

4775
    pStatus = (SStmStatus*)pIter;
226,662✔
4776

4777
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
226,662✔
4778
    stopped = atomic_load_8(&pStatus->stopped);
226,662✔
4779
    if (MST_IS_USER_STOPPED(stopped)) {
226,662✔
4780
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
1,960✔
4781
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
1,960✔
4782
      continue;
1,960✔
4783
    }
4784

4785
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
224,702✔
4786
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
4787
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
4788
      continue;
×
4789
    }
4790

4791
    if (MST_IS_ERROR_STOPPED(stopped)) {
224,702✔
4792
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
11,118✔
4793
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
7,666✔
4794
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4795
            
4796
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
7,666✔
4797
        continue;
7,666✔
4798
      }
4799

4800
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
3,452✔
4801
      continue;
3,452✔
4802
    }
4803

4804
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
213,584✔
4805
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4806
      continue;
×
4807
    }
4808
  }
4809
}
34,739✔
4810

4811
void msmCheckStreamsStatus(SMnode *pMnode) {
242,873✔
4812
  SStmCheckStatusCtx ctx = {0};
242,873✔
4813

4814
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
242,873✔
4815
  
4816
  if (MST_READY_FOR_SDB_LOOP()) {
242,873✔
4817
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
48,304✔
4818
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
48,304✔
4819
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
48,304✔
4820
  }
4821

4822
  if (MST_READY_FOR_MAP_LOOP()) {
242,873✔
4823
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
34,739✔
4824
    msmCheckLoopStreamMap(pMnode);
34,739✔
4825
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
34,739✔
4826
  }
4827
}
242,873✔
4828

4829
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
2,361,588✔
4830
  for (int32_t i = 0; i < taskNum; ++i) {
4,836,768✔
4831
    SStmTaskStatus* pTask = *(pList + i);
2,475,354✔
4832

4833
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
2,475,354✔
4834
      continue;
65,752✔
4835
    }
4836
    
4837
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
2,411,380✔
4838
      continue;
2,410,510✔
4839
    }
4840

4841
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
870✔
4842
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
870✔
4843
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
174✔
4844
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4845
          
4846
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
174✔
4847
      break;
174✔
4848
    }
4849

4850
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
696✔
4851
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4852

4853
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
696✔
4854
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
696✔
4855

4856
    SStmTaskAction task = {0};
696✔
4857
    task.streamId = streamId;
696✔
4858
    task.id = pTask->id;
696✔
4859
    task.flag = pTask->flags;
696✔
4860
    task.type = pTask->type;
696✔
4861
    
4862
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
696✔
4863
  }
4864
}
2,361,588✔
4865

4866
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
103,000✔
4867
  void* pIter = NULL;
103,000✔
4868
  SStmVgStreamStatus* pVg = NULL;
103,000✔
4869
  int64_t streamId = 0;
103,000✔
4870
  
4871
  while (true) {
899,516✔
4872
    pIter = taosHashIterate(pStreams, pIter);
1,002,516✔
4873
    if (NULL == pIter) {
1,002,516✔
4874
      break;
103,000✔
4875
    }
4876

4877
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
899,516✔
4878
    pVg = (SStmVgStreamStatus*)pIter;
899,516✔
4879

4880
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
899,516✔
4881
    if (taskNum > 0) {
899,516✔
4882
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
537,496✔
4883
    }
4884

4885
    taskNum = taosArrayGetSize(pVg->calcReaders);
899,516✔
4886
    if (taskNum > 0) {
899,516✔
4887
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
568,141✔
4888
    }
4889
  }
4890
}
103,000✔
4891

4892
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
4893
  int64_t streamId = 0;
×
4894
  void* pIter = NULL;
×
4895
  SStmVgStreamStatus* pStream = NULL;
×
4896

4897
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
4898
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4899
    return;
×
4900
  }
4901

4902
  
4903
  while (true) {
4904
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
4905
    if (NULL == pIter) {
×
4906
      break;
×
4907
    }
4908

4909
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4910
    
4911
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4912
  }
4913

4914
  taosHashClear(pVg->streamTasks);
×
4915
}
4916

4917

4918
void msmCheckVgroupStatus(SMnode *pMnode) {
242,873✔
4919
  void* pIter = NULL;
242,873✔
4920
  int32_t code = 0;
242,873✔
4921
  
4922
  while (true) {
974,145✔
4923
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,217,018✔
4924
    if (NULL == pIter) {
1,217,018✔
4925
      break;
242,873✔
4926
    }
4927

4928
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
974,145✔
4929
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
974,145✔
4930
      continue;
869,357✔
4931
    }
4932
    
4933
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
104,788✔
4934

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

4953
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
103,000✔
4954

4955
    msmCheckVgroupStreamStatus(pVg->streamTasks);
103,000✔
4956
  }
4957
}
242,873✔
4958

4959
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
404✔
4960
  *deployNum = 0;
404✔
4961
  
4962
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,156✔
4963
    if (pStream->runners[i]) {
982✔
4964
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
404✔
4965
      for (int32_t t = 0; t < taskNum; ++t) {
578✔
4966
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
404✔
4967
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
404✔
4968
        if (stopped) {
404✔
4969
          mstsDebug("stream already stopped %d, ignore it", stopped);
230✔
4970
          *deployNum = 0;
230✔
4971
          return;
230✔
4972
        }
4973

4974
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
174✔
4975
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
174✔
4976
      }
4977
      
4978
      deployId[*deployNum] = i;
174✔
4979
      (*deployNum)++;
174✔
4980
    }
4981
  }
4982
}
4983

4984
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
2,878✔
4985
  pSnode->runnerThreadNum = -1;
2,878✔
4986

4987
  (void)msmSTAddSnodesToMap(pMnode);
2,878✔
4988

4989
  int64_t streamId = 0;
2,878✔
4990
  void* pIter = NULL;
2,878✔
4991
  SStmSnodeStreamStatus* pStream = NULL;
2,878✔
4992
  int32_t deployNum = 0;
2,878✔
4993
  SStmTaskAction task = {0};
2,878✔
4994
  
4995
  while (true) {
4996
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
4,550✔
4997
    if (NULL == pIter) {
4,550✔
4998
      break;
2,878✔
4999
    }
5000

5001
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,672✔
5002
    
5003
    task.streamId = streamId;
1,672✔
5004
    
5005
    pStream = (SStmSnodeStreamStatus*)pIter;
1,672✔
5006
    if (pStream->trigger) {
1,672✔
5007
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
1,268✔
5008
      if (stopped) {
1,268✔
5009
        mstsDebug("stream already stopped %d, ignore it", stopped);
690✔
5010
        continue;
690✔
5011
      }
5012

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

5031
  taosHashClear(pSnode->streamTasks);
2,878✔
5032
}
2,878✔
5033

5034

5035
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
34,305✔
5036
  void* pIter = NULL;
34,305✔
5037
  SStmSnodeStreamStatus* pSnode = NULL;
34,305✔
5038
  int64_t streamId = 0;
34,305✔
5039
  
5040
  while (true) {
5041
    pIter = taosHashIterate(pStreams, pIter);
365,536✔
5042
    if (NULL == pIter) {
365,536✔
5043
      break;
34,305✔
5044
    }
5045

5046
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
331,231✔
5047
    pSnode = (SStmSnodeStreamStatus*)pIter;
331,231✔
5048

5049
    if (NULL != pSnode->trigger) {
331,231✔
5050
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
321,665✔
5051
    }
5052

5053
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,324,924✔
5054
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
993,693✔
5055
      if (taskNum > 0) {
993,693✔
5056
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
934,286✔
5057
      }
5058
    }
5059
  }
5060
}
34,305✔
5061

5062

5063
void msmCheckSnodeStatus(SMnode *pMnode) {
242,873✔
5064
  void* pIter = NULL;
242,873✔
5065
  
5066
  while (true) {
317,183✔
5067
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
560,056✔
5068
    if (NULL == pIter) {
560,056✔
5069
      break;
242,873✔
5070
    }
5071

5072
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
317,183✔
5073
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
317,183✔
5074
      continue;
278,304✔
5075
    }
5076

5077
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
38,879✔
5078
    
5079
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
38,879✔
5080
    if (NULL == pSnode->streamTasks) {
38,879✔
5081
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
2,330✔
5082
      continue;
2,330✔
5083
    }
5084
    
5085
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
36,549✔
5086
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
2,244✔
5087
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5088
      
5089
      msmHandleSnodeLost(pMnode, pSnode);
2,244✔
5090
      continue;
2,244✔
5091
    }
5092
    
5093
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
34,305✔
5094

5095
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
34,305✔
5096
  }
5097
}
242,873✔
5098

5099

5100
void msmCheckTasksStatus(SMnode *pMnode) {
242,873✔
5101
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
242,873✔
5102

5103
  msmCheckVgroupStatus(pMnode);
242,873✔
5104
  msmCheckSnodeStatus(pMnode);
242,873✔
5105
}
242,873✔
5106

5107
void msmCheckSnodesState(SMnode *pMnode) {
242,873✔
5108
  if (!MST_READY_FOR_SNODE_LOOP()) {
242,873✔
5109
    return;
213,600✔
5110
  }
5111

5112
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
29,273✔
5113

5114
  void* pIter = NULL;
29,273✔
5115
  int32_t snodeId = 0;
29,273✔
5116
  while (true) {
33,139✔
5117
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
62,412✔
5118
    if (NULL == pIter) {
62,412✔
5119
      break;
29,273✔
5120
    }
5121

5122
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
33,139✔
5123
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
33,139✔
5124
      continue;
32,505✔
5125
    }
5126

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

5140
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
29,273✔
5141
}
5142

5143
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
7,980,410✔
5144
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
7,980,410✔
5145
  if (0 == active || MND_STM_STATE_NORMAL != state) {
7,980,410✔
5146
    mstTrace("ignore health check since active:%d state:%d", active, state);
1,923✔
5147
    return false;
1,923✔
5148
  }
5149

5150
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,978,487✔
5151
    mstTrace("ignore health check since no stream now");
7,492,741✔
5152
    return false;
7,492,741✔
5153
  }
5154

5155
  return true;
485,746✔
5156
}
5157

5158
void msmHealthCheck(SMnode *pMnode) {
7,737,537✔
5159
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,737,537✔
5160
    return;
7,494,664✔
5161
  }
5162

5163
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
458,813✔
5164
  
5165
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
242,873✔
5166
  if (!msmCheckNeedHealthCheck(pMnode)) {
242,873✔
5167
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
5168
    return;
×
5169
  }
5170
  
5171
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
242,873✔
5172
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
242,873✔
5173

5174
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
242,873✔
5175
  
5176
  msmCheckStreamsStatus(pMnode);
242,873✔
5177
  msmCheckTasksStatus(pMnode);
242,873✔
5178
  msmCheckSnodesState(pMnode);
242,873✔
5179

5180
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
242,873✔
5181

5182
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
242,873✔
5183
}
5184

5185
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
827✔
5186
  SStreamObj *pStream = pObj;
827✔
5187
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
827✔
5188
    return true;
×
5189
  }
5190
  
5191
  pStream->updateTime = *(int64_t*)p1;
827✔
5192
  
5193
  (*(int32_t*)p2)++;
827✔
5194
  
5195
  return true;
827✔
5196
}
5197

5198
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
5199
  int32_t code = 0;
×
5200
  int8_t  stopped = 0;
×
5201
  
5202
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5203
  
5204
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
5205
  if (NULL == pStatus) {
×
5206
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
5207
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5208
    goto _exit;
×
5209
  }
5210

5211
  stopped = atomic_load_8(&pStatus->stopped);
×
5212
  if (stopped) {
×
5213
    mstsError("stream already stopped, stopped:%d", stopped);
×
5214
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
5215
    goto _exit;
×
5216
  }
5217

5218
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
5219
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5220
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
5221
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5222
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5223
    goto _exit;
×
5224
  }
5225

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

5229
_exit:
×
5230
  
5231
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5232

5233
  return code;
×
5234
}
5235

5236
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
372,028✔
5237
  int32_t code = TSDB_CODE_SUCCESS;
372,028✔
5238
  int32_t lino = 0;
372,028✔
5239
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
372,028✔
5240
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
372,028✔
5241
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
372,028✔
5242

5243
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
743,974✔
5244

5245
  mStreamMgmt.stat.activeTimes++;
372,028✔
5246
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
372,028✔
5247
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
372,028✔
5248
  if (NULL == mStreamMgmt.tCtx) {
372,028✔
5249
    code = terrno;
×
5250
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5251
    goto _exit;
×
5252
  }
5253

5254
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
372,028✔
5255
  if (mStreamMgmt.actionQ == NULL) {
372,028✔
5256
    code = terrno;
×
5257
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
5258
    goto _exit;
×
5259
  }
5260
  
5261
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
372,028✔
5262
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
372,028✔
5263
  
5264
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
372,028✔
5265
  
5266
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,231,974✔
5267
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
1,859,946✔
5268

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

5311
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
372,028✔
5312
  if (mStreamMgmt.snodeMap == NULL) {
372,028✔
5313
    code = terrno;
×
5314
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
5315
    goto _exit;
×
5316
  }
5317
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
372,028✔
5318
  
5319
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
372,028✔
5320
  if (mStreamMgmt.dnodeMap == NULL) {
372,028✔
5321
    code = terrno;
×
5322
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5323
    goto _exit;
×
5324
  }
5325

5326
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
372,028✔
5327
  if (mStreamMgmt.toDeployVgMap == NULL) {
372,028✔
5328
    code = terrno;
×
5329
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
5330
    goto _exit;
×
5331
  }
5332
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
372,028✔
5333
  
5334
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
372,028✔
5335
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
372,028✔
5336
    code = terrno;
×
5337
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
5338
    goto _exit;
×
5339
  }
5340
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
372,028✔
5341

5342
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
372,028✔
5343
  if (mStreamMgmt.toUpdateScanMap == NULL) {
372,028✔
5344
    code = terrno;
×
5345
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5346
    goto _exit;
×
5347
  }
5348
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
372,028✔
5349

5350
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
372,028✔
5351
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
372,028✔
5352

5353
  mStreamMgmt.lastTaskId = 1;
372,028✔
5354

5355
  int32_t activeStreamNum = 0;
372,028✔
5356
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
372,028✔
5357

5358
  if (activeStreamNum > 0) {
372,028✔
5359
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
641✔
5360
  } else {
5361
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
371,387✔
5362
  }
5363

5364
_exit:
372,028✔
5365

5366
  if (code) {
372,028✔
5367
    msmDestroyRuntimeInfo(pMnode);
×
5368
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5369
  } else {
5370
    mstInfo("mnode stream runtime init done");
372,028✔
5371
  }
5372

5373
  return code;
372,028✔
5374
}
5375

5376

5377

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