streamExec.c 8.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 21
  void* exec = pTask->exec.executor;

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

  // exec
H
Haojun Liao 已提交
46
  SArray* pResList = taosArrayInit(4, POINTER_BYTES);
L
Liu Jicong 已提交
47 48 49
  while (1) {
    SSDataBlock* output = NULL;
    uint64_t     ts = 0;
H
Haojun Liao 已提交
50 51 52

    taosArrayClear(pResList);
    if (qExecTask(exec, pResList, &ts) < 0) {
L
Liu Jicong 已提交
53 54
      ASSERT(false);
    }
H
Haojun Liao 已提交
55 56

    if (taosArrayGetSize(pResList) == 0) {
5
54liuyao 已提交
57
      if (pItem->type == STREAM_INPUT__DATA_RETRIEVE) {
L
Liu Jicong 已提交
58 59
        SSDataBlock             block = {0};
        const SStreamDataBlock* pRetrieveBlock = (const SStreamDataBlock*)data;
5
54liuyao 已提交
60
        ASSERT(taosArrayGetSize(pRetrieveBlock->blocks) == 1);
L
Liu Jicong 已提交
61
        assignOneDataBlock(&block, taosArrayGet(pRetrieveBlock->blocks, 0));
L
Liu Jicong 已提交
62
        block.info.type = STREAM_PULL_OVER;
L
Liu Jicong 已提交
63 64
        block.info.childId = pTask->selfChildId;
        taosArrayPush(pRes, &block);
L
Liu Jicong 已提交
65

S
Shengliang Guan 已提交
66
        qDebug("task %d(child %d) processed retrieve, reqId %" PRId64, pTask->taskId, pTask->selfChildId,
L
Liu Jicong 已提交
67
               pRetrieveBlock->reqId);
68 69 70
      }
      break;
    }
L
Liu Jicong 已提交
71

H
Haojun Liao 已提交
72
    output = taosArrayGetP(pResList, 0);
L
Liu Jicong 已提交
73 74 75 76 77 78 79
    if (output->info.type == STREAM_RETRIEVE) {
      if (streamBroadcastToChildren(pTask, output) < 0) {
        // TODO
      }
      continue;
    }

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

L
Liu Jicong 已提交
82 83 84 85
    SSDataBlock block = {0};
    assignOneDataBlock(&block, output);
    block.info.childId = pTask->selfChildId;
    taosArrayPush(pRes, &block);
L
Liu Jicong 已提交
86
  }
H
Haojun Liao 已提交
87 88

  taosArrayDestroy(pResList);
L
Liu Jicong 已提交
89 90 91
  return 0;
}

L
Liu Jicong 已提交
92
#if 0
L
Liu Jicong 已提交
93 94 95 96 97
static FORCE_INLINE int32_t streamUpdateVer(SStreamTask* pTask, SStreamDataBlock* pBlock) {
  ASSERT(pBlock->type == STREAM_INPUT__DATA_BLOCK);
  int32_t             childId = pBlock->childId;
  int64_t             ver = pBlock->sourceVer;
  SStreamChildEpInfo* pChildInfo = taosArrayGetP(pTask->childEpInfo, childId);
L
Liu Jicong 已提交
98
  /*pChildInfo-> = ver;*/
L
Liu Jicong 已提交
99 100
  return 0;
}
L
Liu Jicong 已提交
101
#endif
L
Liu Jicong 已提交
102

103
int32_t streamPipelineExec(SStreamTask* pTask, int32_t batchNum) {
104
  ASSERT(pTask->taskLevel != TASK_LEVEL__SINK);
105 106 107

  void* exec = pTask->exec.executor;

H
Haojun Liao 已提交
108
  SArray* pResList = taosArrayInit(4, POINTER_BYTES);
109 110 111 112 113 114 115 116 117
  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) {
H
Haojun Liao 已提交
118 119 120
      uint64_t ts = 0;
      taosArrayClear(pResList);
      if (qExecTask(exec, pResList, &ts) < 0) {
121 122
        ASSERT(0);
      }
H
Haojun Liao 已提交
123 124

      if (taosArrayGetSize(pResList) == 0) break;
125 126

      SSDataBlock block = {0};
H
Haojun Liao 已提交
127 128
      SSDataBlock* output = taosArrayGetP(pResList, 0);

129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      assignOneDataBlock(&block, output);
      block.info.childId = pTask->selfChildId;
      taosArrayPush(pRes, &block);

      if (++batchCnt >= batchNum) break;
    }
    if (taosArrayGetSize(pRes) == 0) {
      taosArrayDestroy(pRes);
      break;
    }
    SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM);
    if (qRes == NULL) {
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
      return -1;
    }

    qRes->type = STREAM_INPUT__DATA_BLOCK;
    qRes->blocks = pRes;
    qRes->childId = pTask->selfChildId;

    if (streamTaskOutput(pTask, qRes) < 0) {
      taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
      taosFreeQitem(qRes);
      return -1;
    }

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

  return 0;
}
L
Liu Jicong 已提交
162 163
// TODO: handle version
int32_t streamExecForAll(SStreamTask* pTask) {
L
Liu Jicong 已提交
164
  while (1) {
L
Liu Jicong 已提交
165
    int32_t cnt = 1;
L
Liu Jicong 已提交
166 167 168 169
    void*   data = NULL;
    while (1) {
      SStreamQueueItem* qItem = streamQueueNextItem(pTask->inputQueue);
      if (qItem == NULL) {
L
Liu Jicong 已提交
170
        qDebug("stream task exec over, queue empty, task: %d", pTask->taskId);
L
Liu Jicong 已提交
171
        break;
L
Liu Jicong 已提交
172 173 174
      }
      if (data == NULL) {
        data = qItem;
175
        streamQueueProcessSuccess(pTask->inputQueue);
176
        if (pTask->taskLevel == TASK_LEVEL__SINK) {
L
Liu Jicong 已提交
177
          break;
L
Liu Jicong 已提交
178
        }
L
Liu Jicong 已提交
179
      } else {
J
jiacy-jcy 已提交
180 181
        void* newRet;
        if ((newRet = streamAppendQueueItem(data, qItem)) == NULL) {
L
Liu Jicong 已提交
182 183 184 185
          streamQueueProcessFail(pTask->inputQueue);
          break;
        } else {
          cnt++;
J
jiacy-jcy 已提交
186
          data = newRet;
L
Liu Jicong 已提交
187 188 189
          /*streamUpdateVer(pTask, (SStreamDataBlock*)qItem);*/
          streamQueueProcessSuccess(pTask->inputQueue);
        }
L
Liu Jicong 已提交
190 191
      }
    }
192

L
Liu Jicong 已提交
193 194
    if (pTask->taskStatus == TASK_STATUS__DROPPING) {
      if (data) streamFreeQitem(data);
L
Liu Jicong 已提交
195
      return 0;
L
Liu Jicong 已提交
196
    }
L
Liu Jicong 已提交
197

L
Liu Jicong 已提交
198
    if (data == NULL) {
L
Liu Jicong 已提交
199 200 201
      break;
    }

202
    if (pTask->taskLevel == TASK_LEVEL__SINK) {
L
Liu Jicong 已提交
203 204 205
      ASSERT(((SStreamQueueItem*)data)->type == STREAM_INPUT__DATA_BLOCK);
      streamTaskOutput(pTask, data);
      continue;
L
Liu Jicong 已提交
206
    }
L
Liu Jicong 已提交
207

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

L
Liu Jicong 已提交
210 211 212
    qDebug("stream task %d exec begin, msg batch: %d", pTask->taskId, cnt);
    streamTaskExecImpl(pTask, data, pRes);
    qDebug("stream task %d exec end", pTask->taskId);
213

L
Liu Jicong 已提交
214 215
    streamFreeQitem(data);

L
Liu Jicong 已提交
216 217 218
    if (taosArrayGetSize(pRes) != 0) {
      SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM);
      if (qRes == NULL) {
219
        // TODO log failed ver
L
Liu Jicong 已提交
220 221
        streamQueueProcessFail(pTask->inputQueue);
        taosArrayDestroy(pRes);
L
Liu Jicong 已提交
222
        return -1;
L
Liu Jicong 已提交
223 224 225
      }
      qRes->type = STREAM_INPUT__DATA_BLOCK;
      qRes->blocks = pRes;
L
Liu Jicong 已提交
226

L
Liu Jicong 已提交
227 228 229 230 231
      if (((SStreamQueueItem*)data)->type == STREAM_INPUT__DATA_SUBMIT) {
        SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)data;
        qRes->childId = pTask->selfChildId;
        qRes->sourceVer = pSubmit->ver;
      }
L
Liu Jicong 已提交
232 233 234 235 236 237 238 239

      if (streamTaskOutput(pTask, qRes) < 0) {
        // TODO save failed ver
        /*streamQueueProcessFail(pTask->inputQueue);*/
        taosArrayDestroyEx(pRes, (FDelete)blockDataFreeRes);
        taosFreeQitem(qRes);
        return -1;
      }
L
Liu Jicong 已提交
240
      /*streamQueueProcessSuccess(pTask->inputQueue);*/
L
Liu Jicong 已提交
241
    }
L
Liu Jicong 已提交
242
  }
L
Liu Jicong 已提交
243
  return 0;
L
Liu Jicong 已提交
244 245
}

L
Liu Jicong 已提交
246 247 248 249 250 251 252 253 254 255
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 已提交
256

L
Liu Jicong 已提交
257 258
    if (!taosQueueEmpty(pTask->inputQueue->queue)) {
      streamSchedExec(pTask);
L
Liu Jicong 已提交
259 260
    }
  }
L
Liu Jicong 已提交
261
  return 0;
L
Liu Jicong 已提交
262
}