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

taosdata / TDengine / #3549

06 Dec 2024 09:44AM UTC coverage: 59.948% (+0.1%) from 59.846%
#3549

push

travis-ci

web-flow
Merge pull request #29057 from taosdata/docs/TD-33031-3.0

docs: description of user privileges

118833 of 254191 branches covered (46.75%)

Branch coverage included in aggregate %.

199893 of 277480 relevant lines covered (72.04%)

19006119.35 hits per line

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

71.86
/source/libs/stream/src/streamTask.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 "executor.h"
17
#include "osDir.h"
18
#include "osMemory.h"
19
#include "streamInt.h"
20
#include "streamsm.h"
21
#include "tmisce.h"
22
#include "tstream.h"
23
#include "ttimer.h"
24
#include "wal.h"
25
#include "streamMsg.h"
26

27
static void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo);
28
static int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated);
29
static int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdate);
30
static void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo);
31

32
static int32_t addToTaskset(SArray* pArray, SStreamTask* pTask) {
13,496✔
33
  int32_t childId = taosArrayGetSize(pArray);
13,496✔
34
  pTask->info.selfChildId = childId;
13,496✔
35
  void* p = taosArrayPush(pArray, &pTask);
13,496✔
36
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
13,496!
37
}
38

39
static int32_t doUpdateTaskEpset(SStreamTask* pTask, int32_t nodeId, SEpSet* pEpSet, bool* pUpdated) {
426✔
40
  int32_t code = 0;
426✔
41
  char    buf[512] = {0};
426✔
42

43
  if (pTask->info.nodeId == nodeId) {  // execution task should be moved away
426✔
44
    bool isEqual = isEpsetEqual(&pTask->info.epSet, pEpSet);
132✔
45
    code = epsetToStr(pEpSet, buf, tListLen(buf));
132✔
46
    if (code) { // print error and continue
132!
47
      stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
48
      return code;
×
49
    }
50

51
    if (!isEqual) {
132✔
52
      (*pUpdated) = true;
112✔
53
      char tmp[512] = {0};
112✔
54
      code = epsetToStr(&pTask->info.epSet, tmp, tListLen(tmp));  // only for log file, ignore errors
112✔
55
      if (code) { // print error and continue
112!
56
        stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
57
        return code;
×
58
      }
59

60
      epsetAssign(&pTask->info.epSet, pEpSet);
112✔
61
      stDebug("s-task:0x%x (vgId:%d) self node epset is updated %s, old:%s", pTask->id.taskId, nodeId, buf, tmp);
112!
62
    } else {
63
      stDebug("s-task:0x%x (vgId:%d) not updated task epset, since epset identical, %s", pTask->id.taskId, nodeId, buf);
20!
64
    }
65
  }
66

67
  // check for the dispatch info and the upstream task info
68
  int32_t level = pTask->info.taskLevel;
426✔
69
  if (level == TASK_LEVEL__SOURCE) {
426✔
70
    code = streamTaskUpdateDownstreamInfo(pTask, nodeId, pEpSet, pUpdated);
209✔
71
  } else if (level == TASK_LEVEL__AGG) {
217✔
72
    code = streamTaskUpdateUpstreamInfo(pTask, nodeId, pEpSet, pUpdated);
8✔
73
    code = streamTaskUpdateDownstreamInfo(pTask, nodeId, pEpSet, pUpdated);
8✔
74
  } else {  // TASK_LEVEL__SINK
75
    code = streamTaskUpdateUpstreamInfo(pTask, nodeId, pEpSet, pUpdated);
209✔
76
  }
77

78
  return code;
426✔
79
}
80

81
static void freeItem(void* p) {
×
82
  SStreamContinueExecInfo* pInfo = p;
×
83
  rpcFreeCont(pInfo->msg.pCont);
×
84
}
×
85

86
static void freeUpstreamItem(void* p) {
82,609✔
87
  SStreamUpstreamEpInfo** pInfo = p;
82,609✔
88
  taosMemoryFree(*pInfo);
82,609✔
89
}
82,614✔
90

91
static SStreamUpstreamEpInfo* createStreamTaskEpInfo(const SStreamTask* pTask) {
19,459✔
92
  SStreamUpstreamEpInfo* pEpInfo = taosMemoryMalloc(sizeof(SStreamUpstreamEpInfo));
19,459✔
93
  if (pEpInfo == NULL) {
19,459!
94
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
95
    return NULL;
×
96
  }
97

98
  pEpInfo->childId = pTask->info.selfChildId;
19,459✔
99
  pEpInfo->epSet = pTask->info.epSet;
19,459✔
100
  pEpInfo->nodeId = pTask->info.nodeId;
19,459✔
101
  pEpInfo->taskId = pTask->id.taskId;
19,459✔
102
  pEpInfo->stage = -1;
19,459✔
103

104
  return pEpInfo;
19,459✔
105
}
106

107
int32_t tNewStreamTask(int64_t streamId, int8_t taskLevel, SEpSet* pEpset, bool fillHistory, int32_t trigger,
13,496✔
108
                       int64_t triggerParam, SArray* pTaskList, bool hasFillhistory, int8_t subtableWithoutMd5,
109
                       SStreamTask** p) {
110
  *p = NULL;
13,496✔
111

112
  SStreamTask* pTask = (SStreamTask*)taosMemoryCalloc(1, sizeof(SStreamTask));
13,496✔
113
  if (pTask == NULL) {
13,496!
114
    stError("s-task:0x%" PRIx64 " failed malloc new stream task, size:%d, code:%s", streamId,
×
115
            (int32_t)sizeof(SStreamTask), tstrerror(terrno));
116
    return terrno;
×
117
  }
118

119
  pTask->ver = SSTREAM_TASK_VER;
13,496✔
120
  pTask->id.taskId = tGenIdPI32();
13,496✔
121
  pTask->id.streamId = streamId;
13,496✔
122

123
  pTask->info.taskLevel = taskLevel;
13,496✔
124
  pTask->info.fillHistory = fillHistory;
13,496✔
125
  pTask->info.trigger = trigger;
13,496✔
126
  pTask->info.delaySchedParam = triggerParam;
13,496✔
127
  pTask->subtableWithoutMd5 = subtableWithoutMd5;
13,496✔
128

129
  int32_t code = streamCreateStateMachine(pTask);
13,496✔
130
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
13,496!
131
    taosMemoryFreeClear(pTask);
×
132
    return code;
×
133
  }
134

135
  char buf[128] = {0};
13,496✔
136
  sprintf(buf, "0x%" PRIx64 "-0x%x", pTask->id.streamId, pTask->id.taskId);
13,496✔
137

138
  pTask->id.idStr = taosStrdup(buf);
13,496✔
139
  if (pTask->id.idStr == NULL) {
13,496!
140
    stError("s-task:0x%x failed to build task id, code: out of memory", pTask->id.taskId);
×
141
    return terrno;
×
142
  }
143

144
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
13,496✔
145
  pTask->status.taskStatus = fillHistory ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
13,496✔
146
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
13,496✔
147
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
13,496✔
148

149
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
13,496✔
150
  code = taosThreadMutexInit(&pTask->taskCheckInfo.checkInfoLock, NULL);
13,496✔
151
  if (code) {
13,496!
152
    return code;
×
153
  }
154

155
  if (fillHistory && !hasFillhistory) {
13,496!
156
    stError("s-task:0x%x create task failed, due to inconsistent fill-history flag", pTask->id.taskId);
×
157
    return TSDB_CODE_INVALID_PARA;
×
158
  }
159

160
  epsetAssign(&(pTask->info.mnodeEpset), pEpset);
13,496✔
161

162
  code = addToTaskset(pTaskList, pTask);
13,496✔
163
  *p = pTask;
13,496✔
164

165
  return code;
13,496✔
166
}
167

168
int32_t tDecodeStreamTaskChkInfo(SDecoder* pDecoder, SCheckpointInfo* pChkpInfo) {
770✔
169
  int64_t skip64;
170
  int8_t  skip8;
171
  int32_t skip32;
172
  int16_t skip16;
173
  SEpSet  epSet;
174

175
  if (tStartDecode(pDecoder) < 0) return -1;
770!
176
  if (tDecodeI64(pDecoder, &pChkpInfo->msgVer) < 0) return -1;
1,540!
177
  // if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
178

179
  if (tDecodeI64(pDecoder, &skip64) < 0) return -1;
770!
180
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
770!
181
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
770!
182
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
770!
183
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
770!
184
  if (tDecodeI16(pDecoder, &skip16) < 0) return -1;
770!
185

186
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
770!
187
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
770!
188

189
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
770!
190
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
770!
191
  if (tDecodeSEpSet(pDecoder, &epSet) < 0) return -1;
770!
192
  if (tDecodeSEpSet(pDecoder, &epSet) < 0) return -1;
770!
193

194
  if (tDecodeI64(pDecoder, &pChkpInfo->checkpointId) < 0) return -1;
1,540!
195
  if (tDecodeI64(pDecoder, &pChkpInfo->checkpointVer) < 0) return -1;
1,540!
196

197
  tEndDecode(pDecoder);
770✔
198
  return 0;
770✔
199
}
200

201
int32_t tDecodeStreamTaskId(SDecoder* pDecoder, STaskId* pTaskId) {
56✔
202
  int64_t ver;
203
  if (tStartDecode(pDecoder) < 0) return -1;
56!
204
  if (tDecodeI64(pDecoder, &ver) < 0) return -1;
56!
205
  if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
56!
206

207
  if (tDecodeI64(pDecoder, &pTaskId->streamId) < 0) return -1;
112!
208

209
  int32_t taskId = 0;
56✔
210
  if (tDecodeI32(pDecoder, &taskId) < 0) return -1;
56!
211

212
  pTaskId->taskId = taskId;
56✔
213
  tEndDecode(pDecoder);
56✔
214
  return 0;
56✔
215
}
216

217
void tFreeStreamTask(void* pParam) {
59,306✔
218
  char*        p = NULL;
59,306✔
219
  SStreamTask* pTask = pParam;
59,306✔
220
  int32_t      taskId = pTask->id.taskId;
59,306✔
221

222
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
59,306✔
223

224
  ETaskStatus status1 = TASK_STATUS__UNINIT;
59,306✔
225
  streamMutexLock(&pTask->lock);
59,306✔
226
  if (pTask->status.pSM != NULL) {
59,321✔
227
    SStreamTaskState status = streamTaskGetStatus(pTask);
27,709✔
228
    p = status.name;
27,701✔
229
    status1 = status.state;
27,701✔
230
  }
231
  streamMutexUnlock(&pTask->lock);
59,313✔
232

233
  stDebug("start to free s-task:0x%x %p, state:%s, refId:%" PRId64, taskId, pTask, p, pTask->id.refId);
59,312✔
234

235
  SCheckpointInfo* pCkInfo = &pTask->chkInfo;
59,312✔
236
  stDebug("s-task:0x%x task exec summary: create:%" PRId64 ", init:%" PRId64 ", start:%" PRId64
59,312✔
237
          ", updateCount:%d latestUpdate:%" PRId64 ", latestCheckPoint:%" PRId64 ", ver:%" PRId64
238
          " nextProcessVer:%" PRId64 ", checkpointCount:%d",
239
          taskId, pStatis->created, pStatis->checkTs, pStatis->readyTs, pStatis->updateCount, pStatis->latestUpdateTs,
240
          pCkInfo->checkpointId, pCkInfo->checkpointVer, pCkInfo->nextProcessVer, pStatis->checkpoint);
241

242
  if (pTask->schedInfo.pDelayTimer != NULL) {
59,312✔
243
    streamTmrStop(pTask->schedInfo.pDelayTimer);
1,219✔
244
    pTask->schedInfo.pDelayTimer = NULL;
1,219✔
245
  }
246

247
  if (pTask->hTaskInfo.pTimer != NULL) {
59,312✔
248
    streamTmrStop(pTask->hTaskInfo.pTimer);
1,837✔
249
    pTask->hTaskInfo.pTimer = NULL;
1,837✔
250
  }
251

252
  if (pTask->msgInfo.pRetryTmr != NULL) {
59,312✔
253
    streamTmrStop(pTask->msgInfo.pRetryTmr);
5,570✔
254
    pTask->msgInfo.pRetryTmr = NULL;
5,570✔
255
  }
256

257
  if (pTask->inputq.queue) {
59,312✔
258
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
14,180✔
259
    pTask->inputq.queue = NULL;
14,178✔
260
  }
261

262
  if (pTask->outputq.queue) {
59,310✔
263
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
14,176✔
264
    pTask->outputq.queue = NULL;
14,178✔
265
  }
266

267
  if (pTask->exec.qmsg) {
59,312✔
268
    taosMemoryFree(pTask->exec.qmsg);
31,178✔
269
  }
270

271
  if (pTask->exec.pExecutor) {
59,312✔
272
    qDestroyTask(pTask->exec.pExecutor);
7,104✔
273
    pTask->exec.pExecutor = NULL;
7,103✔
274
  }
275

276
  if (pTask->exec.pWalReader != NULL) {
59,311✔
277
    walCloseReader(pTask->exec.pWalReader);
7,092✔
278
    pTask->exec.pWalReader = NULL;
7,092✔
279
  }
280

281
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
59,311✔
282

283
  if (pTask->msgInfo.pData != NULL) {
59,315✔
284
    clearBufferedDispatchMsg(pTask);
52✔
285
  }
286

287
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
59,316✔
288
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
28,953✔
289
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
28,956✔
290
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
28,956✔
291
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
28,959✔
292
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
30,363✔
293
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
25,771✔
294
  }
295

296
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
59,321✔
297
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
59,324✔
298

299
  if (pTask->pNameMap) {
59,320✔
300
    tSimpleHashCleanup(pTask->pNameMap);
2,251✔
301
  }
302

303
  streamDestroyStateMachine(pTask->status.pSM);
59,320✔
304
  pTask->status.pSM = NULL;
59,321✔
305

306
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
59,321✔
307

308
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
59,320✔
309
  streamMutexDestroy(&pTask->lock);
59,323✔
310

311
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
59,321✔
312
  pTask->msgInfo.pSendInfo = NULL;
59,322✔
313
  streamMutexDestroy(&pTask->msgInfo.lock);
59,322✔
314

315
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
59,322✔
316
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
59,321✔
317

318
  if (pTask->id.idStr != NULL) {
59,321✔
319
    taosMemoryFree((void*)pTask->id.idStr);
27,673✔
320
  }
321

322
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
59,324✔
323
  pTask->chkInfo.pActiveInfo = NULL;
59,322✔
324

325
  taosMemoryFree(pTask);
59,322✔
326
  stDebug("s-task:0x%x free task completed", taskId);
59,319✔
327
}
59,321✔
328

329
void streamFreeTaskState(SStreamTask* pTask, int8_t remove) {
59,320✔
330
  stDebug("s-task:0x%x start to free task state/backend", pTask->id.taskId);
59,320✔
331
  if (pTask->pState != NULL) {
59,318✔
332
    stDebug("s-task:0x%x start to free task state", pTask->id.taskId);
7,104✔
333
    streamStateClose(pTask->pState, remove);
7,104✔
334

335
    if (remove) taskDbSetClearFileFlag(pTask->pBackend);
7,104✔
336
    taskDbRemoveRef(pTask->pBackend);
7,104✔
337
    pTask->pBackend = NULL;
7,104✔
338
    pTask->pState = NULL;
7,104✔
339
  } else {
340
    stDebug("s-task:0x%x task state is NULL, may del backend:%s", pTask->id.taskId,
52,214✔
341
            pTask->backendPath ? pTask->backendPath : "NULL");
342
    if (remove) {
52,214✔
343
      if (pTask->backendPath != NULL) {
3,403!
344
        stDebug("s-task:0x%x task state is NULL, do del backend:%s", pTask->id.taskId, pTask->backendPath);
3,406✔
345
        taosRemoveDir(pTask->backendPath);
3,406✔
346
      }
347
    }
348
  }
349

350
  if (pTask->backendPath != NULL) {
59,314✔
351
    taosMemoryFree(pTask->backendPath);
14,176✔
352
    pTask->backendPath = NULL;
14,180✔
353
  }
354
}
59,318✔
355

356
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
14,421✔
357
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,421✔
358
  SDataRange*      pRange = &pTask->dataRange;
14,421✔
359

360
  // only set the version info for stream tasks without fill-history task
361
  if ((pTask->info.fillHistory == 0) && (!HAS_RELATED_FILLHISTORY_TASK(pTask))) {
14,421✔
362
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
4,211✔
363
    pChkInfo->processedVer = ver - 1;   // already processed version
4,211✔
364
    pChkInfo->nextProcessVer = ver;     // next processed version
4,211✔
365

366
    pRange->range.maxVer = ver;
4,211✔
367
    pRange->range.minVer = ver;
4,211✔
368
  } else {
369
    // the initial value of processedVer/nextProcessVer/checkpointVer for stream task with related fill-history task
370
    // is set at the mnode.
371
    if (pTask->info.fillHistory == 1) {
10,210✔
372
      pChkInfo->checkpointVer = pRange->range.maxVer;
5,168✔
373
      pChkInfo->processedVer = pRange->range.maxVer;
5,168✔
374
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
5,168✔
375
    } else {
376
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
5,042✔
377
      pChkInfo->processedVer = pRange->range.minVer - 1;
5,042✔
378
      pChkInfo->nextProcessVer = pRange->range.minVer;
5,042✔
379

380
      {  // for compatible purpose, remove it later
381
        if (pRange->range.minVer == 0) {
5,042✔
382
          pChkInfo->checkpointVer = 0;
2,549✔
383
          pChkInfo->processedVer = 0;
2,549✔
384
          pChkInfo->nextProcessVer = 1;
2,549✔
385
          stDebug("s-task:%s update the processedVer to 0 from -1 due to compatible purpose", pTask->id.idStr);
2,549✔
386
        }
387
      }
388
    }
389
  }
390
}
14,421✔
391

392
int32_t streamTaskSetBackendPath(SStreamTask* pTask) {
14,433✔
393
  int64_t streamId = 0;
14,433✔
394
  int32_t taskId = 0;
14,433✔
395

396
  if (pTask->info.fillHistory) {
14,433✔
397
    streamId = pTask->streamTaskId.streamId;
5,168✔
398
    taskId = pTask->streamTaskId.taskId;
5,168✔
399
  } else {
400
    streamId = pTask->id.streamId;
9,265✔
401
    taskId = pTask->id.taskId;
9,265✔
402
  }
403

404
  char    id[128] = {0};
14,433✔
405
  int32_t nBytes = sprintf(id, "0x%" PRIx64 "-0x%x", streamId, taskId);
14,433✔
406
  if (nBytes < 0 || nBytes >= sizeof(id)) {
14,433!
407
    return TSDB_CODE_OUT_OF_BUFFER;
×
408
  }
409

410
  int32_t len = strlen(pTask->pMeta->path);
14,433✔
411
  pTask->backendPath = (char*)taosMemoryMalloc(len + nBytes + 2);
14,433✔
412
  if (pTask->backendPath == NULL) {
14,433!
413
    return terrno;
×
414
  }
415

416
  (void)sprintf(pTask->backendPath, "%s%s%s", pTask->pMeta->path, TD_DIRSEP, id);
14,433✔
417
  stDebug("s-task:%s set backend path:%s", pTask->id.idStr, pTask->backendPath);
14,433✔
418

419
  return 0;
14,431✔
420
}
421

422
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
14,424✔
423
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
14,424✔
424
  if (code) {
14,420!
425
    stError("0x%x failed create stream task id str, code:%s", pTask->id.taskId, tstrerror(code));
×
426
    return code;
×
427
  }
428

429
  pTask->id.refId = 0;
14,420✔
430
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,420✔
431
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,420✔
432

433
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
14,420✔
434
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
14,430✔
435
  if (code1 || code2) {
14,430!
436
    stError("s-task:%s failed to prepare the input/output queue, initialize task failed", pTask->id.idStr);
1!
437
    return TSDB_CODE_OUT_OF_MEMORY;
×
438
  }
439

440
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
14,429✔
441

442
  code = streamCreateStateMachine(pTask);
14,429✔
443
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
14,424!
444
    stError("s-task:%s failed create state-machine for stream task, initialization failed, code:%s", pTask->id.idStr,
×
445
            tstrerror(code));
446
    return code;
×
447
  }
448

449
  pTask->execInfo.created = taosGetTimestampMs();
14,431✔
450
  setInitialVersionInfo(pTask, ver);
14,431✔
451

452
  pTask->pMeta = pMeta;
14,429✔
453
  pTask->pMsgCb = pMsgCb;
14,429✔
454
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
14,429✔
455
  if (pTask->msgInfo.pSendInfo == NULL) {
14,429!
456
    stError("s-task:%s failed to create sendInfo struct for stream task, code:Out of memory", pTask->id.idStr);
×
457
    return terrno;
×
458
  }
459

460
  code = taosThreadMutexInit(&pTask->msgInfo.lock, NULL);
14,429✔
461
  if (code) {
14,428!
462
    stError("s-task:0x%x failed to init msgInfo mutex, code:%s", pTask->id.taskId, tstrerror(code));
×
463
    return code;
×
464
  }
465

466
  TdThreadMutexAttr attr = {0};
14,428✔
467
  code = taosThreadMutexAttrInit(&attr);
14,428✔
468
  if (code != 0) {
14,424!
469
    stError("s-task:%s initElapsed mutex attr failed, code:%s", pTask->id.idStr, tstrerror(code));
×
470
    return code;
×
471
  }
472

473
  code = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
14,424✔
474
  if (code != 0) {
14,421!
475
    stError("s-task:%s set mutex attr recursive, code:%s", pTask->id.idStr, tstrerror(code));
×
476
    return code;
×
477
  }
478

479
  code = taosThreadMutexInit(&pTask->lock, &attr);
14,421✔
480
  if (code) {
14,416!
481
    return code;
×
482
  }
483

484
  code = taosThreadMutexAttrDestroy(&attr);
14,416✔
485
  if (code) {
14,416!
486
    return code;
×
487
  }
488

489
  streamTaskOpenAllUpstreamInput(pTask);
14,416✔
490

491
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,422✔
492
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
14,422✔
493
  if (pOutputInfo->pTokenBucket == NULL) {
14,429!
494
    stError("s-task:%s failed to prepare the tokenBucket, code:%s", pTask->id.idStr, tstrerror(terrno));
×
495
    return terrno;
×
496
  }
497

498
  // 2MiB per second for sink task
499
  // 50 times sink operator per second
500
  code = streamTaskInitTokenBucket(pOutputInfo->pTokenBucket, 35, 35, tsSinkDataRate, pTask->id.idStr);
14,429✔
501
  if (code) {
14,426!
502
    return code;
×
503
  }
504

505
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
14,426✔
506
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
14,422!
507
    stError("s-task:%s failed to prepare downstreamUpdateList, code:%s", pTask->id.idStr, tstrerror(terrno));
×
508
    return terrno;
×
509
  }
510

511
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
14,422✔
512
  if (pTask->taskCheckInfo.pList == NULL) {
14,432!
513
    stError("s-task:%s failed to prepare taskCheckInfo list, code:%s", pTask->id.idStr, tstrerror(terrno));
×
514
    return terrno;
×
515
  }
516

517
  if (pTask->chkInfo.pActiveInfo == NULL) {
14,432!
518
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
14,432✔
519
    if (code) {
14,433!
520
      stError("s-task:%s failed to create active checkpoint info, code:%s", pTask->id.idStr, tstrerror(code));
×
521
      return code;
×
522
    }
523
  }
524

525
  return streamTaskSetBackendPath(pTask);
14,433✔
526
}
527

528
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
122,551✔
529
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
122,551✔
530
    return 0;
6,634✔
531
  }
532

533
  int32_t type = pTask->outputInfo.type;
115,917✔
534
  if (type == TASK_OUTPUT__TABLE) {
115,917✔
535
    return 0;
185✔
536
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
115,732✔
537
    return 1;
12,907✔
538
  } else {
539
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
102,825✔
540
    return taosArrayGetSize(vgInfo);
102,825✔
541
  }
542
}
543

544
int32_t streamTaskGetNumOfUpstream(const SStreamTask* pTask) { return taosArrayGetSize(pTask->upstreamInfo.pList); }
20,135✔
545

546
int32_t streamTaskSetUpstreamInfo(SStreamTask* pTask, const SStreamTask* pUpstreamTask) {
19,459✔
547
  SStreamUpstreamEpInfo* pEpInfo = createStreamTaskEpInfo(pUpstreamTask);
19,459✔
548
  if (pEpInfo == NULL) {
19,459!
549
    return terrno;
×
550
  }
551

552
  if (pTask->upstreamInfo.pList == NULL) {
19,459✔
553
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
6,736✔
554
  }
555

556
  void* p = taosArrayPush(pTask->upstreamInfo.pList, &pEpInfo);
19,459✔
557
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
19,459!
558
}
559

560
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
217✔
561
  int32_t code = 0;
217✔
562
  char    buf[512] = {0};
217✔
563
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
217✔
564
  if (code != 0) {  // print error and continue
217!
565
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
566
    return code;
×
567
  }
568

569
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
217✔
570
  for (int32_t i = 0; i < numOfUpstream; ++i) {
435✔
571
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
373✔
572
    if (pInfo->nodeId == nodeId) {
373✔
573
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
155✔
574
      if (!equal) {
155✔
575
        *pUpdated = true;
114✔
576

577
        char tmp[512] = {0};
114✔
578
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
114✔
579
        if (code != 0) {  // print error and continue
114!
580
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
581
          return code;
×
582
        }
583

584
        epsetAssign(&pInfo->epSet, pEpSet);
114✔
585
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
114!
586
                pInfo->taskId, nodeId, buf, tmp);
587
      } else {
588
        stDebug("s-task:0x%x not update upstreamInfo, since identical, task:0x%x(nodeId:%d) epset:%s", pTask->id.taskId,
41!
589
                pInfo->taskId, nodeId, buf);
590
      }
591

592
      break;
155✔
593
    }
594
  }
595

596
  return code;
217✔
597
}
598

599
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
59,311✔
600
  if (pUpstreamInfo->pList != NULL) {
59,311✔
601
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
52,520✔
602
    pUpstreamInfo->numOfClosed = 0;
52,527✔
603
    pUpstreamInfo->pList = NULL;
52,527✔
604
  }
605
}
59,318✔
606

607
void streamTaskSetFixedDownstreamInfo(SStreamTask* pTask, const SStreamTask* pDownstreamTask) {
938✔
608
  STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
938✔
609
  pDispatcher->taskId = pDownstreamTask->id.taskId;
938✔
610
  pDispatcher->nodeId = pDownstreamTask->info.nodeId;
938✔
611
  pDispatcher->epSet = pDownstreamTask->info.epSet;
938✔
612

613
  pTask->outputInfo.type = TASK_OUTPUT__FIXED_DISPATCH;
938✔
614
  pTask->msgInfo.msgType = TDMT_STREAM_TASK_DISPATCH;
938✔
615
}
938✔
616

617
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
217✔
618
  char    buf[512] = {0};
217✔
619
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
217✔
620
  if (code != 0) {                                        // print error and continue
217!
621
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
622
    return code;
×
623
  }
624

625
  int32_t id = pTask->id.taskId;
217✔
626
  int8_t  type = pTask->outputInfo.type;
217✔
627

628
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
217✔
629
    SArray* pVgs = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
153✔
630

631
    for (int32_t i = 0; i < taosArrayGetSize(pVgs); i++) {
311✔
632
      SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
309✔
633
      if (pVgInfo == NULL) {
309!
634
        continue;
×
635
      }
636

637
      if (pVgInfo->vgId == nodeId) {
309✔
638
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
151✔
639
        if (!isEqual) {
151✔
640
          *pUpdated = true;
114✔
641

642
          char tmp[512] = {0};
114✔
643
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
114✔
644
          if (code != 0) {  // print error and continue
114!
645
            stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
646
            return code;
×
647
          }
648

649
          epsetAssign(&pVgInfo->epSet, pEpSet);
114✔
650
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
114!
651
                  nodeId, buf, tmp);
652
        } else {
653
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
37!
654
                  pVgInfo->taskId, nodeId, buf);
655
        }
656
        break;
151✔
657
      }
658
    }
659
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
64!
660
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
64✔
661
    if (pDispatcher->nodeId == nodeId) {
64✔
662
      bool equal = isEpsetEqual(&pDispatcher->epSet, pEpSet);
4✔
663
      if (!equal) {
4!
664
        *pUpdated = true;
×
665

666
        char tmp[512] = {0};
×
667
        code = epsetToStr(&pDispatcher->epSet, tmp, tListLen(tmp));
×
668
        if (code != 0) {  // print error and continue
×
669
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
670
          return code;
×
671
        }
672

673
        epsetAssign(&pDispatcher->epSet, pEpSet);
×
674
        stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pDispatcher->taskId,
×
675
                nodeId, buf, tmp);
676
      } else {
677
        stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
4!
678
                pDispatcher->taskId, nodeId, buf);
679
      }
680
    }
681
  }
682

683
  return code;
217✔
684
}
685

686
int32_t streamTaskStop(SStreamTask* pTask) {
2,698✔
687
  int32_t     vgId = pTask->pMeta->vgId;
2,698✔
688
  int64_t     st = taosGetTimestampMs();
2,698✔
689
  const char* id = pTask->id.idStr;
2,698✔
690

691
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,698✔
692
  if (code) {
2,698!
693
    stError("failed to handle STOP event, s-task:%s, code:%s", id, tstrerror(code));
×
694
    return code;
×
695
  }
696

697
  if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
2,698✔
698
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
1,322✔
699
    if (code != TSDB_CODE_SUCCESS) {
1,322!
700
      stError("s-task:%s failed to kill task related query handle, code:%s", id, tstrerror(code));
×
701
    }
702
  }
703

704
  while (!streamTaskIsIdle(pTask)) {
2,698!
705
    stDebug("s-task:%s level:%d wait for task to be idle and then close, check again in 100ms", id,
×
706
            pTask->info.taskLevel);
707
    taosMsleep(100);
×
708
  }
709

710
  int64_t el = taosGetTimestampMs() - st;
2,697✔
711
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,697✔
712
  return code;
2,698✔
713
}
714

715
bool streamTaskUpdateEpsetInfo(SStreamTask* pTask, SArray* pNodeList) {
208✔
716
  STaskExecStatisInfo* p = &pTask->execInfo;
208✔
717

718
  int32_t numOfNodes = taosArrayGetSize(pNodeList);
208✔
719
  int64_t prevTs = p->latestUpdateTs;
208✔
720

721
  p->latestUpdateTs = taosGetTimestampMs();
208✔
722
  p->updateCount += 1;
208✔
723
  stDebug("s-task:0x%x update task nodeEp epset, updatedNodes:%d, updateCount:%d, prevTs:%" PRId64, pTask->id.taskId,
208!
724
          numOfNodes, p->updateCount, prevTs);
725

726
  bool updated = false;
208✔
727
  for (int32_t i = 0; i < numOfNodes; ++i) {
634✔
728
    SNodeUpdateInfo* pInfo = taosArrayGet(pNodeList, i);
426✔
729
    if (pInfo == NULL) {
426!
730
      continue;
×
731
    }
732

733
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
426✔
734
    if (code) {
426!
735
      stError("s-task:0x%x failed to update the task nodeEp epset, code:%s", pTask->id.taskId, tstrerror(code));
×
736
    }
737
  }
738

739
  return updated;
208✔
740
}
741

742
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
14,426✔
743
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,426✔
744
    return;
7,217✔
745
  }
746

747
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
7,209✔
748
  for (int32_t i = 0; i < size; ++i) {
27,774✔
749
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
20,562✔
750
    pInfo->stage = -1;
20,561✔
751
  }
752

753
  stDebug("s-task:%s reset all upstream tasks stage info", pTask->id.idStr);
7,212✔
754
}
755

756
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
24,423✔
757
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
24,423✔
758
  if (num == 0) {
24,430✔
759
    return;
12,178✔
760
  }
761

762
  for (int32_t i = 0; i < num; ++i) {
47,052✔
763
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
34,818✔
764
    pInfo->dataAllowed = true;
34,800✔
765
  }
766

767
  pTask->upstreamInfo.numOfClosed = 0;
12,234✔
768
  stDebug("s-task:%s opening up inputQ for %d upstream tasks", pTask->id.idStr, num);
12,234✔
769
}
770

771
void streamTaskCloseUpstreamInput(SStreamTask* pTask, int32_t taskId) {
8,498✔
772
  SStreamUpstreamEpInfo* pInfo = NULL;
8,498✔
773
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
8,498✔
774

775
  if ((pInfo != NULL) && pInfo->dataAllowed) {
8,484!
776
    pInfo->dataAllowed = false;
8,486✔
777
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
8,486!
778
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
8,480✔
779
    } else {
780
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
781
    }
782
  }
783
}
8,528✔
784

785
void streamTaskOpenUpstreamInput(SStreamTask* pTask, int32_t taskId) {
×
786
  SStreamUpstreamEpInfo* pInfo = NULL;
×
787
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
×
788

789
  if (pInfo != NULL && (!pInfo->dataAllowed)) {
×
790
    int32_t t = atomic_sub_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
×
791
    stDebug("s-task:%s open inputQ for upstream:0x%x, remain closed:%d", pTask->id.idStr, taskId, t);
×
792
    pInfo->dataAllowed = true;
×
793
  }
794
}
×
795

796
bool streamTaskIsAllUpstreamClosed(SStreamTask* pTask) {
×
797
  return pTask->upstreamInfo.numOfClosed == taosArrayGetSize(pTask->upstreamInfo.pList);
×
798
}
799

800
bool streamTaskSetSchedStatusWait(SStreamTask* pTask) {
116,059✔
801
  bool ret = false;
116,059✔
802

803
  streamMutexLock(&pTask->lock);
116,059✔
804
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
116,085✔
805
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
80,490✔
806
    ret = true;
80,490✔
807
  }
808

809
  streamMutexUnlock(&pTask->lock);
116,085✔
810
  return ret;
116,090✔
811
}
812

813
int8_t streamTaskSetSchedStatusActive(SStreamTask* pTask) {
78,452✔
814
  streamMutexLock(&pTask->lock);
78,452✔
815
  int8_t status = pTask->status.schedStatus;
78,560✔
816
  if (status == TASK_SCHED_STATUS__WAITING) {
78,560✔
817
    pTask->status.schedStatus = TASK_SCHED_STATUS__ACTIVE;
78,534✔
818
  }
819
  streamMutexUnlock(&pTask->lock);
78,560✔
820

821
  return status;
78,579✔
822
}
823

824
int8_t streamTaskSetSchedStatusInactive(SStreamTask* pTask) {
1,833✔
825
  streamMutexLock(&pTask->lock);
1,833✔
826
  int8_t status = pTask->status.schedStatus;
1,833✔
827
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
1,833✔
828
  streamMutexUnlock(&pTask->lock);
1,833✔
829

830
  return status;
1,833✔
831
}
832

833
int32_t streamTaskClearHTaskAttr(SStreamTask* pTask, int32_t resetRelHalt) {
6,957✔
834
  int32_t      code = 0;
6,957✔
835
  SStreamMeta* pMeta = pTask->pMeta;
6,957✔
836
  SStreamTask* pStreamTask = NULL;
6,957✔
837

838
  if (pTask->info.fillHistory == 0) {
6,957!
839
    return code;
6,962✔
840
  }
841

842
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
843
  if (code == 0) {
5!
844
    stDebug("s-task:%s clear the related stream task:0x%x attr to fill-history task", pTask->id.idStr,
×
845
            (int32_t)pTask->streamTaskId.taskId);
846

847
    streamMutexLock(&(pStreamTask->lock));
×
848
    CLEAR_RELATED_FILLHISTORY_TASK(pStreamTask);
×
849

850
    if (resetRelHalt) {
×
851
      stDebug("s-task:0x%" PRIx64 " set the persistent status attr to be ready, prev:%s, status in sm:%s",
×
852
              pTask->streamTaskId.taskId, streamTaskGetStatusStr(pStreamTask->status.taskStatus),
853
              streamTaskGetStatus(pStreamTask).name);
854
      pStreamTask->status.taskStatus = TASK_STATUS__READY;
×
855
    }
856

857
    code = streamMetaSaveTask(pMeta, pStreamTask);
×
858
    streamMutexUnlock(&(pStreamTask->lock));
×
859

860
    streamMetaReleaseTask(pMeta, pStreamTask);
×
861
  }
862

863
  return code;
5✔
864
}
865

866
int32_t streamBuildAndSendDropTaskMsg(SMsgCb* pMsgCb, int32_t vgId, SStreamTaskId* pTaskId, int64_t resetRelHalt) {
5✔
867
  SVDropStreamTaskReq* pReq = rpcMallocCont(sizeof(SVDropStreamTaskReq));
5✔
868
  if (pReq == NULL) {
5!
869
    return terrno;
×
870
  }
871

872
  pReq->head.vgId = vgId;
5✔
873
  pReq->taskId = pTaskId->taskId;
5✔
874
  pReq->streamId = pTaskId->streamId;
5✔
875
  pReq->resetRelHalt = resetRelHalt;
5✔
876

877
  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_DROP, .pCont = pReq, .contLen = sizeof(SVDropStreamTaskReq)};
5✔
878
  int32_t code = tmsgPutToQueue(pMsgCb, WRITE_QUEUE, &msg);
5✔
879
  if (code != TSDB_CODE_SUCCESS) {
5!
880
    stError("vgId:%d failed to send drop task:0x%x msg, code:%s", vgId, pTaskId->taskId, tstrerror(code));
×
881
  } else {
882
    stDebug("vgId:%d build and send drop task:0x%x msg", vgId, pTaskId->taskId);
5!
883
  }
884

885
  return code;
5✔
886
}
887

888
int32_t streamSendChkptReportMsg(SStreamTask* pTask, SCheckpointInfo* pCheckpointInfo, int8_t dropRelHTask) {
6,380✔
889
  int32_t                code = 0;
6,380✔
890
  int32_t                tlen = 0;
6,380✔
891
  int32_t                vgId = pTask->pMeta->vgId;
6,380✔
892
  const char*            id = pTask->id.idStr;
6,380✔
893
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
6,380✔
894

895
  SCheckpointReport req = {.streamId = pTask->id.streamId,
6,380✔
896
                           .taskId = pTask->id.taskId,
6,380✔
897
                           .nodeId = vgId,
898
                           .dropHTask = dropRelHTask,
899
                           .transId = pActive->transId,
6,380✔
900
                           .checkpointId = pActive->activeId,
6,380✔
901
                           .checkpointVer = pCheckpointInfo->processedVer,
6,380✔
902
                           .checkpointTs = pCheckpointInfo->startTs};
6,380✔
903

904
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
6,380!
905
  if (code < 0) {
6,378!
906
    stError("s-task:%s vgId:%d encode stream task checkpoint-report failed, code:%s", id, vgId, tstrerror(code));
×
907
    return -1;
×
908
  }
909

910
  void* buf = rpcMallocCont(tlen);
6,378✔
911
  if (buf == NULL) {
6,377!
912
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId,
×
913
            tstrerror(TSDB_CODE_OUT_OF_MEMORY));
914
    return -1;
×
915
  }
916

917
  SEncoder encoder;
918
  tEncoderInit(&encoder, buf, tlen);
6,377✔
919
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
6,378!
920
    rpcFreeCont(buf);
×
921
    tEncoderClear(&encoder);
×
922
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId, tstrerror(code));
×
923
    return -1;
×
924
  }
925
  tEncoderClear(&encoder);
6,379✔
926

927
  SRpcMsg msg = {0};
6,378✔
928
  initRpcMsg(&msg, TDMT_MND_STREAM_CHKPT_REPORT, buf, tlen);
6,378✔
929
  stDebug("s-task:%s vgId:%d build and send task checkpoint-report to mnode", id, vgId);
6,376✔
930

931
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
6,376✔
932
}
933

934
STaskId streamTaskGetTaskId(const SStreamTask* pTask) {
69,110✔
935
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
69,110✔
936
  return id;
69,110✔
937
}
938

939
void streamTaskInitForLaunchHTask(SHistoryTaskInfo* pInfo) {
1,882✔
940
  pInfo->waitInterval = LAUNCH_HTASK_INTERVAL;
1,882✔
941
  pInfo->tickCount = ceil(LAUNCH_HTASK_INTERVAL / WAIT_FOR_MINIMAL_INTERVAL);
1,882✔
942
  pInfo->retryTimes = 0;
1,882✔
943
}
1,882✔
944

945
void streamTaskSetRetryInfoForLaunch(SHistoryTaskInfo* pInfo) {
1,877✔
946
  pInfo->waitInterval *= RETRY_LAUNCH_INTERVAL_INC_RATE;
1,877✔
947
  pInfo->tickCount = ceil(pInfo->waitInterval / WAIT_FOR_MINIMAL_INTERVAL);
1,877✔
948
  pInfo->retryTimes += 1;
1,877✔
949
}
1,877✔
950

951
void streamTaskStatusInit(STaskStatusEntry* pEntry, const SStreamTask* pTask) {
8,693✔
952
  pEntry->id.streamId = pTask->id.streamId;
8,693✔
953
  pEntry->id.taskId = pTask->id.taskId;
8,693✔
954
  pEntry->stage = -1;
8,693✔
955
  pEntry->nodeId = pTask->info.nodeId;
8,693✔
956
  pEntry->status = TASK_STATUS__STOP;
8,693✔
957
}
8,693✔
958

959
void streamTaskStatusCopy(STaskStatusEntry* pDst, const STaskStatusEntry* pSrc) {
52,580✔
960
  pDst->stage = pSrc->stage;
52,580✔
961
  pDst->inputQUsed = pSrc->inputQUsed;
52,580✔
962
  pDst->inputRate = pSrc->inputRate;
52,580✔
963
  pDst->procsTotal = pSrc->procsTotal;
52,580✔
964
  pDst->procsThroughput = pSrc->procsThroughput;
52,580✔
965
  pDst->outputTotal = pSrc->outputTotal;
52,580✔
966
  pDst->outputThroughput = pSrc->outputThroughput;
52,580✔
967
  pDst->processedVer = pSrc->processedVer;
52,580✔
968
  pDst->verRange = pSrc->verRange;
52,580✔
969
  pDst->sinkQuota = pSrc->sinkQuota;
52,580✔
970
  pDst->sinkDataSize = pSrc->sinkDataSize;
52,580✔
971
  pDst->checkpointInfo = pSrc->checkpointInfo;
52,580✔
972
  pDst->startCheckpointId = pSrc->startCheckpointId;
52,580✔
973
  pDst->startCheckpointVer = pSrc->startCheckpointVer;
52,580✔
974
  pDst->status = pSrc->status;
52,580✔
975

976
  pDst->startTime = pSrc->startTime;
52,580✔
977
  pDst->hTaskId = pSrc->hTaskId;
52,580✔
978
}
52,580✔
979

980
STaskStatusEntry streamTaskGetStatusEntry(SStreamTask* pTask) {
53,376✔
981
  SStreamMeta*         pMeta = pTask->pMeta;
53,376✔
982
  STaskExecStatisInfo* pExecInfo = &pTask->execInfo;
53,376✔
983

984
  STaskStatusEntry entry = {
160,128✔
985
      .id = streamTaskGetTaskId(pTask),
53,376✔
986
      .status = streamTaskGetStatus(pTask).state,
53,376✔
987
      .nodeId = pMeta->vgId,
53,376✔
988
      .stage = pMeta->stage,
53,376✔
989

990
      .inputQUsed = SIZE_IN_MiB(streamQueueGetItemSize(pTask->inputq.queue)),
53,376✔
991
      .startTime = pExecInfo->readyTs,
53,376✔
992
      .checkpointInfo.latestId = pTask->chkInfo.checkpointId,
53,376✔
993
      .checkpointInfo.latestVer = pTask->chkInfo.checkpointVer,
53,376✔
994
      .checkpointInfo.latestTime = pTask->chkInfo.checkpointTime,
53,376✔
995
      .checkpointInfo.latestSize = 0,
996
      .checkpointInfo.remoteBackup = 0,
997
      .checkpointInfo.consensusChkptId = 0,
998
      .checkpointInfo.consensusTs = 0,
999
      .hTaskId = pTask->hTaskInfo.id.taskId,
53,376✔
1000
      .procsTotal = SIZE_IN_MiB(pExecInfo->inputDataSize),
53,376✔
1001
      .outputTotal = SIZE_IN_MiB(pExecInfo->outputDataSize),
53,376✔
1002
      .procsThroughput = SIZE_IN_KiB(pExecInfo->procsThroughput),
53,376✔
1003
      .outputThroughput = SIZE_IN_KiB(pExecInfo->outputThroughput),
53,376✔
1004
      .startCheckpointId = pExecInfo->startCheckpointId,
53,376✔
1005
      .startCheckpointVer = pExecInfo->startCheckpointVer,
53,376✔
1006
  };
1007
  return entry;
53,376✔
1008
}
1009

1010
static int32_t taskPauseCallback(SStreamTask* pTask, void* param) {
1,420✔
1011
  SStreamMeta* pMeta = pTask->pMeta;
1,420✔
1012
  int32_t      code = 0;
1,420✔
1013

1014
  int32_t num = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
1,420✔
1015
  stInfo("vgId:%d s-task:%s pause stream task. paused task num:%d", pMeta->vgId, pTask->id.idStr, num);
1,426!
1016

1017
  // in case of fill-history task, stop the tsdb file scan operation.
1018
  if (pTask->info.fillHistory == 1) {
1,427✔
1019
    void* pExecutor = pTask->exec.pExecutor;
36✔
1020
    code = qKillTask(pExecutor, TSDB_CODE_SUCCESS);
36✔
1021
  }
1022

1023
  stDebug("vgId:%d s-task:%s set pause flag and pause task", pMeta->vgId, pTask->id.idStr);
1,427✔
1024
  return code;
1,427✔
1025
}
1026

1027
void streamTaskPause(SStreamTask* pTask) {
1,431✔
1028
  int32_t code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_PAUSE, taskPauseCallback, NULL);
1,431✔
1029
  if (code) {
1,438!
1030
    stError("s-task:%s failed handle pause event async, code:%s", pTask->id.idStr, tstrerror(code));
×
1031
  }
1032
}
1,438✔
1033

1034
void streamTaskResume(SStreamTask* pTask) {
1,392✔
1035
  SStreamTaskState prevState = streamTaskGetStatus(pTask);
1,392✔
1036

1037
  SStreamMeta* pMeta = pTask->pMeta;
1,397✔
1038
  int32_t      code = streamTaskRestoreStatus(pTask);
1,397✔
1039
  if (code == TSDB_CODE_SUCCESS) {
1,400✔
1040
    char*   pNew = streamTaskGetStatus(pTask).name;
1,388✔
1041
    int32_t num = atomic_sub_fetch_32(&pMeta->numOfPausedTasks, 1);
1,388✔
1042
    stInfo("s-task:%s status:%s resume from %s, paused task(s):%d", pTask->id.idStr, pNew, prevState.name, num);
1,391!
1043
  } else {
1044
    stInfo("s-task:%s status:%s no need to resume, paused task(s):%d", pTask->id.idStr, prevState.name,
12!
1045
           pMeta->numOfPausedTasks);
1046
  }
1047
}
1,404✔
1048

1049
bool streamTaskIsSinkTask(const SStreamTask* pTask) { return pTask->info.taskLevel == TASK_LEVEL__SINK; }
92,879✔
1050

1051
// this task must success
1052
int32_t streamTaskSendCheckpointReq(SStreamTask* pTask) {
4,709✔
1053
  int32_t     code;
1054
  int32_t     tlen = 0;
4,709✔
1055
  int32_t     vgId = pTask->pMeta->vgId;
4,709✔
1056
  const char* id = pTask->id.idStr;
4,709✔
1057

1058
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,709✔
1059
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,709!
1060
  if (code < 0) {
4,715!
1061
    stError("s-task:%s vgId:%d encode stream task req checkpoint failed, code:%s", id, vgId, tstrerror(code));
×
1062
    return TSDB_CODE_INVALID_MSG;
×
1063
  }
1064

1065
  void* buf = rpcMallocCont(tlen);
4,715✔
1066
  if (buf == NULL) {
4,717!
1067
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:Out of memory", id, vgId);
×
1068
    return terrno;
×
1069
  }
1070

1071
  SEncoder encoder;
1072
  tEncoderInit(&encoder, buf, tlen);
4,717✔
1073
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,719!
1074
    rpcFreeCont(buf);
×
1075
    tEncoderClear(&encoder);
×
1076
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:%s", id, vgId, tstrerror(code));
×
1077
    return code;
×
1078
  }
1079

1080
  tEncoderClear(&encoder);
4,719✔
1081

1082
  SRpcMsg msg = {0};
4,722✔
1083
  initRpcMsg(&msg, TDMT_MND_STREAM_REQ_CHKPT, buf, tlen);
4,722✔
1084
  stDebug("s-task:%s vgId:%d build and send task checkpoint req", id, vgId);
4,719✔
1085

1086
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
4,719✔
1087
}
1088

1089
void streamTaskGetUpstreamTaskEpInfo(SStreamTask* pTask, int32_t taskId, SStreamUpstreamEpInfo** pEpInfo) {
95,203✔
1090
  *pEpInfo = NULL;
95,203✔
1091

1092
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
95,203✔
1093
  for (int32_t i = 0; i < num; ++i) {
186,405!
1094
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
186,417✔
1095
    if (pInfo == NULL) {
186,298!
1096
      return;
×
1097
    }
1098

1099
    if (pInfo->taskId == taskId) {
186,298✔
1100
      *pEpInfo = pInfo;
95,125✔
1101
      return;
95,125✔
1102
    }
1103
  }
1104

1105
  stError("s-task:%s failed to find upstream task:0x%x", pTask->id.idStr, taskId);
×
1106
}
1107

1108
SEpSet* streamTaskGetDownstreamEpInfo(SStreamTask* pTask, int32_t taskId) {
×
1109
  if (pTask->info.taskLevel == TASK_OUTPUT__FIXED_DISPATCH) {
×
1110
    if (pTask->outputInfo.fixedDispatcher.taskId == taskId) {
×
1111
      return &pTask->outputInfo.fixedDispatcher.epSet;
×
1112
    }
1113
  } else if (pTask->info.taskLevel == TASK_OUTPUT__SHUFFLE_DISPATCH) {
×
1114
    SArray* pList = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
×
1115
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1116
      SVgroupInfo* pVgInfo = taosArrayGet(pList, i);
×
1117
      if (pVgInfo == NULL) {
×
1118
        continue;
×
1119
      }
1120

1121
      if (pVgInfo->taskId == taskId) {
×
1122
        return &pVgInfo->epSet;
×
1123
      }
1124
    }
1125
  }
1126

1127
  return NULL;
×
1128
}
1129

1130
int32_t createStreamTaskIdStr(int64_t streamId, int32_t taskId, const char** pId) {
14,422✔
1131
  char buf[128] = {0};
14,422✔
1132
  sprintf(buf, "0x%" PRIx64 "-0x%x", streamId, taskId);
14,422✔
1133
  *pId = taosStrdup(buf);
14,422✔
1134

1135
  if (*pId == NULL) {
14,423✔
1136
    return terrno;
1✔
1137
  } else {
1138
    return TSDB_CODE_SUCCESS;
14,422✔
1139
  }
1140
}
1141

1142
static int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
551✔
1143
  int32_t           code;
1144
  SStreamDataBlock* pData;
1145

1146
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
551✔
1147
  if (code) {
551!
1148
    stError("s-task:%s failed to allocated retrieve-block", pTask->id.idStr);
×
1149
    return terrno = code;
×
1150
  }
1151

1152
  pData->type = STREAM_INPUT__DATA_RETRIEVE;
551✔
1153
  pData->srcVgId = 0;
551✔
1154

1155
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
551✔
1156
  if (code != TSDB_CODE_SUCCESS) {
551!
1157
    stError("s-task:%s failed to convert retrieve-data to block, code:%s", pTask->id.idStr, tstrerror(code));
×
1158
    taosFreeQitem(pData);
×
1159
    return code;
×
1160
  }
1161

1162
  code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pData);
551✔
1163
  if (code != TSDB_CODE_SUCCESS) {
551!
1164
    stError("s-task:%s failed to put retrieve-block into inputQ, inputQ is full, discard the retrieve msg",
×
1165
            pTask->id.idStr);
1166
  }
1167

1168
  return code;
551✔
1169
}
1170

1171
int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
551✔
1172
  int32_t code = streamTaskEnqueueRetrieve(pTask, pReq);
551✔
1173
  if (code != 0) {
551!
1174
    return code;
×
1175
  }
1176
  return streamTrySchedExec(pTask);
551✔
1177
}
1178

1179
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
6,964✔
1180

1181
void streamTaskGetActiveCheckpointInfo(const SStreamTask* pTask, int32_t* pTransId, int64_t* pCheckpointId) {
×
1182
  if (pTransId != NULL) {
×
1183
    *pTransId = pTask->chkInfo.pActiveInfo->transId;
×
1184
  }
1185

1186
  if (pCheckpointId != NULL) {
×
1187
    *pCheckpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1188
  }
1189
}
×
1190

1191
int32_t streamTaskSetActiveCheckpointInfo(SStreamTask* pTask, int64_t activeCheckpointId) {
28✔
1192
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
28✔
1193
  return TSDB_CODE_SUCCESS;
28✔
1194
}
1195

1196
void streamTaskSetFailedChkptInfo(SStreamTask* pTask, int32_t transId, int64_t checkpointId) {
×
1197
  pTask->chkInfo.pActiveInfo->transId = transId;
×
1198
  pTask->chkInfo.pActiveInfo->activeId = checkpointId;
×
1199
  pTask->chkInfo.pActiveInfo->failedId = checkpointId;
×
1200
  stDebug("s-task:%s set failed checkpointId:%"PRId64, pTask->id.idStr, checkpointId);
×
1201
}
×
1202

1203
int32_t streamTaskCreateActiveChkptInfo(SActiveCheckpointInfo** pRes) {
14,464✔
1204
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
14,464✔
1205
  if (pInfo == NULL) {
14,463!
1206
    return terrno;
×
1207
  }
1208

1209
  int32_t code = taosThreadMutexInit(&pInfo->lock, NULL);
14,463✔
1210
  if (code != TSDB_CODE_SUCCESS) {
14,465!
1211
    return code;
×
1212
  }
1213

1214
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
14,465✔
1215
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
14,467✔
1216
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
14,458✔
1217

1218
  *pRes = pInfo;
14,467✔
1219
  return code;
14,467✔
1220
}
1221

1222
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
59,321✔
1223
  if (pInfo == NULL) {
59,321✔
1224
    return;
45,110✔
1225
  }
1226

1227
  streamMutexDestroy(&pInfo->lock);
14,211✔
1228
  taosArrayDestroy(pInfo->pDispatchTriggerList);
14,212✔
1229
  pInfo->pDispatchTriggerList = NULL;
14,214✔
1230
  taosArrayDestroy(pInfo->pReadyMsgList);
14,214✔
1231
  pInfo->pReadyMsgList = NULL;
14,212✔
1232
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
14,212✔
1233
  pInfo->pCheckpointReadyRecvList = NULL;
14,212✔
1234

1235
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
14,212✔
1236
  if (pTriggerTmr->tmrHandle != NULL) {
14,212✔
1237
    streamTmrStop(pTriggerTmr->tmrHandle);
2,272✔
1238
    pTriggerTmr->tmrHandle = NULL;
2,273✔
1239
  }
1240

1241
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
14,213✔
1242
  if (pReadyTmr->tmrHandle != NULL) {
14,213✔
1243
    streamTmrStop(pReadyTmr->tmrHandle);
2,261✔
1244
    pReadyTmr->tmrHandle = NULL;
2,261✔
1245
  }
1246

1247
  taosMemoryFree(pInfo);
14,213✔
1248
}
1249

1250
// NOTE: clear the checkpoint id, and keep the failed id
1251
// failedId for a task will increase as the checkpoint I.D. increases.
1252
void streamTaskClearActiveInfo(SActiveCheckpointInfo* pInfo) {
5,216✔
1253
  pInfo->activeId = 0;
5,216✔
1254
  pInfo->transId = 0;
5,216✔
1255
  pInfo->allUpstreamTriggerRecv = 0;
5,216✔
1256
  pInfo->dispatchTrigger = false;
5,216✔
1257

1258
  taosArrayClear(pInfo->pDispatchTriggerList);
5,216✔
1259
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,197✔
1260
}
5,192✔
1261

1262
const char* streamTaskGetExecType(int32_t type) {
122,229✔
1263
  switch (type) {
122,229!
1264
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
52,349✔
1265
      return "scan-wal-file";
52,349✔
1266
    case STREAM_EXEC_T_START_ALL_TASKS:
8,451✔
1267
      return "start-all-tasks";
8,451✔
1268
    case STREAM_EXEC_T_START_ONE_TASK:
5,208✔
1269
      return "start-one-task";
5,208✔
1270
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
38✔
1271
      return "restart-all-tasks";
38✔
1272
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,595✔
1273
      return "stop-all-tasks";
4,595✔
1274
    case STREAM_EXEC_T_RESUME_TASK:
9,849✔
1275
      return "resume-task-from-idle";
9,849✔
1276
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
1277
      return "record-start-failed-task";
×
1278
    case 0:
41,823✔
1279
      return "exec-all-tasks";
41,823✔
1280
    default:
×
1281
      return "invalid-exec-type";
×
1282
  }
1283
}
1284

1285
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
45,324✔
1286
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
45,324✔
1287
  if (*pRefId != NULL) {
45,330✔
1288
    **pRefId = pTask->id.refId;
45,329✔
1289
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
45,329✔
1290
    if (code != 0) {
45,346!
1291
      stError("s-task:%s failed to add refId:%" PRId64 " into refId-mgmt, code:%s", pTask->id.idStr, pTask->id.refId,
×
1292
              tstrerror(code));
1293
    }
1294
    return code;
45,346✔
1295
  } else {
1296
    stError("s-task:%s failed to alloc new ref id, code:%s", pTask->id.idStr, tstrerror(terrno));
1!
1297
    return terrno;
1✔
1298
  }
1299
}
1300

1301
void streamTaskFreeRefId(int64_t* pRefId) {
41,708✔
1302
  if (pRefId == NULL) {
41,708✔
1303
    return;
2,104✔
1304
  }
1305

1306
  metaRefMgtRemove(pRefId);
39,604✔
1307
}
1308

1309

1310
int32_t tEncodeStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
132,386✔
1311
  int32_t code = 0;
132,386✔
1312
  int32_t lino;
1313

1314
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
132,386!
1315
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->ver));
264,788!
1316
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->id.streamId));
264,788!
1317
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->id.taskId));
264,788!
1318
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.trigger));
264,788!
1319
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.taskLevel));
264,788!
1320
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.type));
264,788!
1321
  TAOS_CHECK_EXIT(tEncodeI16(pEncoder, pTask->msgInfo.msgType));
264,788!
1322

1323
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.taskStatus));
264,788!
1324
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.schedStatus));
264,788!
1325

1326
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
264,788!
1327
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
264,788!
1328
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
132,394!
1329
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
132,409!
1330

1331
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointId));
264,826!
1332
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointVer));
264,826!
1333
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.fillHistory));
264,826!
1334

1335
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
264,826!
1336
  int32_t taskId = pTask->hTaskInfo.id.taskId;
132,413✔
1337
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
132,413!
1338

1339
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
264,826!
1340
  taskId = pTask->streamTaskId.taskId;
132,413✔
1341
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
132,413!
1342

1343
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.minVer));
264,826!
1344
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.maxVer));
264,826!
1345
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.skey));
264,826!
1346
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.ekey));
264,826!
1347

1348
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
132,413✔
1349
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
132,401!
1350
  for (int32_t i = 0; i < epSz; i++) {
315,785✔
1351
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
183,378✔
1352
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
183,355!
1353
  }
1354

1355
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
132,407✔
1356
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
140,022!
1357
  }
1358

1359
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
132,407✔
1360
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
129,476!
1361
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
129,476!
1362
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
129,476!
1363
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
67,669✔
1364
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
780!
1365
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
67,279!
1366
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1367
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
67,279✔
1368
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
20,904!
1369
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
20,904!
1370
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
10,452!
1371
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
56,827✔
1372
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
56,785!
1373
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
113,572!
1374
  }
1375
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
264,816!
1376
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
264,816!
1377
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
264,816!
1378

1379
  tEndEncode(pEncoder);
132,408✔
1380
_exit:
132,400✔
1381
  return code;
132,400✔
1382
}
1383

1384
int32_t tDecodeStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
46,040✔
1385
  int32_t taskId = 0;
46,040✔
1386
  int32_t code = 0;
46,040✔
1387
  int32_t lino;
1388

1389
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
46,040!
1390
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
92,092!
1391
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
46,046!
1392
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
4!
1393
  }
1394

1395
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
92,090!
1396
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
92,085!
1397
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
92,080!
1398
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
92,079!
1399
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
92,076!
1400
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
92,072!
1401

1402
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
92,071!
1403
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
92,071!
1404

1405
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
92,076!
1406
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
92,077!
1407
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
46,037!
1408
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
46,044!
1409

1410
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
92,094!
1411
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
92,090!
1412
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
92,082!
1413

1414
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
92,081!
1415
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
46,043!
1416
  pTask->hTaskInfo.id.taskId = taskId;
46,043✔
1417

1418
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
92,085!
1419
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
46,038!
1420
  pTask->streamTaskId.taskId = taskId;
46,038✔
1421

1422
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
92,079!
1423
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
92,083!
1424
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
92,083!
1425
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
92,079!
1426

1427
  int32_t epSz = -1;
46,038✔
1428
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &epSz) < 0);
46,038!
1429

1430
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
46,038!
1431
    TAOS_CHECK_EXIT(terrno);
×
1432
  }
1433
  for (int32_t i = 0; i < epSz; i++) {
109,501✔
1434
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
63,452✔
1435
    if (pInfo == NULL) {
63,443!
1436
      TAOS_CHECK_EXIT(terrno);
×
1437
    }
1438
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
63,443!
1439
      taosMemoryFreeClear(pInfo);
×
1440
      goto _exit;
×
1441
    }
1442
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
126,916!
1443
      TAOS_CHECK_EXIT(terrno);
×
1444
    }
1445
  }
1446

1447
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
46,049✔
1448
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
48,425!
1449
  }
1450

1451
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
46,050✔
1452
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
45,048!
1453
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
22,525!
1454
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
22,526✔
1455
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
22,525!
1456
      TAOS_CHECK_EXIT(terrno);
×
1457
    }
1458
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
44,959!
1459
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
23,527✔
1460
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
274!
1461
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
23,390!
1462
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1463
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
23,390✔
1464
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
6,856!
1465
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
6,856!
1466
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,428!
1467
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
19,962✔
1468
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
19,936!
1469
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
19,937!
1470
  }
1471
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
92,001!
1472
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
46,042!
1473
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
92,083!
1474
  }
1475
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
46,041!
1476

1477
  tEndDecode(pDecoder);
46,040✔
1478

1479
_exit:
46,041✔
1480
  return code;
46,041✔
1481
}
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