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

taosdata / TDengine / #3559

18 Dec 2024 12:59AM UTC coverage: 59.805% (+0.03%) from 59.778%
#3559

push

travis-ci

web-flow
Merge pull request #29187 from taosdata/merge/mainto3.0

merge: main to 3.0 branch

132705 of 287544 branches covered (46.15%)

Branch coverage included in aggregate %.

87 of 95 new or added lines in 19 files covered. (91.58%)

1132 existing lines in 133 files now uncovered.

209591 of 284807 relevant lines covered (73.59%)

8125235.78 hits per line

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

70.38
/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,362✔
33
  int32_t childId = taosArrayGetSize(pArray);
13,362✔
34
  pTask->info.selfChildId = childId;
13,362✔
35
  void* p = taosArrayPush(pArray, &pTask);
13,362✔
36
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
13,362!
37
}
38

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

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

51
    if (!isEqual) {
102✔
52
      (*pUpdated) = true;
90✔
53
      char tmp[512] = {0};
90✔
54
      code = epsetToStr(&pTask->info.epSet, tmp, tListLen(tmp));  // only for log file, ignore errors
90✔
55
      if (code) { // print error and continue
90!
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);
90✔
61
      stDebug("s-task:0x%x (vgId:%d) self node epset is updated %s, old:%s", pTask->id.taskId, nodeId, buf, tmp);
90!
62
    } else {
63
      stDebug("s-task:0x%x (vgId:%d) not updated task epset, since epset identical, %s", pTask->id.taskId, nodeId, buf);
12!
64
    }
65
  }
66

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

78
  return code;
325✔
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) {
78,646✔
87
  SStreamUpstreamEpInfo** pInfo = p;
78,646✔
88
  taosMemoryFree(*pInfo);
78,646!
89
}
78,652✔
90

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

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

104
  return pEpInfo;
19,275✔
105
}
106

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

112
  SStreamTask* pTask = (SStreamTask*)taosMemoryCalloc(1, sizeof(SStreamTask));
13,362!
113
  if (pTask == NULL) {
13,362!
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,362✔
120
  pTask->id.taskId = tGenIdPI32();
13,362✔
121
  pTask->id.streamId = streamId;
13,362✔
122

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

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

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

138
  pTask->id.idStr = taosStrdup(buf);
13,362!
139
  if (pTask->id.idStr == NULL) {
13,362!
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,362✔
145
  pTask->status.taskStatus = fillHistory ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
13,362✔
146
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
13,362✔
147
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
13,362✔
148

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

155
  if (fillHistory && !hasFillhistory) {
13,362!
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,362✔
161

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

165
  return code;
13,362✔
166
}
167

168
int32_t tDecodeStreamTaskChkInfo(SDecoder* pDecoder, SCheckpointInfo* pChkpInfo) {
665✔
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;
665!
176
  if (tDecodeI64(pDecoder, &pChkpInfo->msgVer) < 0) return -1;
1,330!
177
  // if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
178

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

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

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

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

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

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

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

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

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

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

222
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
55,012✔
223

224
  ETaskStatus status1 = TASK_STATUS__UNINIT;
55,012✔
225
  streamMutexLock(&pTask->lock);
55,012✔
226
  if (pTask->status.pSM != NULL) {
55,034✔
227
    SStreamTaskState status = streamTaskGetStatus(pTask);
27,287✔
228
    p = status.name;
27,272✔
229
    status1 = status.state;
27,272✔
230
  }
231
  streamMutexUnlock(&pTask->lock);
55,019✔
232

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

235
  SCheckpointInfo* pCkInfo = &pTask->chkInfo;
55,036✔
236
  stDebug("s-task:0x%x task exec summary: create:%" PRId64 ", init:%" PRId64 ", start:%" PRId64
55,036✔
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) {
55,036✔
243
    streamTmrStop(pTask->schedInfo.pDelayTimer);
1,197✔
244
    pTask->schedInfo.pDelayTimer = NULL;
1,197✔
245
  }
246

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

252
  if (pTask->msgInfo.pRetryTmr != NULL) {
55,036✔
253
    streamTmrStop(pTask->msgInfo.pRetryTmr);
5,344✔
254
    pTask->msgInfo.pRetryTmr = NULL;
5,345✔
255
  }
256

257
  if (pTask->inputq.queue) {
55,037✔
258
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
13,895✔
259
    pTask->inputq.queue = NULL;
13,888✔
260
  }
261

262
  if (pTask->outputq.queue) {
55,030✔
263
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
13,885✔
264
    pTask->outputq.queue = NULL;
13,891✔
265
  }
266

267
  if (pTask->exec.qmsg) {
55,036✔
268
    taosMemoryFree(pTask->exec.qmsg);
28,649!
269
  }
270

271
  if (pTask->exec.pExecutor) {
55,037✔
272
    qDestroyTask(pTask->exec.pExecutor);
6,975✔
273
    pTask->exec.pExecutor = NULL;
6,975✔
274
  }
275

276
  if (pTask->exec.pWalReader != NULL) {
55,037✔
277
    walCloseReader(pTask->exec.pWalReader);
6,948✔
278
    pTask->exec.pWalReader = NULL;
6,948✔
279
  }
280

281
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
55,037✔
282

283
  if (pTask->msgInfo.pData != NULL) {
55,035✔
284
    clearBufferedDispatchMsg(pTask);
49✔
285
  }
286

287
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
55,033✔
288
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
26,862✔
289
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
26,862!
290
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
26,861✔
291
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
26,866✔
292
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
28,171✔
293
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
24,343✔
294
  }
295

296
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
55,037✔
297
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
55,030✔
298

299
  if (pTask->pNameMap) {
55,033✔
300
    tSimpleHashCleanup(pTask->pNameMap);
2,247✔
301
  }
302

303
  streamDestroyStateMachine(pTask->status.pSM);
55,033✔
304
  pTask->status.pSM = NULL;
55,038✔
305

306
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
55,038✔
307

308
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
55,036!
309
  streamMutexDestroy(&pTask->lock);
55,038✔
310

311
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
55,035✔
312
  pTask->msgInfo.pSendInfo = NULL;
55,038✔
313
  streamMutexDestroy(&pTask->msgInfo.lock);
55,038✔
314

315
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
55,038✔
316
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
55,038✔
317

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

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

325
  taosMemoryFree(pTask);
55,037!
326
  stDebug("s-task:0x%x free task completed", taskId);
55,037✔
327
}
55,037✔
328

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

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

350
  if (pTask->backendPath != NULL) {
55,033✔
351
    taosMemoryFree(pTask->backendPath);
13,890!
352
    pTask->backendPath = NULL;
13,893✔
353
  }
354
}
55,036✔
355

356
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
14,124✔
357
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,124✔
358
  SDataRange*      pRange = &pTask->dataRange;
14,124✔
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,124✔
362
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
4,194✔
363
    pChkInfo->processedVer = ver - 1;   // already processed version
4,194✔
364
    pChkInfo->nextProcessVer = ver;     // next processed version
4,194✔
365

366
    pRange->range.maxVer = ver;
4,194✔
367
    pRange->range.minVer = ver;
4,194✔
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) {
9,930✔
372
      pChkInfo->checkpointVer = pRange->range.maxVer;
5,028✔
373
      pChkInfo->processedVer = pRange->range.maxVer;
5,028✔
374
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
5,028✔
375
    } else {
376
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
4,902✔
377
      pChkInfo->processedVer = pRange->range.minVer - 1;
4,902✔
378
      pChkInfo->nextProcessVer = pRange->range.minVer;
4,902✔
379

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

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

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

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

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

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

419
  return 0;
14,138✔
420
}
421

422
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
14,123✔
423
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
14,123✔
424
  if (code) {
14,128!
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,128✔
430
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,128✔
431
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,128✔
432

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

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

442
  code = streamCreateStateMachine(pTask);
14,147✔
443
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
14,128!
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,146✔
450
  setInitialVersionInfo(pTask, ver);
14,146✔
451

452
  pTask->pMeta = pMeta;
14,139✔
453
  pTask->pMsgCb = pMsgCb;
14,139✔
454
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
14,139✔
455
  if (pTask->msgInfo.pSendInfo == NULL) {
14,137!
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,137✔
461
  if (code) {
14,137!
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,137✔
467
  code = taosThreadMutexAttrInit(&attr);
14,137✔
468
  if (code != 0) {
14,121!
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,121✔
474
  if (code != 0) {
14,121!
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,121✔
480
  if (code) {
14,134!
481
    return code;
×
482
  }
483

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

489
  streamTaskOpenAllUpstreamInput(pTask);
14,124✔
490

491
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,124✔
492
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
14,124!
493
  if (pOutputInfo->pTokenBucket == NULL) {
14,131!
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,131✔
501
  if (code) {
14,144!
502
    return code;
×
503
  }
504

505
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
14,144✔
506
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
14,146!
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,146✔
512
  if (pTask->taskCheckInfo.pList == NULL) {
14,136!
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,136✔
518
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
14,135✔
519
    if (code) {
14,144!
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,145✔
526
}
527

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

533
  int32_t type = pTask->outputInfo.type;
105,830✔
534
  if (type == TASK_OUTPUT__TABLE) {
105,830✔
535
    return 0;
183✔
536
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
105,647✔
537
    return 1;
9,624✔
538
  } else {
539
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
96,023✔
540
    return taosArrayGetSize(vgInfo);
96,023✔
541
  }
542
}
543

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

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

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

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

560
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
171✔
561
  int32_t code = 0;
171✔
562
  char    buf[512] = {0};
171✔
563
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
171✔
564
  if (code != 0) {  // print error and continue
171!
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);
171✔
570
  for (int32_t i = 0; i < numOfUpstream; ++i) {
337✔
571
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
254✔
572
    if (pInfo->nodeId == nodeId) {
254✔
573
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
88✔
574
      if (!equal) {
88✔
575
        *pUpdated = true;
69✔
576

577
        char tmp[512] = {0};
69✔
578
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
69✔
579
        if (code != 0) {  // print error and continue
69!
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);
69✔
585
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
69!
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,
19!
589
                pInfo->taskId, nodeId, buf);
590
      }
591

592
      break;
88✔
593
    }
594
  }
595

596
  return code;
171✔
597
}
598

599
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
55,033✔
600
  if (pUpstreamInfo->pList != NULL) {
55,033✔
601
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
48,310✔
602
    pUpstreamInfo->numOfClosed = 0;
48,309✔
603
    pUpstreamInfo->pList = NULL;
48,309✔
604
  }
605
}
55,032✔
606

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

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

617
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
171✔
618
  char    buf[512] = {0};
171✔
619
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
171✔
620
  if (code != 0) {                                        // print error and continue
171!
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;
171✔
626
  int8_t  type = pTask->outputInfo.type;
171✔
627

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

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

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

642
          char tmp[512] = {0};
69✔
643
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
69✔
644
          if (code != 0) {  // print error and continue
69!
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);
69✔
650
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
69!
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,
15!
654
                  pVgInfo->taskId, nodeId, buf);
655
        }
656
        break;
84✔
657
      }
658
    }
659
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
85!
660
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
85✔
661
    if (pDispatcher->nodeId == nodeId) {
85✔
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;
171✔
684
}
685

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

691
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,482✔
692
  if (code) {
2,482!
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,482✔
698
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
1,230✔
699
    if (code != TSDB_CODE_SUCCESS) {
1,230!
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,482!
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,482✔
711
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,482✔
712
  return code;
2,482✔
713
}
714

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

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

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

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

733
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
325✔
734
    if (code) {
325!
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;
153✔
740
}
741

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

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

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

756
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
22,183✔
757
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
22,183✔
758
  if (num == 0) {
22,194✔
759
    return;
11,063✔
760
  }
761

762
  for (int32_t i = 0; i < num; ++i) {
43,906✔
763
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
32,767✔
764
    pInfo->dataAllowed = true;
32,775✔
765
  }
766

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

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

775
  if ((pInfo != NULL) && pInfo->dataAllowed) {
6,742!
776
    pInfo->dataAllowed = false;
6,742✔
777
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
6,742!
778
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
6,738✔
779
    } else {
780
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
781
    }
782
  }
783
}
6,759✔
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) {
96,491✔
801
  bool ret = false;
96,491✔
802

803
  streamMutexLock(&pTask->lock);
96,491✔
804
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
96,505✔
805
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
59,575✔
806
    ret = true;
59,575✔
807
  }
808

809
  streamMutexUnlock(&pTask->lock);
96,505✔
810
  return ret;
96,502✔
811
}
812

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

821
  return status;
58,449✔
822
}
823

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

830
  return status;
1,066✔
831
}
832

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

838
  if (pTask->info.fillHistory == 0) {
6,900!
839
    return code;
6,906✔
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) {
4,295✔
889
  int32_t                code = 0;
4,295✔
890
  int32_t                tlen = 0;
4,295✔
891
  int32_t                vgId = pTask->pMeta->vgId;
4,295✔
892
  const char*            id = pTask->id.idStr;
4,295✔
893
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
4,295✔
894

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

904
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
4,295!
905
  if (code < 0) {
4,295!
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);
4,295✔
911
  if (buf == NULL) {
4,295!
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);
4,295✔
919
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
4,295!
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);
4,295✔
926

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

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

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

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

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

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

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

976
  pDst->startTime = pSrc->startTime;
21,872✔
977
  pDst->hTaskId = pSrc->hTaskId;
21,872✔
978
}
21,872✔
979

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

984
  STaskStatusEntry entry = {
66,921✔
985
      .id = streamTaskGetTaskId(pTask),
22,307✔
986
      .status = streamTaskGetStatus(pTask).state,
22,307✔
987
      .nodeId = pMeta->vgId,
22,307✔
988
      .stage = pMeta->stage,
22,307✔
989

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

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

1014
  int32_t num = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
1,418✔
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,426✔
1019
    void* pExecutor = pTask->exec.pExecutor;
54✔
1020
    code = qKillTask(pExecutor, TSDB_CODE_SUCCESS);
54✔
1021
  }
1022

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

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

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

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

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

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

1058
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,469✔
1059
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,469!
1060
  if (code < 0) {
4,471!
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,471✔
1066
  if (buf == NULL) {
4,471!
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,471✔
1073
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,471!
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,471✔
1081

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

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

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

1092
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
92,083✔
1093
  for (int32_t i = 0; i < num; ++i) {
185,608✔
1094
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
185,577✔
1095
    if (pInfo == NULL) {
185,525!
1096
      return;
×
1097
    }
1098

1099
    if (pInfo->taskId == taskId) {
185,525✔
1100
      *pEpInfo = pInfo;
92,015✔
1101
      return;
92,015✔
1102
    }
1103
  }
1104

1105
  stError("s-task:%s failed to find upstream task:0x%x", pTask->id.idStr, taskId);
31!
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,120✔
1131
  char buf[128] = {0};
14,120✔
1132
  sprintf(buf, "0x%" PRIx64 "-0x%x", streamId, taskId);
14,120✔
1133
  *pId = taosStrdup(buf);
14,120!
1134

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

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

1146
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
532✔
1147
  if (code) {
533!
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;
533✔
1153
  pData->srcVgId = 0;
533✔
1154

1155
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
533✔
1156
  if (code != TSDB_CODE_SUCCESS) {
533!
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);
533✔
1163
  if (code != TSDB_CODE_SUCCESS) {
533!
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;
533✔
1169
}
1170

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

1179
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
6,909✔
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,158✔
1204
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
14,158!
1205
  if (pInfo == NULL) {
14,170!
1206
    return terrno;
×
1207
  }
1208

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

1214
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
14,173✔
1215
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
14,180✔
1216
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
14,174✔
1217

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

1222
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
55,036✔
1223
  if (pInfo == NULL) {
55,036✔
1224
    return;
41,110✔
1225
  }
1226

1227
  streamMutexDestroy(&pInfo->lock);
13,926✔
1228
  taosArrayDestroy(pInfo->pDispatchTriggerList);
13,924✔
1229
  pInfo->pDispatchTriggerList = NULL;
13,928✔
1230
  taosArrayDestroy(pInfo->pReadyMsgList);
13,928✔
1231
  pInfo->pReadyMsgList = NULL;
13,929✔
1232
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
13,929✔
1233
  pInfo->pCheckpointReadyRecvList = NULL;
13,927✔
1234

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

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

1247
  taosMemoryFree(pInfo);
13,928!
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) {
3,512✔
1253
  pInfo->activeId = 0;
3,512✔
1254
  pInfo->transId = 0;
3,512✔
1255
  pInfo->allUpstreamTriggerRecv = 0;
3,512✔
1256
  pInfo->dispatchTrigger = false;
3,512✔
1257

1258
  taosArrayClear(pInfo->pDispatchTriggerList);
3,512✔
1259
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
3,509✔
1260
}
3,508✔
1261

1262
const char* streamTaskGetExecType(int32_t type) {
93,074✔
1263
  switch (type) {
93,074!
1264
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
32,548✔
1265
      return "scan-wal-file";
32,548✔
1266
    case STREAM_EXEC_T_START_ALL_TASKS:
6,887✔
1267
      return "start-all-tasks";
6,887✔
1268
    case STREAM_EXEC_T_START_ONE_TASK:
5,163✔
1269
      return "start-one-task";
5,163✔
1270
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
32✔
1271
      return "restart-all-tasks";
32✔
1272
    case STREAM_EXEC_T_STOP_ALL_TASKS:
3,739✔
1273
      return "stop-all-tasks";
3,739✔
1274
    case STREAM_EXEC_T_RESUME_TASK:
8,141✔
1275
      return "resume-task-from-idle";
8,141✔
UNCOV
1276
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
UNCOV
1277
      return "record-start-failed-task";
×
1278
    case 0:
36,607✔
1279
      return "exec-all-tasks";
36,607✔
1280
    default:
×
1281
      return "invalid-exec-type";
×
1282
  }
1283
}
1284

1285
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
38,928✔
1286
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
38,928!
1287
  if (*pRefId != NULL) {
38,932!
1288
    **pRefId = pTask->id.refId;
38,932✔
1289
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
38,932✔
1290
    if (code != 0) {
38,944!
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;
38,944✔
1295
  } else {
1296
    stError("s-task:%s failed to alloc new ref id, code:%s", pTask->id.idStr, tstrerror(terrno));
×
1297
    return terrno;
×
1298
  }
1299
}
1300

1301
void streamTaskFreeRefId(int64_t* pRefId) {
35,857✔
1302
  if (pRefId == NULL) {
35,857✔
1303
    return;
2,166✔
1304
  }
1305

1306
  metaRefMgtRemove(pRefId);
33,691✔
1307
}
1308

1309

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

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

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

1326
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
239,414!
1327
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
239,414!
1328
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
119,707!
1329
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
119,717!
1330

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

1335
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
239,416!
1336
  int32_t taskId = pTask->hTaskInfo.id.taskId;
119,708✔
1337
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
119,708!
1338

1339
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
239,416!
1340
  taskId = pTask->streamTaskId.taskId;
119,708✔
1341
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
119,708!
1342

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

1348
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
119,708✔
1349
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
119,709!
1350
  for (int32_t i = 0; i < epSz; i++) {
292,403✔
1351
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
172,698✔
1352
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
172,695!
1353
  }
1354

1355
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
119,705✔
1356
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
124,840!
1357
  }
1358

1359
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
119,705✔
1360
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
116,990!
1361
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
116,990!
1362
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
116,990!
1363
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
61,210✔
1364
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
680!
1365
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
60,870!
1366
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1367
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
60,870✔
1368
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
15,938!
1369
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
15,938!
1370
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
7,969!
1371
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
52,901✔
1372
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
52,853!
1373
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
105,702!
1374
  }
1375
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
239,408!
1376
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
239,408!
1377
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
239,408!
1378

1379
  tEndEncode(pEncoder);
119,704✔
1380
_exit:
119,704✔
1381
  return code;
119,704✔
1382
}
1383

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

1389
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
41,889!
1390
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
83,783!
1391
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
41,888!
1392
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
1393
  }
1394

1395
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
83,773!
1396
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
83,772!
1397
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
83,775!
1398
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
83,775!
1399
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
83,761!
1400
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
83,756!
1401

1402
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
83,771!
1403
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
83,778!
1404

1405
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
83,771!
1406
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
83,767!
1407
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
41,885!
1408
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
41,892!
1409

1410
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
83,784!
1411
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
83,780!
1412
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
83,778!
1413

1414
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
83,775!
1415
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
41,880!
1416
  pTask->hTaskInfo.id.taskId = taskId;
41,880✔
1417

1418
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
83,767!
1419
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
41,888!
1420
  pTask->streamTaskId.taskId = taskId;
41,888✔
1421

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

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

1430
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
41,887!
1431
    TAOS_CHECK_EXIT(terrno);
×
1432
  }
1433
  for (int32_t i = 0; i < epSz; i++) {
101,570✔
1434
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
59,675!
1435
    if (pInfo == NULL) {
59,667!
1436
      TAOS_CHECK_EXIT(terrno);
×
1437
    }
1438
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
59,667!
1439
      taosMemoryFreeClear(pInfo);
×
1440
      goto _exit;
×
1441
    }
1442
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
119,366!
1443
      TAOS_CHECK_EXIT(terrno);
×
1444
    }
1445
  }
1446

1447
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
41,895✔
1448
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
43,529!
1449
  }
1450

1451
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
41,900✔
1452
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
40,948!
1453
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
20,474!
1454
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
20,470!
1455
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
20,476!
1456
      TAOS_CHECK_EXIT(terrno);
×
1457
    }
1458
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
40,896!
1459
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
21,426✔
1460
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
246!
1461
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
21,303!
1462
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1463
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
21,303✔
1464
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
5,428!
1465
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
5,428!
1466
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
2,714!
1467
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
18,589✔
1468
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
18,556!
1469
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
18,558!
1470
  }
1471
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
83,737!
1472
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
41,890!
1473
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
83,783!
1474
  }
1475
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
41,891!
1476

1477
  tEndDecode(pDecoder);
41,888✔
1478

1479
_exit:
41,886✔
1480
  return code;
41,886✔
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