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

taosdata / TDengine / #4131

20 May 2025 07:22AM UTC coverage: 63.096% (+0.7%) from 62.384%
#4131

push

travis-ci

web-flow
docs(datain): add topic meta options docs in tmq (#31147)

157751 of 318088 branches covered (49.59%)

Branch coverage included in aggregate %.

243052 of 317143 relevant lines covered (76.64%)

18743283.33 hits per line

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

59.17
/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 "streamMsg.h"
21
#include "streamsm.h"
22
#include "tmisce.h"
23
#include "tstream.h"
24
#include "ttimer.h"
25
#include "wal.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) {
14,428✔
33
  int32_t childId = taosArrayGetSize(pArray);
14,428✔
34
  pTask->info.selfChildId = childId;
14,428✔
35
  void* p = taosArrayPush(pArray, &pTask);
14,428✔
36
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
14,428!
37
}
38

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

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

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

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

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

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

98
  pEpInfo->childId = pTask->info.selfChildId;
19,982✔
99
  pEpInfo->epSet = pTask->info.epSet;
19,982✔
100
  pEpInfo->nodeId = pTask->info.nodeId;
19,982✔
101
  pEpInfo->taskId = pTask->id.taskId;
19,982✔
102
  pEpInfo->dataAllowed = true;
19,982✔
103
  pEpInfo->stage = -1;
19,982✔
104
  pEpInfo->lastMsgId = -1;
19,982✔
105

106
  return pEpInfo;
19,982✔
107
}
108

109
int32_t tNewStreamTask(int64_t streamId, int8_t taskLevel, SEpSet* pEpset, EStreamTaskType type, int32_t trigger,
14,428✔
110
                       int64_t triggerParam, SArray* pTaskList, bool hasFillhistory, int8_t subtableWithoutMd5,
111
                       int8_t hasAggTasks, SStreamTask** p) {
112
  *p = NULL;
14,428✔
113

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

121
  pTask->ver = SSTREAM_TASK_VER;
14,428✔
122
  pTask->id.taskId = tGenIdPI32();
14,428✔
123
  pTask->id.streamId = streamId;
14,428✔
124

125
  pTask->info.taskLevel = taskLevel;
14,428✔
126
  pTask->info.fillHistory = type;
14,428✔
127
  pTask->info.trigger = trigger;
14,428✔
128
  pTask->info.hasAggTasks = hasAggTasks;
14,428✔
129
  pTask->info.delaySchedParam = triggerParam;
14,428✔
130
  pTask->subtableWithoutMd5 = subtableWithoutMd5;
14,428✔
131

132
  int32_t code = streamCreateStateMachine(pTask);
14,428✔
133
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
14,428!
134
    taosMemoryFreeClear(pTask);
×
135
    return code;
×
136
  }
137

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

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

151
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
14,428✔
152
  pTask->status.taskStatus =
14,428✔
153
      (pTask->info.fillHistory == STREAM_HISTORY_TASK) ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
14,428✔
154
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,428✔
155
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,428✔
156

157
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
14,428✔
158

159
  if ((pTask->info.fillHistory == STREAM_HISTORY_TASK) && !hasFillhistory) {
14,428!
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);
14,428✔
165

166
  code = addToTaskset(pTaskList, pTask);
14,428✔
167
  *p = pTask;
14,428✔
168

169
  return code;
14,428✔
170
}
171

172
int32_t tDecodeStreamTaskChkInfo(SDecoder* pDecoder, SCheckpointInfo* pChkpInfo) {
776✔
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;
776!
180
  if (tDecodeI64(pDecoder, &pChkpInfo->msgVer) < 0) return -1;
1,552✔
181
  // if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
182

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

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

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

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

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

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

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

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

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

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

226
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
59,251✔
227

228
  ETaskStatus status1 = TASK_STATUS__UNINIT;
59,251✔
229
  if (pTask->status.pSM != NULL) {
59,251✔
230
    streamMutexLock(&pTask->lock);
29,622✔
231
    SStreamTaskState status = streamTaskGetStatus(pTask);
29,636✔
232
    p = status.name;
29,623✔
233
    status1 = status.state;
29,623✔
234
    streamMutexUnlock(&pTask->lock);
29,623✔
235
  }
236

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

239
  SCheckpointInfo* pCkInfo = &pTask->chkInfo;
59,268✔
240
  stDebug("s-task:0x%x task exec summary: create:%" PRId64 ", init:%" PRId64 ", start:%" PRId64
59,268✔
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) {
59,268✔
247
    streamTmrStop(pTask->schedInfo.pDelayTimer);
785✔
248
    pTask->schedInfo.pDelayTimer = NULL;
785✔
249
  }
250

251
  if (pTask->hTaskInfo.pTimer != NULL) {
59,268✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
2,210✔
253
    pTask->hTaskInfo.pTimer = NULL;
2,203✔
254
  }
255

256
  if (pTask->msgInfo.pRetryTmr != NULL) {
59,261✔
257
    streamTmrStop(pTask->msgInfo.pRetryTmr);
5,350✔
258
    pTask->msgInfo.pRetryTmr = NULL;
5,351✔
259
  }
260

261
  if (pTask->inputq.queue) {
59,262✔
262
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
15,172✔
263
    pTask->inputq.queue = NULL;
15,173✔
264
  }
265

266
  if (pTask->outputq.queue) {
59,263✔
267
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
15,175✔
268
    pTask->outputq.queue = NULL;
15,180✔
269
  }
270

271
  if (pTask->exec.qmsg) {
59,268✔
272
    taosMemoryFree(pTask->exec.qmsg);
31,138!
273
  }
274

275
  if (pTask->exec.pExecutor) {
59,268✔
276
    qDestroyTask(pTask->exec.pExecutor);
7,688✔
277
    pTask->exec.pExecutor = NULL;
7,688✔
278
  }
279

280
  if (pTask->exec.pWalReader != NULL) {
59,268✔
281
    walCloseReader(pTask->exec.pWalReader);
7,618✔
282
    pTask->exec.pWalReader = NULL;
7,618✔
283
  }
284

285
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
59,268✔
286

287
  if (pTask->msgInfo.pData != NULL) {
59,286✔
288
    clearBufferedDispatchMsg(pTask);
23✔
289
  }
290

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
59,287✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
29,356!
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
29,363!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
29,356✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
29,363✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
29,931✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
25,812✔
298
  } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
4,119!
299
    taosArrayDestroy(pTask->outputInfo.vtableMapDispatcher.taskInfos);
×
300
    tSimpleHashCleanup(pTask->outputInfo.vtableMapDispatcher.vtableMap);
×
301
  }
302

303
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
59,288✔
304
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
59,291✔
305

306
  if (pTask->pNameMap) {
59,280✔
307
    tSimpleHashCleanup(pTask->pNameMap);
2,201✔
308
  }
309

310
  if (pTask->status.pSM != NULL) {
59,280✔
311
    streamMutexDestroy(&pTask->lock);
29,636✔
312
    streamMutexDestroy(&pTask->msgInfo.lock);
29,634✔
313
    streamMutexDestroy(&pTask->taskCheckInfo.checkInfoLock);
29,631✔
314
  }
315

316
  taosArrayDestroy(pTask->pVTables);
59,280✔
317
  pTask->pVTables = NULL;
59,282✔
318

319
  streamDestroyStateMachine(pTask->status.pSM);
59,282✔
320
  pTask->status.pSM = NULL;
59,294✔
321

322
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
59,294✔
323

324
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
59,294!
325

326
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
59,293✔
327
  pTask->msgInfo.pSendInfo = NULL;
59,296✔
328

329
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
59,296✔
330
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
59,293✔
331

332
  if (pTask->id.idStr != NULL) {
59,293✔
333
    taosMemoryFree((void*)pTask->id.idStr);
29,605!
334
  }
335

336
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
59,296✔
337
  pTask->chkInfo.pActiveInfo = NULL;
59,295✔
338

339
  taosArrayDestroyP(pTask->notifyInfo.pNotifyAddrUrls, NULL);
59,295✔
340
  taosMemoryFreeClear(pTask->notifyInfo.streamName);
59,297!
341
  taosMemoryFreeClear(pTask->notifyInfo.stbFullName);
59,297!
342
  tDeleteSchemaWrapper(pTask->notifyInfo.pSchemaWrapper);
59,297!
343

344
  pTask->notifyEventStat = (STaskNotifyEventStat){0};
59,290✔
345

346
  taosMemoryFree(pTask);
59,290!
347
  stDebug("s-task:0x%x free task completed", taskId);
59,295✔
348
}
59,295✔
349

350
void streamFreeTaskState(SStreamTask* pTask, int8_t remove) {
59,287✔
351
  stDebug("s-task:0x%x start to free task state/backend", pTask->id.taskId);
59,287✔
352
  if (pTask->pState != NULL) {
59,293✔
353
    stDebug("s-task:0x%x start to free task state", pTask->id.taskId);
7,687✔
354
    streamStateClose(pTask->pState, remove);
7,687✔
355

356
    if (remove) taskDbSetClearFileFlag(pTask->pBackend);
7,687✔
357
    taskDbRemoveRef(pTask->pBackend);
7,688✔
358
    pTask->pBackend = NULL;
7,686✔
359
    pTask->pState = NULL;
7,686✔
360

361
  } else {
362
    stDebug("s-task:0x%x task state is NULL, may del backend:%s", pTask->id.taskId,
51,606✔
363
            pTask->backendPath ? pTask->backendPath : "NULL");
364
    if (remove) {
51,606✔
365
      if (pTask->backendPath != NULL) {
3,824!
366
        stDebug("s-task:0x%x task state is NULL, do del backend:%s", pTask->id.taskId, pTask->backendPath);
3,827✔
367
        taosRemoveDir(pTask->backendPath);
3,827✔
368
      }
369
    }
370
  }
371

372
  if (pTask->backendPath != NULL) {
59,290✔
373
    taosMemoryFree(pTask->backendPath);
15,174!
374
    pTask->backendPath = NULL;
15,179✔
375
  }
376
  // clear recal backend
377

378
  if (pTask->pRecalState != NULL) {
59,295!
379
    stDebug("s-task:0x%x start to free recal task state", pTask->id.taskId);
×
380
    streamStateClose(pTask->pRecalState, remove);
×
381

382
    if (remove) taskDbSetClearFileFlag(pTask->pRecalBackend);
×
383
    taskDbRemoveRef(pTask->pRecalBackend);
×
384
    pTask->pRecalBackend = NULL;
×
385
    pTask->pRecalState = NULL;
×
386

387
  }  // else {
388
     //  stDebug("s-task:0x%x task state is NULL, may del backend:%s", pTask->id.taskId,
389
     //          pTask->backendPath ? pTask->backendPath : "NULL");
390
     //  if (remove) {
391
     //    if (pTask->backendPath != NULL) {
392
     //      stDebug("s-task:0x%x task state is NULL, do del backend:%s", pTask->id.taskId, pTask->backendPath);
393
     //      taosRemoveDir(pTask->backendPath);
394
     //    }
395
     //  }
396
  //}
397
  // if (pTask->backendPath != NULL) {
398
  //   taosMemoryFree(pTask->backendPath);
399
  //   pTask->backendPath = NULL;
400
  // }
401
}
59,295✔
402

403
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
15,158✔
404
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
15,158✔
405
  SDataRange*      pRange = &pTask->dataRange;
15,158✔
406

407
  // only set the version info for stream tasks without fill-history task
408
  if ((pTask->info.fillHistory == 0) && (!HAS_RELATED_FILLHISTORY_TASK(pTask))) {
15,158✔
409
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
5,656✔
410
    pChkInfo->processedVer = ver - 1;   // already processed version
5,656✔
411
    pChkInfo->nextProcessVer = ver;     // next processed version
5,656✔
412

413
    pRange->range.maxVer = ver;
5,656✔
414
    pRange->range.minVer = ver;
5,656✔
415
  } else {
416
    // the initial value of processedVer/nextProcessVer/checkpointVer for stream task with related fill-history task
417
    // is set at the mnode.
418
    if (pTask->info.fillHistory == 1) {
9,502✔
419
      pChkInfo->checkpointVer = pRange->range.maxVer;
4,761✔
420
      pChkInfo->processedVer = pRange->range.maxVer;
4,761✔
421
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
4,761✔
422
    } else {
423
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
4,741✔
424
      pChkInfo->processedVer = pRange->range.minVer - 1;
4,741✔
425
      pChkInfo->nextProcessVer = pRange->range.minVer;
4,741✔
426

427
      {  // for compatible purpose, remove it later
428
        if (pRange->range.minVer == 0) {
4,741✔
429
          pChkInfo->checkpointVer = 0;
2,398✔
430
          pChkInfo->processedVer = 0;
2,398✔
431
          pChkInfo->nextProcessVer = 1;
2,398✔
432
          stDebug("s-task:%s update the processedVer to 0 from -1 due to compatible purpose", pTask->id.idStr);
2,398✔
433
        }
434
      }
435
    }
436
  }
437
}
15,158✔
438

439
int32_t streamTaskSetBackendPath(SStreamTask* pTask) {
15,174✔
440
  int64_t streamId = 0;
15,174✔
441
  int32_t taskId = 0;
15,174✔
442

443
  if (pTask->info.fillHistory) {
15,174✔
444
    streamId = pTask->streamTaskId.streamId;
4,761✔
445
    taskId = pTask->streamTaskId.taskId;
4,761✔
446
  } else {
447
    streamId = pTask->id.streamId;
10,413✔
448
    taskId = pTask->id.taskId;
10,413✔
449
  }
450

451
  char    id[128] = {0};
15,174✔
452
  int32_t nBytes = snprintf(id, tListLen(id), "0x%" PRIx64 "-0x%x", streamId, taskId);
15,174✔
453
  if (nBytes < 0 || nBytes >= sizeof(id)) {
15,174!
454
    return TSDB_CODE_OUT_OF_BUFFER;
×
455
  }
456

457
  int32_t len = strlen(pTask->pMeta->path);
15,181✔
458
  pTask->backendPath = (char*)taosMemoryMalloc(len + nBytes + 2);
15,181!
459
  if (pTask->backendPath == NULL) {
15,183!
460
    return terrno;
×
461
  }
462

463
  int32_t code = snprintf(pTask->backendPath, len + nBytes + 2, "%s%s%s", pTask->pMeta->path, TD_DIRSEP, id);
15,183✔
464
  if (code < 0 || code >= len + nBytes + 2) {
15,183!
465
    stError("s-task:%s failed to set backend path:%s, code: out of buffer", pTask->id.idStr, pTask->backendPath);
4!
466
    return TSDB_CODE_OUT_OF_BUFFER;
×
467
  } else {
468
    stDebug("s-task:%s set backend path:%s", pTask->id.idStr, pTask->backendPath);
15,179✔
469
    return 0;
15,178✔
470
  }
471
}
472

473
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
15,163✔
474
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
15,163✔
475
  if (code) {
15,152!
476
    stError("0x%x failed create stream task id str, code:%s", pTask->id.taskId, tstrerror(code));
×
477
    return code;
×
478
  }
479

480
  pTask->id.refId = 0;
15,152✔
481
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
15,152✔
482
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
15,152✔
483

484
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
15,152✔
485
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
15,158✔
486
  if (code1 || code2) {
15,178!
487
    stError("s-task:%s failed to prepare the input/output queue, initialize task failed", pTask->id.idStr);
×
488
    return TSDB_CODE_OUT_OF_MEMORY;
×
489
  }
490

491
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
15,178✔
492

493
  code = streamCreateStateMachine(pTask);
15,178✔
494
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
15,162!
495
    stError("s-task:%s failed create state-machine for stream task, initialization failed, code:%s", pTask->id.idStr,
×
496
            tstrerror(code));
497
    return code;
×
498
  }
499

500
  pTask->execInfo.created = taosGetTimestampMs();
15,171✔
501
  setInitialVersionInfo(pTask, ver);
15,171✔
502

503
  pTask->pMeta = pMeta;
15,168✔
504
  pTask->pMsgCb = pMsgCb;
15,168✔
505
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
15,168✔
506
  if (pTask->msgInfo.pSendInfo == NULL) {
15,176!
507
    stError("s-task:%s failed to create sendInfo struct for stream task, code:Out of memory", pTask->id.idStr);
×
508
    return terrno;
×
509
  }
510

511
  code = taosThreadMutexInit(&pTask->msgInfo.lock, NULL);
15,176✔
512
  if (code) {
15,169!
513
    stError("s-task:0x%x failed to init msgInfo mutex, code:%s", pTask->id.taskId, tstrerror(code));
×
514
    return code;
×
515
  }
516

517
  TdThreadMutexAttr attr = {0};
15,169✔
518
  code = taosThreadMutexAttrInit(&attr);
15,169✔
519
  if (code != 0) {
15,158!
520
    stError("s-task:%s initElapsed mutex attr failed, code:%s", pTask->id.idStr, tstrerror(code));
×
521
    return code;
×
522
  }
523

524
  code = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
15,158✔
525
  if (code != 0) {
15,148!
526
    stError("s-task:%s set mutex attr recursive, code:%s", pTask->id.idStr, tstrerror(code));
×
527
    return code;
×
528
  }
529

530
  code = taosThreadMutexInit(&pTask->lock, &attr);
15,148✔
531
  if (code) {
15,159!
532
    return code;
×
533
  }
534

535
  code = taosThreadMutexAttrDestroy(&attr);
15,159✔
536
  if (code) {
15,144!
537
    return code;
×
538
  }
539

540
  streamTaskOpenAllUpstreamInput(pTask);
15,144✔
541

542
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
15,148✔
543
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
15,148!
544
  if (pOutputInfo->pTokenBucket == NULL) {
15,167!
545
    stError("s-task:%s failed to prepare the tokenBucket, code:%s", pTask->id.idStr, tstrerror(terrno));
×
546
    return terrno;
×
547
  }
548

549
  // 2MiB per second for sink task
550
  // 50 times sink operator per second
551
  code = streamTaskInitTokenBucket(pOutputInfo->pTokenBucket, 35, 35, tsSinkDataRate, pTask->id.idStr);
15,167✔
552
  if (code) {
15,169!
553
    return code;
×
554
  }
555

556
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
15,169✔
557
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
15,180!
558
    stError("s-task:%s failed to prepare downstreamUpdateList, code:%s", pTask->id.idStr, tstrerror(terrno));
×
559
    return terrno;
×
560
  }
561

562
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
15,180✔
563
  if (pTask->taskCheckInfo.pList == NULL) {
15,169!
564
    stError("s-task:%s failed to prepare taskCheckInfo list, code:%s", pTask->id.idStr, tstrerror(terrno));
×
565
    return terrno;
×
566
  }
567

568
  code = taosThreadMutexInit(&pTask->taskCheckInfo.checkInfoLock, NULL);
15,169✔
569
  if (code) {
15,165!
570
    return code;
×
571
  }
572

573
  if (pTask->chkInfo.pActiveInfo == NULL) {
15,165!
574
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
15,167✔
575
    if (code) {
15,173!
576
      stError("s-task:%s failed to create active checkpoint info, code:%s", pTask->id.idStr, tstrerror(code));
×
577
      return code;
×
578
    }
579
  }
580

581
  return streamTaskSetBackendPath(pTask);
15,171✔
582
}
583

584
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
113,284✔
585
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
113,284✔
586
    return 0;
7,027✔
587
  }
588

589
  int32_t type = pTask->outputInfo.type;
106,257✔
590
  if (type == TASK_OUTPUT__TABLE) {
106,257✔
591
    return 0;
273✔
592
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
105,984✔
593
    return 1;
11,295✔
594
  } else if (type == TASK_OUTPUT__VTABLE_MAP) {
94,689!
595
    SArray* pTaskInfos = pTask->outputInfo.vtableMapDispatcher.taskInfos;
×
596
    return taosArrayGetSize(pTaskInfos);
×
597
  } else {
598
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
94,689✔
599
    return taosArrayGetSize(vgInfo);
94,689✔
600
  }
601
}
602

603
int32_t streamTaskGetNumOfUpstream(const SStreamTask* pTask) { return taosArrayGetSize(pTask->upstreamInfo.pList); }
16,267✔
604

605
int32_t streamTaskSetUpstreamInfo(SStreamTask* pTask, const SStreamTask* pUpstreamTask) {
19,982✔
606
  SStreamUpstreamEpInfo* pEpInfo = createStreamTaskEpInfo(pUpstreamTask);
19,982✔
607
  if (pEpInfo == NULL) {
19,982!
608
    return terrno;
×
609
  }
610

611
  if (pTask->upstreamInfo.pList == NULL) {
19,982✔
612
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
7,181✔
613
  }
614

615
  void* p = taosArrayPush(pTask->upstreamInfo.pList, &pEpInfo);
19,982✔
616
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
19,982!
617
}
618

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

628
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
572✔
629
  for (int32_t i = 0; i < numOfUpstream; ++i) {
1,104✔
630
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
1,021✔
631
    if (pInfo->nodeId == nodeId) {
1,021✔
632
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
489✔
633
      if (!equal) {
489✔
634
        *pUpdated = true;
24✔
635

636
        char tmp[512] = {0};
24✔
637
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
24✔
638
        if (code != 0) {  // print error and continue
24!
639
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
640
          return code;
×
641
        }
642

643
        epsetAssign(&pInfo->epSet, pEpSet);
24✔
644
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
24!
645
                pInfo->taskId, nodeId, buf, tmp);
646
      } else {
647
        stDebug("s-task:0x%x not update upstreamInfo, since identical, task:0x%x(nodeId:%d) epset:%s", pTask->id.taskId,
465!
648
                pInfo->taskId, nodeId, buf);
649
      }
650

651
      break;
489✔
652
    }
653
  }
654

655
  return code;
572✔
656
}
657

658
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
59,283✔
659
  if (pUpstreamInfo->pList != NULL) {
59,283✔
660
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
51,992✔
661
    pUpstreamInfo->numOfClosed = 0;
52,001✔
662
    pUpstreamInfo->pList = NULL;
52,001✔
663
  }
664
}
59,292✔
665

666
void streamTaskSetFixedDownstreamInfo(SStreamTask* pTask, const SStreamTask* pDownstreamTask) {
886✔
667
  STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
886✔
668
  pDispatcher->taskId = pDownstreamTask->id.taskId;
886✔
669
  pDispatcher->nodeId = pDownstreamTask->info.nodeId;
886✔
670
  pDispatcher->epSet = pDownstreamTask->info.epSet;
886✔
671

672
  pTask->outputInfo.type = TASK_OUTPUT__FIXED_DISPATCH;
886✔
673
  pTask->msgInfo.msgType = TDMT_STREAM_TASK_DISPATCH;
886✔
674
}
886✔
675

676
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
102✔
677
  char    buf[512] = {0};
102✔
678
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
102✔
679
  if (code != 0) {                                        // print error and continue
102!
680
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
681
    return code;
×
682
  }
683

684
  int32_t id = pTask->id.taskId;
102✔
685
  int8_t  type = pTask->outputInfo.type;
102✔
686

687
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
102✔
688
    SArray* pVgs = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
71✔
689

690
    for (int32_t i = 0; i < taosArrayGetSize(pVgs); i++) {
138✔
691
      SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
136✔
692
      if (pVgInfo == NULL) {
136!
693
        continue;
×
694
      }
695

696
      if (pVgInfo->vgId == nodeId) {
136✔
697
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
69✔
698
        if (!isEqual) {
69✔
699
          *pUpdated = true;
18✔
700

701
          char tmp[512] = {0};
18✔
702
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
18✔
703
          if (code != 0) {  // print error and continue
18!
704
            stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
705
            return code;
×
706
          }
707

708
          epsetAssign(&pVgInfo->epSet, pEpSet);
18✔
709
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
18!
710
                  nodeId, buf, tmp);
711
        } else {
712
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
51!
713
                  pVgInfo->taskId, nodeId, buf);
714
        }
715
        break;
69✔
716
      }
717
    }
718
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
31!
719
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
31✔
720
    if (pDispatcher->nodeId == nodeId) {
31✔
721
      bool equal = isEpsetEqual(&pDispatcher->epSet, pEpSet);
4✔
722
      if (!equal) {
4!
723
        *pUpdated = true;
×
724

725
        char tmp[512] = {0};
×
726
        code = epsetToStr(&pDispatcher->epSet, tmp, tListLen(tmp));
×
727
        if (code != 0) {  // print error and continue
×
728
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
729
          return code;
×
730
        }
731

732
        epsetAssign(&pDispatcher->epSet, pEpSet);
×
733
        stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pDispatcher->taskId,
×
734
                nodeId, buf, tmp);
735
      } else {
736
        stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
4!
737
                pDispatcher->taskId, nodeId, buf);
738
      }
739
    }
740
  } else if (type == TASK_OUTPUT__VTABLE_MAP) {
×
741
    SArray* pTaskInfos = pTask->outputInfo.vtableMapDispatcher.taskInfos;
×
742

743
    for (int32_t i = 0; i < taosArrayGetSize(pTaskInfos); ++i) {
×
744
      STaskDispatcherFixed* pAddr = taosArrayGet(pTaskInfos, i);
×
745
      if (pAddr == NULL) {
×
746
        continue;
×
747
      }
748

749
      if (pAddr->nodeId == nodeId) {
×
750
        bool isEqual = isEpsetEqual(&pAddr->epSet, pEpSet);
×
751
        if (!isEqual) {
×
752
          *pUpdated = true;
×
753

754
          char tmp[512] = {0};
×
755
          code = epsetToStr(&pAddr->epSet, tmp, tListLen(tmp));
×
756
          if (code != 0) {  // print error and continue
×
757
            stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
758
            return code;
×
759
          }
760

761
          epsetAssign(&pAddr->epSet, pEpSet);
×
762
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pAddr->taskId,
×
763
                  nodeId, buf, tmp);
764
        } else {
765
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
×
766
                  pAddr->taskId, nodeId, buf);
767
        }
768
        break;
×
769
      }
770
    }
771
  }
772

773
  return code;
102✔
774
}
775

776
int32_t streamTaskStop(SStreamTask* pTask) {
2,994✔
777
  int32_t     vgId = pTask->pMeta->vgId;
2,994✔
778
  int64_t     st = taosGetTimestampMs();
2,994✔
779
  const char* id = pTask->id.idStr;
2,994✔
780

781
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,994✔
782
  if (code) {
2,994!
783
    stError("failed to handle STOP event, s-task:%s, code:%s", id, tstrerror(code));
×
784
    return code;
×
785
  }
786

787
  if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
2,994✔
788
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS, 5000);
1,439✔
789
    if (code != TSDB_CODE_SUCCESS) {
1,439!
790
      stError("s-task:%s failed to kill task related query handle, code:%s", id, tstrerror(code));
×
791
    }
792
  }
793

794
  while (!streamTaskIsIdle(pTask)) {
2,994!
795
    stDebug("s-task:%s level:%d wait for task to be idle and then close, check again in 100ms", id,
×
796
            pTask->info.taskLevel);
797
    taosMsleep(100);
×
798
  }
799

800
  int64_t el = taosGetTimestampMs() - st;
2,994✔
801
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,994✔
802
  return code;
2,994✔
803
}
804

805
bool streamTaskUpdateEpsetInfo(SStreamTask* pTask, SArray* pNodeList) {
335✔
806
  STaskExecStatisInfo* p = &pTask->execInfo;
335✔
807

808
  int32_t numOfNodes = taosArrayGetSize(pNodeList);
335✔
809
  int64_t prevTs = p->latestUpdateTs;
335✔
810

811
  p->latestUpdateTs = taosGetTimestampMs();
335✔
812
  p->updateCount += 1;
335✔
813
  stDebug("s-task:0x%x update task nodeEp epset, updatedNodes:%d, updateCount:%d, prevTs:%" PRId64, pTask->id.taskId,
335!
814
          numOfNodes, p->updateCount, prevTs);
815

816
  bool updated = false;
335✔
817
  for (int32_t i = 0; i < numOfNodes; ++i) {
992✔
818
    SNodeUpdateInfo* pInfo = taosArrayGet(pNodeList, i);
657✔
819
    if (pInfo == NULL) {
657!
820
      continue;
×
821
    }
822

823
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
657✔
824
    if (code) {
657!
825
      stError("s-task:0x%x failed to update the task nodeEp epset, code:%s", pTask->id.taskId, tstrerror(code));
×
826
    }
827
  }
828

829
  return updated;
335✔
830
}
831

832
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
15,175✔
833
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
15,175✔
834
    return;
7,616✔
835
  }
836

837
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
7,559✔
838
  for (int32_t i = 0; i < size; ++i) {
28,671✔
839
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
21,113✔
840
    pInfo->stage = -1;
21,114✔
841
  }
842

843
  stDebug("s-task:%s reset all upstream tasks stage info", pTask->id.idStr);
7,558✔
844
}
845

846
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
23,125✔
847
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
23,125✔
848
  if (num == 0) {
23,142✔
849
    return;
11,567✔
850
  }
851

852
  for (int32_t i = 0; i < num; ++i) {
43,449✔
853
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
31,884✔
854
    pInfo->dataAllowed = true;
31,874✔
855
  }
856

857
  pTask->upstreamInfo.numOfClosed = 0;
11,565✔
858
  stDebug("s-task:%s opening up inputQ for %d upstream tasks", pTask->id.idStr, num);
11,565✔
859
}
860

861
void streamTaskCloseUpstreamInput(SStreamTask* pTask, int32_t taskId) {
6,832✔
862
  SStreamUpstreamEpInfo* pInfo = NULL;
6,832✔
863
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
6,832✔
864

865
  if ((pInfo != NULL) && pInfo->dataAllowed) {
6,835!
866
    pInfo->dataAllowed = false;
6,842✔
867
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
6,842!
868
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
6,830✔
869
    } else {
870
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
871
    }
872
  }
873
}
6,851✔
874

875
void streamTaskOpenUpstreamInput(SStreamTask* pTask, int32_t taskId) {
1✔
876
  SStreamUpstreamEpInfo* pInfo = NULL;
1✔
877
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
1✔
878

879
  if (pInfo != NULL && (!pInfo->dataAllowed)) {
1!
880
    int32_t t = atomic_sub_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
×
881
    stDebug("s-task:%s open inputQ for upstream:0x%x, remain closed:%d", pTask->id.idStr, taskId, t);
×
882
    pInfo->dataAllowed = true;
×
883
  }
884
}
1✔
885

886
bool streamTaskIsAllUpstreamClosed(SStreamTask* pTask) {
×
887
  return pTask->upstreamInfo.numOfClosed == taosArrayGetSize(pTask->upstreamInfo.pList);
×
888
}
889

890
bool streamTaskSetSchedStatusWait(SStreamTask* pTask) {
66,414✔
891
  bool ret = false;
66,414✔
892

893
  streamMutexLock(&pTask->lock);
66,414✔
894
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
66,466✔
895
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
57,133✔
896
    ret = true;
57,133✔
897
  }
898

899
  streamMutexUnlock(&pTask->lock);
66,466✔
900
  return ret;
66,463✔
901
}
902

903
int8_t streamTaskSetSchedStatusActive(SStreamTask* pTask) {
55,578✔
904
  streamMutexLock(&pTask->lock);
55,578✔
905
  int8_t status = pTask->status.schedStatus;
55,670✔
906
  if (status == TASK_SCHED_STATUS__WAITING) {
55,670✔
907
    pTask->status.schedStatus = TASK_SCHED_STATUS__ACTIVE;
55,632✔
908
  }
909
  streamMutexUnlock(&pTask->lock);
55,670✔
910

911
  return status;
55,688✔
912
}
913

914
int8_t streamTaskSetSchedStatusInactive(SStreamTask* pTask) {
1,457✔
915
  streamMutexLock(&pTask->lock);
1,457✔
916
  int8_t status = pTask->status.schedStatus;
1,457✔
917
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
1,457✔
918
  streamMutexUnlock(&pTask->lock);
1,457✔
919

920
  return status;
1,457✔
921
}
922

923
int32_t streamTaskClearHTaskAttr(SStreamTask* pTask, int32_t resetRelHalt) {
7,694✔
924
  int32_t      code = 0;
7,694✔
925
  SStreamMeta* pMeta = pTask->pMeta;
7,694✔
926
  SStreamTask* pStreamTask = NULL;
7,694✔
927

928
  if (pTask->info.fillHistory == 0) {
7,694!
929
    return code;
7,709✔
930
  }
931

932
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
933
  if (code == 0) {
×
934
    stDebug("s-task:%s clear the related stream task:0x%x attr to fill-history task", pTask->id.idStr,
×
935
            (int32_t)pTask->streamTaskId.taskId);
936

937
    streamMutexLock(&(pStreamTask->lock));
×
938
    CLEAR_RELATED_FILLHISTORY_TASK(pStreamTask);
×
939

940
    if (resetRelHalt) {
×
941
      stDebug("s-task:0x%" PRIx64 " set the persistent status attr to be ready, prev:%s, status in sm:%s",
×
942
              pTask->streamTaskId.taskId, streamTaskGetStatusStr(pStreamTask->status.taskStatus),
943
              streamTaskGetStatus(pStreamTask).name);
944
      pStreamTask->status.taskStatus = TASK_STATUS__READY;
×
945
    }
946

947
    code = streamMetaSaveTaskInMeta(pMeta, pStreamTask);
×
948
    streamMutexUnlock(&(pStreamTask->lock));
×
949

950
    streamMetaReleaseTask(pMeta, pStreamTask);
×
951
  }
952

953
  return code;
×
954
}
955

956
int32_t streamBuildAndSendDropTaskMsg(SMsgCb* pMsgCb, int32_t vgId, SStreamTaskId* pTaskId, int64_t resetRelHalt) {
×
957
  SVDropStreamTaskReq* pReq = rpcMallocCont(sizeof(SVDropStreamTaskReq));
×
958
  if (pReq == NULL) {
×
959
    return terrno;
×
960
  }
961

962
  pReq->head.vgId = vgId;
×
963
  pReq->taskId = pTaskId->taskId;
×
964
  pReq->streamId = pTaskId->streamId;
×
965
  pReq->resetRelHalt = resetRelHalt;
×
966

967
  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_DROP, .pCont = pReq, .contLen = sizeof(SVDropStreamTaskReq)};
×
968
  int32_t code = tmsgPutToQueue(pMsgCb, WRITE_QUEUE, &msg);
×
969
  if (code != TSDB_CODE_SUCCESS) {
×
970
    stError("vgId:%d failed to send drop task:0x%x msg, code:%s", vgId, pTaskId->taskId, tstrerror(code));
×
971
  } else {
972
    stDebug("vgId:%d build and send drop task:0x%x msg", vgId, pTaskId->taskId);
×
973
  }
974

975
  return code;
×
976
}
977

978
int32_t streamSendChkptReportMsg(SStreamTask* pTask, SCheckpointInfo* pCheckpointInfo, int8_t dropRelHTask) {
5,234✔
979
  int32_t                code = 0;
5,234✔
980
  int32_t                tlen = 0;
5,234✔
981
  int32_t                vgId = pTask->pMeta->vgId;
5,234✔
982
  const char*            id = pTask->id.idStr;
5,234✔
983
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
5,234✔
984

985
  SCheckpointReport req = {.streamId = pTask->id.streamId,
5,234✔
986
                           .taskId = pTask->id.taskId,
5,234✔
987
                           .nodeId = vgId,
988
                           .dropHTask = dropRelHTask,
989
                           .transId = pActive->transId,
5,234✔
990
                           .checkpointId = pActive->activeId,
5,234✔
991
                           .checkpointVer = pCheckpointInfo->processedVer,
5,234✔
992
                           .checkpointTs = pCheckpointInfo->startTs};
5,234✔
993

994
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
5,234!
995
  if (code < 0) {
5,234!
996
    stError("s-task:%s vgId:%d encode stream task checkpoint-report failed, code:%s", id, vgId, tstrerror(code));
×
997
    return -1;
×
998
  }
999

1000
  void* buf = rpcMallocCont(tlen);
5,234✔
1001
  if (buf == NULL) {
5,234!
1002
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId,
×
1003
            tstrerror(TSDB_CODE_OUT_OF_MEMORY));
1004
    return -1;
×
1005
  }
1006

1007
  SEncoder encoder;
1008
  tEncoderInit(&encoder, buf, tlen);
5,234✔
1009
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
5,234!
1010
    rpcFreeCont(buf);
×
1011
    tEncoderClear(&encoder);
×
1012
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId, tstrerror(code));
×
1013
    return -1;
×
1014
  }
1015
  tEncoderClear(&encoder);
5,234✔
1016

1017
  SRpcMsg msg = {0};
5,234✔
1018
  initRpcMsg(&msg, TDMT_MND_STREAM_CHKPT_REPORT, buf, tlen);
5,234✔
1019
  stDebug("s-task:%s vgId:%d build and send task checkpoint-report to mnode", id, vgId);
5,234✔
1020

1021
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
5,234✔
1022
}
1023

1024
STaskId streamTaskGetTaskId(const SStreamTask* pTask) {
79,026✔
1025
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
79,026✔
1026
  return id;
79,026✔
1027
}
1028

1029
void streamTaskInitForLaunchHTask(SHistoryTaskInfo* pInfo) {
2,212✔
1030
  pInfo->waitInterval = LAUNCH_HTASK_INTERVAL;
2,212✔
1031
  pInfo->tickCount = ceil(LAUNCH_HTASK_INTERVAL / WAIT_FOR_MINIMAL_INTERVAL);
2,212✔
1032
  pInfo->retryTimes = 0;
2,212✔
1033
}
2,212✔
1034

1035
void streamTaskSetRetryInfoForLaunch(SHistoryTaskInfo* pInfo) {
2,193✔
1036
  pInfo->waitInterval *= RETRY_LAUNCH_INTERVAL_INC_RATE;
2,193✔
1037
  pInfo->tickCount = ceil(pInfo->waitInterval / WAIT_FOR_MINIMAL_INTERVAL);
2,193✔
1038
  pInfo->retryTimes += 1;
2,193✔
1039
}
2,193✔
1040

1041
void streamTaskStatusInit(STaskStatusEntry* pEntry, const SStreamTask* pTask) {
10,016✔
1042
  pEntry->id.streamId = pTask->id.streamId;
10,016✔
1043
  pEntry->id.taskId = pTask->id.taskId;
10,016✔
1044
  pEntry->stage = -1;
10,016✔
1045
  pEntry->nodeId = pTask->info.nodeId;
10,016✔
1046
  pEntry->status = TASK_STATUS__STOP;
10,016✔
1047
}
10,016✔
1048

1049
void streamTaskStatusCopy(STaskStatusEntry* pDst, const STaskStatusEntry* pSrc) {
60,916✔
1050
  pDst->stage = pSrc->stage;
60,916✔
1051
  pDst->inputQUsed = pSrc->inputQUsed;
60,916✔
1052
  pDst->inputRate = pSrc->inputRate;
60,916✔
1053
  pDst->procsTotal = pSrc->procsTotal;
60,916✔
1054
  pDst->procsThroughput = pSrc->procsThroughput;
60,916✔
1055
  pDst->outputTotal = pSrc->outputTotal;
60,916✔
1056
  pDst->outputThroughput = pSrc->outputThroughput;
60,916✔
1057
  pDst->processedVer = pSrc->processedVer;
60,916✔
1058
  pDst->verRange = pSrc->verRange;
60,916✔
1059
  pDst->sinkQuota = pSrc->sinkQuota;
60,916✔
1060
  pDst->sinkDataSize = pSrc->sinkDataSize;
60,916✔
1061
  pDst->checkpointInfo = pSrc->checkpointInfo;
60,916✔
1062
  pDst->startCheckpointId = pSrc->startCheckpointId;
60,916✔
1063
  pDst->startCheckpointVer = pSrc->startCheckpointVer;
60,916✔
1064
  pDst->status = pSrc->status;
60,916✔
1065

1066
  pDst->startTime = pSrc->startTime;
60,916✔
1067
  pDst->hTaskId = pSrc->hTaskId;
60,916✔
1068
  pDst->notifyEventStat = pSrc->notifyEventStat;
60,916✔
1069
}
60,916✔
1070

1071
STaskStatusEntry streamTaskGetStatusEntry(SStreamTask* pTask) {
62,290✔
1072
  SStreamMeta*         pMeta = pTask->pMeta;
62,290✔
1073
  STaskExecStatisInfo* pExecInfo = &pTask->execInfo;
62,290✔
1074

1075
  STaskStatusEntry entry = {
186,870✔
1076
      .id = streamTaskGetTaskId(pTask),
62,290✔
1077
      .status = streamTaskGetStatus(pTask).state,
62,290✔
1078
      .nodeId = pMeta->vgId,
62,290✔
1079
      .stage = pMeta->stage,
62,290✔
1080

1081
      .inputQUsed = SIZE_IN_MiB(streamQueueGetItemSize(pTask->inputq.queue)),
62,290✔
1082
      .startTime = pExecInfo->readyTs,
62,290✔
1083
      .checkpointInfo.latestId = pTask->chkInfo.checkpointId,
62,290✔
1084
      .checkpointInfo.latestVer = pTask->chkInfo.checkpointVer,
62,290✔
1085
      .checkpointInfo.latestTime = pTask->chkInfo.checkpointTime,
62,290✔
1086
      .checkpointInfo.latestSize = 0,
1087
      .checkpointInfo.remoteBackup = 0,
1088
      .checkpointInfo.consensusChkptId = 0,
1089
      .checkpointInfo.consensusTs = 0,
1090
      .hTaskId = pTask->hTaskInfo.id.taskId,
62,290✔
1091
      .procsTotal = SIZE_IN_MiB(pExecInfo->inputDataSize),
62,290✔
1092
      .outputTotal = SIZE_IN_MiB(pExecInfo->outputDataSize),
62,290✔
1093
      .procsThroughput = SIZE_IN_KiB(pExecInfo->procsThroughput),
62,290✔
1094
      .outputThroughput = SIZE_IN_KiB(pExecInfo->outputThroughput),
62,290✔
1095
      .startCheckpointId = pExecInfo->startCheckpointId,
62,290✔
1096
      .startCheckpointVer = pExecInfo->startCheckpointVer,
62,290✔
1097
      .notifyEventStat = pTask->notifyEventStat,
1098
  };
1099
  return entry;
62,290✔
1100
}
1101

1102
static int32_t taskPauseCallback(SStreamTask* pTask, void* param) {
731✔
1103
  SStreamMeta* pMeta = pTask->pMeta;
731✔
1104
  int32_t      code = 0;
731✔
1105

1106
  int32_t num = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
731✔
1107
  stInfo("vgId:%d s-task:%s pause stream task. paused task num:%d", pMeta->vgId, pTask->id.idStr, num);
749!
1108

1109
  // in case of fill-history task, stop the tsdb file scan operation.
1110
  if (pTask->info.fillHistory == 1) {
749!
1111
    void* pExecutor = pTask->exec.pExecutor;
×
1112
    code = qKillTask(pExecutor, TSDB_CODE_SUCCESS, 10000);
×
1113
  }
1114

1115
  stDebug("vgId:%d s-task:%s set pause flag and pause task", pMeta->vgId, pTask->id.idStr);
749✔
1116
  return code;
750✔
1117
}
1118

1119
void streamTaskPause(SStreamTask* pTask) {
729✔
1120
  int32_t code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_PAUSE, taskPauseCallback, NULL);
729✔
1121
  if (code) {
750!
1122
    stError("s-task:%s failed handle pause event async, code:%s", pTask->id.idStr, tstrerror(code));
×
1123
  }
1124
}
750✔
1125

1126
void streamTaskResume(SStreamTask* pTask) {
1,226✔
1127
  SStreamTaskState prevState = streamTaskGetStatus(pTask);
1,226✔
1128

1129
  SStreamMeta* pMeta = pTask->pMeta;
1,233✔
1130
  int32_t      code = streamTaskRestoreStatus(pTask);
1,233✔
1131
  if (code == TSDB_CODE_SUCCESS) {
1,241✔
1132
    char*   pNew = streamTaskGetStatus(pTask).name;
733✔
1133
    int32_t num = atomic_sub_fetch_32(&pMeta->numOfPausedTasks, 1);
731✔
1134
    stInfo("s-task:%s status:%s resume from %s, paused task(s):%d", pTask->id.idStr, pNew, prevState.name, num);
736!
1135
  } else {
1136
    stInfo("s-task:%s status:%s no need to resume, paused task(s):%d", pTask->id.idStr, prevState.name,
508!
1137
           pMeta->numOfPausedTasks);
1138
  }
1139
}
1,245✔
1140

1141
bool streamTaskIsSinkTask(const SStreamTask* pTask) { return pTask->info.taskLevel == TASK_LEVEL__SINK; }
59,552✔
1142

1143
// this task must success
1144
int32_t streamTaskSendCheckpointReq(SStreamTask* pTask) {
4,038✔
1145
  int32_t     code;
1146
  int32_t     tlen = 0;
4,038✔
1147
  int32_t     vgId = pTask->pMeta->vgId;
4,038✔
1148
  const char* id = pTask->id.idStr;
4,038✔
1149

1150
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,038✔
1151
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,038!
1152
  if (code < 0) {
4,039!
1153
    stError("s-task:%s vgId:%d encode stream task req checkpoint failed, code:%s", id, vgId, tstrerror(code));
×
1154
    return TSDB_CODE_INVALID_MSG;
×
1155
  }
1156

1157
  void* buf = rpcMallocCont(tlen);
4,039✔
1158
  if (buf == NULL) {
4,042!
1159
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:Out of memory", id, vgId);
×
1160
    return terrno;
×
1161
  }
1162

1163
  SEncoder encoder;
1164
  tEncoderInit(&encoder, buf, tlen);
4,042✔
1165
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,045!
1166
    rpcFreeCont(buf);
×
1167
    tEncoderClear(&encoder);
×
1168
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:%s", id, vgId, tstrerror(code));
×
1169
    return code;
×
1170
  }
1171

1172
  tEncoderClear(&encoder);
4,040✔
1173

1174
  SRpcMsg msg = {0};
4,045✔
1175
  initRpcMsg(&msg, TDMT_MND_STREAM_REQ_CHKPT, buf, tlen);
4,045✔
1176
  stDebug("s-task:%s vgId:%d build and send task checkpoint req", id, vgId);
4,039✔
1177

1178
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
4,039✔
1179
}
1180

1181
void streamTaskGetUpstreamTaskEpInfo(SStreamTask* pTask, int32_t taskId, SStreamUpstreamEpInfo** pEpInfo) {
67,107✔
1182
  *pEpInfo = NULL;
67,107✔
1183

1184
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
67,107✔
1185
  for (int32_t i = 0; i < num; ++i) {
129,541!
1186
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
129,571✔
1187
    if (pInfo == NULL) {
129,496!
1188
      return;
×
1189
    }
1190

1191
    if (pInfo->taskId == taskId) {
129,496✔
1192
      *pEpInfo = pInfo;
67,048✔
1193
      return;
67,048✔
1194
    }
1195
  }
1196

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

1200
SEpSet* streamTaskGetDownstreamEpInfo(SStreamTask* pTask, int32_t taskId) {
×
1201
  if (pTask->info.taskLevel == TASK_OUTPUT__FIXED_DISPATCH) {
×
1202
    if (pTask->outputInfo.fixedDispatcher.taskId == taskId) {
×
1203
      return &pTask->outputInfo.fixedDispatcher.epSet;
×
1204
    }
1205
  } else if (pTask->info.taskLevel == TASK_OUTPUT__SHUFFLE_DISPATCH) {
×
1206
    SArray* pList = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
×
1207
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1208
      SVgroupInfo* pVgInfo = taosArrayGet(pList, i);
×
1209
      if (pVgInfo == NULL) {
×
1210
        continue;
×
1211
      }
1212

1213
      if (pVgInfo->taskId == taskId) {
×
1214
        return &pVgInfo->epSet;
×
1215
      }
1216
    }
1217
  } else if (pTask->info.taskLevel == TASK_OUTPUT__VTABLE_MAP) {
×
1218
    SArray* pTaskInfos = pTask->outputInfo.vtableMapDispatcher.taskInfos;
×
1219
    for (int32_t i = 0; i < taosArrayGetSize(pTaskInfos); ++i) {
×
1220
      STaskDispatcherFixed* pAddr = taosArrayGet(pTaskInfos, i);
×
1221
      if (pAddr == NULL) {
×
1222
        continue;
×
1223
      }
1224

1225
      if (pAddr->taskId == taskId) {
×
1226
        return &pAddr->epSet;
×
1227
      }
1228
    }
1229
  }
1230

1231
  return NULL;
×
1232
}
1233

1234
int32_t createStreamTaskIdStr(int64_t streamId, int32_t taskId, const char** pId) {
15,161✔
1235
  char    buf[128] = {0};
15,161✔
1236
  int32_t code = snprintf(buf, tListLen(buf), "0x%" PRIx64 "-0x%x", streamId, taskId);
15,161✔
1237
  if (code < 0 || code >= tListLen(buf)) {
15,161!
1238
    return TSDB_CODE_OUT_OF_BUFFER;
×
1239
  }
1240

1241
  *pId = taosStrdup(buf);
15,179!
1242

1243
  if (*pId == NULL) {
15,148!
1244
    return terrno;
×
1245
  } else {
1246
    return TSDB_CODE_SUCCESS;
15,148✔
1247
  }
1248
}
1249

1250
static int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
540✔
1251
  int32_t           code;
1252
  SStreamDataBlock* pData;
1253

1254
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
540✔
1255
  if (code) {
541!
1256
    stError("s-task:%s failed to allocated retrieve-block", pTask->id.idStr);
×
1257
    return terrno = code;
×
1258
  }
1259

1260
  pData->type = STREAM_INPUT__DATA_RETRIEVE;
541✔
1261
  pData->srcVgId = 0;
541✔
1262

1263
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
541✔
1264
  if (code != TSDB_CODE_SUCCESS) {
540!
1265
    stError("s-task:%s failed to convert retrieve-data to block, code:%s", pTask->id.idStr, tstrerror(code));
×
1266
    taosFreeQitem(pData);
×
1267
    return code;
×
1268
  }
1269

1270
  code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pData);
540✔
1271
  if (code != TSDB_CODE_SUCCESS) {
542!
1272
    stError("s-task:%s failed to put retrieve-block into inputQ, inputQ is full, discard the retrieve msg",
×
1273
            pTask->id.idStr);
1274
  }
1275

1276
  return code;
542✔
1277
}
1278

1279
int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
540✔
1280
  int32_t code = streamTaskEnqueueRetrieve(pTask, pReq);
540✔
1281
  if (code != 0) {
542!
1282
    return code;
×
1283
  }
1284
  return streamTrySchedExec(pTask, false);
542✔
1285
}
1286

1287
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
7,707✔
1288

1289
void streamTaskGetActiveCheckpointInfo(const SStreamTask* pTask, int32_t* pTransId, int64_t* pCheckpointId) {
×
1290
  if (pTransId != NULL) {
×
1291
    *pTransId = pTask->chkInfo.pActiveInfo->transId;
×
1292
  }
1293

1294
  if (pCheckpointId != NULL) {
×
1295
    *pCheckpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1296
  }
1297
}
×
1298

1299
int32_t streamTaskSetActiveCheckpointInfo(SStreamTask* pTask, int64_t activeCheckpointId) {
36✔
1300
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
36✔
1301
  return TSDB_CODE_SUCCESS;
36✔
1302
}
1303

1304
void streamTaskSetFailedChkptInfo(SStreamTask* pTask, int32_t transId, int64_t checkpointId) {
×
1305
  pTask->chkInfo.pActiveInfo->transId = transId;
×
1306
  pTask->chkInfo.pActiveInfo->activeId = checkpointId;
×
1307
  pTask->chkInfo.pActiveInfo->failedId = checkpointId;
×
1308
  stDebug("s-task:%s set failed checkpointId:%" PRId64, pTask->id.idStr, checkpointId);
×
1309
}
×
1310

1311
int32_t streamTaskCreateActiveChkptInfo(SActiveCheckpointInfo** pRes) {
15,206✔
1312
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
15,206!
1313
  if (pInfo == NULL) {
15,213!
1314
    return terrno;
×
1315
  }
1316

1317
  int32_t code = taosThreadMutexInit(&pInfo->lock, NULL);
15,213✔
1318
  if (code != TSDB_CODE_SUCCESS) {
15,216!
1319
    return code;
×
1320
  }
1321

1322
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
15,216✔
1323
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
15,223✔
1324
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
15,226✔
1325

1326
  *pRes = pInfo;
15,223✔
1327
  return code;
15,223✔
1328
}
1329

1330
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
59,293✔
1331
  if (pInfo == NULL) {
59,293✔
1332
    return;
44,068✔
1333
  }
1334

1335
  streamMutexDestroy(&pInfo->lock);
15,225✔
1336
  taosArrayDestroy(pInfo->pDispatchTriggerList);
15,225✔
1337
  pInfo->pDispatchTriggerList = NULL;
15,228✔
1338
  taosArrayDestroy(pInfo->pReadyMsgList);
15,228✔
1339
  pInfo->pReadyMsgList = NULL;
15,224✔
1340
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
15,224✔
1341
  pInfo->pCheckpointReadyRecvList = NULL;
15,227✔
1342

1343
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
15,227✔
1344
  if (pTriggerTmr->tmrHandle != NULL) {
15,227✔
1345
    streamTmrStop(pTriggerTmr->tmrHandle);
2,020✔
1346
    pTriggerTmr->tmrHandle = NULL;
2,022✔
1347
  }
1348

1349
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
15,229✔
1350
  if (pReadyTmr->tmrHandle != NULL) {
15,229✔
1351
    streamTmrStop(pReadyTmr->tmrHandle);
1,975✔
1352
    pReadyTmr->tmrHandle = NULL;
1,977✔
1353
  }
1354

1355
  taosMemoryFree(pInfo);
15,231!
1356
}
1357

1358
// NOTE: clear the checkpoint id, and keep the failed id
1359
// failedId for a task will increase as the checkpoint I.D. increases.
1360
void streamTaskClearActiveInfo(SActiveCheckpointInfo* pInfo) {
3,832✔
1361
  pInfo->activeId = 0;
3,832✔
1362
  pInfo->transId = 0;
3,832✔
1363
  pInfo->allUpstreamTriggerRecv = 0;
3,832✔
1364
  pInfo->dispatchTrigger = false;
3,832✔
1365

1366
  taosArrayClear(pInfo->pDispatchTriggerList);
3,832✔
1367
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
3,820✔
1368
}
3,813✔
1369

1370
const char* streamTaskGetExecType(int32_t type) {
105,406✔
1371
  switch (type) {
105,406!
1372
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
60,172✔
1373
      return "scan-wal-file";
60,172✔
1374
    case STREAM_EXEC_T_START_ALL_TASKS:
8,952✔
1375
      return "start-all-tasks";
8,952✔
1376
    case STREAM_EXEC_T_START_ONE_TASK:
4,733✔
1377
      return "start-one-task";
4,733✔
1378
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
41✔
1379
      return "restart-all-tasks";
41✔
1380
    case STREAM_EXEC_T_STOP_ALL_TASKS:
5,833✔
1381
      return "stop-all-tasks";
5,833✔
1382
    case STREAM_EXEC_T_RESUME_TASK:
1,670✔
1383
      return "resume-task-from-idle";
1,670✔
1384
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
1385
      return "record-start-failed-task";
×
1386
    case STREAM_EXEC_T_STOP_ONE_TASK:
×
1387
      return "stop-one-task";
×
1388
    case 0:
24,103✔
1389
      return "exec-all-tasks";
24,103✔
1390
    default:
×
1391
      return "invalid-exec-type";
×
1392
  }
1393
}
1394

1395
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
32,137✔
1396
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
32,137!
1397
  if (*pRefId != NULL) {
32,143!
1398
    **pRefId = pTask->id.refId;
32,143✔
1399
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
32,143✔
1400
    if (code != 0) {
32,149!
1401
      stError("s-task:%s failed to add refId:%" PRId64 " into refId-mgmt, code:%s", pTask->id.idStr, pTask->id.refId,
×
1402
              tstrerror(code));
1403
    }
1404
    return code;
32,149✔
1405
  } else {
1406
    stError("s-task:%s failed to alloc new ref id, code:%s", pTask->id.idStr, tstrerror(terrno));
×
1407
    return terrno;
×
1408
  }
1409
}
1410

1411
void streamTaskFreeRefId(int64_t* pRefId) {
58,393✔
1412
  if (pRefId == NULL) {
58,393✔
1413
    return;
30,172✔
1414
  }
1415

1416
  metaRefMgtRemove(pRefId);
28,221✔
1417
}
1418

1419
static int32_t tEncodeStreamNotifyInfo(SEncoder* pEncoder, const SNotifyInfo* info) {
130,115✔
1420
  int32_t code = TSDB_CODE_SUCCESS;
130,115✔
1421
  int32_t lino = 0;
130,115✔
1422

1423
  QUERY_CHECK_NULL(pEncoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
130,115!
1424
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
130,115!
1425

1426
  int32_t addrSize = taosArrayGetSize(info->pNotifyAddrUrls);
130,115✔
1427
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, addrSize));
130,114!
1428
  for (int32_t i = 0; i < addrSize; ++i) {
130,114!
1429
    const char* url = taosArrayGetP(info->pNotifyAddrUrls, i);
×
1430
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, url));
×
1431
  }
1432
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyEventTypes));
260,228!
1433
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyErrorHandle));
260,228!
1434
  if (addrSize > 0) {
130,114!
1435
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, info->streamName));
×
1436
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, info->stbFullName));
×
1437
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, info->pSchemaWrapper));
×
1438
  }
1439

1440
_exit:
130,114✔
1441
  if (code != TSDB_CODE_SUCCESS) {
130,114!
1442
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1443
  }
1444
  return code;
130,114✔
1445
}
1446

1447
static int32_t tDecodeStreamNotifyInfo(SDecoder* pDecoder, SNotifyInfo* info) {
44,804✔
1448
  int32_t code = TSDB_CODE_SUCCESS;
44,804✔
1449
  int32_t lino = 0;
44,804✔
1450

1451
  QUERY_CHECK_NULL(pDecoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
44,804!
1452
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
44,804!
1453

1454
  int32_t addrSize = 0;
44,804✔
1455
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addrSize));
44,809!
1456
  info->pNotifyAddrUrls = taosArrayInit(addrSize, POINTER_BYTES);
44,809✔
1457
  QUERY_CHECK_NULL(info->pNotifyAddrUrls, code, lino, _exit, terrno);
44,824✔
1458
  for (int32_t i = 0; i < addrSize; ++i) {
44,820!
1459
    char* url = NULL;
×
1460
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &url));
×
1461
    url = taosStrndup(url, TSDB_STREAM_NOTIFY_URL_LEN);
×
1462
    QUERY_CHECK_NULL(url, code, lino, _exit, terrno);
×
1463
    if (taosArrayPush(info->pNotifyAddrUrls, &url) == NULL) {
×
1464
      taosMemoryFree(url);
×
1465
      TAOS_CHECK_EXIT(terrno);
×
1466
    }
1467
  }
1468
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyEventTypes));
89,630!
1469
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyErrorHandle));
89,621!
1470
  if (addrSize > 0) {
44,811!
1471
    char* name = NULL;
×
1472
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &name));
×
1473
    info->streamName = taosStrndup(name, TSDB_STREAM_FNAME_LEN + 1);
×
1474
    QUERY_CHECK_NULL(info->streamName, code, lino, _exit, terrno);
×
1475
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &name));
×
1476
    info->stbFullName = taosStrndup(name, TSDB_STREAM_FNAME_LEN + 1);
×
1477
    QUERY_CHECK_NULL(info->stbFullName, code, lino, _exit, terrno);
×
1478
    info->pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
×
1479
    if (info->pSchemaWrapper == NULL) {
×
1480
      TAOS_CHECK_EXIT(terrno);
×
1481
    }
1482
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, info->pSchemaWrapper));
×
1483
  }
1484

1485
_exit:
44,811✔
1486
  if (code != TSDB_CODE_SUCCESS) {
44,811!
1487
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1488
  }
1489
  return code;
44,813✔
1490
}
1491

1492
int32_t tEncodeVTablesInfo(SEncoder* pEncoder, SArray* pVTables) {
130,120✔
1493
  int32_t code = TSDB_CODE_SUCCESS;
130,120✔
1494
  int32_t lino = 0;
130,120✔
1495
  SVCTableMergeInfo* pMergeInfo = NULL;
130,120✔
1496
  int32_t mergeNum = taosArrayGetSize(pVTables);
130,120✔
1497
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, mergeNum));
130,121!
1498
  for (int32_t i = 0; i < mergeNum; ++i) {
130,121!
1499
    pMergeInfo = (SVCTableMergeInfo*)taosArrayGet(pVTables, i);
×
1500
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pMergeInfo->uid));
×
1501
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pMergeInfo->numOfSrcTbls));
×
1502
  }
1503

1504
_exit:
130,121✔
1505
  if (code != TSDB_CODE_SUCCESS) {
130,121!
1506
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1507
  }
1508
  return code;
130,105✔
1509
}
1510

1511
static int32_t tDecodeVTablesInfo(SDecoder* pDecoder, SArray** pTables) {
44,801✔
1512
  int32_t code = TSDB_CODE_SUCCESS;
44,801✔
1513
  int32_t lino = 0;
44,801✔
1514

1515
  int32_t mergeNum = 0;
44,801✔
1516
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &mergeNum));
44,809!
1517
  if (mergeNum > 0) {
44,809!
1518
    *pTables = taosArrayInit(mergeNum, sizeof(SVCTableMergeInfo));
×
1519
    QUERY_CHECK_NULL(*pTables, code, lino, _exit, terrno);
×
1520
  }
1521

1522
  SVCTableMergeInfo mergeInfo;
1523
  for (int32_t i = 0; i < mergeNum; ++i) {
44,806!
1524
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &mergeInfo.uid));
×
1525
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &mergeInfo.numOfSrcTbls));
×
1526
    if (taosArrayPush(*pTables, &mergeInfo) == NULL) {
×
1527
      TAOS_CHECK_EXIT(terrno);
×
1528
    }
1529
  }
1530

1531
_exit:
44,806✔
1532
  if (code != TSDB_CODE_SUCCESS) {
44,806!
1533
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1534
  }
1535
  return code;
44,807✔
1536
}
1537

1538
int32_t tSerializeDispatcherTaskInfo(SEncoder* pEncoder, const SArray* pTaskInfos) {
×
1539
  int32_t code = TSDB_CODE_SUCCESS;
×
1540
  int32_t lino = 0;
×
1541
  int32_t nTasks = taosArrayGetSize(pTaskInfos);
×
1542
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, nTasks));
×
1543
  for (int32_t i = 0; i < nTasks; ++i) {
×
1544
    STaskDispatcherFixed* pAddr = taosArrayGet(pTaskInfos, i);
×
1545
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pAddr->taskId));
×
1546
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pAddr->nodeId));
×
1547
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pAddr->epSet));
×
1548
  }
1549

1550
_exit:
×
1551
  if (code != TSDB_CODE_SUCCESS) {
×
1552
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1553
  }
1554
  return code;
×
1555
}
1556

1557
int32_t tSerializeDispatcherVtableMap(SEncoder* pEncoder, const SSHashObj* pVtables) {
×
1558
  int32_t code = TSDB_CODE_SUCCESS;
×
1559
  int32_t lino = 0;
×
1560
  int32_t tbNum = tSimpleHashGetSize(pVtables);
×
1561
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, tbNum));
×
1562
  int32_t iter = 0;
×
1563
  void*   p = tSimpleHashIterate(pVtables, NULL, &iter);
×
1564
  while (p != NULL) {
×
1565
    int64_t* pUid = tSimpleHashGetKey(p, NULL);
×
1566
    int32_t* pIdx = p;
×
1567
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, *pUid));
×
1568
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, *pIdx));
×
1569
    p = tSimpleHashIterate(pVtables, p, &iter);
×
1570
  }
1571

1572
_exit:
×
1573
  if (code != TSDB_CODE_SUCCESS) {
×
1574
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1575
  }
1576
  return code;
×
1577
}
1578

1579
int32_t tDeserializeDispatcherTaskInfo(SDecoder* pDecoder, SArray** ppTaskInfos) {
×
1580
  int32_t code = TSDB_CODE_SUCCESS;
×
1581
  int32_t lino = 0;
×
1582
  int32_t nTasks = 0;
×
1583
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &nTasks));
×
1584
  if (nTasks <= 0) {
×
1585
    return code;
×
1586
  }
1587

1588
  *ppTaskInfos = taosArrayInit(nTasks, sizeof(STaskDispatcherFixed));
×
1589
  QUERY_CHECK_NULL(*ppTaskInfos, code, lino, _exit, terrno);
×
1590

1591
  STaskDispatcherFixed addr;
1592
  for (int32_t i = 0; i < nTasks; ++i) {
×
1593
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addr.taskId));
×
1594
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addr.nodeId));
×
1595
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &addr.epSet));
×
1596
    void* px = taosArrayPush(*ppTaskInfos, &addr);
×
1597
    QUERY_CHECK_NULL(px, code, lino, _exit, terrno);
×
1598
  }
1599

1600
_exit:
×
1601
  if (code != TSDB_CODE_SUCCESS) {
×
1602
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1603
  }
1604
  return code;
×
1605
}
1606

1607
int32_t tDeserializeDispatcherVtableMap(SDecoder* pDecoder, SSHashObj** ppVtables) {
×
1608
  int32_t code = TSDB_CODE_SUCCESS;
×
1609
  int32_t lino = 0;
×
1610
  int32_t tbNum = 0;
×
1611
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &tbNum));
×
1612
  if (tbNum <= 0) {
×
1613
    return code;
×
1614
  }
1615

1616
  *ppVtables = tSimpleHashInit(tbNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT));
×
1617
  QUERY_CHECK_NULL(*ppVtables, code, lino, _exit, terrno);
×
1618

1619
  uint64_t uid = 0;
×
1620
  int32_t  idx = 0;
×
1621
  for (int32_t i = 0; i < tbNum; ++i) {
×
1622
    TAOS_CHECK_EXIT(tDecodeU64(pDecoder, &uid));
×
1623
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &idx));
×
1624
    TAOS_CHECK_EXIT(tSimpleHashPut(*ppVtables, &uid, sizeof(uid), &idx, sizeof(idx)));
×
1625
  }
1626

1627
_exit:
×
1628

1629
  if (code != TSDB_CODE_SUCCESS) {
×
1630
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1631
  }
1632
  return code;
×
1633
}
1634

1635
int32_t tEncodeStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
130,173✔
1636
  int32_t code = 0;
130,173✔
1637
  int32_t lino;
1638

1639
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
130,173!
1640
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->ver));
260,368!
1641
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->id.streamId));
260,368!
1642
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->id.taskId));
260,368!
1643
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.trigger));
260,368!
1644
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.taskLevel));
260,368!
1645
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.type));
260,368!
1646
  TAOS_CHECK_EXIT(tEncodeI16(pEncoder, pTask->msgInfo.msgType));
260,368!
1647

1648
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.taskStatus));
260,368!
1649
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.schedStatus));
260,368!
1650

1651
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
260,368!
1652
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
260,368!
1653
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
130,184!
1654
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
130,186!
1655

1656
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointId));
260,378!
1657
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointVer));
260,378!
1658
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.fillHistory));
260,378!
1659

1660
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
260,378!
1661
  int32_t taskId = pTask->hTaskInfo.id.taskId;
130,189✔
1662
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
130,189!
1663

1664
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
260,378!
1665
  taskId = pTask->streamTaskId.taskId;
130,189✔
1666
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
130,189!
1667

1668
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.minVer));
260,378!
1669
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.maxVer));
260,378!
1670
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.skey));
260,378!
1671
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.ekey));
260,378!
1672

1673
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
130,189✔
1674
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
130,179!
1675
  for (int32_t i = 0; i < epSz; i++) {
306,008✔
1676
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
175,850✔
1677
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
175,828!
1678
  }
1679

1680
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
130,158✔
1681
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
137,728!
1682
  }
1683

1684
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
130,158✔
1685
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
129,142!
1686
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
129,142!
1687
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
129,142!
1688
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
65,587!
1689
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
×
1690
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
65,587!
1691
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1692
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
65,587✔
1693
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
19,394!
1694
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
19,394!
1695
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
9,697!
1696
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
55,890✔
1697
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
55,851!
1698
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
111,706!
1699
  } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
39!
1700
    TAOS_CHECK_EXIT(tSerializeDispatcherTaskInfo(pEncoder, pTask->outputInfo.vtableMapDispatcher.taskInfos));
×
1701
    TAOS_CHECK_EXIT(tSerializeDispatcherVtableMap(pEncoder, pTask->outputInfo.vtableMapDispatcher.vtableMap));
×
1702
  }
1703
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
260,320!
1704
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
260,320!
1705
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
260,320!
1706

1707
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
130,160✔
1708
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.hasAggTasks));
260,264✔
1709
    TAOS_CHECK_EXIT(tEncodeStreamNotifyInfo(pEncoder, &pTask->notifyInfo));
130,106!
1710
    TAOS_CHECK_EXIT(tEncodeVTablesInfo(pEncoder, pTask->pVTables));
130,115!
1711
  }
1712

1713
  tEndEncode(pEncoder);
130,133✔
1714
_exit:
130,162✔
1715
  return code;
130,162✔
1716
}
1717

1718
int32_t tDecodeStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
44,817✔
1719
  int32_t taskId = 0;
44,817✔
1720
  int32_t code = 0;
44,817✔
1721
  int32_t lino;
1722

1723
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
44,817!
1724
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
89,654✔
1725
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
44,818!
1726
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
1727
  }
1728

1729
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
89,627!
1730
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
89,622!
1731
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
89,631!
1732
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
89,636!
1733
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
89,627!
1734
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
89,622!
1735

1736
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
89,630!
1737
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
89,633!
1738

1739
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
89,620!
1740
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
89,613!
1741
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
44,809!
1742
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
44,821!
1743

1744
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
89,632!
1745
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
89,628!
1746
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
89,626!
1747

1748
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
89,626!
1749
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
44,804!
1750
  pTask->hTaskInfo.id.taskId = taskId;
44,804✔
1751

1752
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
89,614!
1753
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
44,808!
1754
  pTask->streamTaskId.taskId = taskId;
44,808✔
1755

1756
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
89,620!
1757
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
89,618!
1758
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
89,619!
1759
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
89,626!
1760

1761
  int32_t epSz = -1;
44,813✔
1762
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &epSz) < 0);
44,812!
1763

1764
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
44,812!
1765
    TAOS_CHECK_EXIT(terrno);
×
1766
  }
1767
  for (int32_t i = 0; i < epSz; i++) {
105,826✔
1768
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
61,006!
1769
    if (pInfo == NULL) {
60,985!
1770
      TAOS_CHECK_EXIT(terrno);
×
1771
    }
1772
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
60,985!
1773
      taosMemoryFreeClear(pInfo);
×
1774
      goto _exit;
×
1775
    }
1776
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
122,031!
1777
      TAOS_CHECK_EXIT(terrno);
×
1778
    }
1779
  }
1780

1781
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
44,820✔
1782
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
47,123!
1783
  }
1784

1785
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
44,819✔
1786
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
44,418!
1787
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
22,211!
1788
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
22,203!
1789
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
22,207!
1790
      TAOS_CHECK_EXIT(terrno);
×
1791
    }
1792
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
44,398!
1793
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
22,612!
1794
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
×
1795
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
22,612!
1796
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1797
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
22,612✔
1798
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
6,348!
1799
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
6,348!
1800
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,174!
1801
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
19,438✔
1802
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
19,428!
1803
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
19,429!
1804
  } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
10!
1805
    TAOS_CHECK_EXIT(tDeserializeDispatcherTaskInfo(pDecoder, &pTask->outputInfo.vtableMapDispatcher.taskInfos));
×
1806
    TAOS_CHECK_EXIT(tDeserializeDispatcherVtableMap(pDecoder, &pTask->outputInfo.vtableMapDispatcher.vtableMap));
×
1807
  }
1808
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
89,618!
1809
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
44,815!
1810
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
89,629!
1811
  }
1812
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
44,812!
1813

1814
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
44,815!
1815
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.hasAggTasks));
89,639!
1816
    TAOS_CHECK_EXIT(tDecodeStreamNotifyInfo(pDecoder, &pTask->notifyInfo));
44,820!
1817
    TAOS_CHECK_EXIT(tDecodeVTablesInfo(pDecoder, &pTask->pVTables));
44,814!
1818
  }
1819

1820
  tEndDecode(pDecoder);
44,803✔
1821

1822
_exit:
44,812✔
1823
  return code;
44,812✔
1824
}
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