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

taosdata / TDengine / #4869

26 Nov 2025 05:46AM UTC coverage: 64.539% (-0.09%) from 64.629%
#4869

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

771 of 945 new or added lines in 33 files covered. (81.59%)

3214 existing lines in 124 files now uncovered.

158203 of 245129 relevant lines covered (64.54%)

113224023.06 hits per line

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

76.39
/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() {
829,458✔
34
  SStmQNode* pQNode = NULL;
829,458✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
829,458✔
37
    return;
414,729✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,073,285✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,439,710✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,366,425✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,366,425✔
51
  }
52
}
2,073,285✔
53

54
void msmDestroyThreadCtxs() {
829,458✔
55
  if (NULL == mStreamMgmt.tCtx) {
829,458✔
56
    return;
414,729✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,488,014✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,073,285✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
414,729✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
829,458✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
829,458✔
94
}
829,458✔
95

96

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

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

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

111
    pStatus = pStream;
2,960✔
112
  }
113

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

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

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

136
_exit:
393✔
137

138
  taosHashRelease(mStreamMgmt.streamMap, pStream);
2,960✔
139

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

145

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

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

200
  pSnode = NULL;
448,143✔
201

202
_exit:
448,143✔
203

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

208
  return code;
448,143✔
209
}
210

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

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

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

235
  pDnode = NULL;
910,597✔
236

237
_exit:
910,597✔
238

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

243
  return code;
910,597✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
593,183✔
275
  int32_t code = TSDB_CODE_SUCCESS;
593,183✔
276
  int32_t lino = 0;
593,183✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
593,183✔
278
  if (NULL == pStream) {
593,183✔
279
    SStmVgStreamStatus stream = {0};
453,568✔
280
    if (trigReader) {
453,568✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
285,991✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
285,991✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
571,982✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
167,577✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
167,577✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
335,154✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
453,568✔
290
    goto _exit;
453,568✔
291
  }
292
  
293
  if (trigReader) {
139,615✔
294
    if (NULL == pStream->trigReaders) {
23,449✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
23,449✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
23,449✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
46,898✔
300
    goto _exit;
23,449✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
116,166✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
83,322✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
83,322✔
306
  }
307

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

310
_exit:
116,166✔
311

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

320
  return code;
593,183✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
92,585✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
92,585✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
92,585✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
500,598✔
339
  }
340
  
341
_exit:
592,658✔
342

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

350
  return code;
593,183✔
351
}
352

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

361
  while (true) {
×
362
    SStmVgTasksToDeploy* pVg = taosHashAcquire(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId));
595,319✔
363
    if (NULL == pVg) {
595,319✔
364
      vg.taskList = taosArrayInit(20, sizeof(SStmTaskToDeployExt));
169,937✔
365
      TSDB_CHECK_NULL(vg.taskList, code, lino, _return, terrno);
169,937✔
366
      TSDB_CHECK_NULL(taosArrayPush(vg.taskList, &ext), code, lino, _return, terrno);
339,874✔
367
      code = taosHashPut(pVgMap, &pDeploy->task.nodeId, sizeof(pDeploy->task.nodeId), &vg, sizeof(SStmVgTasksToDeploy));
169,937✔
368
      if (TSDB_CODE_SUCCESS == code) {
169,937✔
369
        goto _return;
169,937✔
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);
425,382✔
381
    if (NULL == pVg->taskList) {
425,382✔
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)) {
850,764✔
386
      taosWUnLockLatch(&pVg->lock);
×
387
      TSDB_CHECK_NULL(NULL, code, lino, _return, terrno);
×
388
    }
389
    taosWUnLockLatch(&pVg->lock);
425,382✔
390
    
391
    taosHashRelease(pVgMap, pVg);
425,382✔
392
    break;
425,382✔
393
  }
394
  
395
_return:
595,319✔
396

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

404
  return code;
595,319✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
818,025✔
409
  int32_t code = TSDB_CODE_SUCCESS;
818,025✔
410
  int32_t lino = 0;
818,025✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
818,025✔
412
  if (NULL == pStream) {
818,025✔
413
    SStmSnodeStreamStatus stream = {0};
209,204✔
414
    if (deployId < 0) {
209,204✔
415
      stream.trigger = pStatus;
3,406✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
205,798✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
205,798✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
411,596✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
209,204✔
423
    goto _exit;
209,204✔
424
  }
425
  
426
  if (deployId < 0) {
608,821✔
427
    if (NULL != pStream->trigger) {
195,247✔
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;
195,247✔
433
    goto _exit;
195,247✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
413,574✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
380,352✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
380,352✔
439
  }
440

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

443
_exit:
413,574✔
444

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

453
  return code;
818,025✔
454
}
455

456

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

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

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

484
  return code;
818,025✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
198,653✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
50,820✔
568
      ext.deployed = false;
50,820✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
101,640✔
570

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

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

614
  return code;
619,372✔
615
}
616

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

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

632
_exit:
586,150✔
633

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

638
  return code;
586,150✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
17,616✔
676
    }
677

678
    break;
679
  }
680

681
_exit:
427✔
682

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

687
  return code;  
427✔
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
94,735✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
14,383,175✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
55,355,837✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
40,972,662✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
40,972,662✔
725
  }
726

727
_exit:
14,383,175✔
728

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

733
  return code;
14,383,175✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
383,032✔
739
  int32_t num = taosArrayGetSize(pList);
383,032✔
740
  for (int32_t i = 0; i < num; ++i) {
1,016,332✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
844,882✔
742
    if (pScan->readFromCache) {
844,882✔
743
      return pScan->scanPlan;
211,582✔
744
    }
745
  }
746

747
  return NULL;
171,450✔
748
}
749

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

774
  return TSDB_CODE_SUCCESS;
595,319✔
775
}
776

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

781
  if (pInfo->runnerDeploys > 0) {
195,247✔
782
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
195,247✔
783
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
195,247✔
784
  }
785
  
786
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
780,988✔
787
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
585,741✔
788
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
585,741✔
789

790
    if (!STREAM_IS_TOP_RUNNER(pStatus->flags)) {
585,741✔
UNCOV
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);
UNCOV
793
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
794
    }
795
    
796
    SStreamRunnerTarget runner;
576,291✔
797
    runner.addr.taskId = pStatus->id.taskId;
585,741✔
798
    runner.addr.nodeId = pStatus->id.nodeId;
585,741✔
799
    runner.addr.epset = mndGetDnodeEpsetById(pMnode, pStatus->id.nodeId);
585,741✔
800
    runner.execReplica = pInfo->runnerReplica; 
585,741✔
801
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &runner), code, lino, _exit, terrno);
1,171,482✔
802
    mstsDebug("the %dth runner target added to trigger's runnerList, TASK:%" PRIx64 , i, runner.addr.taskId);
585,741✔
803
  }
804

805
_exit:
195,247✔
806

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

UNCOV
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
  
UNCOV
823
  pInfo->leaderSnodeId = leaderSnodeId;
×
824
  pInfo->replicaSnodeId = pSnode->replicaId;
×
825

UNCOV
826
  mndReleaseSnode(pMnode, pSnode);
×
827

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

UNCOV
833
  return TSDB_CODE_SUCCESS;
×
834
}
835

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

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

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

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

875
  SStreamTaskAddr addr;
195,503✔
876
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
198,653✔
877
  if (triggerReaderNum > 0) {
198,653✔
878
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
196,741✔
879
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
196,741✔
880
  }
881
  
882
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
444,047✔
883
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
245,394✔
884
    addr.taskId = pStatus->id.taskId;
245,394✔
885
    addr.nodeId = pStatus->id.nodeId;
245,394✔
886
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
245,394✔
887
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
490,788✔
888
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
245,394✔
889
  }
890

891
  pMsg->leaderSnodeId = pStream->mainSnodeId;
198,653✔
892
  pMsg->streamName = pInfo->streamName;
198,653✔
893

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

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

901
_exit:
195,247✔
902

903
  if (code) {
195,247✔
UNCOV
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));
195,247✔
907
  }
908
  
909
  return TSDB_CODE_SUCCESS;
195,247✔
910
}
911

912

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

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

944
_exit:
619,372✔
945

946
  if (code) {
619,372✔
UNCOV
947
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
948
  }
949
  
950
  return code;
619,372✔
951
}
952

953

954
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
465,664✔
955
  int32_t code = TSDB_CODE_SUCCESS;
465,664✔
956
  int32_t lino = 0;
465,664✔
957
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
465,664✔
958
  SListNode* pNode = pList ? listHead(pList) : NULL;
465,664✔
959
  
960
  for (int32_t i = 0; i < taskNum; ++i) {
1,058,847✔
961
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
593,183✔
962
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
593,183✔
963
    if (pNode) {
593,183✔
964
      pNode = TD_DLIST_NODE_NEXT(pNode);
279,431✔
965
    }
966
  }
967
  
968
_exit:
465,664✔
969

970
  if (code) {
465,664✔
UNCOV
971
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
972
  }
973
  
974
  return code;
465,664✔
975
}
976

977

978
static int32_t msmSTAddToSnodeMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SStmTaskStatus* pTask, int32_t taskNum, int32_t deployId) {
784,803✔
979
  int32_t code = TSDB_CODE_SUCCESS;
784,803✔
980
  int32_t lino = 0;
784,803✔
981
  int32_t rtaskNum = (taskNum > 0) ? taskNum : taosArrayGetSize(pTasks);
784,803✔
982
  int32_t taskType = (deployId < 0) ? STREAM_TRIGGER_TASK : STREAM_RUNNER_TASK;
784,803✔
983
  
984
  for (int32_t i = 0; i < rtaskNum; ++i) {
1,602,828✔
985
    SStmTaskStatus* pStatus = (taskNum > 0) ? (pTask + i) : taosArrayGet(pTasks, i);
818,025✔
986
    TAOS_CHECK_EXIT(msmSTAddToSnodeMapImpl(streamId, pStatus, deployId));
818,025✔
987
  }
988
  
989
_exit:
784,803✔
990

991
  if (code) {
784,803✔
UNCOV
992
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
993
  }
994

995
  return code;
784,803✔
996
}
997

998
int64_t msmAssignTaskId(void) {
1,410,799✔
999
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
1,410,799✔
1000
}
1001

1002
int64_t msmAssignTaskSeriousId(void) {
1,410,799✔
1003
  return taosGetTimestampNs();
1,410,799✔
1004
}
1005

1006

1007
int32_t msmIsSnodeAlive(SMnode* pMnode, int32_t snodeId, int64_t streamId, bool* alive) {
1,028,603✔
1008
  int32_t code = TSDB_CODE_SUCCESS;
1,028,603✔
1009
  int32_t lino = 0;
1,028,603✔
1010
  bool     noExists = false;
1,028,603✔
1011
  SStmSnodeStatus* pStatus = NULL;
1,028,603✔
1012

1013
  while (true) {
1014
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
1,039,331✔
1015
    if (NULL == pStatus) {
1,039,331✔
1016
      if (noExists) {
10,728✔
UNCOV
1017
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1018
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1019
      }
1020

1021
      noExists = true;
10,728✔
1022
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
10,728✔
1023
      
1024
      continue;
10,728✔
1025
    }
1026

1027
    *alive = (pStatus->runnerThreadNum >= 0);
1,028,603✔
1028
    break;
1,028,603✔
1029
  }
1030

1031
_exit:
1,028,603✔
1032

1033
  if (code) {
1,028,603✔
UNCOV
1034
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1035
  }
1036

1037
  return code;
1,028,603✔
1038
}
1039

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

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

UNCOV
1068
      snodeId = pSnode->replicaId;
×
1069
      mndReleaseSnode(pMnode, pSnode);
×
1070
      
UNCOV
1071
      continue;
×
1072
    }
1073

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

UNCOV
1078
_exit:
×
1079

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

UNCOV
1084
  return 0;
×
1085
}
1086

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

1101
  int32_t snodeTarget = taosRand() % snodeNum;
603,694✔
1102

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

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

1128
    snodeId = pObj->id;
634,294✔
1129
    if (snodeIdx == snodeTarget) {
634,294✔
1130
      sdbRelease(pMnode->pSdb, pObj);
603,694✔
1131
      sdbCancelFetch(pMnode->pSdb, pIter);
603,694✔
1132
      pObj = NULL;
603,694✔
1133
      goto _exit;
603,694✔
1134
    }
1135

1136
    sdbRelease(pMnode->pSdb, pObj);
30,600✔
1137
    snodeIdx++;
30,600✔
1138
  }
1139

1140
_exit:
607,914✔
1141

1142
  if (code) {
607,914✔
UNCOV
1143
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1144
  }
1145

1146
  if (0 == snodeId) {
607,914✔
1147
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
4,220✔
1148
  }
1149

1150
  return snodeId;
607,914✔
1151
}
1152

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

1162
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
784,803✔
1163

1164
_exit:
784,803✔
1165

1166
  if (0 == snodeId) {
784,803✔
UNCOV
1167
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1168
  }
1169

1170
  return snodeId;
784,803✔
1171
}
1172

1173

1174
static int32_t msmBuildTriggerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,653✔
1175
  int32_t code = TSDB_CODE_SUCCESS;
198,653✔
1176
  int32_t lino = 0;
198,653✔
1177
  int64_t streamId = pStream->pCreate->streamId;
198,653✔
1178

1179
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
198,653✔
1180
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
198,653✔
1181

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

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

1203
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
198,653✔
1204
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
198,653✔
1205

1206
_exit:
198,653✔
1207

1208
  if (code) {
198,653✔
UNCOV
1209
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1210
  }
1211

1212
  return code;
198,653✔
1213
}
1214

1215
static int32_t msmTDAddSingleTrigReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t nodeId, SStmStatus* pInfo, int64_t streamId) {
309,440✔
1216
  int32_t code = TSDB_CODE_SUCCESS;
309,440✔
1217
  int32_t lino = 0;
309,440✔
1218

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

1241
_exit:
309,440✔
1242

1243
  if (code) {
309,440✔
UNCOV
1244
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1245
  }
1246

1247
  return code;
309,440✔
1248
}
1249

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

1279
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
104,953✔
1280
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
104,953✔
1281
      
1282
      void *pIter = NULL;
104,953✔
1283
      while (1) {
564,860✔
1284
        SStmTaskDeploy info = {0};
669,813✔
1285
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
669,813✔
1286
        if (pIter == NULL) {
669,813✔
1287
          break;
104,953✔
1288
        }
1289
      
1290
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
564,860✔
1291
          pState = taosArrayReserve(pInfo->trigReaders, 1);
153,606✔
1292

1293
          code = msmTDAddSingleTrigReader(pCtx, pState, pVgroup->vgId, pInfo, streamId);
153,606✔
1294
          if (code) {
153,606✔
UNCOV
1295
            sdbRelease(pSdb, pVgroup);
×
1296
            sdbCancelFetch(pSdb, pIter);
×
1297
            pVgroup = NULL;
×
1298
            TAOS_CHECK_EXIT(code);
×
1299
          }
1300
        }
1301

1302
        sdbRelease(pSdb, pVgroup);
564,860✔
1303
      }
1304
      break;
104,953✔
1305
    }
1306
    default:
1,912✔
1307
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
1,912✔
1308
      break;
1,912✔
1309
  }
1310

1311
_exit:
198,653✔
1312

1313
  mndReleaseDb(pCtx->pMnode, pDb);
198,653✔
1314

1315
  if (code) {
198,653✔
UNCOV
1316
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1317
  }
1318

1319
  return code;
198,653✔
1320
}
1321

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

1345
  key[1] = pSubplan->id.subplanId;
279,840✔
1346

1347
  SArray** ppRes = taosHashGet(mStreamMgmt.toUpdateScanMap, key, sizeof(key));
279,840✔
1348
  if (NULL == ppRes) {
279,840✔
1349
    SArray* pRes = taosArrayInit(1, sizeof(addr));
279,840✔
1350
    TSDB_CHECK_NULL(pRes, code, lino, _exit, terrno);
279,840✔
1351
    TSDB_CHECK_NULL(taosArrayPush(pRes, &addr), code, lino, _exit, terrno);
559,680✔
1352
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.toUpdateScanMap, key, sizeof(key), &pRes, POINTER_BYTES));
279,840✔
1353
  } else {
UNCOV
1354
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &addr), code, lino, _exit, terrno);
×
1355
  }
1356

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

1363
  nodesDestroyNode((SNode*)pSubplan);
279,840✔
1364

1365
  if (code) {
279,840✔
UNCOV
1366
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1367
  }
1368

1369
  return code;
279,840✔
1370
}
1371

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

1380
  SStmTaskSrcAddr addr;
94,530✔
1381
  addr.isFromCache = true;
94,530✔
1382
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pCtx->triggerNodeId);
94,530✔
1383
  addr.taskId = pCtx->triggerTaskId;
94,530✔
1384
  addr.vgId = pCtx->triggerNodeId;
94,530✔
1385
  addr.groupId = pSubplan->id.groupId;
94,530✔
1386

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

1402
  nodesDestroyNode((SNode*)pSubplan);
94,530✔
1403
  
1404
  if (code) {
94,530✔
UNCOV
1405
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1406
  }
1407

1408
  return code;
94,530✔
1409
}
1410

1411

1412
static int32_t msmTDAddSingleCalcReader(SStmGrpCtx* pCtx, SStmTaskStatus* pState, int32_t taskIdx, int32_t nodeId, void* calcScanPlan, SStmStatus* pInfo, int64_t streamId) {
283,743✔
1413
  int32_t code = TSDB_CODE_SUCCESS;
283,743✔
1414
  int32_t lino = 0;
283,743✔
1415

1416
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
283,743✔
1417

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

1440
_exit:
283,743✔
1441

1442
  if (code) {
283,743✔
UNCOV
1443
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1444
  }
1445

1446
  return code;
283,743✔
1447
}
1448

1449

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

1459
  
1460
  for (int32_t i = 0; i < calcTasksNum; ++i) {
572,614✔
1461
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
373,961✔
1462
    if (pScan->readFromCache) {
373,961✔
1463
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
94,530✔
1464
      continue;
94,530✔
1465
    }
1466
    
1467
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
279,431✔
1468
    for (int32_t m = 0; m < vgNum; ++m) {
558,862✔
1469
      pState = tdListReserve(pInfo->calcReaders);
279,431✔
1470
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
279,431✔
1471

1472
      TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pState, i, *(int32_t*)taosArrayGet(pScan->vgList, m), pScan->scanPlan, pInfo, streamId));
279,431✔
1473
      TAOS_CHECK_EXIT(msmUPAddScanTask(pCtx, pStream, pScan->scanPlan, pState->id.nodeId, pState->id.taskId));
279,431✔
1474
    }
1475
  }
1476

1477
_exit:
198,653✔
1478

1479
  if (code) {
198,653✔
UNCOV
1480
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1481
  }
1482

1483
  return code;
198,653✔
1484
}
1485

1486

1487

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

1515
_exit:
409✔
1516

1517
  if (code) {
409✔
UNCOV
1518
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1519
  }
1520

1521
  return code;
409✔
1522
}
1523

1524
static int32_t msmBuildReaderTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,653✔
1525
  int32_t code = TSDB_CODE_SUCCESS;
198,653✔
1526
  int32_t lino = 0;
198,653✔
1527
  int64_t streamId = pStream->pCreate->streamId;
198,653✔
1528
  
1529
  TAOS_CHECK_EXIT(msmTDAddTrigReaderTasks(pCtx, pInfo, pStream));
198,653✔
1530
  TAOS_CHECK_EXIT(msmTDAddCalcReaderTasks(pCtx, pInfo, pStream));
198,653✔
1531

1532
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL));
198,653✔
1533
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL));
198,653✔
1534
  
1535
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, pInfo->trigReaders, NULL, NULL, true));
198,653✔
1536
  TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, pInfo->calcReaders, NULL, false));
198,653✔
1537
  
1538
_exit:
198,653✔
1539

1540
  if (code) {
198,653✔
UNCOV
1541
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1542
  }
1543
  
1544
  return code;
198,653✔
1545
}
1546

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

1558
  source.addr.epSet = pSrc->epset;
1,155,514✔
1559
  source.addr.nodeId = pSrc->vgId;
1,155,514✔
1560

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

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

UNCOV
1580
  mstsError("subplanId %d not found in runner list", subplanId);
×
1581

UNCOV
1582
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1583
}
1584

1585
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
619,372✔
1586
  int32_t code = TSDB_CODE_SUCCESS;
619,372✔
1587
  int32_t lino = 0;
619,372✔
1588
  int64_t key[2] = {streamId, -1};
619,372✔
1589
  SNode* pNode = NULL;
619,372✔
1590
  SStreamTask* pTask = &pDeploy->task;
619,372✔
1591
  FOREACH(pNode, pPlan->pChildren) {
1,774,886✔
1592
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
1,155,514✔
1593
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
33,222✔
1594
      continue;
33,222✔
1595
    }
1596
    
1597
    SValueNode* pVal = (SValueNode*)pNode;
1,122,292✔
1598
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
1,122,292✔
UNCOV
1599
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1600
      continue;
×
1601
    }
1602

1603
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
1,122,292✔
1604

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

1611
    int32_t childrenNum = taosArrayGetSize(*ppRes);
1,122,292✔
1612
    for (int32_t i = 0; i < childrenNum; ++i) {
2,244,584✔
1613
      SStmTaskSrcAddr* pAddr = taosArrayGet(*ppRes, i);
1,122,292✔
1614
      TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(pTask, streamId, pPlan, pDeploy->task.taskId, pAddr, pAddr->isFromCache ? TDMT_STREAM_FETCH_FROM_CACHE : TDMT_STREAM_FETCH, key[1]));
1,122,292✔
1615
    }
1616
  }
1617

1618
_exit:
619,372✔
1619

1620
  if (code) {
619,372✔
UNCOV
1621
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1622
  }
1623

1624
  return code;
619,372✔
1625
}
1626

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

1635
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
619,372✔
1636

1637
  SNode* pTmp = NULL;
619,372✔
1638
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,774,886✔
1639
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
1,155,514✔
1640
      ERASE_NODE(pPlan->pChildren);
1,122,292✔
1641
      continue;
1,122,292✔
1642
    }
1643
    WHERE_NEXT;
33,222✔
1644
  }
1645
  nodesClearList(pPlan->pChildren);
619,372✔
1646
  pPlan->pChildren = NULL;
619,372✔
1647

1648
  if (NULL == pPlan->pParents) {
619,372✔
1649
    goto _exit;
586,150✔
1650
  }
1651

1652
  SNode* pNode = NULL;
33,222✔
1653
  int64_t parentTaskId = 0;
33,222✔
1654
  SStmTaskSrcAddr addr = {0};
33,222✔
1655
  addr.taskId = pDeploy->task.taskId;
33,222✔
1656
  addr.vgId = pDeploy->task.nodeId;
33,222✔
1657
  addr.groupId = pPlan->id.groupId;
33,222✔
1658
  addr.epset = mndGetDnodeEpsetById(pCtx->pMnode, pDeploy->task.nodeId);
33,222✔
1659
  FOREACH(pNode, pPlan->pParents) {
66,444✔
1660
    SSubplan* pSubplan = (SSubplan*)pNode;
33,222✔
1661
    TAOS_CHECK_EXIT(msmGetTaskIdFromSubplanId(pStream, pRunners, beginIdx, pSubplan->id.subplanId, &parentTaskId, &parentTask));
33,222✔
1662
    TAOS_CHECK_EXIT(msmUpdatePlanSourceAddr(parentTask, streamId, pSubplan, parentTaskId, &addr, TDMT_STREAM_FETCH_FROM_RUNNER, pPlan->id.subplanId));
33,222✔
1663
  }
1664
  
1665
_exit:
609,922✔
1666

1667
  if (code) {
619,372✔
UNCOV
1668
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1669
  }
1670

1671
  return code;
619,372✔
1672
}
1673

1674
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
586,150✔
1675
  int32_t code = TSDB_CODE_SUCCESS;
586,150✔
1676
  int32_t lino = 0;
586,150✔
1677
  int64_t streamId = pStream->pCreate->streamId;
586,150✔
1678
  int32_t runnerNum = taosArrayGetSize(pRunners);
586,150✔
1679
  
1680
  for (int32_t i = 0; i < runnerNum; ++i) {
1,205,522✔
1681
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
619,372✔
1682
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
619,372✔
1683
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
619,372✔
1684

1685
    SStreamTask* pTask = &pDeploy->task;
619,372✔
1686
    msttDebugL("runner updated task plan:%s", (const char*)pDeploy->msg.runner.pPlan);
619,372✔
1687
  }
1688

1689
_exit:
586,150✔
1690

1691
  if (code) {
586,150✔
UNCOV
1692
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1693
  }
1694

1695
  return code;
586,150✔
1696
}
1697

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

1711
  if (pDag->numOfSubplans <= 0) {
195,247✔
UNCOV
1712
    mstsError("invalid subplan num:%d", pDag->numOfSubplans);
×
1713
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1714
  }
1715

1716
  if (pDag->numOfSubplans != pStream->pCreate->numOfCalcSubplan) {
195,247✔
UNCOV
1717
    mstsError("numOfCalcSubplan %d mismatch with numOfSubplans %d", pStream->pCreate->numOfCalcSubplan, pDag->numOfSubplans);
×
1718
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1719
  }
1720

1721
  int32_t levelNum = (int32_t)LIST_LENGTH(pDag->pSubplans);
195,247✔
1722
  if (levelNum <= 0) {
195,247✔
UNCOV
1723
    mstsError("invalid level num:%d", levelNum);
×
1724
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1725
  }
1726

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

1741
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
195,247✔
1742
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
195,247✔
1743
  
1744
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
780,988✔
1745
    totalTaskNum = 0;
585,741✔
1746

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

1753
    taskIdx = 0;
585,741✔
1754
    
1755
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
1,188,093✔
1756
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
602,352✔
1757
      if (NULL == plans) {
602,352✔
UNCOV
1758
        mstsError("empty level plan, level:%d", i);
×
1759
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1760
      }
1761

1762
      if (QUERY_NODE_NODE_LIST != nodeType(plans)) {
602,352✔
UNCOV
1763
        mstsError("invalid level plan, level:%d, planNodeType:%d", i, nodeType(plans));
×
1764
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1765
      }
1766

1767
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
602,352✔
1768
      if (taskNum <= 0) {
602,352✔
UNCOV
1769
        mstsError("invalid level plan number:%d, level:%d", taskNum, i);
×
1770
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1771
      }
1772

1773
      totalTaskNum += taskNum;
602,352✔
1774
      if (totalTaskNum > pDag->numOfSubplans) {
602,352✔
UNCOV
1775
        mstsError("current totalTaskNum %d is bigger than numOfSubplans %d, level:%d", totalTaskNum, pDag->numOfSubplans, i);
×
1776
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1777
      }
1778

1779
      for (int32_t n = 0; n < taskNum; ++n) {
1,221,315✔
1780
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
618,963✔
1781
        pState = taosArrayReserve(deployList, 1);
618,963✔
1782

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

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

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

1810
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
602,352✔
1811
    }
1812

1813
    if (totalTaskNum != pDag->numOfSubplans) {
585,741✔
UNCOV
1814
      mstsError("totalTaskNum %d mis-match with numOfSubplans %d", totalTaskNum, pDag->numOfSubplans);
×
1815
      TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1816
    }
1817

1818
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
585,741✔
1819

1820
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
585,741✔
1821

1822
    nodesDestroyNode((SNode *)pDag);
585,741✔
1823
    pDag = NULL;
585,741✔
1824
    
1825
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
585,741✔
1826

1827
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
585,741✔
1828
  }
1829

1830
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
780,988✔
1831
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
585,741✔
1832
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
585,741✔
1833
  }
1834
  
1835
  pInfo->runnerNum = totalTaskNum;
195,247✔
1836
  
1837
_exit:
195,247✔
1838

1839
  if (code) {
195,247✔
UNCOV
1840
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1841
  }
1842

1843
  taosArrayDestroy(deployTaskList);
195,247✔
1844
  nodesDestroyNode((SNode *)pDag);
195,247✔
1845

1846
  return code;
195,247✔
1847
}
1848

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

1866
  for (int32_t r = 0; r < pAction->deployNum; ++r) {
818✔
1867
    deployId = pAction->deployId[r];
409✔
1868

1869
    pRunner = taosArrayGet(pInfo->runners[deployId], 0);
409✔
1870

1871
    pStartRunner = pRunner;
409✔
1872
    totalTaskNum = 0;
409✔
1873

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

1879
    taskIdx = 0;
409✔
1880
    
1881
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
818✔
1882
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
409✔
1883
      taskNum = (int32_t)LIST_LENGTH(plans->pNodeList);
409✔
1884
      totalTaskNum += taskNum;
409✔
1885

1886
      pRunner->flags &= STREAM_FLAG_REDEPLOY_RUNNER;
409✔
1887
      
1888
      for (int32_t n = 0; n < taskNum; ++n) {
818✔
1889
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
409✔
1890

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

1897
        pRunner->id.nodeId = newNodeId;
409✔
1898

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

1909
        pRunner++;
409✔
1910
      }
1911

1912
      mstsDebug("level %d initialized, taskNum:%d", i, taskNum);
409✔
1913
    }
1914

1915
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
409✔
1916

1917
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
409✔
1918

1919
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[deployId], NULL, 0, deployId));
409✔
1920

1921
    nodesDestroyNode((SNode *)pDag);
409✔
1922
    pDag = NULL;
409✔
1923

1924
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
409✔
1925
  }
1926

1927
_exit:
409✔
1928

1929
  if (code) {
409✔
UNCOV
1930
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1931
  }
1932

1933
  nodesDestroyNode((SNode *)pDag);
409✔
1934
  taosArrayDestroy(deployTaskList);
409✔
1935

1936
  return code;
409✔
1937
}
1938

1939

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

1948
_exit:
188,313✔
1949

1950
  if (code) {
188,313✔
UNCOV
1951
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1952
  }
1953

1954
  return code;
188,313✔
1955
}
1956

1957

1958
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,653✔
1959
  if (NULL == pStream->pCreate->calcPlan) {
198,653✔
1960
    return TSDB_CODE_SUCCESS;
3,406✔
1961
  }
1962
  
1963
  int32_t code = TSDB_CODE_SUCCESS;
195,247✔
1964
  int32_t lino = 0;
195,247✔
1965
  int64_t streamId = pStream->pCreate->streamId;
195,247✔
1966
  SQueryPlan* pPlan = NULL;
195,247✔
1967

1968
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
195,247✔
1969

1970
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
780,988✔
1971
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
585,741✔
1972
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
585,741✔
1973
  }
1974

1975
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
195,247✔
1976
  pPlan = NULL;
195,247✔
1977
  
1978
  TAOS_CHECK_EXIT(code);
195,247✔
1979

1980
  taosHashClear(mStreamMgmt.toUpdateScanMap);
195,247✔
1981
  mStreamMgmt.toUpdateScanNum = 0;
195,247✔
1982

1983
_exit:
195,247✔
1984

1985
  nodesDestroyNode((SNode *)pPlan);
195,247✔
1986

1987
  if (code) {
195,247✔
UNCOV
1988
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1989
  }
1990

1991
  return code;
195,247✔
1992
}
1993

1994

1995
static int32_t msmBuildStreamTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
198,653✔
1996
  int32_t code = TSDB_CODE_SUCCESS;
198,653✔
1997
  int32_t lino = 0;
198,653✔
1998
  int64_t streamId = pStream->pCreate->streamId;
198,653✔
1999

2000
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
198,653✔
2001

2002
  pCtx->triggerTaskId = msmAssignTaskId();
198,653✔
2003
  pCtx->triggerNodeId = msmAssignTaskSnodeId(pCtx->pMnode, pStream, true);
198,653✔
2004
  if (!GOT_SNODE(pCtx->triggerNodeId)) {
198,653✔
UNCOV
2005
    TAOS_CHECK_EXIT(terrno);
×
2006
  }
2007

2008
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
198,653✔
2009
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
198,653✔
2010
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
198,653✔
2011
  
2012
_exit:
198,653✔
2013

2014
  if (code) {
198,653✔
UNCOV
2015
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2016
  }
2017

2018
  return code;
198,653✔
2019
}
2020

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

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

UNCOV
2060
_exit:
×
2061

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

UNCOV
2067
  return code;
×
2068
}
2069

2070

2071
static int32_t msmInitStmStatus(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStreamObj* pStream, bool initList) {
191,719✔
2072
  int32_t code = TSDB_CODE_SUCCESS;
191,719✔
2073
  int32_t lino = 0;
191,719✔
2074
  int64_t streamId = pStream->pCreate->streamId;
191,719✔
2075

2076
  pStatus->lastActionTs = INT64_MIN;
191,719✔
2077

2078
  if (NULL == pStatus->streamName) {
191,719✔
2079
    pStatus->streamName = taosStrdup(pStream->name);
191,719✔
2080
    TSDB_CHECK_NULL(pStatus->streamName, code, lino, _exit, terrno);
191,719✔
2081
  }
2082

2083
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
191,719✔
2084
  
2085
  if (pStream->pCreate->numOfCalcSubplan > 0) {
191,719✔
2086
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
188,313✔
2087
    
2088
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
188,313✔
2089
  }
2090

2091
  if (initList) {
191,719✔
UNCOV
2092
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2093

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

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

2111
  if (code) {
191,719✔
UNCOV
2112
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2113
  }
2114

2115
  return code;
191,719✔
2116
}
2117

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

2124
  if (NULL == pStatus) {
198,653✔
2125
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
191,719✔
2126

2127
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
191,719✔
2128

2129
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
191,719✔
2130
  }
2131
  
2132
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
198,653✔
2133

2134
  mstLogSStmStatus("stream deployed", streamId, pStatus);
198,653✔
2135

2136
_exit:
198,653✔
2137

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

2147
  return code;
198,653✔
2148
}
2149

2150

2151
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
10,141✔
2152
  int32_t code = TSDB_CODE_SUCCESS;
10,141✔
2153
  void* pIter = NULL;
10,141✔
2154

2155
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
13,345✔
2156
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
3,204✔
2157
    (void)mstWaitLock(&pVg->lock, true);
3,204✔
2158

2159
    int32_t taskNum = taosArrayGetSize(pVg->taskList);
3,204✔
2160
    if (atomic_load_32(&pVg->deployed) == taskNum) {
3,204✔
UNCOV
2161
      taosRUnLockLatch(&pVg->lock);
×
2162
      continue;
×
2163
    }
2164

2165
    for (int32_t i = 0; i < taskNum; ++i) {
18,868✔
2166
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
15,664✔
2167
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
15,664✔
2168
        continue;
13,528✔
2169
      }
2170

2171
      mstDestroySStmTaskToDeployExt(pExt);
2,136✔
2172
      pExt->deployed = true;
2,136✔
2173
    }
2174
    
2175
    taosRUnLockLatch(&pVg->lock);
3,204✔
2176
  }
2177

2178
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
19,760✔
2179
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
9,619✔
2180
    (void)mstWaitLock(&pSnode->lock, true);
9,619✔
2181

2182
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
9,619✔
2183
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
9,619✔
2184
      for (int32_t i = 0; i < taskNum; ++i) {
12,149✔
2185
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
7,832✔
2186
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
7,832✔
2187
          continue;
6,764✔
2188
        }
2189
        
2190
        mstDestroySStmTaskToDeployExt(pExt);
1,068✔
2191
        pExt->deployed = true;
1,068✔
2192
      }
2193
    }
2194

2195
    taskNum = taosArrayGetSize(pSnode->runnerList);
9,619✔
2196
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
9,619✔
2197
      for (int32_t i = 0; i < taskNum; ++i) {
34,812✔
2198
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
25,950✔
2199
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
25,950✔
2200
          continue;
22,746✔
2201
        }
2202
        
2203
        mstDestroySStmTaskToDeployExt(pExt);
3,204✔
2204
        pExt->deployed = true;
3,204✔
2205
      }
2206
    }
2207

2208
    taosRUnLockLatch(&pSnode->lock);
9,619✔
2209
  }
2210

2211
  
2212
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
45,368✔
2213
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
35,227✔
2214
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
35,227✔
2215
    if (TSDB_CODE_SUCCESS == code) {
35,227✔
2216
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
15,693✔
2217
    }
2218
  }
2219

2220
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
33,580✔
2221
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
23,439✔
2222
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
23,439✔
2223
    if (TSDB_CODE_SUCCESS == code) {
23,439✔
2224
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
18,168✔
2225
    }
2226
  }
2227

2228
  size_t keyLen = 0;
10,141✔
2229
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
864,581✔
2230
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
854,440✔
2231
    if (*pStreamId == streamId) {
854,440✔
2232
      int64_t taskId = *(pStreamId + 1);
68,201✔
2233
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
68,201✔
2234
      if (code) {
68,201✔
UNCOV
2235
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2236
      } else {
2237
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
68,201✔
2238
      }
2239
    }
2240
  }
2241

2242
  if (fromStreamMap) {
10,141✔
2243
    code = taosHashRemove(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
3,207✔
2244
    if (code) {
3,207✔
UNCOV
2245
      mstsError("stream remove from streamMap failed, error:%s", tstrerror(code));
×
2246
    } else {
2247
      mstsDebug("stream removed from streamMap, remains:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,207✔
2248
    }
2249
  }
2250
  
2251
  return code;
10,141✔
2252
}
2253

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

2259
  mstResetSStmStatus(pStatus);
6,934✔
2260

2261
  pStatus->deployTimes++;
6,934✔
2262
}
6,934✔
2263

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

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

2291
  code = mndAcquireStream(pCtx->pMnode, streamName, &pStream);
199,022✔
2292
  if (TSDB_CODE_MND_STREAM_NOT_EXIST == code) {
199,022✔
2293
    mstsWarn("stream %s no longer exists, ignore deploy", streamName);
369✔
2294
    return TSDB_CODE_SUCCESS;
369✔
2295
  }
2296

2297
  TAOS_CHECK_EXIT(code);
198,653✔
2298

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

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

2315
_exit:
198,653✔
2316

2317
  mndReleaseStream(pCtx->pMnode, pStream);
198,653✔
2318

2319
  if (code) {
198,653✔
UNCOV
2320
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2321
  }
2322

2323
  return code;
198,653✔
2324
}
2325

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

2358
_exit:
2,136✔
2359

2360
  if (code) {
2,136✔
UNCOV
2361
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2362
  }
2363

2364
  return code;
2,136✔
2365
}
2366

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

2398
_exit:
2399

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

2404
  return code;
2405
}
2406
*/
2407

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

2457
_exit:
409✔
2458

2459
  if (code) {
409✔
UNCOV
2460
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2461
  }
2462

2463
  return code;
409✔
2464
}
2465

2466

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

2474
  mstsDebug("start to handle stream tasks action, action task type:%s", gStreamTaskTypeStr[pAction->type]);
2,545✔
2475

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

2482
  int8_t stopped = atomic_load_8(&pStatus->stopped);
2,545✔
2483
  if (stopped) {
2,545✔
UNCOV
2484
    mstsWarn("stream %s is already stopped %d, ignore task deploy", pStatus->streamName, stopped);
×
2485
    return TSDB_CODE_SUCCESS;
×
2486
  }
2487

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

2494
  TAOS_CHECK_EXIT(code);
2,545✔
2495

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

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

2526
_exit:
2,545✔
2527

2528
  if (pStream) {
2,545✔
2529
    mndReleaseStream(pCtx->pMnode, pStream);
2,545✔
2530
  }
2531

2532
  if (code) {
2,545✔
UNCOV
2533
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
2534
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2535
  }
2536

2537
  return code;
2,545✔
2538
}
2539

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

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

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

UNCOV
2585
  return TSDB_CODE_SUCCESS;
×
2586
}
2587

2588
static int32_t msmRemoveStreamFromMaps(SMnode* pMnode, int64_t streamId) {
3,207✔
2589
  int32_t code = TSDB_CODE_SUCCESS;
3,207✔
2590
  int32_t lino = 0;
3,207✔
2591

2592
  mstsInfo("start to remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,207✔
2593

2594
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
3,207✔
2595

2596
_exit:
3,207✔
2597

2598
  if (code) {
3,207✔
UNCOV
2599
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2600
  } else {
2601
    mstsInfo("end remove stream from maps, current stream num:%d", taosHashGetSize(mStreamMgmt.streamMap));
3,207✔
2602
  }
2603

2604
  return code;
3,207✔
2605
}
2606

2607
void msmUndeployStream(SMnode* pMnode, int64_t streamId, char* streamName) {
16,334✔
2608
  int32_t code = TSDB_CODE_SUCCESS;
16,334✔
2609
  int32_t lino = 0;
16,334✔
2610

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

2617
  SStmStatus* pStream = (SStmStatus*)taosHashAcquire(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
16,334✔
2618
  if (NULL == pStream) {
16,334✔
2619
    mstsInfo("stream %s already not in streamMap", streamName);
369✔
2620
    goto _exit;
369✔
2621
  }
2622

2623
  atomic_store_8(&pStream->stopped, 2);
15,965✔
2624

2625
  mstsInfo("set stream %s stopped by user", streamName);
15,965✔
2626

UNCOV
2627
_exit:
×
2628

2629
  taosHashRelease(mStreamMgmt.streamMap, pStream);
16,334✔
2630

2631
  if (code) {
16,334✔
UNCOV
2632
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2633
  }
2634

2635
  return;
16,334✔
2636
}
2637

2638
int32_t msmRecalcStream(SMnode* pMnode, int64_t streamId, STimeWindow* timeRange) {
6,547✔
2639
  int32_t code = TSDB_CODE_SUCCESS;
6,547✔
2640
  int32_t lino = 0;
6,547✔
2641

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

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

2655
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
6,547✔
2656

2657
_exit:
6,547✔
2658

2659
  taosHashRelease(mStreamMgmt.streamMap, pStream);
6,547✔
2660

2661
  if (code) {
6,547✔
UNCOV
2662
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2663
  }
2664

2665
  return code;
6,547✔
2666
}
2667

2668
static void msmHandleStreamActions(SStmGrpCtx* pCtx) {
45,811✔
2669
  int32_t code = TSDB_CODE_SUCCESS;
45,811✔
2670
  int32_t lino = 0;
45,811✔
2671
  SStmQNode* pQNode = NULL;
45,811✔
2672

2673
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
247,378✔
2674
    switch (pQNode->type) {
201,567✔
2675
      case STREAM_ACT_DEPLOY:
201,567✔
2676
        if (pQNode->streamAct) {
201,567✔
2677
          mstDebug("start to handle stream deploy action");
199,022✔
2678
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
199,022✔
2679
        } else {
2680
          mstDebug("start to handle task deploy action");
2,545✔
2681
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
2,545✔
2682
        }
2683
        break;
201,567✔
UNCOV
2684
      default:
×
2685
        break;
×
2686
    }
2687
  }
2688

2689
_exit:
45,811✔
2690

2691
  if (code) {
45,811✔
UNCOV
2692
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2693
  }
2694
}
45,811✔
2695

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

UNCOV
2707
    pStatus = (SStmStatus*)pIter;
×
2708

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

UNCOV
2712
    mstsInfo("set stream stopped since %s", tstrerror(errCode));
×
2713
  }
UNCOV
2714
}
×
2715

UNCOV
2716
int32_t msmHandleGrantExpired(SMnode *pMnode, int32_t errCode) {
×
2717
  mstInfo("stream grant expired");
×
2718

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

UNCOV
2724
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
2725

UNCOV
2726
  msmStopAllStreamsByGrant(errCode);
×
2727

UNCOV
2728
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
2729
  
UNCOV
2730
  return errCode;
×
2731
}
2732

2733
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
1,406,527✔
2734
  int32_t code = TSDB_CODE_SUCCESS;
1,406,527✔
2735
  int32_t lino = 0;
1,406,527✔
2736
  int64_t streamId = pDeploy->task.streamId;
1,406,527✔
2737
  
2738
  switch (pDeploy->task.type) {
1,406,527✔
2739
    case STREAM_READER_TASK:
593,183✔
2740
      if (NULL == pStream->readerTasks) {
593,183✔
2741
        pStream->streamId = streamId;
259,810✔
2742
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
259,810✔
2743
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
259,810✔
2744
      }
2745
      
2746
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
1,186,366✔
2747
      break;
593,183✔
2748
    case STREAM_TRIGGER_TASK:
197,585✔
2749
      pStream->streamId = streamId;
197,585✔
2750
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
197,585✔
2751
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
197,585✔
2752
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
197,585✔
2753
      break;
197,585✔
2754
    case STREAM_RUNNER_TASK:
615,759✔
2755
      if (NULL == pStream->runnerTasks) {
615,759✔
2756
        pStream->streamId = streamId;
205,888✔
2757
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
205,888✔
2758
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
205,888✔
2759
      }      
2760
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
1,231,518✔
2761
      break;
615,759✔
UNCOV
2762
    default:
×
2763
      break;
×
2764
  }
2765

2766
_exit:
1,406,527✔
2767

2768
  if (code) {
1,406,527✔
UNCOV
2769
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2770
  }
2771

2772
  return code;
1,406,527✔
2773
}
2774

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

UNCOV
2792
      if (TSDB_CODE_DUP_KEY != code) {
×
2793
        goto _exit;
×
2794
      }    
2795

UNCOV
2796
      tFreeSStmStreamDeploy(&streamDeploy);
×
2797
      continue;
×
2798
    }
2799

2800
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
1,121,198✔
2801
    
2802
    break;
1,121,198✔
2803
  }
2804
  
2805
_exit:
1,406,527✔
2806

2807
  taosHashRelease(pHash, pStream);
1,406,527✔
2808

2809
  if (code) {
1,406,527✔
UNCOV
2810
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2811
  } else {
2812
    msttDebug("task added to GRP deployMap, taskIdx:%d", pTask->taskIdx);
1,406,527✔
2813
  }
2814

2815
  return code;
1,406,527✔
2816
}
2817

2818

2819
int32_t msmGrpAddDeployTasks(SHashObj* pHash, SArray* pTasks, int32_t* deployed) {
272,187✔
2820
  int32_t code = TSDB_CODE_SUCCESS;
272,187✔
2821
  int32_t lino = 0;
272,187✔
2822
  int32_t taskNum = taosArrayGetSize(pTasks);
272,187✔
2823

2824
  for (int32_t i = 0; i < taskNum; ++i) {
1,685,122✔
2825
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
1,412,935✔
2826
    if (pExt->deployed) {
1,412,935✔
2827
      continue;
6,408✔
2828
    }
2829

2830
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
1,406,527✔
2831
    pExt->deployed = true;
1,406,527✔
2832

2833
    (void)atomic_add_fetch_32(deployed, 1);
1,406,527✔
2834
  }
2835

2836
_exit:
272,187✔
2837

2838
  if (code) {
272,187✔
UNCOV
2839
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2840
  }
2841

2842
  return code;
272,187✔
2843
}
2844

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

2852
  mstDebug("start to add stream vgroup tasks deploy");
76,630✔
2853
  
2854
  for (int32_t i = 0; i < vgNum; ++i) {
471,153✔
2855
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
394,523✔
2856

2857
    msmUpdateVgroupUpTs(pCtx, *vgId);
394,523✔
2858

2859
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
394,523✔
2860
    if (NULL == pVg) {
394,523✔
2861
      continue;
224,586✔
2862
    }
2863

2864
    if (taosRTryLockLatch(&pVg->lock)) {
169,937✔
UNCOV
2865
      continue;
×
2866
    }
2867
    
2868
    if (atomic_load_32(&pVg->deployed) == taosArrayGetSize(pVg->taskList)) {
169,937✔
UNCOV
2869
      taosRUnLockLatch(&pVg->lock);
×
2870
      continue;
×
2871
    }
2872
    
2873
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pVg->taskList, &pVg->deployed));
169,937✔
2874
    taosRUnLockLatch(&pVg->lock);
169,937✔
2875
  }
2876

2877
_exit:
76,630✔
2878

2879
  if (code) {
76,630✔
UNCOV
2880
    if (pVg) {
×
2881
      taosRUnLockLatch(&pVg->lock);
×
2882
    }
2883

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

2887
  return code;
76,630✔
2888
}
2889

2890
int32_t msmGrpAddDeploySnodeTasks(SStmGrpCtx* pCtx) {
86,022✔
2891
  int32_t code = TSDB_CODE_SUCCESS;
86,022✔
2892
  int32_t lino = 0;
86,022✔
2893
  SStmSnodeTasksDeploy* pSnode = NULL;
86,022✔
2894
  SStreamHbMsg* pReq = pCtx->pReq;
86,022✔
2895

2896
  mstDebug("start to add stream snode tasks deploy");
86,022✔
2897
  
2898
  pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &pReq->snodeId, sizeof(pReq->snodeId));
86,022✔
2899
  if (NULL == pSnode) {
86,022✔
2900
    return TSDB_CODE_SUCCESS;
31,849✔
2901
  }
2902

2903
  (void)mstWaitLock(&pSnode->lock, false);
54,173✔
2904
  
2905
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
54,173✔
2906
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
49,190✔
2907
  }
2908

2909
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
54,173✔
2910
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
53,060✔
2911
  }
2912
  
2913
  taosWUnLockLatch(&pSnode->lock);
54,173✔
2914

2915
_exit:
54,173✔
2916

2917
  if (code) {
54,173✔
UNCOV
2918
    if (pSnode) {
×
2919
      taosWUnLockLatch(&pSnode->lock);
×
2920
    }
2921

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

2925
  return code;
54,173✔
2926
}
2927

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

2939
_exit:
609,398✔
2940

2941
  if (code) {
609,398✔
UNCOV
2942
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2943
  }
2944

2945
  return code;
609,398✔
2946
}
2947

2948
int32_t msmRspAddStreamsDeploy(SStmGrpCtx* pCtx) {
72,797✔
2949
  int32_t code = TSDB_CODE_SUCCESS;
72,797✔
2950
  int32_t lino = 0;
72,797✔
2951
  int32_t streamNum = taosHashGetSize(pCtx->deployStm);
72,797✔
2952
  void* pIter = NULL;
72,797✔
2953

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

2959
  while (1) {
285,329✔
2960
    pIter = taosHashIterate(pCtx->deployStm, pIter);
358,126✔
2961
    if (pIter == NULL) {
358,126✔
2962
      break;
72,797✔
2963
    }
2964
    
2965
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
285,329✔
2966
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
570,658✔
2967

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

2972
    mstClearSStmStreamDeploy(pDeploy);
285,329✔
2973
    
2974
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
285,329✔
2975
  }
2976
  
2977
_exit:
72,797✔
2978

2979
  if (pIter) {
72,797✔
UNCOV
2980
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2981
  }
2982

2983
  if (code) {
72,797✔
UNCOV
2984
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2985
  }
2986
  
2987
  return code;
72,797✔
2988
}
2989

2990
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
76,630✔
2991
  int32_t code = TSDB_CODE_SUCCESS;
76,630✔
2992
  int32_t lino = 0;
76,630✔
2993
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
76,630✔
2994
  SStmVgTasksToDeploy* pVg = NULL;
76,630✔
2995
  
2996
  for (int32_t i = 0; i < vgNum; ++i) {
471,153✔
2997
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
394,523✔
2998
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
394,523✔
2999
    if (NULL == pVg) {
394,523✔
3000
      continue;
224,586✔
3001
    }
3002

3003
    if (taosWTryLockLatch(&pVg->lock)) {
169,937✔
UNCOV
3004
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3005
      continue;
×
3006
    }
3007
    
3008
    if (atomic_load_32(&pVg->deployed) <= 0) {
169,937✔
UNCOV
3009
      taosWUnLockLatch(&pVg->lock);
×
3010
      taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
×
3011
      continue;
×
3012
    }
3013

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

3025
    for (int32_t m = taskNum - 1; m >= 0; --m) {
4,628✔
3026
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, m);
4,272✔
3027
      if (!pExt->deployed) {
4,272✔
UNCOV
3028
        continue;
×
3029
      }
3030

3031
      mstDestroySStmTaskToDeployExt(pExt);
4,272✔
3032

3033
      taosArrayRemove(pVg->taskList, m);
4,272✔
3034
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeployVgTaskNum, 1);
4,272✔
3035
    }
3036
    atomic_store_32(&pVg->deployed, 0);
356✔
3037
    taosWUnLockLatch(&pVg->lock);
356✔
3038
    taosHashRelease(mStreamMgmt.toDeployVgMap, pVg);
356✔
3039
  }
3040

3041
_exit:
76,630✔
3042

3043
  if (code) {
76,630✔
UNCOV
3044
    if (pVg) {
×
3045
      taosWUnLockLatch(&pVg->lock);
×
3046
    }
3047

UNCOV
3048
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3049
  }
3050
}
76,630✔
3051

3052
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
105,086✔
3053
  if (!GOT_SNODE(snodeId)) {
105,086✔
3054
    return;
19,064✔
3055
  }
3056
  
3057
  int32_t code = TSDB_CODE_SUCCESS;
86,022✔
3058
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
86,022✔
3059
  if (NULL == pSnode) {
86,022✔
3060
    return;
31,849✔
3061
  }
3062

3063
  if (taosWTryLockLatch(&pSnode->lock)) {
54,173✔
UNCOV
3064
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3065
    return;
×
3066
  }
3067

3068
  int32_t triggerNum = taosArrayGetSize(pSnode->triggerList);
54,173✔
3069
  int32_t runnerNum = taosArrayGetSize(pSnode->runnerList);
54,173✔
3070
  
3071
  if (atomic_load_32(&pSnode->triggerDeployed) <= 0 && atomic_load_32(&pSnode->runnerDeployed) <= 0) {
54,173✔
3072
    taosWUnLockLatch(&pSnode->lock);
356✔
3073
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
356✔
3074
    return;
356✔
3075
  }
3076

3077
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
53,817✔
3078
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
51,947✔
3079
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
51,947✔
3080
    pSnode->triggerList = NULL;
51,947✔
3081
  }
3082

3083
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
53,817✔
3084
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
50,834✔
3085
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
50,834✔
3086
    pSnode->runnerList = NULL;
50,834✔
3087
  }
3088

3089
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
53,817✔
3090
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
50,077✔
3091
    taosWUnLockLatch(&pSnode->lock);
50,077✔
3092
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
50,077✔
3093
    return;
50,077✔
3094
  }
3095

3096
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
3,740✔
3097
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
7,791✔
3098
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
5,921✔
3099
      if (!pExt->deployed) {
5,921✔
3100
        continue;
3,785✔
3101
      }
3102

3103
      mstDestroySStmTaskToDeployExt(pExt);
2,136✔
3104
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
2,136✔
3105
      taosArrayRemove(pSnode->triggerList, m);
2,136✔
3106
    }
3107
    
3108
    pSnode->triggerDeployed = 0;
1,870✔
3109
  }
3110

3111
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
3,740✔
3112
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
63,138✔
3113
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
60,912✔
3114
      if (!pExt->deployed) {
60,912✔
3115
        continue;
757✔
3116
      }
3117

3118
      mstDestroySStmTaskToDeployExt(pExt);
60,155✔
3119
      (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, 1);
60,155✔
3120
      taosArrayRemove(pSnode->runnerList, m);
60,155✔
3121
    }
3122
    
3123
    pSnode->runnerDeployed = 0;
2,226✔
3124
  }
3125
  
3126
  taosWUnLockLatch(&pSnode->lock);
3,740✔
3127
  taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
3,740✔
3128
}
3129

3130
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
14,781,744✔
3131
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,781,744✔
3132
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
76,630✔
3133
  }
3134

3135
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
14,781,744✔
3136
    msmCleanDeployedSnodeTasks(pHb->snodeId);
105,086✔
3137
  }
3138
}
14,781,744✔
3139

3140
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
14,781,744✔
3141
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,781,744✔
3142
  if (mStreamMgmt.tCtx) {
14,781,744✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
14,598,430✔
3144
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
14,598,430✔
3145
  }
3146
}
14,781,744✔
3147

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

3165
_exit:
268,222✔
3166

3167
  if (code) {
268,222✔
UNCOV
3168
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3169
  }
3170

3171
  return code;
268,222✔
3172
}
3173

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

UNCOV
3190
_exit:
×
3191

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

UNCOV
3196
  return code;
×
3197
}
3198

3199

3200

3201
int32_t msmGrpAddActionUndeploy(SStmGrpCtx* pCtx, int64_t streamId, SStreamTask* pTask) {
55,847✔
3202
  int32_t code = TSDB_CODE_SUCCESS;
55,847✔
3203
  int32_t lino = 0;
55,847✔
3204
  int32_t action = STREAM_ACT_UNDEPLOY;
55,847✔
3205
  bool    dropped = false;
55,847✔
3206

3207
  TAOS_CHECK_EXIT(mstIsStreamDropped(pCtx->pMnode, streamId, &dropped));
55,847✔
3208
  mstsDebug("stream dropped: %d", dropped);
55,847✔
3209
  
3210
  SStmAction *pAction = taosHashGet(pCtx->actionStm, &streamId, sizeof(streamId));
55,847✔
3211
  if (pAction) {
55,847✔
3212
    pAction->actions |= action;
34,621✔
3213
    if (NULL == pAction->undeploy.taskList) {
34,621✔
UNCOV
3214
      pAction->undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
×
3215
      TSDB_CHECK_NULL(pAction->undeploy.taskList, code, lino, _exit, terrno);
×
3216
    }
3217

3218
    TSDB_CHECK_NULL(taosArrayPush(pAction->undeploy.taskList, &pTask), code, lino, _exit, terrno);
69,242✔
3219
    if (pAction->undeploy.doCheckpoint) {
34,621✔
3220
      pAction->undeploy.doCheckpoint = dropped ? false : true;
14,466✔
3221
    }
3222
    if (!pAction->undeploy.doCleanup) {
34,621✔
3223
      pAction->undeploy.doCleanup = dropped ? true : false;
14,466✔
3224
    }
3225
    
3226
    msttDebug("task append UNDEPLOY action[%d,%d], actions:%x", pAction->undeploy.doCheckpoint, pAction->undeploy.doCleanup, pAction->actions);
34,621✔
3227
  } else {
3228
    SStmAction newAction = {0};
21,226✔
3229
    newAction.actions = action;
21,226✔
3230
    newAction.undeploy.doCheckpoint = dropped ? false : true;
21,226✔
3231
    newAction.undeploy.doCleanup = dropped ? true : false;
21,226✔
3232
    newAction.undeploy.taskList = taosArrayInit(pCtx->taskNum, POINTER_BYTES);
21,226✔
3233
    TSDB_CHECK_NULL(newAction.undeploy.taskList, code, lino, _exit, terrno);
21,226✔
3234
    TSDB_CHECK_NULL(taosArrayPush(newAction.undeploy.taskList, &pTask), code, lino, _exit, terrno);
42,452✔
3235
    TAOS_CHECK_EXIT(taosHashPut(pCtx->actionStm, &streamId, sizeof(streamId), &newAction, sizeof(newAction)));
21,226✔
3236
    
3237
    msttDebug("task add UNDEPLOY action[%d,%d]", newAction.undeploy.doCheckpoint, newAction.undeploy.doCleanup);
21,226✔
3238
  }
3239

3240
_exit:
55,847✔
3241

3242
  if (code) {
55,847✔
UNCOV
3243
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3244
  }
3245

3246
  return code;
55,847✔
3247
}
3248

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

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

3270
_exit:
6,120✔
3271

3272
  if (code) {
6,120✔
UNCOV
3273
    mstDestroySStmAction(&newAction);
×
3274
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3275
  }
3276

3277
  return code;
6,120✔
3278
}
3279

3280
bool msmCheckStreamStartCond(int64_t streamId, int32_t snodeId) {
329,644✔
3281
  SStmStatus* pStream = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
329,644✔
3282
  if (NULL == pStream) {
329,644✔
UNCOV
3283
    return false;
×
3284
  }
3285

3286
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
329,644✔
UNCOV
3287
    return false;
×
3288
  }
3289

3290
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
329,644✔
3291
  for (int32_t i = 0; i < readerNum; ++i) {
724,045✔
3292
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
394,401✔
3293
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
394,401✔
UNCOV
3294
      return false;
×
3295
    }
3296
  }
3297

3298
  readerNum = taosArrayGetSize(pStream->trigOReaders);
329,644✔
3299
  for (int32_t i = 0; i < readerNum; ++i) {
393,690✔
3300
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
64,046✔
3301
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
64,046✔
UNCOV
3302
      return false;
×
3303
    }
3304
  }
3305

3306
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
329,644✔
3307
  SListNode* pNode = listHead(pStream->calcReaders);
329,644✔
3308
  for (int32_t i = 0; i < readerNum; ++i) {
849,807✔
3309
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
526,219✔
3310
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
526,219✔
3311
      return false;
6,056✔
3312
    }
3313
    pNode = TD_DLIST_NODE_NEXT(pNode);
520,163✔
3314
  }
3315

3316
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
1,122,858✔
3317
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
854,636✔
3318
    for (int32_t m = 0; m < runnerNum; ++m) {
1,687,128✔
3319
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
887,858✔
3320
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
887,858✔
3321
        return false;
55,366✔
3322
      }
3323
    }
3324
  }
3325
  
3326
  return true;
268,222✔
3327
}
3328

3329

3330
void msmHandleTaskAbnormalStatus(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pMsg, SStmTaskStatus* pTaskStatus) {
17,538,314✔
3331
  int32_t code = TSDB_CODE_SUCCESS;
17,538,314✔
3332
  int32_t lino = 0;
17,538,314✔
3333
  int32_t action = 0;
17,538,314✔
3334
  int64_t streamId = pMsg->streamId;
17,538,314✔
3335
  SStreamTask* pTask = (SStreamTask*)pMsg;
17,538,314✔
3336
  int8_t  stopped = 0;
17,538,314✔
3337

3338
  msttDebug("start to handle task abnormal status %d", pTask->status);
17,538,314✔
3339
  
3340
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
17,538,314✔
3341
  if (NULL == pStatus) {
17,538,314✔
UNCOV
3342
    msttInfo("stream no longer exists in streamMap, try to undeploy current task, idx:%d", pMsg->taskIdx);
×
3343
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3344
    return;
67,712✔
3345
  }
3346

3347
  stopped = atomic_load_8(&pStatus->stopped);
17,538,314✔
3348
  if (stopped) {
17,538,314✔
UNCOV
3349
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3350
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3351
    return;
×
3352
  }
3353
  
3354
  switch (pMsg->status) {
17,538,314✔
3355
    case STREAM_STATUS_INIT:      
17,534,282✔
3356
      if (STREAM_TRIGGER_TASK != pMsg->type) {
17,534,282✔
3357
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
16,999,371✔
3358
        return;
16,999,371✔
3359
      }
3360
      
3361
      if (INT64_MIN == pStatus->lastActionTs) {
534,911✔
UNCOV
3362
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3363
        return;
×
3364
      }
3365
      
3366
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
534,911✔
3367
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
205,267✔
3368
        return;
205,267✔
3369
      }
3370

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

3386
_exit:
333,676✔
3387

3388
  if (code) {
333,676✔
UNCOV
3389
    msmStopStreamByError(streamId, pStatus, code, pCtx->currTs);
×
3390
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3391
  }
3392
}
3393

3394
void msmHandleStreamTaskErr(SStmGrpCtx* pCtx, EStmErrType err, SStmTaskStatusMsg* pStatus) {
51,815✔
3395
  int32_t code = TSDB_CODE_SUCCESS;
51,815✔
3396
  int32_t lino = 0;
51,815✔
3397
  SStreamTask* pTask = (SStreamTask*)pStatus;
51,815✔
3398
  int64_t streamId = pStatus->streamId;
51,815✔
3399

3400
  msttInfo("start to handle task error, type: %d", err);
51,815✔
3401

3402
  TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
51,815✔
3403

3404
_exit:
51,815✔
3405

3406
  if (code) {
51,815✔
3407
    // IGNORE STOP STREAM BY ERROR  
UNCOV
3408
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3409
  }
3410
}
51,815✔
3411

3412
void msmChkHandleTriggerOperations(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask, SStmTaskStatus* pStatus) {
5,699,638✔
3413
  int32_t code = TSDB_CODE_SUCCESS;
5,699,638✔
3414
  int32_t lino = 0;
5,699,638✔
3415
  SStmStatus* pStream = (SStmStatus*)pStatus->pStream;
5,699,638✔
3416

3417
  if (1 == atomic_val_compare_exchange_8(&pStream->triggerNeedUpdate, 1, 0)) {
5,699,638✔
UNCOV
3418
    TAOS_CHECK_EXIT(msmGrpAddActionUpdateTrigger(pCtx->actionStm, pTask->streamId));
×
3419
  }
3420
  
3421
  SArray* userRecalcList = NULL;
5,699,638✔
3422
  if (atomic_load_ptr(&pStream->userRecalcList)) {
5,699,638✔
3423
    taosWLockLatch(&pStream->userRecalcLock);
6,120✔
3424
    if (pStream->userRecalcList) {
6,120✔
3425
      userRecalcList = pStream->userRecalcList;
6,120✔
3426
      pStream->userRecalcList = NULL;
6,120✔
3427
    }
3428
    taosWUnLockLatch(&pStream->userRecalcLock);
6,120✔
3429
    
3430
    if (userRecalcList) {
6,120✔
3431
      TAOS_CHECK_EXIT(msmGrpAddActionRecalc(pCtx, pTask->streamId, userRecalcList));
6,120✔
3432
    }
3433
  }
3434

3435
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
5,699,638✔
3436
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
2,861,501✔
3437
    if (NULL == pStatus->detailStatus) {
2,861,501✔
3438
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
196,455✔
3439
      if (NULL == pStatus->detailStatus) {
196,455✔
UNCOV
3440
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
UNCOV
3441
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3442
      }
3443
    }
3444
    
3445
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
2,861,501✔
3446
    taosWUnLockLatch(&pStatus->detailStatusLock);
2,861,501✔
3447
  }
3448

3449
_exit:
2,838,137✔
3450

3451
  if (code) {
5,699,638✔
3452
    // IGNORE STOP STREAM BY ERROR
UNCOV
3453
    msttError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3454
  }
3455
}
5,699,638✔
3456

3457
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,474,694✔
3458
  int32_t code = TSDB_CODE_SUCCESS;
1,474,694✔
3459
  int32_t lino = 0;
1,474,694✔
3460
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,474,694✔
3461

3462
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,474,694✔
3463

3464
  for (int32_t i = 0; i < num; ++i) {
45,339,633✔
3465
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
43,864,939✔
3466
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
43,864,939✔
3467
    
3468
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
43,864,939✔
3469
    if (NULL == ppStatus) {
43,864,939✔
3470
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
11,028✔
3471
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
11,028✔
3472
      continue;
11,028✔
3473
    }
3474

3475
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
43,853,911✔
3476
    int8_t stopped = atomic_load_8(&pStream->stopped);
43,853,911✔
3477
    if (stopped) {
43,853,911✔
3478
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
40,787✔
3479
      msmHandleStreamTaskErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
40,787✔
3480
      continue;
40,787✔
3481
    }
3482

3483
    if ((pTask->seriousId != (*ppStatus)->id.seriousId) || (pTask->nodeId != (*ppStatus)->id.nodeId)) {
43,813,124✔
3484
      msttInfo("task mismatch with it in taskMap, will try to rm it, current seriousId:%" PRId64 ", nodeId:%d", 
×
3485
          (*ppStatus)->id.seriousId, (*ppStatus)->id.nodeId);
3486
          
NEW
3487
      msmHandleStreamTaskErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
×
UNCOV
3488
      continue;
×
3489
    }
3490

3491
    if ((*ppStatus)->status != pTask->status) {
43,813,124✔
3492
      if (STREAM_STATUS_RUNNING == pTask->status) {
2,439,852✔
3493
        (*ppStatus)->runningStartTs = pCtx->currTs;
1,042,926✔
3494
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
1,396,926✔
UNCOV
3495
        if (pStream->triggerTask) {
×
3496
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3497
        }
3498
        
UNCOV
3499
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3500
      }
3501
    }
3502
    
3503
    (*ppStatus)->errCode = pTask->errorCode;
43,813,124✔
3504
    (*ppStatus)->status = pTask->status;
43,813,124✔
3505
    (*ppStatus)->lastUpTs = pCtx->currTs;
43,813,124✔
3506
    
3507
    if (STREAM_STATUS_RUNNING != pTask->status) {
43,813,124✔
3508
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
17,538,314✔
3509
    }
3510
    
3511
    if (STREAM_TRIGGER_TASK == pTask->type) {
43,813,124✔
3512
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
5,699,638✔
3513
    }
3514
  }
3515

3516
_exit:
1,474,694✔
3517

3518
  if (code) {
1,474,694✔
UNCOV
3519
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3520
  }
3521

3522
  return code;
1,474,694✔
3523
}
3524

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

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

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

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

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

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

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

3622
_exit:
×
3623

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

3630
  return code;
×
3631
}
3632

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

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

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

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

3661
_exit:
×
3662

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

UNCOV
3667
  return code;
×
3668
}
3669

3670
void msmRspAddStreamStart(int64_t streamId, SStmGrpCtx* pCtx, int32_t streamNum, SStmAction *pAction) {
268,222✔
3671
  int32_t code = TSDB_CODE_SUCCESS;
268,222✔
3672
  int32_t lino = 0;
268,222✔
3673
  if (NULL == pCtx->pRsp->start.taskList) {
268,222✔
3674
    pCtx->pRsp->start.taskList = taosArrayInit(streamNum, sizeof(SStreamTaskStart));
111,992✔
3675
    TSDB_CHECK_NULL(pCtx->pRsp->start.taskList, code, lino, _exit, terrno);
111,992✔
3676
  }
3677

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

3687
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
536,444✔
3688
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
268,222✔
3689

3690
  mstsDebug("stream START added to dnode %d hb rsp, triggerTaskId:%" PRIx64, pId->nodeId, pId->taskId);
268,222✔
3691

3692
  return;
268,222✔
3693

UNCOV
3694
_exit:
×
3695

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

3699

3700
void msmRspAddStreamUndeploy(int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
21,226✔
3701
  int32_t code = TSDB_CODE_SUCCESS;
21,226✔
3702
  int32_t lino = 0;
21,226✔
3703
  int32_t dropNum = taosArrayGetSize(pAction->undeploy.taskList);
21,226✔
3704
  if (NULL == pCtx->pRsp->undeploy.taskList) {
21,226✔
3705
    pCtx->pRsp->undeploy.taskList = taosArrayInit(dropNum, sizeof(SStreamTaskUndeploy));
18,026✔
3706
    TSDB_CHECK_NULL(pCtx->pRsp->undeploy.taskList, code, lino, _exit, terrno);
18,026✔
3707
  }
3708

3709
  SStreamTaskUndeploy undeploy;
21,226✔
3710
  for (int32_t i = 0; i < dropNum; ++i) {
77,073✔
3711
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
55,847✔
3712
    undeploy.task = *pTask;
55,847✔
3713
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
55,847✔
3714
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
55,847✔
3715

3716
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
111,694✔
3717
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
55,847✔
3718

3719
    msttDebug("task UNDEPLOY added to hb rsp, doCheckpoint:%d, doCleanup:%d", undeploy.undeployMsg.doCheckpoint, undeploy.undeployMsg.doCleanup);
55,847✔
3720
  }
3721

3722
  return;
21,226✔
3723

UNCOV
3724
_exit:
×
3725

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

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

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

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

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

3750
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pStream, streamId, &rsp.cont.runnerList));  
×
3751

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

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

3759
_exit:
×
3760

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

3768
void msmRspAddUserRecalc(SMnode * pMnode, int64_t streamId, SStmGrpCtx* pCtx, SStmAction *pAction) {
6,120✔
3769
  int32_t code = TSDB_CODE_SUCCESS;
6,120✔
3770
  int32_t lino = 0;
6,120✔
3771

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

3778
  if (NULL == pStream->triggerTask) {
6,120✔
UNCOV
3779
    mstsWarn("no triggerTask exists, ignore trigger update, stopped:%d", atomic_load_8(&pStream->stopped));
×
UNCOV
3780
    return;
×
3781
  }
3782

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

3790
  if (NULL == pCtx->pRsp->rsps.rspList) {
6,120✔
3791
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
5,760✔
3792
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
5,760✔
3793
  }
3794

3795
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
12,240✔
3796

3797
_exit:
6,120✔
3798

3799
  if (code) {
6,120✔
UNCOV
3800
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3801
  } else {
3802
    mstsDebug("user recalc rsp added, recalcNum:%d", (int32_t)taosArrayGetSize(rsp.cont.recalcList));
6,120✔
3803
  }
3804
}
3805

3806

3807
int32_t msmHandleHbPostActions(SStmGrpCtx* pCtx) {
135,424✔
3808
  int32_t code = TSDB_CODE_SUCCESS;
135,424✔
3809
  int32_t lino = 0;
135,424✔
3810
  void* pIter = NULL;
135,424✔
3811
  int32_t streamNum = taosHashGetSize(pCtx->actionStm);
135,424✔
3812

3813
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
135,424✔
3814

3815
  while (1) {
295,568✔
3816
    pIter = taosHashIterate(pCtx->actionStm, pIter);
430,992✔
3817
    if (pIter == NULL) {
430,992✔
3818
      break;
135,424✔
3819
    }
3820

3821
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
295,568✔
3822
    SStmAction *pAction = (SStmAction *)pIter;
295,568✔
3823
    
3824
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
295,568✔
3825
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
21,226✔
3826
      continue;
21,226✔
3827
    }
3828

3829
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
274,342✔
UNCOV
3830
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3831
    }
3832

3833
    if (STREAM_ACT_RECALC & pAction->actions) {
274,342✔
3834
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
6,120✔
3835
    }
3836

3837
    if (STREAM_ACT_START & pAction->actions) {
274,342✔
3838
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
268,222✔
3839
    }
3840
  }
3841
  
3842
_exit:
135,424✔
3843

3844
  if (pIter) {
135,424✔
UNCOV
3845
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3846
  }
3847

3848
  if (code) {
135,424✔
UNCOV
3849
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3850
  }
3851

3852
  return code;
135,424✔
3853
}
3854

3855
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
14,598,430✔
3856
  int32_t  code = TSDB_CODE_SUCCESS;
14,598,430✔
3857
  int32_t  lino = 0;
14,598,430✔
3858
  int64_t* lastTs = NULL;
14,598,430✔
3859
  bool     noExists = false;
14,598,430✔
3860

3861
  while (true) {
3862
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
15,094,298✔
3863
    if (NULL == lastTs) {
15,094,298✔
3864
      if (noExists) {
633,026✔
3865
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
137,158✔
3866
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
137,158✔
3867
      }
3868

3869
      noExists = true;
495,868✔
3870
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
495,868✔
3871
      
3872
      continue;
495,868✔
3873
    }
3874

UNCOV
3875
    while (true) {
×
3876
      int64_t lastTsValue = atomic_load_64(lastTs);
14,461,272✔
3877
      if (pCtx->currTs > lastTsValue) {
14,461,272✔
3878
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
14,460,391✔
3879
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
14,460,391✔
3880
          return code;
14,460,391✔
3881
        }
3882

UNCOV
3883
        continue;
×
3884
      }
3885

3886
      return code;
881✔
3887
    }
3888

3889
    break;
3890
  }
3891

3892
_exit:
137,158✔
3893

3894
  if (code) {
137,158✔
3895
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
137,158✔
3896
  }
3897

3898
  return code;  
137,158✔
3899
}
3900

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

3914
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
3915
    pStatus = (SStmStatus*)pIter;
×
3916

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

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

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

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

3953
  if (0 != atomic_val_compare_exchange_8(&mStreamMgmt.watch.ending, 0, 1)) {
260✔
UNCOV
3954
    return code;
×
3955
  }
3956

3957
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
260✔
UNCOV
3958
    (void)sched_yield();
×
3959
  }
3960

3961
  if (watchError) {
260✔
3962
    taosHashClear(mStreamMgmt.vgroupMap);
×
3963
    taosHashClear(mStreamMgmt.snodeMap);
×
3964
    taosHashClear(mStreamMgmt.taskMap);
×
UNCOV
3965
    taosHashClear(mStreamMgmt.streamMap);
×
UNCOV
3966
    mstInfo("watch error happends, clear all maps");
×
UNCOV
3967
    goto _exit;
×
3968
  }
3969

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

UNCOV
3975
  msmWatchCheckStreamMap(pCtx);
×
3976

3977
_exit:
260✔
3978

3979
  mStreamMgmt.lastTaskId += 100000;
260✔
3980

3981
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
260✔
3982

3983
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
260✔
3984

3985
  if (code) {
260✔
UNCOV
3986
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3987
  }
3988

3989
  return code;
260✔
3990
}
3991

3992

3993
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
1,040✔
3994
  int32_t code = TSDB_CODE_SUCCESS;
1,040✔
3995
  int32_t lino = 0;
1,040✔
3996
  SStreamHbMsg* pReq = pCtx->pReq;
1,040✔
3997

3998
  (void)atomic_add_fetch_32(&mStreamMgmt.watch.processing, 1);
1,040✔
3999
  
4000
  if (atomic_load_8(&mStreamMgmt.watch.ending)) {
1,040✔
UNCOV
4001
    goto _exit;
×
4002
  }
4003

4004
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
1,040✔
4005
  if (GOT_SNODE(pReq->snodeId)) {
1,040✔
4006
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,040✔
4007
  }
4008

4009
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
1,040✔
UNCOV
4010
    atomic_store_8(&mStreamMgmt.watch.taskRemains, 1);
×
UNCOV
4011
    TAOS_CHECK_EXIT(msmWatchHandleStatusUpdate(pCtx));
×
4012
  }
4013

4014
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
1,040✔
4015
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
260✔
4016
  }
4017

4018
_exit:
1,040✔
4019

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

UNCOV
4025
    (void)msmWatchHandleEnding(pCtx, true);
×
4026
  }
4027

4028
  return code;
1,040✔
4029
}
4030

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

4037
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
81,202✔
4038
  for (int32_t i = 0; i < readerNum; ++i) {
161,884✔
4039
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
97,838✔
4040
    if (pReader->id.nodeId == vgId) {
97,838✔
4041
      readerExists = true;
17,156✔
4042
      break;
17,156✔
4043
    }
4044
  }
4045

4046
  if (!readerExists) {
81,202✔
4047
    if (NULL == pStatus->trigOReaders) {
64,046✔
4048
      pStatus->trigOReaders = taosArrayInit(vgNum, sizeof(SStmTaskStatus));
56,088✔
4049
      TSDB_CHECK_NULL(pStatus->trigOReaders, code, lino, _exit, terrno);
56,088✔
4050
    }
4051
    
4052
    SStmTaskStatus* pState = taosArrayReserve(pStatus->trigOReaders, 1);
64,046✔
4053
    TAOS_CHECK_EXIT(msmTDAddSingleTrigReader(pCtx, pState, vgId, pStatus, streamId));
64,046✔
4054
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pState));
64,046✔
4055
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pState, true));
64,046✔
4056
  }
4057

4058
_exit:
81,202✔
4059

4060
  if (code) {
81,202✔
UNCOV
4061
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4062
  }
4063

4064
  return code;
81,202✔
4065
}
4066

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

4085
  if (NULL == pCtx->pRsp->rsps.rspList) {
72,524✔
NEW
4086
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
NEW
4087
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4088
  }
4089
  
4090
  TSWAP(pTask->pMgmtReq, pMgmtReq);
72,524✔
4091
  rsp.task = *(SStreamTask*)pTask;
72,524✔
4092

4093
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
72,524✔
4094
  if (NULL == pStatus) {
72,524✔
UNCOV
4095
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
4096
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4097
  }
4098

4099
  stopped = atomic_load_8(&pStatus->stopped);
72,524✔
4100
  if (stopped) {
72,524✔
UNCOV
4101
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
UNCOV
4102
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4103
  }
4104

4105
  if (tbNum <= 0) {
72,524✔
UNCOV
4106
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
UNCOV
4107
    goto _exit;
×
4108
  }
4109

4110
  int32_t oReaderNum = taosArrayGetSize(pStatus->trigOReaders);
72,524✔
4111
  if (oReaderNum > 0) {
72,524✔
UNCOV
4112
    mstsWarn("origReaders already exits, num:%d", oReaderNum);
×
UNCOV
4113
    goto _exit;
×
4114
  }
4115

4116
  TAOS_CHECK_EXIT(mstBuildDBVgroupsMap(pCtx->pMnode, &pDbVgroups));
72,524✔
4117
  rsp.cont.vgIds = taosArrayInit(tbNum, sizeof(int32_t));
72,524✔
4118
  TSDB_CHECK_NULL(rsp.cont.vgIds, code, lino, _exit, terrno);
72,524✔
4119

4120
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
72,524✔
4121
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
72,524✔
4122
  
4123
  for (int32_t i = 0; i < tbNum; ++i) {
275,450✔
4124
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
202,926✔
4125
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
202,926✔
4126
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
405,852✔
4127
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
202,926✔
4128
  }
4129

4130
  int32_t vgNum = tSimpleHashGetSize(pVgs);
72,524✔
4131
  while (true) {
4132
    p = tSimpleHashIterate(pVgs, p, &iter);
153,726✔
4133
    if (NULL == p) {
153,726✔
4134
      break;
72,524✔
4135
    }
4136
    
4137
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
81,202✔
4138
  }
4139
  
4140
  vgNum = taosArrayGetSize(pStatus->trigOReaders);
72,524✔
4141
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
72,524✔
4142
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
72,524✔
4143

4144
  SStreamTaskAddr addr;
72,524✔
4145
  for (int32_t i = 0; i < vgNum; ++i) {
136,570✔
4146
    SStmTaskStatus* pOTask = taosArrayGet(pStatus->trigOReaders, i);
64,046✔
4147
    addr.taskId = pOTask->id.taskId;
64,046✔
4148
    addr.nodeId = pOTask->id.nodeId;
64,046✔
4149
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
64,046✔
4150
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
128,092✔
4151
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
64,046✔
4152
  }
4153

4154
_exit:
72,524✔
4155

4156
  tFreeSStreamMgmtReq(pMgmtReq);
72,524✔
4157
  taosMemoryFree(pMgmtReq);
72,524✔
4158

4159
  tSimpleHashCleanup(pVgs);
72,524✔
4160
  mstDestroyDbVgroupsHash(pDbVgroups);
72,524✔
4161

4162
  if (code) {
72,524✔
NEW
4163
    rsp.code = code;
×
4164
    
NEW
4165
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
4166

UNCOV
4167
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4168
  } else {
4169
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
145,048✔
4170
  }
4171

4172
_final:
72,524✔
4173

4174
  if (finalCode) {
72,524✔
NEW
4175
    tFreeSStreamMgmtRsp(&rsp);
×
NEW
4176
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
NEW
4177
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4178
  }
4179

4180
  return finalCode;
72,524✔
4181
}
4182

4183
int32_t msmGetCalcScanFromList(int64_t streamId, SArray* pList, int64_t uid, SStreamCalcScan** ppRes) {
4,312✔
4184
  int32_t num = taosArrayGetSize(pList);
4,312✔
4185
  SStreamCalcScan* pScan = NULL;
4,312✔
4186
  int32_t code = TSDB_CODE_SUCCESS, lino = 0;
4,312✔
4187
  int64_t planUid = 0;
4,312✔
4188
  for (int32_t i = 0; i < num; ++i) {
4,312✔
4189
    pScan = (SStreamCalcScan*)taosArrayGet(pList, i);
4,312✔
4190
    TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, pScan->scanPlan, &planUid));
4,312✔
4191
    if (0 != planUid && planUid == uid) {
4,312✔
4192
      *ppRes = pScan;
4,312✔
4193
      break;
4,312✔
4194
    }
4195
  }
4196

4197
_exit:
4,312✔
4198

4199
  if (code) {
4,312✔
UNCOV
4200
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4201
  }
4202

4203
  return code;
4,312✔
4204
}
4205

4206
int32_t msmCheckDeployCalcReader(SStmGrpCtx* pCtx, SStmStatus* pStatus, SStmTaskStatusMsg* pTask, int32_t vgId, int64_t uid, SStreamTaskAddr* pAddr) {
4,312✔
4207
  int32_t code = TSDB_CODE_SUCCESS;
4,312✔
4208
  int32_t lino = 0;
4,312✔
4209
  bool    readerExists = false;
4,312✔
4210
  int64_t streamId = pTask->streamId;
4,312✔
4211
  SListNode* pNode = listHead(pStatus->calcReaders);
4,312✔
4212
  SStmTaskStatus* pReader = NULL;
4,312✔
4213
  int32_t taskIdx = 0;
4,312✔
4214

4215
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
4,312✔
4216
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
39,984✔
4217
    pReader = (SStmTaskStatus*)pNode->data;
35,672✔
4218
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
35,672✔
UNCOV
4219
      readerExists = true;
×
UNCOV
4220
      pAddr->taskId = pReader->id.taskId;
×
UNCOV
4221
      break;
×
4222
    }
4223
  }
4224

4225
  if (!readerExists) {
4,312✔
4226
    if (NULL == pStatus->calcReaders) {
4,312✔
4227
      pStatus->calcReaders = tdListNew(sizeof(SStmTaskStatus));
×
4228
      TSDB_CHECK_NULL(pStatus->calcReaders, code, lino, _exit, terrno);
×
UNCOV
4229
      taskIdx = 0;
×
4230
    } else {
4231
      pNode = listTail(pStatus->calcReaders);
4,312✔
4232
      pReader = (SStmTaskStatus*)pNode->data;
4,312✔
4233
      taskIdx = pReader->id.taskIdx + 1;
4,312✔
4234
    }
4235

4236
    SStreamCalcScan* pScan = NULL;
4,312✔
4237
    TAOS_CHECK_EXIT(msmGetCalcScanFromList(streamId, pStatus->pCreate->calcScanPlanList, uid, &pScan));
4,312✔
4238
    TSDB_CHECK_NULL(pScan, code, lino, _exit, TSDB_CODE_STREAM_INTERNAL_ERROR);
4,312✔
4239
    pReader = tdListReserve(pStatus->calcReaders);
4,312✔
4240
    TAOS_CHECK_EXIT(msmTDAddSingleCalcReader(pCtx, pReader, taskIdx, vgId, pScan->scanPlan, pStatus, streamId));
4,312✔
4241
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pReader));
4,312✔
4242
    TAOS_CHECK_EXIT(msmSTAddToVgroupMap(pCtx, streamId, NULL, NULL, pReader, false));
4,312✔
4243
    pAddr->taskId = pReader->id.taskId;
4,312✔
4244
  }
4245

4246
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
4,312✔
4247
  pAddr->nodeId = vgId;
4,312✔
4248

4249
_exit:
4,312✔
4250

4251
  if (code) {
4,312✔
UNCOV
4252
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4253
  }
4254

4255
  return code;
4,312✔
4256
}
4257

4258

4259
int32_t msmDeployRunnerOrigReader(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
4,312✔
4260
  int32_t code = TSDB_CODE_SUCCESS, finalCode = TSDB_CODE_SUCCESS;
4,312✔
4261
  int32_t lino = 0;
4,312✔
4262
  int32_t vgId = 0;
4,312✔
4263
  int64_t streamId = pTask->streamId;
4,312✔
4264
  SArray* pReqs = pTask->pMgmtReq->cont.pReqs;
4,312✔
4265
  int32_t reqNum = taosArrayGetSize(pReqs);
4,312✔
4266
  SStreamOReaderDeployReq* pReq = NULL;
4,312✔
4267
  SStreamOReaderDeployRsp* pRsp = NULL;
4,312✔
4268
  SStreamMgmtRsp rsp = {0};
4,312✔
4269
  rsp.reqId = pTask->pMgmtReq->reqId;
4,312✔
4270
  rsp.header.msgType = STREAM_MSG_RUNNER_ORIGTBL_READER;
4,312✔
4271
  SStreamMgmtReq* pMgmtReq = NULL;
4,312✔
4272
  int8_t stopped = 0;
4,312✔
4273
  int32_t vgNum = 0;
4,312✔
4274
  SStreamTaskAddr* pAddr = NULL;
4,312✔
4275

4276
  if (NULL == pCtx->pRsp->rsps.rspList) {
4,312✔
NEW
4277
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
NEW
4278
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, finalCode, lino, _final, terrno);
×
4279
  }
4280
  
4281
  TSWAP(pTask->pMgmtReq, pMgmtReq);
4,312✔
4282
  rsp.task = *(SStreamTask*)pTask;
4,312✔
4283

4284
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
4,312✔
4285
  if (NULL == pStatus) {
4,312✔
UNCOV
4286
    mstsError("stream not deployed, remainStreams:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
4287
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NOT_RUNNING);
×
4288
  }
4289

4290
  stopped = atomic_load_8(&pStatus->stopped);
4,312✔
4291
  if (stopped) {
4,312✔
UNCOV
4292
    msttInfo("stream stopped %d, ignore deploy trigger reader, vgId:%d", stopped, vgId);
×
UNCOV
4293
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_STOPPED);
×
4294
  }
4295

4296
  if (reqNum <= 0) {
4,312✔
UNCOV
4297
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4298
    goto _exit;
×
4299
  }
4300

4301
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
4,312✔
4302
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
4,312✔
4303

4304
  for (int32_t i = 0; i < reqNum; ++i) {
8,624✔
4305
    pReq = (SStreamOReaderDeployReq*)taosArrayGet(pReqs, i);
4,312✔
4306
    pRsp = (SStreamOReaderDeployRsp*)taosArrayGet(rsp.cont.execRspList, i);
4,312✔
4307
    pRsp->execId = pReq->execId;
4,312✔
4308
    vgNum = taosArrayGetSize(pReq->vgIds);
4,312✔
4309
    pRsp->vgList = taosArrayInit_s(sizeof(SStreamTaskAddr), vgNum);
4,312✔
4310
    TSDB_CHECK_NULL(pRsp->vgList, code, lino, _exit, terrno);
4,312✔
4311
    
4312
    for (int32_t n = 0; n < vgNum; ++n) {
8,624✔
4313
      vgId = *(int32_t*)taosArrayGet(pReq->vgIds, n);
4,312✔
4314
      pAddr = taosArrayGet(pRsp->vgList, n);
4,312✔
4315
      TAOS_CHECK_EXIT(msmCheckDeployCalcReader(pCtx, pStatus, pTask, vgId, pReq->uid, pAddr));
4,312✔
4316
    }
4317
  }
4318

4319
_exit:
4,312✔
4320

4321
  tFreeSStreamMgmtReq(pMgmtReq);
4,312✔
4322
  taosMemoryFree(pMgmtReq);
4,312✔
4323

4324
  if (code) {
4,312✔
NEW
4325
    rsp.code = code;
×
4326
    
NEW
4327
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
×
UNCOV
4328
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4329
  } else {
4330
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), finalCode, lino, _final, terrno);
8,624✔
4331
  }
4332

4333

4334
_final:
4,312✔
4335

4336
  if (finalCode) {
4,312✔
NEW
4337
    tFreeSStreamMgmtRsp(&rsp);
×
NEW
4338
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code)); 
×
NEW
4339
    msmHandleStreamTaskErr(pCtx, STM_ERR_PROCESSING_ERR, pTask);
×
4340
  }
4341

4342
  return finalCode;
4,312✔
4343
}
4344

4345

4346
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
76,836✔
4347
  int32_t code = TSDB_CODE_SUCCESS;
76,836✔
4348
  int32_t lino = 0;
76,836✔
4349

4350
  switch (pTask->pMgmtReq->type) {
76,836✔
4351
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
72,524✔
4352
      msmDeployTriggerOrigReader(pCtx, pTask);
72,524✔
4353
      break;
72,524✔
4354
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
4,312✔
4355
      msmDeployRunnerOrigReader(pCtx, pTask);
4,312✔
4356
      break;
4,312✔
4357
    default:
×
UNCOV
4358
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
UNCOV
4359
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
UNCOV
4360
      break;
×
4361
  }
4362

4363
_exit:
76,836✔
4364

4365
  if (code) {
76,836✔
UNCOV
4366
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4367
  }
4368

4369
  return code;
76,836✔
4370
}
4371

4372
int32_t msmHandleStreamRequests(SStmGrpCtx* pCtx) {
26,157✔
4373
  int32_t code = TSDB_CODE_SUCCESS;
26,157✔
4374
  int32_t lino = 0;
26,157✔
4375
  SStreamHbMsg* pReq = pCtx->pReq;
26,157✔
4376
  SStmTaskStatusMsg* pTask = NULL;
26,157✔
4377
  
4378
  int32_t reqNum = taosArrayGetSize(pReq->pStreamReq);
26,157✔
4379
  if (reqNum > 0 && NULL == pCtx->pRsp->rsps.rspList) {
26,157✔
4380
    pCtx->pRsp->rsps.rspList = taosArrayInit(reqNum, sizeof(SStreamMgmtRsp));
26,157✔
4381
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
26,157✔
4382
  }
4383
  
4384
  for (int32_t i = 0; i < reqNum; ++i) {
102,993✔
4385
    int32_t idx = *(int32_t*)taosArrayGet(pReq->pStreamReq, i);
76,836✔
4386
    pTask = (SStmTaskStatusMsg*)taosArrayGet(pReq->pStreamStatus, idx);
76,836✔
4387
    if (NULL == pTask) {
76,836✔
UNCOV
4388
      mstError("idx %d is NULL, reqNum:%d", idx, reqNum);
×
UNCOV
4389
      continue;
×
4390
    }
4391

4392
    if (NULL == pTask->pMgmtReq) {
76,836✔
UNCOV
4393
      msttError("idx %d without mgmtReq", idx);
×
UNCOV
4394
      continue;
×
4395
    }
4396

4397
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
76,836✔
4398
  }
4399

4400
_exit:
26,157✔
4401

4402
  if (code) {
26,157✔
UNCOV
4403
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4404
  }
4405

4406
  return code;
26,157✔
4407
}
4408

4409
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
14,597,390✔
4410
  int32_t code = TSDB_CODE_SUCCESS;
14,597,390✔
4411
  int32_t lino = 0;
14,597,390✔
4412
  SStreamHbMsg* pReq = pCtx->pReq;
14,597,390✔
4413

4414
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
14,597,390✔
4415
  if (GOT_SNODE(pReq->snodeId)) {
14,460,232✔
4416
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,542,989✔
4417
  }
4418
  
4419
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
14,459,805✔
4420
    msmHandleStreamActions(pCtx);
45,811✔
4421
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
45,811✔
4422
  }
4423

4424
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
14,459,805✔
4425
    code = msmHandleStreamRequests(pCtx);
26,157✔
4426
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
26,157✔
4427
    TAOS_CHECK_EXIT(code);
26,157✔
4428
  }
4429

4430
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,459,805✔
4431
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
76,630✔
4432
  } else {
4433
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
14,383,175✔
4434
  }
4435

4436
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
14,459,805✔
4437
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
86,022✔
4438
  }
4439

4440
  if (taosHashGetSize(pCtx->deployStm) > 0) {
14,459,805✔
4441
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
72,797✔
4442
  }
4443

4444
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
14,459,805✔
4445
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,474,694✔
4446
  }
4447

4448
  if (taosHashGetSize(pCtx->actionStm) > 0) {
14,459,805✔
4449
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
135,424✔
4450
  }
4451

4452
_exit:
14,459,805✔
4453

4454
  if (code) {
14,597,390✔
4455
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
137,585✔
4456
  }
4457

4458
  return code;
14,597,390✔
4459
}
4460

4461
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
14,781,744✔
4462
  int32_t lino = 0;
14,781,744✔
4463
  int32_t tlen = 0;
14,781,744✔
4464
  void   *buf = NULL;
14,781,744✔
4465

4466
  if (TSDB_CODE_SUCCESS != code) {
14,781,744✔
4467
    goto _exit;
137,585✔
4468
  }
4469

4470
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
14,644,159✔
4471
  if (code < 0) {
14,644,159✔
UNCOV
4472
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
UNCOV
4473
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4474
  }
4475

4476
  buf = rpcMallocCont(tlen + sizeof(SStreamMsgGrpHeader));
14,644,159✔
4477
  if (buf == NULL) {
14,644,159✔
UNCOV
4478
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(terrno));
×
UNCOV
4479
    TAOS_CHECK_EXIT(terrno);    
×
4480
  }
4481

4482
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
14,644,159✔
4483
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
14,644,159✔
4484

4485
  SEncoder encoder;
14,628,312✔
4486
  tEncoderInit(&encoder, abuf, tlen);
14,644,159✔
4487
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
14,644,159✔
UNCOV
4488
    rpcFreeCont(buf);
×
UNCOV
4489
    buf = NULL;
×
4490
    tEncoderClear(&encoder);
×
4491
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
UNCOV
4492
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4493
  }
4494
  tEncoderClear(&encoder);
14,644,159✔
4495

4496
_exit:
14,781,744✔
4497

4498
  pMsg->code = code;
14,781,744✔
4499
  pMsg->info = *pRpcInfo;
14,781,744✔
4500
  if (TSDB_CODE_SUCCESS == code) {
14,781,744✔
4501
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
14,644,159✔
4502
    pMsg->pCont = buf;
14,644,159✔
4503
  }
4504
}
14,781,744✔
4505

4506

4507
int32_t msmHandleStreamHbMsg(SMnode* pMnode, int64_t currTs, SStreamHbMsg* pHb, SRpcMsg *pReq, SRpcMsg* pRspMsg) {
14,781,744✔
4508
  int32_t code = TSDB_CODE_SUCCESS;
14,781,744✔
4509
  SMStreamHbRspMsg rsp = {0};
14,781,744✔
4510
  rsp.streamGId = pHb->streamGId;
14,781,744✔
4511

4512
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
14,781,744✔
4513

4514
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
14,781,744✔
4515
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
183,314✔
4516
    goto _exit;
183,314✔
4517
  }
4518

4519
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,598,430✔
4520
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
14,598,430✔
4521

4522
  pCtx->tidx = tidx;
14,598,430✔
4523
  pCtx->pMnode = pMnode;
14,598,430✔
4524
  pCtx->currTs = currTs;
14,598,430✔
4525
  pCtx->pReq = pHb;
14,598,430✔
4526
  pCtx->pRsp = &rsp;
14,598,430✔
4527
  pCtx->deployStm = mStreamMgmt.tCtx[pCtx->tidx].deployStm[pHb->streamGId];
14,598,430✔
4528
  pCtx->actionStm = mStreamMgmt.tCtx[pCtx->tidx].actionStm[pHb->streamGId];
14,598,430✔
4529
  
4530
  switch (atomic_load_8(&mStreamMgmt.state)) {
14,598,430✔
4531
    case MND_STM_STATE_WATCH:
1,040✔
4532
      code = msmWatchHandleHbMsg(pCtx);
1,040✔
4533
      break;
1,040✔
4534
    case MND_STM_STATE_NORMAL:
14,597,390✔
4535
      code = msmNormalHandleHbMsg(pCtx);
14,597,390✔
4536
      break;
14,597,390✔
UNCOV
4537
    default:
×
UNCOV
4538
      mstError("Invalid stream state: %d", mStreamMgmt.state);
×
UNCOV
4539
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
UNCOV
4540
      break;
×
4541
  }
4542

4543
_exit:
14,781,744✔
4544

4545
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
14,781,744✔
4546

4547
  msmCleanStreamGrpCtx(pHb);
14,781,744✔
4548
  msmClearStreamToDeployMaps(pHb);
14,781,744✔
4549

4550
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
14,781,744✔
4551
  
4552
  tFreeSMStreamHbRspMsg(&rsp);
14,781,744✔
4553

4554
  return code;
14,781,744✔
4555
}
4556

4557
void msmHandleBecomeLeader(SMnode *pMnode) {
414,729✔
4558
  if (tsDisableStream) {
414,729✔
UNCOV
4559
    return;
×
4560
  }
4561

4562
  mstInfo("start to process mnode become leader");
414,729✔
4563

4564
  int32_t code = 0;
414,729✔
4565
  streamAddVnodeLeader(MNODE_HANDLE);
414,729✔
4566
  
4567
  taosWLockLatch(&mStreamMgmt.runtimeLock);
414,729✔
4568
  msmDestroyRuntimeInfo(pMnode);
414,729✔
4569
  code = msmInitRuntimeInfo(pMnode);
414,729✔
4570
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
414,729✔
4571

4572
  if (TSDB_CODE_SUCCESS == code) {
414,729✔
4573
    atomic_store_8(&mStreamMgmt.active, 1);
414,729✔
4574
  }
4575

4576
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
414,729✔
4577
}
4578

4579
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
609,475✔
4580
  if (tsDisableStream) {
609,475✔
UNCOV
4581
    return;
×
4582
  }
4583

4584
  mstInfo("start to process mnode become not leader");
609,475✔
4585

4586
  streamRemoveVnodeLeader(MNODE_HANDLE);
609,475✔
4587

4588
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
609,475✔
4589
    taosWLockLatch(&mStreamMgmt.runtimeLock);
414,729✔
4590
    msmDestroyRuntimeInfo(pMnode);
414,729✔
4591
    mStreamMgmt.stat.inactiveTimes++;
414,729✔
4592
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
414,729✔
4593
  }
4594
}
4595

4596

UNCOV
4597
static void msmRedeployStream(int64_t streamId, SStmStatus* pStatus) {
×
UNCOV
4598
  if (1 == atomic_val_compare_exchange_8(&pStatus->stopped, 1, 0)) {
×
4599
    mstsInfo("try to reset and redeploy stream, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4600
    mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4601
  } else {
UNCOV
4602
    mstsWarn("stream stopped %d already changed", atomic_load_8(&pStatus->stopped));
×
4603
  }
UNCOV
4604
}
×
4605

4606
static bool msmCheckStreamAssign(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
13,520✔
4607
  int32_t code = TSDB_CODE_SUCCESS;
13,520✔
4608
  int32_t lino = 0;
13,520✔
4609
  SStreamObj* pStream = pObj;
13,520✔
4610
  SSnodeObj* pSnode = p1;
13,520✔
4611
  SArray** ppRes = p2;
13,520✔
4612

4613
  if (pStream->mainSnodeId == pSnode->id) {
13,520✔
4614
    if (NULL == *ppRes) {
7,597✔
4615
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,481✔
4616
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
1,481✔
4617
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
1,481✔
4618
    }
4619

4620
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
15,194✔
4621
  }
4622

4623
  return true;
13,520✔
4624

UNCOV
4625
_exit:
×
4626

UNCOV
4627
  if (code) {
×
UNCOV
4628
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4629
  }  
4630

UNCOV
4631
  *(int32_t*)p3 = code;
×
4632

UNCOV
4633
  return false;
×
4634
}
4635

4636

4637
int32_t msmCheckSnodeReassign(SMnode *pMnode, SSnodeObj* pSnode, SArray** ppRes) {
67,159✔
4638
  int32_t code = TSDB_CODE_SUCCESS;
67,159✔
4639
  int32_t lino = 0;
67,159✔
4640
  
4641
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckStreamAssign, pSnode, ppRes, &code);
67,159✔
4642
  TAOS_CHECK_EXIT(code);
67,159✔
4643

4644
  int32_t streamNum = taosArrayGetSize(*ppRes);
67,159✔
4645
  if (streamNum > 0 && 0 == pSnode->replicaId) {
67,159✔
4646
    mstError("snode %d has no replica while %d streams assigned", pSnode->id, streamNum);
360✔
4647
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_SNODE_IN_USE);
360✔
4648
  }
4649

4650
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4651

4652
_exit:
67,159✔
4653

4654
  if (code) {
67,159✔
4655
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
360✔
4656
  }  
4657

4658
  return code;
67,159✔
4659
}
4660

4661
static bool msmCheckLoopStreamSdb(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
583,194✔
4662
  SStreamObj* pStream = pObj;
583,194✔
4663
  int64_t streamId = pStream->pCreate->streamId;
583,194✔
4664
  SStmStatus* pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
583,194✔
4665
  SStmCheckStatusCtx* pCtx = (SStmCheckStatusCtx*)p1;
583,194✔
4666
  int8_t userDropped = atomic_load_8(&pStream->userDropped), userStopped = atomic_load_8(&pStream->userStopped);
583,194✔
4667
  
4668
  if ((userDropped || userStopped) && (NULL == pStatus)) {
583,194✔
4669
    mstsDebug("stream userDropped %d userStopped %d and not in streamMap, ignore it", userDropped, userStopped);
360✔
4670
    return true;
360✔
4671
  }
4672
  
4673
  if (pStatus && !MST_STM_PASS_ISOLATION(pStream, pStatus)) {
582,834✔
4674
    mstsDebug("stream not pass isolation time, updateTime:%" PRId64 ", lastActionTs:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
424,719✔
4675
        pStream->updateTime, pStatus->lastActionTs, mStreamMgmt.hCtx.currentTs);
4676
    return true;
424,719✔
4677
  }
4678

4679
  if (NULL == pStatus && !MST_STM_STATIC_PASS_SHORT_ISOLATION(pStream)) {
158,115✔
4680
    mstsDebug("stream not pass static isolation time, updateTime:%" PRId64 ", currentTs %" PRId64 ", ignore check it", 
29,431✔
4681
        pStream->updateTime, mStreamMgmt.hCtx.currentTs);
4682
    return true;
29,431✔
4683
  }  
4684

4685
  if (pStatus) {
128,684✔
4686
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
127,124✔
4687
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
360✔
4688
    }
4689

4690
    return true;
127,124✔
4691
  }
4692

4693
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
1,560✔
4694

4695
  return true;
1,560✔
4696
}
4697

4698
void msmCheckLoopStreamMap(SMnode *pMnode) {
56,138✔
4699
  SStmStatus* pStatus = NULL;
56,138✔
4700
  void* pIter = NULL;
56,138✔
4701
  int8_t stopped = 0;
56,138✔
4702
  int64_t streamId = 0;
56,138✔
4703
  
4704
  while (true) {
4705
    pIter = taosHashIterate(mStreamMgmt.streamMap, pIter);
530,597✔
4706
    if (NULL == pIter) {
530,597✔
4707
      break;
56,138✔
4708
    }
4709

4710
    pStatus = (SStmStatus*)pIter;
474,459✔
4711

4712
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
474,459✔
4713
    stopped = atomic_load_8(&pStatus->stopped);
474,459✔
4714
    if (MST_IS_USER_STOPPED(stopped)) {
474,459✔
4715
      mstsDebug("stream already stopped by user, deployTimes:%" PRId64, pStatus->deployTimes);
2,847✔
4716
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
2,847✔
4717
      continue;
2,847✔
4718
    }
4719

4720
    if (!sdbCheckExists(pMnode->pSdb, SDB_STREAM, pStatus->streamName)) {
471,612✔
UNCOV
4721
      mstsDebug("stream already not exists, deployTimes:%" PRId64, pStatus->deployTimes);
×
UNCOV
4722
      (void)msmRemoveStreamFromMaps(pMnode, *(int64_t*)taosHashGetKey(pIter, NULL));
×
UNCOV
4723
      continue;
×
4724
    }
4725

4726
    if (MST_IS_ERROR_STOPPED(stopped)) {
471,612✔
4727
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
8,880✔
4728
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
5,920✔
4729
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4730
            
4731
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
5,920✔
4732
        continue;
5,920✔
4733
      }
4734

4735
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
2,960✔
4736
      continue;
2,960✔
4737
    }
4738

4739
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
462,732✔
4740
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4741
      continue;
×
4742
    }
4743
  }
4744
}
56,138✔
4745

4746
void msmCheckStreamsStatus(SMnode *pMnode) {
419,740✔
4747
  SStmCheckStatusCtx ctx = {0};
419,740✔
4748

4749
  mstDebug("start to check streams status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
419,740✔
4750
  
4751
  if (MST_READY_FOR_SDB_LOOP()) {
419,740✔
4752
    mstDebug("ready to check sdb loop, lastLoopSdbTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SDB].ts);
81,132✔
4753
    sdbTraverse(pMnode->pSdb, SDB_STREAM, msmCheckLoopStreamSdb, &ctx, NULL, NULL);
81,132✔
4754
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_SDB, mStreamMgmt.hCtx.currentTs);
81,132✔
4755
  }
4756

4757
  if (MST_READY_FOR_MAP_LOOP()) {
419,740✔
4758
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
56,138✔
4759
    msmCheckLoopStreamMap(pMnode);
56,138✔
4760
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
56,138✔
4761
  }
4762
}
419,740✔
4763

4764
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
4,516,902✔
4765
  for (int32_t i = 0; i < taskNum; ++i) {
9,254,629✔
4766
    SStmTaskStatus* pTask = *(pList + i);
4,739,975✔
4767

4768
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
4,739,975✔
4769
      continue;
92,093✔
4770
    }
4771
    
4772
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
4,660,542✔
4773
      continue;
4,656,158✔
4774
    }
4775

4776
    int64_t noUpTs = mStreamMgmt.hCtx.currentTs - pTask->lastUpTs;
4,384✔
4777
    if (STREAM_RUNNER_TASK == pTask->type || STREAM_TRIGGER_TASK == pTask->type) {
4,384✔
4778
      mstsWarn("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
2,248✔
4779
          gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4780
          
4781
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_TASK_LOST, mStreamMgmt.hCtx.currentTs);
2,248✔
4782
      break;
2,248✔
4783
    }
4784

4785
    mstsInfo("%s TASK:%" PRIx64 " status not updated for %" PRId64 "ms, will try to redeploy it", 
2,136✔
4786
        gStreamTaskTypeStr[pTask->type], pTask->id.taskId, noUpTs);
4787

4788
    int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
2,136✔
4789
    mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
2,136✔
4790

4791
    SStmTaskAction task = {0};
2,136✔
4792
    task.streamId = streamId;
2,136✔
4793
    task.id = pTask->id;
2,136✔
4794
    task.flag = pTask->flags;
2,136✔
4795
    task.type = pTask->type;
2,136✔
4796
    
4797
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
2,136✔
4798
  }
4799
}
4,516,902✔
4800

4801
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
182,733✔
4802
  void* pIter = NULL;
182,733✔
4803
  SStmVgStreamStatus* pVg = NULL;
182,733✔
4804
  int64_t streamId = 0;
182,733✔
4805
  
4806
  while (true) {
1,818,952✔
4807
    pIter = taosHashIterate(pStreams, pIter);
2,001,685✔
4808
    if (NULL == pIter) {
2,001,685✔
4809
      break;
182,733✔
4810
    }
4811

4812
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
1,818,952✔
4813
    pVg = (SStmVgStreamStatus*)pIter;
1,818,952✔
4814

4815
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
1,818,952✔
4816
    if (taskNum > 0) {
1,818,952✔
4817
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
1,074,779✔
4818
    }
4819

4820
    taskNum = taosArrayGetSize(pVg->calcReaders);
1,818,952✔
4821
    if (taskNum > 0) {
1,818,952✔
4822
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
1,127,746✔
4823
    }
4824
  }
4825
}
182,733✔
4826

UNCOV
4827
void msmHandleVgroupLost(SMnode *pMnode, int32_t vgId, SStmVgroupStatus* pVg) {
×
UNCOV
4828
  int64_t streamId = 0;
×
UNCOV
4829
  void* pIter = NULL;
×
UNCOV
4830
  SStmVgStreamStatus* pStream = NULL;
×
4831

UNCOV
4832
  if (!MST_PASS_ISOLATION(pVg->lastUpTs, 5)) {
×
UNCOV
4833
    mstDebug("vgroup %d lost and still in watch time, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
UNCOV
4834
    return;
×
4835
  }
4836

4837
  
4838
  while (true) {
UNCOV
4839
    pIter = taosHashIterate(pVg->streamTasks, pIter);
×
UNCOV
4840
    if (NULL == pIter) {
×
UNCOV
4841
      break;
×
4842
    }
4843

UNCOV
4844
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
×
4845
    
4846
    msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_VGROUP_LOST, mStreamMgmt.hCtx.currentTs);
×
4847
  }
4848

UNCOV
4849
  taosHashClear(pVg->streamTasks);
×
4850
}
4851

4852

4853
void msmCheckVgroupStatus(SMnode *pMnode) {
419,740✔
4854
  void* pIter = NULL;
419,740✔
4855
  int32_t code = 0;
419,740✔
4856
  
4857
  while (true) {
1,714,346✔
4858
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
2,134,086✔
4859
    if (NULL == pIter) {
2,134,086✔
4860
      break;
419,740✔
4861
    }
4862

4863
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,714,346✔
4864
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,714,346✔
4865
      continue;
1,528,919✔
4866
    }
4867
    
4868
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
185,427✔
4869

4870
    if (MST_PASS_ISOLATION(pVg->lastUpTs, 1)) {
185,427✔
4871
      SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId);
2,694✔
4872
      if (NULL == pVgroup) {
2,694✔
4873
        mstDebug("vgroup %d no longer exits, will remove all %d tasks in it", vgId, (int32_t)taosHashGetSize(pVg->streamTasks));
2,694✔
4874
        code = taosHashRemove(mStreamMgmt.vgroupMap, &vgId, sizeof(vgId));
2,694✔
4875
        if (code) {
2,694✔
UNCOV
4876
          mstWarn("remove vgroup %d from vgroupMap failed since %s", vgId, tstrerror(code));
×
4877
        }
4878
        continue;
2,694✔
4879
      }
UNCOV
4880
      mndReleaseVgroup(pMnode, pVgroup);
×
4881
      
UNCOV
4882
      mstWarn("vgroup %d lost, lastUpTs:%" PRId64 ", streamNum:%d", vgId, pVg->lastUpTs, (int32_t)taosHashGetSize(pVg->streamTasks));
×
4883
      
UNCOV
4884
      msmHandleVgroupLost(pMnode, vgId, pVg);
×
UNCOV
4885
      continue;
×
4886
    }
4887

4888
    mstDebug("vgroup %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, vgId, mStreamMgmt.hCtx.currentTs, pVg->lastUpTs);
182,733✔
4889

4890
    msmCheckVgroupStreamStatus(pVg->streamTasks);
182,733✔
4891
  }
4892
}
419,740✔
4893

4894
void msmHandleRunnerRedeploy(int64_t streamId, SStmSnodeStreamStatus* pStream, int32_t* deployNum, int32_t* deployId) {
2,045✔
4895
  *deployNum = 0;
2,045✔
4896
  
4897
  for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
5,726✔
4898
    if (pStream->runners[i]) {
5,317✔
4899
      int32_t taskNum = taosArrayGetSize(pStream->runners[i]);
2,045✔
4900
      for (int32_t t = 0; t < taskNum; ++t) {
2,454✔
4901
        SStmTaskStatus* pTask = taosArrayGetP(pStream->runners[i], t);
2,045✔
4902
        int8_t stopped = atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped);
2,045✔
4903
        if (stopped) {
2,045✔
4904
          mstsDebug("stream already stopped %d, ignore it", stopped);
1,636✔
4905
          *deployNum = 0;
1,636✔
4906
          return;
1,636✔
4907
        }
4908

4909
        int64_t newSid = atomic_add_fetch_64(&pTask->id.seriousId, 1);
409✔
4910
        mstsDebug("task %" PRIx64 " SID updated to %" PRIx64, pTask->id.taskId, newSid);
409✔
4911
      }
4912
      
4913
      deployId[*deployNum] = i;
409✔
4914
      (*deployNum)++;
409✔
4915
    }
4916
  }
4917
}
4918

4919
void msmHandleSnodeLost(SMnode *pMnode, SStmSnodeStatus* pSnode) {
4,643✔
4920
  pSnode->runnerThreadNum = -1;
4,643✔
4921

4922
  (void)msmSTAddSnodesToMap(pMnode);
4,643✔
4923

4924
  int64_t streamId = 0;
4,643✔
4925
  void* pIter = NULL;
4,643✔
4926
  SStmSnodeStreamStatus* pStream = NULL;
4,643✔
4927
  int32_t deployNum = 0;
4,643✔
4928
  SStmTaskAction task = {0};
4,643✔
4929
  
4930
  while (true) {
4931
    pIter = taosHashIterate(pSnode->streamTasks, pIter);
7,809✔
4932
    if (NULL == pIter) {
7,809✔
4933
      break;
4,643✔
4934
    }
4935

4936
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
3,166✔
4937
    
4938
    task.streamId = streamId;
3,166✔
4939
    
4940
    pStream = (SStmSnodeStreamStatus*)pIter;
3,166✔
4941
    if (pStream->trigger) {
3,166✔
4942
      int8_t stopped = atomic_load_8(&((SStmStatus*)pStream->trigger->pStream)->stopped);
1,121✔
4943
      if (stopped) {
1,121✔
4944
        mstsDebug("stream already stopped %d, ignore it", stopped);
409✔
4945
        continue;
409✔
4946
      }
4947

4948
      mstsInfo("snode lost with trigger task %" PRIx64 ", will try to restart current stream", pStream->trigger->id.taskId);
712✔
4949
      
4950
      msmStopStreamByError(streamId, NULL, TSDB_CODE_MND_STREAM_SNODE_LOST, mStreamMgmt.hCtx.currentTs);
712✔
4951
    } else {
4952
      msmHandleRunnerRedeploy(streamId, pStream, &task.deployNum, task.deployId);
2,045✔
4953
      
4954
      if (task.deployNum > 0) {
2,045✔
4955
        //task.triggerStatus = pStream->trigger;
4956
        task.multiRunner = true;
409✔
4957
        task.type = STREAM_RUNNER_TASK;
409✔
4958
        
4959
        mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
409✔
4960
        
4961
        mstsInfo("runner tasks %d redeploys added to actionQ", task.deployNum);
409✔
4962
      }
4963
    }
4964
  }
4965

4966
  taosHashClear(pSnode->streamTasks);
4,643✔
4967
}
4,643✔
4968

4969

4970
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
57,691✔
4971
  void* pIter = NULL;
57,691✔
4972
  SStmSnodeStreamStatus* pSnode = NULL;
57,691✔
4973
  int64_t streamId = 0;
57,691✔
4974
  
4975
  while (true) {
4976
    pIter = taosHashIterate(pStreams, pIter);
676,516✔
4977
    if (NULL == pIter) {
676,516✔
4978
      break;
57,691✔
4979
    }
4980

4981
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
618,825✔
4982
    pSnode = (SStmSnodeStreamStatus*)pIter;
618,825✔
4983

4984
    if (NULL != pSnode->trigger) {
618,825✔
4985
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
595,928✔
4986
    }
4987

4988
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
2,475,300✔
4989
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
1,856,475✔
4990
      if (taskNum > 0) {
1,856,475✔
4991
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
1,718,449✔
4992
      }
4993
    }
4994
  }
4995
}
57,691✔
4996

4997

4998
void msmCheckSnodeStatus(SMnode *pMnode) {
419,740✔
4999
  void* pIter = NULL;
419,740✔
5000
  
5001
  while (true) {
566,688✔
5002
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
986,428✔
5003
    if (NULL == pIter) {
986,428✔
5004
      break;
419,740✔
5005
    }
5006

5007
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
566,688✔
5008
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
566,688✔
5009
      continue;
498,978✔
5010
    }
5011

5012
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
67,710✔
5013
    
5014
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
67,710✔
5015
    if (NULL == pSnode->streamTasks) {
67,710✔
5016
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
6,550✔
5017
      continue;
6,550✔
5018
    }
5019
    
5020
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
61,160✔
5021
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
3,469✔
5022
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5023
      
5024
      msmHandleSnodeLost(pMnode, pSnode);
3,469✔
5025
      continue;
3,469✔
5026
    }
5027
    
5028
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
57,691✔
5029

5030
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
57,691✔
5031
  }
5032
}
419,740✔
5033

5034

5035
void msmCheckTasksStatus(SMnode *pMnode) {
419,740✔
5036
  mstDebug("start to check tasks status, currTs:%" PRId64, mStreamMgmt.hCtx.currentTs);
419,740✔
5037

5038
  msmCheckVgroupStatus(pMnode);
419,740✔
5039
  msmCheckSnodeStatus(pMnode);
419,740✔
5040
}
419,740✔
5041

5042
void msmCheckSnodesState(SMnode *pMnode) {
419,740✔
5043
  if (!MST_READY_FOR_SNODE_LOOP()) {
419,740✔
5044
    return;
371,823✔
5045
  }
5046

5047
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
47,917✔
5048

5049
  void* pIter = NULL;
47,917✔
5050
  int32_t snodeId = 0;
47,917✔
5051
  while (true) {
55,211✔
5052
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
103,128✔
5053
    if (NULL == pIter) {
103,128✔
5054
      break;
47,917✔
5055
    }
5056

5057
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
55,211✔
5058
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
55,211✔
5059
      continue;
54,037✔
5060
    }
5061

5062
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
1,174✔
5063
    if (NULL == pSnode->streamTasks) {
1,174✔
UNCOV
5064
      mstDebug("snode %d already cleanup, try to rm it", snodeId);
×
UNCOV
5065
      TAOS_UNUSED(taosHashRemove(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId)));
×
UNCOV
5066
      continue;
×
5067
    }
5068
    
5069
    mstWarn("snode %d lost while streams remain, will redeploy all and rm it, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,174✔
5070
        snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5071
    
5072
    msmHandleSnodeLost(pMnode, pSnode);
1,174✔
5073
  }
5074

5075
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
47,917✔
5076
}
5077

5078
bool msmCheckNeedHealthCheck(SMnode *pMnode) {
7,817,720✔
5079
  int8_t active = atomic_load_8(&mStreamMgmt.active), state = atomic_load_8(&mStreamMgmt.state);
7,817,720✔
5080
  if (0 == active || MND_STM_STATE_NORMAL != state) {
7,817,720✔
5081
    mstTrace("ignore health check since active:%d state:%d", active, state);
780✔
5082
    return false;
780✔
5083
  }
5084

5085
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,816,940✔
5086
    mstTrace("ignore health check since no stream now");
6,977,460✔
5087
    return false;
6,977,460✔
5088
  }
5089

5090
  return true;
839,480✔
5091
}
5092

5093
void msmHealthCheck(SMnode *pMnode) {
7,397,980✔
5094
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,397,980✔
5095
    return;
6,978,240✔
5096
  }
5097

5098
  mstDebug("start wait health check, currentTs:%" PRId64,  taosGetTimestampMs());
791,878✔
5099
  
5100
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, false);
419,740✔
5101
  if (!msmCheckNeedHealthCheck(pMnode)) {
419,740✔
UNCOV
5102
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
×
UNCOV
5103
    return;
×
5104
  }
5105
  
5106
  mStreamMgmt.hCtx.slotIdx = (mStreamMgmt.hCtx.slotIdx + 1) % MND_STREAM_ISOLATION_PERIOD_NUM;
419,740✔
5107
  mStreamMgmt.hCtx.currentTs = taosGetTimestampMs();
419,740✔
5108

5109
  mstDebug("start health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
419,740✔
5110
  
5111
  msmCheckStreamsStatus(pMnode);
419,740✔
5112
  msmCheckTasksStatus(pMnode);
419,740✔
5113
  msmCheckSnodesState(pMnode);
419,740✔
5114

5115
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
419,740✔
5116

5117
  mstDebug("end health check, soltIdx:%d, checkStartTs:%" PRId64, mStreamMgmt.hCtx.slotIdx, mStreamMgmt.hCtx.currentTs);
419,740✔
5118
}
5119

5120
static bool msmUpdateProfileStreams(SMnode *pMnode, void *pObj, void *p1, void *p2, void *p3) {
1,560✔
5121
  SStreamObj *pStream = pObj;
1,560✔
5122
  if (atomic_load_8(&pStream->userDropped) || atomic_load_8(&pStream->userStopped)) {
1,560✔
UNCOV
5123
    return true;
×
5124
  }
5125
  
5126
  pStream->updateTime = *(int64_t*)p1;
1,560✔
5127
  
5128
  (*(int32_t*)p2)++;
1,560✔
5129
  
5130
  return true;
1,560✔
5131
}
5132

UNCOV
5133
int32_t msmGetTriggerTaskAddr(SMnode *pMnode, int64_t streamId, SStreamTaskAddr* pAddr) {
×
UNCOV
5134
  int32_t code = 0;
×
UNCOV
5135
  int8_t  stopped = 0;
×
5136
  
UNCOV
5137
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
×
5138
  
UNCOV
5139
  SStmStatus* pStatus = (SStmStatus*)taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
×
UNCOV
5140
  if (NULL == pStatus) {
×
5141
    mstsError("stream not exists in streamMap, streamRemains:%d", taosHashGetSize(mStreamMgmt.streamMap));
×
UNCOV
5142
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
UNCOV
5143
    goto _exit;
×
5144
  }
5145

UNCOV
5146
  stopped = atomic_load_8(&pStatus->stopped);
×
UNCOV
5147
  if (stopped) {
×
UNCOV
5148
    mstsError("stream already stopped, stopped:%d", stopped);
×
UNCOV
5149
    code = TSDB_CODE_MND_STREAM_NOT_RUNNING;
×
UNCOV
5150
    goto _exit;
×
5151
  }
5152

5153
  if (pStatus->triggerTask && STREAM_STATUS_RUNNING == pStatus->triggerTask->status) {
×
UNCOV
5154
    pAddr->taskId = pStatus->triggerTask->id.taskId;
×
5155
    pAddr->nodeId = pStatus->triggerTask->id.nodeId;
×
UNCOV
5156
    pAddr->epset = mndGetDnodeEpsetById(pMnode, pAddr->nodeId);
×
5157
    mstsDebug("stream trigger task %" PRIx64 " got with nodeId %d", pAddr->taskId, pAddr->nodeId);
×
5158
    goto _exit;
×
5159
  }
5160

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

5164
_exit:
×
5165
  
5166
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
×
5167

5168
  return code;
×
5169
}
5170

5171
int32_t msmInitRuntimeInfo(SMnode *pMnode) {
414,729✔
5172
  int32_t code = TSDB_CODE_SUCCESS;
414,729✔
5173
  int32_t lino = 0;
414,729✔
5174
  int32_t vnodeNum = sdbGetSize(pMnode->pSdb, SDB_VGROUP);
414,729✔
5175
  int32_t snodeNum = sdbGetSize(pMnode->pSdb, SDB_SNODE);
414,729✔
5176
  int32_t dnodeNum = sdbGetSize(pMnode->pSdb, SDB_DNODE);
414,729✔
5177

5178
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
828,349✔
5179

5180
  mStreamMgmt.stat.activeTimes++;
414,729✔
5181
  mStreamMgmt.threadNum = tsNumOfMnodeStreamMgmtThreads;
414,729✔
5182
  mStreamMgmt.tCtx = taosMemoryCalloc(mStreamMgmt.threadNum, sizeof(SStmThreadCtx));
414,729✔
5183
  if (NULL == mStreamMgmt.tCtx) {
414,729✔
5184
    code = terrno;
×
UNCOV
5185
    mstError("failed to initialize the stream runtime tCtx, threadNum:%d, error:%s", mStreamMgmt.threadNum, tstrerror(code));
×
5186
    goto _exit;
×
5187
  }
5188

5189
  mStreamMgmt.actionQ = taosMemoryCalloc(1, sizeof(SStmActionQ));
414,729✔
5190
  if (mStreamMgmt.actionQ == NULL) {
414,729✔
UNCOV
5191
    code = terrno;
×
UNCOV
5192
    mError("failed to initialize the stream runtime actionQ, error:%s", tstrerror(code));
×
UNCOV
5193
    goto _exit;
×
5194
  }
5195
  
5196
  mStreamMgmt.actionQ->head = taosMemoryCalloc(1, sizeof(SStmQNode));
414,729✔
5197
  TSDB_CHECK_NULL(mStreamMgmt.actionQ->head, code, lino, _exit, terrno);
414,729✔
5198
  
5199
  mStreamMgmt.actionQ->tail = mStreamMgmt.actionQ->head;
414,729✔
5200
  
5201
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,488,014✔
5202
    SStmThreadCtx* pCtx = mStreamMgmt.tCtx + i;
2,073,285✔
5203

5204
    for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,439,710✔
5205
      pCtx->deployStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,366,425✔
5206
      if (pCtx->deployStm[m] == NULL) {
10,366,425✔
UNCOV
5207
        code = terrno;
×
UNCOV
5208
        mError("failed to initialize the stream runtime deployStm[%d][%d], error:%s", i, m, tstrerror(code));
×
5209
        goto _exit;
×
5210
      }
5211
      taosHashSetFreeFp(pCtx->deployStm[m], tDeepFreeSStmStreamDeploy);
10,366,425✔
5212
      
5213
      pCtx->actionStm[m] = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK);
10,366,425✔
5214
      if (pCtx->actionStm[m] == NULL) {
10,366,425✔
UNCOV
5215
        code = terrno;
×
UNCOV
5216
        mError("failed to initialize the stream runtime actionStm[%d][%d], error:%s", i, m, tstrerror(code));
×
UNCOV
5217
        goto _exit;
×
5218
      }
5219
      taosHashSetFreeFp(pCtx->actionStm[m], mstDestroySStmAction);
10,366,425✔
5220
    }
5221
  }
5222
  
5223
  mStreamMgmt.streamMap = taosHashInit(MND_STREAM_DEFAULT_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
414,729✔
5224
  if (mStreamMgmt.streamMap == NULL) {
414,729✔
5225
    code = terrno;
×
5226
    mError("failed to initialize the stream runtime streamMap, error:%s", tstrerror(code));
×
5227
    goto _exit;
×
5228
  }
5229
  taosHashSetFreeFp(mStreamMgmt.streamMap, mstDestroySStmStatus);
414,729✔
5230
  
5231
  mStreamMgmt.taskMap = taosHashInit(MND_STREAM_DEFAULT_TASK_NUM, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_ENTRY_LOCK);
414,729✔
5232
  if (mStreamMgmt.taskMap == NULL) {
414,729✔
5233
    code = terrno;
×
5234
    mError("failed to initialize the stream runtime taskMap, error:%s", tstrerror(code));
×
5235
    goto _exit;
×
5236
  }
5237
  
5238
  mStreamMgmt.vgroupMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
414,729✔
5239
  if (mStreamMgmt.vgroupMap == NULL) {
414,729✔
UNCOV
5240
    code = terrno;
×
UNCOV
5241
    mError("failed to initialize the stream runtime vgroupMap, error:%s", tstrerror(code));
×
UNCOV
5242
    goto _exit;
×
5243
  }
5244
  taosHashSetFreeFp(mStreamMgmt.vgroupMap, mstDestroySStmVgroupStatus);
414,729✔
5245

5246
  mStreamMgmt.snodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
414,729✔
5247
  if (mStreamMgmt.snodeMap == NULL) {
414,729✔
UNCOV
5248
    code = terrno;
×
UNCOV
5249
    mError("failed to initialize the stream runtime snodeMap, error:%s", tstrerror(code));
×
UNCOV
5250
    goto _exit;
×
5251
  }
5252
  taosHashSetFreeFp(mStreamMgmt.snodeMap, mstDestroySStmSnodeStatus);
414,729✔
5253
  
5254
  mStreamMgmt.dnodeMap = taosHashInit(dnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
414,729✔
5255
  if (mStreamMgmt.dnodeMap == NULL) {
414,729✔
UNCOV
5256
    code = terrno;
×
UNCOV
5257
    mError("failed to initialize the stream runtime dnodeMap, error:%s", tstrerror(code));
×
5258
    goto _exit;
×
5259
  }
5260

5261
  mStreamMgmt.toDeployVgMap = taosHashInit(vnodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
414,729✔
5262
  if (mStreamMgmt.toDeployVgMap == NULL) {
414,729✔
UNCOV
5263
    code = terrno;
×
UNCOV
5264
    mError("failed to initialize the stream runtime toDeployVgMap, error:%s", tstrerror(code));
×
UNCOV
5265
    goto _exit;
×
5266
  }
5267
  taosHashSetFreeFp(mStreamMgmt.toDeployVgMap, mstDestroySStmVgTasksToDeploy);
414,729✔
5268
  
5269
  mStreamMgmt.toDeploySnodeMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_ENTRY_LOCK);
414,729✔
5270
  if (mStreamMgmt.toDeploySnodeMap == NULL) {
414,729✔
UNCOV
5271
    code = terrno;
×
UNCOV
5272
    mError("failed to initialize the stream runtime toDeploySnodeMap, error:%s", tstrerror(code));
×
UNCOV
5273
    goto _exit;
×
5274
  }
5275
  taosHashSetFreeFp(mStreamMgmt.toDeploySnodeMap, mstDestroySStmSnodeTasksDeploy);
414,729✔
5276

5277
  mStreamMgmt.toUpdateScanMap = taosHashInit(snodeNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
414,729✔
5278
  if (mStreamMgmt.toUpdateScanMap == NULL) {
414,729✔
UNCOV
5279
    code = terrno;
×
UNCOV
5280
    mError("failed to initialize the stream runtime toUpdateScanMap, error:%s", tstrerror(code));
×
5281
    goto _exit;
×
5282
  }
5283
  taosHashSetFreeFp(mStreamMgmt.toUpdateScanMap, mstDestroyScanAddrList);
414,729✔
5284

5285
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
414,729✔
5286
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
414,729✔
5287

5288
  mStreamMgmt.lastTaskId = 1;
414,729✔
5289

5290
  int32_t activeStreamNum = 0;
414,729✔
5291
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
414,729✔
5292

5293
  if (activeStreamNum > 0) {
414,729✔
5294
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
260✔
5295
  } else {
5296
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
414,469✔
5297
  }
5298

5299
_exit:
414,729✔
5300

5301
  if (code) {
414,729✔
UNCOV
5302
    msmDestroyRuntimeInfo(pMnode);
×
UNCOV
5303
    mstError("%s failed at line %d since %s", __FUNCTION__, lino, tstrerror(code));
×
5304
  } else {
5305
    mstInfo("mnode stream runtime init done");
414,729✔
5306
  }
5307

5308
  return code;
414,729✔
5309
}
5310

5311

5312

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