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

taosdata / TDengine / #3616

19 Feb 2025 11:22AM UTC coverage: 63.315% (+0.4%) from 62.953%
#3616

push

travis-ci

web-flow
Merge pull request #29823 from taosdata/feat/TS-5928

fix:[TS-5928]add consumer parameters

148228 of 300186 branches covered (49.38%)

Branch coverage included in aggregate %.

232358 of 300909 relevant lines covered (77.22%)

17990742.15 hits per line

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

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

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

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

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

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

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

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

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

104
  return pEpInfo;
20,050✔
105
}
106

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

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

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

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

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

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

159
  if (fillHistory && !hasFillhistory) {
13,941!
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,941✔
165

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

169
  return code;
13,941✔
170
}
171

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

251
  if (pTask->hTaskInfo.pTimer != NULL) {
62,722✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
1,892✔
253
    pTask->hTaskInfo.pTimer = NULL;
1,892✔
254
  }
255

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

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

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

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

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

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

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

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

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
62,724✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
30,794✔
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
30,793!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
30,793✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
30,793✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
31,930✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
27,145✔
298
  }
299

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

367
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
14,633✔
368
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,633✔
369
  SDataRange*      pRange = &pTask->dataRange;
14,633✔
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,633✔
373
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
4,686✔
374
    pChkInfo->processedVer = ver - 1;   // already processed version
4,686✔
375
    pChkInfo->nextProcessVer = ver;     // next processed version
4,686✔
376

377
    pRange->range.maxVer = ver;
4,686✔
378
    pRange->range.minVer = ver;
4,686✔
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,947✔
383
      pChkInfo->checkpointVer = pRange->range.maxVer;
5,032✔
384
      pChkInfo->processedVer = pRange->range.maxVer;
5,032✔
385
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
5,032✔
386
    } else {
387
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
4,915✔
388
      pChkInfo->processedVer = pRange->range.minVer - 1;
4,915✔
389
      pChkInfo->nextProcessVer = pRange->range.minVer;
4,915✔
390

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

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

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

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

421
  int32_t len = strlen(pTask->pMeta->path);
14,642✔
422
  pTask->backendPath = (char*)taosMemoryMalloc(len + nBytes + 2);
14,642!
423
  if (pTask->backendPath == NULL) {
14,636!
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,636✔
428
  if (code < 0 || code >= len + nBytes + 2) {
14,636!
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,639✔
433
    return 0;
14,639✔
434
  }
435
}
436

437
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
14,632✔
438
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
14,632✔
439
  if (code) {
14,630!
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,630✔
445
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,630✔
446
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,630✔
447

448
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
14,630✔
449
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
14,635✔
450
  if (code1 || code2) {
14,640!
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,640✔
456

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

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

467
  pTask->pMeta = pMeta;
14,638✔
468
  pTask->pMsgCb = pMsgCb;
14,638✔
469
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
14,638✔
470
  if (pTask->msgInfo.pSendInfo == NULL) {
14,639!
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,639✔
476
  if (code) {
14,635!
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,635✔
482
  code = taosThreadMutexAttrInit(&attr);
14,635✔
483
  if (code != 0) {
14,631!
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,631✔
489
  if (code != 0) {
14,629!
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,629✔
495
  if (code) {
14,634!
496
    return code;
×
497
  }
498

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

504
  streamTaskOpenAllUpstreamInput(pTask);
14,628✔
505

506
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,631✔
507
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
14,631!
508
  if (pOutputInfo->pTokenBucket == NULL) {
14,638!
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,638✔
516
  if (code) {
14,637!
517
    return code;
×
518
  }
519

520
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
14,637✔
521
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
14,634!
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,634✔
527
  if (pTask->taskCheckInfo.pList == NULL) {
14,640!
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,640!
533
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
14,640✔
534
    if (code) {
14,641!
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,641✔
541
}
542

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

548
  int32_t type = pTask->outputInfo.type;
112,828✔
549
  if (type == TASK_OUTPUT__TABLE) {
112,828✔
550
    return 0;
276✔
551
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
112,552✔
552
    return 1;
9,974✔
553
  } else {
554
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
102,578✔
555
    return taosArrayGetSize(vgInfo);
102,578✔
556
  }
557
}
558

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

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

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

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

575
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
183✔
576
  int32_t code = 0;
183✔
577
  char    buf[512] = {0};
183✔
578
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
183✔
579
  if (code != 0) {  // print error and continue
183!
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);
183✔
585
  for (int32_t i = 0; i < numOfUpstream; ++i) {
319✔
586
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
236✔
587
    if (pInfo->nodeId == nodeId) {
236✔
588
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
100✔
589
      if (!equal) {
100✔
590
        *pUpdated = true;
24✔
591

592
        char tmp[512] = {0};
24✔
593
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
24✔
594
        if (code != 0) {  // print error and continue
24!
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);
24✔
600
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
24!
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,
76!
604
                pInfo->taskId, nodeId, buf);
605
      }
606

607
      break;
100✔
608
    }
609
  }
610

611
  return code;
183✔
612
}
613

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

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

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

632
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
183✔
633
  char    buf[512] = {0};
183✔
634
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
183✔
635
  if (code != 0) {                                        // print error and continue
183!
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;
183✔
641
  int8_t  type = pTask->outputInfo.type;
183✔
642

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

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

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

657
          char tmp[512] = {0};
24✔
658
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
24✔
659
          if (code != 0) {  // print error and continue
24!
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);
24✔
665
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
24!
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,
72!
669
                  pVgInfo->taskId, nodeId, buf);
670
        }
671
        break;
96✔
672
      }
673
    }
674
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
85!
675
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
85✔
676
    if (pDispatcher->nodeId == nodeId) {
85✔
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;
183✔
699
}
700

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

706
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,836✔
707
  if (code) {
2,836!
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,836✔
713
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
1,494✔
714
    if (code != TSDB_CODE_SUCCESS) {
1,494!
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,836!
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,836✔
726
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,836✔
727
  return code;
2,836✔
728
}
729

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

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

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

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

748
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
349✔
749
    if (code) {
349!
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;
159✔
755
}
756

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

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

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

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

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

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

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

790
  if ((pInfo != NULL) && pInfo->dataAllowed) {
8,183!
791
    pInfo->dataAllowed = false;
8,183✔
792
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
8,183!
793
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
8,183✔
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,183✔
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) {
107,589✔
816
  bool ret = false;
107,589✔
817

818
  streamMutexLock(&pTask->lock);
107,589✔
819
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
107,596✔
820
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
69,142✔
821
    ret = true;
69,142✔
822
  }
823

824
  streamMutexUnlock(&pTask->lock);
107,596✔
825
  return ret;
107,596✔
826
}
827

828
int8_t streamTaskSetSchedStatusActive(SStreamTask* pTask) {
67,926✔
829
  streamMutexLock(&pTask->lock);
67,926✔
830
  int8_t status = pTask->status.schedStatus;
67,999✔
831
  if (status == TASK_SCHED_STATUS__WAITING) {
67,999✔
832
    pTask->status.schedStatus = TASK_SCHED_STATUS__ACTIVE;
67,964✔
833
  }
834
  streamMutexUnlock(&pTask->lock);
67,999✔
835

836
  return status;
68,015✔
837
}
838

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

845
  return status;
1,094✔
846
}
847

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

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

857
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
×
858
  if (code == 0) {
6!
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;
6✔
879
}
880

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

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

892
  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_DROP, .pCont = pReq, .contLen = sizeof(SVDropStreamTaskReq)};
6✔
893
  int32_t code = tmsgPutToQueue(pMsgCb, WRITE_QUEUE, &msg);
6✔
894
  if (code != TSDB_CODE_SUCCESS) {
6!
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);
6!
898
  }
899

900
  return code;
6✔
901
}
902

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

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

919
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
6,140!
920
  if (code < 0) {
6,137!
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,137✔
926
  if (buf == NULL) {
6,139!
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,139✔
934
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
6,139!
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,140✔
941

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

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

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

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

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

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

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

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

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

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

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

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

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

1034
  // in case of fill-history task, stop the tsdb file scan operation.
1035
  if (pTask->info.fillHistory == 1) {
1,414✔
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,414✔
1041
  return code;
1,414✔
1042
}
1043

1044
void streamTaskPause(SStreamTask* pTask) {
1,482✔
1045
  int32_t code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_PAUSE, taskPauseCallback, NULL);
1,482✔
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,689✔
1052
  SStreamTaskState prevState = streamTaskGetStatus(pTask);
2,689✔
1053

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

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

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

1075
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,395✔
1076
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,395!
1077
  if (code < 0) {
4,396!
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,396✔
1083
  if (buf == NULL) {
4,397!
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,397✔
1090
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,397!
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,396✔
1098

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

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

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

1109
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
98,135✔
1110
  for (int32_t i = 0; i < num; ++i) {
197,008!
1111
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
197,015✔
1112
    if (pInfo == NULL) {
197,005!
1113
      return;
×
1114
    }
1115

1116
    if (pInfo->taskId == taskId) {
197,005✔
1117
      *pEpInfo = pInfo;
98,141✔
1118
      return;
98,141✔
1119
    }
1120
  }
1121

1122
  stError("s-task:%s failed to find upstream task:0x%x", pTask->id.idStr, taskId);
×
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,631✔
1148
  char buf[128] = {0};
14,631✔
1149
  int32_t code = snprintf(buf, tListLen(buf),"0x%" PRIx64 "-0x%x", streamId, taskId);
14,631✔
1150
  if (code < 0 || code >= tListLen(buf)) {
14,631!
1151
    return TSDB_CODE_OUT_OF_BUFFER;
×
1152
  }
1153

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

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

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

1167
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
550✔
1168
  if (code) {
550!
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;
550✔
1174
  pData->srcVgId = 0;
550✔
1175

1176
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
550✔
1177
  if (code != TSDB_CODE_SUCCESS) {
550!
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);
550✔
1184
  if (code != TSDB_CODE_SUCCESS) {
550!
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;
550✔
1190
}
1191

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

1200
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
7,018✔
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,681✔
1225
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
14,681!
1226
  if (pInfo == NULL) {
14,684!
1227
    return terrno;
×
1228
  }
1229

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

1235
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
14,679✔
1236
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
14,682✔
1237
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
14,683✔
1238

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

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

1248
  streamMutexDestroy(&pInfo->lock);
14,679✔
1249
  taosArrayDestroy(pInfo->pDispatchTriggerList);
14,681✔
1250
  pInfo->pDispatchTriggerList = NULL;
14,682✔
1251
  taosArrayDestroy(pInfo->pReadyMsgList);
14,682✔
1252
  pInfo->pReadyMsgList = NULL;
14,679✔
1253
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
14,679✔
1254
  pInfo->pCheckpointReadyRecvList = NULL;
14,681✔
1255

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

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

1268
  taosMemoryFree(pInfo);
14,681!
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,384✔
1274
  pInfo->activeId = 0;
5,384✔
1275
  pInfo->transId = 0;
5,384✔
1276
  pInfo->allUpstreamTriggerRecv = 0;
5,384✔
1277
  pInfo->dispatchTrigger = false;
5,384✔
1278

1279
  taosArrayClear(pInfo->pDispatchTriggerList);
5,384✔
1280
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,374✔
1281
}
5,376✔
1282

1283
const char* streamTaskGetExecType(int32_t type) {
96,393✔
1284
  switch (type) {
96,393!
1285
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
32,752✔
1286
      return "scan-wal-file";
32,752✔
1287
    case STREAM_EXEC_T_START_ALL_TASKS:
8,127✔
1288
      return "start-all-tasks";
8,127✔
1289
    case STREAM_EXEC_T_START_ONE_TASK:
5,781✔
1290
      return "start-one-task";
5,781✔
1291
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
32✔
1292
      return "restart-all-tasks";
32✔
1293
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,848✔
1294
      return "stop-all-tasks";
4,848✔
1295
    case STREAM_EXEC_T_RESUME_TASK:
6,471✔
1296
      return "resume-task-from-idle";
6,471✔
1297
    case STREAM_EXEC_T_ADD_FAILED_TASK:
×
1298
      return "record-start-failed-task";
×
1299
    case 0:
38,480✔
1300
      return "exec-all-tasks";
38,480✔
1301
    default:
×
1302
      return "invalid-exec-type";
×
1303
  }
1304
}
1305

1306
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
38,328✔
1307
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
38,328!
1308
  if (*pRefId != NULL) {
38,335!
1309
    **pRefId = pTask->id.refId;
38,335✔
1310
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
38,335✔
1311
    if (code != 0) {
38,343!
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;
38,343✔
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) {
35,531✔
1323
  if (pRefId == NULL) {
35,531✔
1324
    return;
2,222✔
1325
  }
1326

1327
  metaRefMgtRemove(pRefId);
33,309✔
1328
}
1329

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

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

1337
  int32_t addrSize = taosArrayGetSize(info->pNotifyAddrUrls);
140,172✔
1338
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, addrSize));
140,181✔
1339
  for (int32_t i = 0; i < addrSize; ++i) {
140,159!
1340
    const char* url = taosArrayGetP(info->pNotifyAddrUrls, i);
×
1341
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, url));
×
1342
  }
1343
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyEventTypes));
280,318!
1344
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyErrorHandle));
280,318!
1345
  if (addrSize > 0) {
140,159!
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,159✔
1352
  if (code != TSDB_CODE_SUCCESS) {
140,181✔
1353
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
22!
1354
  }
1355
  return code;
140,189✔
1356
}
1357

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

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

1365
  int32_t addrSize = 0;
48,738✔
1366
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addrSize));
48,744!
1367
  info->pNotifyAddrUrls = taosArrayInit(addrSize, POINTER_BYTES);
48,744✔
1368
  QUERY_CHECK_NULL(info->pNotifyAddrUrls, code, lino, _exit, terrno);
48,744!
1369
  for (int32_t i = 0; i < addrSize; ++i) {
48,744!
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,485!
1380
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyErrorHandle));
97,478!
1381
  if (addrSize > 0) {
48,737!
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,737✔
1397
  if (code != TSDB_CODE_SUCCESS) {
48,737!
1398
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1399
  }
1400
  return code;
48,735✔
1401
}
1402

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

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

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

1419
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
280,404!
1420
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
280,404!
1421
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
140,202!
1422
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
140,212!
1423

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

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

1432
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
280,430!
1433
  taskId = pTask->streamTaskId.taskId;
140,215✔
1434
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
140,215!
1435

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

1441
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
140,215✔
1442
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
140,210!
1443
  for (int32_t i = 0; i < epSz; i++) {
333,579✔
1444
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
193,375✔
1445
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
193,359!
1446
  }
1447

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

1452
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
140,204✔
1453
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
137,956!
1454
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
137,956!
1455
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
137,956!
1456
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
71,226✔
1457
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
776!
1458
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
70,838!
1459
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1460
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
70,838✔
1461
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
21,696!
1462
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
21,696!
1463
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
10,848!
1464
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
59,990✔
1465
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
59,953!
1466
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
119,904!
1467
  }
1468
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
280,402!
1469
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
280,402!
1470
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
280,402!
1471

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

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

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

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

1492
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
97,480!
1493
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
97,476!
1494
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
97,475!
1495
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
97,476!
1496
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
97,474!
1497
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
97,473!
1498

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

1502
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
97,471!
1503
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
97,469!
1504
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
48,735!
1505
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
48,742!
1506

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

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

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

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

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

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

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

1548
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
48,736✔
1549
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
47,937!
1550
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
23,968!
1551
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
23,971!
1552
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
23,975!
1553
      TAOS_CHECK_EXIT(terrno);
×
1554
    }
1555
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
47,931!
1556
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
24,767✔
1557
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
272!
1558
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
24,631!
1559
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1560
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
24,631✔
1561
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
7,204!
1562
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
7,204!
1563
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
3,602!
1564
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
21,029✔
1565
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
21,026!
1566
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
21,027!
1567
  }
1568
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
97,460!
1569
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
48,739!
1570
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
97,478!
1571
  }
1572
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
48,739!
1573

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

1578
  tEndDecode(pDecoder);
48,736✔
1579

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