streamExec.c 9.4 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/>.
 */

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

L
Liu Jicong 已提交
18
static int32_t streamTaskExecImpl(SStreamTask* pTask, const void* data, SArray* pRes) {
L
Liu Jicong 已提交
19 20
  int32_t code;
  void*   exec = pTask->exec.executor;
L
Liu Jicong 已提交
21 22

  // set input
L
Liu Jicong 已提交
23
  const SStreamQueueItem* pItem = (const SStreamQueueItem*)data;
L
Liu Jicong 已提交
24
  if (pItem->type == STREAM_INPUT__GET_RES) {
L
Liu Jicong 已提交
25
    const SStreamTrigger* pTrigger = (const SStreamTrigger*)data;
L
Liu Jicong 已提交
26
    qSetMultiStreamInput(exec, pTrigger->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
27
  } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
28
    ASSERT(pTask->taskLevel == TASK_LEVEL__SOURCE);
L
Liu Jicong 已提交
29
    const SStreamDataSubmit* pSubmit = (const SStreamDataSubmit*)data;
J
jiajingbin 已提交
30
    qDebug("task %d %p set submit input %p %p %d 1", pTask->taskId, pTask, pSubmit, pSubmit->data, *pSubmit->dataRef);
L
Liu Jicong 已提交
31
    qSetMultiStreamInput(exec, pSubmit->data, 1, STREAM_INPUT__DATA_SUBMIT);
32
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK || pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
L
Liu Jicong 已提交
33 34
    const SStreamDataBlock* pBlock = (const SStreamDataBlock*)data;
    SArray*                 blocks = pBlock->blocks;
L
Liu Jicong 已提交
35
    qDebug("task %d %p set ssdata input", pTask->taskId, pTask);
L
Liu Jicong 已提交
36
    qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_INPUT__DATA_BLOCK);
37
  } else if (pItem->type == STREAM_INPUT__MERGED_SUBMIT) {
L
Liu Jicong 已提交
38 39
    const SStreamMergedSubmit* pMerged = (const SStreamMergedSubmit*)data;
    SArray*                    blocks = pMerged->reqs;
L
Liu Jicong 已提交
40
    qDebug("task %d %p set submit input (merged), batch num: %d", pTask->taskId, pTask, (int32_t)blocks->size);
L
Liu Jicong 已提交
41
    qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_INPUT__MERGED_SUBMIT);
L
Liu Jicong 已提交
42 43 44
  } else if (pItem->type == STREAM_INPUT__REF_DATA_BLOCK) {
    const SStreamRefDataBlock* pRefBlock = (const SStreamRefDataBlock*)data;
    qSetMultiStreamInput(exec, pRefBlock->pBlock, 1, STREAM_INPUT__DATA_BLOCK);
45
  } else {
46
    ASSERT(0);
L
Liu Jicong 已提交
47 48 49 50
  }

  // exec
  while (1) {
L
Liu Jicong 已提交
51 52 53 54
    if (pTask->taskStatus == TASK_STATUS__DROPPING) {
      return 0;
    }

L
Liu Jicong 已提交
55 56
    SSDataBlock* output = NULL;
    uint64_t     ts = 0;
L
Liu Jicong 已提交
57
    if ((code = qExecTask(exec, &output, &ts)) < 0) {
58
      /*ASSERT(false);*/
L
Liu Jicong 已提交
59 60
      qError("unexpected stream execution, stream %" PRId64 " task: %d,  since %s", pTask->streamId, pTask->taskId,
             terrstr());
L
Liu Jicong 已提交
61
      continue;
L
Liu Jicong 已提交
62
    }
63
    if (output == NULL) {
5
54liuyao 已提交
64
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
L
Liu Jicong 已提交
65 66
        SSDataBlock             block = {0};
        const SStreamDataBlock* pRetrieveBlock = (const SStreamDataBlock*)data;
67
        ASSERT(taosArrayGetSize(pRetrieveBlock->blocks) == 1);
L
Liu Jicong 已提交
68
        assignOneDataBlock(&block, taosArrayGet(pRetrieveBlock->blocks, 0));
L
Liu Jicong 已提交
69
        block.info.type = STREAM_PULL_OVER;
L
Liu Jicong 已提交
70 71
        block.info.childId = pTask->selfChildId;
        taosArrayPush(pRes, &block);
L
Liu Jicong 已提交
72

S
Shengliang Guan 已提交
73
        qDebug("task %d(child %d) processed retrieve, reqId %" PRId64, pTask->taskId, pTask->selfChildId,
L
Liu Jicong 已提交
74
               pRetrieveBlock->reqId);
75 76 77
      }
      break;
    }
L
Liu Jicong 已提交
78 79 80 81 82 83 84 85

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

86
    qDebug("task %d(child %d) executed and get block", pTask->taskId, pTask->selfChildId);
J
jiajingbin 已提交
87

L
Liu Jicong 已提交
88 89 90 91
    SSDataBlock block = {0};
    assignOneDataBlock(&block, output);
    block.info.childId = pTask->selfChildId;
    taosArrayPush(pRes, &block);
L
Liu Jicong 已提交
92 93 94 95
  }
  return 0;
}

96
int32_t streamScanExec(SStreamTask* pTask, int32_t batchSz) {
97
  ASSERT(pTask->taskLevel == TASK_LEVEL__SOURCE);
98 99 100

  void* exec = pTask->exec.executor;

L
Liu Jicong 已提交
101
  qSetStreamOpOpen(exec);
L
Liu Jicong 已提交
102
  bool finished = false;
L
Liu Jicong 已提交
103

104 105 106 107 108 109 110 111 112
  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
Liu Jicong 已提交
113 114 115 116
      if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
        return 0;
      }

117 118 119
      SSDataBlock* output = NULL;
      uint64_t     ts = 0;
      if (qExecTask(exec, &output, &ts) < 0) {
L
Liu Jicong 已提交
120
        return -1;
121
      }
L
Liu Jicong 已提交
122
      if (output == NULL) {
L
Liu Jicong 已提交
123 124 125 126 127
        if (qStreamRecoverScanFinished(exec)) {
          finished = true;
        } else {
          qSetStreamOpOpen(exec);
        }
L
Liu Jicong 已提交
128 129
        break;
      }
130 131 132 133 134 135 136 137 138 139 140 141

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

      if (++batchCnt >= batchSz) break;
    }
    if (taosArrayGetSize(pRes) == 0) {
      taosArrayDestroy(pRes);
      break;
    }
S
Shengliang Guan 已提交
142
    SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
143 144 145 146 147 148 149 150 151
    if (qRes == NULL) {
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      return -1;
    }

    qRes->type = STREAM_INPUT__DATA_BLOCK;
    qRes->blocks = pRes;
    streamTaskOutput(pTask, qRes);
L
Liu Jicong 已提交
152 153 154 155

    if (pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH || pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {
      streamDispatch(pTask);
    }
L
Liu Jicong 已提交
156
    if (finished) break;
157 158 159 160 161
  }
  return 0;
}

#if 0
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
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) {
    qDebug("task: %d, stream task exec over, queue empty", pTask->taskId);
    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;
177
    }
178
  }
179

180 181 182 183 184
  // if drop
  if (pItem->type == STREAM_INPUT__DESTROY) {
    // set status drop
    return -1;
  }
185

186
  if (pTask->taskLevel == TASK_LEVEL__SINK) {
187
    ASSERT(((SStreamQueueItem*)pItem)->type == STREAM_INPUT__DATA_BLOCK);
188
    streamTaskOutput(pTask, (SStreamDataBlock*)pItem);
189 190
  }

191 192 193 194
  // exec impl

  // output
  // try dispatch
195 196
  return 0;
}
197
#endif
L
Liu Jicong 已提交
198

L
Liu Jicong 已提交
199
int32_t streamExecForAll(SStreamTask* pTask) {
L
Liu Jicong 已提交
200
  while (1) {
L
Liu Jicong 已提交
201
    int32_t batchCnt = 1;
202
    void*   input = NULL;
L
Liu Jicong 已提交
203 204 205
    while (1) {
      SStreamQueueItem* qItem = streamQueueNextItem(pTask->inputQueue);
      if (qItem == NULL) {
L
Liu Jicong 已提交
206
        qDebug("stream task exec over, queue empty, task: %d", pTask->taskId);
L
Liu Jicong 已提交
207
        break;
L
Liu Jicong 已提交
208
      }
209 210
      if (input == NULL) {
        input = qItem;
211
        streamQueueProcessSuccess(pTask->inputQueue);
212
        if (pTask->taskLevel == TASK_LEVEL__SINK) {
L
Liu Jicong 已提交
213
          break;
L
Liu Jicong 已提交
214
        }
L
Liu Jicong 已提交
215
      } else {
J
jiacy-jcy 已提交
216
        void* newRet;
217
        if ((newRet = streamMergeQueueItem(input, qItem)) == NULL) {
L
Liu Jicong 已提交
218 219 220
          streamQueueProcessFail(pTask->inputQueue);
          break;
        } else {
L
Liu Jicong 已提交
221
          batchCnt++;
222
          input = newRet;
L
Liu Jicong 已提交
223 224
          streamQueueProcessSuccess(pTask->inputQueue);
        }
L
Liu Jicong 已提交
225 226
      }
    }
227

L
Liu Jicong 已提交
228
    if (pTask->taskStatus == TASK_STATUS__DROPPING) {
229
      if (input) streamFreeQitem(input);
L
Liu Jicong 已提交
230
      return 0;
L
Liu Jicong 已提交
231
    }
L
Liu Jicong 已提交
232

233
    if (input == NULL) {
L
Liu Jicong 已提交
234 235 236
      break;
    }

237
    if (pTask->taskLevel == TASK_LEVEL__SINK) {
238
      ASSERT(((SStreamQueueItem*)input)->type == STREAM_INPUT__DATA_BLOCK);
239
      streamTaskOutput(pTask, input);
L
Liu Jicong 已提交
240
      continue;
L
Liu Jicong 已提交
241
    }
L
Liu Jicong 已提交
242

L
Liu Jicong 已提交
243 244
    SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));

L
Liu Jicong 已提交
245
    qDebug("stream task %d exec begin, msg batch: %d", pTask->taskId, batchCnt);
246
    streamTaskExecImpl(pTask, input, pRes);
L
Liu Jicong 已提交
247
    qDebug("stream task %d exec end", pTask->taskId);
248

L
Liu Jicong 已提交
249
    if (taosArrayGetSize(pRes) != 0) {
S
Shengliang Guan 已提交
250
      SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
L
Liu Jicong 已提交
251
      if (qRes == NULL) {
L
Liu Jicong 已提交
252
        taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
253
        streamFreeQitem(input);
L
Liu Jicong 已提交
254
        return -1;
L
Liu Jicong 已提交
255 256 257
      }
      qRes->type = STREAM_INPUT__DATA_BLOCK;
      qRes->blocks = pRes;
L
Liu Jicong 已提交
258

259 260
      if (((SStreamQueueItem*)input)->type == STREAM_INPUT__DATA_SUBMIT) {
        SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)input;
L
Liu Jicong 已提交
261 262
        qRes->childId = pTask->selfChildId;
        qRes->sourceVer = pSubmit->ver;
263 264
      } else if (((SStreamQueueItem*)input)->type == STREAM_INPUT__MERGED_SUBMIT) {
        SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)input;
L
Liu Jicong 已提交
265 266
        qRes->childId = pTask->selfChildId;
        qRes->sourceVer = pMerged->ver;
L
Liu Jicong 已提交
267
      }
L
Liu Jicong 已提交
268 269 270

      if (streamTaskOutput(pTask, qRes) < 0) {
        taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
271
        streamFreeQitem(input);
L
Liu Jicong 已提交
272
        taosFreeQitem(qRes);
L
Liu Jicong 已提交
273 274
        return -1;
      }
L
Liu Jicong 已提交
275 276
    } else {
      taosArrayDestroy(pRes);
L
Liu Jicong 已提交
277
    }
278
    streamFreeQitem(input);
L
Liu Jicong 已提交
279
  }
L
Liu Jicong 已提交
280
  return 0;
L
Liu Jicong 已提交
281 282
}

L
Liu Jicong 已提交
283 284 285 286 287 288 289 290 291 292
int32_t streamTryExec(SStreamTask* pTask) {
  int8_t schedStatus =
      atomic_val_compare_exchange_8(&pTask->schedStatus, TASK_SCHED_STATUS__WAITING, TASK_SCHED_STATUS__ACTIVE);
  if (schedStatus == TASK_SCHED_STATUS__WAITING) {
    int32_t code = streamExecForAll(pTask);
    if (code < 0) {
      atomic_store_8(&pTask->schedStatus, TASK_SCHED_STATUS__FAILED);
      return -1;
    }
    atomic_store_8(&pTask->schedStatus, TASK_SCHED_STATUS__INACTIVE);
L
Liu Jicong 已提交
293

L
Liu Jicong 已提交
294 295
    if (!taosQueueEmpty(pTask->inputQueue->queue)) {
      streamSchedExec(pTask);
L
Liu Jicong 已提交
296 297
    }
  }
L
Liu Jicong 已提交
298
  return 0;
L
Liu Jicong 已提交
299
}