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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

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

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

70.75
/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) {
2,035,781✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
2,035,781✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
2,035,435✔
33
}
34

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

39
static int32_t doOutputResultBlockImpl(SStreamTask* pTask, SStreamDataBlock* pBlock) {
40,672✔
40
  int32_t code = 0;
40,672✔
41
  int32_t type = pTask->outputInfo.type;
40,672✔
42
  if (type == TASK_OUTPUT__TABLE) {
40,672✔
43
    pTask->outputInfo.tbSink.tbSinkFunc(pTask, pTask->outputInfo.tbSink.vnode, pBlock->blocks);
17,473✔
44
    destroyStreamDataBlock(pBlock);
17,475✔
45
  } else if (type == TASK_OUTPUT__SMA) {
23,199✔
46
    pTask->outputInfo.smaSink.smaSink(pTask->outputInfo.smaSink.vnode, pTask->outputInfo.smaSink.smaId, pBlock->blocks);
5✔
47
    destroyStreamDataBlock(pBlock);
5✔
48
  } else {
49
    if (type != TASK_OUTPUT__FIXED_DISPATCH && type != TASK_OUTPUT__SHUFFLE_DISPATCH) {
23,194!
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,194✔
55
    if (code != TSDB_CODE_SUCCESS) {
23,197!
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,197✔
63
  }
64

65
  return code;
40,678✔
66
}
67

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

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

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

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

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

96
  return code;
22,010✔
97
}
98

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

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

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

139
  *totalBlocks = 0;
51,518✔
140
  *totalSize = 0;
51,518✔
141

142
  while (1) {
75,355✔
143
    SSDataBlock* output = NULL;
126,873✔
144
    uint64_t     ts = 0;
126,873✔
145

146
    if (pRes == NULL) {
126,873✔
147
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
51,531✔
148
    }
149

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

155
    if ((code = qExecTask(pExecutor, &output, &ts)) < 0) {
126,880!
UNCOV
156
      if (code == TSDB_CODE_QRY_IN_EXEC) {
×
157
        qResetTaskInfoCode(pExecutor);
×
158
      }
159

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

170
    if (output == NULL) {
126,932✔
171
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
51,572✔
172
         code = doAppendPullOverBlock(pTask, &numOfBlocks, (SStreamDataBlock*) pItem, pRes);
536✔
173
         if (code) {
536!
174
           taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
175
           return code;
×
176
         }
177
      }
178

179
      break;
51,572✔
180
    }
181

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

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

201
    void* p = taosArrayPush(pRes, &block);
74,492✔
202
    if (p == NULL) {
74,492!
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,
74,492✔
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) {
74,492✔
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) {
3!
214
        return code;
×
215
      }
216

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

223
  if (numOfBlocks > 0) {
51,572✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
22,007✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
29,565✔
227
  }
228

229
  return code;
51,566✔
230
}
231

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

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

258
  while (1) {
76,825✔
259
    if (streamTaskShouldStop(pTask)) {
79,998!
260
      break;
×
261
    }
262

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

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

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

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

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

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

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

328
  while (1) {
602✔
329
    if (streamTaskShouldPause(pTask)) {
3,172!
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,571✔
333
    }
334

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

341
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
3,172!
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,172✔
347
    if (pRes == NULL) {
3,173!
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,173✔
354
    streamScanHistoryDataImpl(pTask, pRes, &size, &finished);
3,173✔
355

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

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

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

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

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

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

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

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

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

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

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

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

513
  return code;
4,732✔
514
}
515

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

521
  const SStreamQueueItem* pItem = pInput;
51,528✔
522
  if (pItem->type == STREAM_INPUT__GET_RES) {
51,528✔
523
    const SStreamTrigger* pTrigger = (const SStreamTrigger*)pInput;
2,589✔
524
    code = qSetMultiStreamInput(pExecutor, pTrigger->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
2,589✔
525

526
  } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
48,939✔
527
    const SStreamDataSubmit* pSubmit = (const SStreamDataSubmit*)pInput;
17,058✔
528
    code = qSetMultiStreamInput(pExecutor, &pSubmit->submit, 1, STREAM_INPUT__DATA_SUBMIT);
17,058✔
529
    stDebug("s-task:%s set submit blocks as source block completed, %p %p len:%d ver:%" PRId64, id, pSubmit,
17,063✔
530
            pSubmit->submit.msgStr, pSubmit->submit.msgLen, pSubmit->submit.ver);
531
    if ((*pVer) > pSubmit->submit.ver) {
17,063!
532
      stError("s-task:%s invalid recorded ver:%" PRId64 " greater than new block ver:%" PRId64 ", not update", id,
×
533
              *pVer, pSubmit->submit.ver);
534
    } else {
535
      (*pVer) = pSubmit->submit.ver;
17,063✔
536
    }
537
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK || pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
35,038✔
538
    const SStreamDataBlock* pBlock = (const SStreamDataBlock*)pInput;
3,134✔
539

540
    SArray* pBlockList = pBlock->blocks;
3,134✔
541
    int32_t numOfBlocks = taosArrayGetSize(pBlockList);
3,134✔
542
    stDebug("s-task:%s set sdata blocks as input num:%d, ver:%" PRId64, id, numOfBlocks, pBlock->sourceVer);
3,156✔
543
    code = qSetMultiStreamInput(pExecutor, pBlockList->pData, numOfBlocks, STREAM_INPUT__DATA_BLOCK);
3,156✔
544

545
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
28,747✔
546
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
22,640✔
547

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

554
    if ((*pVer) > pMerged->ver) {
22,632!
UNCOV
555
      stError("s-task:%s invalid recorded ver:%" PRId64 " greater than new block ver:%" PRId64 ", not update", id,
×
556
              *pVer, pMerged->ver);
557
    } else {
558
      (*pVer) = pMerged->ver;
22,632✔
559
    }
560

561
  } else if (pItem->type == STREAM_INPUT__REF_DATA_BLOCK) {
6,107✔
562
    const SStreamRefDataBlock* pRefBlock = (const SStreamRefDataBlock*)pInput;
2,599✔
563
    code = qSetMultiStreamInput(pExecutor, pRefBlock->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
2,599✔
564

565
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT || pItem->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
7,020!
566
    const SStreamDataBlock* pCheckpoint = (const SStreamDataBlock*)pInput;
3,508✔
567
    code = qSetMultiStreamInput(pExecutor, pCheckpoint->blocks, 1, pItem->type);
3,508✔
568

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

574
  return code;
51,531✔
575
}
576

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

584
  if (level == TASK_LEVEL__AGG || level == TASK_LEVEL__SINK) {
9,567✔
585
    int32_t remain = streamAlignTransferState(pTask);
7,207✔
586
    if (remain > 0) {
7,220✔
587
      streamFreeQitem((SStreamQueueItem*)pBlock);
4,848✔
588
      stDebug("s-task:%s receive upstream trans-state msg, not sent remain:%d", id, remain);
4,847✔
589
      return;
4,847✔
590
    }
591
  }
592

593
  // transfer the ownership of executor state
594
  if (type == TASK_OUTPUT__FIXED_DISPATCH || type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
4,732✔
595
    if (level == TASK_LEVEL__SOURCE) {
2,430✔
596
      stDebug("s-task:%s add transfer-state block into outputQ", id);
2,356✔
597
    } else {
598
      stDebug("s-task:%s all upstream tasks send transfer-state block, add transfer-state block into outputQ", id);
74✔
599
    }
600

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

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

628
// static void streamTaskSetIdleInfo(SStreamTask* pTask, int32_t idleTime) { pTask->status.schedIdleTime = idleTime; }
629
static void setLastExecTs(SStreamTask* pTask, int64_t ts) { pTask->status.lastExecTs = ts; }
93,604✔
630

631
static void doRecordThroughput(STaskExecStatisInfo* pInfo, int64_t totalBlocks, int64_t totalSize, int64_t blockSize,
51,595✔
632
                               double st, const char* id) {
633
  double el = (taosGetTimestampMs() - st) / 1000.0;
51,595✔
634

635
  stDebug("s-task:%s batch of input blocks exec end, elapsed time:%.2fs, result size:%.2fMiB, numOfBlocks:%" PRId64, id,
51,595✔
636
          el, SIZE_IN_MiB(totalSize), totalBlocks);
637

638
  pInfo->outputDataBlocks += totalBlocks;
51,596✔
639
  pInfo->outputDataSize += totalSize;
51,596✔
640
  if (fabs(el - 0.0) <= DBL_EPSILON) {
51,596✔
641
    pInfo->procsThroughput = 0;
22,356✔
642
    pInfo->outputThroughput = 0;
22,356✔
643
  } else {
644
    pInfo->outputThroughput = (totalSize / el);
29,240✔
645
    pInfo->procsThroughput = (blockSize / el);
29,240✔
646
  }
647
}
51,596✔
648

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

659
  stDebug("s-task:%s start to process batch blocks, num:%d, type:%s", id, num, streamQueueItemGetTypeStr(pBlock->type));
51,537✔
660

661
  code = doSetStreamInputBlock(pTask, pBlock, &ver, id);
51,537✔
662
  if (code) {
51,523!
663
    stError("s-task:%s failed to set input block, not exec for these blocks", id);
×
664
    return code;
×
665
  }
666

667
  code = streamTaskExecImpl(pTask, pBlock, &totalSize, &totalBlocks);
51,523✔
668
  if (code) {
51,594!
UNCOV
669
    return code;
×
670
  }
671

672
  doRecordThroughput(&pTask->execInfo, totalBlocks, totalSize, blockSize, st, pTask->id.idStr);
51,594✔
673

674
  // update the currentVer if processing the submit blocks.
675
  if (!(pInfo->checkpointVer <= pInfo->nextProcessVer && ver >= pInfo->checkpointVer)) {
51,596✔
676
    stError("s-task:%s invalid info, checkpointVer:%" PRId64 ", nextProcessVer:%" PRId64 " currentVer:%" PRId64, id,
2!
677
            pInfo->checkpointVer, pInfo->nextProcessVer, ver);
678
    return code;
×
679
  }
680

681
  if (ver != pInfo->processedVer) {
51,594✔
682
    stDebug("s-task:%s update processedVer(unsaved) from %" PRId64 " to %" PRId64 " nextProcessVer:%" PRId64
39,733✔
683
            " ckpt:%" PRId64,
684
            id, pInfo->processedVer, ver, pInfo->nextProcessVer, pInfo->checkpointVer);
685
    pInfo->processedVer = ver;
39,732✔
686
  }
687

688
  return code;
51,593✔
689
}
690

691
int32_t flushStateDataInExecutor(SStreamTask* pTask, SStreamQueueItem* pCheckpointBlock) {
3,507✔
692
  const char* id = pTask->id.idStr;
3,507✔
693

694
  // 1. transfer the ownership of executor state
695
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
3,507✔
696
  if (dropRelHTask) {
3,510✔
697
    STaskId*     pHTaskId = &pTask->hTaskInfo.id;
2,358✔
698
    SStreamTask* pHTask = NULL;
2,358✔
699
    int32_t      code = streamMetaAcquireTask(pTask->pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
2,358✔
700
    if (code == TSDB_CODE_SUCCESS) {  // ignore the error code.
2,362!
701
      code = streamTaskReleaseState(pHTask);
2,362✔
702
      if (code) {
2,360!
703
        stError("s-task:%s failed to release query state, code:%s", pHTask->id.idStr, tstrerror(code));
×
704
      }
705

706
      if (code == TSDB_CODE_SUCCESS) {
2,360!
707
        code = streamTaskReloadState(pTask);
2,360✔
708
        if (code) {
2,362!
709
          stError("s-task:%s failed to reload query state, code:%s", pTask->id.idStr, tstrerror(code));
×
710
        }
711
      }
712

713
      stDebug("s-task:%s transfer state from fill-history task:%s, status:%s completed", id, pHTask->id.idStr,
2,362✔
714
              streamTaskGetStatus(pHTask).name);
715
      // todo execute qExecTask to fetch the reload-generated result, if this is stream is for session window query.
716
      /*
717
       * while(1) {
718
       * qExecTask()
719
       * }
720
       * // put into the output queue.
721
       */
722
      streamMetaReleaseTask(pTask->pMeta, pHTask);
2,362✔
723
    } else {
724
      stError("s-task:%s related fill-history task:0x%x failed to acquire, transfer state failed", id,
×
725
              (int32_t)pHTaskId->taskId);
726
    }
727
  } else {
728
    stDebug("s-task:%s no transfer-state needed", id);
1,152✔
729
  }
730

731
  // 2. flush data in executor to K/V store, which should be completed before do checkpoint in the K/V.
732
  int32_t code = doStreamTaskExecImpl(pTask, pCheckpointBlock, 1);
3,514✔
733
  if (code) {
3,514!
734
    stError("s-task:%s failed to exec stream task before checkpoint, code:%s", id, tstrerror(code));
×
735
  }
736

737
  return code;
3,514✔
738
}
739

740
/**
741
 * todo: the batch of blocks should be tuned dynamic, according to the total elapsed time of each batch of blocks, the
742
 * appropriate batch of blocks should be handled in 5 to 10 sec.
743
 */
744
static int32_t doStreamExecTask(SStreamTask* pTask) {
96,942✔
745
  const char* id = pTask->id.idStr;
96,942✔
746
  int32_t     code = 0;
96,942✔
747

748
  // merge multiple input data if possible in the input queue.
749
  stDebug("s-task:%s start to extract data block from inputQ", id);
96,942✔
750

751
  while (1) {
86,235✔
752
    int32_t           blockSize = 0;
183,256✔
753
    int32_t           numOfBlocks = 0;
183,256✔
754
    SStreamQueueItem* pInput = NULL;
183,256✔
755

756
    if (streamTaskShouldStop(pTask) || (streamTaskGetStatus(pTask).state == TASK_STATUS__UNINIT)) {
183,256✔
757
      stDebug("s-task:%s stream task is stopped", id);
38✔
758
      return 0;
97,069✔
759
    }
760

761
    if (streamQueueIsFull(pTask->outputq.queue)) {
183,215✔
762
      stTrace("s-task:%s outputQ is full, idle for 500ms and retry", id);
7!
763
      streamTaskSetIdleInfo(pTask, 1000);
7✔
764
      return 0;
×
765
    }
766

767
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
183,289!
768
      stTrace("s-task:%s downstream task inputQ blocked, idle for 1sec and retry", id);
×
769
      streamTaskSetIdleInfo(pTask, 1000);
×
770
      return 0;
×
771
    }
772

773
    if (taosGetTimestampMs() - pTask->status.lastExecTs < MIN_INVOKE_INTERVAL) {
183,186✔
774
      stDebug("s-task:%s invoke exec too fast, idle and retry in 50ms", id);
17,325✔
775
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
17,325✔
776
      return 0;
17,323✔
777
    }
778

779
    EExtractDataCode ret = streamTaskGetDataFromInputQ(pTask, &pInput, &numOfBlocks, &blockSize);
165,861✔
780
    if (ret == EXEC_AFTER_IDLE) {
165,888!
781
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
×
782
      return 0;
×
783
    } else {
784
      if (pInput == NULL) {
165,911✔
785
        return 0;
76,269✔
786
      }
787
    }
788

789
    pTask->execInfo.inputDataBlocks += numOfBlocks;
89,642✔
790
    pTask->execInfo.inputDataSize += blockSize;
89,642✔
791

792
    // dispatch checkpoint msg to all downstream tasks
793
    int32_t type = pInput->type;
89,642✔
794
    if (type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
89,642✔
795
      code = streamProcessCheckpointTriggerBlock(pTask, (SStreamDataBlock*)pInput);
11,886✔
796
      if (code != 0) {
11,889!
797
        stError("s-task:%s failed to process checkpoint-trigger block, code:%s", pTask->id.idStr, tstrerror(code));
×
798
      }
799
      continue;
38,154✔
800
    }
801

802
    if (type == STREAM_INPUT__TRANS_STATE) {
77,756✔
803
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
9,569✔
804
      continue;
9,576✔
805
    }
806

807
    if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
68,187✔
808
      if (type != STREAM_INPUT__DATA_BLOCK && type != STREAM_INPUT__CHECKPOINT) {
16,686!
809
        stError("s-task:%s invalid block type:%d for sink task, discard", id, type);
×
810
        continue;
×
811
      }
812

813
      int64_t st = taosGetTimestampMs();
16,688✔
814

815
      // here only handle the data block sink operation
816
      if (type == STREAM_INPUT__DATA_BLOCK) {
16,688!
817
        pTask->execInfo.sink.dataSize += blockSize;
16,691✔
818
        stDebug("s-task:%s sink task start to sink %d blocks, size:%.2fKiB", id, numOfBlocks, SIZE_IN_KiB(blockSize));
16,691✔
819
        code = doOutputResultBlockImpl(pTask, (SStreamDataBlock*)pInput);
16,691✔
820
        if (code != TSDB_CODE_SUCCESS) {
16,690!
821
          return code;
×
822
        }
823

824
        double el = (taosGetTimestampMs() - st) / 1000.0;
16,691✔
825
        if (fabs(el - 0.0) <= DBL_EPSILON) {
16,691✔
826
          pTask->execInfo.procsThroughput = 0;
9,679✔
827
        } else {
828
          pTask->execInfo.procsThroughput = (blockSize / el);
7,012✔
829
        }
830

831
        continue;
16,691✔
832
      }
833
    }
834

835
    if (type != STREAM_INPUT__CHECKPOINT) {
51,498✔
836
      code = doStreamTaskExecImpl(pTask, pInput, numOfBlocks);
48,017✔
837
      streamFreeQitem(pInput);
48,079✔
838
      if (code) {
48,081!
UNCOV
839
        return code;
×
840
      }
841
    } else {  // todo other thread may change the status
842
      // do nothing after sync executor state to storage backend, untill the vnode-level checkpoint is completed.
843
      streamMutexLock(&pTask->lock);
3,481✔
844
      SStreamTaskState pState = streamTaskGetStatus(pTask);
3,448✔
845
      if (pState.state == TASK_STATUS__CK) {
3,449!
846
        stDebug("s-task:%s checkpoint block received, set status:%s", id, pState.name);
3,449✔
847
        code = streamTaskBuildCheckpoint(pTask);  // ignore this error msg, and continue
3,449✔
848
      } else {                                    // todo refactor
849
        if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
850
          code = streamTaskSendCheckpointSourceRsp(pTask);
×
851
        } else {
852
          code = streamTaskSendCheckpointReadyMsg(pTask);
×
853
        }
854

855
        if (code != TSDB_CODE_SUCCESS) {
×
856
          // todo: let's retry send rsp to upstream/mnode
857
          stError("s-task:%s failed to send checkpoint rsp to upstream, checkpointId:%d, code:%s", id, 0,
×
858
                  tstrerror(code));
859
        }
860
      }
861

862
      streamMutexUnlock(&pTask->lock);
3,452✔
863
      streamFreeQitem(pInput);
3,452✔
864
      return code;
3,442✔
865
    }
866
  }
867
}
868

869
// the task may be set dropping/stopping, while it is still in the task queue, therefore, the sched-status can not
870
// be updated by tryExec function, therefore, the schedStatus will always be the TASK_SCHED_STATUS__WAITING.
871
bool streamTaskIsIdle(const SStreamTask* pTask) {
2,688✔
872
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,688✔
873
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE || status == TASK_STATUS__STOP ||
2,688!
874
          status == TASK_STATUS__DROPPING);
875
}
876

877
bool streamTaskReadyToRun(const SStreamTask* pTask, char** pStatus) {
92,687✔
878
  SStreamTaskState pState = streamTaskGetStatus(pTask);
92,687✔
879

880
  ETaskStatus st = pState.state;
92,721✔
881
  if (pStatus != NULL) {
92,721!
882
    *pStatus = pState.name;
92,742✔
883
  }
884

885
  // pause & halt will still run for sink tasks.
886
  if (streamTaskIsSinkTask(pTask)) {
92,721✔
887
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
14,558✔
888
            st == TASK_STATUS__PAUSE || st == TASK_STATUS__HALT);
48,726!
889
  } else {
890
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
58,517✔
891
            st == TASK_STATUS__HALT);
892
  }
893
}
894

895
int32_t streamResumeTask(SStreamTask* pTask) {
93,494✔
896
  const char* id = pTask->id.idStr;
93,494✔
897
  int32_t     code = 0;
93,494✔
898

899
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
93,494!
900
    stError("s-task:%s invalid sched status:%d, not resume task", pTask->id.idStr, pTask->status.schedStatus);
×
901
    return code;
×
902
  }
903

904
  while (1) {
3,473✔
905
    code = doStreamExecTask(pTask);
96,967✔
906
    if (code) {
97,070!
UNCOV
907
      stError("s-task:%s failed to exec stream task, code:%s", id, tstrerror(code));
×
UNCOV
908
      return code;
×
909
    }
910
    // check if continue
911
    streamMutexLock(&pTask->lock);
97,070✔
912

913
    int32_t numOfItems = streamQueueGetNumOfItems(pTask->inputq.queue);
97,107✔
914
    if ((numOfItems == 0) || streamTaskShouldStop(pTask) || streamTaskShouldPause(pTask)) {
97,107✔
915
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
78,413✔
916
      streamTaskClearSchedIdleInfo(pTask);
78,414✔
917
      streamMutexUnlock(&pTask->lock);
78,392✔
918

919
      setLastExecTs(pTask, taosGetTimestampMs());
78,395✔
920

921
      char* p = streamTaskGetStatus(pTask).name;
78,364✔
922
      stDebug("s-task:%s exec completed, status:%s, sched-status:%d, lastExecTs:%" PRId64, id, p,
78,376✔
923
              pTask->status.schedStatus, pTask->status.lastExecTs);
924

925
      return code;
78,377✔
926
    } else {
927
      // check if this task needs to be idle for a while
928
      if (pTask->status.schedIdleTime > 0) {
18,687✔
929
        streamTaskResumeInFuture(pTask);
15,214✔
930

931
        streamMutexUnlock(&pTask->lock);
15,232✔
932
        setLastExecTs(pTask, taosGetTimestampMs());
15,232✔
933
        return code;
15,232✔
934
      }
935
    }
936

937
    streamMutexUnlock(&pTask->lock);
3,473✔
938
  }
939

940
  return code;
941
}
942

943
int32_t streamExecTask(SStreamTask* pTask) {
78,339✔
944
  // this function may be executed by multi-threads, so status check is required.
945
  const char* id = pTask->id.idStr;
78,339✔
946
  int32_t     code = 0;
78,339✔
947

948
  int8_t schedStatus = streamTaskSetSchedStatusActive(pTask);
78,339✔
949
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
78,410!
950
    code = streamResumeTask(pTask);
78,411✔
951
  } else {
UNCOV
952
    char* p = streamTaskGetStatus(pTask).name;
×
953
    stDebug("s-task:%s already started to exec by other thread, status:%s, sched-status:%d", id, p,
×
954
            pTask->status.schedStatus);
955
  }
956

957
  return code;
78,414✔
958
}
959

960
int32_t streamTaskReleaseState(SStreamTask* pTask) {
2,361✔
961
  stDebug("s-task:%s release exec state", pTask->id.idStr);
2,361✔
962
  void* pExecutor = pTask->exec.pExecutor;
2,361✔
963

964
  int32_t code = TSDB_CODE_SUCCESS;
2,361✔
965
  if (pExecutor != NULL) {
2,361!
966
    code = qStreamOperatorReleaseState(pExecutor);
2,361✔
967
  }
968

969
  return code;
2,361✔
970
}
971

972
int32_t streamTaskReloadState(SStreamTask* pTask) {
2,360✔
973
  stDebug("s-task:%s reload exec state", pTask->id.idStr);
2,360✔
974
  void* pExecutor = pTask->exec.pExecutor;
2,360✔
975

976
  int32_t code = TSDB_CODE_SUCCESS;
2,360✔
977
  if (pExecutor != NULL) {
2,360!
978
    code = qStreamOperatorReloadState(pExecutor);
2,361✔
979
  }
980

981
  return code;
2,362✔
982
}
983

984
int32_t streamAlignTransferState(SStreamTask* pTask) {
7,197✔
985
  int32_t numOfUpstream = taosArrayGetSize(pTask->upstreamInfo.pList);
7,197✔
986
  int32_t old = atomic_val_compare_exchange_32(&pTask->transferStateAlignCnt, 0, numOfUpstream);
7,204✔
987
  if (old == 0) {
7,219✔
988
    stDebug("s-task:%s set the transfer state aligncnt %d", pTask->id.idStr, numOfUpstream);
2,410✔
989
  }
990

991
  return atomic_sub_fetch_32(&pTask->transferStateAlignCnt, 1);
7,219✔
992
}
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