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

taosdata / TDengine / #3627

02 Mar 2025 11:16PM UTC coverage: 63.596% (-0.2%) from 63.764%
#3627

push

travis-ci

GitHub
Merge pull request #29973 from taosdata/doc/internal

148665 of 299855 branches covered (49.58%)

Branch coverage included in aggregate %.

233076 of 300407 relevant lines covered (77.59%)

17543856.65 hits per line

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

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

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

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

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

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

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

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

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

104
  return pEpInfo;
20,156✔
105
}
106

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

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

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

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

135
  char    buf[128] = {0};
14,043✔
136
  int32_t ret = snprintf(buf, tListLen(buf), "0x%" PRIx64 "-0x%x", pTask->id.streamId, pTask->id.taskId);
14,043✔
137
  if (ret < 0 || ret >= tListLen(buf)) {
14,043!
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);
14,043!
143
  if (pTask->id.idStr == NULL) {
14,043!
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;
14,043✔
149
  pTask->status.taskStatus = fillHistory ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
14,043✔
150
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,043✔
151
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,043✔
152

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

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

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

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

169
  return code;
14,043✔
170
}
171

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

251
  if (pTask->hTaskInfo.pTimer != NULL) {
62,946✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
2,023✔
253
    pTask->hTaskInfo.pTimer = NULL;
2,021✔
254
  }
255

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

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

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

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

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

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

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

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

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
62,960✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
30,919✔
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
30,920!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
30,918✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
30,922✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
32,041✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
27,210✔
298
  }
299

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

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

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

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

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

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

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

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

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

329
  taosArrayDestroyP(pTask->notifyInfo.pNotifyAddrUrls, NULL);
62,960✔
330
  taosMemoryFreeClear(pTask->notifyInfo.streamName);
62,963!
331
  taosMemoryFreeClear(pTask->notifyInfo.stbFullName);
62,963!
332
  tDeleteSchemaWrapper(pTask->notifyInfo.pSchemaWrapper);
62,963!
333

334
  pTask->notifyEventStat = (STaskNotifyEventStat){0};
62,958✔
335

336
  taosMemoryFree(pTask);
62,958!
337
  stDebug("s-task:0x%x free task completed", taskId);
62,962✔
338
}
62,962✔
339

340
void streamFreeTaskState(SStreamTask* pTask, int8_t remove) {
62,956✔
341
  stDebug("s-task:0x%x start to free task state/backend", pTask->id.taskId);
62,956✔
342
  if (pTask->pState != NULL) {
62,958✔
343
    stDebug("s-task:0x%x start to free task state", pTask->id.taskId);
7,482✔
344
    streamStateClose(pTask->pState, remove);
7,482✔
345

346
    if (remove) taskDbSetClearFileFlag(pTask->pBackend);
7,483✔
347
    taskDbRemoveRef(pTask->pBackend);
7,483✔
348
    pTask->pBackend = NULL;
7,483✔
349
    pTask->pState = NULL;
7,483✔
350
  } else {
351
    stDebug("s-task:0x%x task state is NULL, may del backend:%s", pTask->id.taskId,
55,476✔
352
            pTask->backendPath ? pTask->backendPath : "NULL");
353
    if (remove) {
55,476✔
354
      if (pTask->backendPath != NULL) {
3,471!
355
        stDebug("s-task:0x%x task state is NULL, do del backend:%s", pTask->id.taskId, pTask->backendPath);
3,472✔
356
        taosRemoveDir(pTask->backendPath);
3,472✔
357
      }
358
    }
359
  }
360

361
  if (pTask->backendPath != NULL) {
62,956✔
362
    taosMemoryFree(pTask->backendPath);
14,761!
363
    pTask->backendPath = NULL;
14,761✔
364
  }
365
}
62,956✔
366

367
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
14,749✔
368
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,749✔
369
  SDataRange*      pRange = &pTask->dataRange;
14,749✔
370

371
  // only set the version info for stream tasks without fill-history task
372
  if ((pTask->info.fillHistory == 0) && (!HAS_RELATED_FILLHISTORY_TASK(pTask))) {
14,749✔
373
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
4,821✔
374
    pChkInfo->processedVer = ver - 1;   // already processed version
4,821✔
375
    pChkInfo->nextProcessVer = ver;     // next processed version
4,821✔
376

377
    pRange->range.maxVer = ver;
4,821✔
378
    pRange->range.minVer = ver;
4,821✔
379
  } else {
380
    // the initial value of processedVer/nextProcessVer/checkpointVer for stream task with related fill-history task
381
    // is set at the mnode.
382
    if (pTask->info.fillHistory == 1) {
9,928✔
383
      pChkInfo->checkpointVer = pRange->range.maxVer;
5,025✔
384
      pChkInfo->processedVer = pRange->range.maxVer;
5,025✔
385
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
5,025✔
386
    } else {
387
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
4,903✔
388
      pChkInfo->processedVer = pRange->range.minVer - 1;
4,903✔
389
      pChkInfo->nextProcessVer = pRange->range.minVer;
4,903✔
390

391
      {  // for compatible purpose, remove it later
392
        if (pRange->range.minVer == 0) {
4,903✔
393
          pChkInfo->checkpointVer = 0;
2,474✔
394
          pChkInfo->processedVer = 0;
2,474✔
395
          pChkInfo->nextProcessVer = 1;
2,474✔
396
          stDebug("s-task:%s update the processedVer to 0 from -1 due to compatible purpose", pTask->id.idStr);
2,474✔
397
        }
398
      }
399
    }
400
  }
401
}
14,749✔
402

403
int32_t streamTaskSetBackendPath(SStreamTask* pTask) {
14,759✔
404
  int64_t streamId = 0;
14,759✔
405
  int32_t taskId = 0;
14,759✔
406

407
  if (pTask->info.fillHistory) {
14,759✔
408
    streamId = pTask->streamTaskId.streamId;
5,026✔
409
    taskId = pTask->streamTaskId.taskId;
5,026✔
410
  } else {
411
    streamId = pTask->id.streamId;
9,733✔
412
    taskId = pTask->id.taskId;
9,733✔
413
  }
414

415
  char    id[128] = {0};
14,759✔
416
  int32_t nBytes = snprintf(id, tListLen(id), "0x%" PRIx64 "-0x%x", streamId, taskId);
14,759✔
417
  if (nBytes < 0 || nBytes >= sizeof(id)) {
14,759!
418
    return TSDB_CODE_OUT_OF_BUFFER;
×
419
  }
420

421
  int32_t len = strlen(pTask->pMeta->path);
14,765✔
422
  pTask->backendPath = (char*)taosMemoryMalloc(len + nBytes + 2);
14,765!
423
  if (pTask->backendPath == NULL) {
14,761!
424
    return terrno;
×
425
  }
426

427
  int32_t code = snprintf(pTask->backendPath, len + nBytes + 2, "%s%s%s", pTask->pMeta->path, TD_DIRSEP, id);
14,761✔
428
  if (code < 0 || code >= len + nBytes + 2) {
14,761!
429
    stError("s-task:%s failed to set backend path:%s, code: out of buffer", pTask->id.idStr, pTask->backendPath);
×
430
    return TSDB_CODE_OUT_OF_BUFFER;
×
431
  } else {
432
    stDebug("s-task:%s set backend path:%s", pTask->id.idStr, pTask->backendPath);
14,765✔
433
    return 0;
14,763✔
434
  }
435
}
436

437
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
14,750✔
438
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
14,750✔
439
  if (code) {
14,756!
440
    stError("0x%x failed create stream task id str, code:%s", pTask->id.taskId, tstrerror(code));
×
441
    return code;
×
442
  }
443

444
  pTask->id.refId = 0;
14,756✔
445
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,756✔
446
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,756✔
447

448
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
14,756✔
449
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
14,750✔
450
  if (code1 || code2) {
14,764!
451
    stError("s-task:%s failed to prepare the input/output queue, initialize task failed", pTask->id.idStr);
×
452
    return TSDB_CODE_OUT_OF_MEMORY;
×
453
  }
454

455
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
14,764✔
456

457
  code = streamCreateStateMachine(pTask);
14,764✔
458
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
14,756!
459
    stError("s-task:%s failed create state-machine for stream task, initialization failed, code:%s", pTask->id.idStr,
1!
460
            tstrerror(code));
461
    return code;
×
462
  }
463

464
  pTask->execInfo.created = taosGetTimestampMs();
14,758✔
465
  setInitialVersionInfo(pTask, ver);
14,758✔
466

467
  pTask->pMeta = pMeta;
14,754✔
468
  pTask->pMsgCb = pMsgCb;
14,754✔
469
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
14,754✔
470
  if (pTask->msgInfo.pSendInfo == NULL) {
14,756!
471
    stError("s-task:%s failed to create sendInfo struct for stream task, code:Out of memory", pTask->id.idStr);
×
472
    return terrno;
×
473
  }
474

475
  code = taosThreadMutexInit(&pTask->msgInfo.lock, NULL);
14,756✔
476
  if (code) {
14,757!
477
    stError("s-task:0x%x failed to init msgInfo mutex, code:%s", pTask->id.taskId, tstrerror(code));
×
478
    return code;
×
479
  }
480

481
  TdThreadMutexAttr attr = {0};
14,757✔
482
  code = taosThreadMutexAttrInit(&attr);
14,757✔
483
  if (code != 0) {
14,746!
484
    stError("s-task:%s initElapsed mutex attr failed, code:%s", pTask->id.idStr, tstrerror(code));
×
485
    return code;
×
486
  }
487

488
  code = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
14,746✔
489
  if (code != 0) {
14,743!
490
    stError("s-task:%s set mutex attr recursive, code:%s", pTask->id.idStr, tstrerror(code));
×
491
    return code;
×
492
  }
493

494
  code = taosThreadMutexInit(&pTask->lock, &attr);
14,743✔
495
  if (code) {
14,754!
496
    return code;
×
497
  }
498

499
  code = taosThreadMutexAttrDestroy(&attr);
14,754✔
500
  if (code) {
14,740!
501
    return code;
×
502
  }
503

504
  streamTaskOpenAllUpstreamInput(pTask);
14,740✔
505

506
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,739✔
507
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
14,739!
508
  if (pOutputInfo->pTokenBucket == NULL) {
14,760!
509
    stError("s-task:%s failed to prepare the tokenBucket, code:%s", pTask->id.idStr, tstrerror(terrno));
×
510
    return terrno;
×
511
  }
512

513
  // 2MiB per second for sink task
514
  // 50 times sink operator per second
515
  code = streamTaskInitTokenBucket(pOutputInfo->pTokenBucket, 35, 35, tsSinkDataRate, pTask->id.idStr);
14,760✔
516
  if (code) {
14,760!
517
    return code;
×
518
  }
519

520
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
14,760✔
521
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
14,765!
522
    stError("s-task:%s failed to prepare downstreamUpdateList, code:%s", pTask->id.idStr, tstrerror(terrno));
×
523
    return terrno;
×
524
  }
525

526
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
14,765✔
527
  if (pTask->taskCheckInfo.pList == NULL) {
14,757!
528
    stError("s-task:%s failed to prepare taskCheckInfo list, code:%s", pTask->id.idStr, tstrerror(terrno));
×
529
    return terrno;
×
530
  }
531

532
  if (pTask->chkInfo.pActiveInfo == NULL) {
14,757✔
533
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
14,756✔
534
    if (code) {
14,759!
535
      stError("s-task:%s failed to create active checkpoint info, code:%s", pTask->id.idStr, tstrerror(code));
×
536
      return code;
×
537
    }
538
  }
539

540
  return streamTaskSetBackendPath(pTask);
14,760✔
541
}
542

543
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
121,465✔
544
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
121,465✔
545
    return 0;
6,796✔
546
  }
547

548
  int32_t type = pTask->outputInfo.type;
114,669✔
549
  if (type == TASK_OUTPUT__TABLE) {
114,669✔
550
    return 0;
269✔
551
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
114,400✔
552
    return 1;
10,816✔
553
  } else {
554
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
103,584✔
555
    return taosArrayGetSize(vgInfo);
103,584✔
556
  }
557
}
558

559
int32_t streamTaskGetNumOfUpstream(const SStreamTask* pTask) { return taosArrayGetSize(pTask->upstreamInfo.pList); }
19,266✔
560

561
int32_t streamTaskSetUpstreamInfo(SStreamTask* pTask, const SStreamTask* pUpstreamTask) {
20,156✔
562
  SStreamUpstreamEpInfo* pEpInfo = createStreamTaskEpInfo(pUpstreamTask);
20,156✔
563
  if (pEpInfo == NULL) {
20,156!
564
    return terrno;
×
565
  }
566

567
  if (pTask->upstreamInfo.pList == NULL) {
20,156✔
568
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
6,972✔
569
  }
570

571
  void* p = taosArrayPush(pTask->upstreamInfo.pList, &pEpInfo);
20,156✔
572
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
20,156!
573
}
574

575
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
171✔
576
  int32_t code = 0;
171✔
577
  char    buf[512] = {0};
171✔
578
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
171✔
579
  if (code != 0) {  // print error and continue
171!
580
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
581
    return code;
×
582
  }
583

584
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
171✔
585
  for (int32_t i = 0; i < numOfUpstream; ++i) {
329✔
586
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
238✔
587
    if (pInfo->nodeId == nodeId) {
238✔
588
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
80✔
589
      if (!equal) {
80✔
590
        *pUpdated = true;
28✔
591

592
        char tmp[512] = {0};
28✔
593
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
28✔
594
        if (code != 0) {  // print error and continue
28!
595
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
596
          return code;
×
597
        }
598

599
        epsetAssign(&pInfo->epSet, pEpSet);
28✔
600
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
28✔
601
                pInfo->taskId, nodeId, buf, tmp);
602
      } else {
603
        stDebug("s-task:0x%x not update upstreamInfo, since identical, task:0x%x(nodeId:%d) epset:%s", pTask->id.taskId,
52!
604
                pInfo->taskId, nodeId, buf);
605
      }
606

607
      break;
80✔
608
    }
609
  }
610

611
  return code;
171✔
612
}
613

614
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
62,951✔
615
  if (pUpstreamInfo->pList != NULL) {
62,951✔
616
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
55,843✔
617
    pUpstreamInfo->numOfClosed = 0;
55,850✔
618
    pUpstreamInfo->pList = NULL;
55,850✔
619
  }
620
}
62,958✔
621

622
void streamTaskSetFixedDownstreamInfo(SStreamTask* pTask, const SStreamTask* pDownstreamTask) {
948✔
623
  STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
948✔
624
  pDispatcher->taskId = pDownstreamTask->id.taskId;
948✔
625
  pDispatcher->nodeId = pDownstreamTask->info.nodeId;
948✔
626
  pDispatcher->epSet = pDownstreamTask->info.epSet;
948✔
627

628
  pTask->outputInfo.type = TASK_OUTPUT__FIXED_DISPATCH;
948✔
629
  pTask->msgInfo.msgType = TDMT_STREAM_TASK_DISPATCH;
948✔
630
}
948✔
631

632
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
171✔
633
  char    buf[512] = {0};
171✔
634
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
171✔
635
  if (code != 0) {                                        // print error and continue
171!
636
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
637
    return code;
×
638
  }
639

640
  int32_t id = pTask->id.taskId;
171✔
641
  int8_t  type = pTask->outputInfo.type;
171✔
642

643
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
171✔
644
    SArray* pVgs = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
78✔
645

646
    for (int32_t i = 0; i < taosArrayGetSize(pVgs); i++) {
147✔
647
      SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
145✔
648
      if (pVgInfo == NULL) {
145!
649
        continue;
×
650
      }
651

652
      if (pVgInfo->vgId == nodeId) {
145✔
653
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
76✔
654
        if (!isEqual) {
76✔
655
          *pUpdated = true;
28✔
656

657
          char tmp[512] = {0};
28✔
658
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
28✔
659
          if (code != 0) {  // print error and continue
28!
660
            stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
661
            return code;
×
662
          }
663

664
          epsetAssign(&pVgInfo->epSet, pEpSet);
28✔
665
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
28✔
666
                  nodeId, buf, tmp);
667
        } else {
668
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
48!
669
                  pVgInfo->taskId, nodeId, buf);
670
        }
671
        break;
76✔
672
      }
673
    }
674
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
93!
675
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
93✔
676
    if (pDispatcher->nodeId == nodeId) {
93✔
677
      bool equal = isEpsetEqual(&pDispatcher->epSet, pEpSet);
4✔
678
      if (!equal) {
4!
679
        *pUpdated = true;
×
680

681
        char tmp[512] = {0};
×
682
        code = epsetToStr(&pDispatcher->epSet, tmp, tListLen(tmp));
×
683
        if (code != 0) {  // print error and continue
×
684
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
685
          return code;
×
686
        }
687

688
        epsetAssign(&pDispatcher->epSet, pEpSet);
×
689
        stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pDispatcher->taskId,
×
690
                nodeId, buf, tmp);
691
      } else {
692
        stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
4!
693
                pDispatcher->taskId, nodeId, buf);
694
      }
695
    }
696
  }
697

698
  return code;
171✔
699
}
700

701
int32_t streamTaskStop(SStreamTask* pTask) {
2,942✔
702
  int32_t     vgId = pTask->pMeta->vgId;
2,942✔
703
  int64_t     st = taosGetTimestampMs();
2,942✔
704
  const char* id = pTask->id.idStr;
2,942✔
705

706
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,942✔
707
  if (code) {
2,942!
708
    stError("failed to handle STOP event, s-task:%s, code:%s", id, tstrerror(code));
×
709
    return code;
×
710
  }
711

712
  if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
2,942✔
713
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
1,530✔
714
    if (code != TSDB_CODE_SUCCESS) {
1,530!
715
      stError("s-task:%s failed to kill task related query handle, code:%s", id, tstrerror(code));
×
716
    }
717
  }
718

719
  while (!streamTaskIsIdle(pTask)) {
2,942!
720
    stDebug("s-task:%s level:%d wait for task to be idle and then close, check again in 100ms", id,
×
721
            pTask->info.taskLevel);
722
    taosMsleep(100);
×
723
  }
724

725
  int64_t el = taosGetTimestampMs() - st;
2,942✔
726
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,942✔
727
  return code;
2,942✔
728
}
729

730
bool streamTaskUpdateEpsetInfo(SStreamTask* pTask, SArray* pNodeList) {
145✔
731
  STaskExecStatisInfo* p = &pTask->execInfo;
145✔
732

733
  int32_t numOfNodes = taosArrayGetSize(pNodeList);
145✔
734
  int64_t prevTs = p->latestUpdateTs;
145✔
735

736
  p->latestUpdateTs = taosGetTimestampMs();
145✔
737
  p->updateCount += 1;
145✔
738
  stDebug("s-task:0x%x update task nodeEp epset, updatedNodes:%d, updateCount:%d, prevTs:%" PRId64, pTask->id.taskId,
145✔
739
          numOfNodes, p->updateCount, prevTs);
740

741
  bool updated = false;
145✔
742
  for (int32_t i = 0; i < numOfNodes; ++i) {
466✔
743
    SNodeUpdateInfo* pInfo = taosArrayGet(pNodeList, i);
321✔
744
    if (pInfo == NULL) {
321!
745
      continue;
×
746
    }
747

748
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
321✔
749
    if (code) {
321!
750
      stError("s-task:0x%x failed to update the task nodeEp epset, code:%s", pTask->id.taskId, tstrerror(code));
×
751
    }
752
  }
753

754
  return updated;
145✔
755
}
756

757
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
14,759✔
758
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,759✔
759
    return;
7,423✔
760
  }
761

762
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
7,336✔
763
  for (int32_t i = 0; i < size; ++i) {
28,265✔
764
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
20,929✔
765
    pInfo->stage = -1;
20,929✔
766
  }
767

768
  stDebug("s-task:%s reset all upstream tasks stage info", pTask->id.idStr);
7,336✔
769
}
770

771
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
24,539✔
772
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
24,539✔
773
  if (num == 0) {
24,547✔
774
    return;
12,287✔
775
  }
776

777
  for (int32_t i = 0; i < num; ++i) {
47,205✔
778
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
34,946✔
779
    pInfo->dataAllowed = true;
34,945✔
780
  }
781

782
  pTask->upstreamInfo.numOfClosed = 0;
12,259✔
783
  stDebug("s-task:%s opening up inputQ for %d upstream tasks", pTask->id.idStr, num);
12,259✔
784
}
785

786
void streamTaskCloseUpstreamInput(SStreamTask* pTask, int32_t taskId) {
8,150✔
787
  SStreamUpstreamEpInfo* pInfo = NULL;
8,150✔
788
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
8,150✔
789

790
  if ((pInfo != NULL) && pInfo->dataAllowed) {
8,150!
791
    pInfo->dataAllowed = false;
8,150✔
792
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
8,150!
793
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
8,151✔
794
    } else {
795
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
796
    }
797
  }
798
}
8,148✔
799

800
void streamTaskOpenUpstreamInput(SStreamTask* pTask, int32_t taskId) {
1✔
801
  SStreamUpstreamEpInfo* pInfo = NULL;
1✔
802
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
1✔
803

804
  if (pInfo != NULL && (!pInfo->dataAllowed)) {
1!
805
    int32_t t = atomic_sub_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
1✔
806
    stDebug("s-task:%s open inputQ for upstream:0x%x, remain closed:%d", pTask->id.idStr, taskId, t);
1!
807
    pInfo->dataAllowed = true;
1✔
808
  }
809
}
1✔
810

811
bool streamTaskIsAllUpstreamClosed(SStreamTask* pTask) {
×
812
  return pTask->upstreamInfo.numOfClosed == taosArrayGetSize(pTask->upstreamInfo.pList);
×
813
}
814

815
bool streamTaskSetSchedStatusWait(SStreamTask* pTask) {
108,291✔
816
  bool ret = false;
108,291✔
817

818
  streamMutexLock(&pTask->lock);
108,291✔
819
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
108,300✔
820
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
70,106✔
821
    ret = true;
70,106✔
822
  }
823

824
  streamMutexUnlock(&pTask->lock);
108,300✔
825
  return ret;
108,297✔
826
}
827

828
int8_t streamTaskSetSchedStatusActive(SStreamTask* pTask) {
68,987✔
829
  streamMutexLock(&pTask->lock);
68,987✔
830
  int8_t status = pTask->status.schedStatus;
69,021✔
831
  if (status == TASK_SCHED_STATUS__WAITING) {
69,021✔
832
    pTask->status.schedStatus = TASK_SCHED_STATUS__ACTIVE;
69,017✔
833
  }
834
  streamMutexUnlock(&pTask->lock);
69,021✔
835

836
  return status;
69,021✔
837
}
838

839
int8_t streamTaskSetSchedStatusInactive(SStreamTask* pTask) {
1,068✔
840
  streamMutexLock(&pTask->lock);
1,068✔
841
  int8_t status = pTask->status.schedStatus;
1,068✔
842
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
1,068✔
843
  streamMutexUnlock(&pTask->lock);
1,068✔
844

845
  return status;
1,068✔
846
}
847

848
int32_t streamTaskClearHTaskAttr(SStreamTask* pTask, int32_t resetRelHalt) {
7,016✔
849
  int32_t      code = 0;
7,016✔
850
  SStreamMeta* pMeta = pTask->pMeta;
7,016✔
851
  SStreamTask* pStreamTask = NULL;
7,016✔
852

853
  if (pTask->info.fillHistory == 0) {
7,016!
854
    return code;
7,017✔
855
  }
856

857
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
858
  if (code == 0) {
9!
859
    stDebug("s-task:%s clear the related stream task:0x%x attr to fill-history task", pTask->id.idStr,
×
860
            (int32_t)pTask->streamTaskId.taskId);
861

862
    streamMutexLock(&(pStreamTask->lock));
×
863
    CLEAR_RELATED_FILLHISTORY_TASK(pStreamTask);
×
864

865
    if (resetRelHalt) {
×
866
      stDebug("s-task:0x%" PRIx64 " set the persistent status attr to be ready, prev:%s, status in sm:%s",
×
867
              pTask->streamTaskId.taskId, streamTaskGetStatusStr(pStreamTask->status.taskStatus),
868
              streamTaskGetStatus(pStreamTask).name);
869
      pStreamTask->status.taskStatus = TASK_STATUS__READY;
×
870
    }
871

872
    code = streamMetaSaveTask(pMeta, pStreamTask);
×
873
    streamMutexUnlock(&(pStreamTask->lock));
×
874

875
    streamMetaReleaseTask(pMeta, pStreamTask);
×
876
  }
877

878
  return code;
9✔
879
}
880

881
int32_t streamBuildAndSendDropTaskMsg(SMsgCb* pMsgCb, int32_t vgId, SStreamTaskId* pTaskId, int64_t resetRelHalt) {
9✔
882
  SVDropStreamTaskReq* pReq = rpcMallocCont(sizeof(SVDropStreamTaskReq));
9✔
883
  if (pReq == NULL) {
9!
884
    return terrno;
×
885
  }
886

887
  pReq->head.vgId = vgId;
9✔
888
  pReq->taskId = pTaskId->taskId;
9✔
889
  pReq->streamId = pTaskId->streamId;
9✔
890
  pReq->resetRelHalt = resetRelHalt;
9✔
891

892
  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_DROP, .pCont = pReq, .contLen = sizeof(SVDropStreamTaskReq)};
9✔
893
  int32_t code = tmsgPutToQueue(pMsgCb, WRITE_QUEUE, &msg);
9✔
894
  if (code != TSDB_CODE_SUCCESS) {
9!
895
    stError("vgId:%d failed to send drop task:0x%x msg, code:%s", vgId, pTaskId->taskId, tstrerror(code));
×
896
  } else {
897
    stDebug("vgId:%d build and send drop task:0x%x msg", vgId, pTaskId->taskId);
9!
898
  }
899

900
  return code;
9✔
901
}
902

903
int32_t streamSendChkptReportMsg(SStreamTask* pTask, SCheckpointInfo* pCheckpointInfo, int8_t dropRelHTask) {
6,041✔
904
  int32_t                code = 0;
6,041✔
905
  int32_t                tlen = 0;
6,041✔
906
  int32_t                vgId = pTask->pMeta->vgId;
6,041✔
907
  const char*            id = pTask->id.idStr;
6,041✔
908
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
6,041✔
909

910
  SCheckpointReport req = {.streamId = pTask->id.streamId,
6,041✔
911
                           .taskId = pTask->id.taskId,
6,041✔
912
                           .nodeId = vgId,
913
                           .dropHTask = dropRelHTask,
914
                           .transId = pActive->transId,
6,041✔
915
                           .checkpointId = pActive->activeId,
6,041✔
916
                           .checkpointVer = pCheckpointInfo->processedVer,
6,041✔
917
                           .checkpointTs = pCheckpointInfo->startTs};
6,041✔
918

919
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
6,041!
920
  if (code < 0) {
6,041!
921
    stError("s-task:%s vgId:%d encode stream task checkpoint-report failed, code:%s", id, vgId, tstrerror(code));
×
922
    return -1;
×
923
  }
924

925
  void* buf = rpcMallocCont(tlen);
6,041✔
926
  if (buf == NULL) {
6,040!
927
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId,
×
928
            tstrerror(TSDB_CODE_OUT_OF_MEMORY));
929
    return -1;
×
930
  }
931

932
  SEncoder encoder;
933
  tEncoderInit(&encoder, buf, tlen);
6,040✔
934
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
6,040!
935
    rpcFreeCont(buf);
×
936
    tEncoderClear(&encoder);
×
937
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId, tstrerror(code));
×
938
    return -1;
×
939
  }
940
  tEncoderClear(&encoder);
6,039✔
941

942
  SRpcMsg msg = {0};
6,042✔
943
  initRpcMsg(&msg, TDMT_MND_STREAM_CHKPT_REPORT, buf, tlen);
6,042✔
944
  stDebug("s-task:%s vgId:%d build and send task checkpoint-report to mnode", id, vgId);
6,042✔
945

946
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
6,042✔
947
}
948

949
STaskId streamTaskGetTaskId(const SStreamTask* pTask) {
78,884✔
950
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
78,884✔
951
  return id;
78,884✔
952
}
953

954
void streamTaskInitForLaunchHTask(SHistoryTaskInfo* pInfo) {
2,024✔
955
  pInfo->waitInterval = LAUNCH_HTASK_INTERVAL;
2,024✔
956
  pInfo->tickCount = ceil(LAUNCH_HTASK_INTERVAL / WAIT_FOR_MINIMAL_INTERVAL);
2,024✔
957
  pInfo->retryTimes = 0;
2,024✔
958
}
2,024✔
959

960
void streamTaskSetRetryInfoForLaunch(SHistoryTaskInfo* pInfo) {
2,018✔
961
  pInfo->waitInterval *= RETRY_LAUNCH_INTERVAL_INC_RATE;
2,018✔
962
  pInfo->tickCount = ceil(pInfo->waitInterval / WAIT_FOR_MINIMAL_INTERVAL);
2,018✔
963
  pInfo->retryTimes += 1;
2,018✔
964
}
2,018✔
965

966
void streamTaskStatusInit(STaskStatusEntry* pEntry, const SStreamTask* pTask) {
9,309✔
967
  pEntry->id.streamId = pTask->id.streamId;
9,309✔
968
  pEntry->id.taskId = pTask->id.taskId;
9,309✔
969
  pEntry->stage = -1;
9,309✔
970
  pEntry->nodeId = pTask->info.nodeId;
9,309✔
971
  pEntry->status = TASK_STATUS__STOP;
9,309✔
972
}
9,309✔
973

974
void streamTaskStatusCopy(STaskStatusEntry* pDst, const STaskStatusEntry* pSrc) {
61,072✔
975
  pDst->stage = pSrc->stage;
61,072✔
976
  pDst->inputQUsed = pSrc->inputQUsed;
61,072✔
977
  pDst->inputRate = pSrc->inputRate;
61,072✔
978
  pDst->procsTotal = pSrc->procsTotal;
61,072✔
979
  pDst->procsThroughput = pSrc->procsThroughput;
61,072✔
980
  pDst->outputTotal = pSrc->outputTotal;
61,072✔
981
  pDst->outputThroughput = pSrc->outputThroughput;
61,072✔
982
  pDst->processedVer = pSrc->processedVer;
61,072✔
983
  pDst->verRange = pSrc->verRange;
61,072✔
984
  pDst->sinkQuota = pSrc->sinkQuota;
61,072✔
985
  pDst->sinkDataSize = pSrc->sinkDataSize;
61,072✔
986
  pDst->checkpointInfo = pSrc->checkpointInfo;
61,072✔
987
  pDst->startCheckpointId = pSrc->startCheckpointId;
61,072✔
988
  pDst->startCheckpointVer = pSrc->startCheckpointVer;
61,072✔
989
  pDst->status = pSrc->status;
61,072✔
990

991
  pDst->startTime = pSrc->startTime;
61,072✔
992
  pDst->hTaskId = pSrc->hTaskId;
61,072✔
993
  pDst->notifyEventStat = pSrc->notifyEventStat;
61,072✔
994
}
61,072✔
995

996
STaskStatusEntry streamTaskGetStatusEntry(SStreamTask* pTask) {
62,433✔
997
  SStreamMeta*         pMeta = pTask->pMeta;
62,433✔
998
  STaskExecStatisInfo* pExecInfo = &pTask->execInfo;
62,433✔
999

1000
  STaskStatusEntry entry = {
187,299✔
1001
      .id = streamTaskGetTaskId(pTask),
62,433✔
1002
      .status = streamTaskGetStatus(pTask).state,
62,433✔
1003
      .nodeId = pMeta->vgId,
62,433✔
1004
      .stage = pMeta->stage,
62,433✔
1005

1006
      .inputQUsed = SIZE_IN_MiB(streamQueueGetItemSize(pTask->inputq.queue)),
62,433✔
1007
      .startTime = pExecInfo->readyTs,
62,433✔
1008
      .checkpointInfo.latestId = pTask->chkInfo.checkpointId,
62,433✔
1009
      .checkpointInfo.latestVer = pTask->chkInfo.checkpointVer,
62,433✔
1010
      .checkpointInfo.latestTime = pTask->chkInfo.checkpointTime,
62,433✔
1011
      .checkpointInfo.latestSize = 0,
1012
      .checkpointInfo.remoteBackup = 0,
1013
      .checkpointInfo.consensusChkptId = 0,
1014
      .checkpointInfo.consensusTs = 0,
1015
      .hTaskId = pTask->hTaskInfo.id.taskId,
62,433✔
1016
      .procsTotal = SIZE_IN_MiB(pExecInfo->inputDataSize),
62,433✔
1017
      .outputTotal = SIZE_IN_MiB(pExecInfo->outputDataSize),
62,433✔
1018
      .procsThroughput = SIZE_IN_KiB(pExecInfo->procsThroughput),
62,433✔
1019
      .outputThroughput = SIZE_IN_KiB(pExecInfo->outputThroughput),
62,433✔
1020
      .startCheckpointId = pExecInfo->startCheckpointId,
62,433✔
1021
      .startCheckpointVer = pExecInfo->startCheckpointVer,
62,433✔
1022
      .notifyEventStat = pTask->notifyEventStat,
1023
  };
1024
  return entry;
62,433✔
1025
}
1026

1027
static int32_t taskPauseCallback(SStreamTask* pTask, void* param) {
1,402✔
1028
  SStreamMeta* pMeta = pTask->pMeta;
1,402✔
1029
  int32_t      code = 0;
1,402✔
1030

1031
  int32_t num = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
1,402✔
1032
  stInfo("vgId:%d s-task:%s pause stream task. paused task num:%d", pMeta->vgId, pTask->id.idStr, num);
1,412!
1033

1034
  // in case of fill-history task, stop the tsdb file scan operation.
1035
  if (pTask->info.fillHistory == 1) {
1,413✔
1036
    void* pExecutor = pTask->exec.pExecutor;
72✔
1037
    code = qKillTask(pExecutor, TSDB_CODE_SUCCESS);
72✔
1038
  }
1039

1040
  stDebug("vgId:%d s-task:%s set pause flag and pause task", pMeta->vgId, pTask->id.idStr);
1,413✔
1041
  return code;
1,414✔
1042
}
1043

1044
void streamTaskPause(SStreamTask* pTask) {
1,477✔
1045
  int32_t code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_PAUSE, taskPauseCallback, NULL);
1,477✔
1046
  if (code) {
1,486!
1047
    stError("s-task:%s failed handle pause event async, code:%s", pTask->id.idStr, tstrerror(code));
×
1048
  }
1049
}
1,486✔
1050

1051
void streamTaskResume(SStreamTask* pTask) {
2,671✔
1052
  SStreamTaskState prevState = streamTaskGetStatus(pTask);
2,671✔
1053

1054
  SStreamMeta* pMeta = pTask->pMeta;
2,668✔
1055
  int32_t      code = streamTaskRestoreStatus(pTask);
2,668✔
1056
  if (code == TSDB_CODE_SUCCESS) {
2,689✔
1057
    char*   pNew = streamTaskGetStatus(pTask).name;
1,403✔
1058
    int32_t num = atomic_sub_fetch_32(&pMeta->numOfPausedTasks, 1);
1,403✔
1059
    stInfo("s-task:%s status:%s resume from %s, paused task(s):%d", pTask->id.idStr, pNew, prevState.name, num);
1,403!
1060
  } else {
1061
    stInfo("s-task:%s status:%s no need to resume, paused task(s):%d", pTask->id.idStr, prevState.name,
1,286!
1062
           pMeta->numOfPausedTasks);
1063
  }
1064
}
2,691✔
1065

1066
bool streamTaskIsSinkTask(const SStreamTask* pTask) { return pTask->info.taskLevel == TASK_LEVEL__SINK; }
77,596✔
1067

1068
// this task must success
1069
int32_t streamTaskSendCheckpointReq(SStreamTask* pTask) {
4,387✔
1070
  int32_t     code;
1071
  int32_t     tlen = 0;
4,387✔
1072
  int32_t     vgId = pTask->pMeta->vgId;
4,387✔
1073
  const char* id = pTask->id.idStr;
4,387✔
1074

1075
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,387✔
1076
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,387!
1077
  if (code < 0) {
4,386!
1078
    stError("s-task:%s vgId:%d encode stream task req checkpoint failed, code:%s", id, vgId, tstrerror(code));
×
1079
    return TSDB_CODE_INVALID_MSG;
×
1080
  }
1081

1082
  void* buf = rpcMallocCont(tlen);
4,386✔
1083
  if (buf == NULL) {
4,390!
1084
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:Out of memory", id, vgId);
×
1085
    return terrno;
×
1086
  }
1087

1088
  SEncoder encoder;
1089
  tEncoderInit(&encoder, buf, tlen);
4,390✔
1090
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,390!
1091
    rpcFreeCont(buf);
×
1092
    tEncoderClear(&encoder);
×
1093
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:%s", id, vgId, tstrerror(code));
×
1094
    return code;
×
1095
  }
1096

1097
  tEncoderClear(&encoder);
4,387✔
1098

1099
  SRpcMsg msg = {0};
4,387✔
1100
  initRpcMsg(&msg, TDMT_MND_STREAM_REQ_CHKPT, buf, tlen);
4,387✔
1101
  stDebug("s-task:%s vgId:%d build and send task checkpoint req", id, vgId);
4,386✔
1102

1103
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
4,386✔
1104
}
1105

1106
void streamTaskGetUpstreamTaskEpInfo(SStreamTask* pTask, int32_t taskId, SStreamUpstreamEpInfo** pEpInfo) {
99,257✔
1107
  *pEpInfo = NULL;
99,257✔
1108

1109
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
99,257✔
1110
  for (int32_t i = 0; i < num; ++i) {
198,720✔
1111
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
198,717✔
1112
    if (pInfo == NULL) {
198,706!
1113
      return;
×
1114
    }
1115

1116
    if (pInfo->taskId == taskId) {
198,706✔
1117
      *pEpInfo = pInfo;
99,247✔
1118
      return;
99,247✔
1119
    }
1120
  }
1121

1122
  stError("s-task:%s failed to find upstream task:0x%x", pTask->id.idStr, taskId);
3!
1123
}
1124

1125
SEpSet* streamTaskGetDownstreamEpInfo(SStreamTask* pTask, int32_t taskId) {
×
1126
  if (pTask->info.taskLevel == TASK_OUTPUT__FIXED_DISPATCH) {
×
1127
    if (pTask->outputInfo.fixedDispatcher.taskId == taskId) {
×
1128
      return &pTask->outputInfo.fixedDispatcher.epSet;
×
1129
    }
1130
  } else if (pTask->info.taskLevel == TASK_OUTPUT__SHUFFLE_DISPATCH) {
×
1131
    SArray* pList = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
×
1132
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1133
      SVgroupInfo* pVgInfo = taosArrayGet(pList, i);
×
1134
      if (pVgInfo == NULL) {
×
1135
        continue;
×
1136
      }
1137

1138
      if (pVgInfo->taskId == taskId) {
×
1139
        return &pVgInfo->epSet;
×
1140
      }
1141
    }
1142
  }
1143

1144
  return NULL;
×
1145
}
1146

1147
int32_t createStreamTaskIdStr(int64_t streamId, int32_t taskId, const char** pId) {
14,750✔
1148
  char buf[128] = {0};
14,750✔
1149
  int32_t code = snprintf(buf, tListLen(buf),"0x%" PRIx64 "-0x%x", streamId, taskId);
14,750✔
1150
  if (code < 0 || code >= tListLen(buf)) {
14,750!
1151
    return TSDB_CODE_OUT_OF_BUFFER;
×
1152
  }
1153

1154
  *pId = taosStrdup(buf);
14,756!
1155

1156
  if (*pId == NULL) {
14,752!
1157
    return terrno;
×
1158
  } else {
1159
    return TSDB_CODE_SUCCESS;
14,752✔
1160
  }
1161
}
1162

1163
static int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
562✔
1164
  int32_t           code;
1165
  SStreamDataBlock* pData;
1166

1167
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
562✔
1168
  if (code) {
561!
1169
    stError("s-task:%s failed to allocated retrieve-block", pTask->id.idStr);
×
1170
    return terrno = code;
×
1171
  }
1172

1173
  pData->type = STREAM_INPUT__DATA_RETRIEVE;
561✔
1174
  pData->srcVgId = 0;
561✔
1175

1176
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
561✔
1177
  if (code != TSDB_CODE_SUCCESS) {
562!
1178
    stError("s-task:%s failed to convert retrieve-data to block, code:%s", pTask->id.idStr, tstrerror(code));
×
1179
    taosFreeQitem(pData);
×
1180
    return code;
×
1181
  }
1182

1183
  code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pData);
562✔
1184
  if (code != TSDB_CODE_SUCCESS) {
562!
1185
    stError("s-task:%s failed to put retrieve-block into inputQ, inputQ is full, discard the retrieve msg",
×
1186
            pTask->id.idStr);
1187
  }
1188

1189
  return code;
562✔
1190
}
1191

1192
int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
562✔
1193
  int32_t code = streamTaskEnqueueRetrieve(pTask, pReq);
562✔
1194
  if (code != 0) {
562!
1195
    return code;
×
1196
  }
1197
  return streamTrySchedExec(pTask);
562✔
1198
}
1199

1200
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
7,021✔
1201

1202
void streamTaskGetActiveCheckpointInfo(const SStreamTask* pTask, int32_t* pTransId, int64_t* pCheckpointId) {
×
1203
  if (pTransId != NULL) {
×
1204
    *pTransId = pTask->chkInfo.pActiveInfo->transId;
×
1205
  }
1206

1207
  if (pCheckpointId != NULL) {
×
1208
    *pCheckpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1209
  }
1210
}
×
1211

1212
int32_t streamTaskSetActiveCheckpointInfo(SStreamTask* pTask, int64_t activeCheckpointId) {
28✔
1213
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
28✔
1214
  return TSDB_CODE_SUCCESS;
28✔
1215
}
1216

1217
void streamTaskSetFailedChkptInfo(SStreamTask* pTask, int32_t transId, int64_t checkpointId) {
×
1218
  pTask->chkInfo.pActiveInfo->transId = transId;
×
1219
  pTask->chkInfo.pActiveInfo->activeId = checkpointId;
×
1220
  pTask->chkInfo.pActiveInfo->failedId = checkpointId;
×
1221
  stDebug("s-task:%s set failed checkpointId:%"PRId64, pTask->id.idStr, checkpointId);
×
1222
}
×
1223

1224
int32_t streamTaskCreateActiveChkptInfo(SActiveCheckpointInfo** pRes) {
14,796✔
1225
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
14,796!
1226
  if (pInfo == NULL) {
14,804!
1227
    return terrno;
×
1228
  }
1229

1230
  int32_t code = taosThreadMutexInit(&pInfo->lock, NULL);
14,804✔
1231
  if (code != TSDB_CODE_SUCCESS) {
14,805!
1232
    return code;
×
1233
  }
1234

1235
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
14,805✔
1236
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
14,804✔
1237
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
14,801✔
1238

1239
  *pRes = pInfo;
14,803✔
1240
  return code;
14,803✔
1241
}
1242

1243
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
62,956✔
1244
  if (pInfo == NULL) {
62,956✔
1245
    return;
48,157✔
1246
  }
1247

1248
  streamMutexDestroy(&pInfo->lock);
14,799✔
1249
  taosArrayDestroy(pInfo->pDispatchTriggerList);
14,803✔
1250
  pInfo->pDispatchTriggerList = NULL;
14,805✔
1251
  taosArrayDestroy(pInfo->pReadyMsgList);
14,805✔
1252
  pInfo->pReadyMsgList = NULL;
14,805✔
1253
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
14,805✔
1254
  pInfo->pCheckpointReadyRecvList = NULL;
14,804✔
1255

1256
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
14,804✔
1257
  if (pTriggerTmr->tmrHandle != NULL) {
14,804✔
1258
    streamTmrStop(pTriggerTmr->tmrHandle);
2,184✔
1259
    pTriggerTmr->tmrHandle = NULL;
2,184✔
1260
  }
1261

1262
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
14,804✔
1263
  if (pReadyTmr->tmrHandle != NULL) {
14,804✔
1264
    streamTmrStop(pReadyTmr->tmrHandle);
2,154✔
1265
    pReadyTmr->tmrHandle = NULL;
2,154✔
1266
  }
1267

1268
  taosMemoryFree(pInfo);
14,804!
1269
}
1270

1271
// NOTE: clear the checkpoint id, and keep the failed id
1272
// failedId for a task will increase as the checkpoint I.D. increases.
1273
void streamTaskClearActiveInfo(SActiveCheckpointInfo* pInfo) {
5,328✔
1274
  pInfo->activeId = 0;
5,328✔
1275
  pInfo->transId = 0;
5,328✔
1276
  pInfo->allUpstreamTriggerRecv = 0;
5,328✔
1277
  pInfo->dispatchTrigger = false;
5,328✔
1278

1279
  taosArrayClear(pInfo->pDispatchTriggerList);
5,328✔
1280
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,315✔
1281
}
5,319✔
1282

1283
const char* streamTaskGetExecType(int32_t type) {
97,672✔
1284
  switch (type) {
97,672!
1285
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
34,059✔
1286
      return "scan-wal-file";
34,059✔
1287
    case STREAM_EXEC_T_START_ALL_TASKS:
7,755✔
1288
      return "start-all-tasks";
7,755✔
1289
    case STREAM_EXEC_T_START_ONE_TASK:
5,875✔
1290
      return "start-one-task";
5,875✔
1291
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
29✔
1292
      return "restart-all-tasks";
29✔
1293
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,745✔
1294
      return "stop-all-tasks";
4,745✔
1295
    case STREAM_EXEC_T_RESUME_TASK:
6,875✔
1296
      return "resume-task-from-idle";
6,875✔
1297
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
1298
      return "record-start-failed-task";
×
1299
    case 0:
38,409✔
1300
      return "exec-all-tasks";
38,409✔
1301
    default:
×
1302
      return "invalid-exec-type";
×
1303
  }
1304
}
1305

1306
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
39,579✔
1307
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
39,579!
1308
  if (*pRefId != NULL) {
39,586!
1309
    **pRefId = pTask->id.refId;
39,586✔
1310
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
39,586✔
1311
    if (code != 0) {
39,592!
1312
      stError("s-task:%s failed to add refId:%" PRId64 " into refId-mgmt, code:%s", pTask->id.idStr, pTask->id.refId,
×
1313
              tstrerror(code));
1314
    }
1315
    return code;
39,592✔
1316
  } else {
1317
    stError("s-task:%s failed to alloc new ref id, code:%s", pTask->id.idStr, tstrerror(terrno));
×
1318
    return terrno;
×
1319
  }
1320
}
1321

1322
void streamTaskFreeRefId(int64_t* pRefId) {
36,855✔
1323
  if (pRefId == NULL) {
36,855✔
1324
    return;
2,370✔
1325
  }
1326

1327
  metaRefMgtRemove(pRefId);
34,485✔
1328
}
1329

1330
static int32_t tEncodeStreamNotifyInfo(SEncoder* pEncoder, const SNotifyInfo* info) {
140,651✔
1331
  int32_t code = TSDB_CODE_SUCCESS;
140,651✔
1332
  int32_t lino = 0;
140,651✔
1333

1334
  QUERY_CHECK_NULL(pEncoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
140,651!
1335
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
140,651!
1336

1337
  int32_t addrSize = taosArrayGetSize(info->pNotifyAddrUrls);
140,651✔
1338
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, addrSize));
140,653✔
1339
  for (int32_t i = 0; i < addrSize; ++i) {
140,631!
1340
    const char* url = taosArrayGetP(info->pNotifyAddrUrls, i);
×
1341
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, url));
×
1342
  }
1343
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyEventTypes));
281,262!
1344
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyErrorHandle));
281,262!
1345
  if (addrSize > 0) {
140,631!
1346
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, info->streamName));
×
1347
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, info->stbFullName));
×
1348
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, info->pSchemaWrapper));
×
1349
  }
1350

1351
_exit:
140,631✔
1352
  if (code != TSDB_CODE_SUCCESS) {
140,653✔
1353
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
22!
1354
  }
1355
  return code;
140,657✔
1356
}
1357

1358
static int32_t tDecodeStreamNotifyInfo(SDecoder* pDecoder, SNotifyInfo* info) {
48,863✔
1359
  int32_t code = TSDB_CODE_SUCCESS;
48,863✔
1360
  int32_t lino = 0;
48,863✔
1361

1362
  QUERY_CHECK_NULL(pDecoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
48,863!
1363
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
48,863!
1364

1365
  int32_t addrSize = 0;
48,863✔
1366
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addrSize));
48,873!
1367
  info->pNotifyAddrUrls = taosArrayInit(addrSize, POINTER_BYTES);
48,873✔
1368
  QUERY_CHECK_NULL(info->pNotifyAddrUrls, code, lino, _exit, terrno);
48,879✔
1369
  for (int32_t i = 0; i < addrSize; ++i) {
48,878!
1370
    char *url = NULL;
×
1371
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &url));
×
1372
    url = taosStrndup(url, TSDB_STREAM_NOTIFY_URL_LEN);
×
1373
    QUERY_CHECK_NULL(url, code, lino, _exit, terrno);
×
1374
    if (taosArrayPush(info->pNotifyAddrUrls, &url) == NULL) {
×
1375
      taosMemoryFree(url);
×
1376
      TAOS_CHECK_EXIT(terrno);
×
1377
    }
1378
  }
1379
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyEventTypes));
97,745!
1380
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyErrorHandle));
97,739!
1381
  if (addrSize > 0) {
48,872!
1382
    char* name = NULL;
×
1383
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &name));
×
1384
    info->streamName = taosStrndup(name, TSDB_STREAM_FNAME_LEN + 1);
×
1385
    QUERY_CHECK_NULL(info->streamName, code, lino, _exit, terrno);
×
1386
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &name));
×
1387
    info->stbFullName = taosStrndup(name, TSDB_STREAM_FNAME_LEN + 1);
×
1388
    QUERY_CHECK_NULL(info->stbFullName, code, lino, _exit, terrno);
×
1389
    info->pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
×
1390
    if (info->pSchemaWrapper == NULL) {
×
1391
      TAOS_CHECK_EXIT(terrno);
×
1392
    }
1393
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, info->pSchemaWrapper));
×
1394
  }
1395

1396
_exit:
48,872✔
1397
  if (code != TSDB_CODE_SUCCESS) {
48,872!
1398
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1399
  }
1400
  return code;
48,867✔
1401
}
1402

1403
int32_t tEncodeStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
140,668✔
1404
  int32_t code = 0;
140,668✔
1405
  int32_t lino;
1406

1407
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
140,668!
1408
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->ver));
281,334!
1409
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->id.streamId));
281,334!
1410
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->id.taskId));
281,334!
1411
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.trigger));
281,334!
1412
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.taskLevel));
281,334!
1413
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.type));
281,334!
1414
  TAOS_CHECK_EXIT(tEncodeI16(pEncoder, pTask->msgInfo.msgType));
281,334!
1415

1416
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.taskStatus));
281,334!
1417
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.schedStatus));
281,334!
1418

1419
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
281,334!
1420
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
281,334!
1421
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
140,667!
1422
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
140,679!
1423

1424
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointId));
281,360!
1425
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointVer));
281,360!
1426
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.fillHistory));
281,360!
1427

1428
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
281,360!
1429
  int32_t taskId = pTask->hTaskInfo.id.taskId;
140,680✔
1430
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
140,680!
1431

1432
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
281,360!
1433
  taskId = pTask->streamTaskId.taskId;
140,680✔
1434
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
140,680!
1435

1436
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.minVer));
281,360!
1437
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.maxVer));
281,360!
1438
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.skey));
281,360!
1439
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.ekey));
281,360!
1440

1441
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
140,680✔
1442
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
140,675!
1443
  for (int32_t i = 0; i < epSz; i++) {
334,696✔
1444
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
194,017✔
1445
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
194,010!
1446
  }
1447

1448
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
140,679✔
1449
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
149,088!
1450
  }
1451

1452
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
140,679✔
1453
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
138,472!
1454
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
138,472!
1455
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
138,472!
1456
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
71,443✔
1457
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
692!
1458
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
71,097!
1459
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1460
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
71,097✔
1461
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
21,788!
1462
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
21,788!
1463
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
10,894!
1464
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
60,203✔
1465
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
60,154!
1466
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
120,302!
1467
  }
1468
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
281,352!
1469
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
281,352!
1470
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
281,352!
1471

1472
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
140,676✔
1473
    TAOS_CHECK_EXIT(tEncodeStreamNotifyInfo(pEncoder, &pTask->notifyInfo));
140,656✔
1474
  }
1475

1476
  tEndEncode(pEncoder);
140,657✔
1477
_exit:
140,673✔
1478
  return code;
140,673✔
1479
}
1480

1481
int32_t tDecodeStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
48,875✔
1482
  int32_t taskId = 0;
48,875✔
1483
  int32_t code = 0;
48,875✔
1484
  int32_t lino;
1485

1486
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
48,875!
1487
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
97,775✔
1488
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
48,879!
1489
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
1490
  }
1491

1492
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
97,749!
1493
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
97,746!
1494
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
97,742!
1495
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
97,741!
1496
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
97,750!
1497
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
97,748!
1498

1499
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
97,746!
1500
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
97,747!
1501

1502
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
97,740!
1503
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
97,739!
1504
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
48,873!
1505
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
48,880!
1506

1507
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
97,762!
1508
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
97,759!
1509
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
97,756!
1510

1511
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
97,755!
1512
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
48,869!
1513
  pTask->hTaskInfo.id.taskId = taskId;
48,869✔
1514

1515
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
97,740!
1516
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
48,875!
1517
  pTask->streamTaskId.taskId = taskId;
48,875✔
1518

1519
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
97,751!
1520
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
97,745!
1521
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
97,742!
1522
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
97,744!
1523

1524
  int32_t epSz = -1;
48,871✔
1525
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &epSz) < 0);
48,871!
1526

1527
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
48,871!
1528
    TAOS_CHECK_EXIT(terrno);
×
1529
  }
1530
  for (int32_t i = 0; i < epSz; i++) {
115,878✔
1531
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
67,001!
1532
    if (pInfo == NULL) {
66,997!
1533
      TAOS_CHECK_EXIT(terrno);
×
1534
    }
1535
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
66,997!
1536
      taosMemoryFreeClear(pInfo);
×
1537
      goto _exit;
×
1538
    }
1539
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
134,010!
1540
      TAOS_CHECK_EXIT(terrno);
×
1541
    }
1542
  }
1543

1544
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
48,877✔
1545
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
51,480!
1546
  }
1547

1548
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
48,879✔
1549
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
48,083!
1550
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
24,043!
1551
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
24,043!
1552
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
24,042!
1553
      TAOS_CHECK_EXIT(terrno);
×
1554
    }
1555
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
48,064!
1556
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
24,839✔
1557
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
248!
1558
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
24,715!
1559
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1560
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
24,715✔
1561
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
7,306!
1562
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
7,306!
1563
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,653!
1564
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
21,062✔
1565
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
21,051!
1566
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
21,051!
1567
  }
1568
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
97,735!
1569
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
48,872!
1570
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
97,742!
1571
  }
1572
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
48,870!
1573

1574
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
48,873!
1575
    TAOS_CHECK_EXIT(tDecodeStreamNotifyInfo(pDecoder, &pTask->notifyInfo));
48,875!
1576
  }
1577

1578
  tEndDecode(pDecoder);
48,867✔
1579

1580
_exit:
48,881✔
1581
  return code;
48,881✔
1582
}
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