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

taosdata / TDengine / #3532

20 Nov 2024 07:11AM UTC coverage: 60.78% (+0.6%) from 60.213%
#3532

push

travis-ci

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

fix:[TD-32587]fix stmt segmentation fault

119943 of 252352 branches covered (47.53%)

Branch coverage included in aggregate %.

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

463 existing lines in 99 files now uncovered.

200682 of 275165 relevant lines covered (72.93%)

15642683.31 hits per line

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

72.04
/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,097,273✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
2,097,273✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
2,097,086✔
33
}
34

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

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

65
  return code;
44,637✔
66
}
67

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

76
  SStreamDataBlock* pStreamBlocks = NULL;
24,288✔
77

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

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

93
  *totalSize += size;
24,288✔
94
  *totalBlocks += numOfBlocks;
24,288✔
95

96
  return code;
24,288✔
97
}
98

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

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

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

139
  *totalBlocks = 0;
53,909✔
140
  *totalSize = 0;
53,909✔
141

142
  while (1) {
84,455✔
143
    SSDataBlock* output = NULL;
138,364✔
144
    uint64_t     ts = 0;
138,364✔
145

146
    if (pRes == NULL) {
138,364✔
147
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
53,922✔
148
    }
149

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

155
    if ((code = qExecTask(pExecutor, &output, &ts)) < 0) {
138,369!
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) {
×
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 {
UNCOV
165
        qResetTaskCode(pExecutor);
×
166
        continue;
899✔
167
      }
168
    }
169

170
    if (output == NULL) {
138,415✔
171
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
53,954✔
172
         code = doAppendPullOverBlock(pTask, &numOfBlocks, (SStreamDataBlock*) pItem, pRes);
541✔
173
         if (code) {
541!
174
           taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
175
           return code;
×
176
         }
177
      }
178

179
      break;
53,954✔
180
    }
181

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

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

201
    void* p = taosArrayPush(pRes, &block);
83,556✔
202
    if (p == NULL) {
83,556!
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,
83,556✔
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) {
83,556✔
211
      code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
3✔
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) {
53,954✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
24,285✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
29,669✔
227
  }
228

229
  return code;
53,952✔
230
}
231

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

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

258
  while (1) {
66,881✔
259
    if (streamTaskShouldStop(pTask)) {
70,011!
260
      break;
×
261
    }
262

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

384
  SStreamTask* pStreamTask = NULL;
2,436✔
385
  int32_t code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
2,436✔
386
  if (pStreamTask == NULL || code != TSDB_CODE_SUCCESS) {
2,436!
387
    stError(
3!
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);
3✔
395

396
    // 2. save to disk
397
    streamMetaWLock(pMeta);
3✔
398
    if (streamMetaCommit(pMeta) < 0) {
3✔
399
      // persist to disk
400
    }
401
    streamMetaWUnLock(pMeta);
3✔
402
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
3✔
403
  } else {
404
    double el = (taosGetTimestampMs() - pTask->execInfo.step2Start) / 1000.;
2,433✔
405
    stDebug(
2,433✔
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,433✔
412
  STimeWindow* pTimeWindow = &pStreamTask->dataRange.window;
2,433✔
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,433✔
417
    if (!(status == TASK_STATUS__HALT || status == TASK_STATUS__DROPPING || status == TASK_STATUS__STOP)) {
2,361!
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,433✔
442
  status = pState.state;
2,433✔
443
  char* p = pState.name;
2,433✔
444
  if (status == TASK_STATUS__STOP || status == TASK_STATUS__DROPPING) {
2,433!
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,433✔
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,361✔
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,361✔
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,433✔
466

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

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

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

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

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

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

513
  return code;
4,743✔
514
}
515

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

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

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

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

545
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
29,884✔
546
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
23,763✔
547

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

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

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

565
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT || pItem->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
6,996!
566
    const SStreamDataBlock* pCheckpoint = (const SStreamDataBlock*)pInput;
3,497✔
567
    code = qSetMultiStreamInput(pExecutor, pCheckpoint->blocks, 1, pItem->type);
3,497✔
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;
53,918✔
575
}
576

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

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

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

620
    code = streamTransferStatePrepare(pTask);
2,316✔
621
    if (code != TSDB_CODE_SUCCESS) {
2,314✔
622
      stError("s-task:%s failed to prepare transfer state, code:%s", id, tstrerror(code));
1!
623
      int8_t status = streamTaskSetSchedStatusInactive(pTask);  // let's ignore this return status
1✔
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; }
98,901✔
630

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

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

638
  pInfo->outputDataBlocks += totalBlocks;
53,974✔
639
  pInfo->outputDataSize += totalSize;
53,974✔
640
  if (fabs(el - 0.0) <= DBL_EPSILON) {
53,974✔
641
    pInfo->procsThroughput = 0;
23,420✔
642
    pInfo->outputThroughput = 0;
23,420✔
643
  } else {
644
    pInfo->outputThroughput = (totalSize / el);
30,554✔
645
    pInfo->procsThroughput = (blockSize / el);
30,554✔
646
  }
647
}
53,974✔
648

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

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

661
  code = doSetStreamInputBlock(pTask, pBlock, &ver, id);
53,946✔
662
  if (code) {
53,911!
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);
53,911✔
668
  if (code) {
53,974!
UNCOV
669
    return code;
×
670
  }
671

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

674
  // update the currentVer if processing the submit blocks.
675
  if (!(pInfo->checkpointVer <= pInfo->nextProcessVer && ver >= pInfo->checkpointVer)) {
53,974!
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) {
53,974✔
682
    stDebug("s-task:%s update processedVer(unsaved) from %" PRId64 " to %" PRId64 " nextProcessVer:%" PRId64
42,050✔
683
            " ckpt:%" PRId64,
684
            id, pInfo->processedVer, ver, pInfo->nextProcessVer, pInfo->checkpointVer);
685
    pInfo->processedVer = ver;
42,049✔
686
  }
687

688
  return code;
53,973✔
689
}
690

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

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

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

751
  while (1) {
90,372✔
752
    int32_t           blockSize = 0;
192,696✔
753
    int32_t           numOfBlocks = 0;
192,696✔
754
    SStreamQueueItem* pInput = NULL;
192,696✔
755

756
    if (streamTaskShouldStop(pTask) || (streamTaskGetStatus(pTask).state == TASK_STATUS__UNINIT)) {
192,696✔
757
      stDebug("s-task:%s stream task is stopped", id);
35✔
758
      return 0;
102,352✔
759
    }
760

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

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

779
    EExtractDataCode ret = streamTaskGetDataFromInputQ(pTask, &pInput, &numOfBlocks, &blockSize);
173,568✔
780
    if (ret == EXEC_AFTER_IDLE) {
173,585!
781
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
×
782
      return 0;
×
783
    } else {
784
      if (pInput == NULL) {
173,600✔
785
        return 0;
79,829✔
786
      }
787
    }
788

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

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

802
    if (type == STREAM_INPUT__TRANS_STATE) {
81,867✔
803
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
9,574✔
804
      continue;
9,578✔
805
    }
806

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

813
      int64_t st = taosGetTimestampMs();
18,408✔
814

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

824
        double el = (taosGetTimestampMs() - st) / 1000.0;
18,408✔
825
        if (fabs(el - 0.0) <= DBL_EPSILON) {
18,408✔
826
          pTask->execInfo.procsThroughput = 0;
10,823✔
827
        } else {
828
          pTask->execInfo.procsThroughput = (blockSize / el);
7,585✔
829
        }
830

831
        continue;
18,408✔
832
      }
833
    }
834

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

855
        if (code != TSDB_CODE_SUCCESS) {
1!
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,423✔
863
      streamFreeQitem(pInput);
3,423✔
864
      return code;
3,414✔
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,482✔
872
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,482✔
873
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE || status == TASK_STATUS__STOP ||
2,482!
874
          status == TASK_STATUS__DROPPING);
875
}
876

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

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

885
  // pause & halt will still run for sink tasks.
886
  if (streamTaskIsSinkTask(pTask)) {
98,023✔
887
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
14,545✔
888
            st == TASK_STATUS__PAUSE || st == TASK_STATUS__HALT);
50,813!
889
  } else {
890
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
61,757✔
891
            st == TASK_STATUS__HALT);
892
  }
893
}
894

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

899
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
98,871!
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,448✔
905
    code = doStreamExecTask(pTask);
102,319✔
906
    if (code) {
102,353!
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);
102,353✔
912

913
    int32_t numOfItems = streamQueueGetNumOfItems(pTask->inputq.queue);
102,392✔
914
    if ((numOfItems == 0) || streamTaskShouldStop(pTask) || streamTaskShouldPause(pTask)) {
102,393✔
915
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
81,810✔
916
      streamTaskClearSchedIdleInfo(pTask);
81,812✔
917
      streamMutexUnlock(&pTask->lock);
81,779✔
918

919
      setLastExecTs(pTask, taosGetTimestampMs());
81,801✔
920

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

925
      return code;
81,789✔
926
    } else {
927
      // check if this task needs to be idle for a while
928
      if (pTask->status.schedIdleTime > 0) {
20,575✔
929
        streamTaskResumeInFuture(pTask);
17,127✔
930

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

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

940
  return code;
941
}
942

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

948
  int8_t schedStatus = streamTaskSetSchedStatusActive(pTask);
81,755✔
949
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
81,847!
950
    code = streamResumeTask(pTask);
81,847✔
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;
81,830✔
958
}
959

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

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

969
  return code;
2,389✔
970
}
971

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

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

981
  return code;
2,388✔
982
}
983

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

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