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

taosdata / TDengine / #3559

18 Dec 2024 12:59AM UTC coverage: 59.805% (+0.03%) from 59.778%
#3559

push

travis-ci

web-flow
Merge pull request #29187 from taosdata/merge/mainto3.0

merge: main to 3.0 branch

132705 of 287544 branches covered (46.15%)

Branch coverage included in aggregate %.

87 of 95 new or added lines in 19 files covered. (91.58%)

1132 existing lines in 133 files now uncovered.

209591 of 284807 relevant lines covered (73.59%)

8125235.78 hits per line

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

71.68
/source/libs/stream/src/streamExec.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 "streamInt.h"
17

18
// maximum allowed processed block batches. One block may include several submit blocks
19
#define MAX_STREAM_EXEC_BATCH_NUM         32
20
#define STREAM_RESULT_DUMP_THRESHOLD      300
21
#define STREAM_RESULT_DUMP_SIZE_THRESHOLD (1048576 * 1)  // 1MiB result data
22
#define STREAM_SCAN_HISTORY_TIMESLICE     1000           // 1000 ms
23
#define MIN_INVOKE_INTERVAL               50             // 50ms
24
#define FILL_HISTORY_TASK_EXEC_INTERVAL   5000           // 5 sec
25

26
static int32_t streamTransferStateDoPrepare(SStreamTask* pTask);
27
static int32_t streamTaskExecImpl(SStreamTask* pTask, SStreamQueueItem* pItem, int64_t* totalSize,
28
                                  int32_t* totalBlocks);
29

30
bool streamTaskShouldStop(const SStreamTask* pTask) {
1,714,052✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
1,714,052✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
1,713,828✔
33
}
34

35
bool streamTaskShouldPause(const SStreamTask* pTask) {
658,241✔
36
  return (streamTaskGetStatus(pTask).state == TASK_STATUS__PAUSE);
658,241✔
37
}
38

39
static int32_t doOutputResultBlockImpl(SStreamTask* pTask, SStreamDataBlock* pBlock) {
41,331✔
40
  int32_t code = 0;
41,331✔
41
  int32_t type = pTask->outputInfo.type;
41,331✔
42
  if (type == TASK_OUTPUT__TABLE) {
41,331✔
43
    pTask->outputInfo.tbSink.tbSinkFunc(pTask, pTask->outputInfo.tbSink.vnode, pBlock->blocks);
17,387✔
44
    destroyStreamDataBlock(pBlock);
17,389✔
45
  } else if (type == TASK_OUTPUT__SMA) {
23,944✔
46
    pTask->outputInfo.smaSink.smaSink(pTask->outputInfo.smaSink.vnode, pTask->outputInfo.smaSink.smaId, pBlock->blocks);
4✔
47
    destroyStreamDataBlock(pBlock);
4✔
48
  } else {
49
    if (type != TASK_OUTPUT__FIXED_DISPATCH && type != TASK_OUTPUT__SHUFFLE_DISPATCH) {
23,940!
50
      stError("s-task:%s invalid stream output type:%d, internal error", pTask->id.idStr, type);
×
51
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
52
    }
53

54
    code = streamTaskPutDataIntoOutputQ(pTask, pBlock);
23,940✔
55
    if (code != TSDB_CODE_SUCCESS) {
23,940!
56
      destroyStreamDataBlock(pBlock);
×
57
      return code;
×
58
    }
59

60
    // not handle error, if dispatch failed, try next time.
61
    // checkpoint trigger will be checked
62
    code = streamDispatchStreamBlock(pTask);
23,940✔
63
  }
64

65
  return code;
41,336✔
66
}
67

68
static int32_t doDumpResult(SStreamTask* pTask, SStreamQueueItem* pItem, SArray* pRes, int32_t size, int64_t* totalSize,
22,696✔
69
                            int32_t* totalBlocks) {
70
  int32_t numOfBlocks = taosArrayGetSize(pRes);
22,696✔
71
  if (numOfBlocks == 0) {
22,696!
72
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
73
    return TSDB_CODE_SUCCESS;
×
74
  }
75

76
  SStreamDataBlock* pStreamBlocks = NULL;
22,696✔
77

78
  int32_t code = createStreamBlockFromResults(pItem, pTask, size, pRes, &pStreamBlocks);
22,696✔
79
  if (code) {
22,696!
80
    stError("s-task:%s failed to create result stream data block, code:%s", pTask->id.idStr, tstrerror(terrno));
×
81
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
82
    return TSDB_CODE_OUT_OF_MEMORY;
×
83
  }
84

85
  stDebug("s-task:%s dump stream result data blocks, num:%d, size:%.2fMiB", pTask->id.idStr, numOfBlocks,
22,696✔
86
          SIZE_IN_MiB(size));
87

88
  code = doOutputResultBlockImpl(pTask, pStreamBlocks);
22,696✔
89
  if (code != TSDB_CODE_SUCCESS) {  // back pressure and record position
22,696!
90
    return code;
×
91
  }
92

93
  *totalSize += size;
22,696✔
94
  *totalBlocks += numOfBlocks;
22,696✔
95

96
  return code;
22,696✔
97
}
98

99
static int32_t doAppendPullOverBlock(SStreamTask* pTask, int32_t* pNumOfBlocks, SStreamDataBlock* pRetrieveBlock,
533✔
100
                                     SArray* pRes) {
101
  SSDataBlock block = {0};
533✔
102
  int32_t     num = taosArrayGetSize(pRetrieveBlock->blocks);
533✔
103
  if (num != 1) {
533!
104
    stError("s-task:%s invalid retrieve block number:%d, ignore", pTask->id.idStr, num);
×
105
    return TSDB_CODE_INVALID_PARA;
×
106
  }
107

108
  void*   p = taosArrayGet(pRetrieveBlock->blocks, 0);
533✔
109
  int32_t code = assignOneDataBlock(&block, p);
533✔
110
  if (code) {
533!
111
    stError("s-task:%s failed to assign retrieve block, code:%s", pTask->id.idStr, tstrerror(code));
×
112
    return code;
×
113
  }
114

115
  block.info.type = STREAM_PULL_OVER;
533✔
116
  block.info.childId = pTask->info.selfChildId;
533✔
117

118
  p = taosArrayPush(pRes, &block);
533✔
119
  if (p != NULL) {
533!
120
    (*pNumOfBlocks) += 1;
533✔
121
    stDebug("s-task:%s(child %d) retrieve res from upstream completed, QID:0x%" PRIx64, pTask->id.idStr,
533✔
122
            pTask->info.selfChildId, pRetrieveBlock->reqId);
123
  } else {
124
    code = terrno;
×
125
    stError("s-task:%s failed to append pull over block for retrieve data, QID:0x%" PRIx64" code:%s", pTask->id.idStr,
×
126
            pRetrieveBlock->reqId, tstrerror(code));
127
  }
128

129
  return code;
533✔
130
}
131

132
int32_t streamTaskExecImpl(SStreamTask* pTask, SStreamQueueItem* pItem, int64_t* totalSize, int32_t* totalBlocks) {
43,098✔
133
  int32_t size = 0;
43,098✔
134
  int32_t numOfBlocks = 0;
43,098✔
135
  int32_t code = TSDB_CODE_SUCCESS;
43,098✔
136
  void*   pExecutor = pTask->exec.pExecutor;
43,098✔
137
  SArray* pRes = NULL;
43,098✔
138

139
  *totalBlocks = 0;
43,098✔
140
  *totalSize = 0;
43,098✔
141

142
  while (1) {
101,283✔
143
    SSDataBlock* output = NULL;
144,381✔
144
    uint64_t     ts = 0;
144,381✔
145

146
    if (pRes == NULL) {
144,381✔
147
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
43,110✔
148
    }
149

150
    if (streamTaskShouldStop(pTask) || (pRes == NULL)) {
144,409!
151
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
20✔
152
      return code;
20✔
153
    }
154

155
    if ((code = qExecTask(pExecutor, &output, &ts)) < 0) {
144,398✔
156
      if (code == TSDB_CODE_QRY_IN_EXEC) {
14!
157
        qResetTaskInfoCode(pExecutor);
×
158
      }
159

160
      if (code == TSDB_CODE_OUT_OF_MEMORY || code == TSDB_CODE_INVALID_PARA || code == TSDB_CODE_FILE_CORRUPTED) {
14!
161
        stFatal("s-task:%s failed to continue execute since %s", pTask->id.idStr, tstrerror(code));
×
162
        taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
163
        return code;
×
164
      } else {
165
        qResetTaskCode(pExecutor);
14✔
166
        continue;
760✔
167
      }
168
    }
169

170
    if (output == NULL) {
144,396✔
171
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
43,125✔
172
         code = doAppendPullOverBlock(pTask, &numOfBlocks, (SStreamDataBlock*) pItem, pRes);
533✔
173
         if (code) {
533!
UNCOV
174
           taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
175
           return code;
×
176
         }
177
      }
178

179
      break;
43,125✔
180
    }
181

182
    if (output->info.type == STREAM_RETRIEVE) {
101,271✔
183
      if (streamBroadcastToUpTasks(pTask, output) < 0) {
160✔
184
        // TODO
185
      }
186
      continue;
160✔
187
    } else if (output->info.type == STREAM_CHECKPOINT) {
101,111✔
188
      continue;  // checkpoint block not dispatch to downstream tasks
586✔
189
    }
190

191
    SSDataBlock block = {.info.childId = pTask->info.selfChildId};
100,525✔
192
    code = assignOneDataBlock(&block, output);
100,525✔
193
    if (code) {
100,525!
194
      stError("s-task:%s failed to build result block due to out of memory", pTask->id.idStr);
×
195
      continue;
×
196
    }
197

198
    size += blockDataGetSize(output) + sizeof(SSDataBlock) + sizeof(SColumnInfoData) * blockDataGetNumOfCols(&block);
100,525✔
199
    numOfBlocks += 1;
100,523✔
200

201
    void* p = taosArrayPush(pRes, &block);
100,523✔
202
    if (p == NULL) {
100,523!
203
      stError("s-task:%s failed to add computing results, the final res may be incorrect", pTask->id.idStr);
×
204
    } else {
205
      stDebug("s-task:%s (child %d) executed and get %d result blocks, size:%.2fMiB", pTask->id.idStr,
100,523✔
206
              pTask->info.selfChildId, numOfBlocks, SIZE_IN_MiB(size));
207
    }
208

209
    // current output should be dispatched to down stream nodes
210
    if (numOfBlocks >= STREAM_RESULT_DUMP_THRESHOLD || size >= STREAM_RESULT_DUMP_SIZE_THRESHOLD) {
100,523!
211
      code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
2✔
212
      // todo: here we need continue retry to put it into output buffer
213
      if (code != TSDB_CODE_SUCCESS) {
2!
214
        return code;
×
215
      }
216

217
      pRes = NULL;
2✔
218
      size = 0;
2✔
219
      numOfBlocks = 0;
2✔
220
    }
221
  }
222

223
  if (numOfBlocks > 0) {
43,125✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
22,694✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
20,431✔
227
  }
228

229
  return code;
43,123✔
230
}
231

232
// todo contiuous try to create result blocks
233
static int32_t handleScanhistoryResultBlocks(SStreamTask* pTask, SArray* pRes, int32_t size) {
3,027✔
234
  int32_t code = TSDB_CODE_SUCCESS;
3,027✔
235
  if (taosArrayGetSize(pRes) > 0) {
3,027✔
236
    SStreamDataBlock* pStreamBlocks = NULL;
1,857✔
237
    code = createStreamBlockFromResults(NULL, pTask, size, pRes, &pStreamBlocks);
1,857✔
238
    if (code) {
1,857!
239
      stError("s-task:%s failed to build history result blocks", pTask->id.idStr);
×
240
      return code;
×
241
    }
242

243
    code = doOutputResultBlockImpl(pTask, pStreamBlocks);
1,857✔
244
    if (code != TSDB_CODE_SUCCESS) {  // should not have error code
1,857!
245
      stError("s-task:%s dump fill-history results failed, code:%s", pTask->id.idStr, tstrerror(code));
×
246
    }
247
  } else {
248
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
1,170✔
249
  }
250
  return code;
3,027✔
251
}
252

253
static void streamScanHistoryDataImpl(SStreamTask* pTask, SArray* pRes, int32_t* pSize, bool* pFinish) {
3,032✔
254
  int32_t code = TSDB_CODE_SUCCESS;
3,032✔
255
  void*   exec = pTask->exec.pExecutor;
3,032✔
256
  int32_t numOfBlocks = 0;
3,032✔
257

258
  while (1) {
43,291✔
259
    if (streamTaskShouldStop(pTask)) {
46,323!
260
      break;
×
261
    }
262

263
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
46,323!
264
      stDebug("s-task:%s level:%d inputQ is blocked, retry in 5s", pTask->id.idStr, pTask->info.taskLevel);
×
265
      break;
×
266
    }
267

268
    SSDataBlock* output = NULL;
46,323✔
269
    uint64_t     ts = 0;
46,323✔
270
    code = qExecTask(exec, &output, &ts);
46,323✔
271
    if (code != TSDB_CODE_TSC_QUERY_KILLED && code != TSDB_CODE_SUCCESS) {  // if out of memory occurs, quit
46,323!
272
      stError("s-task:%s scan-history data error occurred code:%s, continue scan-history", pTask->id.idStr,
×
273
              tstrerror(code));
274
      qResetTaskCode(exec);
×
275
      continue;
×
276
    }
277

278
    // the generated results before fill-history task been paused, should be dispatched to sink node
279
    if (output == NULL) {
46,323✔
280
      (*pFinish) = qStreamScanhistoryFinished(exec);
2,354✔
281
      break;
2,354✔
282
    }
283

284
    SSDataBlock block = {0};
43,969✔
285
    code = assignOneDataBlock(&block, output);
43,969✔
286
    if (code) {
43,969!
287
      stError("s-task:%s failed to build result block due to out of memory", pTask->id.idStr);
×
288
    }
289

290
    block.info.childId = pTask->info.selfChildId;
43,969✔
291
    void* p = taosArrayPush(pRes, &block);
43,969✔
292
    if (p == NULL) {
43,969!
293
      stError("s-task:%s failed to add computing results, the final res may be incorrect", pTask->id.idStr);
×
294
    }
295

296
    (*pSize) +=
43,969✔
297
        blockDataGetSize(output) + sizeof(SSDataBlock) + sizeof(SColumnInfoData) * blockDataGetNumOfCols(&block);
43,969✔
298
    numOfBlocks += 1;
43,969✔
299

300
    if (numOfBlocks >= STREAM_RESULT_DUMP_THRESHOLD || (*pSize) >= STREAM_RESULT_DUMP_SIZE_THRESHOLD) {
43,969✔
301
      stDebug("s-task:%s scan exec numOfBlocks:%d, size:%.2fKiB output num-limit:%d, size-limit:%.2fKiB reached",
678!
302
              pTask->id.idStr, numOfBlocks, SIZE_IN_KiB(*pSize), STREAM_RESULT_DUMP_THRESHOLD,
303
              SIZE_IN_KiB(STREAM_RESULT_DUMP_SIZE_THRESHOLD));
304
      break;
678✔
305
    }
306
  }
307
}
3,032✔
308

309
static SScanhistoryDataInfo buildScanhistoryExecRet(EScanHistoryCode code, int32_t idleTime) {
2,548✔
310
  return (SScanhistoryDataInfo){code, idleTime};
2,548✔
311
}
312

313
SScanhistoryDataInfo streamScanHistoryData(SStreamTask* pTask, int64_t st) {
2,548✔
314
  void*       exec = pTask->exec.pExecutor;
2,548✔
315
  bool        finished = false;
2,548✔
316
  const char* id = pTask->id.idStr;
2,548✔
317

318
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
2,548!
319
    stError("s-task:%s not source scan-history task, not exec, quit", pTask->id.idStr);
×
320
    return buildScanhistoryExecRet(TASK_SCANHISTORY_QUIT, 0);
×
321
  }
322

323
  if (!pTask->hTaskInfo.operatorOpen) {
2,548✔
324
    int32_t code = qSetStreamOpOpen(exec);
2,354✔
325
    pTask->hTaskInfo.operatorOpen = true;
2,353✔
326
  }
327

328
  while (1) {
484✔
329
    if (streamTaskShouldPause(pTask)) {
3,031!
330
      stDebug("s-task:%s paused from the scan-history task", id);
×
331
      // quit from step1, not continue to handle the step2
332
      return buildScanhistoryExecRet(TASK_SCANHISTORY_QUIT, 0);
2,548✔
333
    }
334

335
    // output queue is full, idle for 5 sec.
336
    if (streamQueueIsFull(pTask->outputq.queue)) {
3,031!
337
      stWarn("s-task:%s outputQ is full, idle for 1sec and retry", id);
×
338
      return buildScanhistoryExecRet(TASK_SCANHISTORY_REXEC, STREAM_SCAN_HISTORY_TIMESLICE);
×
339
    }
340

341
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
3,032!
342
      stWarn("s-task:%s downstream task inputQ blocked, idle for 5sec and retry", id);
×
343
      return buildScanhistoryExecRet(TASK_SCANHISTORY_REXEC, FILL_HISTORY_TASK_EXEC_INTERVAL);
×
344
    }
345

346
    SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
3,032✔
347
    if (pRes == NULL) {
3,032!
348
      terrno = TSDB_CODE_OUT_OF_MEMORY;
×
349
      stError("s-task:%s scan-history prepare result block failed, code:%s, retry later", id, tstrerror(terrno));
×
350
      continue;
×
351
    }
352

353
    int32_t size = 0;
3,032✔
354
    streamScanHistoryDataImpl(pTask, pRes, &size, &finished);
3,032✔
355

356
    if (streamTaskShouldStop(pTask)) {
3,032✔
357
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
5✔
358
      return buildScanhistoryExecRet(TASK_SCANHISTORY_QUIT, 0);
5✔
359
    }
360

361
    // dispatch the generated results, todo fix error
362
    int32_t code = handleScanhistoryResultBlocks(pTask, pRes, size);
3,027✔
363
    if (code) {
3,027!
364
      stError("s-task:%s failed to handle scan result block, code:%s", pTask->id.idStr, tstrerror(code));
×
365
    }
366

367
    if (finished) {
3,027✔
368
      return buildScanhistoryExecRet(TASK_SCANHISTORY_CONT, 0);
2,349✔
369
    }
370

371
    int64_t el = taosGetTimestampMs() - st;
678✔
372
    if (el >= STREAM_SCAN_HISTORY_TIMESLICE && (pTask->info.fillHistory == 1)) {
678!
373
      stDebug("s-task:%s fill-history:%d time slice exhausted, elapsed time:%.2fs, retry in 100ms", id,
194!
374
              pTask->info.fillHistory, el / 1000.0);
375
      return buildScanhistoryExecRet(TASK_SCANHISTORY_REXEC, 100);
194✔
376
    }
377
  }
378
}
379

380
int32_t streamTransferStateDoPrepare(SStreamTask* pTask) {
2,307✔
381
  SStreamMeta* pMeta = pTask->pMeta;
2,307✔
382
  const char*  id = pTask->id.idStr;
2,307✔
383

384
  SStreamTask* pStreamTask = NULL;
2,307✔
385
  int32_t code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
2,307✔
386
  if (pStreamTask == NULL || code != TSDB_CODE_SUCCESS) {
2,307!
387
    stError(
×
388
        "s-task:%s failed to find related stream task:0x%x, may have been destroyed or closed, destroy related "
389
        "fill-history task",
390
        id, (int32_t)pTask->streamTaskId.taskId);
391

392
    // 1. free it and remove fill-history task from disk meta-store
393
    // todo: this function should never be failed.
394
    code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
×
395

396
    // 2. save to disk
397
    streamMetaWLock(pMeta);
×
398
    if (streamMetaCommit(pMeta) < 0) {
×
399
      // persist to disk
400
    }
401
    streamMetaWUnLock(pMeta);
×
402
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
403
  } else {
404
    double el = (taosGetTimestampMs() - pTask->execInfo.step2Start) / 1000.;
2,307✔
405
    stDebug(
2,307✔
406
        "s-task:%s fill-history task end, status:%s, scan wal elapsed time:%.2fSec, update related stream task:%s "
407
        "info, prepare transfer exec state",
408
        id, streamTaskGetStatus(pTask).name, el, pStreamTask->id.idStr);
409
  }
410

411
  ETaskStatus  status = streamTaskGetStatus(pStreamTask).state;
2,307✔
412
  STimeWindow* pTimeWindow = &pStreamTask->dataRange.window;
2,307✔
413

414
  // It must be halted for a source stream task, since when the related scan-history-data task start scan the history
415
  // for the step 2.
416
  if (pStreamTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2,307✔
417
    if (!(status == TASK_STATUS__HALT || status == TASK_STATUS__DROPPING || status == TASK_STATUS__STOP)) {
2,238!
418
      stError("s-task:%s invalid task status:%d", id, status);
×
419
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
420
    }
421
  } else {
422
    if (!(status == TASK_STATUS__READY || status == TASK_STATUS__PAUSE || status == TASK_STATUS__DROPPING ||
69!
423
          status == TASK_STATUS__STOP)) {
424
      stError("s-task:%s invalid task status:%d", id, status);
×
425
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
426
    }
427
    code = streamTaskHandleEvent(pStreamTask->status.pSM, TASK_EVENT_HALT);
69✔
428
    if (code != TSDB_CODE_SUCCESS) {
69!
429
      stError("s-task:%s halt stream task:%s failed, code:%s not transfer state to stream task", id,
×
430
              pStreamTask->id.idStr, tstrerror(code));
431
      streamMetaReleaseTask(pMeta, pStreamTask);
×
432
      return code;
×
433
    } else {
434
      stDebug("s-task:%s halt by related fill-history task:%s", pStreamTask->id.idStr, id);
69✔
435
    }
436
  }
437

438
  // In case of sink tasks, no need to halt them.
439
  // In case of source tasks and agg tasks, we should HALT them, and wait for them to be idle. And then, it's safe to
440
  // start the task state transfer procedure.
441
  SStreamTaskState pState = streamTaskGetStatus(pStreamTask);
2,307✔
442
  status = pState.state;
2,307✔
443
  char* p = pState.name;
2,307✔
444
  if (status == TASK_STATUS__STOP || status == TASK_STATUS__DROPPING) {
2,307!
445
    stError("s-task:%s failed to transfer state from fill-history task:%s, status:%s", id, pStreamTask->id.idStr, p);
×
446
    streamMetaReleaseTask(pMeta, pStreamTask);
×
447
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
×
448
  }
449

450
  // 1. expand the query time window for stream task of WAL scanner
451
  if (pStreamTask->info.taskLevel == TASK_LEVEL__SOURCE) {
2,307✔
452
    // update the scan data range for source task.
453
    stDebug("s-task:%s level:%d stream task window %" PRId64 " - %" PRId64 " update to %" PRId64 " - %" PRId64
2,238✔
454
            ", status:%s, sched-status:%d",
455
            pStreamTask->id.idStr, TASK_LEVEL__SOURCE, pTimeWindow->skey, pTimeWindow->ekey, INT64_MIN,
456
            pTimeWindow->ekey, p, pStreamTask->status.schedStatus);
457

458
    code = streamTaskResetTimewindowFilter(pStreamTask);
2,238✔
459
  } else {
460
    stDebug("s-task:%s no need to update/reset filter time window for non-source tasks", pStreamTask->id.idStr);
69✔
461
  }
462

463
  // NOTE: transfer the ownership of executor state before handle the checkpoint block during stream exec
464
  // 2. send msg to mnode to launch a checkpoint to keep the state for current stream
465
  code = streamTaskSendCheckpointReq(pStreamTask);
2,307✔
466

467
  // 3. assign the status to the value that will be kept in disk
468
  pStreamTask->status.taskStatus = streamTaskGetStatus(pStreamTask).state;
2,307✔
469

470
  // 4. open the inputQ for all upstream tasks
471
  streamTaskOpenAllUpstreamInput(pStreamTask);
2,307✔
472

473
  streamMetaReleaseTask(pMeta, pStreamTask);
2,307✔
474
  return code;
2,307✔
475
}
476

477
static int32_t haltCallback(SStreamTask* pTask, void* param) {
2,165✔
478
  streamTaskOpenAllUpstreamInput(pTask);
2,165✔
479
  return streamTaskSendCheckpointReq(pTask);
2,162✔
480
}
481

482
int32_t streamTransferStatePrepare(SStreamTask* pTask) {
4,473✔
483
  int32_t      code = TSDB_CODE_SUCCESS;
4,473✔
484
  SStreamMeta* pMeta = pTask->pMeta;
4,473✔
485

486
  if (pTask->status.appendTranstateBlock != 1) {
4,473!
487
    stError("s-task:%s not set appendTransBlock flag, internal error", pTask->id.idStr);
×
488
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
489
  }
490

491
  int32_t level = pTask->info.taskLevel;
4,473✔
492
  if (level == TASK_LEVEL__AGG || level == TASK_LEVEL__SOURCE) {  // do transfer task operator states.
4,473✔
493
    code = streamTransferStateDoPrepare(pTask);
2,307✔
494
  } else {
495
    // no state transfer for sink tasks, and drop fill-history task, followed by opening inputQ of sink task.
496
    SStreamTask* pStreamTask = NULL;
2,166✔
497
    code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
2,166✔
498
    if (pStreamTask != NULL) {
2,167!
499
      // halt the related stream sink task
500
      code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, haltCallback, NULL);
2,167✔
501
      if (code != TSDB_CODE_SUCCESS) {
2,166!
502
        stError("s-task:%s halt stream task:%s failed, code:%s not transfer state to stream task", pTask->id.idStr,
×
503
                pStreamTask->id.idStr, tstrerror(code));
504
        streamMetaReleaseTask(pMeta, pStreamTask);
×
505
        return code;
×
506
      } else {
507
        stDebug("s-task:%s sink task halt by related fill-history task:%s", pStreamTask->id.idStr, pTask->id.idStr);
2,166✔
508
      }
509
      streamMetaReleaseTask(pMeta, pStreamTask);
2,166✔
510
    }
511
  }
512

513
  return code;
4,473✔
514
}
515

516
// set input
517
static int32_t doSetStreamInputBlock(SStreamTask* pTask, const void* pInput, int64_t* pVer, const char* id) {
43,112✔
518
  void*   pExecutor = pTask->exec.pExecutor;
43,112✔
519
  int32_t code = 0;
43,112✔
520

521
  const SStreamQueueItem* pItem = pInput;
43,112✔
522
  if (pItem->type == STREAM_INPUT__GET_RES) {
43,112✔
523
    const SStreamTrigger* pTrigger = (const SStreamTrigger*)pInput;
2,881✔
524
    code = qSetMultiStreamInput(pExecutor, pTrigger->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
2,881✔
525
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
2,881✔
526
      stDebug("s-task:%s set force_window_close as source block, skey:%"PRId64, id, pTrigger->pBlock->info.window.skey);
1,586!
527
      (*pVer) = pTrigger->pBlock->info.window.skey;
1,586✔
528
    }
529
  } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
40,231✔
530
    const SStreamDataSubmit* pSubmit = (const SStreamDataSubmit*)pInput;
9,409✔
531
    code = qSetMultiStreamInput(pExecutor, &pSubmit->submit, 1, STREAM_INPUT__DATA_SUBMIT);
9,409✔
532
    stDebug("s-task:%s set submit blocks as source block completed, %p %p len:%d ver:%" PRId64, id, pSubmit,
9,407✔
533
            pSubmit->submit.msgStr, pSubmit->submit.msgLen, pSubmit->submit.ver);
534
    if ((*pVer) > pSubmit->submit.ver) {
9,407!
535
      stError("s-task:%s invalid recorded ver:%" PRId64 " greater than new block ver:%" PRId64 ", not update", id,
×
536
              *pVer, pSubmit->submit.ver);
537
    } else {
538
      (*pVer) = pSubmit->submit.ver;
9,407✔
539
    }
540
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK || pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
33,325✔
541
    const SStreamDataBlock* pBlock = (const SStreamDataBlock*)pInput;
2,499✔
542

543
    SArray* pBlockList = pBlock->blocks;
2,499✔
544
    int32_t numOfBlocks = taosArrayGetSize(pBlockList);
2,499✔
545
    stDebug("s-task:%s set sdata blocks as input num:%d, ver:%" PRId64, id, numOfBlocks, pBlock->sourceVer);
2,503✔
546
    code = qSetMultiStreamInput(pExecutor, pBlockList->pData, numOfBlocks, STREAM_INPUT__DATA_BLOCK);
2,503✔
547

548
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
28,323✔
549
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
23,451✔
550

551
    SArray* pBlockList = pMerged->submits;
23,451✔
552
    int32_t numOfBlocks = taosArrayGetSize(pBlockList);
23,451✔
553
    stDebug("s-task:%s %p set (merged) submit blocks as a batch, numOfBlocks:%d, ver:%" PRId64, id, pTask, numOfBlocks,
23,455✔
554
            pMerged->ver);
555
    code = qSetMultiStreamInput(pExecutor, pBlockList->pData, numOfBlocks, STREAM_INPUT__MERGED_SUBMIT);
23,455✔
556

557
    if ((*pVer) > pMerged->ver) {
23,453!
UNCOV
558
      stError("s-task:%s invalid recorded ver:%" PRId64 " greater than new block ver:%" PRId64 ", not update", id,
×
559
              *pVer, pMerged->ver);
560
    } else {
561
      (*pVer) = pMerged->ver;
23,453✔
562
    }
563

564
  } else if (pItem->type == STREAM_INPUT__REF_DATA_BLOCK) {
4,872✔
565
    const SStreamRefDataBlock* pRefBlock = (const SStreamRefDataBlock*)pInput;
2,615✔
566
    code = qSetMultiStreamInput(pExecutor, pRefBlock->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
2,615✔
567

568
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT || pItem->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
4,513!
569
    const SStreamDataBlock* pCheckpoint = (const SStreamDataBlock*)pInput;
2,257✔
570
    code = qSetMultiStreamInput(pExecutor, pCheckpoint->blocks, 1, pItem->type);
2,257✔
571

572
  } else {
573
    stError("s-task:%s invalid input block type:%d, discard", id, pItem->type);
×
574
    code = TSDB_CODE_STREAM_INTERNAL_ERROR;
×
575
  }
576

577
  return code;
43,103✔
578
}
579

580
void streamProcessTransstateBlock(SStreamTask* pTask, SStreamDataBlock* pBlock) {
9,149✔
581
  const char* id = pTask->id.idStr;
9,149✔
582
  int32_t     code = TSDB_CODE_SUCCESS;
9,149✔
583
  int32_t     level = pTask->info.taskLevel;
9,149✔
584
  // dispatch the tran-state block to downstream task immediately
585
  int32_t type = pTask->outputInfo.type;
9,149✔
586

587
  if (level == TASK_LEVEL__AGG || level == TASK_LEVEL__SINK) {
9,149✔
588
    int32_t remain = streamAlignTransferState(pTask);
6,906✔
589
    if (remain > 0) {
6,913✔
590
      streamFreeQitem((SStreamQueueItem*)pBlock);
4,678✔
591
      stDebug("s-task:%s receive upstream trans-state msg, not sent remain:%d", id, remain);
4,675✔
592
      return;
4,675✔
593
    }
594
  }
595

596
  // transfer the ownership of executor state
597
  if (type == TASK_OUTPUT__FIXED_DISPATCH || type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
4,478✔
598
    if (level == TASK_LEVEL__SOURCE) {
2,302✔
599
      stDebug("s-task:%s add transfer-state block into outputQ", id);
2,233✔
600
    } else {
601
      stDebug("s-task:%s all upstream tasks send transfer-state block, add transfer-state block into outputQ", id);
69✔
602
    }
603

604
    // agg task should dispatch trans-state msg to sink task, to flush all data to sink task.
605
    if (level == TASK_LEVEL__AGG || level == TASK_LEVEL__SOURCE) {
2,302!
606
      pBlock->srcVgId = pTask->pMeta->vgId;
2,302✔
607
      code = taosWriteQitem(pTask->outputq.queue->pQueue, pBlock);
2,302✔
608
      if (code == 0) {
2,302!
609
        code = streamDispatchStreamBlock(pTask);
2,302✔
610
        if (code) {
2,302!
611
          stError("s-task:%s failed to dispatch stream block, code:%s", id, tstrerror(code));
×
612
        }
613
      } else {  // todo put into queue failed, retry
614
        streamFreeQitem((SStreamQueueItem*)pBlock);
×
615
      }
616
    } else {  // level == TASK_LEVEL__SINK
617
      streamFreeQitem((SStreamQueueItem*)pBlock);
×
618
    }
619
  } else {  // non-dispatch task, do task state transfer directly
620
    streamFreeQitem((SStreamQueueItem*)pBlock);
2,176✔
621
    stDebug("s-task:%s non-dispatch task, level:%d start to transfer state directly", id, level);
2,176✔
622

623
    code = streamTransferStatePrepare(pTask);
2,176✔
624
    if (code != TSDB_CODE_SUCCESS) {
2,174!
625
      stError("s-task:%s failed to prepare transfer state, code:%s", id, tstrerror(code));
×
626
      int8_t status = streamTaskSetSchedStatusInactive(pTask);  // let's ignore this return status
×
627
    }
628
  }
629
}
630

631
// static void streamTaskSetIdleInfo(SStreamTask* pTask, int32_t idleTime) { pTask->status.schedIdleTime = idleTime; }
632
static void setLastExecTs(SStreamTask* pTask, int64_t ts) { pTask->status.lastExecTs = ts; }
69,384✔
633

634
static void doRecordThroughput(STaskExecStatisInfo* pInfo, int64_t totalBlocks, int64_t totalSize, int64_t blockSize,
43,136✔
635
                               double st, const char* id) {
636
  double el = (taosGetTimestampMs() - st) / 1000.0;
43,138✔
637

638
  stDebug("s-task:%s batch of input blocks exec end, elapsed time:%.2fs, result size:%.2fMiB, numOfBlocks:%" PRId64, id,
43,138✔
639
          el, SIZE_IN_MiB(totalSize), totalBlocks);
640

641
  pInfo->outputDataBlocks += totalBlocks;
43,138✔
642
  pInfo->outputDataSize += totalSize;
43,138✔
643
  if (fabs(el - 0.0) <= DBL_EPSILON) {
43,138✔
644
    pInfo->procsThroughput = 0;
14,720✔
645
    pInfo->outputThroughput = 0;
14,720✔
646
  } else {
647
    pInfo->outputThroughput = (totalSize / el);
28,418✔
648
    pInfo->procsThroughput = (blockSize / el);
28,418✔
649
  }
650
}
43,138✔
651

652
static int32_t doStreamTaskExecImpl(SStreamTask* pTask, SStreamQueueItem* pBlock, int32_t num) {
43,116✔
653
  const char*      id = pTask->id.idStr;
43,116✔
654
  int32_t          blockSize = 0;
43,116✔
655
  int64_t          st = taosGetTimestampMs();
43,123✔
656
  SCheckpointInfo* pInfo = &pTask->chkInfo;
43,123✔
657
  int64_t          ver = pInfo->processedVer;
43,123✔
658
  int64_t          totalSize = 0;
43,123✔
659
  int32_t          totalBlocks = 0;
43,123✔
660
  int32_t          code = 0;
43,123✔
661

662
  stDebug("s-task:%s start to process batch blocks, num:%d, type:%s", id, num, streamQueueItemGetTypeStr(pBlock->type));
43,123✔
663

664
  code = doSetStreamInputBlock(pTask, pBlock, &ver, id);
43,123✔
665
  if (code) {
43,103!
666
    stError("s-task:%s failed to set input block, not exec for these blocks", id);
×
667
    return code;
×
668
  }
669

670
  code = streamTaskExecImpl(pTask, pBlock, &totalSize, &totalBlocks);
43,103✔
671
  if (code) {
43,142✔
672
    return code;
6✔
673
  }
674

675
  doRecordThroughput(&pTask->execInfo, totalBlocks, totalSize, blockSize, st, pTask->id.idStr);
43,136✔
676

677
  // update the currentVer if processing the submitted blocks.
678
  if (!(pInfo->checkpointVer <= pInfo->nextProcessVer && ver >= pInfo->checkpointVer)) {
43,136!
679
    stError("s-task:%s invalid info, checkpointVer:%" PRId64 ", nextProcessVer:%" PRId64 " currentVer:%" PRId64, id,
×
680
            pInfo->checkpointVer, pInfo->nextProcessVer, ver);
681
    return code;
×
682
  }
683

684
  if (ver != pInfo->processedVer) {
43,136✔
685
    stDebug("s-task:%s update processedVer(unsaved) from %" PRId64 " to %" PRId64 " nextProcessVer:%" PRId64
34,465✔
686
            " ckpt:%" PRId64,
687
            id, pInfo->processedVer, ver, pInfo->nextProcessVer, pInfo->checkpointVer);
688
    pInfo->processedVer = ver;
34,465✔
689
  }
690

691
  return code;
43,136✔
692
}
693

694
// do nothing after sync executor state to storage backend, untill checkpoint is completed.
695
static int32_t doHandleChkptBlock(SStreamTask* pTask) {
2,168✔
696
  int32_t     code = 0;
2,168✔
697
  const char* id = pTask->id.idStr;
2,168✔
698

699
  streamMutexLock(&pTask->lock);
2,168✔
700
  SStreamTaskState pState = streamTaskGetStatus(pTask);
2,172✔
701
  if (pState.state == TASK_STATUS__CK) {  // todo other thread may change the status
2,171!
702
    stDebug("s-task:%s checkpoint block received, set status:%s", id, pState.name);
2,171✔
703
    code = streamTaskBuildCheckpoint(pTask);  // ignore this error msg, and continue
2,171✔
704
  } else {                                    // todo refactor
705
    if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
706
      code = streamTaskSendCheckpointSourceRsp(pTask);
×
707
    } else {
708
      code = streamTaskSendCheckpointReadyMsg(pTask);
×
709
    }
710

711
    if (code != TSDB_CODE_SUCCESS) {
×
712
      // todo: let's retry send rsp to upstream/mnode
713
      stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%d, code:%s", id, 0,
×
714
              tstrerror(code));
715
    }
716
  }
717

718
  streamMutexUnlock(&pTask->lock);
2,172✔
719
  return code;
2,172✔
720
}
721

722
int32_t flushStateDataInExecutor(SStreamTask* pTask, SStreamQueueItem* pCheckpointBlock) {
2,254✔
723
  const char* id = pTask->id.idStr;
2,254✔
724

725
  // 1. transfer the ownership of executor state
726
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
2,254✔
727
  if (dropRelHTask) {
2,255✔
728
    STaskId*     pHTaskId = &pTask->hTaskInfo.id;
2,218✔
729
    SStreamTask* pHTask = NULL;
2,218✔
730
    int32_t      code = streamMetaAcquireTask(pTask->pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
2,218✔
731
    if (code == TSDB_CODE_SUCCESS) {  // ignore the error code.
2,219!
732
      code = streamTaskReleaseState(pHTask);
2,219✔
733
      if (code) {
2,219!
734
        stError("s-task:%s failed to release query state, code:%s", pHTask->id.idStr, tstrerror(code));
×
735
      }
736

737
      if (code == TSDB_CODE_SUCCESS) {
2,218!
738
        code = streamTaskReloadState(pTask);
2,218✔
739
        if (code) {
2,220!
740
          stError("s-task:%s failed to reload query state, code:%s", pTask->id.idStr, tstrerror(code));
×
741
        }
742
      }
743

744
      stDebug("s-task:%s transfer state from fill-history task:%s, status:%s completed", id, pHTask->id.idStr,
2,220✔
745
              streamTaskGetStatus(pHTask).name);
746
      // todo execute qExecTask to fetch the reload-generated result, if this is stream is for session window query.
747
      /*
748
       * while(1) {
749
       * qExecTask()
750
       * }
751
       * // put into the output queue.
752
       */
753
      streamMetaReleaseTask(pTask->pMeta, pHTask);
2,220✔
754
    } else {
755
      stError("s-task:%s related fill-history task:0x%x failed to acquire, transfer state failed", id,
×
756
              (int32_t)pHTaskId->taskId);
757
    }
758
  } else {
759
    stDebug("s-task:%s no transfer-state needed", id);
37✔
760
  }
761

762
  // 2. flush data in executor to K/V store, which should be completed before do checkpoint in the K/V.
763
  int32_t code = doStreamTaskExecImpl(pTask, pCheckpointBlock, 1);
2,258✔
764
  if (code) {
2,257!
765
    stError("s-task:%s failed to exec stream task before checkpoint, code:%s", id, tstrerror(code));
×
766
  }
767

768
  return code;
2,257✔
769
}
770

771
/**
772
 * todo: the batch of blocks should be tuned dynamic, according to the total elapsed time of each batch of blocks, the
773
 * appropriate batch of blocks should be handled in 5 to 10 sec.
774
 */
775
static int32_t doStreamExecTask(SStreamTask* pTask) {
71,557✔
776
  const char* id = pTask->id.idStr;
71,557✔
777
  int32_t     code = 0;
71,557✔
778

779
  // merge multiple input data if possible in the input queue.
780
  stDebug("s-task:%s start to extract data block from inputQ", id);
71,557✔
781

782
  while (1) {
75,736✔
783
    int32_t           blockSize = 0;
147,311✔
784
    int32_t           numOfBlocks = 0;
147,311✔
785
    SStreamQueueItem* pInput = NULL;
147,311✔
786

787
    if (streamTaskShouldStop(pTask) || (streamTaskGetStatus(pTask).state == TASK_STATUS__UNINIT)) {
147,311!
788
      stDebug("s-task:%s stream task is stopped", id);
19✔
789
      return 0;
71,589✔
790
    }
791

792
    if (streamQueueIsFull(pTask->outputq.queue)) {
147,276✔
793
      stTrace("s-task:%s outputQ is full, idle for 500ms and retry", id);
3!
794
      streamTaskSetIdleInfo(pTask, 1000);
3✔
795
      return 0;
×
796
    }
797

798
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
147,284!
799
      stTrace("s-task:%s downstream task inputQ blocked, idle for 1sec and retry", id);
×
800
      streamTaskSetIdleInfo(pTask, 1000);
×
801
      return 0;
×
802
    }
803

804
    if (taosGetTimestampMs() - pTask->status.lastExecTs < MIN_INVOKE_INTERVAL) {
147,262✔
805
      stDebug("s-task:%s invoke exec too fast, idle and retry in 50ms", id);
12,589✔
806
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
12,589✔
807
      return 0;
12,589✔
808
    }
809

810
    EExtractDataCode ret = streamTaskGetDataFromInputQ(pTask, &pInput, &numOfBlocks, &blockSize);
134,673✔
811
    if (ret == EXEC_AFTER_IDLE) {
134,663!
812
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
×
813
      return 0;
×
814
    } else {
815
      if (pInput == NULL) {
134,684✔
816
        return 0;
56,805✔
817
      }
818
    }
819

820
    pTask->execInfo.inputDataBlocks += numOfBlocks;
77,879✔
821
    pTask->execInfo.inputDataSize += blockSize;
77,879✔
822

823
    // dispatch checkpoint msg to all downstream tasks
824
    int32_t type = pInput->type;
77,879✔
825
    if (type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
77,879✔
826
      code = streamProcessCheckpointTriggerBlock(pTask, (SStreamDataBlock*)pInput);
8,912✔
827
      if (code != 0) {
8,923!
828
        stError("s-task:%s failed to process checkpoint-trigger block, code:%s", pTask->id.idStr, tstrerror(code));
×
829
      }
830
      continue;
34,856✔
831
    }
832

833
    if (type == STREAM_INPUT__TRANS_STATE) {
68,967✔
834
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
9,149✔
835
      continue;
9,150✔
836
    }
837

838
    if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
59,818✔
839
      if (type != STREAM_INPUT__DATA_BLOCK && type != STREAM_INPUT__CHECKPOINT) {
16,778!
840
        stError("s-task:%s invalid block type:%d for sink task, discard", id, type);
×
841
        continue;
×
842
      }
843

844
      int64_t st = taosGetTimestampMs();
16,781✔
845

846
      // here only handle the data block sink operation
847
      if (type == STREAM_INPUT__DATA_BLOCK) {
16,781!
848
        pTask->execInfo.sink.dataSize += blockSize;
16,781✔
849
        stDebug("s-task:%s sink task start to sink %d blocks, size:%.2fKiB", id, numOfBlocks, SIZE_IN_KiB(blockSize));
16,781✔
850
        code = doOutputResultBlockImpl(pTask, (SStreamDataBlock*)pInput);
16,781✔
851
        if (code != TSDB_CODE_SUCCESS) {
16,783!
852
          return code;
×
853
        }
854

855
        double el = (taosGetTimestampMs() - st) / 1000.0;
16,783✔
856
        if (fabs(el - 0.0) <= DBL_EPSILON) {
16,783✔
857
          pTask->execInfo.procsThroughput = 0;
9,004✔
858
        } else {
859
          pTask->execInfo.procsThroughput = (blockSize / el);
7,779✔
860
        }
861

862
        continue;
16,783✔
863
      }
864
    }
865

866
    if (type == STREAM_INPUT__CHECKPOINT) {
43,040✔
867
      code = doHandleChkptBlock(pTask);
2,169✔
868
      streamFreeQitem(pInput);
2,172✔
869
      return code;
2,170✔
870
    } else {
871
      code = doStreamTaskExecImpl(pTask, pInput, numOfBlocks);
40,871✔
872
      streamFreeQitem(pInput);
40,881✔
873
      if (code) {
40,886✔
874
        return code;
6✔
875
      }
876
    }
877
  }
878
}
879

880
// the task may be set dropping/stopping, while it is still in the task queue, therefore, the sched-status can not
881
// be updated by tryExec function, therefore, the schedStatus will always be the TASK_SCHED_STATUS__WAITING.
882
bool streamTaskIsIdle(const SStreamTask* pTask) {
2,482✔
883
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,482✔
884
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE || status == TASK_STATUS__STOP ||
2,482!
885
          status == TASK_STATUS__DROPPING);
886
}
887

888
bool streamTaskReadyToRun(const SStreamTask* pTask, char** pStatus) {
67,896✔
889
  SStreamTaskState pState = streamTaskGetStatus(pTask);
67,896✔
890

891
  ETaskStatus st = pState.state;
67,898✔
892
  if (pStatus != NULL) {
67,898!
893
    *pStatus = pState.name;
67,907✔
894
  }
895

896
  // pause & halt will still run for sink tasks.
897
  if (streamTaskIsSinkTask(pTask)) {
67,898✔
898
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
13,673✔
899
            st == TASK_STATUS__PAUSE || st == TASK_STATUS__HALT);
46,746!
900
  } else {
901
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
34,823✔
902
            st == TASK_STATUS__HALT);
903
  }
904
}
905

906
int32_t streamResumeTask(SStreamTask* pTask) {
69,385✔
907
  const char* id = pTask->id.idStr;
69,385✔
908
  int32_t     code = 0;
69,385✔
909

910
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
69,385!
911
    stError("s-task:%s invalid sched status:%d, not resume task", pTask->id.idStr, pTask->status.schedStatus);
×
912
    return code;
×
913
  }
914

915
  while (1) {
2,189✔
916
    code = doStreamExecTask(pTask);
71,574✔
917
    if (code) {
71,588✔
918
      stError("s-task:%s failed to exec stream task, code:%s", id, tstrerror(code));
6!
919
      return code;
6✔
920
    }
921
    // check if continue
922
    streamMutexLock(&pTask->lock);
71,582✔
923

924
    int32_t numOfItems = streamQueueGetNumOfItems(pTask->inputq.queue);
71,587✔
925
    if ((numOfItems == 0) || streamTaskShouldStop(pTask) || streamTaskShouldPause(pTask)) {
71,586✔
926
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
58,368✔
927
      streamTaskClearSchedIdleInfo(pTask);
58,370✔
928
      streamMutexUnlock(&pTask->lock);
58,367✔
929

930
      setLastExecTs(pTask, taosGetTimestampMs());
58,359✔
931

932
      char* p = streamTaskGetStatus(pTask).name;
58,355✔
933
      stDebug("s-task:%s exec completed, status:%s, sched-status:%d, lastExecTs:%" PRId64, id, p,
58,356✔
934
              pTask->status.schedStatus, pTask->status.lastExecTs);
935

936
      return code;
58,358✔
937
    } else {
938
      // check if this task needs to be idle for a while
939
      if (pTask->status.schedIdleTime > 0) {
13,216✔
940
        streamTaskResumeInFuture(pTask);
11,027✔
941

942
        streamMutexUnlock(&pTask->lock);
11,032✔
943
        setLastExecTs(pTask, taosGetTimestampMs());
11,031✔
944
        return code;
11,031✔
945
      }
946
    }
947

948
    streamMutexUnlock(&pTask->lock);
2,189✔
949
  }
950

951
  return code;
952
}
953

954
int32_t streamExecTask(SStreamTask* pTask) {
58,417✔
955
  // this function may be executed by multi-threads, so status check is required.
956
  const char* id = pTask->id.idStr;
58,417✔
957
  int32_t     code = 0;
58,417✔
958

959
  int8_t schedStatus = streamTaskSetSchedStatusActive(pTask);
58,417✔
960
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
58,449!
961
    code = streamResumeTask(pTask);
58,449✔
962
  } else {
963
    char* p = streamTaskGetStatus(pTask).name;
×
964
    stDebug("s-task:%s already started to exec by other thread, status:%s, sched-status:%d", id, p,
×
965
            pTask->status.schedStatus);
966
  }
967

968
  return code;
58,442✔
969
}
970

971
int32_t streamTaskReleaseState(SStreamTask* pTask) {
2,219✔
972
  stDebug("s-task:%s release exec state", pTask->id.idStr);
2,219✔
973
  void* pExecutor = pTask->exec.pExecutor;
2,219✔
974

975
  int32_t code = TSDB_CODE_SUCCESS;
2,219✔
976
  if (pExecutor != NULL) {
2,219!
977
    code = qStreamOperatorReleaseState(pExecutor);
2,219✔
978
  }
979

980
  return code;
2,218✔
981
}
982

983
int32_t streamTaskReloadState(SStreamTask* pTask) {
2,218✔
984
  stDebug("s-task:%s reload exec state", pTask->id.idStr);
2,218✔
985
  void* pExecutor = pTask->exec.pExecutor;
2,218✔
986

987
  int32_t code = TSDB_CODE_SUCCESS;
2,218✔
988
  if (pExecutor != NULL) {
2,218!
989
    code = qStreamOperatorReloadState(pExecutor);
2,218✔
990
  }
991

992
  return code;
2,219✔
993
}
994

995
int32_t streamAlignTransferState(SStreamTask* pTask) {
6,908✔
996
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
6,908✔
997
  int32_t old = atomic_val_compare_exchange_32(&pTask->transferStateAlignCnt, 0, numOfUpstream);
6,908✔
998
  if (old == 0) {
6,913✔
999
    stDebug("s-task:%s set the transfer state aligncnt %d", pTask->id.idStr, numOfUpstream);
2,318✔
1000
  }
1001

1002
  return atomic_sub_fetch_32(&pTask->transferStateAlignCnt, 1);
6,913✔
1003
}
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