tstream.c 10.4 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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/>.
 */

#include "tstream.h"
#include "executor.h"

int32_t streamExecTask(SStreamTask* pTask, SMsgCb* pMsgCb, const void* input, int32_t inputType, int32_t workId) {
  SArray* pRes = NULL;
  // source
  if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK && pTask->sourceType != TASK_SOURCE__SCAN) return 0;

  // exec
L
Liu Jicong 已提交
25
  if (pTask->execType != TASK_EXEC__NONE) {
L
Liu Jicong 已提交
26 27 28
    ASSERT(workId < pTask->exec.numOfRunners);
    void* exec = pTask->exec.runners[workId].executor;
    pRes = taosArrayInit(0, sizeof(SSDataBlock));
L
Liu Jicong 已提交
29 30 31
    if (pRes == NULL) {
      return -1;
    }
L
Liu Jicong 已提交
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
    if (inputType == STREAM_DATA_TYPE_SUBMIT_BLOCK) {
      qSetStreamInput(exec, input, inputType);
      while (1) {
        SSDataBlock* output;
        uint64_t     ts;
        if (qExecTask(exec, &output, &ts) < 0) {
          ASSERT(false);
        }
        if (output == NULL) {
          break;
        }
        taosArrayPush(pRes, output);
      }
    } else if (inputType == STREAM_DATA_TYPE_SSDATA_BLOCK) {
      const SArray* blocks = (const SArray*)input;
      int32_t       sz = taosArrayGetSize(blocks);
      for (int32_t i = 0; i < sz; i++) {
        SSDataBlock* pBlock = taosArrayGet(blocks, i);
        qSetStreamInput(exec, pBlock, inputType);
        while (1) {
          SSDataBlock* output;
          uint64_t     ts;
          if (qExecTask(exec, &output, &ts) < 0) {
            ASSERT(false);
          }
          if (output == NULL) {
            break;
          }
          taosArrayPush(pRes, output);
        }
      }
    } else {
      ASSERT(0);
    }
  } else {
    ASSERT(inputType == STREAM_DATA_TYPE_SSDATA_BLOCK);
    pRes = (SArray*)input;
  }

  // sink
  if (pTask->sinkType == TASK_SINK__TABLE) {
    //
  } else if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
75
    pTask->smaSink.smaHandle(pTask->ahandle, pTask->smaSink.smaId, pRes);
L
Liu Jicong 已提交
76 77 78 79 80 81 82 83 84 85
    //
  } else if (pTask->sinkType == TASK_SINK__FETCH) {
    //
  } else if (pTask->sinkType == TASK_SINK__SHOW) {
    blockDebugShowData(pRes);
  } else {
    ASSERT(pTask->sinkType == TASK_SINK__NONE);
  }

  // dispatch
L
Liu Jicong 已提交
86
  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
L
Liu Jicong 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
    SStreamTaskExecReq req = {
        .streamId = pTask->streamId,
        .taskId = pTask->taskId,
        .data = pRes,
    };

    int32_t tlen = sizeof(SMsgHead) + tEncodeSStreamTaskExecReq(NULL, &req);
    void*   buf = rpcMallocCont(tlen);

    if (buf == NULL) {
      return -1;
    }
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
    tEncodeSStreamTaskExecReq(&abuf, &req);

    SRpcMsg dispatchMsg = {
        .pCont = buf,
        .contLen = tlen,
        .code = 0,
        .msgType = pTask->dispatchMsgType,
    };
L
Liu Jicong 已提交
108 109 110 111 112 113 114 115 116

    int32_t qType;
    if (pTask->dispatchMsgType == TDMT_VND_TASK_PIPE_EXEC || pTask->dispatchMsgType == TDMT_SND_TASK_PIPE_EXEC) {
      qType = FETCH_QUEUE;
    } else if (pTask->dispatchMsgType == TDMT_VND_TASK_MERGE_EXEC ||
               pTask->dispatchMsgType == TDMT_SND_TASK_MERGE_EXEC) {
      qType = MERGE_QUEUE;
    } else if (pTask->dispatchMsgType == TDMT_VND_TASK_WRITE_EXEC) {
      qType = WRITE_QUEUE;
L
Liu Jicong 已提交
117 118 119
    } else {
      ASSERT(0);
    }
L
Liu Jicong 已提交
120 121 122 123 124
    tmsgPutToQueue(pMsgCb, qType, &dispatchMsg);

  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
    SStreamTaskExecReq req = {
        .streamId = pTask->streamId,
L
Liu Jicong 已提交
125
        .taskId = pTask->fixedEpDispatcher.taskId,
L
Liu Jicong 已提交
126 127 128 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 155
        .data = pRes,
    };

    int32_t tlen = sizeof(SMsgHead) + tEncodeSStreamTaskExecReq(NULL, &req);
    void*   buf = rpcMallocCont(tlen);

    if (buf == NULL) {
      return -1;
    }

    ((SMsgHead*)buf)->vgId = htonl(pTask->fixedEpDispatcher.nodeId);
    void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
    tEncodeSStreamTaskExecReq(&abuf, &req);

    SRpcMsg dispatchMsg = {
        .pCont = buf,
        .contLen = tlen,
        .code = 0,
        .msgType = pTask->dispatchMsgType,
    };

    SEpSet* pEpSet = &pTask->fixedEpDispatcher.epSet;

    tmsgSendReq(pMsgCb, pEpSet, &dispatchMsg);

  } else if (pTask->dispatchType == TASK_DISPATCH__SHUFFLE) {
    // TODO

  } else {
    ASSERT(pTask->dispatchType == TASK_DISPATCH__NONE);
L
Liu Jicong 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175
  }
  return 0;
}

int32_t tEncodeSStreamTaskExecReq(void** buf, const SStreamTaskExecReq* pReq) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI64(buf, pReq->streamId);
  tlen += taosEncodeFixedI32(buf, pReq->taskId);
  tlen += tEncodeDataBlocks(buf, pReq->data);
  return tlen;
}

void* tDecodeSStreamTaskExecReq(const void* buf, SStreamTaskExecReq* pReq) {
  buf = taosDecodeFixedI64(buf, &pReq->streamId);
  buf = taosDecodeFixedI32(buf, &pReq->taskId);
  buf = tDecodeDataBlocks(buf, &pReq->data);
  return (void*)buf;
}

void tFreeSStreamTaskExecReq(SStreamTaskExecReq* pReq) { taosArrayDestroy(pReq->data); }
L
Liu Jicong 已提交
176 177

SStreamTask* tNewSStreamTask(int64_t streamId) {
wafwerar's avatar
wafwerar 已提交
178
  SStreamTask* pTask = (SStreamTask*)taosMemoryCalloc(1, sizeof(SStreamTask));
L
Liu Jicong 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200
  if (pTask == NULL) {
    return NULL;
  }
  pTask->taskId = tGenIdPI32();
  pTask->streamId = streamId;
  pTask->status = STREAM_TASK_STATUS__RUNNING;
  /*pTask->qmsg = NULL;*/
  return pTask;
}

int32_t tEncodeSStreamTask(SCoder* pEncoder, const SStreamTask* pTask) {
  /*if (tStartEncode(pEncoder) < 0) return -1;*/
  if (tEncodeI64(pEncoder, pTask->streamId) < 0) return -1;
  if (tEncodeI32(pEncoder, pTask->taskId) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->status) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->sourceType) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->execType) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->sinkType) < 0) return -1;
  if (tEncodeI8(pEncoder, pTask->dispatchType) < 0) return -1;
  if (tEncodeI16(pEncoder, pTask->dispatchMsgType) < 0) return -1;
  if (tEncodeI32(pEncoder, pTask->downstreamTaskId) < 0) return -1;

L
Liu Jicong 已提交
201 202 203 204
  if (tEncodeI32(pEncoder, pTask->nodeId) < 0) return -1;
  if (tEncodeSEpSet(pEncoder, &pTask->epSet) < 0) return -1;

  if (pTask->execType != TASK_EXEC__NONE) {
L
Liu Jicong 已提交
205 206 207 208
    if (tEncodeI8(pEncoder, pTask->exec.parallelizable) < 0) return -1;
    if (tEncodeCStr(pEncoder, pTask->exec.qmsg) < 0) return -1;
  }

L
Liu Jicong 已提交
209
  if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
210
    if (tEncodeI8(pEncoder, pTask->tbSink.reserved) < 0) return -1;
L
Liu Jicong 已提交
211
  } else if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
212
    if (tEncodeI64(pEncoder, pTask->smaSink.smaId) < 0) return -1;
L
Liu Jicong 已提交
213 214 215 216 217 218
  } else if (pTask->sinkType == TASK_SINK__FETCH) {
    if (tEncodeI8(pEncoder, pTask->fetchSink.reserved) < 0) return -1;
  } else if (pTask->sinkType == TASK_SINK__SHOW) {
    if (tEncodeI8(pEncoder, pTask->showSink.reserved) < 0) return -1;
  } else {
    ASSERT(pTask->sinkType == TASK_SINK__NONE);
L
Liu Jicong 已提交
219 220 221
  }

  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
L
Liu Jicong 已提交
222
    if (tEncodeI32(pEncoder, pTask->inplaceDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
223
  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
L
Liu Jicong 已提交
224
    if (tEncodeI32(pEncoder, pTask->fixedEpDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
    if (tEncodeI32(pEncoder, pTask->fixedEpDispatcher.nodeId) < 0) return -1;
    if (tEncodeSEpSet(pEncoder, &pTask->fixedEpDispatcher.epSet) < 0) return -1;
  } else if (pTask->dispatchType == TASK_DISPATCH__SHUFFLE) {
    if (tEncodeI8(pEncoder, pTask->shuffleDispatcher.hashMethod) < 0) return -1;
  }

  /*tEndEncode(pEncoder);*/
  return pEncoder->pos;
}

int32_t tDecodeSStreamTask(SCoder* pDecoder, SStreamTask* pTask) {
  /*if (tStartDecode(pDecoder) < 0) return -1;*/
  if (tDecodeI64(pDecoder, &pTask->streamId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pTask->taskId) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->status) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->sourceType) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->execType) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->sinkType) < 0) return -1;
  if (tDecodeI8(pDecoder, &pTask->dispatchType) < 0) return -1;
  if (tDecodeI16(pDecoder, &pTask->dispatchMsgType) < 0) return -1;
  if (tDecodeI32(pDecoder, &pTask->downstreamTaskId) < 0) return -1;

L
Liu Jicong 已提交
247 248 249 250
  if (tDecodeI32(pDecoder, &pTask->nodeId) < 0) return -1;
  if (tDecodeSEpSet(pDecoder, &pTask->epSet) < 0) return -1;

  if (pTask->execType != TASK_EXEC__NONE) {
L
Liu Jicong 已提交
251 252 253 254
    if (tDecodeI8(pDecoder, &pTask->exec.parallelizable) < 0) return -1;
    if (tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg) < 0) return -1;
  }

L
Liu Jicong 已提交
255
  if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
256
    if (tDecodeI8(pDecoder, &pTask->tbSink.reserved) < 0) return -1;
L
Liu Jicong 已提交
257
  } else if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
258
    if (tDecodeI64(pDecoder, &pTask->smaSink.smaId) < 0) return -1;
L
Liu Jicong 已提交
259 260 261 262 263 264
  } else if (pTask->sinkType == TASK_SINK__FETCH) {
    if (tDecodeI8(pDecoder, &pTask->fetchSink.reserved) < 0) return -1;
  } else if (pTask->sinkType == TASK_SINK__SHOW) {
    if (tDecodeI8(pDecoder, &pTask->showSink.reserved) < 0) return -1;
  } else {
    ASSERT(pTask->sinkType == TASK_SINK__NONE);
L
Liu Jicong 已提交
265 266 267
  }

  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
L
Liu Jicong 已提交
268
    if (tDecodeI32(pDecoder, &pTask->inplaceDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
269
  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
L
Liu Jicong 已提交
270
    if (tDecodeI32(pDecoder, &pTask->fixedEpDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
271 272 273 274 275 276 277 278 279 280 281 282
    if (tDecodeI32(pDecoder, &pTask->fixedEpDispatcher.nodeId) < 0) return -1;
    if (tDecodeSEpSet(pDecoder, &pTask->fixedEpDispatcher.epSet) < 0) return -1;
  } else if (pTask->dispatchType == TASK_DISPATCH__SHUFFLE) {
    if (tDecodeI8(pDecoder, &pTask->shuffleDispatcher.hashMethod) < 0) return -1;
  }

  /*tEndDecode(pDecoder);*/
  return 0;
}

void tFreeSStreamTask(SStreamTask* pTask) {
  // TODO
wafwerar's avatar
wafwerar 已提交
283 284 285
  /*taosMemoryFree(pTask->qmsg);*/
  /*taosMemoryFree(pTask->executor);*/
  /*taosMemoryFree(pTask);*/
L
Liu Jicong 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
}

#if 0
int32_t tEncodeSStreamTaskExecReq(SCoder* pEncoder, const SStreamTaskExecReq* pReq) {
  if (tEncodeI64(pEncoder, pReq->streamId) < 0) return -1;
  if (tEncodeI32(pEncoder, pReq->taskId) < 0) return -1;
  /*if (tEncodeDataBlocks(buf, pReq->streamId) < 0) return -1;*/
  return pEncoder->size;
}
int32_t tDecodeSStreamTaskExecReq(SCoder* pDecoder, SStreamTaskExecReq* pReq) {
  return pEncoder->size;
}
void    tFreeSStreamTaskExecReq(SStreamTaskExecReq* pReq) {
  taosArrayDestroyEx(pReq->data, tDeleteSSDataBlock);
}
#endif