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

taosdata / TDengine / #3910

23 Apr 2025 02:47AM UTC coverage: 62.362% (-0.7%) from 63.063%
#3910

push

travis-ci

web-flow
docs(datain): add missing health status types (#30828)

155061 of 317305 branches covered (48.87%)

Branch coverage included in aggregate %.

240172 of 316469 relevant lines covered (75.89%)

6269478.46 hits per line

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

58.57
/source/libs/stream/src/streamTask.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#include "executor.h"
17
#include "osDir.h"
18
#include "osMemory.h"
19
#include "streamInt.h"
20
#include "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) {
6,457✔
33
  int32_t childId = taosArrayGetSize(pArray);
6,457✔
34
  pTask->info.selfChildId = childId;
6,457✔
35
  void* p = taosArrayPush(pArray, &pTask);
6,457✔
36
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
6,457!
37
}
38

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

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

51
    if (!isEqual) {
56✔
52
      (*pUpdated) = true;
54✔
53
      char tmp[512] = {0};
54✔
54
      code = epsetToStr(&pTask->info.epSet, tmp, tListLen(tmp));  // only for log file, ignore errors
54✔
55
      if (code) {                                                 // print error and continue
54!
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);
54✔
61
      stDebug("s-task:0x%x (vgId:%d) self node epset is updated %s, old:%s", pTask->id.taskId, nodeId, buf, tmp);
54!
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;
187✔
69
  if (level == TASK_LEVEL__SOURCE) {
187✔
70
    code = streamTaskUpdateDownstreamInfo(pTask, nodeId, pEpSet, pUpdated);
85✔
71
  } else if (level == TASK_LEVEL__AGG) {
102✔
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);
85✔
76
  }
77

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

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

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

106
  return pEpInfo;
8,465✔
107
}
108

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

114
  SStreamTask* pTask = (SStreamTask*)taosMemoryCalloc(1, sizeof(SStreamTask));
6,457!
115
  if (pTask == NULL) {
6,457!
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;
6,457✔
122
  pTask->id.taskId = tGenIdPI32();
6,457✔
123
  pTask->id.streamId = streamId;
6,457✔
124

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

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

138
  char    buf[128] = {0};
6,457✔
139
  int32_t ret = snprintf(buf, tListLen(buf), "0x%" PRIx64 "-0x%x", pTask->id.streamId, pTask->id.taskId);
6,457✔
140
  if (ret < 0 || ret >= tListLen(buf)) {
6,457!
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);
6,457!
146
  if (pTask->id.idStr == NULL) {
6,457!
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;
6,457✔
152
  pTask->status.taskStatus =
6,457✔
153
      (pTask->info.fillHistory == STREAM_HISTORY_TASK) ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
6,457✔
154
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
6,457✔
155
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
6,457✔
156

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

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

166
  code = addToTaskset(pTaskList, pTask);
6,457✔
167
  *p = pTask;
6,457✔
168

169
  return code;
6,457✔
170
}
171

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

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

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

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

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

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

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

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

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

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

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

226
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
24,313✔
227

228
  ETaskStatus status1 = TASK_STATUS__UNINIT;
24,313✔
229
  if (pTask->status.pSM != NULL) {
24,313✔
230
    streamMutexLock(&pTask->lock);
13,276✔
231
    SStreamTaskState status = streamTaskGetStatus(pTask);
13,276✔
232
    p = status.name;
13,276✔
233
    status1 = status.state;
13,276✔
234
    streamMutexUnlock(&pTask->lock);
13,276✔
235
  }
236

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

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

251
  if (pTask->hTaskInfo.pTimer != NULL) {
24,313✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
934✔
253
    pTask->hTaskInfo.pTimer = NULL;
934✔
254
  }
255

256
  if (pTask->msgInfo.pRetryTmr != NULL) {
24,313✔
257
    streamTmrStop(pTask->msgInfo.pRetryTmr);
2,393✔
258
    pTask->msgInfo.pRetryTmr = NULL;
2,393✔
259
  }
260

261
  if (pTask->inputq.queue) {
24,313✔
262
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
6,789✔
263
    pTask->inputq.queue = NULL;
6,789✔
264
  }
265

266
  if (pTask->outputq.queue) {
24,313✔
267
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
6,789✔
268
    pTask->outputq.queue = NULL;
6,789✔
269
  }
270

271
  if (pTask->exec.qmsg) {
24,313✔
272
    taosMemoryFree(pTask->exec.qmsg);
13,178!
273
  }
274

275
  if (pTask->exec.pExecutor) {
24,313✔
276
    qDestroyTask(pTask->exec.pExecutor);
3,573✔
277
    pTask->exec.pExecutor = NULL;
3,573✔
278
  }
279

280
  if (pTask->exec.pWalReader != NULL) {
24,313✔
281
    walCloseReader(pTask->exec.pWalReader);
3,474✔
282
    pTask->exec.pWalReader = NULL;
3,474✔
283
  }
284

285
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
24,313✔
286

287
  if (pTask->msgInfo.pData != NULL) {
24,313✔
288
    clearBufferedDispatchMsg(pTask);
25✔
289
  }
290

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
24,313✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
12,080!
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
12,080!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
12,080✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
12,080✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
12,233✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
9,815✔
298
  } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
2,418!
299
    taosArrayDestroy(pTask->outputInfo.vtableMapDispatcher.taskInfos);
×
300
    tSimpleHashCleanup(pTask->outputInfo.vtableMapDispatcher.vtableMap);
×
301
  }
302

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

306
  if (pTask->pNameMap) {
24,313✔
307
    tSimpleHashCleanup(pTask->pNameMap);
1,139✔
308
  }
309

310
  if (pTask->status.pSM != NULL) {
24,313✔
311
    streamMutexDestroy(&pTask->lock);
13,276✔
312
    streamMutexDestroy(&pTask->msgInfo.lock);
13,276✔
313
    streamMutexDestroy(&pTask->taskCheckInfo.checkInfoLock);
13,276✔
314
  }
315

316
  taosArrayDestroy(pTask->pVTables);
24,313✔
317
  pTask->pVTables = NULL;
24,313✔
318

319
  streamDestroyStateMachine(pTask->status.pSM);
24,313✔
320
  pTask->status.pSM = NULL;
24,313✔
321

322
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
24,313✔
323

324
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
24,313!
325

326
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
24,313✔
327
  pTask->msgInfo.pSendInfo = NULL;
24,313✔
328

329
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
24,313✔
330
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
24,313✔
331

332
  if (pTask->id.idStr != NULL) {
24,313✔
333
    taosMemoryFree((void*)pTask->id.idStr);
13,242!
334
  }
335

336
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
24,313✔
337
  pTask->chkInfo.pActiveInfo = NULL;
24,313✔
338

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

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

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

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

356
    if (remove) taskDbSetClearFileFlag(pTask->pBackend);
3,573✔
357
    taskDbRemoveRef(pTask->pBackend);
3,573✔
358
    pTask->pBackend = NULL;
3,573✔
359
    pTask->pState = NULL;
3,573✔
360

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

372
  if (pTask->backendPath != NULL) {
24,313✔
373
    taosMemoryFree(pTask->backendPath);
6,789!
374
    pTask->backendPath = NULL;
6,789✔
375
  }
376
  // clear recal backend
377

378
  if (pTask->pRecalState != NULL) {
24,313!
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
}
24,313✔
402

403
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
6,789✔
404
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
6,789✔
405
  SDataRange*      pRange = &pTask->dataRange;
6,789✔
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))) {
6,789✔
409
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
2,988✔
410
    pChkInfo->processedVer = ver - 1;   // already processed version
2,988✔
411
    pChkInfo->nextProcessVer = ver;     // next processed version
2,988✔
412

413
    pRange->range.maxVer = ver;
2,988✔
414
    pRange->range.minVer = ver;
2,988✔
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) {
3,801✔
419
      pChkInfo->checkpointVer = pRange->range.maxVer;
1,901✔
420
      pChkInfo->processedVer = pRange->range.maxVer;
1,901✔
421
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
1,901✔
422
    } else {
423
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
1,900✔
424
      pChkInfo->processedVer = pRange->range.minVer - 1;
1,900✔
425
      pChkInfo->nextProcessVer = pRange->range.minVer;
1,900✔
426

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

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

443
  if (pTask->info.fillHistory) {
6,789✔
444
    streamId = pTask->streamTaskId.streamId;
1,901✔
445
    taskId = pTask->streamTaskId.taskId;
1,901✔
446
  } else {
447
    streamId = pTask->id.streamId;
4,888✔
448
    taskId = pTask->id.taskId;
4,888✔
449
  }
450

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

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

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

473
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
6,789✔
474
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
6,789✔
475
  if (code) {
6,789!
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;
6,789✔
481
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
6,789✔
482
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
6,789✔
483

484
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
6,789✔
485
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
6,789✔
486
  if (code1 || code2) {
6,789!
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;
6,789✔
492

493
  code = streamCreateStateMachine(pTask);
6,789✔
494
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
6,789!
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();
6,789✔
501
  setInitialVersionInfo(pTask, ver);
6,789✔
502

503
  pTask->pMeta = pMeta;
6,789✔
504
  pTask->pMsgCb = pMsgCb;
6,789✔
505
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
6,789✔
506
  if (pTask->msgInfo.pSendInfo == NULL) {
6,789!
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);
6,789✔
512
  if (code) {
6,789!
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};
6,789✔
518
  code = taosThreadMutexAttrInit(&attr);
6,789✔
519
  if (code != 0) {
6,789!
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);
6,789✔
525
  if (code != 0) {
6,789!
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);
6,789✔
531
  if (code) {
6,789!
532
    return code;
×
533
  }
534

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

540
  streamTaskOpenAllUpstreamInput(pTask);
6,789✔
541

542
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
6,789✔
543
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
6,789!
544
  if (pOutputInfo->pTokenBucket == NULL) {
6,789!
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);
6,789✔
552
  if (code) {
6,789!
553
    return code;
×
554
  }
555

556
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
6,789✔
557
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
6,789!
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));
6,789✔
563
  if (pTask->taskCheckInfo.pList == NULL) {
6,788!
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);
6,788✔
569
  if (code) {
6,788!
570
    return code;
×
571
  }
572

573
  if (pTask->chkInfo.pActiveInfo == NULL) {
6,788!
574
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
6,788✔
575
    if (code) {
6,789!
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);
6,789✔
582
}
583

584
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
40,514✔
585
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
40,514✔
586
    return 0;
3,007✔
587
  }
588

589
  int32_t type = pTask->outputInfo.type;
37,507✔
590
  if (type == TASK_OUTPUT__TABLE) {
37,507✔
591
    return 0;
276✔
592
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
37,231✔
593
    return 1;
6,129✔
594
  } else if (type == TASK_OUTPUT__VTABLE_MAP) {
31,102!
595
    SArray* pTaskInfos = pTask->outputInfo.vtableMapDispatcher.taskInfos;
×
596
    return taosArrayGetSize(pTaskInfos);
×
597
  } else {
598
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
31,102✔
599
    return taosArrayGetSize(vgInfo);
31,102✔
600
  }
601
}
602

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

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

611
  if (pTask->upstreamInfo.pList == NULL) {
8,465✔
612
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
3,144✔
613
  }
614

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

619
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
102✔
620
  int32_t code = 0;
102✔
621
  char    buf[512] = {0};
102✔
622
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
102✔
623
  if (code != 0) {                                // print error and continue
102!
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);
102✔
629
  for (int32_t i = 0; i < numOfUpstream; ++i) {
202✔
630
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
119✔
631
    if (pInfo->nodeId == nodeId) {
119✔
632
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
19✔
633
      if (!equal) {
19✔
634
        *pUpdated = true;
15✔
635

636
        char tmp[512] = {0};
15✔
637
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
15✔
638
        if (code != 0) {  // print error and continue
15!
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);
15✔
644
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
15!
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,
4!
648
                pInfo->taskId, nodeId, buf);
649
      }
650

651
      break;
19✔
652
    }
653
  }
654

655
  return code;
102✔
656
}
657

658
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
24,313✔
659
  if (pUpstreamInfo->pList != NULL) {
24,313✔
660
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
20,959✔
661
    pUpstreamInfo->numOfClosed = 0;
20,959✔
662
    pUpstreamInfo->pList = NULL;
20,959✔
663
  }
664
}
24,313✔
665

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

672
  pTask->outputInfo.type = TASK_OUTPUT__FIXED_DISPATCH;
599✔
673
  pTask->msgInfo.msgType = TDMT_STREAM_TASK_DISPATCH;
599✔
674
}
599✔
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;
17✔
689

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

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

701
          char tmp[512] = {0};
15✔
702
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
15✔
703
          if (code != 0) {  // print error and continue
15!
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);
15✔
709
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
15!
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,
×
713
                  pVgInfo->taskId, nodeId, buf);
714
        }
715
        break;
15✔
716
      }
717
    }
718
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
85!
719
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
85✔
720
    if (pDispatcher->nodeId == nodeId) {
85✔
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,333✔
777
  int32_t     vgId = pTask->pMeta->vgId;
2,333✔
778
  int64_t     st = taosGetTimestampMs();
2,333✔
779
  const char* id = pTask->id.idStr;
2,333✔
780

781
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,333✔
782
  if (code) {
2,333!
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,333✔
788
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS, 5000);
1,268✔
789
    if (code != TSDB_CODE_SUCCESS) {
1,268!
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,333!
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,333✔
801
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,333✔
802
  return code;
2,333✔
803
}
804

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

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

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

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

823
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
187✔
824
    if (code) {
187!
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;
69✔
830
}
831

832
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
6,787✔
833
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
6,787✔
834
    return;
3,473✔
835
  }
836

837
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
3,314✔
838
  for (int32_t i = 0; i < size; ++i) {
12,372✔
839
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
9,058✔
840
    pInfo->stage = -1;
9,057✔
841
  }
842

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

846
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
9,807✔
847
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
9,807✔
848
  if (num == 0) {
9,807✔
849
    return;
4,976✔
850
  }
851

852
  for (int32_t i = 0; i < num; ++i) {
18,403✔
853
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
13,574✔
854
    pInfo->dataAllowed = true;
13,572✔
855
  }
856

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

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

865
  if ((pInfo != NULL) && pInfo->dataAllowed) {
2,413!
866
    pInfo->dataAllowed = false;
2,413✔
867
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
2,413!
868
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
2,413✔
869
    } else {
870
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
871
    }
872
  }
873
}
2,413✔
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) {
28,049✔
891
  bool ret = false;
28,049✔
892

893
  streamMutexLock(&pTask->lock);
28,049✔
894
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
28,050✔
895
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
23,879✔
896
    ret = true;
23,879✔
897
  }
898

899
  streamMutexUnlock(&pTask->lock);
28,050✔
900
  return ret;
28,049✔
901
}
902

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

911
  return status;
23,270✔
912
}
913

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

920
  return status;
603✔
921
}
922

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

928
  if (pTask->info.fillHistory == 0) {
2,633!
929
    return code;
2,633✔
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) {
1,643✔
979
  int32_t                code = 0;
1,643✔
980
  int32_t                tlen = 0;
1,643✔
981
  int32_t                vgId = pTask->pMeta->vgId;
1,643✔
982
  const char*            id = pTask->id.idStr;
1,643✔
983
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
1,643✔
984

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

994
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
1,643!
995
  if (code < 0) {
1,643!
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);
1,643✔
1001
  if (buf == NULL) {
1,643!
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);
1,643✔
1009
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
1,643!
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);
1,643✔
1016

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

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

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

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

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

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

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

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

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

1075
  STaskStatusEntry entry = {
63,336✔
1076
      .id = streamTaskGetTaskId(pTask),
21,112✔
1077
      .status = streamTaskGetStatus(pTask).state,
21,112✔
1078
      .nodeId = pMeta->vgId,
21,112✔
1079
      .stage = pMeta->stage,
21,112✔
1080

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

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

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

1109
  // in case of fill-history task, stop the tsdb file scan operation.
1110
  if (pTask->info.fillHistory == 1) {
156!
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);
156!
1116
  return code;
156✔
1117
}
1118

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

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

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

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

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

1150
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
1,660✔
1151
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
1,660!
1152
  if (code < 0) {
1,660!
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);
1,660✔
1158
  if (buf == NULL) {
1,660!
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);
1,660✔
1165
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
1,660!
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);
1,660✔
1173

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

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

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

1184
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
26,984✔
1185
  for (int32_t i = 0; i < num; ++i) {
55,313!
1186
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
55,314✔
1187
    if (pInfo == NULL) {
55,314!
1188
      return;
×
1189
    }
1190

1191
    if (pInfo->taskId == taskId) {
55,314✔
1192
      *pEpInfo = pInfo;
26,985✔
1193
      return;
26,985✔
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) {
6,789✔
1235
  char    buf[128] = {0};
6,789✔
1236
  int32_t code = snprintf(buf, tListLen(buf), "0x%" PRIx64 "-0x%x", streamId, taskId);
6,789✔
1237
  if (code < 0 || code >= tListLen(buf)) {
6,789!
1238
    return TSDB_CODE_OUT_OF_BUFFER;
×
1239
  }
1240

1241
  *pId = taosStrdup(buf);
6,789!
1242

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

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

1254
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
408✔
1255
  if (code) {
408!
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;
408✔
1261
  pData->srcVgId = 0;
408✔
1262

1263
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
408✔
1264
  if (code != TSDB_CODE_SUCCESS) {
408!
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);
408✔
1271
  if (code != TSDB_CODE_SUCCESS) {
408!
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;
408✔
1277
}
1278

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

1287
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
2,633✔
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) {
28✔
1300
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
28✔
1301
  return TSDB_CODE_SUCCESS;
28✔
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) {
6,833✔
1312
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
6,833!
1313
  if (pInfo == NULL) {
6,833!
1314
    return terrno;
×
1315
  }
1316

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

1322
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
6,833✔
1323
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
6,833✔
1324
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
6,833✔
1325

1326
  *pRes = pInfo;
6,833✔
1327
  return code;
6,833✔
1328
}
1329

1330
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
24,313✔
1331
  if (pInfo == NULL) {
24,313✔
1332
    return;
17,484✔
1333
  }
1334

1335
  streamMutexDestroy(&pInfo->lock);
6,829✔
1336
  taosArrayDestroy(pInfo->pDispatchTriggerList);
6,829✔
1337
  pInfo->pDispatchTriggerList = NULL;
6,829✔
1338
  taosArrayDestroy(pInfo->pReadyMsgList);
6,829✔
1339
  pInfo->pReadyMsgList = NULL;
6,829✔
1340
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
6,829✔
1341
  pInfo->pCheckpointReadyRecvList = NULL;
6,829✔
1342

1343
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
6,829✔
1344
  if (pTriggerTmr->tmrHandle != NULL) {
6,829✔
1345
    streamTmrStop(pTriggerTmr->tmrHandle);
829✔
1346
    pTriggerTmr->tmrHandle = NULL;
829✔
1347
  }
1348

1349
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
6,829✔
1350
  if (pReadyTmr->tmrHandle != NULL) {
6,829✔
1351
    streamTmrStop(pReadyTmr->tmrHandle);
826✔
1352
    pReadyTmr->tmrHandle = NULL;
826✔
1353
  }
1354

1355
  taosMemoryFree(pInfo);
6,829!
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) {
1,334✔
1361
  pInfo->activeId = 0;
1,334✔
1362
  pInfo->transId = 0;
1,334✔
1363
  pInfo->allUpstreamTriggerRecv = 0;
1,334✔
1364
  pInfo->dispatchTrigger = false;
1,334✔
1365

1366
  taosArrayClear(pInfo->pDispatchTriggerList);
1,334✔
1367
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
1,334✔
1368
}
1,334✔
1369

1370
const char* streamTaskGetExecType(int32_t type) {
98,320✔
1371
  switch (type) {
98,320!
1372
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
56,668✔
1373
      return "scan-wal-file";
56,668✔
1374
    case STREAM_EXEC_T_START_ALL_TASKS:
8,048✔
1375
      return "start-all-tasks";
8,048✔
1376
    case STREAM_EXEC_T_START_ONE_TASK:
4,736✔
1377
      return "start-one-task";
4,736✔
1378
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
11✔
1379
      return "restart-all-tasks";
11✔
1380
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,815✔
1381
      return "stop-all-tasks";
4,815✔
1382
    case STREAM_EXEC_T_RESUME_TASK:
1,561✔
1383
      return "resume-task-from-idle";
1,561✔
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:
22,566✔
1389
      return "exec-all-tasks";
22,566✔
1390
    default:
×
1391
      return "invalid-exec-type";
×
1392
  }
1393
}
1394

1395
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
12,989✔
1396
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
12,989!
1397
  if (*pRefId != NULL) {
12,989!
1398
    **pRefId = pTask->id.refId;
12,989✔
1399
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
12,989✔
1400
    if (code != 0) {
12,989!
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;
12,989✔
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) {
12,952✔
1412
  if (pRefId == NULL) {
12,952✔
1413
    return;
1,786✔
1414
  }
1415

1416
  metaRefMgtRemove(pRefId);
11,166✔
1417
}
1418

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

1423
  QUERY_CHECK_NULL(pEncoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
54,463!
1424
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
54,463!
1425

1426
  int32_t addrSize = taosArrayGetSize(info->pNotifyAddrUrls);
54,463✔
1427
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, addrSize));
54,463!
1428
  for (int32_t i = 0; i < addrSize; ++i) {
54,463!
1429
    const char* url = taosArrayGetP(info->pNotifyAddrUrls, i);
×
1430
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, url));
×
1431
  }
1432
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyEventTypes));
108,926!
1433
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyErrorHandle));
108,926!
1434
  if (addrSize > 0) {
54,463!
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:
54,463✔
1441
  if (code != TSDB_CODE_SUCCESS) {
54,463!
1442
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1443
  }
1444
  return code;
54,461✔
1445
}
1446

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

1451
  QUERY_CHECK_NULL(pDecoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
17,817!
1452
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
17,817!
1453

1454
  int32_t addrSize = 0;
17,817✔
1455
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addrSize));
17,817!
1456
  info->pNotifyAddrUrls = taosArrayInit(addrSize, POINTER_BYTES);
17,817✔
1457
  QUERY_CHECK_NULL(info->pNotifyAddrUrls, code, lino, _exit, terrno);
17,817!
1458
  for (int32_t i = 0; i < addrSize; ++i) {
17,817!
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));
35,634!
1469
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyErrorHandle));
35,634!
1470
  if (addrSize > 0) {
17,817!
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:
17,817✔
1486
  if (code != TSDB_CODE_SUCCESS) {
17,817!
1487
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1488
  }
1489
  return code;
17,817✔
1490
}
1491

1492
int32_t tEncodeVTablesInfo(SEncoder* pEncoder, SArray* pVTables) {
54,461✔
1493
  int32_t code = TSDB_CODE_SUCCESS;
54,461✔
1494
  int32_t lino = 0;
54,461✔
1495
  SVCTableMergeInfo* pMergeInfo = NULL;
54,461✔
1496
  int32_t mergeNum = taosArrayGetSize(pVTables);
54,461✔
1497
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, mergeNum));
54,460!
1498
  for (int32_t i = 0; i < mergeNum; ++i) {
54,460!
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:
54,460✔
1505
  if (code != TSDB_CODE_SUCCESS) {
54,460!
1506
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1507
  }
1508
  return code;
54,460✔
1509
}
1510

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

1515
  int32_t mergeNum = 0;
17,817✔
1516
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &mergeNum));
17,817!
1517
  if (mergeNum > 0) {
17,817!
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) {
17,817!
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:
17,817✔
1532
  if (code != TSDB_CODE_SUCCESS) {
17,817!
1533
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1534
  }
1535
  return code;
17,817✔
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) {
54,508✔
1636
  int32_t code = 0;
54,508✔
1637
  int32_t lino;
1638

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

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

1651
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
109,014!
1652
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
109,014!
1653
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
54,507!
1654
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
54,508!
1655

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

1660
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
109,006!
1661
  int32_t taskId = pTask->hTaskInfo.id.taskId;
54,503✔
1662
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
54,503!
1663

1664
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
109,006!
1665
  taskId = pTask->streamTaskId.taskId;
54,503✔
1666
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
54,503!
1667

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

1673
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
54,503✔
1674
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
54,508!
1675
  for (int32_t i = 0; i < epSz; i++) {
126,983✔
1676
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
72,476✔
1677
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
72,474!
1678
  }
1679

1680
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
54,507✔
1681
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
59,368!
1682
  }
1683

1684
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
54,507✔
1685
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
54,298!
1686
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
54,298!
1687
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
54,298!
1688
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
27,358!
1689
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
×
1690
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
27,358!
1691
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1692
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
27,358✔
1693
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
10,908!
1694
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
10,908!
1695
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
5,454!
1696
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
21,904✔
1697
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
21,848!
1698
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
43,692!
1699
  } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
56!
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));
109,010!
1704
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
109,010!
1705
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
109,010!
1706

1707
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
54,505✔
1708
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.hasAggTasks));
108,972✔
1709
    TAOS_CHECK_EXIT(tEncodeStreamNotifyInfo(pEncoder, &pTask->notifyInfo));
54,464!
1710
    TAOS_CHECK_EXIT(tEncodeVTablesInfo(pEncoder, pTask->pVTables));
54,462!
1711
  }
1712

1713
  tEndEncode(pEncoder);
54,479✔
1714
_exit:
54,506✔
1715
  return code;
54,506✔
1716
}
1717

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

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

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

1736
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
35,634!
1737
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
35,634!
1738

1739
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
35,634!
1740
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
35,634!
1741
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
17,817!
1742
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
17,815!
1743

1744
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
35,634!
1745
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
35,634!
1746
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
35,634!
1747

1748
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
35,634!
1749
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
17,817!
1750
  pTask->hTaskInfo.id.taskId = taskId;
17,817✔
1751

1752
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
35,634!
1753
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
17,816!
1754
  pTask->streamTaskId.taskId = taskId;
17,816✔
1755

1756
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
35,633!
1757
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
35,634!
1758
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
35,634!
1759
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
35,634!
1760

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

1764
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
17,817!
1765
    TAOS_CHECK_EXIT(terrno);
×
1766
  }
1767
  for (int32_t i = 0; i < epSz; i++) {
41,559✔
1768
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
23,744!
1769
    if (pInfo == NULL) {
23,745!
1770
      TAOS_CHECK_EXIT(terrno);
×
1771
    }
1772
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
23,745!
1773
      taosMemoryFreeClear(pInfo);
×
1774
      goto _exit;
×
1775
    }
1776
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
47,486!
1777
      TAOS_CHECK_EXIT(terrno);
×
1778
    }
1779
  }
1780

1781
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
17,815✔
1782
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
19,288!
1783
  }
1784

1785
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
17,815✔
1786
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
17,730!
1787
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
8,865!
1788
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
8,865!
1789
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
8,864!
1790
      TAOS_CHECK_EXIT(terrno);
×
1791
    }
1792
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
17,727!
1793
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
8,950!
1794
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
×
1795
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
8,950!
1796
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1797
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
8,950✔
1798
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
3,532!
1799
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
3,532!
1800
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
1,766!
1801
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
7,184✔
1802
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
7,182!
1803
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
7,181!
1804
  } else if (pTask->outputInfo.type == TASK_OUTPUT__VTABLE_MAP) {
2!
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));
35,627!
1809
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
17,815!
1810
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
35,630!
1811
  }
1812
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
17,815!
1813

1814
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
17,817!
1815
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.hasAggTasks));
35,634!
1816
    TAOS_CHECK_EXIT(tDecodeStreamNotifyInfo(pDecoder, &pTask->notifyInfo));
17,817!
1817
    TAOS_CHECK_EXIT(tDecodeVTablesInfo(pDecoder, &pTask->pVTables));
17,817!
1818
  }
1819

1820
  tEndDecode(pDecoder);
17,817✔
1821

1822
_exit:
17,825✔
1823
  return code;
17,825✔
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