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

taosdata / TDengine / #4861

22 Nov 2025 07:23AM UTC coverage: 64.274% (-0.06%) from 64.335%
#4861

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

0 of 79 new or added lines in 2 files covered. (0.0%)

820 existing lines in 129 files now uncovered.

154466 of 240326 relevant lines covered (64.27%)

112821527.61 hits per line

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

72.54
/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() {
813,946✔
34
  SStmQNode* pQNode = NULL;
813,946✔
35

36
  if (NULL == mStreamMgmt.actionQ) {
813,946✔
37
    return;
406,973✔
38
  }
39

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

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

47
void msmDestroySStmThreadCtx(SStmThreadCtx* pCtx) {
2,034,505✔
48
  for (int32_t m = 0; m < STREAM_MAX_GROUP_NUM; ++m) {
12,207,030✔
49
    taosHashCleanup(pCtx->deployStm[m]);
10,172,525✔
50
    taosHashCleanup(pCtx->actionStm[m]);
10,172,525✔
51
  }
52
}
2,034,505✔
53

54
void msmDestroyThreadCtxs() {
813,946✔
55
  if (NULL == mStreamMgmt.tCtx) {
813,946✔
56
    return;
406,973✔
57
  }
58
  
59
  for (int32_t i = 0; i < mStreamMgmt.threadNum; ++i) {
2,441,478✔
60
    msmDestroySStmThreadCtx(mStreamMgmt.tCtx + i);
2,034,505✔
61
  }
62
  taosMemoryFreeClear(mStreamMgmt.tCtx);
406,973✔
63
}
64

65

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

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

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

91
  memset(mStreamMgmt.lastTs, 0, sizeof(mStreamMgmt.lastTs));
813,946✔
92

93
  mstInfo("mnode stream mgmt destroyed");  
813,946✔
94
}
813,946✔
95

96

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

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

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

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

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

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

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

136
_exit:
391✔
137

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

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

145

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

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

200
  pSnode = NULL;
435,700✔
201

202
_exit:
435,700✔
203

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

208
  return code;
435,700✔
209
}
210

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

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

235
  pDnode = NULL;
946,560✔
236

237
_exit:
946,560✔
238

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

243
  return code;
946,560✔
244
}
245

246

247

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

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

274
static int32_t msmSTAddToVgStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, bool trigReader) {
320,239✔
275
  int32_t code = TSDB_CODE_SUCCESS;
320,239✔
276
  int32_t lino = 0;
320,239✔
277
  SStmVgStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
320,239✔
278
  if (NULL == pStream) {
320,239✔
279
    SStmVgStreamStatus stream = {0};
247,294✔
280
    if (trigReader) {
247,294✔
281
      stream.trigReaders = taosArrayInit(1, POINTER_BYTES);
194,603✔
282
      TSDB_CHECK_NULL(stream.trigReaders, code, lino, _exit, terrno);
194,603✔
283
      TSDB_CHECK_NULL(taosArrayPush(stream.trigReaders, &pStatus), code, lino, _exit, terrno);
389,206✔
284
    } else {
285
      stream.calcReaders = taosArrayInit(2, POINTER_BYTES);
52,691✔
286
      TSDB_CHECK_NULL(stream.calcReaders, code, lino, _exit, terrno);
52,691✔
287
      TSDB_CHECK_NULL(taosArrayPush(stream.calcReaders, &pStatus), code, lino, _exit, terrno);
105,382✔
288
    }
289
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
247,294✔
290
    goto _exit;
247,294✔
291
  }
292
  
293
  if (trigReader) {
72,945✔
294
    if (NULL == pStream->trigReaders) {
1,424✔
295
      pStream->trigReaders = taosArrayInit(1, POINTER_BYTES);
1,424✔
296
      TSDB_CHECK_NULL(pStream->trigReaders, code, lino, _exit, terrno);
1,424✔
297
    }
298
    
299
    TSDB_CHECK_NULL(taosArrayPush(pStream->trigReaders, &pStatus), code, lino, _exit, terrno);
2,848✔
300
    goto _exit;
1,424✔
301
  }
302
  
303
  if (NULL == pStream->calcReaders) {
71,521✔
304
    pStream->calcReaders = taosArrayInit(1, POINTER_BYTES);
61,535✔
305
    TSDB_CHECK_NULL(pStream->calcReaders, code, lino, _exit, terrno);
61,535✔
306
  }
307

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

310
_exit:
71,521✔
311

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

320
  return code;
320,239✔
321
}
322

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

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

334
    vg.lastUpTs = taosGetTimestampMs();
86,168✔
335
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(vg.streamTasks, streamId, pStatus, trigReader));
86,168✔
336
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.vgroupMap, &pStatus->id.nodeId, sizeof(pStatus->id.nodeId), &vg, sizeof(vg)));
86,168✔
337
  } else {
338
    TAOS_CHECK_EXIT(msmSTAddToVgStreamHash(pVg->streamTasks, streamId, pStatus, trigReader));
234,071✔
339
  }
340
  
341
_exit:
319,715✔
342

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

350
  return code;
320,239✔
351
}
352

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

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

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

404
  return code;
322,351✔
405
}
406

407

408
static int32_t msmSTAddToSnodeStreamHash(SHashObj* pHash, int64_t streamId, SStmTaskStatus* pStatus, int32_t deployId) {
616,293✔
409
  int32_t code = TSDB_CODE_SUCCESS;
616,293✔
410
  int32_t lino = 0;
616,293✔
411
  SStmSnodeStreamStatus* pStream = taosHashGet(pHash, &streamId, sizeof(streamId));
616,293✔
412
  if (NULL == pStream) {
616,293✔
413
    SStmSnodeStreamStatus stream = {0};
156,306✔
414
    if (deployId < 0) {
156,306✔
415
      stream.trigger = pStatus;
379✔
416
    } else {
417
      stream.runners[deployId] = taosArrayInit(2, POINTER_BYTES);
155,927✔
418
      TSDB_CHECK_NULL(stream.runners[deployId], code, lino, _exit, terrno);
155,927✔
419
      TSDB_CHECK_NULL(taosArrayPush(stream.runners[deployId], &pStatus), code, lino, _exit, terrno);
311,854✔
420
    }
421
    
422
    TAOS_CHECK_EXIT(taosHashPut(pHash, &streamId, sizeof(streamId), &stream, sizeof(stream)));
156,306✔
423
    goto _exit;
156,306✔
424
  }
425
  
426
  if (deployId < 0) {
459,987✔
427
    if (NULL != pStream->trigger) {
145,730✔
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;
145,730✔
433
    goto _exit;
145,730✔
434
  }
435
  
436
  if (NULL == pStream->runners[deployId]) {
314,257✔
437
    pStream->runners[deployId] = taosArrayInit(2, POINTER_BYTES);
281,263✔
438
    TSDB_CHECK_NULL(pStream->runners[deployId], code, lino, _exit, terrno);
281,263✔
439
  }
440

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

443
_exit:
314,257✔
444

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

453
  return code;
616,293✔
454
}
455

456

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

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

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

484
  return code;
616,293✔
485
}
486

487

488

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

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

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

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

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

550
  return code;
146,109✔
551
}
552

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

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

567
      ext.deploy = *pDeploy;
42,272✔
568
      ext.deployed = false;
42,272✔
569
      TSDB_CHECK_NULL(taosArrayPush(snode.runnerList, &ext), code, lino, _return, terrno);
84,544✔
570

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

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

614
  return code;
470,184✔
615
}
616

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

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

632
_exit:
437,190✔
633

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

638
  return code;
437,190✔
639
}
640

641

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

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

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

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

672
        continue;
×
673
      }
674

675
      return code;
17,669✔
676
    }
677

678
    break;
679
  }
680

UNCOV
681
_exit:
×
682

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

UNCOV
687
  return code;  
×
688
}
689

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

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

707
      continue;
×
708
    }
709

710
    return;
84,568✔
711
  }  
712
}
713

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

719
  mstDebug("start to update vgroups upTs");
14,544,470✔
720
  
721
  for (int32_t i = 0; i < vgNum; ++i) {
53,633,352✔
722
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
39,088,882✔
723

724
    msmUpdateVgroupUpTs(pCtx, *vgId);
39,088,882✔
725
  }
726

727
_exit:
14,544,470✔
728

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

733
  return code;
14,544,470✔
734
}
735

736

737

738
void* msmSearchCalcCacheScanPlan(SArray* pList) {
222,944✔
739
  int32_t num = taosArrayGetSize(pList);
222,944✔
740
  for (int32_t i = 0; i < num; ++i) {
398,968✔
741
    SStreamCalcScan* pScan = taosArrayGet(pList, i);
301,913✔
742
    if (pScan->readFromCache) {
301,913✔
743
      return pScan->scanPlan;
125,889✔
744
    }
745
  }
746

747
  return NULL;
97,055✔
748
}
749

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

773
  return TSDB_CODE_SUCCESS;
322,351✔
774
}
775

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

780
  if (pInfo->runnerDeploys > 0) {
145,730✔
781
    *ppRes = taosArrayInit(pInfo->runnerDeploys, sizeof(SStreamRunnerTarget));
145,730✔
782
    TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
145,730✔
783
  }
784
  
785
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
582,920✔
786
    SStmTaskStatus* pStatus = taosArrayGetLast(pInfo->runners[i]);
437,190✔
787
    TSDB_CHECK_NULL(pStatus, code, lino, _exit, terrno);
437,190✔
788

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

804
_exit:
145,730✔
805

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

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

825
  mndReleaseSnode(pMnode, pSnode);
×
826

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

832
  return TSDB_CODE_SUCCESS;
×
833
}
834

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

853
  pMsg->pNotifyAddrUrls = pInfo->pCreate->pNotifyAddrUrls;
146,109✔
854
  pMsg->notifyEventTypes = pStream->pCreate->notifyEventTypes;
146,109✔
855
  pMsg->addOptions = pStream->pCreate->addOptions;
146,109✔
856
  pMsg->notifyHistory = pStream->pCreate->notifyHistory;
146,109✔
857

858
  pMsg->maxDelay = pStream->pCreate->maxDelay;
146,109✔
859
  pMsg->fillHistoryStartTime = pStream->pCreate->fillHistoryStartTime;
146,109✔
860
  pMsg->watermark = pStream->pCreate->watermark;
146,109✔
861
  pMsg->expiredTime = pStream->pCreate->expiredTime;
146,109✔
862
  pMsg->trigger = pInfo->pCreate->trigger;
146,109✔
863

864
  pMsg->eventTypes = pStream->pCreate->eventTypes;
146,109✔
865
  pMsg->placeHolderBitmap = pStream->pCreate->placeHolderBitmap;
146,109✔
866
  pMsg->calcTsSlotId = pStream->pCreate->calcTsSlotId;
146,109✔
867
  pMsg->triTsSlotId = pStream->pCreate->triTsSlotId;
146,109✔
868
  pMsg->triggerPrevFilter = pInfo->pCreate->triggerPrevFilter;
146,109✔
869
  if (STREAM_IS_VIRTUAL_TABLE(pStream->pCreate->triggerTblType, pStream->pCreate->flags)) {
146,109✔
870
    pMsg->triggerScanPlan = pInfo->pCreate->triggerScanPlan;
25,861✔
871
    pMsg->calcCacheScanPlan = msmSearchCalcCacheScanPlan(pInfo->pCreate->calcScanPlanList);
25,861✔
872
  }
873

874
  SStreamTaskAddr addr;
141,957✔
875
  int32_t triggerReaderNum = taosArrayGetSize(pInfo->trigReaders);
146,109✔
876
  if (triggerReaderNum > 0) {
146,109✔
877
    pMsg->readerList = taosArrayInit(triggerReaderNum, sizeof(SStreamTaskAddr));
145,714✔
878
    TSDB_CHECK_NULL(pMsg->readerList, code, lino, _exit, terrno);
145,714✔
879
  }
880
  
881
  for (int32_t i = 0; i < triggerReaderNum; ++i) {
331,872✔
882
    SStmTaskStatus* pStatus = taosArrayGet(pInfo->trigReaders, i);
185,763✔
883
    addr.taskId = pStatus->id.taskId;
185,763✔
884
    addr.nodeId = pStatus->id.nodeId;
185,763✔
885
    addr.epset = mndGetVgroupEpsetById(pMnode, pStatus->id.nodeId);
185,763✔
886
    TSDB_CHECK_NULL(taosArrayPush(pMsg->readerList, &addr), code, lino, _exit, terrno);
371,526✔
887
    mstsDebug("the %dth trigReader src added to trigger's readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
185,763✔
888
  }
889

890
  pMsg->leaderSnodeId = pStream->mainSnodeId;
146,109✔
891
  pMsg->streamName = pInfo->streamName;
146,109✔
892

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

898
  TAOS_CHECK_EXIT(msmBuildTriggerRunnerTargets(pMnode, pInfo, streamId, &pMsg->runnerList));
145,730✔
899

900
_exit:
145,730✔
901

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

911

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

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

943
_exit:
470,184✔
944

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

952

953
static int32_t msmSTAddToVgroupMap(SStmGrpCtx* pCtx, int64_t streamId, SArray* pTasks, SList* pList, SStmTaskStatus* pTask, bool trigReader) {
306,750✔
954
  int32_t code = TSDB_CODE_SUCCESS;
306,750✔
955
  int32_t lino = 0;
306,750✔
956
  int32_t taskNum = pTask ? 1 : (pList ? MST_LIST_SIZE(pList) :taosArrayGetSize(pTasks));
306,750✔
957
  SListNode* pNode = pList ? listHead(pList) : NULL;
306,750✔
958
  
959
  for (int32_t i = 0; i < taskNum; ++i) {
626,989✔
960
    SStmTaskStatus* pStatus = pTask ? pTask : (pNode ? (SStmTaskStatus*)pNode->data : taosArrayGet(pTasks, i));
320,239✔
961
    TAOS_CHECK_EXIT(msmSTAddToVgroupMapImpl(streamId, pStatus, trigReader));
320,239✔
962
    if (pNode) {
320,239✔
963
      pNode = TD_DLIST_NODE_NEXT(pNode);
119,944✔
964
    }
965
  }
966
  
967
_exit:
306,750✔
968

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

976

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

990
  if (code) {
583,299✔
991
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
992
  }
993

994
  return code;
583,299✔
995
}
996

997
int64_t msmAssignTaskId(void) {
936,532✔
998
  return atomic_fetch_add_64(&mStreamMgmt.lastTaskId, 1);
936,532✔
999
}
1000

1001
int64_t msmAssignTaskSeriousId(void) {
936,532✔
1002
  return taosGetTimestampNs();
936,532✔
1003
}
1004

1005

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

1012
  while (true) {
1013
    pStatus = taosHashGet(mStreamMgmt.snodeMap, &snodeId, sizeof(snodeId));
793,242✔
1014
    if (NULL == pStatus) {
793,242✔
1015
      if (noExists) {
9,298✔
1016
        mstsError("snode %d not exists in snodeMap", snodeId);
×
1017
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1018
      }
1019

1020
      noExists = true;
9,298✔
1021
      TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
9,298✔
1022
      
1023
      continue;
9,298✔
1024
    }
1025

1026
    *alive = (pStatus->runnerThreadNum >= 0);
783,944✔
1027
    break;
783,944✔
1028
  }
1029

1030
_exit:
783,944✔
1031

1032
  if (code) {
783,944✔
1033
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1034
  }
1035

1036
  return code;
783,944✔
1037
}
1038

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

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

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

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

1077
_exit:
×
1078

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

1083
  return 0;
×
1084
}
1085

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

1100
  int32_t snodeTarget = taosRand() % snodeNum;
454,270✔
1101

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

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

1127
    snodeId = pObj->id;
492,105✔
1128
    if (snodeIdx == snodeTarget) {
492,105✔
1129
      sdbRelease(pMnode->pSdb, pObj);
454,270✔
1130
      sdbCancelFetch(pMnode->pSdb, pIter);
454,270✔
1131
      pObj = NULL;
454,270✔
1132
      goto _exit;
454,270✔
1133
    }
1134

1135
    sdbRelease(pMnode->pSdb, pObj);
37,835✔
1136
    snodeIdx++;
37,835✔
1137
  }
1138

1139
_exit:
458,440✔
1140

1141
  if (code) {
458,440✔
1142
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1143
  }
1144

1145
  if (0 == snodeId) {
458,440✔
1146
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
4,170✔
1147
  }
1148

1149
  return snodeId;
458,440✔
1150
}
1151

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

1161
  snodeId = isStatic ? msmRetrieveStaticSnodeId(pMnode, pStream) : msmAssignRandomSnodeId(pMnode, streamId);
583,299✔
1162

1163
_exit:
583,299✔
1164

1165
  if (0 == snodeId) {
583,299✔
1166
    terrno = TSDB_CODE_SNODE_NO_AVAILABLE_NODE;
×
1167
  }
1168

1169
  return snodeId;
583,299✔
1170
}
1171

1172

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

1178
  pInfo->triggerTask = taosMemoryCalloc(1, sizeof(SStmTaskStatus));
146,109✔
1179
  TSDB_CHECK_NULL(pInfo->triggerTask, code, lino, _exit, terrno);
146,109✔
1180

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

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

1202
  TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, NULL, NULL, pInfo->triggerTask));
146,109✔
1203
  TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, NULL, pInfo->triggerTask, 1, -1));
146,109✔
1204

1205
_exit:
146,109✔
1206

1207
  if (code) {
146,109✔
1208
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1209
  }
1210

1211
  return code;
146,109✔
1212
}
1213

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

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

1240
_exit:
196,027✔
1241

1242
  if (code) {
196,027✔
1243
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1244
  }
1245

1246
  return code;
196,027✔
1247
}
1248

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

1278
      pInfo->trigReaders = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SStmTaskStatus));
95,155✔
1279
      TSDB_CHECK_NULL(pInfo->trigReaders, code, lino, _exit, terrno);
95,155✔
1280
      
1281
      void *pIter = NULL;
95,155✔
1282
      while (1) {
533,768✔
1283
        SStmTaskDeploy info = {0};
628,923✔
1284
        pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup);
628,923✔
1285
        if (pIter == NULL) {
628,923✔
1286
          break;
95,155✔
1287
        }
1288
      
1289
        if (pVgroup->dbUid == pDb->uid && !pVgroup->isTsma) {
533,768✔
1290
          pState = taosArrayReserve(pInfo->trigReaders, 1);
135,204✔
1291

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

1301
        sdbRelease(pSdb, pVgroup);
533,768✔
1302
      }
1303
      break;
95,155✔
1304
    }
1305
    default:
395✔
1306
      mstsDebug("%s ignore triggerTblType %d", __FUNCTION__, pStream->pCreate->triggerTblType);
395✔
1307
      break;
395✔
1308
  }
1309

1310
_exit:
146,109✔
1311

1312
  mndReleaseDb(pCtx->pMnode, pDb);
146,109✔
1313

1314
  if (code) {
146,109✔
1315
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1316
  }
1317

1318
  return code;
146,109✔
1319
}
1320

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

1344
  key[1] = pSubplan->id.subplanId;
119,944✔
1345

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

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

1362
  nodesDestroyNode((SNode*)pSubplan);
119,944✔
1363

1364
  if (code) {
119,944✔
1365
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1366
  }
1367

1368
  return code;
119,944✔
1369
}
1370

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

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

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

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

1407
  return code;
67,656✔
1408
}
1409

1410

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

1415
  TAOS_CHECK_EXIT(mstGetScanUidFromPlan(streamId, calcScanPlan, &pState->id.uid));
124,212✔
1416

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

1439
_exit:
124,212✔
1440

1441
  if (code) {
124,212✔
1442
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1443
  }
1444

1445
  return code;
124,212✔
1446
}
1447

1448

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

1458
  
1459
  for (int32_t i = 0; i < calcTasksNum; ++i) {
333,709✔
1460
    SStreamCalcScan* pScan = taosArrayGet(pInfo->pCreate->calcScanPlanList, i);
187,600✔
1461
    if (pScan->readFromCache) {
187,600✔
1462
      TAOS_CHECK_EXIT(msmUPAddCacheTask(pCtx, pScan, pStream));
67,656✔
1463
      continue;
67,656✔
1464
    }
1465
    
1466
    int32_t vgNum = taosArrayGetSize(pScan->vgList);
119,944✔
1467
    for (int32_t m = 0; m < vgNum; ++m) {
239,888✔
1468
      pState = tdListReserve(pInfo->calcReaders);
119,944✔
1469
      TSDB_CHECK_NULL(pState, code, lino, _exit, terrno);
119,944✔
1470

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

1476
_exit:
146,109✔
1477

1478
  if (code) {
146,109✔
1479
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1480
  }
1481

1482
  return code;
146,109✔
1483
}
1484

1485

1486

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

UNCOV
1514
_exit:
×
1515

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

UNCOV
1520
  return code;
×
1521
}
1522

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

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

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

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

1557
  source.addr.epSet = pSrc->epset;
595,794✔
1558
  source.addr.nodeId = pSrc->vgId;
595,794✔
1559

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

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

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

1581
  return TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
1582
}
1583

1584
int32_t msmUpdateLowestPlanSourceAddr(SSubplan* pPlan, SStmTaskDeploy* pDeploy, int64_t streamId) {
470,184✔
1585
  int32_t code = TSDB_CODE_SUCCESS;
470,184✔
1586
  int32_t lino = 0;
470,184✔
1587
  int64_t key[2] = {streamId, -1};
470,184✔
1588
  SNode* pNode = NULL;
470,184✔
1589
  SStreamTask* pTask = &pDeploy->task;
470,184✔
1590
  FOREACH(pNode, pPlan->pChildren) {
1,065,978✔
1591
    if (QUERY_NODE_VALUE != nodeType(pNode)) {
595,794✔
1592
      msttDebug("node type %d is not valueNode, skip it", nodeType(pNode));
32,994✔
1593
      continue;
32,994✔
1594
    }
1595
    
1596
    SValueNode* pVal = (SValueNode*)pNode;
562,800✔
1597
    if (TSDB_DATA_TYPE_BIGINT != pVal->node.resType.type) {
562,800✔
1598
      msttWarn("invalid value node data type %d for runner's child subplan", pVal->node.resType.type);
×
1599
      continue;
×
1600
    }
1601

1602
    key[1] = MND_GET_RUNNER_SUBPLANID(pVal->datum.i);
562,800✔
1603

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

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

1617
_exit:
470,184✔
1618

1619
  if (code) {
470,184✔
1620
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1621
  }
1622

1623
  return code;
470,184✔
1624
}
1625

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

1634
  TAOS_CHECK_EXIT(msmUpdateLowestPlanSourceAddr(pPlan, pDeploy, streamId));
470,184✔
1635

1636
  SNode* pTmp = NULL;
470,184✔
1637
  WHERE_EACH(pTmp, pPlan->pChildren) {
1,065,978✔
1638
    if (QUERY_NODE_VALUE == nodeType(pTmp)) {
595,794✔
1639
      ERASE_NODE(pPlan->pChildren);
562,800✔
1640
      continue;
562,800✔
1641
    }
1642
    WHERE_NEXT;
32,994✔
1643
  }
1644
  nodesClearList(pPlan->pChildren);
470,184✔
1645
  pPlan->pChildren = NULL;
470,184✔
1646

1647
  if (NULL == pPlan->pParents) {
470,184✔
1648
    goto _exit;
437,190✔
1649
  }
1650

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

1666
  if (code) {
470,184✔
1667
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1668
  }
1669

1670
  return code;
470,184✔
1671
}
1672

1673
int32_t msmUpdateRunnerPlans(SStmGrpCtx* pCtx, SArray* pRunners, SStreamObj* pStream) {
437,190✔
1674
  int32_t code = TSDB_CODE_SUCCESS;
437,190✔
1675
  int32_t lino = 0;
437,190✔
1676
  int64_t streamId = pStream->pCreate->streamId;
437,190✔
1677
  int32_t runnerNum = taosArrayGetSize(pRunners);
437,190✔
1678
  
1679
  for (int32_t i = 0; i < runnerNum; ++i) {
907,374✔
1680
    SStmTaskDeploy* pDeploy = taosArrayGet(pRunners, i);
470,184✔
1681
    TAOS_CHECK_EXIT(msmUpdateRunnerPlan(pCtx, pRunners, i, pDeploy, pStream));
470,184✔
1682
    TAOS_CHECK_EXIT(nodesNodeToString((SNode*)pDeploy->msg.runner.pPlan, false, (char**)&pDeploy->msg.runner.pPlan, NULL));
470,184✔
1683

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

1688
_exit:
437,190✔
1689

1690
  if (code) {
437,190✔
1691
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1692
  }
1693

1694
  return code;
437,190✔
1695
}
1696

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

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

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

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

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

1740
  deployTaskList = taosArrayInit_s(sizeof(SStmTaskDeploy), pDag->numOfSubplans);
145,730✔
1741
  TSDB_CHECK_NULL(deployTaskList, code, lino, _exit, terrno);
145,730✔
1742
  
1743
  for (int32_t deployId = 0; deployId < pInfo->runnerDeploys; ++deployId) {
582,920✔
1744
    totalTaskNum = 0;
437,190✔
1745

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

1752
    taskIdx = 0;
437,190✔
1753
    
1754
    for (int32_t i = lowestLevelIdx; i >= 0; --i) {
890,877✔
1755
      plans = (SNodeListNode *)nodesListGetNode(pDag->pSubplans, i);
453,687✔
1756
      if (NULL == plans) {
453,687✔
1757
        mstsError("empty level plan, level:%d", i);
×
1758
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);
×
1759
      }
1760

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

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

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

1778
      for (int32_t n = 0; n < taskNum; ++n) {
923,871✔
1779
        SSubplan *plan = (SSubplan *)nodesListGetNode(plans->pNodeList, n);
470,184✔
1780
        pState = taosArrayReserve(deployList, 1);
470,184✔
1781

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

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

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

1809
      mstsDebug("deploy %d level %d initialized, taskNum:%d", deployId, i, taskNum);
453,687✔
1810
    }
1811

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

1817
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
437,190✔
1818

1819
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
437,190✔
1820

1821
    nodesDestroyNode((SNode *)pDag);
437,190✔
1822
    pDag = NULL;
437,190✔
1823
    
1824
    TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pDag));
437,190✔
1825

1826
    mstsDebug("total %d runner tasks added for deploy %d", totalTaskNum, deployId);
437,190✔
1827
  }
1828

1829
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
582,920✔
1830
    TAOS_CHECK_EXIT(msmSTAddToTaskMap(pCtx, streamId, pInfo->runners[i], NULL, NULL));
437,190✔
1831
    TAOS_CHECK_EXIT(msmSTAddToSnodeMap(pCtx, streamId, pInfo->runners[i], NULL, 0, i));
437,190✔
1832
  }
1833
  
1834
  pInfo->runnerNum = totalTaskNum;
145,730✔
1835
  
1836
_exit:
145,730✔
1837

1838
  if (code) {
145,730✔
1839
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1840
  }
1841

1842
  taosArrayDestroy(deployTaskList);
145,730✔
1843
  nodesDestroyNode((SNode *)pDag);
145,730✔
1844

1845
  return code;
145,730✔
1846
}
1847

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

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

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

UNCOV
1870
    pStartRunner = pRunner;
×
UNCOV
1871
    totalTaskNum = 0;
×
1872

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

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

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

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

UNCOV
1896
        pRunner->id.nodeId = newNodeId;
×
1897

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

UNCOV
1908
        pRunner++;
×
1909
      }
1910

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

UNCOV
1914
    TAOS_CHECK_EXIT(msmUpdateRunnerPlans(pCtx, deployTaskList, pStream));
×
1915

UNCOV
1916
    TAOS_CHECK_EXIT(msmTDAddRunnersToSnodeMap(deployTaskList, pStream));
×
1917

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

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

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

UNCOV
1926
_exit:
×
1927

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

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

UNCOV
1935
  return code;
×
1936
}
1937

1938

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

1947
_exit:
138,596✔
1948

1949
  if (code) {
138,596✔
1950
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1951
  }
1952

1953
  return code;
138,596✔
1954
}
1955

1956

1957
static int32_t msmBuildRunnerTasks(SStmGrpCtx* pCtx, SStmStatus* pInfo, SStreamObj* pStream) {
146,109✔
1958
  if (NULL == pStream->pCreate->calcPlan) {
146,109✔
1959
    return TSDB_CODE_SUCCESS;
379✔
1960
  }
1961
  
1962
  int32_t code = TSDB_CODE_SUCCESS;
145,730✔
1963
  int32_t lino = 0;
145,730✔
1964
  int64_t streamId = pStream->pCreate->streamId;
145,730✔
1965
  SQueryPlan* pPlan = NULL;
145,730✔
1966

1967
  TAOS_CHECK_EXIT(nodesStringToNode(pStream->pCreate->calcPlan, (SNode**)&pPlan));
145,730✔
1968

1969
  for (int32_t i = 0; i < pInfo->runnerDeploys; ++i) {
582,920✔
1970
    pInfo->runners[i] = taosArrayInit(pPlan->numOfSubplans, sizeof(SStmTaskStatus));
437,190✔
1971
    TSDB_CHECK_NULL(pInfo->runners[i], code, lino, _exit, terrno);
437,190✔
1972
  }
1973

1974
  code = msmBuildRunnerTasksImpl(pCtx, pPlan, pInfo, pStream);
145,730✔
1975
  pPlan = NULL;
145,730✔
1976
  
1977
  TAOS_CHECK_EXIT(code);
145,730✔
1978

1979
  taosHashClear(mStreamMgmt.toUpdateScanMap);
145,730✔
1980
  mStreamMgmt.toUpdateScanNum = 0;
145,730✔
1981

1982
_exit:
145,730✔
1983

1984
  nodesDestroyNode((SNode *)pPlan);
145,730✔
1985

1986
  if (code) {
145,730✔
1987
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
1988
  }
1989

1990
  return code;
145,730✔
1991
}
1992

1993

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

1999
  mstsInfo("start to deploy stream tasks, deployTimes:%" PRId64, pInfo->deployTimes);
146,109✔
2000

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

2007
  TAOS_CHECK_EXIT(msmBuildReaderTasks(pCtx, pInfo, pStream));
146,109✔
2008
  TAOS_CHECK_EXIT(msmBuildRunnerTasks(pCtx, pInfo, pStream));
146,109✔
2009
  TAOS_CHECK_EXIT(msmBuildTriggerTasks(pCtx, pInfo, pStream));
146,109✔
2010
  
2011
_exit:
146,109✔
2012

2013
  if (code) {
146,109✔
2014
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2015
  }
2016

2017
  return code;
146,109✔
2018
}
2019

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

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

2059
_exit:
×
2060

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

2066
  return code;
×
2067
}
2068

2069

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

2075
  pStatus->lastActionTs = INT64_MIN;
138,975✔
2076

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

2082
  TAOS_CHECK_EXIT(tCloneStreamCreateDeployPointers(pStream->pCreate, &pStatus->pCreate));
138,975✔
2083
  
2084
  if (pStream->pCreate->numOfCalcSubplan > 0) {
138,975✔
2085
    pStatus->runnerNum = pStream->pCreate->numOfCalcSubplan;
138,596✔
2086
    
2087
    TAOS_CHECK_EXIT(msmSetStreamRunnerExecReplica(streamId, pStatus));
138,596✔
2088
  }
2089

2090
  if (initList) {
138,975✔
2091
    TAOS_CHECK_EXIT(msmInitTrigReaderList(pCtx, pStatus, pStream));
×
2092

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

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

2110
  if (code) {
138,975✔
2111
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2112
  }
2113

2114
  return code;
138,975✔
2115
}
2116

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

2123
  if (NULL == pStatus) {
146,109✔
2124
    TAOS_CHECK_EXIT(msmInitStmStatus(pCtx, &info, pStream, false));
138,975✔
2125

2126
    TAOS_CHECK_EXIT(taosHashPut(mStreamMgmt.streamMap, &streamId, sizeof(streamId), &info, sizeof(info)));
138,975✔
2127

2128
    pStatus = taosHashGet(mStreamMgmt.streamMap, &streamId, sizeof(streamId));
138,975✔
2129
  }
2130
  
2131
  TAOS_CHECK_EXIT(msmBuildStreamTasks(pCtx, pStatus, pStream));
146,109✔
2132

2133
  mstLogSStmStatus("stream deployed", streamId, pStatus);
146,109✔
2134

2135
_exit:
146,109✔
2136

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

2146
  return code;
146,109✔
2147
}
2148

2149

2150
static int32_t msmSTRemoveStream(int64_t streamId, bool fromStreamMap) {
7,885✔
2151
  int32_t code = TSDB_CODE_SUCCESS;
7,885✔
2152
  void* pIter = NULL;
7,885✔
2153

2154
  while ((pIter = taosHashIterate(mStreamMgmt.toDeployVgMap, pIter))) {
12,061✔
2155
    SStmVgTasksToDeploy* pVg = (SStmVgTasksToDeploy*)pIter;
4,176✔
2156
    (void)mstWaitLock(&pVg->lock, true);
4,176✔
2157

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

2164
    for (int32_t i = 0; i < taskNum; ++i) {
36,800✔
2165
      SStmTaskToDeployExt* pExt = taosArrayGet(pVg->taskList, i);
32,624✔
2166
      if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
32,624✔
2167
        continue;
28,496✔
2168
      }
2169

2170
      mstDestroySStmTaskToDeployExt(pExt);
4,128✔
2171
      pExt->deployed = true;
4,128✔
2172
    }
2173
    
2174
    taosRUnLockLatch(&pVg->lock);
4,176✔
2175
  }
2176

2177
  while ((pIter = taosHashIterate(mStreamMgmt.toDeploySnodeMap, pIter))) {
15,933✔
2178
    SStmSnodeTasksDeploy* pSnode = (SStmSnodeTasksDeploy*)pIter;
8,048✔
2179
    (void)mstWaitLock(&pSnode->lock, true);
8,048✔
2180

2181
    int32_t taskNum = taosArrayGetSize(pSnode->triggerList);
8,048✔
2182
    if (atomic_load_32(&pSnode->triggerDeployed) != taskNum) {
8,048✔
2183
      for (int32_t i = 0; i < taskNum; ++i) {
22,248✔
2184
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, i);
15,960✔
2185
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
15,960✔
2186
          continue;
13,896✔
2187
        }
2188
        
2189
        mstDestroySStmTaskToDeployExt(pExt);
2,064✔
2190
        pExt->deployed = true;
2,064✔
2191
      }
2192
    }
2193

2194
    taskNum = taosArrayGetSize(pSnode->runnerList);
8,048✔
2195
    if (atomic_load_32(&pSnode->runnerDeployed) != taskNum) {
8,048✔
2196
      for (int32_t i = 0; i < taskNum; ++i) {
56,632✔
2197
        SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, i);
48,584✔
2198
        if (pExt->deployed || pExt->deploy.task.streamId != streamId) {
48,584✔
2199
          continue;
42,392✔
2200
        }
2201
        
2202
        mstDestroySStmTaskToDeployExt(pExt);
6,192✔
2203
        pExt->deployed = true;
6,192✔
2204
      }
2205
    }
2206

2207
    taosRUnLockLatch(&pSnode->lock);
8,048✔
2208
  }
2209

2210
  
2211
  while ((pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter))) {
28,442✔
2212
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
20,557✔
2213
    code = taosHashRemove(pSnode->streamTasks, &streamId, sizeof(streamId));
20,557✔
2214
    if (TSDB_CODE_SUCCESS == code) {
20,557✔
2215
      mstsDebug("stream removed from snodeMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pSnode->streamTasks));
11,757✔
2216
    }
2217
  }
2218

2219
  while ((pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter))) {
25,254✔
2220
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
17,369✔
2221
    code = taosHashRemove(pVg->streamTasks, &streamId, sizeof(streamId));
17,369✔
2222
    if (TSDB_CODE_SUCCESS == code) {
17,369✔
2223
      mstsDebug("stream removed from vgroupMap %d, remainStreams:%d", *(int32_t*)taosHashGetKey(pIter, NULL), (int32_t)taosHashGetSize(pVg->streamTasks));
12,112✔
2224
    }
2225
  }
2226

2227
  size_t keyLen = 0;
7,885✔
2228
  while ((pIter = taosHashIterate(mStreamMgmt.taskMap, pIter))) {
376,092✔
2229
    int64_t* pStreamId = taosHashGetKey(pIter, &keyLen);
368,207✔
2230
    if (*pStreamId == streamId) {
368,207✔
2231
      int64_t taskId = *(pStreamId + 1);
50,860✔
2232
      code = taosHashRemove(mStreamMgmt.taskMap, pStreamId, keyLen);
50,860✔
2233
      if (code) {
50,860✔
2234
        mstsError("TASK:%" PRIx64 " remove from taskMap failed, error:%s", taskId, tstrerror(code));
×
2235
      } else {
2236
        mstsDebug("TASK:%" PRIx64 " removed from taskMap", taskId);
50,860✔
2237
      }
2238
    }
2239
  }
2240

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

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

2258
  mstResetSStmStatus(pStatus);
7,134✔
2259

2260
  pStatus->deployTimes++;
7,134✔
2261
}
7,134✔
2262

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

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

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

2296
  TAOS_CHECK_EXIT(code);
146,109✔
2297

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

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

2314
_exit:
146,109✔
2315

2316
  mndReleaseStream(pCtx->pMnode, pStream);
146,109✔
2317

2318
  if (code) {
146,109✔
2319
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2320
  }
2321

2322
  return code;
146,109✔
2323
}
2324

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

2357
_exit:
2,112✔
2358

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

2363
  return code;
2,112✔
2364
}
2365

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

2397
_exit:
2398

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

2403
  return code;
2404
}
2405
*/
2406

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

UNCOV
2456
_exit:
×
2457

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

UNCOV
2462
  return code;
×
2463
}
2464

2465

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

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

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

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

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

2493
  TAOS_CHECK_EXIT(code);
2,112✔
2494

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

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

2525
_exit:
2,112✔
2526

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

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

2536
  return code;
2,112✔
2537
}
2538

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

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

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

2584
  return TSDB_CODE_SUCCESS;
×
2585
}
2586

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

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

2593
  TAOS_CHECK_EXIT(msmSTRemoveStream(streamId, true));
751✔
2594

2595
_exit:
751✔
2596

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

2603
  return code;
751✔
2604
}
2605

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

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

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

2622
  atomic_store_8(&pStream->stopped, 2);
15,881✔
2623

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

2626
_exit:
×
2627

2628
  taosHashRelease(mStreamMgmt.streamMap, pStream);
16,246✔
2629

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

2634
  return;
16,246✔
2635
}
2636

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

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

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

2654
  TAOS_CHECK_EXIT(mstAppendNewRecalcRange(streamId, pStream, timeRange));
6,498✔
2655

2656
_exit:
6,498✔
2657

2658
  taosHashRelease(mStreamMgmt.streamMap, pStream);
6,498✔
2659

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

2664
  return code;
6,498✔
2665
}
2666

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

2672
  while (mndStreamActionDequeue(mStreamMgmt.actionQ, &pQNode)) {
187,109✔
2673
    switch (pQNode->type) {
148,961✔
2674
      case STREAM_ACT_DEPLOY:
148,961✔
2675
        if (pQNode->streamAct) {
148,961✔
2676
          mstDebug("start to handle stream deploy action");
146,849✔
2677
          TAOS_CHECK_EXIT(msmLaunchStreamDeployAction(pCtx, &pQNode->action.stream));
146,849✔
2678
        } else {
2679
          mstDebug("start to handle task deploy action");
2,112✔
2680
          TAOS_CHECK_EXIT(msmLaunchTaskDeployAction(pCtx, &pQNode->action.task));
2,112✔
2681
        }
2682
        break;
148,961✔
2683
      default:
×
2684
        break;
×
2685
    }
2686
  }
2687

2688
_exit:
38,148✔
2689

2690
  if (code) {
38,148✔
2691
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2692
  }
2693
}
38,148✔
2694

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

2706
    pStatus = (SStmStatus*)pIter;
×
2707

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

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

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

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

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

2725
  msmStopAllStreamsByGrant(errCode);
×
2726

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

2732
static int32_t msmInitStreamDeploy(SStmStreamDeploy* pStream, SStmTaskDeploy* pDeploy) {
926,260✔
2733
  int32_t code = TSDB_CODE_SUCCESS;
926,260✔
2734
  int32_t lino = 0;
926,260✔
2735
  int64_t streamId = pDeploy->task.streamId;
926,260✔
2736
  
2737
  switch (pDeploy->task.type) {
926,260✔
2738
    case STREAM_READER_TASK:
318,223✔
2739
      if (NULL == pStream->readerTasks) {
318,223✔
2740
        pStream->streamId = streamId;
159,281✔
2741
        pStream->readerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
159,281✔
2742
        TSDB_CHECK_NULL(pStream->readerTasks, code, lino, _exit, terrno);
159,281✔
2743
      }
2744
      
2745
      TSDB_CHECK_NULL(taosArrayPush(pStream->readerTasks, pDeploy), code, lino, _exit, terrno);
636,446✔
2746
      break;
318,223✔
2747
    case STREAM_TRIGGER_TASK:
144,045✔
2748
      pStream->streamId = streamId;
144,045✔
2749
      pStream->triggerTask = taosMemoryMalloc(sizeof(SStmTaskDeploy));
144,045✔
2750
      TSDB_CHECK_NULL(pStream->triggerTask, code, lino, _exit, terrno);
144,045✔
2751
      memcpy(pStream->triggerTask, pDeploy, sizeof(SStmTaskDeploy));
144,045✔
2752
      break;
144,045✔
2753
    case STREAM_RUNNER_TASK:
463,992✔
2754
      if (NULL == pStream->runnerTasks) {
463,992✔
2755
        pStream->streamId = streamId;
153,511✔
2756
        pStream->runnerTasks = taosArrayInit(20, sizeof(SStmTaskDeploy));
153,511✔
2757
        TSDB_CHECK_NULL(pStream->runnerTasks, code, lino, _exit, terrno);
153,511✔
2758
      }      
2759
      TSDB_CHECK_NULL(taosArrayPush(pStream->runnerTasks, pDeploy), code, lino, _exit, terrno);
927,984✔
2760
      break;
463,992✔
2761
    default:
×
2762
      break;
×
2763
  }
2764

2765
_exit:
926,260✔
2766

2767
  if (code) {
926,260✔
2768
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
2769
  }
2770

2771
  return code;
926,260✔
2772
}
2773

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

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

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

2799
    TAOS_CHECK_EXIT(msmInitStreamDeploy(pStream, pDeploy));
752,526✔
2800
    
2801
    break;
752,526✔
2802
  }
2803
  
2804
_exit:
926,260✔
2805

2806
  taosHashRelease(pHash, pStream);
926,260✔
2807

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

2814
  return code;
926,260✔
2815
}
2816

2817

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

2823
  for (int32_t i = 0; i < taskNum; ++i) {
1,126,631✔
2824
    SStmTaskToDeployExt* pExt = taosArrayGet(pTasks, i);
938,644✔
2825
    if (pExt->deployed) {
938,644✔
2826
      continue;
12,384✔
2827
    }
2828

2829
    TAOS_CHECK_EXIT(msmGrpAddDeployTask(pHash, &pExt->deploy));
926,260✔
2830
    pExt->deployed = true;
926,260✔
2831

2832
    (void)atomic_add_fetch_32(deployed, 1);
926,260✔
2833
  }
2834

2835
_exit:
187,987✔
2836

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

2841
  return code;
187,987✔
2842
}
2843

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

2851
  mstDebug("start to add stream vgroup tasks deploy");
48,293✔
2852
  
2853
  for (int32_t i = 0; i < vgNum; ++i) {
291,385✔
2854
    int32_t* vgId = taosArrayGet(pCtx->pReq->pVgLeaders, i);
243,092✔
2855

2856
    msmUpdateVgroupUpTs(pCtx, *vgId);
243,092✔
2857

2858
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
243,092✔
2859
    if (NULL == pVg) {
243,092✔
2860
      continue;
137,754✔
2861
    }
2862

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

2876
_exit:
48,293✔
2877

2878
  if (code) {
48,293✔
2879
    if (pVg) {
×
2880
      taosRUnLockLatch(&pVg->lock);
×
2881
    }
2882

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

2886
  return code;
48,293✔
2887
}
2888

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

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

2902
  (void)mstWaitLock(&pSnode->lock, false);
42,651✔
2903
  
2904
  if (atomic_load_32(&pSnode->triggerDeployed) < taosArrayGetSize(pSnode->triggerList)) {
42,651✔
2905
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->triggerList, &pSnode->triggerDeployed));
39,998✔
2906
  }
2907

2908
  if (atomic_load_32(&pSnode->runnerDeployed) < taosArrayGetSize(pSnode->runnerList)) {
42,651✔
2909
    TAOS_CHECK_EXIT(msmGrpAddDeployTasks(pCtx->deployStm, pSnode->runnerList, &pSnode->runnerDeployed));
42,651✔
2910
  }
2911
  
2912
  taosWUnLockLatch(&pSnode->lock);
42,651✔
2913

2914
_exit:
42,651✔
2915

2916
  if (code) {
42,651✔
2917
    if (pSnode) {
×
2918
      taosWUnLockLatch(&pSnode->lock);
×
2919
    }
2920

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

2924
  return code;
42,651✔
2925
}
2926

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

2938
_exit:
398,360✔
2939

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

2944
  return code;
398,360✔
2945
}
2946

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

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

2958
  while (1) {
173,734✔
2959
    pIter = taosHashIterate(pCtx->deployStm, pIter);
223,787✔
2960
    if (pIter == NULL) {
223,787✔
2961
      break;
50,053✔
2962
    }
2963
    
2964
    SStmStreamDeploy *pDeploy = (SStmStreamDeploy *)pIter;
173,734✔
2965
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->deploy.streamList, pDeploy), code, lino, _exit, terrno);
347,468✔
2966

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

2971
    mstClearSStmStreamDeploy(pDeploy);
173,734✔
2972
    
2973
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(pDeploy->streamId, pCtx->currTs));
173,734✔
2974
  }
2975
  
2976
_exit:
50,053✔
2977

2978
  if (pIter) {
50,053✔
2979
    taosHashCancelIterate(pCtx->deployStm, pIter);
×
2980
  }
2981

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

2989
void msmCleanDeployedVgTasks(SArray* pVgLeaders) {
48,293✔
2990
  int32_t code = TSDB_CODE_SUCCESS;
48,293✔
2991
  int32_t lino = 0;
48,293✔
2992
  int32_t vgNum = taosArrayGetSize(pVgLeaders);
48,293✔
2993
  SStmVgTasksToDeploy* pVg = NULL;
48,293✔
2994
  
2995
  for (int32_t i = 0; i < vgNum; ++i) {
291,385✔
2996
    int32_t* vgId = taosArrayGet(pVgLeaders, i);
243,092✔
2997
    pVg = taosHashAcquire(mStreamMgmt.toDeployVgMap, vgId, sizeof(*vgId));
243,092✔
2998
    if (NULL == pVg) {
243,092✔
2999
      continue;
137,754✔
3000
    }
3001

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

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

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

3030
      mstDestroySStmTaskToDeployExt(pExt);
8,256✔
3031

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

3040
_exit:
48,293✔
3041

3042
  if (code) {
48,293✔
3043
    if (pVg) {
×
3044
      taosWUnLockLatch(&pVg->lock);
×
3045
    }
3046

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

3051
void msmCleanDeployedSnodeTasks (int32_t snodeId) {
46,423✔
3052
  if (!GOT_SNODE(snodeId)) {
46,423✔
3053
    return;
352✔
3054
  }
3055
  
3056
  int32_t code = TSDB_CODE_SUCCESS;
46,071✔
3057
  SStmSnodeTasksDeploy* pSnode = taosHashAcquire(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId));
46,071✔
3058
  if (NULL == pSnode) {
46,071✔
3059
    return;
3,420✔
3060
  }
3061

3062
  if (taosWTryLockLatch(&pSnode->lock)) {
42,651✔
3063
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
×
3064
    return;
×
3065
  }
3066

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

3076
  if (atomic_load_32(&pSnode->triggerDeployed) == triggerNum) {
42,651✔
3077
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, triggerNum);
41,779✔
3078
    taosArrayDestroyEx(pSnode->triggerList, mstDestroySStmTaskToDeployExt);
41,779✔
3079
    pSnode->triggerList = NULL;
41,779✔
3080
  }
3081

3082
  if (atomic_load_32(&pSnode->runnerDeployed) == runnerNum) {
42,651✔
3083
    (void)atomic_sub_fetch_32(&mStreamMgmt.toDeploySnodeTaskNum, runnerNum);
41,779✔
3084
    taosArrayDestroyEx(pSnode->runnerList, mstDestroySStmTaskToDeployExt);
41,779✔
3085
    pSnode->runnerList = NULL;
41,779✔
3086
  }
3087

3088
  if (NULL == pSnode->triggerList && NULL == pSnode->runnerList) {
42,651✔
3089
    TAOS_UNUSED(taosHashRemove(mStreamMgmt.toDeploySnodeMap, &snodeId, sizeof(snodeId)));
41,779✔
3090
    taosWUnLockLatch(&pSnode->lock);
41,779✔
3091
    taosHashRelease(mStreamMgmt.toDeploySnodeMap, pSnode);
41,779✔
3092
    return;
41,779✔
3093
  }
3094

3095
  if (atomic_load_32(&pSnode->triggerDeployed) > 0 && pSnode->triggerList) {
872✔
3096
    for (int32_t m = triggerNum - 1; m >= 0; --m) {
5,000✔
3097
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->triggerList, m);
4,128✔
3098
      if (!pExt->deployed) {
4,128✔
3099
        continue;
×
3100
      }
3101

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

3110
  if (atomic_load_32(&pSnode->runnerDeployed) > 0 && pSnode->runnerList) {
872✔
3111
    for (int32_t m = runnerNum - 1; m >= 0; --m) {
13,256✔
3112
      SStmTaskToDeployExt* pExt = taosArrayGet(pSnode->runnerList, m);
12,384✔
3113
      if (!pExt->deployed) {
12,384✔
3114
        continue;
×
3115
      }
3116

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

3129
void msmClearStreamToDeployMaps(SStreamHbMsg* pHb) {
14,985,724✔
3130
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,985,724✔
3131
    msmCleanDeployedVgTasks(pHb->pVgLeaders);
48,293✔
3132
  }
3133

3134
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0) {
14,985,724✔
3135
    msmCleanDeployedSnodeTasks(pHb->snodeId);
46,423✔
3136
  }
3137
}
14,985,724✔
3138

3139
void msmCleanStreamGrpCtx(SStreamHbMsg* pHb) {
14,985,724✔
3140
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,985,724✔
3141
  if (mStreamMgmt.tCtx) {
14,985,724✔
3142
    taosHashClear(mStreamMgmt.tCtx[tidx].actionStm[pHb->streamGId]);
14,786,962✔
3143
    taosHashClear(mStreamMgmt.tCtx[tidx].deployStm[pHb->streamGId]);
14,786,962✔
3144
  }
3145
}
14,985,724✔
3146

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

3164
_exit:
167,732✔
3165

3166
  if (code) {
167,732✔
3167
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3168
  }
3169

3170
  return code;
167,732✔
3171
}
3172

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

3189
_exit:
×
3190

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

3195
  return code;
×
3196
}
3197

3198

3199

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

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

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

3239
_exit:
56,894✔
3240

3241
  if (code) {
56,894✔
3242
    mstsError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3243
  }
3244

3245
  return code;
56,894✔
3246
}
3247

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

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

3269
_exit:
6,076✔
3270

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

3276
  return code;
6,076✔
3277
}
3278

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

3285
  if (pStream->triggerTask->id.nodeId != snodeId || STREAM_STATUS_INIT != pStream->triggerTask->status) {
200,860✔
3286
    return false;
×
3287
  }
3288

3289
  int32_t readerNum = taosArrayGetSize(pStream->trigReaders);
200,860✔
3290
  for (int32_t i = 0; i < readerNum; ++i) {
450,323✔
3291
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigReaders, i);
249,463✔
3292
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
249,463✔
3293
      return false;
×
3294
    }
3295
  }
3296

3297
  readerNum = taosArrayGetSize(pStream->trigOReaders);
200,860✔
3298
  for (int32_t i = 0; i < readerNum; ++i) {
211,124✔
3299
    SStmTaskStatus* pStatus = taosArrayGet(pStream->trigOReaders, i);
10,264✔
3300
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
10,264✔
3301
      return false;
×
3302
    }
3303
  }
3304

3305
  readerNum = MST_LIST_SIZE(pStream->calcReaders);
200,860✔
3306
  SListNode* pNode = listHead(pStream->calcReaders);
200,860✔
3307
  for (int32_t i = 0; i < readerNum; ++i) {
367,449✔
3308
    SStmTaskStatus* pStatus = (SStmTaskStatus*)pNode->data;
166,589✔
3309
    if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
166,589✔
3310
      return false;
×
3311
    }
3312
    pNode = TD_DLIST_NODE_NEXT(pNode);
166,589✔
3313
  }
3314

3315
  for (int32_t i = 0; i < pStream->runnerDeploys; ++i) {
702,919✔
3316
    int32_t runnerNum = taosArrayGetSize(pStream->runners[i]);
535,187✔
3317
    for (int32_t m = 0; m < runnerNum; ++m) {
1,070,240✔
3318
      SStmTaskStatus* pStatus = taosArrayGet(pStream->runners[i], m);
568,181✔
3319
      if (STREAM_STATUS_INIT != pStatus->status && STREAM_STATUS_RUNNING != pStatus->status) {
568,181✔
3320
        return false;
33,128✔
3321
      }
3322
    }
3323
  }
3324
  
3325
  return true;
167,732✔
3326
}
3327

3328

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

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

3346
  stopped = atomic_load_8(&pStatus->stopped);
6,782,510✔
3347
  if (stopped) {
6,782,510✔
3348
    msttInfo("stream stopped %d, try to undeploy current task, idx:%d", stopped, pMsg->taskIdx);
×
3349
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
×
3350
    return;
×
3351
  }
3352
  
3353
  switch (pMsg->status) {
6,782,510✔
3354
    case STREAM_STATUS_INIT:      
6,778,486✔
3355
      if (STREAM_TRIGGER_TASK != pMsg->type) {
6,778,486✔
3356
        msttTrace("task status is INIT and not trigger task, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
6,457,148✔
3357
        return;
6,457,148✔
3358
      }
3359
      
3360
      if (INT64_MIN == pStatus->lastActionTs) {
321,338✔
3361
        msttDebug("task still not deployed, ignore it, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
×
3362
        return;
×
3363
      }
3364
      
3365
      if ((pCtx->currTs - pStatus->lastActionTs) < STREAM_ACT_MIN_DELAY_MSEC) {
321,338✔
3366
        msttDebug("task wait not enough between actions, currTs:%" PRId64 ", lastTs:%" PRId64, pCtx->currTs, pStatus->lastActionTs);
120,478✔
3367
        return;
120,478✔
3368
      }
3369

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

3385
_exit:
204,884✔
3386

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

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

3399
  msttInfo("start to handle task status update exception, type: %d", err);
52,870✔
3400
  
3401
  // STREAMTODO
3402

3403
  if (STM_ERR_TASK_NOT_EXISTS == err || STM_ERR_STREAM_STOPPED == err) {
52,870✔
3404
    TAOS_CHECK_EXIT(msmGrpAddActionUndeploy(pCtx, streamId, pTask));
52,870✔
3405
  }
3406

3407
_exit:
52,870✔
3408

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

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

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

3438
  if (pTask->detailStatus >= 0 && pCtx->pReq->pTriggerStatus) {
2,663,060✔
3439
    (void)mstWaitLock(&pStatus->detailStatusLock, false);
1,335,895✔
3440
    if (NULL == pStatus->detailStatus) {
1,335,895✔
3441
      pStatus->detailStatus = taosMemoryCalloc(1, sizeof(SSTriggerRuntimeStatus));
141,871✔
3442
      if (NULL == pStatus->detailStatus) {
141,871✔
3443
        taosWUnLockLatch(&pStatus->detailStatusLock);
×
3444
        TSDB_CHECK_NULL(pStatus->detailStatus, code, lino, _exit, terrno);
×
3445
      }
3446
    }
3447
    
3448
    memcpy(pStatus->detailStatus, taosArrayGet(pCtx->pReq->pTriggerStatus, pTask->detailStatus), sizeof(SSTriggerRuntimeStatus));
1,335,895✔
3449
    taosWUnLockLatch(&pStatus->detailStatusLock);
1,335,895✔
3450
  }
3451

3452
_exit:
1,327,165✔
3453

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

3460
int32_t msmNormalHandleStatusUpdate(SStmGrpCtx* pCtx) {
1,176,196✔
3461
  int32_t code = TSDB_CODE_SUCCESS;
1,176,196✔
3462
  int32_t lino = 0;
1,176,196✔
3463
  int32_t num = taosArrayGetSize(pCtx->pReq->pStreamStatus);
1,176,196✔
3464

3465
  mstDebug("NORMAL: start to handle stream group %d tasks status, taskNum:%d", pCtx->pReq->streamGId, num);
1,176,196✔
3466

3467
  for (int32_t i = 0; i < num; ++i) {
17,886,220✔
3468
    SStmTaskStatusMsg* pTask = taosArrayGet(pCtx->pReq->pStreamStatus, i);
16,710,024✔
3469
    msttDebug("task status %s got on dnode %d, taskIdx:%d", gStreamStatusStr[pTask->status], pCtx->pReq->dnodeId, pTask->taskIdx);
16,710,024✔
3470
    
3471
    SStmTaskStatus** ppStatus = taosHashGet(mStreamMgmt.taskMap, &pTask->streamId, sizeof(pTask->streamId) + sizeof(pTask->taskId));
16,710,024✔
3472
    if (NULL == ppStatus) {
16,710,024✔
3473
      msttWarn("task no longer exists in taskMap, will try to undeploy current task, taskIdx:%d", pTask->taskIdx);
12,328✔
3474
      msmHandleStatusUpdateErr(pCtx, STM_ERR_TASK_NOT_EXISTS, pTask);
12,328✔
3475
      continue;
12,328✔
3476
    }
3477

3478
    SStmStatus* pStream = (SStmStatus*)(*ppStatus)->pStream;
16,697,696✔
3479
    int8_t stopped = atomic_load_8(&pStream->stopped);
16,697,696✔
3480
    if (stopped) {
16,697,696✔
3481
      msttWarn("stream already stopped %d, will try to undeploy current task, taskIdx:%d", stopped, pTask->taskIdx);
40,542✔
3482
      msmHandleStatusUpdateErr(pCtx, STM_ERR_STREAM_STOPPED, pTask);
40,542✔
3483
      continue;
40,542✔
3484
    }
3485

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

3494
    if ((*ppStatus)->status != pTask->status) {
16,657,154✔
3495
      if (STREAM_STATUS_RUNNING == pTask->status) {
1,561,591✔
3496
        (*ppStatus)->runningStartTs = pCtx->currTs;
641,931✔
3497
      } else if (MST_IS_RUNNER_GETTING_READY(pTask) && STREAM_IS_REDEPLOY_RUNNER((*ppStatus)->flags)) {
919,660✔
3498
        if (pStream->triggerTask) {
×
3499
          atomic_store_8(&pStream->triggerNeedUpdate, 1);
×
3500
        }
3501
        
3502
        STREAM_CLR_FLAG((*ppStatus)->flags, STREAM_FLAG_REDEPLOY_RUNNER);
×
3503
      }
3504
    }
3505
    
3506
    (*ppStatus)->errCode = pTask->errorCode;
16,657,154✔
3507
    (*ppStatus)->status = pTask->status;
16,657,154✔
3508
    (*ppStatus)->lastUpTs = pCtx->currTs;
16,657,154✔
3509
    
3510
    if (STREAM_STATUS_RUNNING != pTask->status) {
16,657,154✔
3511
      msmHandleTaskAbnormalStatus(pCtx, pTask, *ppStatus);
6,782,510✔
3512
    }
3513
    
3514
    if (STREAM_TRIGGER_TASK == pTask->type) {
16,657,154✔
3515
      msmChkHandleTriggerOperations(pCtx, pTask, *ppStatus);
2,663,060✔
3516
    }
3517
  }
3518

3519
_exit:
1,176,196✔
3520

3521
  if (code) {
1,176,196✔
3522
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3523
  }
3524

3525
  return code;
1,176,196✔
3526
}
3527

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

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

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

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

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

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

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

3625
_exit:
×
3626

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

3633
  return code;
×
3634
}
3635

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

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

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

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

3664
_exit:
×
3665

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

3670
  return code;
×
3671
}
3672

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

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

3690
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->start.taskList, &start), code, lino, _exit, terrno);
335,464✔
3691
  TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
167,732✔
3692

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

3695
  return;
167,732✔
3696

3697
_exit:
×
3698

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

3702

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

3712
  SStreamTaskUndeploy undeploy;
22,163✔
3713
  for (int32_t i = 0; i < dropNum; ++i) {
79,057✔
3714
    SStreamTask* pTask = (SStreamTask*)taosArrayGetP(pAction->undeploy.taskList, i);
56,894✔
3715
    undeploy.task = *pTask;
56,894✔
3716
    undeploy.undeployMsg.doCheckpoint = pAction->undeploy.doCheckpoint;
56,894✔
3717
    undeploy.undeployMsg.doCleanup = pAction->undeploy.doCleanup;
56,894✔
3718

3719
    TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->undeploy.taskList, &undeploy), code, lino, _exit, terrno);
113,788✔
3720
    TAOS_CHECK_EXIT(msmUpdateStreamLastActTs(streamId, pCtx->currTs));
56,894✔
3721

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

3725
  return;
22,163✔
3726

3727
_exit:
×
3728

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

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

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

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

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

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

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

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

3762
_exit:
×
3763

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

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

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

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

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

3793
  if (NULL == pCtx->pRsp->rsps.rspList) {
6,076✔
3794
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
5,716✔
3795
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
5,716✔
3796
  }
3797

3798
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
12,152✔
3799

3800
_exit:
6,076✔
3801

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

3809

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

3816
  mstDebug("start to handle stream group %d post actions", pCtx->pReq->streamGId);
111,814✔
3817

3818
  while (1) {
195,971✔
3819
    pIter = taosHashIterate(pCtx->actionStm, pIter);
307,785✔
3820
    if (pIter == NULL) {
307,785✔
3821
      break;
111,814✔
3822
    }
3823

3824
    int64_t* pStreamId = taosHashGetKey(pIter, NULL);
195,971✔
3825
    SStmAction *pAction = (SStmAction *)pIter;
195,971✔
3826
    
3827
    if (STREAM_ACT_UNDEPLOY & pAction->actions) {
195,971✔
3828
      msmRspAddStreamUndeploy(*pStreamId, pCtx, pAction);
22,163✔
3829
      continue;
22,163✔
3830
    }
3831

3832
    if (STREAM_ACT_UPDATE_TRIGGER & pAction->actions) {
173,808✔
3833
      msmRspAddTriggerUpdate(pCtx->pMnode, *pStreamId, pCtx, pAction);
×
3834
    }
3835

3836
    if (STREAM_ACT_RECALC & pAction->actions) {
173,808✔
3837
      msmRspAddUserRecalc(pCtx->pMnode, *pStreamId, pCtx, pAction);
6,076✔
3838
    }
3839

3840
    if (STREAM_ACT_START & pAction->actions) {
173,808✔
3841
      msmRspAddStreamStart(*pStreamId, pCtx, streamNum, pAction);
167,732✔
3842
    }
3843
  }
3844
  
3845
_exit:
111,814✔
3846

3847
  if (pIter) {
111,814✔
3848
    taosHashCancelIterate(pCtx->actionStm, pIter);
×
3849
  }
3850

3851
  if (code) {
111,814✔
3852
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
3853
  }
3854

3855
  return code;
111,814✔
3856
}
3857

3858
int32_t msmCheckUpdateDnodeTs(SStmGrpCtx* pCtx) {
14,786,962✔
3859
  int32_t  code = TSDB_CODE_SUCCESS;
14,786,962✔
3860
  int32_t  lino = 0;
14,786,962✔
3861
  int64_t* lastTs = NULL;
14,786,962✔
3862
  bool     noExists = false;
14,786,962✔
3863

3864
  while (true) {
3865
    lastTs = taosHashGet(mStreamMgmt.dnodeMap, &pCtx->pReq->dnodeId, sizeof(pCtx->pReq->dnodeId));
15,326,549✔
3866
    if (NULL == lastTs) {
15,326,549✔
3867
      if (noExists) {
732,762✔
3868
        mstWarn("Got unknown dnode %d hb msg, may be dropped", pCtx->pReq->dnodeId);
193,175✔
3869
        TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_NODE_NOT_EXISTS);
193,175✔
3870
      }
3871

3872
      noExists = true;
539,587✔
3873
      TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pCtx->pMnode));
539,587✔
3874
      
3875
      continue;
539,587✔
3876
    }
3877

3878
    while (true) {
×
3879
      int64_t lastTsValue = atomic_load_64(lastTs);
14,593,787✔
3880
      if (pCtx->currTs > lastTsValue) {
14,593,787✔
3881
        if (lastTsValue == atomic_val_compare_exchange_64(lastTs, lastTsValue, pCtx->currTs)) {
14,593,749✔
3882
          mstDebug("dnode %d lastUpTs updated", pCtx->pReq->dnodeId);
14,593,749✔
3883
          return code;
14,593,749✔
3884
        }
3885

3886
        continue;
×
3887
      }
3888

3889
      return code;
38✔
3890
    }
3891

3892
    break;
3893
  }
3894

3895
_exit:
193,175✔
3896

3897
  if (code) {
193,175✔
3898
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
193,175✔
3899
  }
3900

3901
  return code;  
193,175✔
3902
}
3903

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

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

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

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

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

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

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

3960
  while (atomic_load_32(&mStreamMgmt.watch.processing) > minVal) {
256✔
3961
    (void)sched_yield();
×
3962
  }
3963

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

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

3978
  msmWatchCheckStreamMap(pCtx);
×
3979

3980
_exit:
256✔
3981

3982
  mStreamMgmt.lastTaskId += 100000;
256✔
3983

3984
  mstInfo("watch state end, new taskId begin from:%" PRIx64, mStreamMgmt.lastTaskId);
256✔
3985

3986
  msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
256✔
3987

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

3992
  return code;
256✔
3993
}
3994

3995

3996
int32_t msmWatchHandleHbMsg(SStmGrpCtx* pCtx) {
1,024✔
3997
  int32_t code = TSDB_CODE_SUCCESS;
1,024✔
3998
  int32_t lino = 0;
1,024✔
3999
  SStreamHbMsg* pReq = pCtx->pReq;
1,024✔
4000

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

4007
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
1,024✔
4008
  if (GOT_SNODE(pReq->snodeId)) {
1,024✔
4009
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,024✔
4010
  }
4011

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

4017
  if ((pCtx->currTs - MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN)) > MST_SHORT_ISOLATION_DURATION) {
1,024✔
4018
    TAOS_CHECK_EXIT(msmWatchHandleEnding(pCtx, false));
256✔
4019
  }
4020

4021
_exit:
1,024✔
4022

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

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

4031
  return code;
1,024✔
4032
}
4033

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

4040
  int32_t readerNum = taosArrayGetSize(pStatus->trigReaders);
26,933✔
4041
  for (int32_t i = 0; i < readerNum; ++i) {
38,621✔
4042
    SStmTaskStatus* pReader = (SStmTaskStatus*)taosArrayGet(pStatus->trigReaders, i);
28,357✔
4043
    if (pReader->id.nodeId == vgId) {
28,357✔
4044
      readerExists = true;
16,669✔
4045
      break;
16,669✔
4046
    }
4047
  }
4048

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

4061
_exit:
26,933✔
4062

4063
  if (code) {
26,933✔
4064
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4065
  }
4066

4067
  return code;
26,933✔
4068
}
4069

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

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

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

4103
  if (tbNum <= 0) {
25,861✔
4104
    mstsWarn("empty table list in origReader req, array:%p", pTbs);
×
4105
    goto _exit;
×
4106
  }
4107

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

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

4118
  pVgs = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT));
25,861✔
4119
  TSDB_CHECK_NULL(pVgs, code, lino, _exit, terrno);
25,861✔
4120
  
4121
  for (int32_t i = 0; i < tbNum; ++i) {
75,182✔
4122
    pName = (SStreamDbTableName*)taosArrayGet(pTbs, i);
49,321✔
4123
    TAOS_CHECK_EXIT(mstGetTableVgId(pDbVgroups, pName->dbFName, pName->tbName, &vgId));
49,321✔
4124
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.vgIds, &vgId), code, lino, _exit, terrno);
98,642✔
4125
    TAOS_CHECK_EXIT(tSimpleHashPut(pVgs, &vgId, sizeof(vgId), &vgId, sizeof(vgId)));
49,321✔
4126
  }
4127

4128
  int32_t vgNum = tSimpleHashGetSize(pVgs);
25,861✔
4129
  while (true) {
4130
    p = tSimpleHashIterate(pVgs, p, &iter);
52,794✔
4131
    if (NULL == p) {
52,794✔
4132
      break;
25,861✔
4133
    }
4134
    
4135
    TAOS_CHECK_EXIT(msmCheckDeployTrigReader(pCtx, pStatus, pTask, *(int32_t*)p, vgNum));
26,933✔
4136
  }
4137
  
4138
  vgNum = taosArrayGetSize(pStatus->trigOReaders);
25,861✔
4139
  rsp.cont.readerList = taosArrayInit(vgNum, sizeof(SStreamTaskAddr));
25,861✔
4140
  TSDB_CHECK_NULL(rsp.cont.readerList, code, lino, _exit, terrno);
25,861✔
4141

4142
  SStreamTaskAddr addr;
25,861✔
4143
  for (int32_t i = 0; i < vgNum; ++i) {
36,125✔
4144
    SStmTaskStatus* pOTask = taosArrayGet(pStatus->trigOReaders, i);
10,264✔
4145
    addr.taskId = pOTask->id.taskId;
10,264✔
4146
    addr.nodeId = pOTask->id.nodeId;
10,264✔
4147
    addr.epset = mndGetVgroupEpsetById(pCtx->pMnode, pOTask->id.nodeId);
10,264✔
4148
    TSDB_CHECK_NULL(taosArrayPush(rsp.cont.readerList, &addr), code, lino, _exit, terrno);
20,528✔
4149
    mstsDebug("the %dth otrigReader src added to trigger's virtual orig readerList, TASK:%" PRIx64 " nodeId:%d", i, addr.taskId, addr.nodeId);
10,264✔
4150
  }
4151

4152
  if (NULL == pCtx->pRsp->rsps.rspList) {
25,861✔
4153
    pCtx->pRsp->rsps.rspList = taosArrayInit(2, sizeof(SStreamMgmtRsp));
×
4154
    TSDB_CHECK_NULL(pCtx->pRsp->rsps.rspList, code, lino, _exit, terrno);
×
4155
  }
4156

4157
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
51,722✔
4158

4159
_exit:
25,861✔
4160

4161
  tFreeSStreamMgmtReq(pMgmtReq);
25,861✔
4162
  taosMemoryFree(pMgmtReq);
25,861✔
4163

4164
  tSimpleHashCleanup(pVgs);
25,861✔
4165

4166
  if (code) {
25,861✔
4167
    tFreeSStreamMgmtRsp(&rsp);
×
4168
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4169
  }
4170

4171
  mstDestroyDbVgroupsHash(pDbVgroups);
25,861✔
4172

4173
  return code;
25,861✔
4174
}
4175

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

4190
_exit:
4,268✔
4191

4192
  if (code) {
4,268✔
4193
    mstsError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
4194
  }
4195

4196
  return code;
4,268✔
4197
}
4198

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

4208
  int32_t readerNum = MST_LIST_SIZE(pStatus->calcReaders);
4,268✔
4209
  for (int32_t i = 0; i < readerNum; ++i, pNode = TD_DLIST_NODE_NEXT(pNode)) {
39,576✔
4210
    pReader = (SStmTaskStatus*)pNode->data;
35,308✔
4211
    if (pReader->id.nodeId == vgId && pReader->id.uid == uid) {
35,308✔
4212
      readerExists = true;
×
4213
      pAddr->taskId = pReader->id.taskId;
×
4214
      break;
×
4215
    }
4216
  }
4217

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

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

4239
  pAddr->epset = mndGetVgroupEpsetById(pCtx->pMnode, vgId);
4,268✔
4240
  pAddr->nodeId = vgId;
4,268✔
4241

4242
_exit:
4,268✔
4243

4244
  if (code) {
4,268✔
4245
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4246
  }
4247

4248
  return code;
4,268✔
4249
}
4250

4251

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

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

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

4284
  if (reqNum <= 0) {
4,268✔
4285
    mstsWarn("empty req list in origReader req, array:%p", pReqs);
×
4286
    goto _exit;
×
4287
  }
4288

4289
  rsp.cont.execRspList = taosArrayInit_s(sizeof(SStreamOReaderDeployRsp), reqNum);
4,268✔
4290
  TSDB_CHECK_NULL(rsp.cont.execRspList, code, lino, _exit, terrno);
4,268✔
4291

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

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

4312
  TSDB_CHECK_NULL(taosArrayPush(pCtx->pRsp->rsps.rspList, &rsp), code, lino, _exit, terrno);
8,536✔
4313

4314
_exit:
4,268✔
4315

4316
  tFreeSStreamMgmtReq(pMgmtReq);
4,268✔
4317
  taosMemoryFree(pMgmtReq);
4,268✔
4318

4319
  if (code) {
4,268✔
4320
    tFreeSStreamMgmtRsp(&rsp);
×
4321
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4322
  }
4323

4324
  return code;
4,268✔
4325
}
4326

4327

4328
int32_t msmHandleTaskMgmtReq(SStmGrpCtx* pCtx, SStmTaskStatusMsg* pTask) {
30,129✔
4329
  int32_t code = TSDB_CODE_SUCCESS;
30,129✔
4330
  int32_t lino = 0;
30,129✔
4331

4332
  switch (pTask->pMgmtReq->type) {
30,129✔
4333
    case STREAM_MGMT_REQ_TRIGGER_ORIGTBL_READER:
25,861✔
4334
      TAOS_CHECK_EXIT(msmDeployTriggerOrigReader(pCtx, pTask));
25,861✔
4335
      break;
25,861✔
4336
    case STREAM_MGMT_REQ_RUNNER_ORIGTBL_READER:
4,268✔
4337
      TAOS_CHECK_EXIT(msmDeployRunnerOrigReader(pCtx, pTask));
4,268✔
4338
      break;
4,268✔
4339
    default:
×
4340
      msttError("Invalid mgmtReq type:%d", pTask->pMgmtReq->type);
×
4341
      code = TSDB_CODE_MND_STREAM_INTERNAL_ERROR;
×
4342
      break;
×
4343
  }
4344

4345
_exit:
30,129✔
4346

4347
  if (code) {
30,129✔
4348
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4349
  }
4350

4351
  return code;
30,129✔
4352
}
4353

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

4374
    if (NULL == pTask->pMgmtReq) {
30,129✔
4375
      msttError("idx %d without mgmtReq", idx);
×
4376
      continue;
×
4377
    }
4378

4379
    TAOS_CHECK_EXIT(msmHandleTaskMgmtReq(pCtx, pTask));
30,129✔
4380
  }
4381

4382
_exit:
12,838✔
4383

4384
  if (code) {
12,838✔
4385
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
×
4386
  }
4387

4388
  return code;
12,838✔
4389
}
4390

4391
int32_t msmNormalHandleHbMsg(SStmGrpCtx* pCtx) {
14,785,938✔
4392
  int32_t code = TSDB_CODE_SUCCESS;
14,785,938✔
4393
  int32_t lino = 0;
14,785,938✔
4394
  SStreamHbMsg* pReq = pCtx->pReq;
14,785,938✔
4395

4396
  TAOS_CHECK_EXIT(msmCheckUpdateDnodeTs(pCtx));
14,785,938✔
4397
  if (GOT_SNODE(pReq->snodeId)) {
14,592,763✔
4398
    TAOS_CHECK_EXIT(msmUpdateSnodeUpTs(pCtx));
1,278,342✔
4399
  }
4400
  
4401
  if (atomic_load_64(&mStreamMgmt.actionQ->qRemainNum) > 0 && 0 == taosWTryLockLatch(&mStreamMgmt.actionQLock)) {
14,592,763✔
4402
    msmHandleStreamActions(pCtx);
38,148✔
4403
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
38,148✔
4404
  }
4405

4406
  if (taosArrayGetSize(pReq->pStreamReq) > 0 && mstWaitLock(&mStreamMgmt.actionQLock, false)) {
14,592,763✔
4407
    code = msmHandleStreamRequests(pCtx);
12,838✔
4408
    taosWUnLockLatch(&mStreamMgmt.actionQLock);
12,838✔
4409
    TAOS_CHECK_EXIT(code);
12,838✔
4410
  }
4411

4412
  if (atomic_load_32(&mStreamMgmt.toDeployVgTaskNum) > 0) {
14,592,763✔
4413
    TAOS_CHECK_EXIT(msmGrpAddDeployVgTasks(pCtx));
48,293✔
4414
  } else {
4415
    TAOS_CHECK_EXIT(msmUpdateVgroupsUpTs(pCtx));
14,544,470✔
4416
  }
4417

4418
  if (atomic_load_32(&mStreamMgmt.toDeploySnodeTaskNum) > 0 && GOT_SNODE(pReq->snodeId)) {
14,592,763✔
4419
    TAOS_CHECK_EXIT(msmGrpAddDeploySnodeTasks(pCtx));
46,071✔
4420
  }
4421

4422
  if (taosHashGetSize(pCtx->deployStm) > 0) {
14,592,763✔
4423
    TAOS_CHECK_EXIT(msmRspAddStreamsDeploy(pCtx));
50,053✔
4424
  }
4425

4426
  if (taosArrayGetSize(pReq->pStreamStatus) > 0) {
14,592,763✔
4427
    TAOS_CHECK_EXIT(msmNormalHandleStatusUpdate(pCtx));
1,176,196✔
4428
  }
4429

4430
  if (taosHashGetSize(pCtx->actionStm) > 0) {
14,592,763✔
4431
    TAOS_CHECK_EXIT(msmHandleHbPostActions(pCtx));
111,814✔
4432
  }
4433

4434
_exit:
14,592,763✔
4435

4436
  if (code) {
14,785,938✔
4437
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
193,175✔
4438
  }
4439

4440
  return code;
14,785,938✔
4441
}
4442

4443
void msmEncodeStreamHbRsp(int32_t code, SRpcHandleInfo *pRpcInfo, SMStreamHbRspMsg* pRsp, SRpcMsg* pMsg) {
14,985,724✔
4444
  int32_t lino = 0;
14,985,724✔
4445
  int32_t tlen = 0;
14,985,724✔
4446
  void   *buf = NULL;
14,985,724✔
4447

4448
  if (TSDB_CODE_SUCCESS != code) {
14,985,724✔
4449
    goto _exit;
193,175✔
4450
  }
4451

4452
  tEncodeSize(tEncodeStreamHbRsp, pRsp, tlen, code);
14,792,549✔
4453
  if (code < 0) {
14,792,549✔
4454
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4455
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4456
  }
4457

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

4464
  ((SStreamMsgGrpHeader *)buf)->streamGid = pRsp->streamGId;
14,792,549✔
4465
  void *abuf = POINTER_SHIFT(buf, sizeof(SStreamMsgGrpHeader));
14,792,549✔
4466

4467
  SEncoder encoder;
14,777,830✔
4468
  tEncoderInit(&encoder, abuf, tlen);
14,792,549✔
4469
  if ((code = tEncodeStreamHbRsp(&encoder, pRsp)) < 0) {
14,792,549✔
4470
    rpcFreeCont(buf);
×
4471
    buf = NULL;
×
4472
    tEncoderClear(&encoder);
×
4473
    mstError("encode stream hb msg rsp failed, code:%s", tstrerror(code));
×
4474
    TAOS_CHECK_EXIT(TSDB_CODE_MND_STREAM_INTERNAL_ERROR);    
×
4475
  }
4476
  tEncoderClear(&encoder);
14,792,549✔
4477

4478
_exit:
14,985,724✔
4479

4480
  pMsg->code = code;
14,985,724✔
4481
  pMsg->info = *pRpcInfo;
14,985,724✔
4482
  if (TSDB_CODE_SUCCESS == code) {
14,985,724✔
4483
    pMsg->contLen = tlen + sizeof(SStreamMsgGrpHeader);
14,792,549✔
4484
    pMsg->pCont = buf;
14,792,549✔
4485
  }
4486
}
14,985,724✔
4487

4488

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

4494
  (void)mstWaitLock(&mStreamMgmt.runtimeLock, true);
14,985,724✔
4495

4496
  if (0 == atomic_load_8(&mStreamMgmt.active)) {
14,985,724✔
4497
    mstWarn("mnode stream become NOT active, ignore stream hb from dnode %d streamGid %d", pHb->dnodeId, pHb->streamGId);
198,762✔
4498
    goto _exit;
198,762✔
4499
  }
4500

4501
  int32_t tidx = streamGetThreadIdx(mStreamMgmt.threadNum, pHb->streamGId);
14,786,962✔
4502
  SStmGrpCtx* pCtx = &mStreamMgmt.tCtx[tidx].grpCtx[pHb->streamGId];
14,786,962✔
4503

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

4525
_exit:
14,985,724✔
4526

4527
  msmEncodeStreamHbRsp(code, &pReq->info, &rsp, pRspMsg);
14,985,724✔
4528

4529
  msmCleanStreamGrpCtx(pHb);
14,985,724✔
4530
  msmClearStreamToDeployMaps(pHb);
14,985,724✔
4531

4532
  taosRUnLockLatch(&mStreamMgmt.runtimeLock);
14,985,724✔
4533
  
4534
  tFreeSMStreamHbRspMsg(&rsp);
14,985,724✔
4535

4536
  return code;
14,985,724✔
4537
}
4538

4539
void msmHandleBecomeLeader(SMnode *pMnode) {
406,973✔
4540
  if (tsDisableStream) {
406,973✔
4541
    return;
×
4542
  }
4543

4544
  mstInfo("start to process mnode become leader");
406,973✔
4545

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

4554
  if (TSDB_CODE_SUCCESS == code) {
406,973✔
4555
    atomic_store_8(&mStreamMgmt.active, 1);
406,973✔
4556
  }
4557

4558
  mstInfo("mnode stream mgmt active:%d", atomic_load_8(&mStreamMgmt.active));
406,973✔
4559
}
4560

4561
void msmHandleBecomeNotLeader(SMnode *pMnode) {  
599,191✔
4562
  if (tsDisableStream) {
599,191✔
4563
    return;
×
4564
  }
4565

4566
  mstInfo("start to process mnode become not leader");
599,191✔
4567

4568
  streamRemoveVnodeLeader(MNODE_HANDLE);
599,191✔
4569

4570
  if (atomic_val_compare_exchange_8(&mStreamMgmt.active, 1, 0)) {
599,191✔
4571
    taosWLockLatch(&mStreamMgmt.runtimeLock);
406,973✔
4572
    msmDestroyRuntimeInfo(pMnode);
406,973✔
4573
    mStreamMgmt.stat.inactiveTimes++;
406,973✔
4574
    taosWUnLockLatch(&mStreamMgmt.runtimeLock);
406,973✔
4575
  }
4576
}
4577

4578

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

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

4595
  if (pStream->mainSnodeId == pSnode->id) {
13,564✔
4596
    if (NULL == *ppRes) {
8,358✔
4597
      int32_t streamNum = sdbGetSize(pMnode->pSdb, SDB_STREAM);
1,479✔
4598
      *ppRes = taosArrayInit(streamNum, POINTER_BYTES);
1,479✔
4599
      TSDB_CHECK_NULL(*ppRes, code, lino, _exit, terrno);
1,479✔
4600
    }
4601

4602
    TSDB_CHECK_NULL(taosArrayPush(*ppRes, &pStream), code, lino, _exit, terrno);
16,716✔
4603
  }
4604

4605
  return true;
13,564✔
4606

4607
_exit:
×
4608

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

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

4615
  return false;
×
4616
}
4617

4618

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

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

4632
  //STREAMTODO CHECK REPLICA UPDATED OR NOT
4633

4634
_exit:
62,388✔
4635

4636
  if (code) {
62,388✔
4637
    mstError("%s failed at line %d, error:%s", __FUNCTION__, lino, tstrerror(code));
360✔
4638
  }  
4639

4640
  return code;
62,388✔
4641
}
4642

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

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

4667
  if (pStatus) {
76,472✔
4668
    if (userDropped || userStopped || MST_IS_USER_STOPPED(atomic_load_8(&pStatus->stopped))) {
73,553✔
4669
      (void)msmRemoveStreamFromMaps(pMnode, streamId);
360✔
4670
    }
4671

4672
    return true;
73,553✔
4673
  }
4674

4675
  mstPostStreamAction(mStreamMgmt.actionQ, streamId, pStream->pCreate->name, NULL, false, STREAM_ACT_DEPLOY);
2,919✔
4676

4677
  return true;
2,919✔
4678
}
4679

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

4692
    pStatus = (SStmStatus*)pIter;
263,833✔
4693

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

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

4708
    if (MST_IS_ERROR_STOPPED(stopped)) {
263,442✔
4709
      if (mStreamMgmt.hCtx.currentTs < pStatus->fatalRetryTs) {
6,582✔
4710
        mstsDebug("stream already stopped by error %s, retried times:%" PRId64 ", next time not reached, currTs:%" PRId64 ", nextRetryTs:%" PRId64,
4,388✔
4711
            tstrerror(pStatus->fatalError), pStatus->fatalRetryTimes, mStreamMgmt.hCtx.currentTs, pStatus->fatalRetryTs);
4712
            
4713
        MND_STREAM_SET_LAST_TS(STM_EVENT_STM_TERR, mStreamMgmt.hCtx.currentTs);
4,388✔
4714
        continue;
4,388✔
4715
      }
4716

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

4721
    if (MST_IS_GRANT_STOPPED(stopped) && TSDB_CODE_SUCCESS == grantCheckExpire(TSDB_GRANT_STREAMS)) {
256,860✔
4722
      mstPostStreamAction(mStreamMgmt.actionQ, *(int64_t*)taosHashGetKey(pIter, NULL), pStatus->streamName, NULL, false, STREAM_ACT_DEPLOY);
×
4723
      continue;
×
4724
    }
4725
  }
4726
}
50,288✔
4727

4728
void msmCheckStreamsStatus(SMnode *pMnode) {
361,501✔
4729
  SStmCheckStatusCtx ctx = {0};
361,501✔
4730

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

4739
  if (MST_READY_FOR_MAP_LOOP()) {
361,501✔
4740
    mstDebug("ready to check map loop, lastLoopMapTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_MAP].ts);
50,288✔
4741
    msmCheckLoopStreamMap(pMnode);
50,288✔
4742
    MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
50,288✔
4743
  }
4744
}
361,501✔
4745

4746
void msmCheckTaskListStatus(int64_t streamId, SStmTaskStatus** pList, int32_t taskNum) {
1,768,154✔
4747
  for (int32_t i = 0; i < taskNum; ++i) {
3,597,950✔
4748
    SStmTaskStatus* pTask = *(pList + i);
1,831,286✔
4749

4750
    if (atomic_load_8(&((SStmStatus*)pTask->pStream)->stopped)) {
1,831,286✔
4751
      continue;
58,258✔
4752
    }
4753
    
4754
    if (!MST_PASS_ISOLATION(pTask->lastUpTs, 1)) {
1,783,732✔
4755
      continue;
1,780,130✔
4756
    }
4757

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

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

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

4773
    SStmTaskAction task = {0};
2,112✔
4774
    task.streamId = streamId;
2,112✔
4775
    task.id = pTask->id;
2,112✔
4776
    task.flag = pTask->flags;
2,112✔
4777
    task.type = pTask->type;
2,112✔
4778
    
4779
    mstPostTaskAction(mStreamMgmt.actionQ, &task, STREAM_ACT_DEPLOY);
2,112✔
4780
  }
4781
}
1,768,154✔
4782

4783
void msmCheckVgroupStreamStatus(SHashObj* pStreams) {
149,671✔
4784
  void* pIter = NULL;
149,671✔
4785
  SStmVgStreamStatus* pVg = NULL;
149,671✔
4786
  int64_t streamId = 0;
149,671✔
4787
  
4788
  while (true) {
520,922✔
4789
    pIter = taosHashIterate(pStreams, pIter);
670,593✔
4790
    if (NULL == pIter) {
670,593✔
4791
      break;
149,671✔
4792
    }
4793

4794
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
520,922✔
4795
    pVg = (SStmVgStreamStatus*)pIter;
520,922✔
4796

4797
    int32_t taskNum = taosArrayGetSize(pVg->trigReaders);
520,922✔
4798
    if (taskNum > 0) {
520,922✔
4799
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->trigReaders, 0), taskNum);
386,229✔
4800
    }
4801

4802
    taskNum = taosArrayGetSize(pVg->calcReaders);
520,922✔
4803
    if (taskNum > 0) {
520,922✔
4804
      msmCheckTaskListStatus(streamId, taosArrayGet(pVg->calcReaders, 0), taskNum);
233,793✔
4805
    }
4806
  }
4807
}
149,671✔
4808

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

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

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

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

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

4834

4835
void msmCheckVgroupStatus(SMnode *pMnode) {
361,501✔
4836
  void* pIter = NULL;
361,501✔
4837
  int32_t code = 0;
361,501✔
4838
  
4839
  while (true) {
1,410,004✔
4840
    pIter = taosHashIterate(mStreamMgmt.vgroupMap, pIter);
1,771,505✔
4841
    if (NULL == pIter) {
1,771,505✔
4842
      break;
361,501✔
4843
    }
4844

4845
    int32_t vgId = *(int32_t*)taosHashGetKey(pIter, NULL);
1,410,004✔
4846
    if ((vgId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
1,410,004✔
4847
      continue;
1,257,663✔
4848
    }
4849
    
4850
    SStmVgroupStatus* pVg = (SStmVgroupStatus*)pIter;
152,341✔
4851

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

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

4872
    msmCheckVgroupStreamStatus(pVg->streamTasks);
149,671✔
4873
  }
4874
}
361,501✔
4875

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

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

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

4904
  (void)msmSTAddSnodesToMap(pMnode);
1,760✔
4905

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

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

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

4948
  taosHashClear(pSnode->streamTasks);
1,760✔
4949
}
1,760✔
4950

4951

4952
void msmCheckSnodeStreamStatus(SHashObj* pStreams) {
49,309✔
4953
  void* pIter = NULL;
49,309✔
4954
  SStmSnodeStreamStatus* pSnode = NULL;
49,309✔
4955
  int64_t streamId = 0;
49,309✔
4956
  
4957
  while (true) {
4958
    pIter = taosHashIterate(pStreams, pIter);
355,459✔
4959
    if (NULL == pIter) {
355,459✔
4960
      break;
49,309✔
4961
    }
4962

4963
    streamId = *(int64_t*)taosHashGetKey(pIter, NULL);
306,150✔
4964
    pSnode = (SStmSnodeStreamStatus*)pIter;
306,150✔
4965

4966
    if (NULL != pSnode->trigger) {
306,150✔
4967
      msmCheckTaskListStatus(streamId, &pSnode->trigger, 1);
287,642✔
4968
    }
4969

4970
    for (int32_t i = 0; i < MND_STREAM_RUNNER_DEPLOY_NUM; ++i) {
1,224,600✔
4971
      int32_t taskNum = taosArrayGetSize(pSnode->runners[i]);
918,450✔
4972
      if (taskNum > 0) {
918,450✔
4973
        msmCheckTaskListStatus(streamId, taosArrayGet(pSnode->runners[i], 0), taskNum);
860,490✔
4974
      }
4975
    }
4976
  }
4977
}
49,309✔
4978

4979

4980
void msmCheckSnodeStatus(SMnode *pMnode) {
361,501✔
4981
  void* pIter = NULL;
361,501✔
4982
  
4983
  while (true) {
465,305✔
4984
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
826,806✔
4985
    if (NULL == pIter) {
826,806✔
4986
      break;
361,501✔
4987
    }
4988

4989
    int32_t snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
465,305✔
4990
    if ((snodeId % MND_STREAM_ISOLATION_PERIOD_NUM) != mStreamMgmt.hCtx.slotIdx) {
465,305✔
4991
      continue;
408,722✔
4992
    }
4993

4994
    mstDebug("start to check snode %d status, currTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs);
56,583✔
4995
    
4996
    SStmSnodeStatus* pSnode = (SStmSnodeStatus*)pIter;
56,583✔
4997
    if (NULL == pSnode->streamTasks) {
56,583✔
4998
      mstDebug("ignore snode %d health check since empty tasks", snodeId);
5,866✔
4999
      continue;
5,866✔
5000
    }
5001
    
5002
    if (MST_PASS_ISOLATION(pSnode->lastUpTs, 1)) {
50,717✔
5003
      mstInfo("snode %d lost, lastUpTs:%" PRId64 ", runnerThreadNum:%d, streamNum:%d", 
1,408✔
5004
          snodeId, pSnode->lastUpTs, pSnode->runnerThreadNum, (int32_t)taosHashGetSize(pSnode->streamTasks));
5005
      
5006
      msmHandleSnodeLost(pMnode, pSnode);
1,408✔
5007
      continue;
1,408✔
5008
    }
5009
    
5010
    mstDebug("snode %d online, try to check tasks status, currTs:%" PRId64 ", lastUpTs:%" PRId64, snodeId, mStreamMgmt.hCtx.currentTs, pSnode->lastUpTs);
49,309✔
5011

5012
    msmCheckSnodeStreamStatus(pSnode->streamTasks);
49,309✔
5013
  }
5014
}
361,501✔
5015

5016

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

5020
  msmCheckVgroupStatus(pMnode);
361,501✔
5021
  msmCheckSnodeStatus(pMnode);
361,501✔
5022
}
361,501✔
5023

5024
void msmCheckSnodesState(SMnode *pMnode) {
361,501✔
5025
  if (!MST_READY_FOR_SNODE_LOOP()) {
361,501✔
5026
    return;
317,154✔
5027
  }
5028

5029
  mstDebug("ready to check snode loop, lastTs:%" PRId64, mStreamMgmt.lastTs[STM_EVENT_LOOP_SNODE].ts);
44,347✔
5030

5031
  void* pIter = NULL;
44,347✔
5032
  int32_t snodeId = 0;
44,347✔
5033
  while (true) {
49,590✔
5034
    pIter = taosHashIterate(mStreamMgmt.snodeMap, pIter);
93,937✔
5035
    if (NULL == pIter) {
93,937✔
5036
      break;
44,347✔
5037
    }
5038

5039
    snodeId = *(int32_t*)taosHashGetKey(pIter, NULL);
49,590✔
5040
    if (sdbCheckExists(pMnode->pSdb, SDB_SNODE, &snodeId)) {
49,590✔
5041
      continue;
49,238✔
5042
    }
5043

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

5057
  MND_STREAM_SET_LAST_TS(STM_EVENT_LOOP_MAP, mStreamMgmt.hCtx.currentTs);
44,347✔
5058
}
5059

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

5067
  if (sdbGetSize(pMnode->pSdb, SDB_STREAM) <= 0) {
7,825,862✔
5068
    mstTrace("ignore health check since no stream now");
7,102,860✔
5069
    return false;
7,102,860✔
5070
  }
5071

5072
  return true;
723,002✔
5073
}
5074

5075
void msmHealthCheck(SMnode *pMnode) {
7,465,129✔
5076
  if (!msmCheckNeedHealthCheck(pMnode)) {
7,465,129✔
5077
    return;
7,103,628✔
5078
  }
5079

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

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

5097
  taosWUnLockLatch(&mStreamMgmt.runtimeLock);
361,501✔
5098

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

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

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

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

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

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

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

5150
  return code;
×
5151
}
5152

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

5160
  MND_STREAM_SET_LAST_TS(STM_EVENT_ACTIVE_BEGIN, taosGetTimestampMs());
812,843✔
5161

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

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

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

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

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

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

5267
  TAOS_CHECK_EXIT(msmSTAddSnodesToMap(pMnode));
406,973✔
5268
  TAOS_CHECK_EXIT(msmSTAddDnodesToMap(pMnode));
406,973✔
5269

5270
  mStreamMgmt.lastTaskId = 1;
406,973✔
5271

5272
  int32_t activeStreamNum = 0;
406,973✔
5273
  sdbTraverse(pMnode->pSdb, SDB_STREAM, msmUpdateProfileStreams, &MND_STREAM_GET_LAST_TS(STM_EVENT_ACTIVE_BEGIN), &activeStreamNum, NULL);
406,973✔
5274

5275
  if (activeStreamNum > 0) {
406,973✔
5276
    msmSetInitRuntimeState(MND_STM_STATE_WATCH);
256✔
5277
  } else {
5278
    msmSetInitRuntimeState(MND_STM_STATE_NORMAL);
406,717✔
5279
  }
5280

5281
_exit:
406,973✔
5282

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

5290
  return code;
406,973✔
5291
}
5292

5293

5294

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