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

taosdata / TDengine / #3582

16 Jan 2025 05:41AM UTC coverage: 63.803% (-0.006%) from 63.809%
#3582

push

travis-ci

web-flow
Merge pull request #29579 from taosdata/test/3.0/TS-5873

Test(wal): add case for remove wal.

141331 of 284535 branches covered (49.67%)

Branch coverage included in aggregate %.

219941 of 281694 relevant lines covered (78.08%)

19199606.59 hits per line

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

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

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

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

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

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

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

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

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

104
  return pEpInfo;
20,098✔
105
}
106

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

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

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

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

135
  char    buf[128] = {0};
13,991✔
136
  int32_t ret = snprintf(buf, tListLen(buf), "0x%" PRIx64 "-0x%x", pTask->id.streamId, pTask->id.taskId);
13,991✔
137
  if (ret < 0 || ret >= tListLen(buf)) {
13,991!
138
    stError("s-task:0x%x failed to set the taskIdstr, code: out of buffer", pTask->id.taskId);
×
139
    return TSDB_CODE_OUT_OF_BUFFER;
×
140
  }
141

142
  pTask->id.idStr = taosStrdup(buf);
13,991!
143
  if (pTask->id.idStr == NULL) {
13,991!
144
    stError("s-task:0x%x failed to build task id, code: out of memory", pTask->id.taskId);
×
145
    return terrno;
×
146
  }
147

148
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
13,991✔
149
  pTask->status.taskStatus = fillHistory ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
13,991✔
150
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
13,991✔
151
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
13,991✔
152

153
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
13,991✔
154
  code = taosThreadMutexInit(&pTask->taskCheckInfo.checkInfoLock, NULL);
13,991✔
155
  if (code) {
13,991!
156
    return code;
×
157
  }
158

159
  if (fillHistory && !hasFillhistory) {
13,991!
160
    stError("s-task:0x%x create task failed, due to inconsistent fill-history flag", pTask->id.taskId);
×
161
    return TSDB_CODE_INVALID_PARA;
×
162
  }
163

164
  epsetAssign(&(pTask->info.mnodeEpset), pEpset);
13,991✔
165

166
  code = addToTaskset(pTaskList, pTask);
13,991✔
167
  *p = pTask;
13,991✔
168

169
  return code;
13,991✔
170
}
171

172
int32_t tDecodeStreamTaskChkInfo(SDecoder* pDecoder, SCheckpointInfo* pChkpInfo) {
855✔
173
  int64_t skip64;
174
  int8_t  skip8;
175
  int32_t skip32;
176
  int16_t skip16;
177
  SEpSet  epSet;
178

179
  if (tStartDecode(pDecoder) < 0) return -1;
855!
180
  if (tDecodeI64(pDecoder, &pChkpInfo->msgVer) < 0) return -1;
1,712!
181
  // if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
182

183
  if (tDecodeI64(pDecoder, &skip64) < 0) return -1;
856!
184
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
856!
185
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
856!
186
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
856!
187
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
856!
188
  if (tDecodeI16(pDecoder, &skip16) < 0) return -1;
856!
189

190
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
856!
191
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
856!
192

193
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
856!
194
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
856!
195
  if (tDecodeSEpSet(pDecoder, &epSet) < 0) return -1;
856!
196
  if (tDecodeSEpSet(pDecoder, &epSet) < 0) return -1;
856!
197

198
  if (tDecodeI64(pDecoder, &pChkpInfo->checkpointId) < 0) return -1;
1,712!
199
  if (tDecodeI64(pDecoder, &pChkpInfo->checkpointVer) < 0) return -1;
1,712!
200

201
  tEndDecode(pDecoder);
856✔
202
  return 0;
856✔
203
}
204

205
int32_t tDecodeStreamTaskId(SDecoder* pDecoder, STaskId* pTaskId) {
56✔
206
  int64_t ver;
207
  if (tStartDecode(pDecoder) < 0) return -1;
56!
208
  if (tDecodeI64(pDecoder, &ver) < 0) return -1;
56!
209
  if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
56!
210

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

213
  int32_t taskId = 0;
56✔
214
  if (tDecodeI32(pDecoder, &taskId) < 0) return -1;
56!
215

216
  pTaskId->taskId = taskId;
56✔
217
  tEndDecode(pDecoder);
56✔
218
  return 0;
56✔
219
}
220

221
void tFreeStreamTask(void* pParam) {
62,656✔
222
  char*        p = NULL;
62,656✔
223
  SStreamTask* pTask = pParam;
62,656✔
224
  int32_t      taskId = pTask->id.taskId;
62,656✔
225

226
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
62,656✔
227

228
  ETaskStatus status1 = TASK_STATUS__UNINIT;
62,656✔
229
  streamMutexLock(&pTask->lock);
62,656✔
230
  if (pTask->status.pSM != NULL) {
62,665✔
231
    SStreamTaskState status = streamTaskGetStatus(pTask);
28,736✔
232
    p = status.name;
28,731✔
233
    status1 = status.state;
28,731✔
234
  }
235
  streamMutexUnlock(&pTask->lock);
62,660✔
236

237
  stDebug("start to free s-task:0x%x %p, state:%s, refId:%" PRId64, taskId, pTask, p, pTask->id.refId);
62,656✔
238

239
  SCheckpointInfo* pCkInfo = &pTask->chkInfo;
62,656✔
240
  stDebug("s-task:0x%x task exec summary: create:%" PRId64 ", init:%" PRId64 ", start:%" PRId64
62,656✔
241
          ", updateCount:%d latestUpdate:%" PRId64 ", latestCheckPoint:%" PRId64 ", ver:%" PRId64
242
          " nextProcessVer:%" PRId64 ", checkpointCount:%d",
243
          taskId, pStatis->created, pStatis->checkTs, pStatis->readyTs, pStatis->updateCount, pStatis->latestUpdateTs,
244
          pCkInfo->checkpointId, pCkInfo->checkpointVer, pCkInfo->nextProcessVer, pStatis->checkpoint);
245

246
  if (pTask->schedInfo.pDelayTimer != NULL) {
62,656✔
247
    streamTmrStop(pTask->schedInfo.pDelayTimer);
1,306✔
248
    pTask->schedInfo.pDelayTimer = NULL;
1,306✔
249
  }
250

251
  if (pTask->hTaskInfo.pTimer != NULL) {
62,656✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
1,783✔
253
    pTask->hTaskInfo.pTimer = NULL;
1,784✔
254
  }
255

256
  if (pTask->msgInfo.pRetryTmr != NULL) {
62,657✔
257
    streamTmrStop(pTask->msgInfo.pRetryTmr);
5,430✔
258
    pTask->msgInfo.pRetryTmr = NULL;
5,431✔
259
  }
260

261
  if (pTask->inputq.queue) {
62,658✔
262
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
14,718✔
263
    pTask->inputq.queue = NULL;
14,717✔
264
  }
265

266
  if (pTask->outputq.queue) {
62,657✔
267
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
14,719✔
268
    pTask->outputq.queue = NULL;
14,718✔
269
  }
270

271
  if (pTask->exec.qmsg) {
62,656✔
272
    taosMemoryFree(pTask->exec.qmsg);
33,027!
273
  }
274

275
  if (pTask->exec.pExecutor) {
62,657✔
276
    qDestroyTask(pTask->exec.pExecutor);
7,407✔
277
    pTask->exec.pExecutor = NULL;
7,406✔
278
  }
279

280
  if (pTask->exec.pWalReader != NULL) {
62,656✔
281
    walCloseReader(pTask->exec.pWalReader);
7,395✔
282
    pTask->exec.pWalReader = NULL;
7,396✔
283
  }
284

285
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
62,657✔
286

287
  if (pTask->msgInfo.pData != NULL) {
62,662✔
288
    clearBufferedDispatchMsg(pTask);
38✔
289
  }
290

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
62,664✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
30,733!
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
30,731!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
30,733✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
30,736✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
31,931✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
27,048✔
298
  }
299

300
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
62,667✔
301
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
62,667✔
302

303
  if (pTask->pNameMap) {
62,662✔
304
    tSimpleHashCleanup(pTask->pNameMap);
2,339✔
305
  }
306

307
  streamDestroyStateMachine(pTask->status.pSM);
62,662✔
308
  pTask->status.pSM = NULL;
62,667✔
309

310
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
62,667✔
311

312
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
62,665!
313
  streamMutexDestroy(&pTask->lock);
62,669✔
314

315
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
62,669✔
316
  pTask->msgInfo.pSendInfo = NULL;
62,667✔
317
  streamMutexDestroy(&pTask->msgInfo.lock);
62,667✔
318

319
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
62,666✔
320
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
62,668✔
321

322
  if (pTask->id.idStr != NULL) {
62,668✔
323
    taosMemoryFree((void*)pTask->id.idStr);
28,707!
324
  }
325

326
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
62,665✔
327
  pTask->chkInfo.pActiveInfo = NULL;
62,665✔
328

329
  taosMemoryFree(pTask);
62,665!
330
  stDebug("s-task:0x%x free task completed", taskId);
62,668✔
331
}
62,668✔
332

333
void streamFreeTaskState(SStreamTask* pTask, int8_t remove) {
62,667✔
334
  stDebug("s-task:0x%x start to free task state/backend", pTask->id.taskId);
62,667✔
335
  if (pTask->pState != NULL) {
62,666✔
336
    stDebug("s-task:0x%x start to free task state", pTask->id.taskId);
7,407✔
337
    streamStateClose(pTask->pState, remove);
7,407✔
338

339
    if (remove) taskDbSetClearFileFlag(pTask->pBackend);
7,407✔
340
    taskDbRemoveRef(pTask->pBackend);
7,407✔
341
    pTask->pBackend = NULL;
7,406✔
342
    pTask->pState = NULL;
7,406✔
343
  } else {
344
    stDebug("s-task:0x%x task state is NULL, may del backend:%s", pTask->id.taskId,
55,259✔
345
            pTask->backendPath ? pTask->backendPath : "NULL");
346
    if (remove) {
55,259✔
347
      if (pTask->backendPath != NULL) {
3,433!
348
        stDebug("s-task:0x%x task state is NULL, do del backend:%s", pTask->id.taskId, pTask->backendPath);
3,433✔
349
        taosRemoveDir(pTask->backendPath);
3,433✔
350
      }
351
    }
352
  }
353

354
  if (pTask->backendPath != NULL) {
62,661✔
355
    taosMemoryFree(pTask->backendPath);
14,715!
356
    pTask->backendPath = NULL;
14,714✔
357
  }
358
}
62,660✔
359

360
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
14,954✔
361
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,954✔
362
  SDataRange*      pRange = &pTask->dataRange;
14,954✔
363

364
  // only set the version info for stream tasks without fill-history task
365
  if ((pTask->info.fillHistory == 0) && (!HAS_RELATED_FILLHISTORY_TASK(pTask))) {
14,954✔
366
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
4,736✔
367
    pChkInfo->processedVer = ver - 1;   // already processed version
4,736✔
368
    pChkInfo->nextProcessVer = ver;     // next processed version
4,736✔
369

370
    pRange->range.maxVer = ver;
4,736✔
371
    pRange->range.minVer = ver;
4,736✔
372
  } else {
373
    // the initial value of processedVer/nextProcessVer/checkpointVer for stream task with related fill-history task
374
    // is set at the mnode.
375
    if (pTask->info.fillHistory == 1) {
10,218✔
376
      pChkInfo->checkpointVer = pRange->range.maxVer;
5,175✔
377
      pChkInfo->processedVer = pRange->range.maxVer;
5,175✔
378
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
5,175✔
379
    } else {
380
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
5,043✔
381
      pChkInfo->processedVer = pRange->range.minVer - 1;
5,043✔
382
      pChkInfo->nextProcessVer = pRange->range.minVer;
5,043✔
383

384
      {  // for compatible purpose, remove it later
385
        if (pRange->range.minVer == 0) {
5,043✔
386
          pChkInfo->checkpointVer = 0;
2,541✔
387
          pChkInfo->processedVer = 0;
2,541✔
388
          pChkInfo->nextProcessVer = 1;
2,541✔
389
          stDebug("s-task:%s update the processedVer to 0 from -1 due to compatible purpose", pTask->id.idStr);
2,541✔
390
        }
391
      }
392
    }
393
  }
394
}
14,954✔
395

396
int32_t streamTaskSetBackendPath(SStreamTask* pTask) {
14,969✔
397
  int64_t streamId = 0;
14,969✔
398
  int32_t taskId = 0;
14,969✔
399

400
  if (pTask->info.fillHistory) {
14,969✔
401
    streamId = pTask->streamTaskId.streamId;
5,175✔
402
    taskId = pTask->streamTaskId.taskId;
5,175✔
403
  } else {
404
    streamId = pTask->id.streamId;
9,794✔
405
    taskId = pTask->id.taskId;
9,794✔
406
  }
407

408
  char    id[128] = {0};
14,969✔
409
  int32_t nBytes = snprintf(id, tListLen(id), "0x%" PRIx64 "-0x%x", streamId, taskId);
14,969✔
410
  if (nBytes < 0 || nBytes >= sizeof(id)) {
14,969!
411
    return TSDB_CODE_OUT_OF_BUFFER;
×
412
  }
413

414
  int32_t len = strlen(pTask->pMeta->path);
14,976✔
415
  pTask->backendPath = (char*)taosMemoryMalloc(len + nBytes + 2);
14,976!
416
  if (pTask->backendPath == NULL) {
14,967!
417
    return terrno;
×
418
  }
419

420
  int32_t code = snprintf(pTask->backendPath, len + nBytes + 2, "%s%s%s", pTask->pMeta->path, TD_DIRSEP, id);
14,967✔
421
  if (code < 0 || code >= len + nBytes + 2) {
14,967!
422
    stError("s-task:%s failed to set backend path:%s, code: out of buffer", pTask->id.idStr, pTask->backendPath);
4!
423
    return TSDB_CODE_OUT_OF_BUFFER;
×
424
  } else {
425
    stDebug("s-task:%s set backend path:%s", pTask->id.idStr, pTask->backendPath);
14,963✔
426
    return 0;
14,962✔
427
  }
428
}
429

430
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
14,951✔
431
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
14,951✔
432
  if (code) {
14,958!
433
    stError("0x%x failed create stream task id str, code:%s", pTask->id.taskId, tstrerror(code));
×
434
    return code;
×
435
  }
436

437
  pTask->id.refId = 0;
14,958✔
438
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,958✔
439
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,958✔
440

441
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
14,958✔
442
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
14,956✔
443
  if (code1 || code2) {
14,975!
444
    stError("s-task:%s failed to prepare the input/output queue, initialize task failed", pTask->id.idStr);
×
445
    return TSDB_CODE_OUT_OF_MEMORY;
×
446
  }
447

448
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
14,975✔
449

450
  code = streamCreateStateMachine(pTask);
14,975✔
451
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
14,954!
452
    stError("s-task:%s failed create state-machine for stream task, initialization failed, code:%s", pTask->id.idStr,
×
453
            tstrerror(code));
454
    return code;
×
455
  }
456

457
  pTask->execInfo.created = taosGetTimestampMs();
14,965✔
458
  setInitialVersionInfo(pTask, ver);
14,965✔
459

460
  pTask->pMeta = pMeta;
14,962✔
461
  pTask->pMsgCb = pMsgCb;
14,962✔
462
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
14,962✔
463
  if (pTask->msgInfo.pSendInfo == NULL) {
14,964!
464
    stError("s-task:%s failed to create sendInfo struct for stream task, code:Out of memory", pTask->id.idStr);
×
465
    return terrno;
×
466
  }
467

468
  code = taosThreadMutexInit(&pTask->msgInfo.lock, NULL);
14,964✔
469
  if (code) {
14,969!
470
    stError("s-task:0x%x failed to init msgInfo mutex, code:%s", pTask->id.taskId, tstrerror(code));
×
471
    return code;
×
472
  }
473

474
  TdThreadMutexAttr attr = {0};
14,969✔
475
  code = taosThreadMutexAttrInit(&attr);
14,969✔
476
  if (code != 0) {
14,952!
477
    stError("s-task:%s initElapsed mutex attr failed, code:%s", pTask->id.idStr, tstrerror(code));
×
478
    return code;
×
479
  }
480

481
  code = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
14,952✔
482
  if (code != 0) {
14,947!
483
    stError("s-task:%s set mutex attr recursive, code:%s", pTask->id.idStr, tstrerror(code));
×
484
    return code;
×
485
  }
486

487
  code = taosThreadMutexInit(&pTask->lock, &attr);
14,947✔
488
  if (code) {
14,963!
489
    return code;
×
490
  }
491

492
  code = taosThreadMutexAttrDestroy(&attr);
14,963✔
493
  if (code) {
14,943!
494
    return code;
×
495
  }
496

497
  streamTaskOpenAllUpstreamInput(pTask);
14,943✔
498

499
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,943✔
500
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
14,943!
501
  if (pOutputInfo->pTokenBucket == NULL) {
14,965!
502
    stError("s-task:%s failed to prepare the tokenBucket, code:%s", pTask->id.idStr, tstrerror(terrno));
×
503
    return terrno;
×
504
  }
505

506
  // 2MiB per second for sink task
507
  // 50 times sink operator per second
508
  code = streamTaskInitTokenBucket(pOutputInfo->pTokenBucket, 35, 35, tsSinkDataRate, pTask->id.idStr);
14,965✔
509
  if (code) {
14,965!
510
    return code;
×
511
  }
512

513
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
14,965✔
514
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
14,975!
515
    stError("s-task:%s failed to prepare downstreamUpdateList, code:%s", pTask->id.idStr, tstrerror(terrno));
×
516
    return terrno;
×
517
  }
518

519
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
14,975✔
520
  if (pTask->taskCheckInfo.pList == NULL) {
14,967!
521
    stError("s-task:%s failed to prepare taskCheckInfo list, code:%s", pTask->id.idStr, tstrerror(terrno));
×
522
    return terrno;
×
523
  }
524

525
  if (pTask->chkInfo.pActiveInfo == NULL) {
14,967!
526
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
14,967✔
527
    if (code) {
14,971!
528
      stError("s-task:%s failed to create active checkpoint info, code:%s", pTask->id.idStr, tstrerror(code));
×
529
      return code;
×
530
    }
531
  }
532

533
  return streamTaskSetBackendPath(pTask);
14,971✔
534
}
535

536
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
119,117✔
537
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
119,117✔
538
    return 0;
6,841✔
539
  }
540

541
  int32_t type = pTask->outputInfo.type;
112,276✔
542
  if (type == TASK_OUTPUT__TABLE) {
112,276✔
543
    return 0;
270✔
544
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
112,006✔
545
    return 1;
11,479✔
546
  } else {
547
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
100,527✔
548
    return taosArrayGetSize(vgInfo);
100,527✔
549
  }
550
}
551

552
int32_t streamTaskGetNumOfUpstream(const SStreamTask* pTask) { return taosArrayGetSize(pTask->upstreamInfo.pList); }
19,157✔
553

554
int32_t streamTaskSetUpstreamInfo(SStreamTask* pTask, const SStreamTask* pUpstreamTask) {
20,098✔
555
  SStreamUpstreamEpInfo* pEpInfo = createStreamTaskEpInfo(pUpstreamTask);
20,098✔
556
  if (pEpInfo == NULL) {
20,098!
557
    return terrno;
×
558
  }
559

560
  if (pTask->upstreamInfo.pList == NULL) {
20,098✔
561
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
6,946✔
562
  }
563

564
  void* p = taosArrayPush(pTask->upstreamInfo.pList, &pEpInfo);
20,098✔
565
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
20,098!
566
}
567

568
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
255✔
569
  int32_t code = 0;
255✔
570
  char    buf[512] = {0};
255✔
571
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
255✔
572
  if (code != 0) {  // print error and continue
255!
573
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
574
    return code;
×
575
  }
576

577
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
255✔
578
  for (int32_t i = 0; i < numOfUpstream; ++i) {
527✔
579
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
436✔
580
    if (pInfo->nodeId == nodeId) {
436✔
581
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
164✔
582
      if (!equal) {
164✔
583
        *pUpdated = true;
103✔
584

585
        char tmp[512] = {0};
103✔
586
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
103✔
587
        if (code != 0) {  // print error and continue
103!
588
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
589
          return code;
×
590
        }
591

592
        epsetAssign(&pInfo->epSet, pEpSet);
103✔
593
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
103✔
594
                pInfo->taskId, nodeId, buf, tmp);
595
      } else {
596
        stDebug("s-task:0x%x not update upstreamInfo, since identical, task:0x%x(nodeId:%d) epset:%s", pTask->id.taskId,
61!
597
                pInfo->taskId, nodeId, buf);
598
      }
599

600
      break;
164✔
601
    }
602
  }
603

604
  return code;
255✔
605
}
606

607
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
62,660✔
608
  if (pUpstreamInfo->pList != NULL) {
62,660✔
609
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
55,584✔
610
    pUpstreamInfo->numOfClosed = 0;
55,588✔
611
    pUpstreamInfo->pList = NULL;
55,588✔
612
  }
613
}
62,664✔
614

615
void streamTaskSetFixedDownstreamInfo(SStreamTask* pTask, const SStreamTask* pDownstreamTask) {
960✔
616
  STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
960✔
617
  pDispatcher->taskId = pDownstreamTask->id.taskId;
960✔
618
  pDispatcher->nodeId = pDownstreamTask->info.nodeId;
960✔
619
  pDispatcher->epSet = pDownstreamTask->info.epSet;
960✔
620

621
  pTask->outputInfo.type = TASK_OUTPUT__FIXED_DISPATCH;
960✔
622
  pTask->msgInfo.msgType = TDMT_STREAM_TASK_DISPATCH;
960✔
623
}
960✔
624

625
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
255✔
626
  char    buf[512] = {0};
255✔
627
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
255✔
628
  if (code != 0) {                                        // print error and continue
255!
629
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
630
    return code;
×
631
  }
632

633
  int32_t id = pTask->id.taskId;
255✔
634
  int8_t  type = pTask->outputInfo.type;
255✔
635

636
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
255✔
637
    SArray* pVgs = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
162✔
638

639
    for (int32_t i = 0; i < taosArrayGetSize(pVgs); i++) {
345✔
640
      SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
343✔
641
      if (pVgInfo == NULL) {
343!
642
        continue;
×
643
      }
644

645
      if (pVgInfo->vgId == nodeId) {
343✔
646
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
160✔
647
        if (!isEqual) {
160✔
648
          *pUpdated = true;
103✔
649

650
          char tmp[512] = {0};
103✔
651
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
103✔
652
          if (code != 0) {  // print error and continue
103!
653
            stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
654
            return code;
×
655
          }
656

657
          epsetAssign(&pVgInfo->epSet, pEpSet);
103✔
658
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
103✔
659
                  nodeId, buf, tmp);
660
        } else {
661
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
57!
662
                  pVgInfo->taskId, nodeId, buf);
663
        }
664
        break;
160✔
665
      }
666
    }
667
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
93!
668
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
93✔
669
    if (pDispatcher->nodeId == nodeId) {
93✔
670
      bool equal = isEpsetEqual(&pDispatcher->epSet, pEpSet);
4✔
671
      if (!equal) {
4!
672
        *pUpdated = true;
×
673

674
        char tmp[512] = {0};
×
675
        code = epsetToStr(&pDispatcher->epSet, tmp, tListLen(tmp));
×
676
        if (code != 0) {  // print error and continue
×
677
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
678
          return code;
×
679
        }
680

681
        epsetAssign(&pDispatcher->epSet, pEpSet);
×
682
        stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pDispatcher->taskId,
×
683
                nodeId, buf, tmp);
684
      } else {
685
        stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
4!
686
                pDispatcher->taskId, nodeId, buf);
687
      }
688
    }
689
  }
690

691
  return code;
255✔
692
}
693

694
int32_t streamTaskStop(SStreamTask* pTask) {
3,202✔
695
  int32_t     vgId = pTask->pMeta->vgId;
3,202✔
696
  int64_t     st = taosGetTimestampMs();
3,203✔
697
  const char* id = pTask->id.idStr;
3,203✔
698

699
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
3,203✔
700
  if (code) {
3,203!
701
    stError("failed to handle STOP event, s-task:%s, code:%s", id, tstrerror(code));
×
702
    return code;
×
703
  }
704

705
  if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
3,203✔
706
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
1,583✔
707
    if (code != TSDB_CODE_SUCCESS) {
1,583!
708
      stError("s-task:%s failed to kill task related query handle, code:%s", id, tstrerror(code));
×
709
    }
710
  }
711

712
  while (!streamTaskIsIdle(pTask)) {
3,202!
713
    stDebug("s-task:%s level:%d wait for task to be idle and then close, check again in 100ms", id,
×
714
            pTask->info.taskLevel);
715
    taosMsleep(100);
×
716
  }
717

718
  int64_t el = taosGetTimestampMs() - st;
3,203✔
719
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
3,203✔
720
  return code;
3,202✔
721
}
722

723
bool streamTaskUpdateEpsetInfo(SStreamTask* pTask, SArray* pNodeList) {
220✔
724
  STaskExecStatisInfo* p = &pTask->execInfo;
220✔
725

726
  int32_t numOfNodes = taosArrayGetSize(pNodeList);
220✔
727
  int64_t prevTs = p->latestUpdateTs;
221✔
728

729
  p->latestUpdateTs = taosGetTimestampMs();
221✔
730
  p->updateCount += 1;
221✔
731
  stDebug("s-task:0x%x update task nodeEp epset, updatedNodes:%d, updateCount:%d, prevTs:%" PRId64, pTask->id.taskId,
221✔
732
          numOfNodes, p->updateCount, prevTs);
733

734
  bool updated = false;
220✔
735
  for (int32_t i = 0; i < numOfNodes; ++i) {
709✔
736
    SNodeUpdateInfo* pInfo = taosArrayGet(pNodeList, i);
488✔
737
    if (pInfo == NULL) {
489!
738
      continue;
×
739
    }
740

741
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
489✔
742
    if (code) {
489!
743
      stError("s-task:0x%x failed to update the task nodeEp epset, code:%s", pTask->id.taskId, tstrerror(code));
×
744
    }
745
  }
746

747
  return updated;
221✔
748
}
749

750
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
14,970✔
751
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,970✔
752
    return;
7,522✔
753
  }
754

755
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
7,448✔
756
  for (int32_t i = 0; i < size; ++i) {
28,696✔
757
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
21,247✔
758
    pInfo->stage = -1;
21,249✔
759
  }
760

761
  stDebug("s-task:%s reset all upstream tasks stage info", pTask->id.idStr);
7,449✔
762
}
763

764
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
24,825✔
765
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
24,825✔
766
  if (num == 0) {
24,828✔
767
    return;
12,445✔
768
  }
769

770
  for (int32_t i = 0; i < num; ++i) {
47,623✔
771
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
35,237✔
772
    pInfo->dataAllowed = true;
35,240✔
773
  }
774

775
  pTask->upstreamInfo.numOfClosed = 0;
12,386✔
776
  stDebug("s-task:%s opening up inputQ for %d upstream tasks", pTask->id.idStr, num);
12,386✔
777
}
778

779
void streamTaskCloseUpstreamInput(SStreamTask* pTask, int32_t taskId) {
8,101✔
780
  SStreamUpstreamEpInfo* pInfo = NULL;
8,101✔
781
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
8,101✔
782

783
  if ((pInfo != NULL) && pInfo->dataAllowed) {
8,086!
784
    pInfo->dataAllowed = false;
8,087✔
785
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
8,087!
786
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
8,088✔
787
    } else {
788
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
789
    }
790
  }
791
}
8,111✔
792

793
void streamTaskOpenUpstreamInput(SStreamTask* pTask, int32_t taskId) {
1✔
794
  SStreamUpstreamEpInfo* pInfo = NULL;
1✔
795
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
1✔
796

797
  if (pInfo != NULL && (!pInfo->dataAllowed)) {
1!
798
    int32_t t = atomic_sub_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
1✔
799
    stDebug("s-task:%s open inputQ for upstream:0x%x, remain closed:%d", pTask->id.idStr, taskId, t);
1!
800
    pInfo->dataAllowed = true;
1✔
801
  }
802
}
1✔
803

804
bool streamTaskIsAllUpstreamClosed(SStreamTask* pTask) {
×
805
  return pTask->upstreamInfo.numOfClosed == taosArrayGetSize(pTask->upstreamInfo.pList);
×
806
}
807

808
bool streamTaskSetSchedStatusWait(SStreamTask* pTask) {
104,630✔
809
  bool ret = false;
104,630✔
810

811
  streamMutexLock(&pTask->lock);
104,630✔
812
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
104,633✔
813
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
70,691✔
814
    ret = true;
70,691✔
815
  }
816

817
  streamMutexUnlock(&pTask->lock);
104,633✔
818
  return ret;
104,639✔
819
}
820

821
int8_t streamTaskSetSchedStatusActive(SStreamTask* pTask) {
69,485✔
822
  streamMutexLock(&pTask->lock);
69,485✔
823
  int8_t status = pTask->status.schedStatus;
69,526✔
824
  if (status == TASK_SCHED_STATUS__WAITING) {
69,526✔
825
    pTask->status.schedStatus = TASK_SCHED_STATUS__ACTIVE;
69,515✔
826
  }
827
  streamMutexUnlock(&pTask->lock);
69,526✔
828

829
  return status;
69,527✔
830
}
831

832
int8_t streamTaskSetSchedStatusInactive(SStreamTask* pTask) {
1,078✔
833
  streamMutexLock(&pTask->lock);
1,078✔
834
  int8_t status = pTask->status.schedStatus;
1,078✔
835
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
1,078✔
836
  streamMutexUnlock(&pTask->lock);
1,078✔
837

838
  return status;
1,078✔
839
}
840

841
int32_t streamTaskClearHTaskAttr(SStreamTask* pTask, int32_t resetRelHalt) {
7,021✔
842
  int32_t      code = 0;
7,021✔
843
  SStreamMeta* pMeta = pTask->pMeta;
7,021✔
844
  SStreamTask* pStreamTask = NULL;
7,021✔
845

846
  if (pTask->info.fillHistory == 0) {
7,021✔
847
    return code;
7,020✔
848
  }
849

850
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
1✔
851
  if (code == 0) {
6!
852
    stDebug("s-task:%s clear the related stream task:0x%x attr to fill-history task", pTask->id.idStr,
×
853
            (int32_t)pTask->streamTaskId.taskId);
854

855
    streamMutexLock(&(pStreamTask->lock));
×
856
    CLEAR_RELATED_FILLHISTORY_TASK(pStreamTask);
×
857

858
    if (resetRelHalt) {
×
859
      stDebug("s-task:0x%" PRIx64 " set the persistent status attr to be ready, prev:%s, status in sm:%s",
×
860
              pTask->streamTaskId.taskId, streamTaskGetStatusStr(pStreamTask->status.taskStatus),
861
              streamTaskGetStatus(pStreamTask).name);
862
      pStreamTask->status.taskStatus = TASK_STATUS__READY;
×
863
    }
864

865
    code = streamMetaSaveTask(pMeta, pStreamTask);
×
866
    streamMutexUnlock(&(pStreamTask->lock));
×
867

868
    streamMetaReleaseTask(pMeta, pStreamTask);
×
869
  }
870

871
  return code;
6✔
872
}
873

874
int32_t streamBuildAndSendDropTaskMsg(SMsgCb* pMsgCb, int32_t vgId, SStreamTaskId* pTaskId, int64_t resetRelHalt) {
6✔
875
  SVDropStreamTaskReq* pReq = rpcMallocCont(sizeof(SVDropStreamTaskReq));
6✔
876
  if (pReq == NULL) {
6!
877
    return terrno;
×
878
  }
879

880
  pReq->head.vgId = vgId;
6✔
881
  pReq->taskId = pTaskId->taskId;
6✔
882
  pReq->streamId = pTaskId->streamId;
6✔
883
  pReq->resetRelHalt = resetRelHalt;
6✔
884

885
  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_DROP, .pCont = pReq, .contLen = sizeof(SVDropStreamTaskReq)};
6✔
886
  int32_t code = tmsgPutToQueue(pMsgCb, WRITE_QUEUE, &msg);
6✔
887
  if (code != TSDB_CODE_SUCCESS) {
6!
888
    stError("vgId:%d failed to send drop task:0x%x msg, code:%s", vgId, pTaskId->taskId, tstrerror(code));
×
889
  } else {
890
    stDebug("vgId:%d build and send drop task:0x%x msg", vgId, pTaskId->taskId);
6!
891
  }
892

893
  return code;
6✔
894
}
895

896
int32_t streamSendChkptReportMsg(SStreamTask* pTask, SCheckpointInfo* pCheckpointInfo, int8_t dropRelHTask) {
6,030✔
897
  int32_t                code = 0;
6,030✔
898
  int32_t                tlen = 0;
6,030✔
899
  int32_t                vgId = pTask->pMeta->vgId;
6,030✔
900
  const char*            id = pTask->id.idStr;
6,030✔
901
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
6,030✔
902

903
  SCheckpointReport req = {.streamId = pTask->id.streamId,
6,030✔
904
                           .taskId = pTask->id.taskId,
6,030✔
905
                           .nodeId = vgId,
906
                           .dropHTask = dropRelHTask,
907
                           .transId = pActive->transId,
6,030✔
908
                           .checkpointId = pActive->activeId,
6,030✔
909
                           .checkpointVer = pCheckpointInfo->processedVer,
6,030✔
910
                           .checkpointTs = pCheckpointInfo->startTs};
6,030✔
911

912
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
6,030!
913
  if (code < 0) {
6,030!
914
    stError("s-task:%s vgId:%d encode stream task checkpoint-report failed, code:%s", id, vgId, tstrerror(code));
×
915
    return -1;
×
916
  }
917

918
  void* buf = rpcMallocCont(tlen);
6,030✔
919
  if (buf == NULL) {
6,027!
920
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId,
×
921
            tstrerror(TSDB_CODE_OUT_OF_MEMORY));
922
    return -1;
×
923
  }
924

925
  SEncoder encoder;
926
  tEncoderInit(&encoder, buf, tlen);
6,027✔
927
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
6,029!
928
    rpcFreeCont(buf);
×
929
    tEncoderClear(&encoder);
×
930
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId, tstrerror(code));
×
931
    return -1;
×
932
  }
933
  tEncoderClear(&encoder);
6,029✔
934

935
  SRpcMsg msg = {0};
6,029✔
936
  initRpcMsg(&msg, TDMT_MND_STREAM_CHKPT_REPORT, buf, tlen);
6,029✔
937
  stDebug("s-task:%s vgId:%d build and send task checkpoint-report to mnode", id, vgId);
6,029✔
938

939
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
6,029✔
940
}
941

942
STaskId streamTaskGetTaskId(const SStreamTask* pTask) {
77,888✔
943
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
77,888✔
944
  return id;
77,888✔
945
}
946

947
void streamTaskInitForLaunchHTask(SHistoryTaskInfo* pInfo) {
1,832✔
948
  pInfo->waitInterval = LAUNCH_HTASK_INTERVAL;
1,832✔
949
  pInfo->tickCount = ceil(LAUNCH_HTASK_INTERVAL / WAIT_FOR_MINIMAL_INTERVAL);
1,832✔
950
  pInfo->retryTimes = 0;
1,832✔
951
}
1,832✔
952

953
void streamTaskSetRetryInfoForLaunch(SHistoryTaskInfo* pInfo) {
1,828✔
954
  pInfo->waitInterval *= RETRY_LAUNCH_INTERVAL_INC_RATE;
1,828✔
955
  pInfo->tickCount = ceil(pInfo->waitInterval / WAIT_FOR_MINIMAL_INTERVAL);
1,828✔
956
  pInfo->retryTimes += 1;
1,828✔
957
}
1,828✔
958

959
void streamTaskStatusInit(STaskStatusEntry* pEntry, const SStreamTask* pTask) {
9,251✔
960
  pEntry->id.streamId = pTask->id.streamId;
9,251✔
961
  pEntry->id.taskId = pTask->id.taskId;
9,251✔
962
  pEntry->stage = -1;
9,251✔
963
  pEntry->nodeId = pTask->info.nodeId;
9,251✔
964
  pEntry->status = TASK_STATUS__STOP;
9,251✔
965
}
9,251✔
966

967
void streamTaskStatusCopy(STaskStatusEntry* pDst, const STaskStatusEntry* pSrc) {
60,812✔
968
  pDst->stage = pSrc->stage;
60,812✔
969
  pDst->inputQUsed = pSrc->inputQUsed;
60,812✔
970
  pDst->inputRate = pSrc->inputRate;
60,812✔
971
  pDst->procsTotal = pSrc->procsTotal;
60,812✔
972
  pDst->procsThroughput = pSrc->procsThroughput;
60,812✔
973
  pDst->outputTotal = pSrc->outputTotal;
60,812✔
974
  pDst->outputThroughput = pSrc->outputThroughput;
60,812✔
975
  pDst->processedVer = pSrc->processedVer;
60,812✔
976
  pDst->verRange = pSrc->verRange;
60,812✔
977
  pDst->sinkQuota = pSrc->sinkQuota;
60,812✔
978
  pDst->sinkDataSize = pSrc->sinkDataSize;
60,812✔
979
  pDst->checkpointInfo = pSrc->checkpointInfo;
60,812✔
980
  pDst->startCheckpointId = pSrc->startCheckpointId;
60,812✔
981
  pDst->startCheckpointVer = pSrc->startCheckpointVer;
60,812✔
982
  pDst->status = pSrc->status;
60,812✔
983

984
  pDst->startTime = pSrc->startTime;
60,812✔
985
  pDst->hTaskId = pSrc->hTaskId;
60,812✔
986
}
60,812✔
987

988
STaskStatusEntry streamTaskGetStatusEntry(SStreamTask* pTask) {
61,681✔
989
  SStreamMeta*         pMeta = pTask->pMeta;
61,681✔
990
  STaskExecStatisInfo* pExecInfo = &pTask->execInfo;
61,681✔
991

992
  STaskStatusEntry entry = {
185,043✔
993
      .id = streamTaskGetTaskId(pTask),
61,681✔
994
      .status = streamTaskGetStatus(pTask).state,
61,681✔
995
      .nodeId = pMeta->vgId,
61,681✔
996
      .stage = pMeta->stage,
61,681✔
997

998
      .inputQUsed = SIZE_IN_MiB(streamQueueGetItemSize(pTask->inputq.queue)),
61,681✔
999
      .startTime = pExecInfo->readyTs,
61,681✔
1000
      .checkpointInfo.latestId = pTask->chkInfo.checkpointId,
61,681✔
1001
      .checkpointInfo.latestVer = pTask->chkInfo.checkpointVer,
61,681✔
1002
      .checkpointInfo.latestTime = pTask->chkInfo.checkpointTime,
61,681✔
1003
      .checkpointInfo.latestSize = 0,
1004
      .checkpointInfo.remoteBackup = 0,
1005
      .checkpointInfo.consensusChkptId = 0,
1006
      .checkpointInfo.consensusTs = 0,
1007
      .hTaskId = pTask->hTaskInfo.id.taskId,
61,681✔
1008
      .procsTotal = SIZE_IN_MiB(pExecInfo->inputDataSize),
61,681✔
1009
      .outputTotal = SIZE_IN_MiB(pExecInfo->outputDataSize),
61,681✔
1010
      .procsThroughput = SIZE_IN_KiB(pExecInfo->procsThroughput),
61,681✔
1011
      .outputThroughput = SIZE_IN_KiB(pExecInfo->outputThroughput),
61,681✔
1012
      .startCheckpointId = pExecInfo->startCheckpointId,
61,681✔
1013
      .startCheckpointVer = pExecInfo->startCheckpointVer,
61,681✔
1014
  };
1015
  return entry;
61,681✔
1016
}
1017

1018
static int32_t taskPauseCallback(SStreamTask* pTask, void* param) {
1,408✔
1019
  SStreamMeta* pMeta = pTask->pMeta;
1,408✔
1020
  int32_t      code = 0;
1,408✔
1021

1022
  int32_t num = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
1,408✔
1023
  stInfo("vgId:%d s-task:%s pause stream task. paused task num:%d", pMeta->vgId, pTask->id.idStr, num);
1,419!
1024

1025
  // in case of fill-history task, stop the tsdb file scan operation.
1026
  if (pTask->info.fillHistory == 1) {
1,419✔
1027
    void* pExecutor = pTask->exec.pExecutor;
72✔
1028
    code = qKillTask(pExecutor, TSDB_CODE_SUCCESS);
72✔
1029
  }
1030

1031
  stDebug("vgId:%d s-task:%s set pause flag and pause task", pMeta->vgId, pTask->id.idStr);
1,419✔
1032
  return code;
1,420✔
1033
}
1034

1035
void streamTaskPause(SStreamTask* pTask) {
1,467✔
1036
  int32_t code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_PAUSE, taskPauseCallback, NULL);
1,467✔
1037
  if (code) {
1,482!
1038
    stError("s-task:%s failed handle pause event async, code:%s", pTask->id.idStr, tstrerror(code));
×
1039
  }
1040
}
1,482✔
1041

1042
void streamTaskResume(SStreamTask* pTask) {
2,669✔
1043
  SStreamTaskState prevState = streamTaskGetStatus(pTask);
2,669✔
1044

1045
  SStreamMeta* pMeta = pTask->pMeta;
2,673✔
1046
  int32_t      code = streamTaskRestoreStatus(pTask);
2,673✔
1047
  if (code == TSDB_CODE_SUCCESS) {
2,679✔
1048
    char*   pNew = streamTaskGetStatus(pTask).name;
1,403✔
1049
    int32_t num = atomic_sub_fetch_32(&pMeta->numOfPausedTasks, 1);
1,403✔
1050
    stInfo("s-task:%s status:%s resume from %s, paused task(s):%d", pTask->id.idStr, pNew, prevState.name, num);
1,402!
1051
  } else {
1052
    stInfo("s-task:%s status:%s no need to resume, paused task(s):%d", pTask->id.idStr, prevState.name,
1,276!
1053
           pMeta->numOfPausedTasks);
1054
  }
1055
}
2,685✔
1056

1057
bool streamTaskIsSinkTask(const SStreamTask* pTask) { return pTask->info.taskLevel == TASK_LEVEL__SINK; }
79,698✔
1058

1059
// this task must success
1060
int32_t streamTaskSendCheckpointReq(SStreamTask* pTask) {
4,474✔
1061
  int32_t     code;
1062
  int32_t     tlen = 0;
4,474✔
1063
  int32_t     vgId = pTask->pMeta->vgId;
4,474✔
1064
  const char* id = pTask->id.idStr;
4,474✔
1065

1066
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,474✔
1067
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,474!
1068
  if (code < 0) {
4,473!
1069
    stError("s-task:%s vgId:%d encode stream task req checkpoint failed, code:%s", id, vgId, tstrerror(code));
×
1070
    return TSDB_CODE_INVALID_MSG;
×
1071
  }
1072

1073
  void* buf = rpcMallocCont(tlen);
4,473✔
1074
  if (buf == NULL) {
4,477!
1075
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:Out of memory", id, vgId);
×
1076
    return terrno;
×
1077
  }
1078

1079
  SEncoder encoder;
1080
  tEncoderInit(&encoder, buf, tlen);
4,477✔
1081
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,477!
1082
    rpcFreeCont(buf);
×
1083
    tEncoderClear(&encoder);
×
1084
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:%s", id, vgId, tstrerror(code));
×
1085
    return code;
×
1086
  }
1087

1088
  tEncoderClear(&encoder);
4,477✔
1089

1090
  SRpcMsg msg = {0};
4,476✔
1091
  initRpcMsg(&msg, TDMT_MND_STREAM_REQ_CHKPT, buf, tlen);
4,476✔
1092
  stDebug("s-task:%s vgId:%d build and send task checkpoint req", id, vgId);
4,476✔
1093

1094
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
4,476✔
1095
}
1096

1097
void streamTaskGetUpstreamTaskEpInfo(SStreamTask* pTask, int32_t taskId, SStreamUpstreamEpInfo** pEpInfo) {
95,330✔
1098
  *pEpInfo = NULL;
95,330✔
1099

1100
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
95,330✔
1101
  for (int32_t i = 0; i < num; ++i) {
190,779✔
1102
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
190,743✔
1103
    if (pInfo == NULL) {
190,692!
1104
      return;
×
1105
    }
1106

1107
    if (pInfo->taskId == taskId) {
190,692✔
1108
      *pEpInfo = pInfo;
95,260✔
1109
      return;
95,260✔
1110
    }
1111
  }
1112

1113
  stError("s-task:%s failed to find upstream task:0x%x", pTask->id.idStr, taskId);
36!
1114
}
1115

1116
SEpSet* streamTaskGetDownstreamEpInfo(SStreamTask* pTask, int32_t taskId) {
×
1117
  if (pTask->info.taskLevel == TASK_OUTPUT__FIXED_DISPATCH) {
×
1118
    if (pTask->outputInfo.fixedDispatcher.taskId == taskId) {
×
1119
      return &pTask->outputInfo.fixedDispatcher.epSet;
×
1120
    }
1121
  } else if (pTask->info.taskLevel == TASK_OUTPUT__SHUFFLE_DISPATCH) {
×
1122
    SArray* pList = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
×
1123
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1124
      SVgroupInfo* pVgInfo = taosArrayGet(pList, i);
×
1125
      if (pVgInfo == NULL) {
×
1126
        continue;
×
1127
      }
1128

1129
      if (pVgInfo->taskId == taskId) {
×
1130
        return &pVgInfo->epSet;
×
1131
      }
1132
    }
1133
  }
1134

1135
  return NULL;
×
1136
}
1137

1138
int32_t createStreamTaskIdStr(int64_t streamId, int32_t taskId, const char** pId) {
14,951✔
1139
  char buf[128] = {0};
14,951✔
1140
  int32_t code = snprintf(buf, tListLen(buf),"0x%" PRIx64 "-0x%x", streamId, taskId);
14,951✔
1141
  if (code < 0 || code >= tListLen(buf)) {
14,951!
1142
    return TSDB_CODE_OUT_OF_BUFFER;
×
1143
  }
1144

1145
  *pId = taosStrdup(buf);
14,971!
1146

1147
  if (*pId == NULL) {
14,955!
1148
    return terrno;
×
1149
  } else {
1150
    return TSDB_CODE_SUCCESS;
14,955✔
1151
  }
1152
}
1153

1154
static int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
557✔
1155
  int32_t           code;
1156
  SStreamDataBlock* pData;
1157

1158
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
557✔
1159
  if (code) {
558!
1160
    stError("s-task:%s failed to allocated retrieve-block", pTask->id.idStr);
×
1161
    return terrno = code;
×
1162
  }
1163

1164
  pData->type = STREAM_INPUT__DATA_RETRIEVE;
558✔
1165
  pData->srcVgId = 0;
558✔
1166

1167
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
558✔
1168
  if (code != TSDB_CODE_SUCCESS) {
558!
1169
    stError("s-task:%s failed to convert retrieve-data to block, code:%s", pTask->id.idStr, tstrerror(code));
×
1170
    taosFreeQitem(pData);
×
1171
    return code;
×
1172
  }
1173

1174
  code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pData);
558✔
1175
  if (code != TSDB_CODE_SUCCESS) {
557!
1176
    stError("s-task:%s failed to put retrieve-block into inputQ, inputQ is full, discard the retrieve msg",
×
1177
            pTask->id.idStr);
1178
  }
1179

1180
  return code;
558✔
1181
}
1182

1183
int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
556✔
1184
  int32_t code = streamTaskEnqueueRetrieve(pTask, pReq);
556✔
1185
  if (code != 0) {
558!
1186
    return code;
×
1187
  }
1188
  return streamTrySchedExec(pTask);
558✔
1189
}
1190

1191
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
7,030✔
1192

1193
void streamTaskGetActiveCheckpointInfo(const SStreamTask* pTask, int32_t* pTransId, int64_t* pCheckpointId) {
×
1194
  if (pTransId != NULL) {
×
1195
    *pTransId = pTask->chkInfo.pActiveInfo->transId;
×
1196
  }
1197

1198
  if (pCheckpointId != NULL) {
×
1199
    *pCheckpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1200
  }
1201
}
×
1202

1203
int32_t streamTaskSetActiveCheckpointInfo(SStreamTask* pTask, int64_t activeCheckpointId) {
28✔
1204
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
28✔
1205
  return TSDB_CODE_SUCCESS;
28✔
1206
}
1207

1208
void streamTaskSetFailedChkptInfo(SStreamTask* pTask, int32_t transId, int64_t checkpointId) {
×
1209
  pTask->chkInfo.pActiveInfo->transId = transId;
×
1210
  pTask->chkInfo.pActiveInfo->activeId = checkpointId;
×
1211
  pTask->chkInfo.pActiveInfo->failedId = checkpointId;
×
1212
  stDebug("s-task:%s set failed checkpointId:%"PRId64, pTask->id.idStr, checkpointId);
×
1213
}
×
1214

1215
int32_t streamTaskCreateActiveChkptInfo(SActiveCheckpointInfo** pRes) {
15,005✔
1216
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
15,005!
1217
  if (pInfo == NULL) {
15,008!
1218
    return terrno;
×
1219
  }
1220

1221
  int32_t code = taosThreadMutexInit(&pInfo->lock, NULL);
15,008✔
1222
  if (code != TSDB_CODE_SUCCESS) {
15,013!
1223
    return code;
×
1224
  }
1225

1226
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
15,013✔
1227
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
15,016✔
1228
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
15,010✔
1229

1230
  *pRes = pInfo;
15,015✔
1231
  return code;
15,015✔
1232
}
1233

1234
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
62,665✔
1235
  if (pInfo == NULL) {
62,665✔
1236
    return;
47,908✔
1237
  }
1238

1239
  streamMutexDestroy(&pInfo->lock);
14,757✔
1240
  taosArrayDestroy(pInfo->pDispatchTriggerList);
14,758✔
1241
  pInfo->pDispatchTriggerList = NULL;
14,759✔
1242
  taosArrayDestroy(pInfo->pReadyMsgList);
14,759✔
1243
  pInfo->pReadyMsgList = NULL;
14,759✔
1244
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
14,759✔
1245
  pInfo->pCheckpointReadyRecvList = NULL;
14,759✔
1246

1247
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
14,759✔
1248
  if (pTriggerTmr->tmrHandle != NULL) {
14,759✔
1249
    streamTmrStop(pTriggerTmr->tmrHandle);
2,116✔
1250
    pTriggerTmr->tmrHandle = NULL;
2,116✔
1251
  }
1252

1253
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
14,759✔
1254
  if (pReadyTmr->tmrHandle != NULL) {
14,759✔
1255
    streamTmrStop(pReadyTmr->tmrHandle);
2,089✔
1256
    pReadyTmr->tmrHandle = NULL;
2,089✔
1257
  }
1258

1259
  taosMemoryFree(pInfo);
14,759!
1260
}
1261

1262
// NOTE: clear the checkpoint id, and keep the failed id
1263
// failedId for a task will increase as the checkpoint I.D. increases.
1264
void streamTaskClearActiveInfo(SActiveCheckpointInfo* pInfo) {
5,302✔
1265
  pInfo->activeId = 0;
5,302✔
1266
  pInfo->transId = 0;
5,302✔
1267
  pInfo->allUpstreamTriggerRecv = 0;
5,302✔
1268
  pInfo->dispatchTrigger = false;
5,302✔
1269

1270
  taosArrayClear(pInfo->pDispatchTriggerList);
5,302✔
1271
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,292✔
1272
}
5,289✔
1273

1274
const char* streamTaskGetExecType(int32_t type) {
101,005✔
1275
  switch (type) {
101,005!
1276
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
35,037✔
1277
      return "scan-wal-file";
35,037✔
1278
    case STREAM_EXEC_T_START_ALL_TASKS:
8,156✔
1279
      return "start-all-tasks";
8,156✔
1280
    case STREAM_EXEC_T_START_ONE_TASK:
5,787✔
1281
      return "start-one-task";
5,787✔
1282
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
35✔
1283
      return "restart-all-tasks";
35✔
1284
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,687✔
1285
      return "stop-all-tasks";
4,687✔
1286
    case STREAM_EXEC_T_RESUME_TASK:
8,320✔
1287
      return "resume-task-from-idle";
8,320✔
1288
    case STREAM_EXEC_T_ADD_FAILED_TASK:
5✔
1289
      return "record-start-failed-task";
5✔
1290
    case 0:
39,043✔
1291
      return "exec-all-tasks";
39,043✔
1292
    default:
×
1293
      return "invalid-exec-type";
×
1294
  }
1295
}
1296

1297
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
42,247✔
1298
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
42,247!
1299
  if (*pRefId != NULL) {
42,251!
1300
    **pRefId = pTask->id.refId;
42,251✔
1301
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
42,251✔
1302
    if (code != 0) {
42,256!
1303
      stError("s-task:%s failed to add refId:%" PRId64 " into refId-mgmt, code:%s", pTask->id.idStr, pTask->id.refId,
×
1304
              tstrerror(code));
1305
    }
1306
    return code;
42,256✔
1307
  } else {
1308
    stError("s-task:%s failed to alloc new ref id, code:%s", pTask->id.idStr, tstrerror(terrno));
×
1309
    return terrno;
×
1310
  }
1311
}
1312

1313
void streamTaskFreeRefId(int64_t* pRefId) {
40,127✔
1314
  if (pRefId == NULL) {
40,127✔
1315
    return;
3,034✔
1316
  }
1317

1318
  metaRefMgtRemove(pRefId);
37,093✔
1319
}
1320

1321

1322
int32_t tEncodeStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
140,018✔
1323
  int32_t code = 0;
140,018✔
1324
  int32_t lino;
1325

1326
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
140,018!
1327
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->ver));
280,052!
1328
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->id.streamId));
280,052!
1329
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->id.taskId));
280,052!
1330
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.trigger));
280,052!
1331
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.taskLevel));
280,052!
1332
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.type));
280,052!
1333
  TAOS_CHECK_EXIT(tEncodeI16(pEncoder, pTask->msgInfo.msgType));
280,052!
1334

1335
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.taskStatus));
280,052!
1336
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.schedStatus));
280,052!
1337

1338
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
280,052!
1339
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
280,052!
1340
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
140,026!
1341
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
140,024!
1342

1343
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointId));
280,056!
1344
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointVer));
280,056!
1345
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.fillHistory));
280,056!
1346

1347
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
280,056!
1348
  int32_t taskId = pTask->hTaskInfo.id.taskId;
140,028✔
1349
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
140,028!
1350

1351
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
280,056!
1352
  taskId = pTask->streamTaskId.taskId;
140,028✔
1353
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
140,028!
1354

1355
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.minVer));
280,056!
1356
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.maxVer));
280,056!
1357
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.skey));
280,056!
1358
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.ekey));
280,056!
1359

1360
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
140,028✔
1361
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
140,023!
1362
  for (int32_t i = 0; i < epSz; i++) {
332,990✔
1363
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
192,966✔
1364
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
192,952!
1365
  }
1366

1367
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
140,024✔
1368
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
148,568!
1369
  }
1370

1371
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
140,024✔
1372
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
137,638!
1373
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
137,638!
1374
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
137,638!
1375
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
71,205✔
1376
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
776!
1377
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
70,817!
1378
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1379
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
70,817✔
1380
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
22,064!
1381
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
22,064!
1382
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
11,032!
1383
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
59,785✔
1384
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
59,736!
1385
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
119,474!
1386
  }
1387
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
280,050!
1388
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
280,050!
1389
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
280,050!
1390

1391
  tEndEncode(pEncoder);
140,025✔
1392
_exit:
140,026✔
1393
  return code;
140,026✔
1394
}
1395

1396
int32_t tDecodeStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
48,895✔
1397
  int32_t taskId = 0;
48,895✔
1398
  int32_t code = 0;
48,895✔
1399
  int32_t lino;
1400

1401
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
48,895!
1402
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
97,800!
1403
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
48,898!
1404
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
2!
1405
  }
1406

1407
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
97,792!
1408
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
97,792!
1409
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
97,793!
1410
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
97,792!
1411
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
97,792!
1412
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
97,791!
1413

1414
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
97,782!
1415
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
97,780!
1416

1417
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
97,788!
1418
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
97,791!
1419
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
48,897!
1420
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
48,900!
1421

1422
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
97,799!
1423
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
97,796!
1424
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
97,785!
1425

1426
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
97,782!
1427
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
48,898!
1428
  pTask->hTaskInfo.id.taskId = taskId;
48,898✔
1429

1430
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
97,794!
1431
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
48,888!
1432
  pTask->streamTaskId.taskId = taskId;
48,888✔
1433

1434
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
97,782!
1435
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
97,786!
1436
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
97,782!
1437
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
97,771!
1438

1439
  int32_t epSz = -1;
48,881✔
1440
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &epSz) < 0);
48,888!
1441

1442
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
48,888!
1443
    TAOS_CHECK_EXIT(terrno);
×
1444
  }
1445
  for (int32_t i = 0; i < epSz; i++) {
115,892✔
1446
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
66,987!
1447
    if (pInfo == NULL) {
66,959!
1448
      TAOS_CHECK_EXIT(terrno);
×
1449
    }
1450
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
66,959!
1451
      taosMemoryFreeClear(pInfo);
×
1452
      goto _exit;
×
1453
    }
1454
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
133,989!
1455
      TAOS_CHECK_EXIT(terrno);
×
1456
    }
1457
  }
1458

1459
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
48,905✔
1460
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
51,553!
1461
  }
1462

1463
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
48,906✔
1464
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
48,046!
1465
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
24,024!
1466
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
24,023!
1467
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
24,023!
1468
      TAOS_CHECK_EXIT(terrno);
×
1469
    }
1470
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
48,019!
1471
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
24,884✔
1472
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
270!
1473
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
24,749!
1474
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1475
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
24,749✔
1476
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
7,380!
1477
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
7,380!
1478
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,690!
1479
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
21,059✔
1480
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
21,030!
1481
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
21,030!
1482
  }
1483
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
97,765!
1484
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
48,887!
1485
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
97,776!
1486
  }
1487
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
48,887!
1488

1489
  tEndDecode(pDecoder);
48,895✔
1490

1491
_exit:
48,893✔
1492
  return code;
48,893✔
1493
}
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