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

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

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

69.35
/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) {
740,341✔
31
  SStreamTaskState pState = streamTaskGetStatus(pTask);
740,341✔
32
  return (pState.state == TASK_STATUS__STOP) || (pState.state == TASK_STATUS__DROPPING);
740,335✔
33
}
34

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

39
static int32_t doOutputResultBlockImpl(SStreamTask* pTask, SStreamDataBlock* pBlock) {
15,353✔
40
  int32_t code = 0;
15,353✔
41
  int32_t type = pTask->outputInfo.type;
15,353✔
42
  if (type == TASK_OUTPUT__TABLE) {
15,353✔
43
    pTask->outputInfo.tbSink.tbSinkFunc(pTask, pTask->outputInfo.tbSink.vnode, pBlock->blocks);
7,160✔
44
    destroyStreamDataBlock(pBlock);
7,160✔
45
  } else if (type == TASK_OUTPUT__SMA) {
8,193✔
46
    pTask->outputInfo.smaSink.smaSink(pTask->outputInfo.smaSink.vnode, pTask->outputInfo.smaSink.smaId, pBlock->blocks);
3✔
47
    destroyStreamDataBlock(pBlock);
3✔
48
  } else {
49
    if (type != TASK_OUTPUT__FIXED_DISPATCH && type != TASK_OUTPUT__SHUFFLE_DISPATCH) {
8,190!
50
      stError("s-task:%s invalid stream output type:%d, internal error", pTask->id.idStr, type);
×
51
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
52
    }
53

54
    code = streamTaskPutDataIntoOutputQ(pTask, pBlock);
8,190✔
55
    if (code != TSDB_CODE_SUCCESS) {
8,190!
56
      destroyStreamDataBlock(pBlock);
×
57
      return code;
×
58
    }
59

60
    // not handle error, if dispatch failed, try next time.
61
    // checkpoint trigger will be checked
62
    code = streamDispatchStreamBlock(pTask);
8,190✔
63
  }
64

65
  return code;
15,353✔
66
}
67

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

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

78
  int32_t code = createStreamBlockFromResults(pItem, pTask, size, pRes, &pStreamBlocks);
7,547✔
79
  if (code) {
7,547!
80
    stError("s-task:%s failed to create result stream data block, code:%s", pTask->id.idStr, tstrerror(terrno));
×
81
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
×
82
    return TSDB_CODE_OUT_OF_MEMORY;
×
83
  }
84

85
  stDebug("s-task:%s dump stream result data blocks, num:%d, size:%.2fMiB", pTask->id.idStr, numOfBlocks,
7,547!
86
          SIZE_IN_MiB(size));
87

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

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

96
  return code;
7,547✔
97
}
98

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

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

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

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

142
  while (1) {
11,759✔
143
    SSDataBlock* output = NULL;
32,705✔
144
    uint64_t     ts = 0;
32,705✔
145

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

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

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

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

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

179
      break;
20,930✔
180
    }
181

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

191
    SSDataBlock block = {.info.childId = pTask->info.selfChildId};
11,356✔
192
    code = assignOneDataBlock(&block, output);
11,356✔
193
    if (code) {
11,356!
194
      stError("s-task:%s failed to build result block due to out of memory", pTask->id.idStr);
×
195
      continue;
×
196
    }
197

198
    size += blockDataGetSize(output) + sizeof(SSDataBlock) + sizeof(SColumnInfoData) * blockDataGetNumOfCols(&block);
11,356✔
199
    numOfBlocks += 1;
11,355✔
200

201
    void* p = taosArrayPush(pRes, &block);
11,355✔
202
    if (p == NULL) {
11,355!
203
      stError("s-task:%s failed to add computing results, the final res may be incorrect", pTask->id.idStr);
×
204
    } else {
205
      stDebug("s-task:%s (child %d) executed and get %d result blocks, size:%.2fMiB", pTask->id.idStr,
11,355!
206
              pTask->info.selfChildId, numOfBlocks, SIZE_IN_MiB(size));
207
    }
208

209
    // current output should be dispatched to down stream nodes
210
    if (numOfBlocks >= STREAM_RESULT_DUMP_THRESHOLD || size >= STREAM_RESULT_DUMP_SIZE_THRESHOLD) {
11,356!
211
      code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
2✔
212
      // todo: here we need continue retry to put it into output buffer
213
      if (code != TSDB_CODE_SUCCESS) {
2!
214
        return code;
×
215
      }
216

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

223
  if (numOfBlocks > 0) {
20,930✔
224
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
7,545✔
225
  } else {
226
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
13,385✔
227
  }
228

229
  return code;
20,930✔
230
}
231

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

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

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

263
    if (pTask->inputq.status == TASK_INPUT_STATUS__BLOCKED) {
7,308!
264
      stDebug("s-task:%s level:%d inputQ is blocked, retry in 5s", pTask->id.idStr, pTask->info.taskLevel);
×
265
      break;
×
266
    }
267

268
    SSDataBlock* output = NULL;
7,308✔
269
    uint64_t     ts = 0;
7,308✔
270
    code = qExecTask(exec, &output, &ts);
7,308✔
271
    if (code != TSDB_CODE_TSC_QUERY_KILLED && code != TSDB_CODE_SUCCESS) {  // if out of memory occurs, quit
7,309!
272
      stError("s-task:%s scan-history data error occurred code:%s, continue scan-history", pTask->id.idStr,
×
273
              tstrerror(code));
274
      qResetTaskCode(exec);
×
275
      continue;
×
276
    }
277

278
    // the generated results before fill-history task been paused, should be dispatched to sink node
279
    if (output == NULL) {
7,309✔
280
      (*pFinish) = qStreamScanhistoryFinished(exec);
1,442✔
281
      break;
1,442✔
282
    }
283

284
    SSDataBlock block = {0};
5,867✔
285
    code = assignOneDataBlock(&block, output);
5,867✔
286
    if (code) {
5,867!
287
      stError("s-task:%s failed to build result block due to out of memory", pTask->id.idStr);
×
288
    }
289

290
    block.info.childId = pTask->info.selfChildId;
5,867✔
291
    void* p = taosArrayPush(pRes, &block);
5,867✔
292
    if (p == NULL) {
5,867!
293
      stError("s-task:%s failed to add computing results, the final res may be incorrect", pTask->id.idStr);
×
294
    }
295

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

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

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

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

318
  if (pTask->info.taskLevel != TASK_LEVEL__SOURCE) {
1,604!
319
    stError("s-task:%s not source scan-history task, not exec, quit", pTask->id.idStr);
×
320
    return buildScanhistoryExecRet(TASK_SCANHISTORY_QUIT, 0);
×
321
  }
322

323
  if (!pTask->hTaskInfo.operatorOpen) {
1,604✔
324
    int32_t code = qSetStreamOpOpen(exec);
1,442✔
325
    pTask->hTaskInfo.operatorOpen = true;
1,442✔
326
  }
327

328
  while (1) {
395✔
329
    if (streamTaskShouldPause(pTask)) {
1,999!
330
      stDebug("s-task:%s paused from the scan-history task", id);
×
331
      // quit from step1, not continue to handle the step2
332
      return buildScanhistoryExecRet(TASK_SCANHISTORY_QUIT, 0);
1,604✔
333
    }
334

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

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

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

367
    if (finished) {
1,999✔
368
      return buildScanhistoryExecRet(TASK_SCANHISTORY_CONT, 0);
1,442✔
369
    }
370

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

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

384
  SStreamTask* pStreamTask = NULL;
1,471✔
385
  int32_t code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,471✔
386
  if (pStreamTask == NULL || code != TSDB_CODE_SUCCESS) {
1,471!
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.;
1,470✔
405
    stDebug(
1,470!
406
        "s-task:%s fill-history task end, status:%s, scan wal elapsed time:%.2fSec, update related stream task:%s "
407
        "info, prepare transfer exec state",
408
        id, streamTaskGetStatus(pTask).name, el, pStreamTask->id.idStr);
409
  }
410

411
  ETaskStatus  status = streamTaskGetStatus(pStreamTask).state;
1,470✔
412
  STimeWindow* pTimeWindow = &pStreamTask->dataRange.window;
1,470✔
413

414
  // It must be halted for a source stream task, since when the related scan-history-data task start scan the history
415
  // for the step 2.
416
  if (pStreamTask->info.taskLevel == TASK_LEVEL__SOURCE) {
1,470✔
417
    if (!(status == TASK_STATUS__HALT || status == TASK_STATUS__DROPPING || status == TASK_STATUS__STOP)) {
1,427!
418
      stError("s-task:%s invalid task status:%d", id, status);
×
419
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
420
    }
421
  } else {
422
    if (!(status == TASK_STATUS__READY || status == TASK_STATUS__PAUSE || status == TASK_STATUS__DROPPING ||
43!
423
          status == TASK_STATUS__STOP)) {
424
      stError("s-task:%s invalid task status:%d", id, status);
×
425
      return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
426
    }
427
    code = streamTaskHandleEvent(pStreamTask->status.pSM, TASK_EVENT_HALT);
43✔
428
    if (code != TSDB_CODE_SUCCESS) {
43!
429
      stError("s-task:%s halt stream task:%s failed, code:%s not transfer state to stream task", id,
×
430
              pStreamTask->id.idStr, tstrerror(code));
431
      streamMetaReleaseTask(pMeta, pStreamTask);
×
432
      return code;
×
433
    } else {
434
      stDebug("s-task:%s halt by related fill-history task:%s", pStreamTask->id.idStr, id);
43!
435
    }
436
  }
437

438
  // In case of sink tasks, no need to halt them.
439
  // In case of source tasks and agg tasks, we should HALT them, and wait for them to be idle. And then, it's safe to
440
  // start the task state transfer procedure.
441
  SStreamTaskState pState = streamTaskGetStatus(pStreamTask);
1,470✔
442
  status = pState.state;
1,470✔
443
  char* p = pState.name;
1,470✔
444
  if (status == TASK_STATUS__STOP || status == TASK_STATUS__DROPPING) {
1,470!
445
    stError("s-task:%s failed to transfer state from fill-history task:%s, status:%s", id, pStreamTask->id.idStr, p);
1!
446
    streamMetaReleaseTask(pMeta, pStreamTask);
1✔
447
    return TSDB_CODE_STREAM_TASK_IVLD_STATUS;
1✔
448
  }
449

450
  // 1. expand the query time window for stream task of WAL scanner
451
  if (pStreamTask->info.taskLevel == TASK_LEVEL__SOURCE) {
1,469✔
452
    // update the scan data range for source task.
453
    stDebug("s-task:%s level:%d stream task window %" PRId64 " - %" PRId64 " update to %" PRId64 " - %" PRId64
1,426!
454
            ", status:%s, sched-status:%d",
455
            pStreamTask->id.idStr, TASK_LEVEL__SOURCE, pTimeWindow->skey, pTimeWindow->ekey, INT64_MIN,
456
            pTimeWindow->ekey, p, pStreamTask->status.schedStatus);
457

458
    code = streamTaskResetTimewindowFilter(pStreamTask);
1,426✔
459
  } else {
460
    stDebug("s-task:%s no need to update/reset filter time window for non-source tasks", pStreamTask->id.idStr);
43!
461
  }
462

463
  // NOTE: transfer the ownership of executor state before handle the checkpoint block during stream exec
464
  // 2. send msg to mnode to launch a checkpoint to keep the state for current stream
465
  code = streamTaskSendCheckpointReq(pStreamTask);
1,469✔
466

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

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

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

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

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

486
  if (pTask->status.appendTranstateBlock != 1) {
2,861!
487
    stError("s-task:%s not set appendTransBlock flag, internal error", pTask->id.idStr);
×
488
    return TSDB_CODE_STREAM_INTERNAL_ERROR;
×
489
  }
490

491
  int32_t level = pTask->info.taskLevel;
2,861✔
492
  if (level == TASK_LEVEL__AGG || level == TASK_LEVEL__SOURCE) {  // do transfer task operator states.
2,861✔
493
    code = streamTransferStateDoPrepare(pTask);
1,471✔
494
  } else {
495
    // no state transfer for sink tasks, and drop fill-history task, followed by opening inputQ of sink task.
496
    SStreamTask* pStreamTask = NULL;
1,390✔
497
    code = streamMetaAcquireTask(pMeta, pTask->streamTaskId.streamId, pTask->streamTaskId.taskId, &pStreamTask);
1,390✔
498
    if (pStreamTask != NULL) {
1,390✔
499
      // halt the related stream sink task
500
      code = streamTaskHandleEventAsync(pStreamTask->status.pSM, TASK_EVENT_HALT, haltCallback, NULL);
1,389✔
501
      if (code != TSDB_CODE_SUCCESS) {
1,389!
502
        stError("s-task:%s halt stream task:%s failed, code:%s not transfer state to stream task", pTask->id.idStr,
×
503
                pStreamTask->id.idStr, tstrerror(code));
504
        streamMetaReleaseTask(pMeta, pStreamTask);
×
505
        return code;
×
506
      } else {
507
        stDebug("s-task:%s sink task halt by related fill-history task:%s", pStreamTask->id.idStr, pTask->id.idStr);
1,389!
508
      }
509
      streamMetaReleaseTask(pMeta, pStreamTask);
1,389✔
510
    }
511
  }
512

513
  return code;
2,861✔
514
}
515

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

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

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

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

545
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
9,447✔
546
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;
6,556✔
547

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

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

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

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

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

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

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

620
    code = streamTransferStatePrepare(pTask);
1,399✔
621
    if (code != TSDB_CODE_SUCCESS) {
1,399✔
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; }
41,282✔
630

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

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

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

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

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

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

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

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

688
  return code;
20,943✔
689
}
690

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

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

737
  return code;
1,492✔
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) {
42,742✔
745
  const char* id = pTask->id.idStr;
42,742✔
746
  int32_t     code = 0;
42,742✔
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);
42,742!
750

751
  while (1) {
37,853✔
752
    int32_t           blockSize = 0;
80,595✔
753
    int32_t           numOfBlocks = 0;
80,595✔
754
    SStreamQueueItem* pInput = NULL;
80,595✔
755

756
    if (streamTaskShouldStop(pTask) || (streamTaskGetStatus(pTask).state == TASK_STATUS__UNINIT)) {
80,595!
757
      stDebug("s-task:%s stream task is stopped", id);
14!
758
      return 0;
42,742✔
759
    }
760

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

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

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

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

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

802
    if (type == STREAM_INPUT__TRANS_STATE) {
33,259✔
803
      streamProcessTransstateBlock(pTask, (SStreamDataBlock*)pInput);
5,973✔
804
      continue;
5,973✔
805
    }
806

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

813
      int64_t st = taosGetTimestampMs();
6,382✔
814

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

824
        double el = (taosGetTimestampMs() - st) / 1000.0;
6,382✔
825
        if (fabs(el - 0.0) <= DBL_EPSILON) {
6,382✔
826
          pTask->execInfo.procsThroughput = 0;
3,108✔
827
        } else {
828
          pTask->execInfo.procsThroughput = (blockSize / el);
3,274✔
829
        }
830

831
        continue;
6,382✔
832
      }
833
    }
834

835
    if (type != STREAM_INPUT__CHECKPOINT) {
20,904✔
836
      code = doStreamTaskExecImpl(pTask, pInput, numOfBlocks);
19,454✔
837
      streamFreeQitem(pInput);
19,454✔
838
      if (code) {
19,454✔
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);
1,450✔
844
      SStreamTaskState pState = streamTaskGetStatus(pTask);
1,450✔
845
      if (pState.state == TASK_STATUS__CK) {
1,450!
846
        stDebug("s-task:%s checkpoint block received, set status:%s", id, pState.name);
1,450!
847
        code = streamTaskBuildCheckpoint(pTask);  // ignore this error msg, and continue
1,450✔
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);
1,450✔
863
      streamFreeQitem(pInput);
1,450✔
864
      return code;
1,450✔
865
    }
866
  }
867
}
868

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

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

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

885
  // pause & halt will still run for sink tasks.
886
  if (streamTaskIsSinkTask(pTask)) {
40,782✔
887
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
8,840!
888
            st == TASK_STATUS__PAUSE || st == TASK_STATUS__HALT);
23,104!
889
  } else {
890
    return (st == TASK_STATUS__READY || st == TASK_STATUS__SCAN_HISTORY || st == TASK_STATUS__CK ||
26,518✔
891
            st == TASK_STATUS__HALT);
892
  }
893
}
894

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

899
  if (pTask->status.schedStatus != TASK_SCHED_STATUS__ACTIVE) {
41,285!
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) {
1,457✔
905
    code = doStreamExecTask(pTask);
42,742✔
906
    if (code) {
42,742✔
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);
42,739✔
912

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

919
      setLastExecTs(pTask, taosGetTimestampMs());
34,793✔
920

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

925
      return code;
34,793✔
926
    } else {
927
      // check if this task needs to be idle for a while
928
      if (pTask->status.schedIdleTime > 0) {
7,946✔
929
        streamTaskResumeInFuture(pTask);
6,489✔
930

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

937
    streamMutexUnlock(&pTask->lock);
1,457✔
938
  }
939

940
  return code;
941
}
942

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

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

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

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

969
  return code;
1,434✔
970
}
971

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

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

981
  return code;
1,434✔
982
}
983

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

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