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

taosdata / TDengine / #3532

20 Nov 2024 07:11AM UTC coverage: 60.78% (+0.6%) from 60.213%
#3532

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

119943 of 252352 branches covered (47.53%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

463 existing lines in 99 files now uncovered.

200682 of 275165 relevant lines covered (72.93%)

15642683.31 hits per line

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

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

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

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

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

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

78
  return code;
300✔
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,112✔
87
  SStreamUpstreamEpInfo** pInfo = p;
82,112✔
88
  taosMemoryFree(*pInfo);
82,112✔
89
}
82,115✔
90

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

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

104
  return pEpInfo;
19,289✔
105
}
106

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

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

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

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

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

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

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

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

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

165
  return code;
13,418✔
166
}
167

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

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

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

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

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

197
  tEndDecode(pDecoder);
684✔
198
  return 0;
684✔
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) {
58,868✔
218
  char*        p = NULL;
58,868✔
219
  SStreamTask* pTask = pParam;
58,868✔
220
  int32_t      taskId = pTask->id.taskId;
58,868✔
221

222
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
58,868✔
223

224
  ETaskStatus status1 = TASK_STATUS__UNINIT;
58,868✔
225
  streamMutexLock(&pTask->lock);
58,868✔
226
  if (pTask->status.pSM != NULL) {
58,883✔
227
    SStreamTaskState status = streamTaskGetStatus(pTask);
27,426✔
228
    p = status.name;
27,412✔
229
    status1 = status.state;
27,412✔
230
  }
231
  streamMutexUnlock(&pTask->lock);
58,869✔
232

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

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

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

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

257
  if (pTask->inputq.queue) {
58,883✔
258
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
13,977✔
259
    pTask->inputq.queue = NULL;
13,976✔
260
  }
261

262
  if (pTask->outputq.queue) {
58,882✔
263
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
13,975✔
264
    pTask->outputq.queue = NULL;
13,979✔
265
  }
266

267
  if (pTask->exec.qmsg) {
58,886✔
268
    taosMemoryFree(pTask->exec.qmsg);
30,958✔
269
  }
270

271
  if (pTask->exec.pExecutor) {
58,887✔
272
    qDestroyTask(pTask->exec.pExecutor);
7,054✔
273
    pTask->exec.pExecutor = NULL;
7,053✔
274
  }
275

276
  if (pTask->exec.pWalReader != NULL) {
58,886✔
277
    walCloseReader(pTask->exec.pWalReader);
6,994✔
278
    pTask->exec.pWalReader = NULL;
6,995✔
279
  }
280

281
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
58,887✔
282

283
  if (pTask->msgInfo.pData != NULL) {
58,885✔
284
    clearBufferedDispatchMsg(pTask);
38✔
285
  }
286

287
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
58,885✔
288
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
28,708!
289
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
28,714✔
290
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
28,711✔
291
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
28,714✔
292
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
30,177✔
293
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
25,728✔
294
  }
295

296
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
58,890✔
297
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
58,888✔
298

299
  if (pTask->pNameMap) {
58,881✔
300
    tSimpleHashCleanup(pTask->pNameMap);
2,236✔
301
  }
302

303
  streamDestroyStateMachine(pTask->status.pSM);
58,881✔
304
  pTask->status.pSM = NULL;
58,891✔
305

306
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
58,891✔
307

308
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
58,889✔
309
  streamMutexDestroy(&pTask->lock);
58,892✔
310

311
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
58,888✔
312
  pTask->msgInfo.pSendInfo = NULL;
58,892✔
313
  streamMutexDestroy(&pTask->msgInfo.lock);
58,892✔
314

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

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

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

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

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

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

350
  if (pTask->backendPath != NULL) {
58,875✔
351
    taosMemoryFree(pTask->backendPath);
13,977✔
352
    pTask->backendPath = NULL;
13,977✔
353
  }
354
}
58,875✔
355

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

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

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

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

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

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

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

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

419
  return 0;
14,210✔
420
}
421

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

433
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
14,206✔
434
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
14,206✔
435
  if (code1 || code2) {
14,212!
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,211✔
441

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

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

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

489
  streamTaskOpenAllUpstreamInput(pTask);
14,190✔
490

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

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

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

533
  int32_t type = pTask->outputInfo.type;
118,567✔
534
  if (type == TASK_OUTPUT__TABLE) {
118,567✔
535
    return 0;
186✔
536
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
118,381✔
537
    return 1;
14,208✔
538
  } else {
539
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
104,173✔
540
    return taosArrayGetSize(vgInfo);
104,173✔
541
  }
542
}
543

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

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

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

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

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

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

592
      break;
149✔
593
    }
594
  }
595

596
  return code;
151✔
597
}
598

599
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
58,882✔
600
  if (pUpstreamInfo->pList != NULL) {
58,882✔
601
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
52,128✔
602
    pUpstreamInfo->numOfClosed = 0;
52,131✔
603
    pUpstreamInfo->pList = NULL;
52,131✔
604
  }
605
}
58,885✔
606

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

775
  if ((pInfo != NULL) && pInfo->dataAllowed) {
8,649!
776
    pInfo->dataAllowed = false;
8,651✔
777
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
8,651!
778
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
8,649✔
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,671✔
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) {
119,195✔
801
  bool ret = false;
119,195✔
802

803
  streamMutexLock(&pTask->lock);
119,195✔
804
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
119,233✔
805
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
83,600✔
806
    ret = true;
83,600✔
807
  }
808

809
  streamMutexUnlock(&pTask->lock);
119,233✔
810
  return ret;
119,231✔
811
}
812

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

821
  return status;
81,848✔
822
}
823

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

830
  return status;
1,726✔
831
}
832

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

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

UNCOV
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,435✔
889
  int32_t                code = 0;
6,435✔
890
  int32_t                tlen = 0;
6,435✔
891
  int32_t                vgId = pTask->pMeta->vgId;
6,435✔
892
  const char*            id = pTask->id.idStr;
6,435✔
893
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
6,435✔
894

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

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

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

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

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

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

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

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

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

976
  pDst->startTime = pSrc->startTime;
51,422✔
977
  pDst->hTaskId = pSrc->hTaskId;
51,422✔
978
}
51,422✔
979

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

984
  STaskStatusEntry entry = {
156,225✔
985
      .id = streamTaskGetTaskId(pTask),
52,075✔
986
      .status = streamTaskGetStatus(pTask).state,
52,075✔
987
      .nodeId = pMeta->vgId,
52,075✔
988
      .stage = pMeta->stage,
52,075✔
989

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1092
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
96,906✔
1093
  for (int32_t i = 0; i < num; ++i) {
190,783!
1094
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
190,843✔
1095
    if (pInfo == NULL) {
190,735!
1096
      return;
×
1097
    }
1098

1099
    if (pInfo->taskId == taskId) {
190,735✔
1100
      *pEpInfo = pInfo;
96,923✔
1101
      return;
96,923✔
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,195✔
1131
  char buf[128] = {0};
14,195✔
1132
  sprintf(buf, "0x%" PRIx64 "-0x%x", streamId, taskId);
14,195✔
1133
  *pId = taosStrdup(buf);
14,195✔
1134

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

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

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

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

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

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

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

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

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

1222
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
58,889✔
1223
  if (pInfo == NULL) {
58,889✔
1224
    return;
44,877✔
1225
  }
1226

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

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

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

1247
  taosMemoryFree(pInfo);
14,015✔
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,249✔
1253
  pInfo->activeId = 0;
5,249✔
1254
  pInfo->transId = 0;
5,249✔
1255
  pInfo->allUpstreamTriggerRecv = 0;
5,249✔
1256
  pInfo->dispatchTrigger = false;
5,249✔
1257

1258
  taosArrayClear(pInfo->pDispatchTriggerList);
5,249✔
1259
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,235✔
1260
}
5,237✔
1261

1262
const char* streamTaskGetExecType(int32_t type) {
129,144✔
1263
  switch (type) {
129,144!
1264
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
56,995✔
1265
      return "scan-wal-file";
56,995✔
1266
    case STREAM_EXEC_T_START_ALL_TASKS:
9,568✔
1267
      return "start-all-tasks";
9,568✔
1268
    case STREAM_EXEC_T_START_ONE_TASK:
5,102✔
1269
      return "start-one-task";
5,102✔
1270
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
27✔
1271
      return "restart-all-tasks";
27✔
1272
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,639✔
1273
      return "stop-all-tasks";
4,639✔
1274
    case STREAM_EXEC_T_RESUME_TASK:
10,958✔
1275
      return "resume-task-from-idle";
10,958✔
1276
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
1277
      return "record-start-failed-task";
×
1278
    case 0:
41,940✔
1279
      return "exec-all-tasks";
41,940✔
1280
    default:
×
1281
      return "invalid-exec-type";
×
1282
  }
1283
}
1284

1285
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
47,616✔
1286
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
47,616✔
1287
  if (*pRefId != NULL) {
47,625!
1288
    **pRefId = pTask->id.refId;
47,626✔
1289
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
47,626✔
1290
    if (code != 0) {
47,641!
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;
47,641✔
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) {
44,031✔
1302
  if (pRefId == NULL) {
44,031✔
1303
    return;
2,406✔
1304
  }
1305

1306
  metaRefMgtRemove(pRefId);
41,625✔
1307
}
1308

1309

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

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

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

1326
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
263,354!
1327
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
263,354!
1328
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
131,677!
1329
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
131,669!
1330

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

1335
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
263,346!
1336
  int32_t taskId = pTask->hTaskInfo.id.taskId;
131,673✔
1337
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
131,673!
1338

1339
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
263,346!
1340
  taskId = pTask->streamTaskId.taskId;
131,673✔
1341
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
131,673!
1342

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

1348
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
131,673✔
1349
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
131,660!
1350
  for (int32_t i = 0; i < epSz; i++) {
314,158✔
1351
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
182,491✔
1352
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
182,485!
1353
  }
1354

1355
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
131,667✔
1356
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
139,266!
1357
  }
1358

1359
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
131,667✔
1360
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
128,592!
1361
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
128,592!
1362
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
128,592!
1363
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
67,371✔
1364
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
852!
1365
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
66,945!
1366
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1367
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
66,945✔
1368
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
20,296!
1369
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
20,296!
1370
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
10,148!
1371
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
56,797✔
1372
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
56,755!
1373
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
113,506!
1374
  }
1375
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
263,328!
1376
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
263,328!
1377
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
263,328!
1378

1379
  tEndEncode(pEncoder);
131,664✔
1380
_exit:
131,672✔
1381
  return code;
131,672✔
1382
}
1383

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

1389
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
45,671!
1390
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
91,365!
1391
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
45,683!
1392
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
1393
  }
1394

1395
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
91,364!
1396
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
91,361!
1397
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
91,357!
1398
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
91,355!
1399
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
91,355!
1400
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
91,351!
1401

1402
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
91,345!
1403
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
91,344!
1404

1405
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
91,346!
1406
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
91,349!
1407
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
45,676!
1408
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
45,682!
1409

1410
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
91,365!
1411
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
91,366!
1412
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
91,359!
1413

1414
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
91,352!
1415
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
45,678!
1416
  pTask->hTaskInfo.id.taskId = taskId;
45,678✔
1417

1418
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
91,357!
1419
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
45,676!
1420
  pTask->streamTaskId.taskId = taskId;
45,676✔
1421

1422
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
91,354!
1423
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
91,358!
1424
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
91,360!
1425
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
91,356!
1426

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

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

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

1451
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
45,685✔
1452
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
44,640!
1453
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
22,320!
1454
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
22,322✔
1455
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
22,323✔
1456
      TAOS_CHECK_EXIT(terrno);
1!
1457
    }
1458
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
44,617!
1459
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
23,365✔
1460
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
302!
1461
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
23,214!
1462
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1463
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
23,214✔
1464
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
6,518!
1465
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
6,518!
1466
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,259!
1467
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
19,955✔
1468
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
19,930!
1469
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
19,931!
1470
  }
1471
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
91,340!
1472
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
45,680!
1473
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
91,362!
1474
  }
1475
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
45,678!
1476

1477
  tEndDecode(pDecoder);
45,676✔
1478

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