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

taosdata / TDengine / #3608

12 Feb 2025 05:57AM UTC coverage: 63.066% (+1.4%) from 61.715%
#3608

push

travis-ci

web-flow
Merge pull request #29746 from taosdata/merge/mainto3.02

merge: from main to 3.0 branch

140199 of 286257 branches covered (48.98%)

Branch coverage included in aggregate %.

89 of 161 new or added lines in 18 files covered. (55.28%)

3211 existing lines in 190 files now uncovered.

218998 of 283298 relevant lines covered (77.3%)

5949310.66 hits per line

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

70.64
/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) {
689,418✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
689,418✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
689,407✔
33
}
34

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

39
static int32_t doOutputResultBlockImpl(SStreamTask* pTask, SStreamDataBlock* pBlock) {
15,452✔
40
  int32_t code = 0;
15,452✔
41
  int32_t type = pTask->outputInfo.type;
15,452✔
42
  if (type == TASK_OUTPUT__TABLE) {
15,452✔
43
    pTask->outputInfo.tbSink.tbSinkFunc(pTask, pTask->outputInfo.tbSink.vnode, pBlock->blocks);
7,385✔
44
    destroyStreamDataBlock(pBlock);
7,385✔
45
  } else if (type == TASK_OUTPUT__SMA) {
8,067✔
46
    pTask->outputInfo.smaSink.smaSink(pTask->outputInfo.smaSink.vnode, pTask->outputInfo.smaSink.smaId, pBlock->blocks);
3✔
47
    destroyStreamDataBlock(pBlock);
3✔
48
  } else {
49
    if (type != TASK_OUTPUT__FIXED_DISPATCH && type != TASK_OUTPUT__SHUFFLE_DISPATCH) {
8,064!
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);
8,064✔
55
    if (code != TSDB_CODE_SUCCESS) {
8,064!
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);
8,064✔
63
  }
64

65
  return code;
15,452✔
66
}
67

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

76
  SStreamDataBlock* pStreamBlocks = NULL;
7,705✔
77

78
  int32_t code = createStreamBlockFromResults(pItem, pTask, size, pRes, &pStreamBlocks);
7,705✔
79
  if (code) {
7,705!
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,
7,705✔
86
          SIZE_IN_MiB(size));
87

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

93
  *totalSize += size;
7,705✔
94
  *totalBlocks += numOfBlocks;
7,705✔
95

96
  return code;
7,705✔
97
}
98

99
static int32_t doAppendPullOverBlock(SStreamTask* pTask, int32_t* pNumOfBlocks, SStreamDataBlock* pRetrieveBlock,
448✔
100
                                     SArray* pRes) {
101
  SSDataBlock block = {0};
448✔
102
  int32_t     num = taosArrayGetSize(pRetrieveBlock->blocks);
448✔
103
  if (num != 1) {
448!
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);
448✔
109
  int32_t code = assignOneDataBlock(&block, p);
448✔
110
  if (code) {
448!
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;
448✔
116
  block.info.childId = pTask->info.selfChildId;
448✔
117

118
  p = taosArrayPush(pRes, &block);
448✔
119
  if (p != NULL) {
448!
120
    (*pNumOfBlocks) += 1;
448✔
121
    stDebug("s-task:%s(child %d) retrieve res from upstream completed, QID:0x%" PRIx64, pTask->id.idStr,
448!
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;
448✔
130
}
131

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

139
  *totalBlocks = 0;
20,197✔
140
  *totalSize = 0;
20,197✔
141

142
  while (1) {
12,130✔
143
    SSDataBlock* output = NULL;
32,327✔
144
    uint64_t     ts = 0;
32,327✔
145

146
    if (pRes == NULL) {
32,327✔
147
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
20,199✔
148
    }
149

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

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

160
      if (code == TSDB_CODE_OUT_OF_MEMORY || code == TSDB_CODE_INVALID_PARA || code == TSDB_CODE_FILE_CORRUPTED) {
10!
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);
10✔
166
        continue;
378✔
167
      }
168
    }
169

170
    if (output == NULL) {
32,306✔
171
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
20,186✔
172
         code = doAppendPullOverBlock(pTask, &numOfBlocks, (SStreamDataBlock*) pItem, pRes);
448✔
173
         if (code) {
448!
174
           taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
175
           return code;
×
176
         }
177
      }
178

179
      break;
20,186✔
180
    }
181

182
    if (output->info.type == STREAM_RETRIEVE) {
12,120✔
183
      if (streamBroadcastToUpTasks(pTask, output) < 0) {
127✔
184
        // TODO
185
      }
186
      continue;
127✔
187
    } else if (output->info.type == STREAM_CHECKPOINT) {
11,993✔
188
      continue;  // checkpoint block not dispatch to downstream tasks
241✔
189
    }
190

191
    SSDataBlock block = {.info.childId = pTask->info.selfChildId};
11,752✔
192
    code = assignOneDataBlock(&block, output);
11,752✔
193
    if (code) {
11,752!
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);
11,752✔
199
    numOfBlocks += 1;
11,752✔
200

201
    void* p = taosArrayPush(pRes, &block);
11,752✔
202
    if (p == NULL) {
11,752!
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,
11,752✔
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) {
11,752!
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) {
20,186✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
7,703✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
12,483✔
227
  }
228

229
  return code;
20,186✔
230
}
231

232
// todo contiuous try to create result blocks
233
static int32_t handleScanhistoryResultBlocks(SStreamTask* pTask, SArray* pRes, int32_t size) {
2,010✔
234
  int32_t code = TSDB_CODE_SUCCESS;
2,010✔
235
  if (taosArrayGetSize(pRes) > 0) {
2,010✔
236
    SStreamDataBlock* pStreamBlocks = NULL;
1,431✔
237
    code = createStreamBlockFromResults(NULL, pTask, size, pRes, &pStreamBlocks);
1,431✔
238
    if (code) {
1,431!
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,431✔
244
    if (code != TSDB_CODE_SUCCESS) {  // should not have error code
1,431!
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);
579✔
249
  }
250
  return code;
2,010✔
251
}
252

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

258
  while (1) {
5,323✔
259
    if (streamTaskShouldStop(pTask)) {
7,335!
260
      break;
×
261
    }
262

263
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
7,335!
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;
7,335✔
269
    uint64_t     ts = 0;
7,335✔
270
    code = qExecTask(exec, &output, &ts);
7,335✔
271
    if (code != TSDB_CODE_TSC_QUERY_KILLED && code != TSDB_CODE_SUCCESS) {  // if out of memory occurs, quit
7,335!
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) {
7,335✔
280
      (*pFinish) = qStreamScanhistoryFinished(exec);
1,455✔
281
      break;
1,455✔
282
    }
283

284
    SSDataBlock block = {0};
5,880✔
285
    code = assignOneDataBlock(&block, output);
5,880✔
286
    if (code) {
5,880!
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;
5,880✔
291
    void* p = taosArrayPush(pRes, &block);
5,880✔
292
    if (p == NULL) {
5,880!
293
      stError("s-task:%s failed to add computing results, the final res may be incorrect", pTask->id.idStr);
×
294
    }
295

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

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

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

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

318
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
1,633!
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) {
1,633✔
324
    int32_t code = qSetStreamOpOpen(exec);
1,455✔
325
    pTask->hTaskInfo.operatorOpen = true;
1,455✔
326
  }
327

328
  while (1) {
379✔
329
    if (streamTaskShouldPause(pTask)) {
2,012!
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);
1,633✔
333
    }
334

335
    // output queue is full, idle for 5 sec.
336
    if (streamQueueIsFull(pTask->outputq.queue)) {
2,012!
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) {
2,012!
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));
2,012✔
347
    if (pRes == NULL) {
2,012!
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;
2,012✔
354
    streamScanHistoryDataImpl(pTask, pRes, &size, &finished);
2,012✔
355

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

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

367
    if (finished) {
2,010✔
368
      return buildScanhistoryExecRet(TASK_SCANHISTORY_CONT, 0);
1,453✔
369
    }
370

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

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

384
  SStreamTask* pStreamTask = NULL;
1,445✔
385
  int32_t code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,445✔
386
  if (pStreamTask == NULL || code != TSDB_CODE_SUCCESS) {
1,445!
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.;
1,445✔
405
    stDebug(
1,445✔
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;
1,445✔
412
  STimeWindow* pTimeWindow = &pStreamTask->dataRange.window;
1,445✔
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) {
1,445✔
417
    if (!(status == TASK_STATUS__HALT || status == TASK_STATUS__DROPPING || status == TASK_STATUS__STOP)) {
1,402!
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 ||
43!
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);
43✔
428
    if (code != TSDB_CODE_SUCCESS) {
43!
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);
43✔
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);
1,445✔
442
  status = pState.state;
1,445✔
443
  char* p = pState.name;
1,445✔
444
  if (status == TASK_STATUS__STOP || status == TASK_STATUS__DROPPING) {
1,445!
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) {
1,445✔
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
1,402✔
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);
1,402✔
459
  } else {
460
    stDebug("s-task:%s no need to update/reset filter time window for non-source tasks", pStreamTask->id.idStr);
43✔
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);
1,445✔
466

467
  // 3. the default task status should be ready or something, not halt.
468
  // status to the value that will be kept in disk
469

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

473
  streamMetaReleaseTask(pMeta, pStreamTask);
1,445✔
474
  return code;
1,445✔
475
}
476

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

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

486
  if (pTask->status.appendTranstateBlock != 1) {
2,785!
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;
2,785✔
492
  if (level == TASK_LEVEL__AGG || level == TASK_LEVEL__SOURCE) {  // do transfer task operator states.
2,785✔
493
    code = streamTransferStateDoPrepare(pTask);
1,445✔
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;
1,340✔
497
    code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,340✔
498
    if (pStreamTask != NULL) {
1,340!
499
      // halt the related stream sink task
500
      code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, haltCallback, NULL);
1,340✔
501
      if (code != TSDB_CODE_SUCCESS) {
1,340!
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);
1,340✔
508
      }
509
      streamMetaReleaseTask(pMeta, pStreamTask);
1,340✔
510
    }
511
  }
512

513
  return code;
2,785✔
514
}
515

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

521
  const SStreamQueueItem* pItem = pInput;
20,197✔
522
  if (pItem->type == STREAM_INPUT__GET_RES) {
20,197✔
523
    const SStreamTrigger* pTrigger = (const SStreamTrigger*)pInput;
4,227✔
524
    code = qSetMultiStreamInput(pExecutor, pTrigger->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
4,227✔
525
    if (pTask->info.trigger == STREAM_TRIGGER_FORCE_WINDOW_CLOSE) {
4,227✔
526
      stDebug("s-task:%s set force_window_close as source block, skey:%"PRId64, id, pTrigger->pBlock->info.window.skey);
3,182!
527
      (*pVer) = pTrigger->pBlock->info.window.skey;
3,182✔
528
    }
529
  } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
15,970✔
530
    const SStreamDataSubmit* pSubmit = (const SStreamDataSubmit*)pInput;
4,791✔
531
    code = qSetMultiStreamInput(pExecutor, &pSubmit->submit, 1, STREAM_INPUT__DATA_SUBMIT);
4,791✔
532
    stDebug("s-task:%s set submit blocks as source block completed, %p %p len:%d ver:%" PRId64, id, pSubmit,
4,791✔
533
            pSubmit->submit.msgStr, pSubmit->submit.msgLen, pSubmit->submit.ver);
534
    if ((*pVer) > pSubmit->submit.ver) {
4,791!
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;
4,791✔
539
    }
540
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK || pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
12,963✔
541
    const SStreamDataBlock* pBlock = (const SStreamDataBlock*)pInput;
1,784✔
542

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

548
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
9,395✔
549
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
6,551✔
550

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

557
    if ((*pVer) > pMerged->ver) {
6,551!
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;
6,551✔
562
    }
563

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

568
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT || pItem->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
2,880!
569
    const SStreamDataBlock* pCheckpoint = (const SStreamDataBlock*)pInput;
1,440✔
570
    code = qSetMultiStreamInput(pExecutor, pCheckpoint->blocks, 1, pItem->type);
1,440✔
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;
20,197✔
578
}
579

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

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

596
  // transfer the ownership of executor state
597
  if (type == TASK_OUTPUT__FIXED_DISPATCH || type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
2,789✔
598
    if (level == TASK_LEVEL__SOURCE) {
1,432✔
599
      stDebug("s-task:%s add transfer-state block into outputQ", id);
1,386✔
600
    } else {
601
      stDebug("s-task:%s all upstream tasks send transfer-state block, add transfer-state block into outputQ", id);
46✔
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) {
1,432!
606
      pBlock->srcVgId = pTask->pMeta->vgId;
1,432✔
607
      code = taosWriteQitem(pTask->outputq.queue->pQueue, pBlock);
1,432✔
608
      if (code == 0) {
1,432!
609
        code = streamDispatchStreamBlock(pTask);
1,432✔
610
        if (code) {
1,432!
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);
1,357✔
621
    stDebug("s-task:%s non-dispatch task, level:%d start to transfer state directly", id, level);
1,357✔
622

623
    code = streamTransferStatePrepare(pTask);
1,357✔
624
    if (code != TSDB_CODE_SUCCESS) {
1,357!
UNCOV
625
      stError("s-task:%s failed to prepare transfer state, code:%s", id, tstrerror(code));
×
UNCOV
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; }
38,114✔
633

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

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

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

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

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

664
  code = doSetStreamInputBlock(pTask, pBlock, &ver, id);
20,197✔
665
  if (code) {
20,197!
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);
20,197✔
671
  if (code) {
20,197✔
672
    return code;
1✔
673
  }
674

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

677
  // update the currentVer if processing the submitted blocks.
678
  if (!(pInfo->checkpointVer <= pInfo->nextProcessVer && ver >= pInfo->checkpointVer)) {
20,196!
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) {
20,196✔
685
    stDebug("s-task:%s update processedVer(unsaved) from %" PRId64 " to %" PRId64 " nextProcessVer:%" PRId64
14,523✔
686
            " ckpt:%" PRId64,
687
            id, pInfo->processedVer, ver, pInfo->nextProcessVer, pInfo->checkpointVer);
688
    pInfo->processedVer = ver;
14,523✔
689
  }
690

691
  return code;
20,196✔
692
}
693

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

699
  streamMutexLock(&pTask->lock);
1,404✔
700
  SStreamTaskState pState = streamTaskGetStatus(pTask);
1,404✔
701
  if (pState.state == TASK_STATUS__CK) {  // todo other thread may change the status
1,404!
702
    stDebug("s-task:%s checkpoint block received, set status:%s", id, pState.name);
1,404✔
703
    code = streamTaskBuildCheckpoint(pTask);  // ignore this error msg, and continue
1,404✔
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);
1,404✔
719
  return code;
1,404✔
720
}
721

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

725
  // 1. transfer the ownership of executor state
726
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
1,440✔
727
  if (dropRelHTask) {
1,440✔
728
    STaskId*     pHTaskId = &pTask->hTaskInfo.id;
1,405✔
729
    SStreamTask* pHTask = NULL;
1,405✔
730
    int32_t      code = streamMetaAcquireTask(pTask->pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
1,405✔
731
    if (code == TSDB_CODE_SUCCESS) {  // ignore the error code.
1,405!
732
      code = streamTaskReleaseState(pHTask);
1,405✔
733
      if (code) {
1,405!
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) {
1,405!
738
        code = streamTaskReloadState(pTask);
1,405✔
739
        if (code) {
1,405!
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,
1,405✔
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);
1,405✔
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);
35✔
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);
1,440✔
764
  if (code) {
1,440!
765
    stError("s-task:%s failed to exec stream task before checkpoint, code:%s", id, tstrerror(code));
×
766
  }
767

768
  return code;
1,440✔
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) {
39,526✔
776
  const char* id = pTask->id.idStr;
39,526✔
777
  int32_t     code = 0;
39,526✔
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);
39,526✔
781

782
  while (1) {
36,752✔
783
    int32_t           blockSize = 0;
76,278✔
784
    int32_t           numOfBlocks = 0;
76,278✔
785
    SStreamQueueItem* pInput = NULL;
76,278✔
786

787
    if (streamTaskShouldStop(pTask) || (streamTaskGetStatus(pTask).state == TASK_STATUS__UNINIT)) {
76,278!
788
      stDebug("s-task:%s stream task is stopped", id);
10!
789
      return 0;
39,526✔
790
    }
791

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

798
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
76,268!
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) {
76,268✔
805
      stDebug("s-task:%s invoke exec too fast, idle and retry in 50ms", id);
6,126✔
806
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
6,126✔
807
      return 0;
6,126✔
808
    }
809

810
    EExtractDataCode ret = streamTaskGetDataFromInputQ(pTask, &pInput, &numOfBlocks, &blockSize);
70,142✔
811
    if (ret == EXEC_AFTER_IDLE) {
70,142!
812
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
×
813
      return 0;
×
814
    } else {
815
      if (pInput == NULL) {
70,142✔
816
        return 0;
31,985✔
817
      }
818
    }
819

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

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

833
    if (type == STREAM_INPUT__TRANS_STATE) {
32,346✔
834
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
5,869✔
835
      continue;
5,869✔
836
    }
837

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

844
      int64_t st = taosGetTimestampMs();
6,316✔
845

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

855
        double el = (taosGetTimestampMs() - st) / 1000.0;
6,316✔
856
        if (fabs(el - 0.0) <= DBL_EPSILON) {
6,316✔
857
          pTask->execInfo.procsThroughput = 0;
3,063✔
858
        } else {
859
          pTask->execInfo.procsThroughput = (blockSize / el);
3,253✔
860
        }
861

862
        continue;
6,316✔
863
      }
864
    }
865

866
    if (type == STREAM_INPUT__CHECKPOINT) {
20,161✔
867
      code = doHandleChkptBlock(pTask);
1,404✔
868
      streamFreeQitem(pInput);
1,404✔
869
      return code;
1,404✔
870
    } else {
871
      code = doStreamTaskExecImpl(pTask, pInput, numOfBlocks);
18,757✔
872
      streamFreeQitem(pInput);
18,757✔
873
      if (code) {
18,757✔
874
        return code;
1✔
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,527✔
883
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,527✔
884
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE || status == TASK_STATUS__STOP ||
2,527!
885
          status == TASK_STATUS__DROPPING);
886
}
887

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

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

896
  // pause & halt will still run for sink tasks.
897
  if (streamTaskIsSinkTask(pTask)) {
37,304✔
898
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
8,376!
899
            st == TASK_STATUS__PAUSE || st == TASK_STATUS__HALT);
22,227!
900
  } else {
901
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
23,453✔
902
            st == TASK_STATUS__HALT);
903
  }
904
}
905

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

910
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
38,114!
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) {
1,412✔
916
    code = doStreamExecTask(pTask);
39,526✔
917
    if (code) {
39,526✔
918
      stError("s-task:%s failed to exec stream task, code:%s, continue", id, tstrerror(code));
1!
919
    }
920
    // check if continue
921
    streamMutexLock(&pTask->lock);
39,526✔
922

923
    int32_t numOfItems = streamQueueGetNumOfItems(pTask->inputq.queue);
39,526✔
924
    if ((numOfItems == 0) || streamTaskShouldStop(pTask) || streamTaskShouldPause(pTask)) {
39,526!
925
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
34,242✔
926
      streamTaskClearSchedIdleInfo(pTask);
34,242✔
927
      streamMutexUnlock(&pTask->lock);
34,242✔
928

929
      setLastExecTs(pTask, taosGetTimestampMs());
34,242✔
930

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

935
      return code;
34,242✔
936
    } else {
937
      // check if this task needs to be idle for a while
938
      if (pTask->status.schedIdleTime > 0) {
5,284✔
939
        streamTaskResumeInFuture(pTask);
3,872✔
940

941
        streamMutexUnlock(&pTask->lock);
3,872✔
942
        setLastExecTs(pTask, taosGetTimestampMs());
3,872✔
943
        return code;
3,872✔
944
      }
945
    }
946

947
    streamMutexUnlock(&pTask->lock);
1,412✔
948
  }
949

950
  return code;
951
}
952

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

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

967
  return code;
34,280✔
968
}
969

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

974
  int32_t code = TSDB_CODE_SUCCESS;
1,405✔
975
  if (pExecutor != NULL) {
1,405!
976
    code = qStreamOperatorReleaseState(pExecutor);
1,405✔
977
  }
978

979
  return code;
1,405✔
980
}
981

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

986
  int32_t code = TSDB_CODE_SUCCESS;
1,405✔
987
  if (pExecutor != NULL) {
1,405!
988
    code = qStreamOperatorReloadState(pExecutor);
1,405✔
989
  }
990

991
  return code;
1,405✔
992
}
993

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

1001
  return atomic_sub_fetch_32(&pTask->transferStateAlignCnt, 1);
4,466✔
1002
}
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