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

taosdata / TDengine / #3533

20 Nov 2024 07:11AM UTC coverage: 58.848% (-1.9%) from 60.78%
#3533

push

travis-ci

web-flow
Merge pull request #28823 from taosdata/fix/3.0/TD-32587

fix:[TD-32587]fix stmt segmentation fault

115578 of 252434 branches covered (45.79%)

Branch coverage included in aggregate %.

1 of 4 new or added lines in 1 file covered. (25.0%)

8038 existing lines in 233 files now uncovered.

194926 of 275199 relevant lines covered (70.83%)

1494459.59 hits per line

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

68.26
/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) {
257,931✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
257,931✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
257,933✔
33
}
34

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

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

65
  return code;
7,101✔
66
}
67

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

76
  SStreamDataBlock* pStreamBlocks = NULL;
4,452✔
77

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

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

93
  *totalSize += size;
4,452✔
94
  *totalBlocks += numOfBlocks;
4,452✔
95

96
  return code;
4,452✔
97
}
98

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

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

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

139
  *totalBlocks = 0;
9,456✔
140
  *totalSize = 0;
9,456✔
141

142
  while (1) {
7,664✔
143
    SSDataBlock* output = NULL;
17,120✔
144
    uint64_t     ts = 0;
17,120✔
145

146
    if (pRes == NULL) {
17,120✔
147
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
9,456✔
148
    }
149

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

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

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

170
    if (output == NULL) {
17,104✔
171
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
9,443✔
172
         code = doAppendPullOverBlock(pTask, &numOfBlocks, (SStreamDataBlock*) pItem, pRes);
294✔
173
         if (code) {
294!
174
           taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
175
           return code;
×
176
         }
177
      }
178

179
      break;
9,443✔
180
    }
181

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

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

201
    void* p = taosArrayPush(pRes, &block);
7,346✔
202
    if (p == NULL) {
7,346!
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,
7,346✔
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) {
7,346!
UNCOV
211
      code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
×
212
      // todo: here we need continue retry to put it into output buffer
UNCOV
213
      if (code != TSDB_CODE_SUCCESS) {
×
214
        return code;
×
215
      }
216

UNCOV
217
      pRes = NULL;
×
UNCOV
218
      size = 0;
×
UNCOV
219
      numOfBlocks = 0;
×
220
    }
221
  }
222

223
  if (numOfBlocks > 0) {
9,443✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
4,452✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
4,991✔
227
  }
228

229
  return code;
9,443✔
230
}
231

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

243
    code = doOutputResultBlockImpl(pTask, pStreamBlocks);
202✔
244
    if (code != TSDB_CODE_SUCCESS) {  // should not have error code
202!
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);
225✔
249
  }
250
  return code;
427✔
251
}
252

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

258
  while (1) {
447✔
259
    if (streamTaskShouldStop(pTask)) {
874!
260
      break;
×
261
    }
262

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

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

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

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

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

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

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

UNCOV
328
  while (1) {
×
329
    if (streamTaskShouldPause(pTask)) {
427!
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);
427✔
333
    }
334

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

356
    if (streamTaskShouldStop(pTask)) {
427!
UNCOV
357
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
UNCOV
358
      return buildScanhistoryExecRet(TASK_SCANHISTORY_QUIT, 0);
×
359
    }
360

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

367
    if (finished) {
427!
368
      return buildScanhistoryExecRet(TASK_SCANHISTORY_CONT, 0);
427✔
369
    }
370

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

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

384
  SStreamTask* pStreamTask = NULL;
459✔
385
  int32_t code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
459✔
386
  if (pStreamTask == NULL || code != TSDB_CODE_SUCCESS) {
459!
UNCOV
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.
UNCOV
394
    code = streamBuildAndSendDropTaskMsg(pTask->pMsgCb, pMeta->vgId, &pTask->id, 0);
×
395

396
    // 2. save to disk
UNCOV
397
    streamMetaWLock(pMeta);
×
UNCOV
398
    if (streamMetaCommit(pMeta) < 0) {
×
399
      // persist to disk
400
    }
UNCOV
401
    streamMetaWUnLock(pMeta);
×
UNCOV
402
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
×
403
  } else {
404
    double el = (taosGetTimestampMs() - pTask->execInfo.step2Start) / 1000.;
459✔
405
    stDebug(
459✔
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;
459✔
412
  STimeWindow* pTimeWindow = &pStreamTask->dataRange.window;
459✔
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) {
459✔
417
    if (!(status == TASK_STATUS__HALT || status == TASK_STATUS__DROPPING || status == TASK_STATUS__STOP)) {
419!
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 ||
40!
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);
40✔
428
    if (code != TSDB_CODE_SUCCESS) {
40!
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);
40✔
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);
459✔
442
  status = pState.state;
459✔
443
  char* p = pState.name;
459✔
444
  if (status == TASK_STATUS__STOP || status == TASK_STATUS__DROPPING) {
459!
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) {
459✔
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
419✔
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);
419✔
459
  } else {
460
    stDebug("s-task:%s no need to update/reset filter time window for non-source tasks", pStreamTask->id.idStr);
40✔
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);
459✔
466

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

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

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

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

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

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

513
  return code;
856✔
514
}
515

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

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

526
  } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
8,569✔
527
    const SStreamDataSubmit* pSubmit = (const SStreamDataSubmit*)pInput;
3,742✔
528
    code = qSetMultiStreamInput(pExecutor, &pSubmit->submit, 1, STREAM_INPUT__DATA_SUBMIT);
3,742✔
529
    stDebug("s-task:%s set submit blocks as source block completed, %p %p len:%d ver:%" PRId64, id, pSubmit,
3,742✔
530
            pSubmit->submit.msgStr, pSubmit->submit.msgLen, pSubmit->submit.ver);
531
    if ((*pVer) > pSubmit->submit.ver) {
3,742!
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;
3,742✔
536
    }
537
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK || pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
6,255✔
538
    const SStreamDataBlock* pBlock = (const SStreamDataBlock*)pInput;
1,428✔
539

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

545
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
3,399✔
546
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
2,256✔
547

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

554
    if ((*pVer) > pMerged->ver) {
2,256!
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;
2,256✔
559
    }
560

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

565
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT || pItem->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
934!
566
    const SStreamDataBlock* pCheckpoint = (const SStreamDataBlock*)pInput;
467✔
567
    code = qSetMultiStreamInput(pExecutor, pCheckpoint->blocks, 1, pItem->type);
467✔
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;
9,456✔
575
}
576

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

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

593
  // transfer the ownership of executor state
594
  if (type == TASK_OUTPUT__FIXED_DISPATCH || type == TASK_OUTPUT__SHUFFLE_DISPATCH) {
856✔
595
    if (level == TASK_LEVEL__SOURCE) {
452✔
596
      stDebug("s-task:%s add transfer-state block into outputQ", id);
412✔
597
    } else {
598
      stDebug("s-task:%s all upstream tasks send transfer-state block, add transfer-state block into outputQ", id);
40✔
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) {
452!
603
      pBlock->srcVgId = pTask->pMeta->vgId;
452✔
604
      code = taosWriteQitem(pTask->outputq.queue->pQueue, pBlock);
452✔
605
      if (code == 0) {
452!
606
        code = streamDispatchStreamBlock(pTask);
452✔
607
        if (code) {
452!
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);
404✔
618
    stDebug("s-task:%s non-dispatch task, level:%d start to transfer state directly", id, level);
404✔
619

620
    code = streamTransferStatePrepare(pTask);
404✔
621
    if (code != TSDB_CODE_SUCCESS) {
404!
UNCOV
622
      stError("s-task:%s failed to prepare transfer state, code:%s", id, tstrerror(code));
×
UNCOV
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; }
16,005✔
630

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

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

638
  pInfo->outputDataBlocks += totalBlocks;
9,453✔
639
  pInfo->outputDataSize += totalSize;
9,453✔
640
  if (fabs(el - 0.0) <= DBL_EPSILON) {
9,453✔
641
    pInfo->procsThroughput = 0;
2,815✔
642
    pInfo->outputThroughput = 0;
2,815✔
643
  } else {
644
    pInfo->outputThroughput = (totalSize / el);
6,638✔
645
    pInfo->procsThroughput = (blockSize / el);
6,638✔
646
  }
647
}
9,453✔
648

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

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

661
  code = doSetStreamInputBlock(pTask, pBlock, &ver, id);
9,456✔
662
  if (code) {
9,456!
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);
9,456✔
668
  if (code) {
9,456✔
669
    return code;
3✔
670
  }
671

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

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

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

688
  return code;
9,453✔
689
}
690

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

694
  // 1. transfer the ownership of executor state
695
  bool dropRelHTask = (streamTaskGetPrevStatus(pTask) == TASK_STATUS__HALT);
467✔
696
  if (dropRelHTask) {
467✔
697
    STaskId*     pHTaskId = &pTask->hTaskInfo.id;
430✔
698
    SStreamTask* pHTask = NULL;
430✔
699
    int32_t      code = streamMetaAcquireTask(pTask->pMeta, pHTaskId->streamId, pHTaskId->taskId, &pHTask);
430✔
700
    if (code == TSDB_CODE_SUCCESS) {  // ignore the error code.
430!
701
      code = streamTaskReleaseState(pHTask);
430✔
702
      if (code) {
430!
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) {
430!
707
        code = streamTaskReloadState(pTask);
430✔
708
        if (code) {
430!
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,
430✔
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);
430✔
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);
37✔
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);
467✔
733
  if (code) {
467!
734
    stError("s-task:%s failed to exec stream task before checkpoint, code:%s", id, tstrerror(code));
×
735
  }
736

737
  return code;
467✔
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) {
16,456✔
745
  const char* id = pTask->id.idStr;
16,456✔
746
  int32_t     code = 0;
16,456✔
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);
16,456✔
750

751
  while (1) {
13,935✔
752
    int32_t           blockSize = 0;
30,391✔
753
    int32_t           numOfBlocks = 0;
30,391✔
754
    SStreamQueueItem* pInput = NULL;
30,391✔
755

756
    if (streamTaskShouldStop(pTask) || (streamTaskGetStatus(pTask).state == TASK_STATUS__UNINIT)) {
30,391!
757
      stDebug("s-task:%s stream task is stopped", id);
11!
758
      return 0;
16,456✔
759
    }
760

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

767
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
30,380!
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) {
30,380✔
774
      stDebug("s-task:%s invoke exec too fast, idle and retry in 50ms", id);
3,370✔
775
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
3,370✔
776
      return 0;
3,370✔
777
    }
778

779
    EExtractDataCode ret = streamTaskGetDataFromInputQ(pTask, &pInput, &numOfBlocks, &blockSize);
27,010✔
780
    if (ret == EXEC_AFTER_IDLE) {
27,010!
781
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
×
782
      return 0;
×
783
    } else {
784
      if (pInput == NULL) {
27,010✔
785
        return 0;
12,629✔
786
      }
787
    }
788

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

792
    // dispatch checkpoint msg to all downstream tasks
793
    int32_t type = pInput->type;
14,381✔
794
    if (type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
14,381✔
795
      code = streamProcessCheckpointTriggerBlock(pTask, (SStreamDataBlock*)pInput);
1,252✔
796
      if (code != 0) {
1,252✔
797
        stError("s-task:%s failed to process checkpoint-trigger block, code:%s", pTask->id.idStr, tstrerror(code));
1!
798
      }
799
      continue;
4,949✔
800
    }
801

802
    if (type == STREAM_INPUT__TRANS_STATE) {
13,129✔
803
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
1,250✔
804
      continue;
1,250✔
805
    }
806

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

813
      int64_t st = taosGetTimestampMs();
2,447✔
814

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

824
        double el = (taosGetTimestampMs() - st) / 1000.0;
2,447✔
825
        if (fabs(el - 0.0) <= DBL_EPSILON) {
2,447✔
826
          pTask->execInfo.procsThroughput = 0;
1,567✔
827
        } else {
828
          pTask->execInfo.procsThroughput = (blockSize / el);
880✔
829
        }
830

831
        continue;
2,447✔
832
      }
833
    }
834

835
    if (type != STREAM_INPUT__CHECKPOINT) {
9,432✔
836
      code = doStreamTaskExecImpl(pTask, pInput, numOfBlocks);
8,989✔
837
      streamFreeQitem(pInput);
8,989✔
838
      if (code) {
8,989✔
839
        return code;
3✔
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);
443✔
844
      SStreamTaskState pState = streamTaskGetStatus(pTask);
443✔
845
      if (pState.state == TASK_STATUS__CK) {
443!
846
        stDebug("s-task:%s checkpoint block received, set status:%s", id, pState.name);
443✔
847
        code = streamTaskBuildCheckpoint(pTask);  // ignore this error msg, and continue
443✔
848
      } else {                                    // todo refactor
UNCOV
849
        if (pTask->info.taskLevel == TASK_LEVEL__SOURCE) {
×
UNCOV
850
          code = streamTaskSendCheckpointSourceRsp(pTask);
×
851
        } else {
852
          code = streamTaskSendCheckpointReadyMsg(pTask);
×
853
        }
854

UNCOV
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);
443✔
863
      streamFreeQitem(pInput);
443✔
864
      return code;
443✔
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) {
1,143✔
872
  ETaskStatus status = streamTaskGetStatus(pTask).state;
1,143✔
873
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE || status == TASK_STATUS__STOP ||
1,143!
874
          status == TASK_STATUS__DROPPING);
875
}
876

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

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

885
  // pause & halt will still run for sink tasks.
886
  if (streamTaskIsSinkTask(pTask)) {
15,818✔
887
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
1,752!
888
            st == TASK_STATUS__PAUSE || st == TASK_STATUS__HALT);
5,919!
889
  } else {
890
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
11,651✔
891
            st == TASK_STATUS__HALT);
892
  }
893
}
894

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

899
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
16,008!
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) {
448✔
905
    code = doStreamExecTask(pTask);
16,456✔
906
    if (code) {
16,456✔
907
      stError("s-task:%s failed to exec stream task, code:%s", id, tstrerror(code));
3!
908
      return code;
3✔
909
    }
910
    // check if continue
911
    streamMutexLock(&pTask->lock);
16,453✔
912

913
    int32_t numOfItems = streamQueueGetNumOfItems(pTask->inputq.queue);
16,453✔
914
    if ((numOfItems == 0) || streamTaskShouldStop(pTask) || streamTaskShouldPause(pTask)) {
16,453!
915
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
13,356✔
916
      streamTaskClearSchedIdleInfo(pTask);
13,356✔
917
      streamMutexUnlock(&pTask->lock);
13,356✔
918

919
      setLastExecTs(pTask, taosGetTimestampMs());
13,356✔
920

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

925
      return code;
13,356✔
926
    } else {
927
      // check if this task needs to be idle for a while
928
      if (pTask->status.schedIdleTime > 0) {
3,097✔
929
        streamTaskResumeInFuture(pTask);
2,649✔
930

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

937
    streamMutexUnlock(&pTask->lock);
448✔
938
  }
939

940
  return code;
941
}
942

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

948
  int8_t schedStatus = streamTaskSetSchedStatusActive(pTask);
13,369✔
949
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
13,369!
950
    code = streamResumeTask(pTask);
13,369✔
951
  } else {
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;
13,369✔
958
}
959

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

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

969
  return code;
430✔
970
}
971

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

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

981
  return code;
430✔
982
}
983

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

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