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

taosdata / TDengine / #3549

06 Dec 2024 09:44AM UTC coverage: 59.948% (+0.1%) from 59.846%
#3549

push

travis-ci

web-flow
Merge pull request #29057 from taosdata/docs/TD-33031-3.0

docs: description of user privileges

118833 of 254191 branches covered (46.75%)

Branch coverage included in aggregate %.

199893 of 277480 relevant lines covered (72.04%)

19006119.35 hits per line

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

73.03
/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,075,948✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
2,075,948✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
2,075,720✔
33
}
34

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

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

65
  return code;
41,506✔
66
}
67

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

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

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

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

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

96
  return code;
22,909✔
97
}
98

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

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

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

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

142
  while (1) {
83,080✔
143
    SSDataBlock* output = NULL;
136,450✔
144
    uint64_t     ts = 0;
136,450✔
145

146
    if (pRes == NULL) {
136,450✔
147
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
53,383✔
148
    }
149

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

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

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

170
    if (output == NULL) {
136,512✔
171
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
53,428✔
172
         code = doAppendPullOverBlock(pTask, &numOfBlocks, (SStreamDataBlock*) pItem, pRes);
551✔
173
         if (code) {
551✔
174
           taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
1✔
175
           return code;
×
176
         }
177
      }
178

179
      break;
53,427✔
180
    }
181

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

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

201
    void* p = taosArrayPush(pRes, &block);
82,186✔
202
    if (p == NULL) {
82,186!
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,
82,186✔
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) {
82,186✔
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,427✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
22,906✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
30,521✔
227
  }
228

229
  return code;
53,425✔
230
}
231

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

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

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

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

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

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

300
    if (numOfBlocks >= STREAM_RESULT_DUMP_THRESHOLD || (*pSize) >= STREAM_RESULT_DUMP_SIZE_THRESHOLD) {
67,660✔
301
      stDebug("s-task:%s scan exec numOfBlocks:%d, size:%.2fKiB output num-limit:%d, size-limit:%.2fKiB reached",
756!
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,140✔
308

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

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

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

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

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

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

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

367
    if (finished) {
3,131✔
368
      return buildScanhistoryExecRet(TASK_SCANHISTORY_CONT, 0);
2,375✔
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,
181!
374
              pTask->info.fillHistory, el / 1000.0);
375
      return buildScanhistoryExecRet(TASK_SCANHISTORY_REXEC, 100);
181✔
376
    }
377
  }
378
}
379

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

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

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

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

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

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

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

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

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

513
  return code;
4,725✔
514
}
515

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

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

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

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

545
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
30,109✔
546
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
24,033✔
547

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

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

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

565
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT || pItem->type == STREAM_INPUT__CHECKPOINT_TRIGGER) {
6,931!
566
    const SStreamDataBlock* pCheckpoint = (const SStreamDataBlock*)pInput;
3,463✔
567
    code = qSetMultiStreamInput(pExecutor, pCheckpoint->blocks, 1, pItem->type);
3,463✔
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,381✔
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,208✔
586
    if (remain > 0) {
7,212✔
587
      streamFreeQitem((SStreamQueueItem*)pBlock);
4,840✔
588
      stDebug("s-task:%s receive upstream trans-state msg, not sent remain:%d", id, remain);
4,838✔
589
      return;
4,839✔
590
    }
591
  }
592

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

620
    code = streamTransferStatePrepare(pTask);
2,309✔
621
    if (code != TSDB_CODE_SUCCESS) {
2,307!
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,707✔
630

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

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

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

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

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

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

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

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

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

688
  return code;
53,444✔
689
}
690

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

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

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

751
  while (1) {
87,947✔
752
    int32_t           blockSize = 0;
185,052✔
753
    int32_t           numOfBlocks = 0;
185,052✔
754
    SStreamQueueItem* pInput = NULL;
185,052✔
755

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

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

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

779
    EExtractDataCode ret = streamTaskGetDataFromInputQ(pTask, &pInput, &numOfBlocks, &blockSize);
167,999✔
780
    if (ret == EXEC_AFTER_IDLE) {
167,964!
781
      streamTaskSetIdleInfo(pTask, MIN_INVOKE_INTERVAL);
×
782
      return 0;
×
783
    } else {
784
      if (pInput == NULL) {
167,992✔
785
        return 0;
76,682✔
786
      }
787
    }
788

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

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

802
    if (type == STREAM_INPUT__TRANS_STATE) {
79,577✔
803
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
9,567✔
804
      continue;
9,568✔
805
    }
806

807
    if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
70,010✔
808
      if (type != STREAM_INPUT__DATA_BLOCK && type != STREAM_INPUT__CHECKPOINT) {
16,650!
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,649✔
814

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

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

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

835
    if (type != STREAM_INPUT__CHECKPOINT) {
53,360✔
836
      code = doStreamTaskExecImpl(pTask, pInput, numOfBlocks);
49,930✔
837
      streamFreeQitem(pInput);
49,978✔
838
      if (code) {
49,980✔
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);
3,430✔
844
      SStreamTaskState pState = streamTaskGetStatus(pTask);
3,404✔
845
      if (pState.state == TASK_STATUS__CK) {
3,404!
846
        stDebug("s-task:%s checkpoint block received, set status:%s", id, pState.name);
3,404✔
847
        code = streamTaskBuildCheckpoint(pTask);  // ignore this error msg, and continue
3,404✔
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,404✔
863
      streamFreeQitem(pInput);
3,404✔
864
      return code;
3,392✔
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,695✔
872
  ETaskStatus status = streamTaskGetStatus(pTask).state;
2,695✔
873
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE || status == TASK_STATUS__STOP ||
2,697!
874
          status == TASK_STATUS__DROPPING);
875
}
876

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

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

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

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

899
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
93,655!
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,431✔
905
    code = doStreamExecTask(pTask);
97,086✔
906
    if (code) {
97,151✔
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);
97,148✔
912

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

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

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

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

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

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

940
  return code;
941
}
942

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

948
  int8_t schedStatus = streamTaskSetSchedStatusActive(pTask);
78,473✔
949
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
78,577!
950
    code = streamResumeTask(pTask);
78,577✔
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;
78,548✔
958
}
959

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

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

969
  return code;
2,379✔
970
}
971

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

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

981
  return code;
2,379✔
982
}
983

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

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