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

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

5
54liuyao 已提交
18 19
#define STREAM_EXEC_MAX_BATCH_NUM 100

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

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

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

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

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

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

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

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

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

  void* exec = pTask->exec.executor;

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

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

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

      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 已提交
144
    SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
145 146 147 148 149 150 151 152 153
    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 已提交
154 155 156 157

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

#if 0
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
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;
179
    }
180
  }
181

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

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

193 194 195 196
  // exec impl

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

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

L
Liu Jicong 已提交
233
    if (pTask->taskStatus == TASK_STATUS__DROPPING) {
234
      if (input) streamFreeQitem(input);
L
Liu Jicong 已提交
235
      return 0;
L
Liu Jicong 已提交
236
    }
L
Liu Jicong 已提交
237

238
    if (input == NULL) {
L
Liu Jicong 已提交
239 240 241
      break;
    }

242
    if (pTask->taskLevel == TASK_LEVEL__SINK) {
243
      ASSERT(((SStreamQueueItem*)input)->type == STREAM_INPUT__DATA_BLOCK);
244
      streamTaskOutput(pTask, input);
L
Liu Jicong 已提交
245
      continue;
L
Liu Jicong 已提交
246
    }
L
Liu Jicong 已提交
247

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

L
Liu Jicong 已提交
250
    qDebug("stream task %d exec begin, msg batch: %d", pTask->taskId, batchCnt);
251
    streamTaskExecImpl(pTask, input, pRes);
L
Liu Jicong 已提交
252
    qDebug("stream task %d exec end", pTask->taskId);
253

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

264 265
      if (((SStreamQueueItem*)input)->type == STREAM_INPUT__DATA_SUBMIT) {
        SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)input;
L
Liu Jicong 已提交
266 267
        qRes->childId = pTask->selfChildId;
        qRes->sourceVer = pSubmit->ver;
268 269
      } else if (((SStreamQueueItem*)input)->type == STREAM_INPUT__MERGED_SUBMIT) {
        SStreamMergedSubmit* pMerged = (SStreamMergedSubmit*)input;
L
Liu Jicong 已提交
270 271
        qRes->childId = pTask->selfChildId;
        qRes->sourceVer = pMerged->ver;
L
Liu Jicong 已提交
272
      }
L
Liu Jicong 已提交
273 274 275

      if (streamTaskOutput(pTask, qRes) < 0) {
        taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
276
        streamFreeQitem(input);
L
Liu Jicong 已提交
277
        taosFreeQitem(qRes);
L
Liu Jicong 已提交
278 279
        return -1;
      }
L
Liu Jicong 已提交
280 281
    } else {
      taosArrayDestroy(pRes);
L
Liu Jicong 已提交
282
    }
283
    streamFreeQitem(input);
L
Liu Jicong 已提交
284
  }
L
Liu Jicong 已提交
285
  return 0;
L
Liu Jicong 已提交
286 287
}

L
Liu Jicong 已提交
288 289 290 291 292 293 294 295 296 297
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 已提交
298

L
Liu Jicong 已提交
299 300
    if (!taosQueueEmpty(pTask->inputQueue->queue)) {
      streamSchedExec(pTask);
L
Liu Jicong 已提交
301 302
    }
  }
L
Liu Jicong 已提交
303
  return 0;
L
Liu Jicong 已提交
304
}