streamExec.c 21.5 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

16
#include "streamInt.h"
L
Liu Jicong 已提交
17

H
Haojun Liao 已提交
18
// maximum allowed processed block batches. One block may include several submit blocks
19
#define MAX_STREAM_EXEC_BATCH_NUM 32
20
#define MIN_STREAM_EXEC_BATCH_NUM 4
21
#define MAX_STREAM_RESULT_DUMP_THRESHOLD  100
5
54liuyao 已提交
22

Y
yihaoDeng 已提交
23
static int32_t updateCheckPointInfo(SStreamTask* pTask);
24

25
bool streamTaskShouldStop(const SStreamStatus* pStatus) {
dengyihao's avatar
dengyihao 已提交
26
  int32_t status = atomic_load_8((int8_t*)&pStatus->taskStatus);
27 28 29
  return (status == TASK_STATUS__STOP) || (status == TASK_STATUS__DROPPING);
}

L
liuyao 已提交
30
bool streamTaskShouldPause(const SStreamStatus* pStatus) {
dengyihao's avatar
dengyihao 已提交
31
  int32_t status = atomic_load_8((int8_t*)&pStatus->taskStatus);
L
liuyao 已提交
32 33 34
  return (status == TASK_STATUS__PAUSE);
}

35 36
static int32_t doDumpResult(SStreamTask* pTask, SStreamQueueItem* pItem, SArray* pRes, int32_t size, int64_t* totalSize,
                            int32_t* totalBlocks) {
37 38
  int32_t code = updateCheckPointInfo(pTask);
  if (code != TSDB_CODE_SUCCESS) {
39
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
40 41 42 43 44
    return code;
  }

  int32_t numOfBlocks = taosArrayGetSize(pRes);
  if (numOfBlocks > 0) {
45
    SStreamDataBlock* pStreamBlocks = createStreamBlockFromResults(pItem, pTask, size, pRes);
46
    if (pStreamBlocks == NULL) {
47
      qError("s-task:%s failed to create result stream data block, code:%s", pTask->id.idStr, tstrerror(terrno));
48
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
49 50 51
      return -1;
    }

Y
yihaoDeng 已提交
52 53
    qDebug("s-task:%s dump stream result data blocks, num:%d, size:%.2fMiB", pTask->id.idStr, numOfBlocks,
           size / 1048576.0);
54 55

    code = streamTaskOutputResultBlock(pTask, pStreamBlocks);
Y
yihaoDeng 已提交
56
    if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY) {  // back pressure and record position
57
      destroyStreamDataBlock(pStreamBlocks);
58 59
      return -1;
    }
60 61 62

    *totalSize += size;
    *totalBlocks += numOfBlocks;
63
  } else {
64
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
65 66 67 68 69
  }

  return TSDB_CODE_SUCCESS;
}

Y
yihaoDeng 已提交
70 71
static int32_t streamTaskExecImpl(SStreamTask* pTask, SStreamQueueItem* pItem, int64_t* totalSize,
                                  int32_t* totalBlocks) {
72 73
  int32_t code = TSDB_CODE_SUCCESS;
  void*   pExecutor = pTask->exec.pExecutor;
L
Liu Jicong 已提交
74

75 76 77 78 79
  *totalBlocks = 0;
  *totalSize = 0;

  int32_t size = 0;
  int32_t numOfBlocks = 0;
H
Haojun Liao 已提交
80
  SArray* pRes = NULL;
L
Liu Jicong 已提交
81 82

  while (1) {
H
Haojun Liao 已提交
83 84 85
    if (pRes == NULL) {
      pRes = taosArrayInit(4, sizeof(SSDataBlock));
    }
H
Haojun Liao 已提交
86

87
    if (streamTaskShouldStop(&pTask->status)) {
Y
yihaoDeng 已提交
88
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
L
Liu Jicong 已提交
89 90 91
      return 0;
    }

L
Liu Jicong 已提交
92 93
    SSDataBlock* output = NULL;
    uint64_t     ts = 0;
94
    if ((code = qExecTask(pExecutor, &output, &ts)) < 0) {
5
54liuyao 已提交
95
      if (code == TSDB_CODE_QRY_IN_EXEC) {
96
        resetTaskInfo(pExecutor);
5
54liuyao 已提交
97
      }
98 99

      qError("unexpected stream execution, s-task:%s since %s", pTask->id.idStr, terrstr());
L
Liu Jicong 已提交
100
      continue;
L
Liu Jicong 已提交
101
    }
102

103
    if (output == NULL) {
5
54liuyao 已提交
104
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
Y
yihaoDeng 已提交
105
        SSDataBlock             block = {0};
Y
yihaoDeng 已提交
106
        const SStreamDataBlock* pRetrieveBlock = (const SStreamDataBlock*)pItem;
107
        ASSERT(taosArrayGetSize(pRetrieveBlock->blocks) == 1);
108

L
Liu Jicong 已提交
109
        assignOneDataBlock(&block, taosArrayGet(pRetrieveBlock->blocks, 0));
L
Liu Jicong 已提交
110
        block.info.type = STREAM_PULL_OVER;
111
        block.info.childId = pTask->info.selfChildId;
L
Liu Jicong 已提交
112
        taosArrayPush(pRes, &block);
H
Haojun Liao 已提交
113
        numOfBlocks += 1;
114 115

        qDebug("s-task:%s(child %d) retrieve process completed, reqId:0x%" PRIx64" dump results", pTask->id.idStr, pTask->info.selfChildId,
L
Liu Jicong 已提交
116
               pRetrieveBlock->reqId);
117
      }
H
Haojun Liao 已提交
118

119 120
      break;
    }
L
Liu Jicong 已提交
121 122 123 124 125 126 127 128

    if (output->info.type == STREAM_RETRIEVE) {
      if (streamBroadcastToChildren(pTask, output) < 0) {
        // TODO
      }
      continue;
    }

L
Liu Jicong 已提交
129 130
    SSDataBlock block = {0};
    assignOneDataBlock(&block, output);
131
    block.info.childId = pTask->info.selfChildId;
H
Haojun Liao 已提交
132

133 134 135
    size += blockDataGetSize(output) + sizeof(SSDataBlock) + sizeof(SColumnInfoData) * blockDataGetNumOfCols(&block);
    numOfBlocks += 1;

L
Liu Jicong 已提交
136
    taosArrayPush(pRes, &block);
H
Haojun Liao 已提交
137

138
    qDebug("s-task:%s (child %d) executed and get %d result blocks, size:%.2fMiB", pTask->id.idStr,
139
           pTask->info.selfChildId, numOfBlocks, size / 1048576.0);
140 141

    // current output should be dispatched to down stream nodes
142 143 144
    if (numOfBlocks >= MAX_STREAM_RESULT_DUMP_THRESHOLD) {
      ASSERT(numOfBlocks == taosArrayGetSize(pRes));
      code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
145 146 147 148
      if (code != TSDB_CODE_SUCCESS) {
        return code;
      }

H
Haojun Liao 已提交
149
      pRes = NULL;
150 151
      size = 0;
      numOfBlocks = 0;
152
    }
153
  }
154

155 156 157
  if (numOfBlocks > 0) {
    ASSERT(numOfBlocks == taosArrayGetSize(pRes));
    code = doDumpResult(pTask, pItem, pRes, size, totalSize, totalBlocks);
158
  } else {
Y
yihaoDeng 已提交
159
    taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
L
Liu Jicong 已提交
160
  }
161

H
Haojun Liao 已提交
162
  return code;
L
Liu Jicong 已提交
163 164
}

165
int32_t streamScanExec(SStreamTask* pTask, int32_t batchSz) {
dengyihao's avatar
dengyihao 已提交
166
  int32_t code = 0;
167

168
  ASSERT(pTask->info.taskLevel == TASK_LEVEL__SOURCE);
169
  void* exec = pTask->exec.pExecutor;
170

L
Liu Jicong 已提交
171
  qSetStreamOpOpen(exec);
L
Liu Jicong 已提交
172
  bool finished = false;
L
Liu Jicong 已提交
173

174 175 176 177 178 179 180 181 182
  while (1) {
    SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
    if (pRes == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    int32_t batchCnt = 0;
    while (1) {
L
liuyao 已提交
183
      if (streamTaskShouldStop(&pTask->status)) {
L
liuyao 已提交
184
        taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
L
Liu Jicong 已提交
185 186 187
        return 0;
      }

188 189 190
      SSDataBlock* output = NULL;
      uint64_t     ts = 0;
      if (qExecTask(exec, &output, &ts) < 0) {
5
54liuyao 已提交
191
        continue;
192
      }
193

L
Liu Jicong 已提交
194
      if (output == NULL) {
L
Liu Jicong 已提交
195 196 197 198
        if (qStreamRecoverScanFinished(exec)) {
          finished = true;
        } else {
          qSetStreamOpOpen(exec);
L
liuyao 已提交
199
          if (streamTaskShouldPause(&pTask->status)) {
L
liuyao 已提交
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
            SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
            if (qRes == NULL) {
              taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
              terrno = TSDB_CODE_OUT_OF_MEMORY;
              return -1;
            }

            qRes->type = STREAM_INPUT__DATA_BLOCK;
            qRes->blocks = pRes;
            code = streamTaskOutputResultBlock(pTask, qRes);
            if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY) {
              taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
              taosFreeQitem(qRes);
              return code;
            }
L
liuyao 已提交
215 216
            return 0;
          }
L
Liu Jicong 已提交
217
        }
L
Liu Jicong 已提交
218 219
        break;
      }
220 221 222

      SSDataBlock block = {0};
      assignOneDataBlock(&block, output);
223
      block.info.childId = pTask->info.selfChildId;
224 225
      taosArrayPush(pRes, &block);

L
Liu Jicong 已提交
226 227
      batchCnt++;

228
      qDebug("s-task:%s scan exec numOfBlocks:%d, limit:%d", pTask->id.idStr, batchCnt, batchSz);
H
Haojun Liao 已提交
229 230 231
      if (batchCnt >= batchSz) {
        break;
      }
232
    }
H
Haojun Liao 已提交
233

234
    if (taosArrayGetSize(pRes) == 0) {
H
Haojun Liao 已提交
235 236
      taosArrayDestroy(pRes);

237
      if (finished) {
H
Haojun Liao 已提交
238
        qDebug("s-task:%s finish recover exec task ", pTask->id.idStr);
239 240
        break;
      } else {
H
Haojun Liao 已提交
241
        qDebug("s-task:%s continue recover exec task ", pTask->id.idStr);
242 243
        continue;
      }
244
    }
H
Haojun Liao 已提交
245

S
Shengliang Guan 已提交
246
    SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
247 248 249 250 251 252 253 254
    if (qRes == NULL) {
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    qRes->type = STREAM_INPUT__DATA_BLOCK;
    qRes->blocks = pRes;
255
    code = streamTaskOutputResultBlock(pTask, qRes);
dengyihao's avatar
dengyihao 已提交
256
    if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY) {
dengyihao's avatar
dengyihao 已提交
257
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
dengyihao's avatar
dengyihao 已提交
258
      taosFreeQitem(qRes);
dengyihao's avatar
dengyihao 已提交
259 260
      return code;
    }
261 262 263 264

    if (finished) {
      break;
    }
265 266 267 268 269
  }
  return 0;
}

#if 0
270 271 272 273
int32_t streamBatchExec(SStreamTask* pTask, int32_t batchLimit) {
  // fetch all queue item, merge according to batchLimit
  int32_t numOfItems = taosReadAllQitems(pTask->inputQueue1, pTask->inputQall);
  if (numOfItems == 0) {
274
    qDebug("task: %d, stream task exec over, queue empty", pTask->id.taskId);
275 276 277 278 279 280 281 282 283 284
    return 0;
  }
  SStreamQueueItem* pMerged = NULL;
  SStreamQueueItem* pItem = NULL;
  taosGetQitem(pTask->inputQall, (void**)&pItem);
  if (pItem == NULL) {
    if (pMerged != NULL) {
      // process merged item
    } else {
      return 0;
285
    }
286
  }
287

288 289 290 291 292
  // if drop
  if (pItem->type == STREAM_INPUT__DESTROY) {
    // set status drop
    return -1;
  }
293

294
  if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
295
    ASSERT(((SStreamQueueItem*)pItem)->type == STREAM_INPUT__DATA_BLOCK);
296
    streamTaskOutputResultBlock(pTask, (SStreamDataBlock*)pItem);
297 298
  }

299 300 301 302
  // exec impl

  // output
  // try dispatch
303 304
  return 0;
}
305
#endif
L
Liu Jicong 已提交
306

Y
yihaoDeng 已提交
307
int32_t updateCheckPointInfo(SStreamTask* pTask) {
308 309 310 311 312 313 314
  int64_t ckId = 0;
  int64_t dataVer = 0;
  qGetCheckpointVersion(pTask->exec.pExecutor, &dataVer, &ckId);

  SCheckpointInfo* pCkInfo = &pTask->chkInfo;
  if (ckId > pCkInfo->id) {  // save it since the checkpoint is updated
    qDebug("s-task:%s exec end, start to update check point, ver from %" PRId64 " to %" PRId64
Y
yihaoDeng 已提交
315 316
           ", checkPoint id:%" PRId64 " -> %" PRId64,
           pTask->id.idStr, pCkInfo->version, dataVer, pCkInfo->id, ckId);
317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335

    pTask->chkInfo = (SCheckpointInfo){.version = dataVer, .id = ckId, .currentVer = pCkInfo->currentVer};

    taosWLockLatch(&pTask->pMeta->lock);

    streamMetaSaveTask(pTask->pMeta, pTask);
    if (streamMetaCommit(pTask->pMeta) < 0) {
      taosWUnLockLatch(&pTask->pMeta->lock);
      qError("s-task:%s failed to commit stream meta, since %s", pTask->id.idStr, terrstr());
      return -1;
    } else {
      taosWUnLockLatch(&pTask->pMeta->lock);
      qDebug("s-task:%s update checkpoint ver succeed", pTask->id.idStr);
    }
  }

  return TSDB_CODE_SUCCESS;
}

336
static void waitForTaskIdle(SStreamTask* pTask, SStreamTask* pStreamTask) {
337 338 339 340 341 342 343 344 345 346 347
  // wait for the stream task to be idle
  int64_t st = taosGetTimestampMs();

  while (!streamTaskIsIdle(pStreamTask)) {
    qDebug("s-task:%s level:%d wait for stream task:%s to be idle, check again in 100ms", pTask->id.idStr,
           pTask->info.taskLevel, pStreamTask->id.idStr);
    taosMsleep(100);
  }

  double el = (taosGetTimestampMs() - st) / 1000.0;
  if (el > 0) {
348
    qDebug("s-task:%s wait for stream task:%s for %.2fs to handle all data in inputQ", pTask->id.idStr,
349 350 351 352 353
           pStreamTask->id.idStr, el);
  }
}

static int32_t streamTransferStateToStreamTask(SStreamTask* pTask) {
354 355 356
  SStreamMeta* pMeta = pTask->pMeta;

  SStreamTask* pStreamTask = streamMetaAcquireTask(pMeta, pTask->streamTaskId.taskId);
357
  if (pStreamTask == NULL) {
358 359 360 361
    pTask->status.transferState = false;  // reset this value, to avoid transfer state again

    qError("s-task:%s failed to find related stream task:0x%x, it may have been destroyed or closed", pTask->id.idStr,
           pTask->streamTaskId.taskId);
362 363
    return TSDB_CODE_STREAM_TASK_NOT_EXIST;
  } else {
H
Haojun Liao 已提交
364 365
    qDebug("s-task:%s fill-history task end, update related stream task:%s info, transfer exec state", pTask->id.idStr,
           pStreamTask->id.idStr);
366
  }
367

368 369 370 371
  // todo fix race condition
  streamTaskDisablePause(pTask);
  streamTaskDisablePause(pStreamTask);

372 373
  ASSERT(pStreamTask->historyTaskId.taskId == pTask->id.taskId && pTask->status.transferState == true);

374 375
  STimeWindow* pTimeWindow = &pStreamTask->dataRange.window;

376 377
  // It must be halted for a source stream task, since when the related scan-history-data task start scan the history
  // for the step 2. For a agg task
378
  int8_t status = pStreamTask->status.taskStatus;
379
  if (pStreamTask->info.taskLevel == TASK_LEVEL__SOURCE) {
380
    ASSERT(status == TASK_STATUS__HALT);
381
  } else {
382
    ASSERT(status == TASK_STATUS__SCAN_HISTORY);
383
    pStreamTask->status.taskStatus = TASK_STATUS__HALT;
384
    qDebug("s-task:%s halt by related fill history task:%s", pStreamTask->id.idStr, pTask->id.idStr);
385 386
  }

387
  // wait for the stream task to handle all in the inputQ, and to be idle
388
  waitForTaskIdle(pTask, pStreamTask);
389

390 391 392 393
  // In case of sink tasks, no need to be halted for them.
  // 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
  // start the task state transfer procedure.
  // When a task is idle with halt status, all data in inputQ are consumed.
394 395
  if (pStreamTask->info.taskLevel == TASK_LEVEL__SOURCE) {
    // update the scan data range for source task.
396
    qDebug("s-task:%s level:%d stream task window %" PRId64 " - %" PRId64 " update to %" PRId64 " - %" PRId64
397
           ", status:%s, sched-status:%d",
398 399 400
           pStreamTask->id.idStr, TASK_LEVEL__SOURCE, pTimeWindow->skey, pTimeWindow->ekey, INT64_MIN,
           pTimeWindow->ekey, streamGetTaskStatusStr(TASK_STATUS__NORMAL), pStreamTask->status.schedStatus);
  } else {
401
    qDebug("s-task:%s no need to update time window for non-source task", pStreamTask->id.idStr);
402 403 404 405
  }

  // expand the query time window for stream scanner
  pTimeWindow->skey = INT64_MIN;
406
  qResetStreamInfoTimeWindow(pStreamTask->exec.pExecutor);
407

408 409 410 411
  // transfer the ownership of executor state
  streamTaskReleaseState(pTask);
  streamTaskReloadState(pStreamTask);

412
  // reset the status of stream task
413
  streamSetStatusNormal(pStreamTask);
414

415 416 417 418 419 420 421 422 423 424 425 426
  pTask->status.taskStatus = TASK_STATUS__DROPPING;
  qDebug("s-task:%s fill-history task set status to be dropping, save the state into disk", pTask->id.idStr);

  // save to disk
  taosWLockLatch(&pMeta->lock);
  streamMetaSaveTask(pMeta, pTask);
  streamMetaSaveTask(pMeta, pStreamTask);
  if (streamMetaCommit(pMeta) < 0) {
    // persist to disk
  }
  taosWUnLockLatch(&pMeta->lock);

427 428 429 430
  // pause allowed
  streamTaskEnablePause(pStreamTask);
  streamTaskEnablePause(pTask);

431
  streamSchedExec(pStreamTask);
432
  streamMetaReleaseTask(pMeta, pStreamTask);
433 434 435
  return TSDB_CODE_SUCCESS;
}

436 437 438 439
static int32_t extractMsgFromInputQ(SStreamTask* pTask, SStreamQueueItem** pInput, int32_t* numOfBlocks,
                                    const char* id) {
  int32_t retryTimes = 0;
  int32_t MAX_RETRY_TIMES = 5;
440

L
Liu Jicong 已提交
441
  while (1) {
442 443 444 445
    if (streamTaskShouldPause(&pTask->status)) {
      qDebug("s-task:%s task should pause, input blocks:%d", pTask->id.idStr, *numOfBlocks);
      return TSDB_CODE_SUCCESS;
    }
H
Haojun Liao 已提交
446

447 448
    SStreamQueueItem* qItem = streamQueueNextItem(pTask->inputQueue);
    if (qItem == NULL) {
H
Haojun Liao 已提交
449
      if (pTask->info.taskLevel == TASK_LEVEL__SOURCE && (++retryTimes) < MAX_RETRY_TIMES) {
450 451 452
        taosMsleep(10);
        qDebug("===stream===try again batchSize:%d, retry:%d", *numOfBlocks, retryTimes);
        continue;
L
liuyao 已提交
453
      }
454

455 456 457
      qDebug("===stream===break batchSize:%d", *numOfBlocks);
      return TSDB_CODE_SUCCESS;
    }
458

459
    // do not merge blocks for sink node
H
Haojun Liao 已提交
460
    if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
461 462 463 464
      *numOfBlocks = 1;
      *pInput = qItem;
      return TSDB_CODE_SUCCESS;
    }
465

466
    if (*pInput == NULL) {
467 468 469 470 471 472 473 474 475
      ASSERT((*numOfBlocks) == 0);
      *pInput = qItem;
    } else {
      // todo we need to sort the data block, instead of just appending into the array list.
      void* newRet = streamMergeQueueItem(*pInput, qItem);
      if (newRet == NULL) {
        qError("s-task:%s failed to merge blocks from inputQ, numOfBlocks:%d", id, *numOfBlocks);
        streamQueueProcessFail(pTask->inputQueue);
        return TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
476
      }
477 478

      *pInput = newRet;
L
Liu Jicong 已提交
479
    }
480

481 482 483 484 485 486
    *numOfBlocks += 1;
    streamQueueProcessSuccess(pTask->inputQueue);

    if (*numOfBlocks >= MAX_STREAM_EXEC_BATCH_NUM) {
      qDebug("s-task:%s batch size limit:%d reached, start to process blocks", id, MAX_STREAM_EXEC_BATCH_NUM);
      return TSDB_CODE_SUCCESS;
L
Liu Jicong 已提交
487
    }
488 489 490
  }
}

491 492 493 494
/**
 * todo: the batch of blocks should be tuned dynamic, according to the total elapsed time of each batch of blocks, the
 * appropriate batch of blocks should be handled in 5 to 10 sec.
 */
L
Liu Jicong 已提交
495
int32_t streamExecForAll(SStreamTask* pTask) {
496 497
  const char* id = pTask->id.idStr;

L
Liu Jicong 已提交
498
  while (1) {
499
    int32_t batchSize = 0;
500 501
    SStreamQueueItem* pInput = NULL;

502
    // merge multiple input data if possible in the input queue.
503
    qDebug("s-task:%s start to extract data block from inputQ", id);
L
Liu Jicong 已提交
504

505
    /*int32_t code = */extractMsgFromInputQ(pTask, &pInput, &batchSize, id);
506
    if (pInput == NULL) {
507
      ASSERT(batchSize == 0);
508 509
      if (pTask->info.fillHistory && pTask->status.transferState) {
        int32_t code = streamTransferStateToStreamTask(pTask);
510 511 512
        if (code != TSDB_CODE_SUCCESS) { // todo handle this
          return 0;
        }
H
Haojun Liao 已提交
513
      }
514

L
Liu Jicong 已提交
515 516 517
      break;
    }

518
    if (pTask->info.taskLevel == TASK_LEVEL__SINK) {
519
      ASSERT(pInput->type == STREAM_INPUT__DATA_BLOCK);
520
      qDebug("s-task:%s sink task start to sink %d blocks", id, batchSize);
521
      streamTaskOutputResultBlock(pTask, (SStreamDataBlock*)pInput);
L
Liu Jicong 已提交
522
      continue;
L
Liu Jicong 已提交
523
    }
L
Liu Jicong 已提交
524

525
    int64_t st = taosGetTimestampMs();
526
    qDebug("s-task:%s start to process batch of blocks, num:%d", id, batchSize);
H
Haojun Liao 已提交
527

528 529
    {
      // set input
530
      void* pExecutor = pTask->exec.pExecutor;
531 532 533 534 535 536

      const SStreamQueueItem* pItem = pInput;
      if (pItem->type == STREAM_INPUT__GET_RES) {
        const SStreamTrigger* pTrigger = (const SStreamTrigger*)pInput;
        qSetMultiStreamInput(pExecutor, pTrigger->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
      } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
537
        ASSERT(pTask->info.taskLevel == TASK_LEVEL__SOURCE);
538 539
        const SStreamDataSubmit* pSubmit = (const SStreamDataSubmit*)pInput;
        qSetMultiStreamInput(pExecutor, &pSubmit->submit, 1, STREAM_INPUT__DATA_SUBMIT);
540
        qDebug("s-task:%s set submit blocks as source block completed, %p %p len:%d ver:%" PRId64, id, pSubmit,
541 542 543 544 545 546
               pSubmit->submit.msgStr, pSubmit->submit.msgLen, pSubmit->submit.ver);
      } else if (pItem->type == STREAM_INPUT__DATA_BLOCK || pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
        const SStreamDataBlock* pBlock = (const SStreamDataBlock*)pInput;

        SArray* pBlockList = pBlock->blocks;
        int32_t numOfBlocks = taosArrayGetSize(pBlockList);
547
        qDebug("s-task:%s set sdata blocks as input num:%d, ver:%" PRId64, id, numOfBlocks, pBlock->sourceVer);
548 549 550 551 552 553
        qSetMultiStreamInput(pExecutor, pBlockList->pData, numOfBlocks, STREAM_INPUT__DATA_BLOCK);
      } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
        const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)pInput;

        SArray* pBlockList = pMerged->submits;
        int32_t numOfBlocks = taosArrayGetSize(pBlockList);
554
        qDebug("s-task:%s %p set (merged) submit blocks as a batch, numOfBlocks:%d", id, pTask, numOfBlocks);
555 556 557 558 559 560
        qSetMultiStreamInput(pExecutor, pBlockList->pData, numOfBlocks, STREAM_INPUT__MERGED_SUBMIT);
      } else if (pItem->type == STREAM_INPUT__REF_DATA_BLOCK) {
        const SStreamRefDataBlock* pRefBlock = (const SStreamRefDataBlock*)pInput;
        qSetMultiStreamInput(pExecutor, pRefBlock->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
      } else {
        ASSERT(0);
L
Liu Jicong 已提交
561
      }
562
    }
563

564 565 566
    int64_t resSize = 0;
    int32_t totalBlocks = 0;
    streamTaskExecImpl(pTask, pInput, &resSize, &totalBlocks);
L
Liu Jicong 已提交
567

568
    double  el = (taosGetTimestampMs() - st) / 1000.0;
569 570
    qDebug("s-task:%s batch of input blocks exec end, elapsed time:%.2fs, result size:%.2fMiB, numOfBlocks:%d",
           id, el, resSize / 1048576.0, totalBlocks);
571

572
    streamFreeQitem(pInput);
L
Liu Jicong 已提交
573
  }
574

L
Liu Jicong 已提交
575
  return 0;
L
Liu Jicong 已提交
576 577
}

578
bool streamTaskIsIdle(const SStreamTask* pTask) {
579
  return (pTask->status.schedStatus == TASK_SCHED_STATUS__INACTIVE/* && pTask->status.taskStatus != TASK_STATUS__HALT*/);
580 581
}

L
Liu Jicong 已提交
582
int32_t streamTryExec(SStreamTask* pTask) {
583
  // this function may be executed by multi-threads, so status check is required.
L
Liu Jicong 已提交
584
  int8_t schedStatus =
585
      atomic_val_compare_exchange_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__WAITING, TASK_SCHED_STATUS__ACTIVE);
586

L
Liu Jicong 已提交
587 588
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
    int32_t code = streamExecForAll(pTask);
589
    if (code < 0) {  // todo this status shoudl be removed
590
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__FAILED);
L
Liu Jicong 已提交
591 592
      return -1;
    }
593

594
    // todo the task should be commit here
595
    atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
596
    qDebug("s-task:%s exec completed, status:%s, sched-status:%d", pTask->id.idStr, streamGetTaskStatusStr(pTask->status.taskStatus),
597
           pTask->status.schedStatus);
L
Liu Jicong 已提交
598

dengyihao's avatar
dengyihao 已提交
599 600
    if (!taosQueueEmpty(pTask->inputQueue->queue) && (!streamTaskShouldStop(&pTask->status)) &&
        (!streamTaskShouldPause(&pTask->status))) {
L
Liu Jicong 已提交
601
      streamSchedExec(pTask);
L
Liu Jicong 已提交
602 603
    }
  }
604

L
Liu Jicong 已提交
605
  return 0;
L
Liu Jicong 已提交
606
}
L
liuyao 已提交
607 608

int32_t streamTaskReleaseState(SStreamTask* pTask) {
609
  qDebug("s-task:%s release exec state", pTask->id.idStr);
H
Haojun Liao 已提交
610 611 612 613 614 615 616
  void* pExecutor = pTask->exec.pExecutor;
  if (pExecutor != NULL) {
    int32_t code = qStreamOperatorReleaseState(pExecutor);
    return code;
  } else {
    return TSDB_CODE_SUCCESS;
  }
L
liuyao 已提交
617 618 619
}

int32_t streamTaskReloadState(SStreamTask* pTask) {
620
  qDebug("s-task:%s reload exec state", pTask->id.idStr);
H
Haojun Liao 已提交
621 622 623 624 625 626 627
  void* pExecutor = pTask->exec.pExecutor;
  if (pExecutor != NULL) {
    int32_t code = qStreamOperatorReloadState(pExecutor);
    return code;
  } else {
    return TSDB_CODE_SUCCESS;
  }
L
liuyao 已提交
628
}
H
Haojun Liao 已提交
629 630 631 632 633 634 635 636 637 638

int32_t streamAlignTransferState(SStreamTask* pTask) {
  int32_t numOfUpstream = taosArrayGetSize(pTask->pUpstreamEpInfoList);
  int32_t old = atomic_val_compare_exchange_32(&pTask->transferStateAlignCnt, 0, numOfUpstream);
  if (old == 0) {
    qDebug("s-task:%s set the transfer state aligncnt %d", pTask->id.idStr, numOfUpstream);
  }

  return atomic_sub_fetch_32(&pTask->transferStateAlignCnt, 1);
}