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

taosdata / TDengine / #3525

10 Nov 2024 03:50AM UTC coverage: 60.818% (-0.08%) from 60.898%
#3525

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

118634 of 249004 branches covered (47.64%)

Branch coverage included in aggregate %.

136 of 169 new or added lines in 23 files covered. (80.47%)

542 existing lines in 129 files now uncovered.

199071 of 273386 relevant lines covered (72.82%)

15691647.46 hits per line

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

71.59
/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

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

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

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

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

50
    if (!isEqual) {
112✔
51
      (*pUpdated) = true;
72✔
52
      char tmp[512] = {0};
72✔
53
      code = epsetToStr(&pTask->info.epSet, tmp, tListLen(tmp));  // only for log file, ignore errors
72✔
54
      if (code) { // print error and continue
72!
55
        stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
56
        return code;
×
57
      }
58

59
      epsetAssign(&pTask->info.epSet, pEpSet);
72✔
60
      stDebug("s-task:0x%x (vgId:%d) self node epset is updated %s, old:%s", pTask->id.taskId, nodeId, buf, tmp);
72!
61
    } else {
62
      stDebug("s-task:0x%x (vgId:%d) not updated task epset, since epset identical, %s", pTask->id.taskId, nodeId, buf);
40!
63
    }
64
  }
65

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

77
  return code;
352✔
78
}
79

80
static void freeItem(void* p) {
×
81
  SStreamContinueExecInfo* pInfo = p;
×
82
  rpcFreeCont(pInfo->msg.pCont);
×
83
}
×
84

85
static void freeUpstreamItem(void* p) {
82,401✔
86
  SStreamUpstreamEpInfo** pInfo = p;
82,401✔
87
  taosMemoryFree(*pInfo);
82,401✔
88
}
82,405✔
89

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

97
  pEpInfo->childId = pTask->info.selfChildId;
19,337✔
98
  pEpInfo->epSet = pTask->info.epSet;
19,337✔
99
  pEpInfo->nodeId = pTask->info.nodeId;
19,337✔
100
  pEpInfo->taskId = pTask->id.taskId;
19,337✔
101
  pEpInfo->stage = -1;
19,337✔
102

103
  return pEpInfo;
19,337✔
104
}
105

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

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

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

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

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

134
  char buf[128] = {0};
13,469✔
135
  sprintf(buf, "0x%" PRIx64 "-0x%x", pTask->id.streamId, pTask->id.taskId);
13,469✔
136

137
  pTask->id.idStr = taosStrdup(buf);
13,469✔
138
  if (pTask->id.idStr == NULL) {
13,469!
139
    stError("s-task:0x%x failed to build task id, code: out of memory", pTask->id.taskId);
×
140
    return terrno;
×
141
  }
142

143
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
13,469✔
144
  pTask->status.taskStatus = fillHistory ? TASK_STATUS__SCAN_HISTORY : TASK_STATUS__READY;
13,469✔
145
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
13,469✔
146
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
13,469✔
147

148
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
13,469✔
149
  code = taosThreadMutexInit(&pTask->taskCheckInfo.checkInfoLock, NULL);
13,469✔
150
  if (code) {
13,469!
151
    return code;
×
152
  }
153

154
  if (fillHistory && !hasFillhistory) {
13,469!
155
    stError("s-task:0x%x create task failed, due to inconsistent fill-history flag", pTask->id.taskId);
×
156
    return TSDB_CODE_INVALID_PARA;
×
157
  }
158

159
  epsetAssign(&(pTask->info.mnodeEpset), pEpset);
13,469✔
160

161
  code = addToTaskset(pTaskList, pTask);
13,469✔
162
  *p = pTask;
13,469✔
163

164
  return code;
13,469✔
165
}
166

167
int32_t tDecodeStreamTaskChkInfo(SDecoder* pDecoder, SCheckpointInfo* pChkpInfo) {
682✔
168
  int64_t skip64;
169
  int8_t  skip8;
170
  int32_t skip32;
171
  int16_t skip16;
172
  SEpSet  epSet;
173

174
  if (tStartDecode(pDecoder) < 0) return -1;
682!
175
  if (tDecodeI64(pDecoder, &pChkpInfo->msgVer) < 0) return -1;
1,368!
176
  // if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
177

178
  if (tDecodeI64(pDecoder, &skip64) < 0) return -1;
684!
179
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
684!
180
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
684!
181
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
684!
182
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
684!
183
  if (tDecodeI16(pDecoder, &skip16) < 0) return -1;
684!
184

185
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
684!
186
  if (tDecodeI8(pDecoder, &skip8) < 0) return -1;
684!
187

188
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
684!
189
  if (tDecodeI32(pDecoder, &skip32) < 0) return -1;
683!
190
  if (tDecodeSEpSet(pDecoder, &epSet) < 0) return -1;
683!
191
  if (tDecodeSEpSet(pDecoder, &epSet) < 0) return -1;
684!
192

193
  if (tDecodeI64(pDecoder, &pChkpInfo->checkpointId) < 0) return -1;
1,368!
194
  if (tDecodeI64(pDecoder, &pChkpInfo->checkpointVer) < 0) return -1;
1,368!
195

196
  tEndDecode(pDecoder);
684✔
197
  return 0;
684✔
198
}
199

200
int32_t tDecodeStreamTaskId(SDecoder* pDecoder, STaskId* pTaskId) {
56✔
201
  int64_t ver;
202
  if (tStartDecode(pDecoder) < 0) return -1;
56!
203
  if (tDecodeI64(pDecoder, &ver) < 0) return -1;
56!
204
  if (ver <= SSTREAM_TASK_INCOMPATIBLE_VER) return -1;
56!
205

206
  if (tDecodeI64(pDecoder, &pTaskId->streamId) < 0) return -1;
112!
207

208
  int32_t taskId = 0;
56✔
209
  if (tDecodeI32(pDecoder, &taskId) < 0) return -1;
56!
210

211
  pTaskId->taskId = taskId;
56✔
212
  tEndDecode(pDecoder);
56✔
213
  return 0;
56✔
214
}
215

216
void tFreeStreamTask(void* pParam) {
59,191✔
217
  char*        p = NULL;
59,191✔
218
  SStreamTask* pTask = pParam;
59,191✔
219
  int32_t      taskId = pTask->id.taskId;
59,191✔
220

221
  STaskExecStatisInfo* pStatis = &pTask->execInfo;
59,191✔
222

223
  ETaskStatus status1 = TASK_STATUS__UNINIT;
59,191✔
224
  streamMutexLock(&pTask->lock);
59,191✔
225
  if (pTask->status.pSM != NULL) {
59,209✔
226
    SStreamTaskState status = streamTaskGetStatus(pTask);
27,521✔
227
    p = status.name;
27,511✔
228
    status1 = status.state;
27,511✔
229
  }
230
  streamMutexUnlock(&pTask->lock);
59,199✔
231

232
  stDebug("start to free s-task:0x%x %p, state:%s, refId:%" PRId64, taskId, pTask, p, pTask->id.refId);
59,215✔
233

234
  SCheckpointInfo* pCkInfo = &pTask->chkInfo;
59,215✔
235
  stDebug("s-task:0x%x task exec summary: create:%" PRId64 ", init:%" PRId64 ", start:%" PRId64
59,215✔
236
          ", updateCount:%d latestUpdate:%" PRId64 ", latestCheckPoint:%" PRId64 ", ver:%" PRId64
237
          " nextProcessVer:%" PRId64 ", checkpointCount:%d",
238
          taskId, pStatis->created, pStatis->checkTs, pStatis->readyTs, pStatis->updateCount, pStatis->latestUpdateTs,
239
          pCkInfo->checkpointId, pCkInfo->checkpointVer, pCkInfo->nextProcessVer, pStatis->checkpoint);
240

241
  if (pTask->schedInfo.pDelayTimer != NULL) {
59,215✔
242
    streamTmrStop(pTask->schedInfo.pDelayTimer);
1,177✔
243
    pTask->schedInfo.pDelayTimer = NULL;
1,177✔
244
  }
245

246
  if (pTask->hTaskInfo.pTimer != NULL) {
59,215✔
247
    streamTmrStop(pTask->hTaskInfo.pTimer);
1,852✔
248
    pTask->hTaskInfo.pTimer = NULL;
1,855✔
249
  }
250

251
  if (pTask->msgInfo.pRetryTmr != NULL) {
59,218✔
252
    streamTmrStop(pTask->msgInfo.pRetryTmr);
5,590✔
253
    pTask->msgInfo.pRetryTmr = NULL;
5,593✔
254
  }
255

256
  if (pTask->inputq.queue) {
59,221✔
257
    streamQueueClose(pTask->inputq.queue, pTask->id.taskId);
14,021✔
258
    pTask->inputq.queue = NULL;
14,019✔
259
  }
260

261
  if (pTask->outputq.queue) {
59,219✔
262
    streamQueueClose(pTask->outputq.queue, pTask->id.taskId);
14,017✔
263
    pTask->outputq.queue = NULL;
14,028✔
264
  }
265

266
  if (pTask->exec.qmsg) {
59,230✔
267
    taosMemoryFree(pTask->exec.qmsg);
31,147✔
268
  }
269

270
  if (pTask->exec.pExecutor) {
59,228✔
271
    qDestroyTask(pTask->exec.pExecutor);
7,073✔
272
    pTask->exec.pExecutor = NULL;
7,073✔
273
  }
274

275
  if (pTask->exec.pWalReader != NULL) {
59,228✔
276
    walCloseReader(pTask->exec.pWalReader);
7,016✔
277
    pTask->exec.pWalReader = NULL;
7,017✔
278
  }
279

280
  streamClearChkptReadyMsg(pTask->chkInfo.pActiveInfo);
59,229✔
281

282
  if (pTask->msgInfo.pData != NULL) {
59,210✔
283
    clearBufferedDispatchMsg(pTask);
41✔
284
  }
285

286
  if (pTask->outputInfo.type == TASK_OUTPUT__TABLE) {
59,214✔
287
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pSchemaWrapper);
28,881✔
288
    taosMemoryFree(pTask->outputInfo.tbSink.pTSchema);
28,886✔
289
    tSimpleHashCleanup(pTask->outputInfo.tbSink.pTbInfo);
28,887✔
290
    tDeleteSchemaWrapper(pTask->outputInfo.tbSink.pTagSchema);
28,887✔
291
  } else if (pTask->outputInfo.type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
30,333✔
292
    taosArrayDestroy(pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos);
25,787✔
293
  }
294

295
  streamTaskCleanupCheckInfo(&pTask->taskCheckInfo);
59,220✔
296
  streamFreeTaskState(pTask, pTask->status.removeBackendFiles ? 1 : 0);
59,219✔
297

298
  if (pTask->pNameMap) {
59,218✔
299
    tSimpleHashCleanup(pTask->pNameMap);
2,241✔
300
  }
301

302
  streamDestroyStateMachine(pTask->status.pSM);
59,218✔
303
  pTask->status.pSM = NULL;
59,219✔
304

305
  streamTaskDestroyUpstreamInfo(&pTask->upstreamInfo);
59,219✔
306

307
  taosMemoryFree(pTask->outputInfo.pTokenBucket);
59,222✔
308
  streamMutexDestroy(&pTask->lock);
59,222✔
309

310
  taosArrayDestroy(pTask->msgInfo.pSendInfo);
59,220✔
311
  pTask->msgInfo.pSendInfo = NULL;
59,221✔
312
  streamMutexDestroy(&pTask->msgInfo.lock);
59,221✔
313

314
  taosArrayDestroy(pTask->outputInfo.pNodeEpsetUpdateList);
59,218✔
315
  pTask->outputInfo.pNodeEpsetUpdateList = NULL;
59,222✔
316

317
  if (pTask->id.idStr != NULL) {
59,222✔
318
    taosMemoryFree((void*)pTask->id.idStr);
27,497✔
319
  }
320

321
  streamTaskDestroyActiveChkptInfo(pTask->chkInfo.pActiveInfo);
59,222✔
322
  pTask->chkInfo.pActiveInfo = NULL;
59,221✔
323

324
  taosMemoryFree(pTask);
59,221✔
325
  stDebug("s-task:0x%x free task completed", taskId);
59,223✔
326
}
59,223✔
327

328
void streamFreeTaskState(SStreamTask* pTask, int8_t remove) {
59,217✔
329
  stDebug("s-task:0x%x start to free task state/backend", pTask->id.taskId);
59,217✔
330
  if (pTask->pState != NULL) {
59,217✔
331
    stDebug("s-task:0x%x start to free task state", pTask->id.taskId);
7,074✔
332
    streamStateClose(pTask->pState, remove);
7,074✔
333

334
    if (remove) taskDbSetClearFileFlag(pTask->pBackend);
7,074✔
335
    taskDbRemoveRef(pTask->pBackend);
7,074✔
336
    pTask->pBackend = NULL;
7,073✔
337
    pTask->pState = NULL;
7,073✔
338
  } else {
339
    stDebug("s-task:0x%x task state is NULL, may del backend:%s", pTask->id.taskId,
52,143✔
340
            pTask->backendPath ? pTask->backendPath : "NULL");
341
    if (remove) {
52,144✔
342
      if (pTask->backendPath != NULL) {
3,416!
343
        stDebug("s-task:0x%x task state is NULL, do del backend:%s", pTask->id.taskId, pTask->backendPath);
3,417✔
344
        taosRemoveDir(pTask->backendPath);
3,417✔
345
      }
346
    }
347
  }
348

349
  if (pTask->backendPath != NULL) {
59,213✔
350
    taosMemoryFree(pTask->backendPath);
14,021✔
351
    pTask->backendPath = NULL;
14,023✔
352
  }
353
}
59,215✔
354

355
static void setInitialVersionInfo(SStreamTask* pTask, int64_t ver) {
14,239✔
356
  SCheckpointInfo* pChkInfo = &pTask->chkInfo;
14,239✔
357
  SDataRange*      pRange = &pTask->dataRange;
14,239✔
358

359
  // only set the version info for stream tasks without fill-history task
360
  if ((pTask->info.fillHistory == 0) && (!HAS_RELATED_FILLHISTORY_TASK(pTask))) {
14,239✔
361
    pChkInfo->checkpointVer = ver - 1;  // only update when generating checkpoint
4,153✔
362
    pChkInfo->processedVer = ver - 1;   // already processed version
4,153✔
363
    pChkInfo->nextProcessVer = ver;     // next processed version
4,153✔
364

365
    pRange->range.maxVer = ver;
4,153✔
366
    pRange->range.minVer = ver;
4,153✔
367
  } else {
368
    // the initial value of processedVer/nextProcessVer/checkpointVer for stream task with related fill-history task
369
    // is set at the mnode.
370
    if (pTask->info.fillHistory == 1) {
10,086✔
371
      pChkInfo->checkpointVer = pRange->range.maxVer;
5,108✔
372
      pChkInfo->processedVer = pRange->range.maxVer;
5,108✔
373
      pChkInfo->nextProcessVer = pRange->range.maxVer + 1;
5,108✔
374
    } else {
375
      pChkInfo->checkpointVer = pRange->range.minVer - 1;
4,978✔
376
      pChkInfo->processedVer = pRange->range.minVer - 1;
4,978✔
377
      pChkInfo->nextProcessVer = pRange->range.minVer;
4,978✔
378

379
      {  // for compatible purpose, remove it later
380
        if (pRange->range.minVer == 0) {
4,978✔
381
          pChkInfo->checkpointVer = 0;
2,520✔
382
          pChkInfo->processedVer = 0;
2,520✔
383
          pChkInfo->nextProcessVer = 1;
2,520✔
384
          stDebug("s-task:%s update the processedVer to 0 from -1 due to compatible purpose", pTask->id.idStr);
2,520✔
385
        }
386
      }
387
    }
388
  }
389
}
14,239✔
390

391
int32_t streamTaskSetBackendPath(SStreamTask* pTask) {
14,254✔
392
  int64_t streamId = 0;
14,254✔
393
  int32_t taskId = 0;
14,254✔
394

395
  if (pTask->info.fillHistory) {
14,254✔
396
    streamId = pTask->streamTaskId.streamId;
5,108✔
397
    taskId = pTask->streamTaskId.taskId;
5,108✔
398
  } else {
399
    streamId = pTask->id.streamId;
9,146✔
400
    taskId = pTask->id.taskId;
9,146✔
401
  }
402

403
  char    id[128] = {0};
14,254✔
404
  int32_t nBytes = sprintf(id, "0x%" PRIx64 "-0x%x", streamId, taskId);
14,254✔
405
  if (nBytes < 0 || nBytes >= sizeof(id)) {
14,254!
406
    return TSDB_CODE_OUT_OF_BUFFER;
×
407
  }
408

409
  int32_t len = strlen(pTask->pMeta->path);
14,255✔
410
  pTask->backendPath = (char*)taosMemoryMalloc(len + nBytes + 2);
14,255✔
411
  if (pTask->backendPath == NULL) {
14,257!
412
    return terrno;
×
413
  }
414

415
  (void)sprintf(pTask->backendPath, "%s%s%s", pTask->pMeta->path, TD_DIRSEP, id);
14,257✔
416
  stDebug("s-task:%s set backend path:%s", pTask->id.idStr, pTask->backendPath);
14,257✔
417

418
  return 0;
14,254✔
419
}
420

421
int32_t streamTaskInit(SStreamTask* pTask, SStreamMeta* pMeta, SMsgCb* pMsgCb, int64_t ver) {
14,242✔
422
  int32_t code = createStreamTaskIdStr(pTask->id.streamId, pTask->id.taskId, &pTask->id.idStr);
14,242✔
423
  if (code) {
14,247!
424
    stError("0x%x failed create stream task id str, code:%s", pTask->id.taskId, tstrerror(code));
×
425
    return code;
×
426
  }
427

428
  pTask->id.refId = 0;
14,247✔
429
  pTask->inputq.status = TASK_INPUT_STATUS__NORMAL;
14,247✔
430
  pTask->outputq.status = TASK_OUTPUT_STATUS__NORMAL;
14,247✔
431

432
  int32_t code1 = streamQueueOpen(512 << 10, &pTask->inputq.queue);
14,247✔
433
  int32_t code2 = streamQueueOpen(512 << 10, &pTask->outputq.queue);
14,236✔
434
  if (code1 || code2) {
14,259!
UNCOV
435
    stError("s-task:%s failed to prepare the input/output queue, initialize task failed", pTask->id.idStr);
×
436
    return TSDB_CODE_OUT_OF_MEMORY;
×
437
  }
438

439
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
14,259✔
440

441
  code = streamCreateStateMachine(pTask);
14,259✔
442
  if (pTask->status.pSM == NULL || code != TSDB_CODE_SUCCESS) {
14,245!
443
    stError("s-task:%s failed create state-machine for stream task, initialization failed, code:%s", pTask->id.idStr,
×
444
            tstrerror(code));
445
    return code;
×
446
  }
447

448
  pTask->execInfo.created = taosGetTimestampMs();
14,255✔
449
  setInitialVersionInfo(pTask, ver);
14,255✔
450

451
  pTask->pMeta = pMeta;
14,253✔
452
  pTask->pMsgCb = pMsgCb;
14,253✔
453
  pTask->msgInfo.pSendInfo = taosArrayInit(4, sizeof(SDispatchEntry));
14,253✔
454
  if (pTask->msgInfo.pSendInfo == NULL) {
14,251!
455
    stError("s-task:%s failed to create sendInfo struct for stream task, code:Out of memory", pTask->id.idStr);
×
456
    return terrno;
×
457
  }
458

459
  code = taosThreadMutexInit(&pTask->msgInfo.lock, NULL);
14,251✔
460
  if (code) {
14,247!
461
    stError("s-task:0x%x failed to init msgInfo mutex, code:%s", pTask->id.taskId, tstrerror(code));
×
462
    return code;
×
463
  }
464

465
  TdThreadMutexAttr attr = {0};
14,247✔
466
  code = taosThreadMutexAttrInit(&attr);
14,247✔
467
  if (code != 0) {
14,241!
468
    stError("s-task:%s initElapsed mutex attr failed, code:%s", pTask->id.idStr, tstrerror(code));
×
469
    return code;
×
470
  }
471

472
  code = taosThreadMutexAttrSetType(&attr, PTHREAD_MUTEX_RECURSIVE);
14,241✔
473
  if (code != 0) {
14,236!
474
    stError("s-task:%s set mutex attr recursive, code:%s", pTask->id.idStr, tstrerror(code));
×
475
    return code;
×
476
  }
477

478
  code = taosThreadMutexInit(&pTask->lock, &attr);
14,236✔
479
  if (code) {
14,242!
480
    return code;
×
481
  }
482

483
  code = taosThreadMutexAttrDestroy(&attr);
14,242✔
484
  if (code) {
14,245!
485
    return code;
×
486
  }
487

488
  streamTaskOpenAllUpstreamInput(pTask);
14,245✔
489

490
  STaskOutputInfo* pOutputInfo = &pTask->outputInfo;
14,237✔
491
  pOutputInfo->pTokenBucket = taosMemoryCalloc(1, sizeof(STokenBucket));
14,237✔
492
  if (pOutputInfo->pTokenBucket == NULL) {
14,248!
493
    stError("s-task:%s failed to prepare the tokenBucket, code:%s", pTask->id.idStr, tstrerror(terrno));
×
494
    return terrno;
×
495
  }
496

497
  // 2MiB per second for sink task
498
  // 50 times sink operator per second
499
  code = streamTaskInitTokenBucket(pOutputInfo->pTokenBucket, 35, 35, tsSinkDataRate, pTask->id.idStr);
14,248✔
500
  if (code) {
14,238!
501
    return code;
×
502
  }
503

504
  pOutputInfo->pNodeEpsetUpdateList = taosArrayInit(4, sizeof(SDownstreamTaskEpset));
14,238✔
505
  if (pOutputInfo->pNodeEpsetUpdateList == NULL) {
14,248!
506
    stError("s-task:%s failed to prepare downstreamUpdateList, code:%s", pTask->id.idStr, tstrerror(terrno));
×
507
    return terrno;
×
508
  }
509

510
  pTask->taskCheckInfo.pList = taosArrayInit(4, sizeof(SDownstreamStatusInfo));
14,248✔
511
  if (pTask->taskCheckInfo.pList == NULL) {
14,252!
512
    stError("s-task:%s failed to prepare taskCheckInfo list, code:%s", pTask->id.idStr, tstrerror(terrno));
×
513
    return terrno;
×
514
  }
515

516
  if (pTask->chkInfo.pActiveInfo == NULL) {
14,252✔
517
    code = streamTaskCreateActiveChkptInfo(&pTask->chkInfo.pActiveInfo);
14,251✔
518
    if (code) {
14,253!
519
      stError("s-task:%s failed to create active checkpoint info, code:%s", pTask->id.idStr, tstrerror(code));
×
520
      return code;
×
521
    }
522
  }
523

524
  return streamTaskSetBackendPath(pTask);
14,254✔
525
}
526

527
int32_t streamTaskGetNumOfDownstream(const SStreamTask* pTask) {
124,596✔
528
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
124,596✔
529
    return 0;
6,574✔
530
  }
531

532
  int32_t type = pTask->outputInfo.type;
118,022✔
533
  if (type == TASK_OUTPUT__TABLE) {
118,022✔
534
    return 0;
184✔
535
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
117,838✔
536
    return 1;
13,051✔
537
  } else {
538
    SArray* vgInfo = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
104,787✔
539
    return taosArrayGetSize(vgInfo);
104,787✔
540
  }
541
}
542

543
int32_t streamTaskGetNumOfUpstream(const SStreamTask* pTask) { return taosArrayGetSize(pTask->upstreamInfo.pList); }
20,549✔
544

545
int32_t streamTaskSetUpstreamInfo(SStreamTask* pTask, const SStreamTask* pUpstreamTask) {
19,337✔
546
  SStreamUpstreamEpInfo* pEpInfo = createStreamTaskEpInfo(pUpstreamTask);
19,337✔
547
  if (pEpInfo == NULL) {
19,337!
548
    return terrno;
×
549
  }
550

551
  if (pTask->upstreamInfo.pList == NULL) {
19,337✔
552
    pTask->upstreamInfo.pList = taosArrayInit(4, POINTER_BYTES);
6,722✔
553
  }
554

555
  void* p = taosArrayPush(pTask->upstreamInfo.pList, &pEpInfo);
19,337✔
556
  return (p == NULL) ? terrno : TSDB_CODE_SUCCESS;
19,337!
557
}
558

559
int32_t streamTaskUpdateUpstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
177✔
560
  int32_t code = 0;
177✔
561
  char    buf[512] = {0};
177✔
562
  code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore error since it is only for log file.
177✔
563
  if (code != 0) {  // print error and continue
177!
564
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
565
    return code;
×
566
  }
567

568
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
177✔
569
  for (int32_t i = 0; i < numOfUpstream; ++i) {
352✔
570
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
350✔
571
    if (pInfo->nodeId == nodeId) {
350✔
572
      bool equal = isEpsetEqual(&pInfo->epSet, pEpSet);
175✔
573
      if (!equal) {
175✔
574
        *pUpdated = true;
112✔
575

576
        char tmp[512] = {0};
112✔
577
        code = epsetToStr(&pInfo->epSet, tmp, tListLen(tmp));
112✔
578
        if (code != 0) {  // print error and continue
112!
579
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
580
          return code;
×
581
        }
582

583
        epsetAssign(&pInfo->epSet, pEpSet);
112✔
584
        stDebug("s-task:0x%x update the upstreamInfo taskId:0x%x(nodeId:%d) newEpset:%s old:%s", pTask->id.taskId,
112!
585
                pInfo->taskId, nodeId, buf, tmp);
586
      } else {
587
        stDebug("s-task:0x%x not update upstreamInfo, since identical, task:0x%x(nodeId:%d) epset:%s", pTask->id.taskId,
63!
588
                pInfo->taskId, nodeId, buf);
589
      }
590

591
      break;
175✔
592
    }
593
  }
594

595
  return code;
177✔
596
}
597

598
void streamTaskDestroyUpstreamInfo(SUpstreamInfo* pUpstreamInfo) {
59,215✔
599
  if (pUpstreamInfo->pList != NULL) {
59,215✔
600
    taosArrayDestroyEx(pUpstreamInfo->pList, freeUpstreamItem);
52,435✔
601
    pUpstreamInfo->numOfClosed = 0;
52,441✔
602
    pUpstreamInfo->pList = NULL;
52,441✔
603
  }
604
}
59,221✔
605

606
void streamTaskSetFixedDownstreamInfo(SStreamTask* pTask, const SStreamTask* pDownstreamTask) {
967✔
607
  STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
967✔
608
  pDispatcher->taskId = pDownstreamTask->id.taskId;
967✔
609
  pDispatcher->nodeId = pDownstreamTask->info.nodeId;
967✔
610
  pDispatcher->epSet = pDownstreamTask->info.epSet;
967✔
611

612
  pTask->outputInfo.type = TASK_OUTPUT__FIXED_DISPATCH;
967✔
613
  pTask->msgInfo.msgType = TDMT_STREAM_TASK_DISPATCH;
967✔
614
}
967✔
615

616
int32_t streamTaskUpdateDownstreamInfo(SStreamTask* pTask, int32_t nodeId, const SEpSet* pEpSet, bool* pUpdated) {
177✔
617
  char    buf[512] = {0};
177✔
618
  int32_t code = epsetToStr(pEpSet, buf, tListLen(buf));  // ignore the error since only for log files.
177✔
619
  if (code != 0) {                                        // print error and continue
177!
620
    stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
621
    return code;
×
622
  }
623

624
  int32_t id = pTask->id.taskId;
177✔
625
  int8_t  type = pTask->outputInfo.type;
177✔
626

627
  if (type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
177✔
628
    SArray* pVgs = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
173✔
629

630
    for (int32_t i = 0; i < taosArrayGetSize(pVgs); i++) {
348✔
631
      SVgroupInfo* pVgInfo = taosArrayGet(pVgs, i);
346✔
632
      if (pVgInfo == NULL) {
346!
633
        continue;
×
634
      }
635

636
      if (pVgInfo->vgId == nodeId) {
346✔
637
        bool isEqual = isEpsetEqual(&pVgInfo->epSet, pEpSet);
171✔
638
        if (!isEqual) {
171✔
639
          *pUpdated = true;
112✔
640

641
          char tmp[512] = {0};
112✔
642
          code = epsetToStr(&pVgInfo->epSet, tmp, tListLen(tmp));
112✔
643
          if (code != 0) {  // print error and continue
112!
644
            stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
645
            return code;
×
646
          }
647

648
          epsetAssign(&pVgInfo->epSet, pEpSet);
112✔
649
          stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pVgInfo->taskId,
112!
650
                  nodeId, buf, tmp);
651
        } else {
652
          stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
59!
653
                  pVgInfo->taskId, nodeId, buf);
654
        }
655
        break;
171✔
656
      }
657
    }
658
  } else if (type == TASK_OUTPUT__FIXED_DISPATCH) {
4!
659
    STaskDispatcherFixed* pDispatcher = &pTask->outputInfo.fixedDispatcher;
4✔
660
    if (pDispatcher->nodeId == nodeId) {
4!
661
      bool equal = isEpsetEqual(&pDispatcher->epSet, pEpSet);
4✔
662
      if (!equal) {
4!
663
        *pUpdated = true;
×
664

665
        char tmp[512] = {0};
×
666
        code = epsetToStr(&pDispatcher->epSet, tmp, tListLen(tmp));
×
667
        if (code != 0) {  // print error and continue
×
668
          stError("%s failed to convert epset to str, code:%s", pTask->id.idStr, tstrerror(code));
×
669
          return code;
×
670
        }
671

672
        epsetAssign(&pDispatcher->epSet, pEpSet);
×
673
        stDebug("s-task:0x%x update dispatch info, task:0x%x(nodeId:%d) newEpset:%s old:%s", id, pDispatcher->taskId,
×
674
                nodeId, buf, tmp);
675
      } else {
676
        stDebug("s-task:0x%x not update dispatch info, since identical, task:0x%x(nodeId:%d) epset:%s", id,
4!
677
                pDispatcher->taskId, nodeId, buf);
678
      }
679
    }
680
  }
681

682
  return code;
177✔
683
}
684

685
int32_t streamTaskStop(SStreamTask* pTask) {
2,534✔
686
  int32_t     vgId = pTask->pMeta->vgId;
2,534✔
687
  int64_t     st = taosGetTimestampMs();
2,534✔
688
  const char* id = pTask->id.idStr;
2,534✔
689

690
  int32_t code = streamTaskHandleEvent(pTask->status.pSM, TASK_EVENT_STOP);
2,534✔
691
  if (code) {
2,534!
692
    stError("failed to handle STOP event, s-task:%s, code:%s", id, tstrerror(code));
×
693
    return code;
×
694
  }
695

696
  if (pTask->info.taskLevel != TASK_LEVEL__SINK && pTask->exec.pExecutor != NULL) {
2,534✔
697
    code = qKillTask(pTask->exec.pExecutor, TSDB_CODE_SUCCESS);
1,274✔
698
    if (code != TSDB_CODE_SUCCESS) {
1,274!
699
      stError("s-task:%s failed to kill task related query handle, code:%s", id, tstrerror(code));
×
700
    }
701
  }
702

703
  while (!streamTaskIsIdle(pTask)) {
2,533!
704
    stDebug("s-task:%s level:%d wait for task to be idle and then close, check again in 100ms", id,
×
705
            pTask->info.taskLevel);
706
    taosMsleep(100);
×
707
  }
708

709
  int64_t el = taosGetTimestampMs() - st;
2,534✔
710
  stDebug("vgId:%d s-task:%s is closed in %" PRId64 " ms", vgId, id, el);
2,534✔
711
  return code;
2,534✔
712
}
713

714
bool streamTaskUpdateEpsetInfo(SStreamTask* pTask, SArray* pNodeList) {
194✔
715
  STaskExecStatisInfo* p = &pTask->execInfo;
194✔
716

717
  int32_t numOfNodes = taosArrayGetSize(pNodeList);
194✔
718
  int64_t prevTs = p->latestUpdateTs;
194✔
719

720
  p->latestUpdateTs = taosGetTimestampMs();
194✔
721
  p->updateCount += 1;
194✔
722
  stDebug("s-task:0x%x update task nodeEp epset, updatedNodes:%d, updateCount:%d, prevTs:%" PRId64, pTask->id.taskId,
194!
723
          numOfNodes, p->updateCount, prevTs);
724

725
  bool updated = false;
194✔
726
  for (int32_t i = 0; i < numOfNodes; ++i) {
546✔
727
    SNodeUpdateInfo* pInfo = taosArrayGet(pNodeList, i);
352✔
728
    if (pInfo == NULL) {
352!
729
      continue;
×
730
    }
731

732
    int32_t code = doUpdateTaskEpset(pTask, pInfo->nodeId, &pInfo->newEp, &updated);
352✔
733
    if (code) {
352!
734
      stError("s-task:0x%x failed to update the task nodeEp epset, code:%s", pTask->id.taskId, tstrerror(code));
×
735
    }
736
  }
737

738
  return updated;
194✔
739
}
740

741
void streamTaskResetUpstreamStageInfo(SStreamTask* pTask) {
14,253✔
742
  if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
14,253✔
743
    return;
7,131✔
744
  }
745

746
  int32_t size = taosArrayGetSize(pTask->upstreamInfo.pList);
7,122✔
747
  for (int32_t i = 0; i < size; ++i) {
27,478✔
748
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
20,357✔
749
    pInfo->stage = -1;
20,357✔
750
  }
751

752
  stDebug("s-task:%s reset all upstream tasks stage info", pTask->id.idStr);
7,121✔
753
}
754

755
void streamTaskOpenAllUpstreamInput(SStreamTask* pTask) {
24,388✔
756
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
24,388✔
757
  if (num == 0) {
24,403✔
758
    return;
12,171✔
759
  }
760

761
  for (int32_t i = 0; i < num; ++i) {
47,064✔
762
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
34,833✔
763
    pInfo->dataAllowed = true;
34,832✔
764
  }
765

766
  pTask->upstreamInfo.numOfClosed = 0;
12,231✔
767
  stDebug("s-task:%s opening up inputQ for %d upstream tasks", pTask->id.idStr, num);
12,231✔
768
}
769

770
void streamTaskCloseUpstreamInput(SStreamTask* pTask, int32_t taskId) {
8,666✔
771
  SStreamUpstreamEpInfo* pInfo = NULL;
8,666✔
772
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
8,666✔
773

774
  if ((pInfo != NULL) && pInfo->dataAllowed) {
8,662!
775
    pInfo->dataAllowed = false;
8,663✔
776
    if (pTask->upstreamInfo.numOfClosed < streamTaskGetNumOfUpstream(pTask)) {
8,663!
777
      int32_t t = atomic_add_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
8,667✔
778
    } else {
779
      stError("s-task:%s not inc closed input, since they have been all closed already", pTask->id.idStr);
×
780
    }
781
  }
782
}
8,695✔
783

784
void streamTaskOpenUpstreamInput(SStreamTask* pTask, int32_t taskId) {
×
785
  SStreamUpstreamEpInfo* pInfo = NULL;
×
786
  streamTaskGetUpstreamTaskEpInfo(pTask, taskId, &pInfo);
×
787

788
  if (pInfo != NULL && (!pInfo->dataAllowed)) {
×
789
    int32_t t = atomic_sub_fetch_32(&pTask->upstreamInfo.numOfClosed, 1);
×
790
    stDebug("s-task:%s open inputQ for upstream:0x%x, remain closed:%d", pTask->id.idStr, taskId, t);
×
791
    pInfo->dataAllowed = true;
×
792
  }
793
}
×
794

795
bool streamTaskIsAllUpstreamClosed(SStreamTask* pTask) {
×
796
  return pTask->upstreamInfo.numOfClosed == taosArrayGetSize(pTask->upstreamInfo.pList);
×
797
}
798

799
bool streamTaskSetSchedStatusWait(SStreamTask* pTask) {
119,222✔
800
  bool ret = false;
119,222✔
801

802
  streamMutexLock(&pTask->lock);
119,222✔
803
  if (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE) {
119,266✔
804
    pTask->status.schedStatus = TASK_SCHED_STATUS__WAITING;
80,792✔
805
    ret = true;
80,792✔
806
  }
807

808
  streamMutexUnlock(&pTask->lock);
119,266✔
809
  return ret;
119,261✔
810
}
811

812
int8_t streamTaskSetSchedStatusActive(SStreamTask* pTask) {
78,890✔
813
  streamMutexLock(&pTask->lock);
78,890✔
814
  int8_t status = pTask->status.schedStatus;
78,974✔
815
  if (status == TASK_SCHED_STATUS__WAITING) {
78,974✔
816
    pTask->status.schedStatus = TASK_SCHED_STATUS__ACTIVE;
78,957✔
817
  }
818
  streamMutexUnlock(&pTask->lock);
78,974✔
819

820
  return status;
78,989✔
821
}
822

823
int8_t streamTaskSetSchedStatusInactive(SStreamTask* pTask) {
1,749✔
824
  streamMutexLock(&pTask->lock);
1,749✔
825
  int8_t status = pTask->status.schedStatus;
1,749✔
826
  pTask->status.schedStatus = TASK_SCHED_STATUS__INACTIVE;
1,749✔
827
  streamMutexUnlock(&pTask->lock);
1,749✔
828

829
  return status;
1,749✔
830
}
831

832
int32_t streamTaskClearHTaskAttr(SStreamTask* pTask, int32_t resetRelHalt) {
6,968✔
833
  int32_t      code = 0;
6,968✔
834
  SStreamMeta* pMeta = pTask->pMeta;
6,968✔
835
  SStreamTask* pStreamTask = NULL;
6,968✔
836

837
  if (pTask->info.fillHistory == 0) {
6,968!
838
    return code;
6,977✔
839
  }
840

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

846
    streamMutexLock(&(pStreamTask->lock));
×
847
    CLEAR_RELATED_FILLHISTORY_TASK(pStreamTask);
×
848

849
    if (resetRelHalt) {
×
850
      stDebug("s-task:0x%" PRIx64 " set the persistent status attr to be ready, prev:%s, status in sm:%s",
×
851
              pTask->streamTaskId.taskId, streamTaskGetStatusStr(pStreamTask->status.taskStatus),
852
              streamTaskGetStatus(pStreamTask).name);
853
      pStreamTask->status.taskStatus = TASK_STATUS__READY;
×
854
    }
855

856
    code = streamMetaSaveTask(pMeta, pStreamTask);
×
857
    streamMutexUnlock(&(pStreamTask->lock));
×
858

859
    streamMetaReleaseTask(pMeta, pStreamTask);
×
860
  }
861

862
  return code;
4✔
863
}
864

865
int32_t streamBuildAndSendDropTaskMsg(SMsgCb* pMsgCb, int32_t vgId, SStreamTaskId* pTaskId, int64_t resetRelHalt) {
4✔
866
  SVDropStreamTaskReq* pReq = rpcMallocCont(sizeof(SVDropStreamTaskReq));
4✔
867
  if (pReq == NULL) {
4!
868
    return terrno;
×
869
  }
870

871
  pReq->head.vgId = vgId;
4✔
872
  pReq->taskId = pTaskId->taskId;
4✔
873
  pReq->streamId = pTaskId->streamId;
4✔
874
  pReq->resetRelHalt = resetRelHalt;
4✔
875

876
  SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_DROP, .pCont = pReq, .contLen = sizeof(SVDropStreamTaskReq)};
4✔
877
  int32_t code = tmsgPutToQueue(pMsgCb, WRITE_QUEUE, &msg);
4✔
878
  if (code != TSDB_CODE_SUCCESS) {
4!
879
    stError("vgId:%d failed to send drop task:0x%x msg, code:%s", vgId, pTaskId->taskId, tstrerror(code));
×
880
  } else {
881
    stDebug("vgId:%d build and send drop task:0x%x msg", vgId, pTaskId->taskId);
4!
882
  }
883

884
  return code;
4✔
885
}
886

887
int32_t streamSendChkptReportMsg(SStreamTask* pTask, SCheckpointInfo* pCheckpointInfo, int8_t dropRelHTask) {
6,504✔
888
  int32_t                code = 0;
6,504✔
889
  int32_t                tlen = 0;
6,504✔
890
  int32_t                vgId = pTask->pMeta->vgId;
6,504✔
891
  const char*            id = pTask->id.idStr;
6,504✔
892
  SActiveCheckpointInfo* pActive = pCheckpointInfo->pActiveInfo;
6,504✔
893

894
  SCheckpointReport req = {.streamId = pTask->id.streamId,
6,504✔
895
                           .taskId = pTask->id.taskId,
6,504✔
896
                           .nodeId = vgId,
897
                           .dropHTask = dropRelHTask,
898
                           .transId = pActive->transId,
6,504✔
899
                           .checkpointId = pActive->activeId,
6,504✔
900
                           .checkpointVer = pCheckpointInfo->processedVer,
6,504✔
901
                           .checkpointTs = pCheckpointInfo->startTs};
6,504✔
902

903
  tEncodeSize(tEncodeStreamTaskChkptReport, &req, tlen, code);
6,504!
904
  if (code < 0) {
6,501!
905
    stError("s-task:%s vgId:%d encode stream task checkpoint-report failed, code:%s", id, vgId, tstrerror(code));
×
906
    return -1;
×
907
  }
908

909
  void* buf = rpcMallocCont(tlen);
6,501✔
910
  if (buf == NULL) {
6,504!
911
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId,
×
912
            tstrerror(TSDB_CODE_OUT_OF_MEMORY));
913
    return -1;
×
914
  }
915

916
  SEncoder encoder;
917
  tEncoderInit(&encoder, buf, tlen);
6,504✔
918
  if ((code = tEncodeStreamTaskChkptReport(&encoder, &req)) < 0) {
6,503!
919
    rpcFreeCont(buf);
×
920
    tEncoderClear(&encoder);
×
921
    stError("s-task:%s vgId:%d encode stream task checkpoint-report msg failed, code:%s", id, vgId, tstrerror(code));
×
922
    return -1;
×
923
  }
924
  tEncoderClear(&encoder);
6,503✔
925

926
  SRpcMsg msg = {0};
6,505✔
927
  initRpcMsg(&msg, TDMT_MND_STREAM_CHKPT_REPORT, buf, tlen);
6,505✔
928
  stDebug("s-task:%s vgId:%d build and send task checkpoint-report to mnode", id, vgId);
6,504✔
929

930
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
6,504✔
931
}
932

933
STaskId streamTaskGetTaskId(const SStreamTask* pTask) {
70,457✔
934
  STaskId id = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId};
70,457✔
935
  return id;
70,457✔
936
}
937

938
void streamTaskInitForLaunchHTask(SHistoryTaskInfo* pInfo) {
1,893✔
939
  pInfo->waitInterval = LAUNCH_HTASK_INTERVAL;
1,893✔
940
  pInfo->tickCount = ceil(LAUNCH_HTASK_INTERVAL / WAIT_FOR_MINIMAL_INTERVAL);
1,893✔
941
  pInfo->retryTimes = 0;
1,893✔
942
}
1,893✔
943

944
void streamTaskSetRetryInfoForLaunch(SHistoryTaskInfo* pInfo) {
1,888✔
945
  pInfo->waitInterval *= RETRY_LAUNCH_INTERVAL_INC_RATE;
1,888✔
946
  pInfo->tickCount = ceil(pInfo->waitInterval / WAIT_FOR_MINIMAL_INTERVAL);
1,888✔
947
  pInfo->retryTimes += 1;
1,888✔
948
}
1,888✔
949

950
void streamTaskStatusInit(STaskStatusEntry* pEntry, const SStreamTask* pTask) {
8,654✔
951
  pEntry->id.streamId = pTask->id.streamId;
8,654✔
952
  pEntry->id.taskId = pTask->id.taskId;
8,654✔
953
  pEntry->stage = -1;
8,654✔
954
  pEntry->nodeId = pTask->info.nodeId;
8,654✔
955
  pEntry->status = TASK_STATUS__STOP;
8,654✔
956
}
8,654✔
957

958
void streamTaskStatusCopy(STaskStatusEntry* pDst, const STaskStatusEntry* pSrc) {
54,257✔
959
  pDst->stage = pSrc->stage;
54,257✔
960
  pDst->inputQUsed = pSrc->inputQUsed;
54,257✔
961
  pDst->inputRate = pSrc->inputRate;
54,257✔
962
  pDst->procsTotal = pSrc->procsTotal;
54,257✔
963
  pDst->procsThroughput = pSrc->procsThroughput;
54,257✔
964
  pDst->outputTotal = pSrc->outputTotal;
54,257✔
965
  pDst->outputThroughput = pSrc->outputThroughput;
54,257✔
966
  pDst->processedVer = pSrc->processedVer;
54,257✔
967
  pDst->verRange = pSrc->verRange;
54,257✔
968
  pDst->sinkQuota = pSrc->sinkQuota;
54,257✔
969
  pDst->sinkDataSize = pSrc->sinkDataSize;
54,257✔
970
  pDst->checkpointInfo = pSrc->checkpointInfo;
54,257✔
971
  pDst->startCheckpointId = pSrc->startCheckpointId;
54,257✔
972
  pDst->startCheckpointVer = pSrc->startCheckpointVer;
54,257✔
973
  pDst->status = pSrc->status;
54,257✔
974

975
  pDst->startTime = pSrc->startTime;
54,257✔
976
  pDst->hTaskId = pSrc->hTaskId;
54,257✔
977
}
54,257✔
978

979
STaskStatusEntry streamTaskGetStatusEntry(SStreamTask* pTask) {
54,807✔
980
  SStreamMeta*         pMeta = pTask->pMeta;
54,807✔
981
  STaskExecStatisInfo* pExecInfo = &pTask->execInfo;
54,807✔
982

983
  STaskStatusEntry entry = {
164,421✔
984
      .id = streamTaskGetTaskId(pTask),
54,807✔
985
      .status = streamTaskGetStatus(pTask).state,
54,807✔
986
      .nodeId = pMeta->vgId,
54,807✔
987
      .stage = pMeta->stage,
54,807✔
988

989
      .inputQUsed = SIZE_IN_MiB(streamQueueGetItemSize(pTask->inputq.queue)),
54,807✔
990
      .startTime = pExecInfo->readyTs,
54,807✔
991
      .checkpointInfo.latestId = pTask->chkInfo.checkpointId,
54,807✔
992
      .checkpointInfo.latestVer = pTask->chkInfo.checkpointVer,
54,807✔
993
      .checkpointInfo.latestTime = pTask->chkInfo.checkpointTime,
54,807✔
994
      .checkpointInfo.latestSize = 0,
995
      .checkpointInfo.remoteBackup = 0,
996
      .checkpointInfo.consensusChkptId = 0,
997
      .checkpointInfo.consensusTs = 0,
998
      .hTaskId = pTask->hTaskInfo.id.taskId,
54,807✔
999
      .procsTotal = SIZE_IN_MiB(pExecInfo->inputDataSize),
54,807✔
1000
      .outputTotal = SIZE_IN_MiB(pExecInfo->outputDataSize),
54,807✔
1001
      .procsThroughput = SIZE_IN_KiB(pExecInfo->procsThroughput),
54,807✔
1002
      .outputThroughput = SIZE_IN_KiB(pExecInfo->outputThroughput),
54,807✔
1003
      .startCheckpointId = pExecInfo->startCheckpointId,
54,807✔
1004
      .startCheckpointVer = pExecInfo->startCheckpointVer,
54,807✔
1005
  };
1006
  return entry;
54,807✔
1007
}
1008

1009
static int32_t taskPauseCallback(SStreamTask* pTask, void* param) {
1,313✔
1010
  SStreamMeta* pMeta = pTask->pMeta;
1,313✔
1011
  int32_t      code = 0;
1,313✔
1012

1013
  int32_t num = atomic_add_fetch_32(&pMeta->numOfPausedTasks, 1);
1,313✔
1014
  stInfo("vgId:%d s-task:%s pause stream task. paused task num:%d", pMeta->vgId, pTask->id.idStr, num);
1,312!
1015

1016
  // in case of fill-history task, stop the tsdb file scan operation.
1017
  if (pTask->info.fillHistory == 1) {
1,315✔
1018
    void* pExecutor = pTask->exec.pExecutor;
84✔
1019
    code = qKillTask(pExecutor, TSDB_CODE_SUCCESS);
84✔
1020
  }
1021

1022
  stDebug("vgId:%d s-task:%s set pause flag and pause task", pMeta->vgId, pTask->id.idStr);
1,315✔
1023
  return code;
1,315✔
1024
}
1025

1026
void streamTaskPause(SStreamTask* pTask) {
1,384✔
1027
  int32_t code = streamTaskHandleEventAsync(pTask->status.pSM, TASK_EVENT_PAUSE, taskPauseCallback, NULL);
1,384✔
1028
  if (code) {
1,385!
1029
    stError("s-task:%s failed handle pause event async, code:%s", pTask->id.idStr, tstrerror(code));
×
1030
  }
1031
}
1,385✔
1032

1033
void streamTaskResume(SStreamTask* pTask) {
1,367✔
1034
  SStreamTaskState prevState = streamTaskGetStatus(pTask);
1,367✔
1035

1036
  SStreamMeta* pMeta = pTask->pMeta;
1,367✔
1037
  int32_t      code = streamTaskRestoreStatus(pTask);
1,367✔
1038
  if (code == TSDB_CODE_SUCCESS) {
1,366✔
1039
    char*   pNew = streamTaskGetStatus(pTask).name;
1,294✔
1040
    int32_t num = atomic_sub_fetch_32(&pMeta->numOfPausedTasks, 1);
1,294✔
1041
    stInfo("s-task:%s status:%s resume from %s, paused task(s):%d", pTask->id.idStr, pNew, prevState.name, num);
1,295!
1042
  } else {
1043
    stInfo("s-task:%s status:%s no need to resume, paused task(s):%d", pTask->id.idStr, prevState.name,
72!
1044
           pMeta->numOfPausedTasks);
1045
  }
1046
}
1,368✔
1047

1048
bool streamTaskIsSinkTask(const SStreamTask* pTask) { return pTask->info.taskLevel == TASK_LEVEL__SINK; }
94,504✔
1049

1050
// this task must success
1051
int32_t streamTaskSendCheckpointReq(SStreamTask* pTask) {
4,721✔
1052
  int32_t     code;
1053
  int32_t     tlen = 0;
4,721✔
1054
  int32_t     vgId = pTask->pMeta->vgId;
4,721✔
1055
  const char* id = pTask->id.idStr;
4,721✔
1056

1057
  SStreamTaskCheckpointReq req = {.streamId = pTask->id.streamId, .taskId = pTask->id.taskId, .nodeId = vgId};
4,721✔
1058
  tEncodeSize(tEncodeStreamTaskCheckpointReq, &req, tlen, code);
4,721!
1059
  if (code < 0) {
4,722!
1060
    stError("s-task:%s vgId:%d encode stream task req checkpoint failed, code:%s", id, vgId, tstrerror(code));
×
1061
    return TSDB_CODE_INVALID_MSG;
×
1062
  }
1063

1064
  void* buf = rpcMallocCont(tlen);
4,722✔
1065
  if (buf == NULL) {
4,730!
1066
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:Out of memory", id, vgId);
×
1067
    return terrno;
×
1068
  }
1069

1070
  SEncoder encoder;
1071
  tEncoderInit(&encoder, buf, tlen);
4,730✔
1072
  if ((code = tEncodeStreamTaskCheckpointReq(&encoder, &req)) < 0) {
4,729!
1073
    rpcFreeCont(buf);
×
1074
    tEncoderClear(&encoder);
×
1075
    stError("s-task:%s vgId:%d encode stream task req checkpoint msg failed, code:%s", id, vgId, tstrerror(code));
×
1076
    return code;
×
1077
  }
1078

1079
  tEncoderClear(&encoder);
4,726✔
1080

1081
  SRpcMsg msg = {0};
4,726✔
1082
  initRpcMsg(&msg, TDMT_MND_STREAM_REQ_CHKPT, buf, tlen);
4,726✔
1083
  stDebug("s-task:%s vgId:%d build and send task checkpoint req", id, vgId);
4,725✔
1084

1085
  return tmsgSendReq(&pTask->info.mnodeEpset, &msg);
4,725✔
1086
}
1087

1088
void streamTaskGetUpstreamTaskEpInfo(SStreamTask* pTask, int32_t taskId, SStreamUpstreamEpInfo** pEpInfo) {
97,260✔
1089
  *pEpInfo = NULL;
97,260✔
1090

1091
  int32_t num = taosArrayGetSize(pTask->upstreamInfo.pList);
97,260✔
1092
  for (int32_t i = 0; i < num; ++i) {
192,092✔
1093
    SStreamUpstreamEpInfo* pInfo = taosArrayGetP(pTask->upstreamInfo.pList, i);
192,080✔
1094
    if (pInfo == NULL) {
192,007!
1095
      return;
×
1096
    }
1097

1098
    if (pInfo->taskId == taskId) {
192,007✔
1099
      *pEpInfo = pInfo;
97,246✔
1100
      return;
97,246✔
1101
    }
1102
  }
1103

1104
  stError("s-task:%s failed to find upstream task:0x%x", pTask->id.idStr, taskId);
12!
1105
}
1106

1107
SEpSet* streamTaskGetDownstreamEpInfo(SStreamTask* pTask, int32_t taskId) {
×
1108
  if (pTask->info.taskLevel == TASK_OUTPUT__FIXED_DISPATCH) {
×
1109
    if (pTask->outputInfo.fixedDispatcher.taskId == taskId) {
×
1110
      return &pTask->outputInfo.fixedDispatcher.epSet;
×
1111
    }
1112
  } else if (pTask->info.taskLevel == TASK_OUTPUT__SHUFFLE_DISPATCH) {
×
1113
    SArray* pList = pTask->outputInfo.shuffleDispatcher.dbInfo.pVgroupInfos;
×
1114
    for (int32_t i = 0; i < taosArrayGetSize(pList); ++i) {
×
1115
      SVgroupInfo* pVgInfo = taosArrayGet(pList, i);
×
1116
      if (pVgInfo == NULL) {
×
1117
        continue;
×
1118
      }
1119

1120
      if (pVgInfo->taskId == taskId) {
×
1121
        return &pVgInfo->epSet;
×
1122
      }
1123
    }
1124
  }
1125

1126
  return NULL;
×
1127
}
1128

1129
int32_t createStreamTaskIdStr(int64_t streamId, int32_t taskId, const char** pId) {
14,240✔
1130
  char buf[128] = {0};
14,240✔
1131
  sprintf(buf, "0x%" PRIx64 "-0x%x", streamId, taskId);
14,240✔
1132
  *pId = taosStrdup(buf);
14,240✔
1133

1134
  if (*pId == NULL) {
14,242!
1135
    return terrno;
×
1136
  } else {
1137
    return TSDB_CODE_SUCCESS;
14,244✔
1138
  }
1139
}
1140

1141
static int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
531✔
1142
  int32_t           code;
1143
  SStreamDataBlock* pData;
1144

1145
  code = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, sizeof(SStreamDataBlock), (void**)&pData);
531✔
1146
  if (code) {
531!
1147
    stError("s-task:%s failed to allocated retrieve-block", pTask->id.idStr);
×
1148
    return terrno = code;
×
1149
  }
1150

1151
  pData->type = STREAM_INPUT__DATA_RETRIEVE;
531✔
1152
  pData->srcVgId = 0;
531✔
1153

1154
  code = streamRetrieveReqToData(pReq, pData, pTask->id.idStr);
531✔
1155
  if (code != TSDB_CODE_SUCCESS) {
531!
1156
    stError("s-task:%s failed to convert retrieve-data to block, code:%s", pTask->id.idStr, tstrerror(code));
×
1157
    taosFreeQitem(pData);
×
1158
    return code;
×
1159
  }
1160

1161
  code = streamTaskPutDataIntoInputQ(pTask, (SStreamQueueItem*)pData);
531✔
1162
  if (code != TSDB_CODE_SUCCESS) {
531!
1163
    stError("s-task:%s failed to put retrieve-block into inputQ, inputQ is full, discard the retrieve msg",
×
1164
            pTask->id.idStr);
1165
  }
1166

1167
  return code;
531✔
1168
}
1169

1170
int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq) {
531✔
1171
  int32_t code = streamTaskEnqueueRetrieve(pTask, pReq);
531✔
1172
  if (code != 0) {
531!
1173
    return code;
×
1174
  }
1175
  return streamTrySchedExec(pTask);
531✔
1176
}
1177

1178
void streamTaskSetRemoveBackendFiles(SStreamTask* pTask) { pTask->status.removeBackendFiles = true; }
6,980✔
1179

1180
void streamTaskGetActiveCheckpointInfo(const SStreamTask* pTask, int32_t* pTransId, int64_t* pCheckpointId) {
×
1181
  if (pTransId != NULL) {
×
1182
    *pTransId = pTask->chkInfo.pActiveInfo->transId;
×
1183
  }
1184

1185
  if (pCheckpointId != NULL) {
×
1186
    *pCheckpointId = pTask->chkInfo.pActiveInfo->activeId;
×
1187
  }
1188
}
×
1189

1190
int32_t streamTaskSetActiveCheckpointInfo(SStreamTask* pTask, int64_t activeCheckpointId) {
28✔
1191
  pTask->chkInfo.pActiveInfo->activeId = activeCheckpointId;
28✔
1192
  return TSDB_CODE_SUCCESS;
28✔
1193
}
1194

1195
void streamTaskSetFailedChkptInfo(SStreamTask* pTask, int32_t transId, int64_t checkpointId) {
×
1196
  pTask->chkInfo.pActiveInfo->transId = transId;
×
1197
  pTask->chkInfo.pActiveInfo->activeId = checkpointId;
×
1198
  pTask->chkInfo.pActiveInfo->failedId = checkpointId;
×
1199
  stDebug("s-task:%s set failed checkpointId:%"PRId64, pTask->id.idStr, checkpointId);
×
1200
}
×
1201

1202
int32_t streamTaskCreateActiveChkptInfo(SActiveCheckpointInfo** pRes) {
14,282✔
1203
  SActiveCheckpointInfo* pInfo = taosMemoryCalloc(1, sizeof(SActiveCheckpointInfo));
14,282✔
1204
  if (pInfo == NULL) {
14,288!
1205
    return terrno;
×
1206
  }
1207

1208
  int32_t code = taosThreadMutexInit(&pInfo->lock, NULL);
14,288✔
1209
  if (code != TSDB_CODE_SUCCESS) {
14,287!
1210
    return code;
×
1211
  }
1212

1213
  pInfo->pDispatchTriggerList = taosArrayInit(4, sizeof(STaskTriggerSendInfo));
14,287✔
1214
  pInfo->pReadyMsgList = taosArrayInit(4, sizeof(STaskCheckpointReadyInfo));
14,288✔
1215
  pInfo->pCheckpointReadyRecvList = taosArrayInit(4, sizeof(STaskDownstreamReadyInfo));
14,286✔
1216

1217
  *pRes = pInfo;
14,287✔
1218
  return code;
14,287✔
1219
}
1220

1221
void streamTaskDestroyActiveChkptInfo(SActiveCheckpointInfo* pInfo) {
59,218✔
1222
  if (pInfo == NULL) {
59,218✔
1223
    return;
45,160✔
1224
  }
1225

1226
  streamMutexDestroy(&pInfo->lock);
14,058✔
1227
  taosArrayDestroy(pInfo->pDispatchTriggerList);
14,059✔
1228
  pInfo->pDispatchTriggerList = NULL;
14,061✔
1229
  taosArrayDestroy(pInfo->pReadyMsgList);
14,061✔
1230
  pInfo->pReadyMsgList = NULL;
14,060✔
1231
  taosArrayDestroy(pInfo->pCheckpointReadyRecvList);
14,060✔
1232
  pInfo->pCheckpointReadyRecvList = NULL;
14,061✔
1233

1234
  SStreamTmrInfo* pTriggerTmr = &pInfo->chkptTriggerMsgTmr;
14,061✔
1235
  if (pTriggerTmr->tmrHandle != NULL) {
14,061✔
1236
    streamTmrStop(pTriggerTmr->tmrHandle);
2,297✔
1237
    pTriggerTmr->tmrHandle = NULL;
2,296✔
1238
  }
1239

1240
  SStreamTmrInfo* pReadyTmr = &pInfo->chkptReadyMsgTmr;
14,060✔
1241
  if (pReadyTmr->tmrHandle != NULL) {
14,060✔
1242
    streamTmrStop(pReadyTmr->tmrHandle);
2,283✔
1243
    pReadyTmr->tmrHandle = NULL;
2,284✔
1244
  }
1245

1246
  taosMemoryFree(pInfo);
14,061✔
1247
}
1248

1249
//NOTE: clear the checkpoint id, and keep the failed id
1250
void streamTaskClearActiveInfo(SActiveCheckpointInfo* pInfo) {
5,358✔
1251
  pInfo->activeId = 0;
5,358✔
1252
  pInfo->transId = 0;
5,358✔
1253
  pInfo->allUpstreamTriggerRecv = 0;
5,358✔
1254
  pInfo->dispatchTrigger = false;
5,358✔
1255
//  pInfo->failedId = 0;
1256

1257
  taosArrayClear(pInfo->pDispatchTriggerList);
5,358✔
1258
  taosArrayClear(pInfo->pCheckpointReadyRecvList);
5,344✔
1259
}
5,350✔
1260

1261
const char* streamTaskGetExecType(int32_t type) {
126,374✔
1262
  switch (type) {
126,374!
1263
    case STREAM_EXEC_T_EXTRACT_WAL_DATA:
54,399✔
1264
      return "scan-wal-file";
54,399✔
1265
    case STREAM_EXEC_T_START_ALL_TASKS:
9,581✔
1266
      return "start-all-tasks";
9,581✔
1267
    case STREAM_EXEC_T_START_ONE_TASK:
5,153✔
1268
      return "start-one-task";
5,153✔
1269
    case STREAM_EXEC_T_RESTART_ALL_TASKS:
27✔
1270
      return "restart-all-tasks";
27✔
1271
    case STREAM_EXEC_T_STOP_ALL_TASKS:
4,861✔
1272
      return "stop-all-tasks";
4,861✔
1273
    case STREAM_EXEC_T_RESUME_TASK:
10,802✔
1274
      return "resume-task-from-idle";
10,802✔
1275
    case STREAM_EXEC_T_ADD_FAILED_TASK:
1✔
1276
      return "record-start-failed-task";
1✔
1277
    case 0:
41,613✔
1278
      return "exec-all-tasks";
41,613✔
1279
    default:
×
1280
      return "invalid-exec-type";
×
1281
  }
1282
}
1283

1284
int32_t streamTaskAllocRefId(SStreamTask* pTask, int64_t** pRefId) {
46,993✔
1285
  *pRefId = taosMemoryMalloc(sizeof(int64_t));
46,993✔
1286
  if (*pRefId != NULL) {
47,002!
1287
    **pRefId = pTask->id.refId;
47,004✔
1288
    int32_t code = metaRefMgtAdd(pTask->pMeta->vgId, *pRefId);
47,004✔
1289
    if (code != 0) {
47,021!
1290
      stError("s-task:%s failed to add refId:%" PRId64 " into refId-mgmt, code:%s", pTask->id.idStr, pTask->id.refId,
×
1291
              tstrerror(code));
1292
    }
1293
    return code;
47,021✔
1294
  } else {
1295
    stError("s-task:%s failed to alloc new ref id, code:%s", pTask->id.idStr, tstrerror(terrno));
×
1296
    return terrno;
×
1297
  }
1298
}
1299

1300
void streamTaskFreeRefId(int64_t* pRefId) {
43,403✔
1301
  if (pRefId == NULL) {
43,403✔
1302
    return;
2,162✔
1303
  }
1304

1305
  metaRefMgtRemove(pRefId);
41,241✔
1306
}
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