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

taosdata / TDengine / #3631

07 Mar 2025 03:18PM UTC coverage: 60.671% (-3.0%) from 63.629%
#3631

push

travis-ci

web-flow
Merge pull request #30074 from taosdata/ciup30

ci: update ci workflow to fix path issue

141481 of 300084 branches covered (47.15%)

Branch coverage included in aggregate %.

223132 of 300884 relevant lines covered (74.16%)

7878557.0 hits per line

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

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

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

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

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

51
    if (!isEqual) {
82✔
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);
22!
64
    }
65
  }
66

67
  // check for the dispatch info and the upstream task info
68
  int32_t level = pTask->info.taskLevel;
261✔
69
  if (level == TASK_LEVEL__SOURCE) {
261✔
70
    code = streamTaskUpdateDownstreamInfo(pTask, nodeId, pEpSet, pUpdated);
122✔
71
  } else if (level == TASK_LEVEL__AGG) {
139✔
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);
122✔
76
  }
77

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

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

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

104
  return pEpInfo;
15,868✔
105
}
106

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

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

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

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

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

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

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

166
  code = addToTaskset(pTaskList, pTask);
11,058✔
167
  *p = pTask;
11,058✔
168

169
  return code;
11,058✔
170
}
171

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

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

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

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

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

201
  tEndDecode(pDecoder);
373✔
202
  return 0;
373✔
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) {
45,192✔
222
  char*        p = NULL;
45,192✔
223
  SStreamTask* pTask = pParam;
45,192✔
224
  int32_t      taskId = pTask->id.taskId;
45,192✔
225

226
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
45,192✔
227

228
  ETaskStatus status1 = TASK_STATUS__UNINIT;
45,192✔
229
  streamMutexLock(&pTask->lock);
45,192✔
230
  if (pTask->status.pSM != NULL) {
45,205✔
231
    SStreamTaskState status = streamTaskGetStatus(pTask);
22,540✔
232
    p = status.name;
22,538✔
233
    status1 = status.state;
22,538✔
234
  }
235
  streamMutexUnlock(&pTask->lock);
45,203✔
236

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

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

251
  if (pTask->hTaskInfo.pTimer != NULL) {
45,207✔
252
    streamTmrStop(pTask->hTaskInfo.pTimer);
1,574✔
253
    pTask->hTaskInfo.pTimer = NULL;
1,573✔
254
  }
255

256
  if (pTask->msgInfo.pRetryTmr != NULL) {
45,206✔
257
    streamTmrStop(pTask->msgInfo.pRetryTmr);
4,370✔
258
    pTask->msgInfo.pRetryTmr = NULL;
4,368✔
259
  }
260

261
  if (pTask->inputq.queue) {
45,204✔
262
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
11,448✔
263
    pTask->inputq.queue = NULL;
11,449✔
264
  }
265

266
  if (pTask->outputq.queue) {
45,205✔
267
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
11,449✔
268
    pTask->outputq.queue = NULL;
11,450✔
269
  }
270

271
  if (pTask->exec.qmsg) {
45,206✔
272
    taosMemoryFree(pTask->exec.qmsg);
23,790!
273
  }
274

275
  if (pTask->exec.pExecutor) {
45,207✔
276
    qDestroyTask(pTask->exec.pExecutor);
5,941✔
277
    pTask->exec.pExecutor = NULL;
5,941✔
278
  }
279

280
  if (pTask->exec.pWalReader != NULL) {
45,207✔
281
    walCloseReader(pTask->exec.pWalReader);
5,794✔
282
    pTask->exec.pWalReader = NULL;
5,794✔
283
  }
284

285
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
45,207✔
286

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

291
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
45,208✔
292
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
22,158✔
293
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
22,158!
294
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
22,158✔
295
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
22,158✔
296
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
23,050✔
297
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
19,639✔
298
  }
299

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

303
  if (pTask->pNameMap) {
45,204✔
304
    tSimpleHashCleanup(pTask->pNameMap);
1,863✔
305
  }
306

307
  streamDestroyStateMachine(pTask->status.pSM);
45,204✔
308
  pTask->status.pSM = NULL;
45,208✔
309

310
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
45,208✔
311

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

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

319
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
45,207✔
320
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
45,206✔
321

322
  if (pTask->id.idStr != NULL) {
45,206✔
323
    taosMemoryFree((void*)pTask->id.idStr);
22,507!
324
  }
325

326
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
45,208✔
327
  pTask->chkInfo.pActiveInfo = NULL;
45,207✔
328

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

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

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

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

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

361
  if (pTask->backendPath != NULL) {
45,207✔
362
    taosMemoryFree(pTask->backendPath);
11,451!
363
    pTask->backendPath = NULL;
11,448✔
364
  }
365
}
45,204✔
366

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

377
    pRange->range.maxVer = ver;
3,595✔
378
    pRange->range.minVer = ver;
3,595✔
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) {
7,850✔
383
      pChkInfo->checkpointVer = pRange->range.maxVer;
3,926✔
384
      pChkInfo->processedVer = pRange->range.maxVer;
3,926✔
385
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
3,926✔
386
    } else {
387
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
3,924✔
388
      pChkInfo->processedVer = pRange->range.minVer - 1;
3,924✔
389
      pChkInfo->nextProcessVer = pRange->range.minVer;
3,924✔
390

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

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

407
  if (pTask->info.fillHistory) {
11,444✔
408
    streamId = pTask->streamTaskId.streamId;
3,925✔
409
    taskId = pTask->streamTaskId.taskId;
3,925✔
410
  } else {
411
    streamId = pTask->id.streamId;
7,519✔
412
    taskId = pTask->id.taskId;
7,519✔
413
  }
414

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

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

427
  int32_t code = snprintf(pTask->backendPath, len + nBytes + 2, "%s%s%s", pTask->pMeta->path, TD_DIRSEP, id);
11,446✔
428
  if (code < 0 || code >= len + nBytes + 2) {
11,446!
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);
11,449✔
433
    return 0;
11,450✔
434
  }
435
}
436

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

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

455
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
11,446✔
456

457
  code = streamCreateStateMachine(pTask);
11,446✔
458
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
11,448!
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();
11,450✔
465
  setInitialVersionInfo(pTask, ver);
11,450✔
466

467
  pTask->pMeta = pMeta;
11,448✔
468
  pTask->pMsgCb = pMsgCb;
11,448✔
469
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
11,448✔
470
  if (pTask->msgInfo.pSendInfo == NULL) {
11,447!
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);
11,447✔
476
  if (code) {
11,446!
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};
11,446✔
482
  code = taosThreadMutexAttrInit(&attr);
11,446✔
483
  if (code != 0) {
11,445!
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);
11,445✔
489
  if (code != 0) {
11,444!
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);
11,444✔
495
  if (code) {
11,444!
496
    return code;
×
497
  }
498

499
  code = taosThreadMutexAttrDestroy(&attr);
11,444✔
500
  if (code) {
11,442!
501
    return code;
×
502
  }
503

504
  streamTaskOpenAllUpstreamInput(pTask);
11,442✔
505

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

520
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
11,450✔
521
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
11,451!
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));
11,451✔
527
  if (pTask->taskCheckInfo.pList == NULL) {
11,445!
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) {
11,445!
533
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
11,446✔
534
    if (code) {
11,445!
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);
11,444✔
541
}
542

543
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
92,986✔
544
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
92,986✔
545
    return 0;
5,322✔
546
  }
547

548
  int32_t type = pTask->outputInfo.type;
87,664✔
549
  if (type == TASK_OUTPUT__TABLE) {
87,664✔
550
    return 0;
257✔
551
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
87,407✔
552
    return 1;
7,378✔
553
  } else {
554
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
80,029✔
555
    return taosArrayGetSize(vgInfo);
80,029✔
556
  }
557
}
558

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

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

567
  if (pTask->upstreamInfo.pList == NULL) {
15,868✔
568
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
5,458✔
569
  }
570

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

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

607
      break;
56✔
608
    }
609
  }
610

611
  return code;
139✔
612
}
613

614
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
45,201✔
615
  if (pUpstreamInfo->pList != NULL) {
45,201✔
616
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
39,563✔
617
    pUpstreamInfo->numOfClosed = 0;
39,566✔
618
    pUpstreamInfo->pList = NULL;
39,566✔
619
  }
620
}
45,204✔
621

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

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

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

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

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

652
      if (pVgInfo->vgId == nodeId) {
99✔
653
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
52✔
654
        if (!isEqual) {
52✔
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,
28!
669
                  pVgInfo->taskId, nodeId, buf);
670
        }
671
        break;
52✔
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;
139✔
699
}
700

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

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

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

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

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

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

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

757
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
11,446✔
758
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
11,446✔
759
    return;
5,792✔
760
  }
761

762
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
5,654✔
763
  for (int32_t i = 0; i < size; ++i) {
21,862✔
764
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
16,208✔
765
    pInfo->stage = -1;
16,207✔
766
  }
767

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

771
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
17,729✔
772
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
17,729✔
773
  if (num == 0) {
17,735✔
774
    return;
8,927✔
775
  }
776

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

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

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

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

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

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

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

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

818
  streamMutexLock(&pTask->lock);
77,268✔
819
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
77,271✔
820
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
46,327✔
821
    ret = true;
46,327✔
822
  }
823

824
  streamMutexUnlock(&pTask->lock);
77,271✔
825
  return ret;
77,269✔
826
}
827

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

836
  return status;
45,187✔
837
}
838

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

845
  return status;
1,142✔
846
}
847

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

853
  if (pTask->info.fillHistory == 0) {
5,094✔
854
    return code;
5,088✔
855
  }
856

857
  code = streamMetaAcquireTaskUnsafe(pMeta, &pTask->streamTaskId, &pStreamTask);
6✔
858
  if (code == 0) {
7!
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 = streamMetaSaveTaskInMeta(pMeta, pStreamTask);
×
873
    streamMutexUnlock(&(pStreamTask->lock));
×
874

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

878
  return code;
7✔
879
}
880

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

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

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

900
  return code;
7✔
901
}
902

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

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

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

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

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

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

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

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

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

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

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

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

1000
  STaskStatusEntry entry = {
68,874✔
1001
      .id = streamTaskGetTaskId(pTask),
22,958✔
1002
      .status = streamTaskGetStatus(pTask).state,
22,958✔
1003
      .nodeId = pMeta->vgId,
22,958✔
1004
      .stage = pMeta->stage,
22,958✔
1005

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

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

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

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

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

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

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

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

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

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

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

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

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

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

1109
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
79,774✔
1110
  for (int32_t i = 0; i < num; ++i) {
163,275!
1111
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
163,276✔
1112
    if (pInfo == NULL) {
163,267!
1113
      return;
×
1114
    }
1115

1116
    if (pInfo->taskId == taskId) {
163,267✔
1117
      *pEpInfo = pInfo;
79,764✔
1118
      return;
79,764✔
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) {
11,446✔
1148
  char buf[128] = {0};
11,446✔
1149
  int32_t code = snprintf(buf, tListLen(buf),"0x%" PRIx64 "-0x%x", streamId, taskId);
11,446✔
1150
  if (code < 0 || code >= tListLen(buf)) {
11,446!
1151
    return TSDB_CODE_OUT_OF_BUFFER;
×
1152
  }
1153

1154
  *pId = taosStrdup(buf);
11,451!
1155

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

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

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

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

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

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

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

1235
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
11,482✔
1236
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
11,481✔
1237
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
11,476✔
1238

1239
  *pRes = pInfo;
11,480✔
1240
  return code;
11,480✔
1241
}
1242

1243
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
45,206✔
1244
  if (pInfo == NULL) {
45,206✔
1245
    return;
33,723✔
1246
  }
1247

1248
  streamMutexDestroy(&pInfo->lock);
11,483✔
1249
  taosArrayDestroy(pInfo->pDispatchTriggerList);
11,484✔
1250
  pInfo->pDispatchTriggerList = NULL;
11,484✔
1251
  taosArrayDestroy(pInfo->pReadyMsgList);
11,484✔
1252
  pInfo->pReadyMsgList = NULL;
11,484✔
1253
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
11,484✔
1254
  pInfo->pCheckpointReadyRecvList = NULL;
11,484✔
1255

1256
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
11,484✔
1257
  if (pTriggerTmr->tmrHandle != NULL) {
11,484✔
1258
    streamTmrStop(pTriggerTmr->tmrHandle);
1,739✔
1259
    pTriggerTmr->tmrHandle = NULL;
1,739✔
1260
  }
1261

1262
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
11,484✔
1263
  if (pReadyTmr->tmrHandle != NULL) {
11,484✔
1264
    streamTmrStop(pReadyTmr->tmrHandle);
1,712✔
1265
    pReadyTmr->tmrHandle = NULL;
1,712✔
1266
  }
1267

1268
  taosMemoryFree(pInfo);
11,484!
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) {
2,758✔
1274
  pInfo->activeId = 0;
2,758✔
1275
  pInfo->transId = 0;
2,758✔
1276
  pInfo->allUpstreamTriggerRecv = 0;
2,758✔
1277
  pInfo->dispatchTrigger = false;
2,758✔
1278

1279
  taosArrayClear(pInfo->pDispatchTriggerList);
2,758✔
1280
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
2,755✔
1281
}
2,754✔
1282

1283
const char* streamTaskGetExecType(int32_t type) {
129,250✔
1284
  switch (type) {
129,250!
1285
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
67,406✔
1286
      return "scan-wal-file";
67,406✔
1287
    case STREAM_EXEC_T_START_ALL_TASKS:
7,705✔
1288
      return "start-all-tasks";
7,705✔
1289
    case STREAM_EXEC_T_START_ONE_TASK:
5,562✔
1290
      return "start-one-task";
5,562✔
1291
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
20✔
1292
      return "restart-all-tasks";
20✔
1293
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,655✔
1294
      return "stop-all-tasks";
4,655✔
1295
    case STREAM_EXEC_T_RESUME_TASK:
6,533✔
1296
      return "resume-task-from-idle";
6,533✔
1297
    case STREAM_EXEC_T_ADD_FAILED_TASK:
3✔
1298
      return "record-start-failed-task";
3✔
1299
    case STREAM_EXEC_T_STOP_ONE_TASK:
×
1300
      return "stop-one-task";
×
1301
    case 0:
37,459✔
1302
      return "exec-all-tasks";
37,459✔
1303
    default:
×
1304
      return "invalid-exec-type";
×
1305
  }
1306
}
1307

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

1324
void streamTaskFreeRefId(int64_t* pRefId) {
25,777✔
1325
  if (pRefId == NULL) {
25,777✔
1326
    return;
2,185✔
1327
  }
1328

1329
  metaRefMgtRemove(pRefId);
23,592✔
1330
}
1331

1332
static int32_t tEncodeStreamNotifyInfo(SEncoder* pEncoder, const SNotifyInfo* info) {
99,519✔
1333
  int32_t code = TSDB_CODE_SUCCESS;
99,519✔
1334
  int32_t lino = 0;
99,519✔
1335

1336
  QUERY_CHECK_NULL(pEncoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
99,519!
1337
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
99,519!
1338

1339
  int32_t addrSize = taosArrayGetSize(info->pNotifyAddrUrls);
99,519✔
1340
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, addrSize));
99,524✔
1341
  for (int32_t i = 0; i < addrSize; ++i) {
99,502!
1342
    const char* url = taosArrayGetP(info->pNotifyAddrUrls, i);
×
1343
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, url));
×
1344
  }
1345
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyEventTypes));
199,004!
1346
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, info->notifyErrorHandle));
199,004!
1347
  if (addrSize > 0) {
99,502!
1348
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, info->streamName));
×
1349
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, info->stbFullName));
×
1350
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, info->pSchemaWrapper));
×
1351
  }
1352

1353
_exit:
99,502✔
1354
  if (code != TSDB_CODE_SUCCESS) {
99,524✔
1355
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
22!
1356
  }
1357
  return code;
99,517✔
1358
}
1359

1360
static int32_t tDecodeStreamNotifyInfo(SDecoder* pDecoder, SNotifyInfo* info) {
34,101✔
1361
  int32_t code = TSDB_CODE_SUCCESS;
34,101✔
1362
  int32_t lino = 0;
34,101✔
1363

1364
  QUERY_CHECK_NULL(pDecoder, code, lino, _exit, TSDB_CODE_INVALID_PARA);
34,101!
1365
  QUERY_CHECK_NULL(info, code, lino, _exit, TSDB_CODE_INVALID_PARA);
34,101!
1366

1367
  int32_t addrSize = 0;
34,101✔
1368
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &addrSize));
34,105!
1369
  info->pNotifyAddrUrls = taosArrayInit(addrSize, POINTER_BYTES);
34,105✔
1370
  QUERY_CHECK_NULL(info->pNotifyAddrUrls, code, lino, _exit, terrno);
34,108!
1371
  for (int32_t i = 0; i < addrSize; ++i) {
34,108!
1372
    char *url = NULL;
×
1373
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &url));
×
1374
    url = taosStrndup(url, TSDB_STREAM_NOTIFY_URL_LEN);
×
1375
    QUERY_CHECK_NULL(url, code, lino, _exit, terrno);
×
1376
    if (taosArrayPush(info->pNotifyAddrUrls, &url) == NULL) {
×
1377
      taosMemoryFree(url);
×
1378
      TAOS_CHECK_EXIT(terrno);
×
1379
    }
1380
  }
1381
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyEventTypes));
68,210!
1382
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &info->notifyErrorHandle));
68,208!
1383
  if (addrSize > 0) {
34,106!
1384
    char* name = NULL;
×
1385
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &name));
×
1386
    info->streamName = taosStrndup(name, TSDB_STREAM_FNAME_LEN + 1);
×
1387
    QUERY_CHECK_NULL(info->streamName, code, lino, _exit, terrno);
×
1388
    TAOS_CHECK_EXIT(tDecodeCStr(pDecoder, &name));
×
1389
    info->stbFullName = taosStrndup(name, TSDB_STREAM_FNAME_LEN + 1);
×
1390
    QUERY_CHECK_NULL(info->stbFullName, code, lino, _exit, terrno);
×
1391
    info->pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
×
1392
    if (info->pSchemaWrapper == NULL) {
×
1393
      TAOS_CHECK_EXIT(terrno);
×
1394
    }
1395
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, info->pSchemaWrapper));
×
1396
  }
1397

1398
_exit:
34,106✔
1399
  if (code != TSDB_CODE_SUCCESS) {
34,106!
1400
    stError("%s failed at line %d since %s", __func__, lino, tstrerror(code));
×
1401
  }
1402
  return code;
34,106✔
1403
}
1404

1405
int32_t tEncodeStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
99,543✔
1406
  int32_t code = 0;
99,543✔
1407
  int32_t lino;
1408

1409
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
99,543!
1410
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->ver));
199,098!
1411
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->id.streamId));
199,098!
1412
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->id.taskId));
199,098!
1413
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.trigger));
199,098!
1414
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.taskLevel));
199,098!
1415
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.type));
199,098!
1416
  TAOS_CHECK_EXIT(tEncodeI16(pEncoder, pTask->msgInfo.msgType));
199,098!
1417

1418
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.taskStatus));
199,098!
1419
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->status.schedStatus));
199,098!
1420

1421
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.selfChildId));
199,098!
1422
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->info.nodeId));
199,098!
1423
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.epSet));
99,549!
1424
  TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->info.mnodeEpset));
99,547!
1425

1426
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointId));
199,086!
1427
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->chkInfo.checkpointVer));
199,086!
1428
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->info.fillHistory));
199,086!
1429

1430
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->hTaskInfo.id.streamId));
199,086!
1431
  int32_t taskId = pTask->hTaskInfo.id.taskId;
99,543✔
1432
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
99,543!
1433

1434
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->streamTaskId.streamId));
199,086!
1435
  taskId = pTask->streamTaskId.taskId;
99,543✔
1436
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, taskId));
99,543!
1437

1438
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.minVer));
199,086!
1439
  TAOS_CHECK_EXIT(tEncodeU64(pEncoder, pTask->dataRange.range.maxVer));
199,086!
1440
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.skey));
199,086!
1441
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->dataRange.window.ekey));
199,086!
1442

1443
  int32_t epSz = taosArrayGetSize(pTask->upstreamInfo.pList);
99,543✔
1444
  TAOS_CHECK_EXIT(tEncodeI32(pEncoder, epSz));
99,542!
1445
  for (int32_t i = 0; i < epSz; i++) {
241,207✔
1446
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
141,674✔
1447
    TAOS_CHECK_EXIT(tEncodeStreamEpInfo(pEncoder, pInfo));
141,671!
1448
  }
1449

1450
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
99,533✔
1451
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->exec.qmsg));
105,076!
1452
  }
1453

1454
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
99,533✔
1455
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.tbSink.stbUid));
97,790!
1456
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.tbSink.stbFullName));
97,790!
1457
    TAOS_CHECK_EXIT(tEncodeSSchemaWrapper(pEncoder, pTask->outputInfo.tbSink.pSchemaWrapper));
97,790!
1458
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
50,638✔
1459
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->outputInfo.smaSink.smaId));
692!
1460
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
50,292!
1461
    TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->outputInfo.fetchSink.reserved));
×
1462
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
50,292✔
1463
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.taskId));
14,268!
1464
    TAOS_CHECK_EXIT(tEncodeI32(pEncoder, pTask->outputInfo.fixedDispatcher.nodeId));
14,268!
1465
    TAOS_CHECK_EXIT(tEncodeSEpSet(pEncoder, &pTask->outputInfo.fixedDispatcher.epSet));
7,134!
1466
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
43,158✔
1467
    TAOS_CHECK_EXIT(tSerializeSUseDbRspImp(pEncoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
43,113!
1468
    TAOS_CHECK_EXIT(tEncodeCStr(pEncoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
86,226!
1469
  }
1470
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pTask->info.delaySchedParam));
199,066!
1471
  TAOS_CHECK_EXIT(tEncodeI8(pEncoder, pTask->subtableWithoutMd5));
199,066!
1472
  TAOS_CHECK_EXIT(tEncodeCStrWithLen(pEncoder, pTask->reserve, sizeof(pTask->reserve) - 1));
199,066!
1473

1474
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
99,533✔
1475
    TAOS_CHECK_EXIT(tEncodeStreamNotifyInfo(pEncoder, &pTask->notifyInfo));
99,521✔
1476
  }
1477

1478
  tEndEncode(pEncoder);
99,508✔
1479
_exit:
99,543✔
1480
  return code;
99,543✔
1481
}
1482

1483
int32_t tDecodeStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
34,108✔
1484
  int32_t taskId = 0;
34,108✔
1485
  int32_t code = 0;
34,108✔
1486
  int32_t lino;
1487

1488
  TAOS_CHECK_EXIT(tStartDecode(pDecoder));
34,108!
1489
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->ver));
68,228✔
1490
  if (pTask->ver <= SSTREAM_TASK_INCOMPATIBLE_VER || pTask->ver > SSTREAM_TASK_VER) {
34,106!
1491
    TAOS_CHECK_EXIT(TSDB_CODE_INVALID_MSG);
×
1492
  }
1493

1494
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->id.streamId));
68,213!
1495
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->id.taskId));
68,213!
1496
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.trigger));
68,210!
1497
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.taskLevel));
68,208!
1498
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.type));
68,208!
1499
  TAOS_CHECK_EXIT(tDecodeI16(pDecoder, &pTask->msgInfo.msgType));
68,204!
1500

1501
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.taskStatus));
68,197!
1502
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->status.schedStatus));
68,194!
1503

1504
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.selfChildId));
68,197!
1505
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->info.nodeId));
68,200!
1506
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.epSet));
34,100!
1507
  TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->info.mnodeEpset));
34,106!
1508

1509
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointId));
68,213!
1510
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->chkInfo.checkpointVer));
68,207!
1511
  TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->info.fillHistory));
68,201!
1512

1513
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->hTaskInfo.id.streamId));
68,200!
1514
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
34,101!
1515
  pTask->hTaskInfo.id.taskId = taskId;
34,101✔
1516

1517
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->streamTaskId.streamId));
68,201!
1518
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &taskId));
34,098!
1519
  pTask->streamTaskId.taskId = taskId;
34,098✔
1520

1521
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.minVer));
68,197!
1522
  TAOS_CHECK_EXIT(tDecodeU64(pDecoder, (uint64_t*)&pTask->dataRange.range.maxVer));
68,198!
1523
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.skey));
68,200!
1524
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->dataRange.window.ekey));
68,200!
1525

1526
  int32_t epSz = -1;
34,099✔
1527
  TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &epSz) < 0);
34,100!
1528

1529
  if ((pTask->upstreamInfo.pList = taosArrayInit(epSz, POINTER_BYTES)) == NULL) {
34,100!
1530
    TAOS_CHECK_EXIT(terrno);
×
1531
  }
1532
  for (int32_t i = 0; i < epSz; i++) {
81,927✔
1533
    SStreamUpstreamEpInfo* pInfo = taosMemoryCalloc(1, sizeof(SStreamUpstreamEpInfo));
47,822!
1534
    if (pInfo == NULL) {
47,813!
1535
      TAOS_CHECK_EXIT(terrno);
×
1536
    }
1537
    if ((code = tDecodeStreamEpInfo(pDecoder, pInfo)) < 0) {
47,813!
1538
      taosMemoryFreeClear(pInfo);
×
1539
      goto _exit;
×
1540
    }
1541
    if (taosArrayPush(pTask->upstreamInfo.pList, &pInfo) == NULL) {
95,642!
1542
      TAOS_CHECK_EXIT(terrno);
×
1543
    }
1544
  }
1545

1546
  if (pTask->info.taskLevel != TASK_LEVEL__SINK) {
34,105✔
1547
    TAOS_CHECK_EXIT(tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg));
35,825!
1548
  }
1549

1550
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
34,106✔
1551
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.tbSink.stbUid));
33,473!
1552
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.tbSink.stbFullName));
16,737!
1553
    pTask->outputInfo.tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
16,739!
1554
    if (pTask->outputInfo.tbSink.pSchemaWrapper == NULL) {
16,738!
1555
      TAOS_CHECK_EXIT(terrno);
×
1556
    }
1557
    TAOS_CHECK_EXIT(tDecodeSSchemaWrapper(pDecoder, pTask->outputInfo.tbSink.pSchemaWrapper));
33,458!
1558
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SMA) {
17,370✔
1559
    TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->outputInfo.smaSink.smaId));
240!
1560
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FETCH) {
17,250!
1561
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->outputInfo.fetchSink.reserved));
×
1562
  } else if (pTask->outputInfo.type == TASK_OUTPUT__FIXED_DISPATCH) {
17,250✔
1563
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.taskId));
4,844!
1564
    TAOS_CHECK_EXIT(tDecodeI32(pDecoder, &pTask->outputInfo.fixedDispatcher.nodeId));
4,844!
1565
    TAOS_CHECK_EXIT(tDecodeSEpSet(pDecoder, &pTask->outputInfo.fixedDispatcher.epSet));
2,422!
1566
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
14,828✔
1567
    TAOS_CHECK_EXIT(tDeserializeSUseDbRspImp(pDecoder, &pTask->outputInfo.shuffleDispatcher.dbInfo));
14,823!
1568
    TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->outputInfo.shuffleDispatcher.stbFullName));
14,823!
1569
  }
1570
  TAOS_CHECK_EXIT(tDecodeI64(pDecoder, &pTask->info.delaySchedParam));
68,191!
1571
  if (pTask->ver >= SSTREAM_TASK_SUBTABLE_CHANGED_VER) {
34,101!
1572
    TAOS_CHECK_EXIT(tDecodeI8(pDecoder, &pTask->subtableWithoutMd5));
68,205!
1573
  }
1574
  TAOS_CHECK_EXIT(tDecodeCStrTo(pDecoder, pTask->reserve));
34,102!
1575

1576
  if (pTask->ver >= SSTREAM_TASK_ADD_NOTIFY_VER) {
34,104!
1577
    TAOS_CHECK_EXIT(tDecodeStreamNotifyInfo(pDecoder, &pTask->notifyInfo));
34,104!
1578
  }
1579

1580
  tEndDecode(pDecoder);
34,107✔
1581

1582
_exit:
34,113✔
1583
  return code;
34,113✔
1584
}
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