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

taosdata / TDengine / #3581

15 Jan 2025 01:12AM UTC coverage: 63.809% (+0.3%) from 63.556%
#3581

push

travis-ci

web-flow
Merge pull request #29561 from taosdata/fix/TD-33504

fix:[TD-33504]add test case

141492 of 284535 branches covered (49.73%)

Branch coverage included in aggregate %.

219814 of 281694 relevant lines covered (78.03%)

19173854.68 hits per line

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

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

16
#include "executor.h"
17
#include "osDir.h"
18
#include "osMemory.h"
19
#include "streamInt.h"
20
#include "streamsm.h"
21
#include "tmisce.h"
22
#include "tstream.h"
23
#include "ttimer.h"
24
#include "wal.h"
25
#include "streamMsg.h"
26

27
static void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo);
28
static int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated);
29
static int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdate);
30
static void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo);
31

32
static int32_t addToTaskset(SArray* pArray, SStreamTask* pTask) {
13,977✔
33
  int32_t childId = taosArrayGetSize(pArray);
13,977✔
34
  pTask->info.selfChildId = childId;
13,977✔
35
  void* p = taosArrayPush(pArray, &pTask);
13,977✔
36
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
13,977!
37
}
38

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

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

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

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

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

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

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

104
  return pEpInfo;
20,086✔
105
}
106

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

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

119
  pTask->ver = SSTREAM_TASK_VER;
13,977✔
120
  pTask->id.taskId = tGenIdPI32();
13,977✔
121
  pTask->id.streamId = streamId;
13,977✔
122

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

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

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

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

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

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

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

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

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

169
  return code;
13,977✔
170
}
171

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

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

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

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

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

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

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

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

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

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

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

226
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
63,220✔
227

228
  ETaskStatus status1 = TASK_STATUS__UNINIT;
63,220✔
229
  streamMutexLock(&pTask->lock);
63,220✔
230
  if (pTask->status.pSM != NULL) {
63,237✔
231
    SStreamTaskState status = streamTaskGetStatus(pTask);
28,641✔
232
    p = status.name;
28,631✔
233
    status1 = status.state;
28,631✔
234
  }
235
  streamMutexUnlock(&pTask->lock);
63,227✔
236

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

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

251
  if (pTask->hTaskInfo.pTimer != NULL) {
63,224✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
1,822✔
253
    pTask->hTaskInfo.pTimer = NULL;
1,816✔
254
  }
255

256
  if (pTask->msgInfo.pRetryTmr != NULL) {
63,218✔
257
    streamTmrStop(pTask->msgInfo.pRetryTmr);
5,399✔
258
    pTask->msgInfo.pRetryTmr = NULL;
5,401✔
259
  }
260

261
  if (pTask->inputq.queue) {
63,220✔
262
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
14,633✔
263
    pTask->inputq.queue = NULL;
14,631✔
264
  }
265

266
  if (pTask->outputq.queue) {
63,218✔
267
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
14,635✔
268
    pTask->outputq.queue = NULL;
14,637✔
269
  }
270

271
  if (pTask->exec.qmsg) {
63,220✔
272
    taosMemoryFree(pTask->exec.qmsg);
33,361!
273
  }
274

275
  if (pTask->exec.pExecutor) {
63,220✔
276
    qDestroyTask(pTask->exec.pExecutor);
7,377✔
277
    pTask->exec.pExecutor = NULL;
7,377✔
278
  }
279

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

285
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
63,220✔
286

287
  if (pTask->msgInfo.pData != NULL) {
63,238✔
288
    clearBufferedDispatchMsg(pTask);
81✔
289
  }
290

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
63,240✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
31,033!
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
31,029!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
31,034✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
31,036✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
32,207✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
27,281✔
298
  }
299

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

303
  if (pTask->pNameMap) {
63,235✔
304
    tSimpleHashCleanup(pTask->pNameMap);
2,352✔
305
  }
306

307
  streamDestroyStateMachine(pTask->status.pSM);
63,235✔
308
  pTask->status.pSM = NULL;
63,238✔
309

310
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
63,238✔
311

312
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
63,235!
313
  streamMutexDestroy(&pTask->lock);
63,238✔
314

315
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
63,238✔
316
  pTask->msgInfo.pSendInfo = NULL;
63,240✔
317
  streamMutexDestroy(&pTask->msgInfo.lock);
63,240✔
318

319
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
63,239✔
320
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
63,241✔
321

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

326
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
63,240✔
327
  pTask->chkInfo.pActiveInfo = NULL;
63,238✔
328

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

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

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

354
  if (pTask->backendPath != NULL) {
63,227✔
355
    taosMemoryFree(pTask->backendPath);
14,631!
356
    pTask->backendPath = NULL;
14,632✔
357
  }
358
}
63,228✔
359

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

497
  streamTaskOpenAllUpstreamInput(pTask);
14,847✔
498

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

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

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

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

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

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

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

541
  int32_t type = pTask->outputInfo.type;
115,190✔
542
  if (type == TASK_OUTPUT__TABLE) {
115,190✔
543
    return 0;
272✔
544
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
114,918✔
545
    return 1;
11,204✔
546
  } else {
547
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
103,714✔
548
    return taosArrayGetSize(vgInfo);
103,714✔
549
  }
550
}
551

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

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

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

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

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

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

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

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

600
      break;
151✔
601
    }
602
  }
603

604
  return code;
213✔
605
}
606

607
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
63,234✔
608
  if (pUpstreamInfo->pList != NULL) {
63,234✔
609
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
56,165✔
610
    pUpstreamInfo->numOfClosed = 0;
56,162✔
611
    pUpstreamInfo->pList = NULL;
56,162✔
612
  }
613
}
63,231✔
614

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

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

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

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

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

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

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

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

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

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

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

691
  return code;
213✔
692
}
693

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

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

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

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

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

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

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

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

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

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

747
  return updated;
218✔
748
}
749

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

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

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

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

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

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

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

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

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

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

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

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

811
  streamMutexLock(&pTask->lock);
107,634✔
812
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
107,643✔
813
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
71,522✔
814
    ret = true;
71,522✔
815
  }
816

817
  streamMutexUnlock(&pTask->lock);
107,643✔
818
  return ret;
107,650✔
819
}
820

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

829
  return status;
70,362✔
830
}
831

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

838
  return status;
1,115✔
839
}
840

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

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

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

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

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

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

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

871
  return code;
4✔
872
}
873

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

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

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

893
  return code;
4✔
894
}
895

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

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

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

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

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

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

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

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

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

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

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

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

984
  pDst->startTime = pSrc->startTime;
65,495✔
985
  pDst->hTaskId = pSrc->hTaskId;
65,495✔
986
}
65,495✔
987

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

992
  STaskStatusEntry entry = {
199,353✔
993
      .id = streamTaskGetTaskId(pTask),
66,451✔
994
      .status = streamTaskGetStatus(pTask).state,
66,451✔
995
      .nodeId = pMeta->vgId,
66,451✔
996
      .stage = pMeta->stage,
66,451✔
997

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1100
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
96,689✔
1101
  for (int32_t i = 0; i < num; ++i) {
193,065!
1102
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
193,077✔
1103
    if (pInfo == NULL) {
193,023!
1104
      return;
×
1105
    }
1106

1107
    if (pInfo->taskId == taskId) {
193,023✔
1108
      *pEpInfo = pInfo;
96,669✔
1109
      return;
96,669✔
1110
    }
1111
  }
1112

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

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

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

1135
  return NULL;
×
1136
}
1137

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

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

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

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

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

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

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

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

1180
  return code;
560✔
1181
}
1182

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

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

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

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

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

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

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

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

1226
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
14,920✔
1227
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
14,927✔
1228
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
14,927✔
1229

1230
  *pRes = pInfo;
14,929✔
1231
  return code;
14,929✔
1232
}
1233

1234
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
63,234✔
1235
  if (pInfo == NULL) {
63,234✔
1236
    return;
48,565✔
1237
  }
1238

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

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

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

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

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

1270
  taosArrayClear(pInfo->pDispatchTriggerList);
5,548✔
1271
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,534✔
1272
}
5,532✔
1273

1274
const char* streamTaskGetExecType(int32_t type) {
99,434✔
1275
  switch (type) {
99,434!
1276
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
34,125✔
1277
      return "scan-wal-file";
34,125✔
1278
    case STREAM_EXEC_T_START_ALL_TASKS:
8,390✔
1279
      return "start-all-tasks";
8,390✔
1280
    case STREAM_EXEC_T_START_ONE_TASK:
5,816✔
1281
      return "start-one-task";
5,816✔
1282
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
44✔
1283
      return "restart-all-tasks";
44✔
1284
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,769✔
1285
      return "stop-all-tasks";
4,769✔
1286
    case STREAM_EXEC_T_RESUME_TASK:
7,623✔
1287
      return "resume-task-from-idle";
7,623✔
1288
    case STREAM_EXEC_T_ADD_FAILED_TASK:
2✔
1289
      return "record-start-failed-task";
2✔
1290
    case 0:
38,723✔
1291
      return "exec-all-tasks";
38,723✔
1292
    default:
×
1293
      return "invalid-exec-type";
×
1294
  }
1295
}
1296

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

1313
void streamTaskFreeRefId(int64_t* pRefId) {
39,732✔
1314
  if (pRefId == NULL) {
39,732✔
1315
    return;
2,676✔
1316
  }
1317

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

1321

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

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

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

1338
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
283,796!
1339
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
283,796!
1340
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
141,898!
1341
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
141,911!
1342

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

1347
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
283,816!
1348
  int32_t taskId = pTask->hTaskInfo.id.taskId;
141,908✔
1349
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
141,908!
1350

1351
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
283,816!
1352
  taskId = pTask->streamTaskId.taskId;
141,908✔
1353
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
141,908!
1354

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

1360
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
141,908✔
1361
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
141,895!
1362
  for (int32_t i = 0; i < epSz; i++) {
336,748✔
1363
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
194,839✔
1364
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
194,806!
1365
  }
1366

1367
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
141,909✔
1368
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
150,726!
1369
  }
1370

1371
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
141,909✔
1372
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
139,572!
1373
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
139,572!
1374
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
139,572!
1375
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
72,123✔
1376
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
780!
1377
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
71,733!
1378
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1379
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
71,733✔
1380
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
22,492!
1381
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
22,492!
1382
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
11,246!
1383
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
60,487✔
1384
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
60,424!
1385
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
120,848!
1386
  }
1387
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
283,818!
1388
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
283,818!
1389
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
283,818!
1390

1391
  tEndEncode(pEncoder);
141,909✔
1392
_exit:
141,883✔
1393
  return code;
141,883✔
1394
}
1395

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

1401
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
49,483!
1402
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
98,971!
1403
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
49,484!
1404
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
2!
1405
  }
1406

1407
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
98,965!
1408
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
98,959!
1409
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
98,953!
1410
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
98,946!
1411
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
98,944!
1412
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
98,946!
1413

1414
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
98,939!
1415
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
98,930!
1416

1417
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
98,938!
1418
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
98,946!
1419
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
49,472!
1420
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
49,483!
1421

1422
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
98,977!
1423
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
98,975!
1424
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
98,960!
1425

1426
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
98,946!
1427
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
49,484!
1428
  pTask->hTaskInfo.id.taskId = taskId;
49,484✔
1429

1430
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
98,963!
1431
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
49,476!
1432
  pTask->streamTaskId.taskId = taskId;
49,476✔
1433

1434
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
98,953!
1435
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
98,953!
1436
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
98,950!
1437
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
98,948!
1438

1439
  int32_t epSz = -1;
49,474✔
1440
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &epSz) < 0);
49,482!
1441

1442
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
49,482!
1443
    TAOS_CHECK_EXIT(terrno);
×
1444
  }
1445
  for (int32_t i = 0; i < epSz; i++) {
117,063✔
1446
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
67,580!
1447
    if (pInfo == NULL) {
67,555!
1448
      TAOS_CHECK_EXIT(terrno);
×
1449
    }
1450
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
67,555!
1451
      taosMemoryFreeClear(pInfo);
×
1452
      goto _exit;
×
1453
    }
1454
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
135,159!
1455
      TAOS_CHECK_EXIT(terrno);
×
1456
    }
1457
  }
1458

1459
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
49,483✔
1460
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
52,233!
1461
  }
1462

1463
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
49,482✔
1464
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
48,644!
1465
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
24,322!
1466
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
24,314!
1467
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
24,317!
1468
      TAOS_CHECK_EXIT(terrno);
×
1469
    }
1470
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
48,618!
1471
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
25,160✔
1472
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
274!
1473
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
25,023!
1474
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1475
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
25,023✔
1476
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
7,474!
1477
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
7,474!
1478
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,737!
1479
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
21,286✔
1480
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
21,265!
1481
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
21,265!
1482
  }
1483
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
98,943!
1484
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
49,484!
1485
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
98,968!
1486
  }
1487
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
49,482!
1488

1489
  tEndDecode(pDecoder);
49,485✔
1490

1491
_exit:
49,484✔
1492
  return code;
49,484✔
1493
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc