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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

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

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

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

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

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

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

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

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

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

67
  // check for the dispatch info and the upstream task info
68
  int32_t level = pTask->info.taskLevel;
10✔
69
  if (level == TASK_LEVEL__SOURCE) {
10✔
70
    code = streamTaskUpdateDownstreamInfo(pTask, nodeId, pEpSet, pUpdated);
4✔
71
  } else if (level == TASK_LEVEL__AGG) {
6✔
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);
4✔
76
  }
77

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

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

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

104
  return pEpInfo;
3,361✔
105
}
106

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

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

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

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

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

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

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

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

162
  code = addToTaskset(pTaskList, pTask);
3,217✔
163
  *p = pTask;
3,217✔
164

165
  return code;
3,217✔
166
}
167

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

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

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

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

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

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

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

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

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

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

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

222
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
11,696✔
223

224
  ETaskStatus status1 = TASK_STATUS__UNINIT;
11,696✔
225
  streamMutexLock(&pTask->lock);
11,696✔
226
  if (pTask->status.pSM != NULL) {
11,696✔
227
    SStreamTaskState status = streamTaskGetStatus(pTask);
6,508✔
228
    p = status.name;
6,508✔
229
    status1 = status.state;
6,508✔
230
  }
231
  streamMutexUnlock(&pTask->lock);
11,696✔
232

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

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

247
  if (pTask->hTaskInfo.pTimer != NULL) {
11,695✔
248
    streamTmrStop(pTask->hTaskInfo.pTimer);
185✔
249
    pTask->hTaskInfo.pTimer = NULL;
185✔
250
  }
251

252
  if (pTask->msgInfo.pRetryTmr != NULL) {
11,695✔
253
    streamTmrStop(pTask->msgInfo.pRetryTmr);
1,202✔
254
    pTask->msgInfo.pRetryTmr = NULL;
1,202✔
255
  }
256

257
  if (pTask->inputq.queue) {
11,695✔
258
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
3,257✔
259
    pTask->inputq.queue = NULL;
3,257✔
260
  }
261

262
  if (pTask->outputq.queue) {
11,695✔
263
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
3,257✔
264
    pTask->outputq.queue = NULL;
3,257✔
265
  }
266

267
  if (pTask->exec.qmsg) {
11,695✔
268
    taosMemoryFree(pTask->exec.qmsg);
6,671✔
269
  }
270

271
  if (pTask->exec.pExecutor) {
11,694✔
272
    qDestroyTask(pTask->exec.pExecutor);
1,819✔
273
    pTask->exec.pExecutor = NULL;
1,820✔
274
  }
275

276
  if (pTask->exec.pWalReader != NULL) {
11,695✔
277
    walCloseReader(pTask->exec.pWalReader);
1,667✔
278
    pTask->exec.pWalReader = NULL;
1,667✔
279
  }
280

281
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
11,695✔
282

283
  if (pTask->msgInfo.pData != NULL) {
11,696✔
284
    clearBufferedDispatchMsg(pTask);
14✔
285
  }
286

287
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
11,696✔
288
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
5,511!
289
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
5,511✔
290
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
5,511✔
291
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
5,511✔
292
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
6,185✔
293
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
4,070✔
294
  }
295

296
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
11,696✔
297
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
11,695✔
298

299
  if (pTask->pNameMap) {
11,696✔
300
    tSimpleHashCleanup(pTask->pNameMap);
404✔
301
  }
302

303
  streamDestroyStateMachine(pTask->status.pSM);
11,696✔
304
  pTask->status.pSM = NULL;
11,696✔
305

306
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
11,696✔
307

308
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
11,695✔
309
  streamMutexDestroy(&pTask->lock);
11,696✔
310

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

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

318
  if (pTask->id.idStr != NULL) {
11,696✔
319
    taosMemoryFree((void*)pTask->id.idStr);
6,474✔
320
  }
321

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

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

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

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

350
  if (pTask->backendPath != NULL) {
11,695✔
351
    taosMemoryFree(pTask->backendPath);
3,257✔
352
    pTask->backendPath = NULL;
3,257✔
353
  }
354
}
11,695✔
355

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

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

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

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

396
  if (pTask->info.fillHistory) {
3,286✔
397
    streamId = pTask->streamTaskId.streamId;
900✔
398
    taskId = pTask->streamTaskId.taskId;
900✔
399
  } else {
400
    streamId = pTask->id.streamId;
2,386✔
401
    taskId = pTask->id.taskId;
2,386✔
402
  }
403

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

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

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

419
  return 0;
3,286✔
420
}
421

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

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

440
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
3,286✔
441

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

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

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

489
  streamTaskOpenAllUpstreamInput(pTask);
3,286✔
490

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

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

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

533
  int32_t type = pTask->outputInfo.type;
18,440✔
534
  if (type == TASK_OUTPUT__TABLE) {
18,440✔
535
    return 0;
168✔
536
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
18,272✔
537
    return 1;
5,329✔
538
  } else {
539
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
12,943✔
540
    return taosArrayGetSize(vgInfo);
12,943✔
541
  }
542
}
543

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

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

552
  if (pTask->upstreamInfo.pList == NULL) {
3,361✔
553
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
1,560✔
554
  }
555

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

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

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

UNCOV
584
        epsetAssign(&pInfo->epSet, pEpSet);
×
UNCOV
585
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
×
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,
4!
589
                pInfo->taskId, nodeId, buf);
590
      }
591

592
      break;
4✔
593
    }
594
  }
595

596
  return code;
6✔
597
}
598

599
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
11,696✔
600
  if (pUpstreamInfo->pList != NULL) {
11,696✔
601
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
10,005✔
602
    pUpstreamInfo->numOfClosed = 0;
10,005✔
603
    pUpstreamInfo->pList = NULL;
10,005✔
604
  }
605
}
11,696✔
606

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

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

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

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

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

637
      if (pVgInfo->vgId == nodeId) {
4!
UNCOV
638
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
×
UNCOV
639
        if (!isEqual) {
×
UNCOV
640
          *pUpdated = true;
×
641

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

UNCOV
649
          epsetAssign(&pVgInfo->epSet, pEpSet);
×
UNCOV
650
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
×
651
                  nodeId, buf, tmp);
652
        } else {
UNCOV
653
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
×
654
                  pVgInfo->taskId, nodeId, buf);
655
        }
UNCOV
656
        break;
×
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;
6✔
684
}
685

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

691
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
1,143✔
692
  if (code) {
1,143!
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) {
1,143✔
698
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
684✔
699
    if (code != TSDB_CODE_SUCCESS) {
684!
700
      stError("s-task:%s failed to kill task related query handle, code:%s", id, tstrerror(code));
×
701
    }
702
  }
703

704
  while (!streamTaskIsIdle(pTask)) {
1,143!
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;
1,143✔
711
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
1,143✔
712
  return code;
1,143✔
713
}
714

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

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

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

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

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

742
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
3,286✔
743
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
3,286✔
744
    return;
1,681✔
745
  }
746

747
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
1,605✔
748
  for (int32_t i = 0; i < size; ++i) {
4,993✔
749
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
3,389✔
750
    pInfo->stage = -1;
3,388✔
751
  }
752

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

756
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
4,580✔
757
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
4,580✔
758
  if (num == 0) {
4,580✔
759
    return;
2,328✔
760
  }
761

762
  for (int32_t i = 0; i < num; ++i) {
6,883✔
763
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
4,632✔
764
    pInfo->dataAllowed = true;
4,631✔
765
  }
766

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

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

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

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

789
  if (pInfo != NULL && (!pInfo->dataAllowed)) {
1!
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
}
1✔
795

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

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

803
  streamMutexLock(&pTask->lock);
16,776✔
804
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
16,776✔
805
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
13,636✔
806
    ret = true;
13,636✔
807
  }
808

809
  streamMutexUnlock(&pTask->lock);
16,776✔
810
  return ret;
16,776✔
811
}
812

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

821
  return status;
13,369✔
822
}
823

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

830
  return status;
237✔
831
}
832

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

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

842
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
UNCOV
843
  if (code == 0) {
×
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

UNCOV
863
  return code;
×
864
}
865

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

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

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

UNCOV
885
  return code;
×
886
}
887

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

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

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

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

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

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

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

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

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

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

976
  pDst->startTime = pSrc->startTime;
7,441✔
977
  pDst->hTaskId = pSrc->hTaskId;
7,441✔
978
}
7,441✔
979

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

984
  STaskStatusEntry entry = {
23,118✔
985
      .id = streamTaskGetTaskId(pTask),
7,706✔
986
      .status = streamTaskGetStatus(pTask).state,
7,706✔
987
      .nodeId = pMeta->vgId,
7,706✔
988
      .stage = pMeta->stage,
7,706✔
989

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1092
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
11,675✔
1093
  for (int32_t i = 0; i < num; ++i) {
19,813!
1094
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
19,813✔
1095
    if (pInfo == NULL) {
19,813!
1096
      return;
×
1097
    }
1098

1099
    if (pInfo->taskId == taskId) {
19,813✔
1100
      *pEpInfo = pInfo;
11,675✔
1101
      return;
11,675✔
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) {
3,286✔
1131
  char buf[128] = {0};
3,286✔
1132
  sprintf(buf, "0x%" PRIx64 "-0x%x", streamId, taskId);
3,286✔
1133
  *pId = taosStrdup(buf);
3,286✔
1134

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

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

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

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

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

1179
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
1,288✔
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) {
30✔
1192
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
30✔
1193
  return TSDB_CODE_SUCCESS;
30✔
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) {
3,320✔
1204
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
3,320✔
1205
  if (pInfo == NULL) {
3,320!
1206
    return terrno;
×
1207
  }
1208

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

1214
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
3,320✔
1215
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
3,319✔
1216
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
3,320✔
1217

1218
  *pRes = pInfo;
3,320✔
1219
  return code;
3,320✔
1220
}
1221

1222
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
11,696✔
1223
  if (pInfo == NULL) {
11,696✔
1224
    return;
8,405✔
1225
  }
1226

1227
  streamMutexDestroy(&pInfo->lock);
3,291✔
1228
  taosArrayDestroy(pInfo->pDispatchTriggerList);
3,291✔
1229
  pInfo->pDispatchTriggerList = NULL;
3,291✔
1230
  taosArrayDestroy(pInfo->pReadyMsgList);
3,291✔
1231
  pInfo->pReadyMsgList = NULL;
3,291✔
1232
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
3,291✔
1233
  pInfo->pCheckpointReadyRecvList = NULL;
3,290✔
1234

1235
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
3,290✔
1236
  if (pTriggerTmr->tmrHandle != NULL) {
3,290✔
1237
    streamTmrStop(pTriggerTmr->tmrHandle);
427✔
1238
    pTriggerTmr->tmrHandle = NULL;
427✔
1239
  }
1240

1241
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
3,290✔
1242
  if (pReadyTmr->tmrHandle != NULL) {
3,290✔
1243
    streamTmrStop(pReadyTmr->tmrHandle);
425✔
1244
    pReadyTmr->tmrHandle = NULL;
425✔
1245
  }
1246

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

1258
  taosArrayClear(pInfo->pDispatchTriggerList);
430✔
1259
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
430✔
1260
}
430✔
1261

1262
const char* streamTaskGetExecType(int32_t type) {
40,940✔
1263
  switch (type) {
40,940!
1264
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
16,038✔
1265
      return "scan-wal-file";
16,038✔
1266
    case STREAM_EXEC_T_START_ALL_TASKS:
5,527✔
1267
      return "start-all-tasks";
5,527✔
1268
    case STREAM_EXEC_T_START_ONE_TASK:
2,328✔
1269
      return "start-one-task";
2,328✔
1270
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
3✔
1271
      return "restart-all-tasks";
3✔
1272
    case STREAM_EXEC_T_STOP_ALL_TASKS:
2,549✔
1273
      return "stop-all-tasks";
2,549✔
1274
    case STREAM_EXEC_T_RESUME_TASK:
2,321✔
1275
      return "resume-task-from-idle";
2,321✔
1276
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
1277
      return "record-start-failed-task";
×
1278
    case 0:
12,220✔
1279
      return "exec-all-tasks";
12,220✔
1280
    default:
×
1281
      return "invalid-exec-type";
×
1282
  }
1283
}
1284

1285
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
8,411✔
1286
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
8,411✔
1287
  if (*pRefId != NULL) {
8,411!
1288
    **pRefId = pTask->id.refId;
8,411✔
1289
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
8,411✔
1290
    if (code != 0) {
8,411!
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;
8,411✔
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) {
7,849✔
1302
  if (pRefId == NULL) {
7,849✔
1303
    return;
614✔
1304
  }
1305

1306
  metaRefMgtRemove(pRefId);
7,235✔
1307
}
1308

1309

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

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

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

1326
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
52,342!
1327
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
52,342!
1328
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
26,171!
1329
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
26,171!
1330

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

1335
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
52,342!
1336
  int32_t taskId = pTask->hTaskInfo.id.taskId;
26,171✔
1337
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
26,171!
1338

1339
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
52,342!
1340
  taskId = pTask->streamTaskId.taskId;
26,171✔
1341
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
26,171!
1342

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

1348
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
26,171✔
1349
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
26,171!
1350
  for (int32_t i = 0; i < epSz; i++) {
53,321✔
1351
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
27,149✔
1352
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
27,150!
1353
  }
1354

1355
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
26,172✔
1356
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
29,958!
1357
  }
1358

1359
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
26,172✔
1360
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
24,770!
1361
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
24,770!
1362
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
24,770!
1363
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
13,787✔
1364
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
452!
1365
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
13,561!
1366
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1367
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
13,561✔
1368
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
9,112!
1369
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
9,112!
1370
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
4,556!
1371
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
9,005✔
1372
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
8,943!
1373
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
17,888!
1374
  }
1375
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
52,346!
1376
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
52,346!
1377
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
52,346!
1378

1379
  tEndEncode(pEncoder);
26,173✔
1380
_exit:
26,170✔
1381
  return code;
26,170✔
1382
}
1383

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

1389
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
8,485!
1390
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
16,970!
1391
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
8,485!
1392
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
1393
  }
1394

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

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

1405
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
16,970!
1406
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
16,970!
1407
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
8,485!
1408
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
8,485!
1409

1410
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
16,969!
1411
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
16,968!
1412
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
16,967!
1413

1414
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
16,966!
1415
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
8,483!
1416
  pTask->hTaskInfo.id.taskId = taskId;
8,483✔
1417

1418
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
16,967!
1419
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
8,484!
1420
  pTask->streamTaskId.taskId = taskId;
8,484✔
1421

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

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

1430
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
8,484!
1431
    TAOS_CHECK_EXIT(terrno);
×
1432
  }
1433
  for (int32_t i = 0; i < epSz; i++) {
17,051✔
1434
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
8,565✔
1435
    if (pInfo == NULL) {
8,566!
1436
      TAOS_CHECK_EXIT(terrno);
×
1437
    }
1438
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
8,566!
1439
      taosMemoryFreeClear(pInfo);
×
1440
      goto _exit;
×
1441
    }
1442
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
17,132!
1443
      TAOS_CHECK_EXIT(terrno);
×
1444
    }
1445
  }
1446

1447
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
8,486✔
1448
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
9,657!
1449
  }
1450

1451
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
8,485✔
1452
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
8,014!
1453
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
4,007!
1454
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
4,006✔
1455
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
4,007!
UNCOV
1456
      TAOS_CHECK_EXIT(terrno);
×
1457
    }
1458
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
8,012!
1459
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
4,478✔
1460
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
170!
1461
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
4,393!
1462
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1463
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
4,393✔
1464
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
2,846!
1465
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
2,846!
1466
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
1,423!
1467
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
2,970✔
1468
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
2,950!
1469
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
2,950!
1470
  }
1471
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
16,968!
1472
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
8,485!
1473
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
16,970!
1474
  }
1475
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
8,485!
1476

1477
  tEndDecode(pDecoder);
8,485✔
1478

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