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

L
Liu Jicong 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
int32_t streamDataBlockEncode(void** buf, const SStreamDataBlock* pOutput) {
  int32_t tlen = 0;
  tlen += taosEncodeFixedI8(buf, pOutput->type);
  tlen += taosEncodeFixedI32(buf, pOutput->sourceVg);
  tlen += taosEncodeFixedI64(buf, pOutput->sourceVer);
  ASSERT(pOutput->type == STREAM_INPUT__DATA_BLOCK);
  tlen += tEncodeDataBlocks(buf, pOutput->blocks);
  return tlen;
}

void* streamDataBlockDecode(const void* buf, SStreamDataBlock* pInput) {
  buf = taosDecodeFixedI8(buf, &pInput->type);
  buf = taosDecodeFixedI32(buf, &pInput->sourceVg);
  buf = taosDecodeFixedI64(buf, &pInput->sourceVer);
  ASSERT(pInput->type == STREAM_INPUT__DATA_BLOCK);
  buf = tDecodeDataBlocks(buf, &pInput->blocks);
  return (void*)buf;
}

L
Liu Jicong 已提交
38 39 40 41 42 43 44 45 46 47 48 49
static int32_t streamBuildDispatchMsg(SStreamTask* pTask, SArray* data, SRpcMsg* pMsg, SEpSet** ppEpSet) {
  SStreamTaskExecReq req = {
      .streamId = pTask->streamId,
      .data = data,
  };

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

  if (buf == NULL) {
    return -1;
  }
L
Liu Jicong 已提交
50

L
Liu Jicong 已提交
51 52 53
  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
    ((SMsgHead*)buf)->vgId = 0;
    req.taskId = pTask->inplaceDispatcher.taskId;
L
Liu Jicong 已提交
54

L
Liu Jicong 已提交
55 56 57 58
  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
    ((SMsgHead*)buf)->vgId = htonl(pTask->fixedEpDispatcher.nodeId);
    *ppEpSet = &pTask->fixedEpDispatcher.epSet;
    req.taskId = pTask->fixedEpDispatcher.taskId;
L
Liu Jicong 已提交
59

L
Liu Jicong 已提交
60
  } else if (pTask->dispatchType == TASK_DISPATCH__SHUFFLE) {
L
fix  
Liu Jicong 已提交
61
    // TODO use general name rule of schemaless
L
Liu Jicong 已提交
62
    char ctbName[TSDB_TABLE_FNAME_LEN + 22];
L
Liu Jicong 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
    // all groupId must be the same in an array
    SSDataBlock* pBlock = taosArrayGet(data, 0);
    sprintf(ctbName, "%s:%ld", pTask->shuffleDispatcher.stbFullName, pBlock->info.groupId);

    // TODO: get hash function by hashMethod

    // get groupId, compute hash value
    uint32_t hashValue = MurmurHash3_32(ctbName, strlen(ctbName));
    //
    // get node
    // TODO: optimize search process
    SArray* vgInfo = pTask->shuffleDispatcher.dbInfo.pVgroupInfos;
    int32_t sz = taosArrayGetSize(vgInfo);
L
Liu Jicong 已提交
76
    int32_t nodeId = 0;
L
Liu Jicong 已提交
77 78 79 80
    for (int32_t i = 0; i < sz; i++) {
      SVgroupInfo* pVgInfo = taosArrayGet(vgInfo, i);
      if (hashValue >= pVgInfo->hashBegin && hashValue <= pVgInfo->hashEnd) {
        nodeId = pVgInfo->vgId;
L
Liu Jicong 已提交
81
        req.taskId = pVgInfo->taskId;
L
Liu Jicong 已提交
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
        *ppEpSet = &pVgInfo->epSet;
        break;
      }
    }
    ASSERT(nodeId != 0);
    ((SMsgHead*)buf)->vgId = htonl(nodeId);
  }

  void* abuf = POINTER_SHIFT(buf, sizeof(SMsgHead));
  tEncodeSStreamTaskExecReq(&abuf, &req);

  pMsg->pCont = buf;
  pMsg->contLen = tlen;
  pMsg->code = 0;
  pMsg->msgType = pTask->dispatchMsgType;
S
Shengliang Guan 已提交
97
  pMsg->info.noResp = 1;
L
Liu Jicong 已提交
98 99 100 101 102 103 104 105 106

  return 0;
}

static int32_t streamShuffleDispatch(SStreamTask* pTask, SMsgCb* pMsgCb, SHashObj* data) {
  void* pIter = NULL;
  while (1) {
    pIter = taosHashIterate(data, pIter);
    if (pIter == NULL) return 0;
L
Liu Jicong 已提交
107
    SArray* pData = *(SArray**)pIter;
L
Liu Jicong 已提交
108 109 110 111 112 113 114 115 116 117 118
    SRpcMsg dispatchMsg = {0};
    SEpSet* pEpSet;
    if (streamBuildDispatchMsg(pTask, pData, &dispatchMsg, &pEpSet) < 0) {
      ASSERT(0);
      return -1;
    }
    tmsgSendReq(pMsgCb, pEpSet, &dispatchMsg);
  }
  return 0;
}

L
Liu Jicong 已提交
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
int32_t streamEnqueueDataSubmit(SStreamTask* pTask, SStreamDataSubmit* input) {
  ASSERT(pTask->inputType == TASK_INPUT_TYPE__SUMBIT_BLOCK);
  int8_t inputStatus = atomic_load_8(&pTask->inputStatus);
  if (inputStatus == TASK_INPUT_STATUS__NORMAL) {
    streamDataSubmitRefInc(input);
    taosWriteQitem(pTask->inputQ, input);
  }
  return inputStatus;
}

int32_t streamEnqueueDataBlk(SStreamTask* pTask, SStreamDataBlock* input) {
  ASSERT(pTask->inputType == TASK_INPUT_TYPE__DATA_BLOCK);
  taosWriteQitem(pTask->inputQ, input);
  int8_t inputStatus = atomic_load_8(&pTask->inputStatus);
  return inputStatus;
}

L
Liu Jicong 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
int32_t streamTaskExecImpl(SStreamTask* pTask, void* data, SArray* pRes) {
  void* exec = pTask->exec.runners[0].executor;

  // set input
  if (pTask->inputType == STREAM_INPUT__DATA_SUBMIT) {
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)data;
    ASSERT(pSubmit->type == STREAM_INPUT__DATA_SUBMIT);

    qSetStreamInput(exec, pSubmit->data, STREAM_DATA_TYPE_SUBMIT_BLOCK);
  } else if (pTask->inputType == STREAM_INPUT__DATA_BLOCK) {
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
    ASSERT(pBlock->type == STREAM_INPUT__DATA_BLOCK);

    SArray* blocks = pBlock->blocks;
    qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK);
  }

  // exec
  while (1) {
    SSDataBlock* output;
    uint64_t     ts = 0;
    if (qExecTask(exec, &output, &ts) < 0) {
      ASSERT(false);
    }
    if (output == NULL) break;
    taosArrayPush(pRes, &output);
  }

  // destroy
  if (pTask->inputType == STREAM_INPUT__DATA_SUBMIT) {
    streamDataSubmitRefDec((SStreamDataSubmit*)data);
  } else {
    taosArrayDestroyEx(((SStreamDataBlock*)data)->blocks, (FDelete)tDeleteSSDataBlock);
  }
  return 0;
}

// TODO: handle version
int32_t streamTaskExec2(SStreamTask* pTask, SMsgCb* pMsgCb) {
  SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
  if (pRes == NULL) return -1;
  while (1) {
    int8_t execStatus = atomic_val_compare_exchange_8(&pTask->status, TASK_STATUS__IDLE, TASK_STATUS__EXECUTING);
    void*  exec = pTask->exec.runners[0].executor;
    if (execStatus == TASK_STATUS__IDLE) {
      // first run, from qall, handle failure from last exec
      while (1) {
        void* data = NULL;
        taosGetQitem(pTask->inputQAll, &data);
        if (data == NULL) break;

        streamTaskExecImpl(pTask, data, pRes);

        taosFreeQitem(data);

        if (taosArrayGetSize(pRes) != 0) {
          SStreamDataBlock* resQ = taosAllocateQitem(sizeof(void**), DEF_QITEM);
          resQ->type = STREAM_INPUT__DATA_BLOCK;
          resQ->blocks = pRes;
          taosWriteQitem(pTask->outputQ, resQ);
          pRes = taosArrayInit(0, sizeof(SSDataBlock));
          if (pRes == NULL) goto FAIL;
        }
      }
      // second run, from inputQ
      taosReadAllQitems(pTask->inputQ, pTask->inputQAll);
      while (1) {
        void* data = NULL;
        taosGetQitem(pTask->inputQAll, &data);
        if (data == NULL) break;

        streamTaskExecImpl(pTask, data, pRes);

        taosFreeQitem(data);

        if (taosArrayGetSize(pRes) != 0) {
          SStreamDataBlock* resQ = taosAllocateQitem(sizeof(void**), DEF_QITEM);
          resQ->type = STREAM_INPUT__DATA_BLOCK;
          resQ->blocks = pRes;
          taosWriteQitem(pTask->outputQ, resQ);
          pRes = taosArrayInit(0, sizeof(SSDataBlock));
          if (pRes == NULL) goto FAIL;
        }
      }
      // set status closing
      atomic_store_8(&pTask->status, TASK_STATUS__CLOSING);
      // third run, make sure all inputQ is cleared
      taosReadAllQitems(pTask->inputQ, pTask->inputQAll);
      while (1) {
        void* data = NULL;
        taosGetQitem(pTask->inputQAll, &data);
        if (data == NULL) break;

        streamTaskExecImpl(pTask, data, pRes);

        taosFreeQitem(data);

        if (taosArrayGetSize(pRes) != 0) {
          SStreamDataBlock* resQ = taosAllocateQitem(sizeof(void**), DEF_QITEM);
          resQ->type = STREAM_INPUT__DATA_BLOCK;
          resQ->blocks = pRes;
          taosWriteQitem(pTask->outputQ, resQ);
          pRes = taosArrayInit(0, sizeof(SSDataBlock));
          if (pRes == NULL) goto FAIL;
        }
      }
      // set status closing
      atomic_store_8(&pTask->status, TASK_STATUS__CLOSING);
      // third run, make sure all inputQ is cleared
      taosReadAllQitems(pTask->inputQ, pTask->inputQAll);
      while (1) {
        void* data = NULL;
        taosGetQitem(pTask->inputQAll, &data);
        if (data == NULL) break;
      }

      atomic_store_8(&pTask->status, TASK_STATUS__IDLE);
      break;
    } else if (execStatus == TASK_STATUS__CLOSING) {
      continue;
    } else if (execStatus == TASK_STATUS__EXECUTING) {
      break;
    } else {
      ASSERT(0);
    }
  }
  return 0;
FAIL:
  atomic_store_8(&pTask->status, TASK_STATUS__IDLE);
  return -1;
}

int32_t streamTaskDispatchDown(SStreamTask* pTask, SMsgCb* pMsgCb) {
  //
  return 0;
}

int32_t streamTaskSink(SStreamTask* pTask) {
L
Liu Jicong 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
  //
  return 0;
}

int32_t streamTaskProcessInputReq(SStreamTask* pTask, SMsgCb* pMsgCb, SStreamDataBlock* pBlock, SRpcMsg* pRsp) {
  // 1. handle input
  // 1.1 enqueue
  taosWriteQitem(pTask->inputQ, pBlock);
  // 1.2 calc back pressure
  // 1.3 rsp by input status
  int8_t              inputStatus = atomic_load_8(&pTask->inputStatus);
  SStreamDispatchRsp* pCont = rpcMallocCont(sizeof(SStreamDispatchRsp));
  pCont->status = inputStatus;
  pRsp->pCont = pCont;
  pRsp->contLen = sizeof(SStreamDispatchRsp);
  tmsgSendRsp(pRsp);
  // 2. try exec
  // 2.1. idle: exec
  // 2.2. executing: return
  // 2.3. closing: keep trying
  while (1) {
    int8_t execStatus = atomic_val_compare_exchange_8(&pTask->status, TASK_STATUS__IDLE, TASK_STATUS__EXECUTING);
    if (execStatus == TASK_STATUS__IDLE) {
L
Liu Jicong 已提交
297
      void*         exec = pTask->exec.runners[0].executor;
L
Liu Jicong 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
      SArray*       pRes = taosArrayInit(0, sizeof(void*));
      const SArray* blocks = pBlock->blocks;
      qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK);
      while (1) {
        SSDataBlock* output;
        uint64_t     ts = 0;
        if (qExecTask(exec, &output, &ts) < 0) {
          ASSERT(false);
        }
        if (output == NULL) break;
        taosArrayPush(pRes, &output);
      }
      // TODO: wrap destroy block
      taosArrayDestroyP(pBlock->blocks, (FDelete)blockDataDestroy);

      if (taosArrayGetSize(pRes) != 0) {
        SArray** resQ = taosAllocateQitem(sizeof(void**), DEF_QITEM);
        *resQ = pRes;
        taosWriteQitem(pTask->outputQ, resQ);
      }

    } else if (execStatus == TASK_STATUS__CLOSING) {
      continue;
    } else if (execStatus == TASK_STATUS__EXECUTING)
      break;
    else {
      ASSERT(0);
    }
  }
  // 3. handle output
  // 3.1 check and set status
  // 3.2 dispatch / sink
  STaosQall* qall = taosAllocateQall();
  taosReadAllQitems(pTask->outputQ, qall);
  SArray** ppRes = NULL;
  while (1) {
    taosGetQitem(qall, (void**)&ppRes);
    if (ppRes == NULL) break;

    SArray* pRes = *ppRes;
    if (pTask->sinkType == TASK_SINK__TABLE) {
      pTask->tbSink.tbSinkFunc(pTask, pTask->tbSink.vnode, pBlock->sourceVer, pRes);
    } else if (pTask->sinkType == TASK_SINK__SMA) {
      pTask->smaSink.smaSink(pTask->ahandle, pTask->smaSink.smaId, pRes);
    } else {
    }

    // dispatch
    if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
      SRpcMsg dispatchMsg = {0};
      if (streamBuildDispatchMsg(pTask, pRes, &dispatchMsg, NULL) < 0) {
        ASSERT(0);
        return -1;
      }

      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;
      } else {
        ASSERT(0);
      }
      tmsgPutToQueue(pMsgCb, qType, &dispatchMsg);

    } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
      SRpcMsg dispatchMsg = {0};
      SEpSet* pEpSet = NULL;
      if (streamBuildDispatchMsg(pTask, pRes, &dispatchMsg, &pEpSet) < 0) {
        ASSERT(0);
        return -1;
      }

      tmsgSendReq(pMsgCb, pEpSet, &dispatchMsg);

    } else if (pTask->dispatchType == TASK_DISPATCH__SHUFFLE) {
      SHashObj* pShuffleRes = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
      if (pShuffleRes == NULL) {
        return -1;
      }

      int32_t sz = taosArrayGetSize(pRes);
      for (int32_t i = 0; i < sz; i++) {
        SSDataBlock* pDataBlock = taosArrayGet(pRes, i);
        SArray*      pArray = taosHashGet(pShuffleRes, &pDataBlock->info.groupId, sizeof(int64_t));
        if (pArray == NULL) {
          pArray = taosArrayInit(0, sizeof(SSDataBlock));
          if (pArray == NULL) {
            return -1;
          }
          taosHashPut(pShuffleRes, &pDataBlock->info.groupId, sizeof(int64_t), &pArray, sizeof(void*));
        }
        taosArrayPush(pArray, pDataBlock);
      }

      if (streamShuffleDispatch(pTask, pMsgCb, pShuffleRes) < 0) {
        return -1;
      }

    } else {
      ASSERT(pTask->dispatchType == TASK_DISPATCH__NONE);
    }
  }
  //
  return 0;
}

int32_t streamTaskProcessDispatchRsp(SStreamTask* pTask, char* msg, int32_t msgLen) {
  //
  return 0;
}

int32_t streamTaskProcessRecoverReq(SStreamTask* pTask, char* msg) {
  //
  return 0;
}

L
Liu Jicong 已提交
418
int32_t streamTaskRun(SStreamTask* pTask) {
L
Liu Jicong 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
  SArray* pRes = NULL;
  if (pTask->execType == TASK_EXEC__PIPE || pTask->execType == TASK_EXEC__MERGE) {
    // TODO remove multi runner
    void* exec = pTask->exec.runners[0].executor;

    int8_t status = atomic_val_compare_exchange_8(&pTask->status, TASK_STATUS__IDLE, TASK_STATUS__EXECUTING);
    if (status == TASK_STATUS__IDLE) {
      pRes = taosArrayInit(0, sizeof(void*));
      if (pRes == NULL) {
        return -1;
      }

      void* input = NULL;
      taosWriteQitem(pTask->inputQ, &input);
      if (input == NULL) return 0;

      // TODO: fix type
      if (pTask->sourceType == TASK_SOURCE__SCAN) {
        SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)input;
        qSetStreamInput(exec, pSubmit->data, STREAM_DATA_TYPE_SUBMIT_BLOCK);
        while (1) {
          SSDataBlock* output;
          uint64_t     ts = 0;
          if (qExecTask(exec, &output, &ts) < 0) {
            ASSERT(false);
          }
          if (output == NULL) break;
          taosArrayPush(pRes, &output);
        }
        streamDataSubmitRefDec(pSubmit);
      } else {
        SStreamDataBlock* pStreamBlock = (SStreamDataBlock*)input;
        const SArray*     blocks = pStreamBlock->blocks;
        qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK);
        while (1) {
          SSDataBlock* output;
          uint64_t     ts = 0;
          if (qExecTask(exec, &output, &ts) < 0) {
            ASSERT(false);
          }
          if (output == NULL) break;
          taosArrayPush(pRes, &output);
        }
        // TODO: wrap destroy block
        taosArrayDestroyP(pStreamBlock->blocks, (FDelete)blockDataDestroy);
      }

      if (taosArrayGetSize(pRes) != 0) {
        SArray** resQ = taosAllocateQitem(sizeof(void**), DEF_QITEM);
        *resQ = pRes;
        taosWriteQitem(pTask->outputQ, resQ);
      }
    }
  }
  return 0;
}

L
Liu Jicong 已提交
476 477 478 479 480 481
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 已提交
482
  if (pTask->execType != TASK_EXEC__NONE) {
L
Liu Jicong 已提交
483 484 485
    ASSERT(workId < pTask->exec.numOfRunners);
    void* exec = pTask->exec.runners[workId].executor;
    pRes = taosArrayInit(0, sizeof(SSDataBlock));
L
Liu Jicong 已提交
486 487 488
    if (pRes == NULL) {
      return -1;
    }
L
Liu Jicong 已提交
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
    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;
L
Liu Jicong 已提交
504 505 506 507 508 509 510 511 512 513
      /*int32_t       sz = taosArrayGetSize(blocks);*/
      /*for (int32_t i = 0; i < sz; i++) {*/
      /*SSDataBlock* pBlock = taosArrayGet(blocks, i);*/
      /*qSetStreamInput(exec, pBlock, inputType);*/
      qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK);
      while (1) {
        SSDataBlock* output;
        uint64_t     ts;
        if (qExecTask(exec, &output, &ts) < 0) {
          ASSERT(false);
L
Liu Jicong 已提交
514
        }
L
Liu Jicong 已提交
515 516 517 518
        if (output == NULL) {
          break;
        }
        taosArrayPush(pRes, output);
L
Liu Jicong 已提交
519
      }
L
Liu Jicong 已提交
520
      /*}*/
L
Liu Jicong 已提交
521 522 523 524 525 526 527 528
    } else {
      ASSERT(0);
    }
  } else {
    ASSERT(inputType == STREAM_DATA_TYPE_SSDATA_BLOCK);
    pRes = (SArray*)input;
  }

L
Liu Jicong 已提交
529 530
  if (pRes == NULL || taosArrayGetSize(pRes) == 0) return 0;

L
Liu Jicong 已提交
531 532
  // sink
  if (pTask->sinkType == TASK_SINK__TABLE) {
5
54liuyao 已提交
533
    // blockDebugShowData(pRes);
L
Liu Jicong 已提交
534
    pTask->tbSink.tbSinkFunc(pTask, pTask->tbSink.vnode, 0, pRes);
L
Liu Jicong 已提交
535
  } else if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
536
    pTask->smaSink.smaSink(pTask->ahandle, pTask->smaSink.smaId, pRes);
L
Liu Jicong 已提交
537 538 539 540 541 542 543 544 545
    //
  } else if (pTask->sinkType == TASK_SINK__FETCH) {
    //
  } else {
    ASSERT(pTask->sinkType == TASK_SINK__NONE);
  }

  // dispatch

L
Liu Jicong 已提交
546 547 548 549
  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
    SRpcMsg dispatchMsg = {0};
    if (streamBuildDispatchMsg(pTask, pRes, &dispatchMsg, NULL) < 0) {
      ASSERT(0);
L
Liu Jicong 已提交
550 551
      return -1;
    }
L
Liu Jicong 已提交
552 553 554 555 556 557 558 559 560

    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 已提交
561 562 563
    } else {
      ASSERT(0);
    }
L
Liu Jicong 已提交
564 565 566
    tmsgPutToQueue(pMsgCb, qType, &dispatchMsg);

  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
L
Liu Jicong 已提交
567 568 569 570
    SRpcMsg dispatchMsg = {0};
    SEpSet* pEpSet = NULL;
    if (streamBuildDispatchMsg(pTask, pRes, &dispatchMsg, &pEpSet) < 0) {
      ASSERT(0);
L
Liu Jicong 已提交
571 572 573 574 575 576
      return -1;
    }

    tmsgSendReq(pMsgCb, pEpSet, &dispatchMsg);

  } else if (pTask->dispatchType == TASK_DISPATCH__SHUFFLE) {
L
Liu Jicong 已提交
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598
    SHashObj* pShuffleRes = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
    if (pShuffleRes == NULL) {
      return -1;
    }

    int32_t sz = taosArrayGetSize(pRes);
    for (int32_t i = 0; i < sz; i++) {
      SSDataBlock* pDataBlock = taosArrayGet(pRes, i);
      SArray*      pArray = taosHashGet(pShuffleRes, &pDataBlock->info.groupId, sizeof(int64_t));
      if (pArray == NULL) {
        pArray = taosArrayInit(0, sizeof(SSDataBlock));
        if (pArray == NULL) {
          return -1;
        }
        taosHashPut(pShuffleRes, &pDataBlock->info.groupId, sizeof(int64_t), &pArray, sizeof(void*));
      }
      taosArrayPush(pArray, pDataBlock);
    }

    if (streamShuffleDispatch(pTask, pMsgCb, pShuffleRes) < 0) {
      return -1;
    }
L
Liu Jicong 已提交
599 600 601

  } else {
    ASSERT(pTask->dispatchType == TASK_DISPATCH__NONE);
L
Liu Jicong 已提交
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621
  }
  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 已提交
622 623

SStreamTask* tNewSStreamTask(int64_t streamId) {
wafwerar's avatar
wafwerar 已提交
624
  SStreamTask* pTask = (SStreamTask*)taosMemoryCalloc(1, sizeof(SStreamTask));
L
Liu Jicong 已提交
625 626 627 628 629
  if (pTask == NULL) {
    return NULL;
  }
  pTask->taskId = tGenIdPI32();
  pTask->streamId = streamId;
L
Liu Jicong 已提交
630 631 632 633
  pTask->status = TASK_STATUS__IDLE;

  pTask->inputQ = taosOpenQueue();
  pTask->outputQ = taosOpenQueue();
L
Liu Jicong 已提交
634 635 636 637
  pTask->inputQAll = taosAllocateQall();
  pTask->outputQAll = taosAllocateQall();
  if (pTask->inputQ == NULL || pTask->outputQ == NULL || pTask->inputQAll == NULL || pTask->outputQAll == NULL)
    goto FAIL;
L
Liu Jicong 已提交
638
  return pTask;
L
Liu Jicong 已提交
639 640 641
FAIL:
  if (pTask->inputQ) taosCloseQueue(pTask->inputQ);
  if (pTask->outputQ) taosCloseQueue(pTask->outputQ);
L
Liu Jicong 已提交
642 643
  if (pTask->inputQAll) taosFreeQall(pTask->inputQAll);
  if (pTask->outputQAll) taosFreeQall(pTask->outputQAll);
L
Liu Jicong 已提交
644 645
  if (pTask) taosMemoryFree(pTask);
  return NULL;
L
Liu Jicong 已提交
646 647
}

H
Hongze Cheng 已提交
648
int32_t tEncodeSStreamTask(SEncoder* pEncoder, const SStreamTask* pTask) {
L
Liu Jicong 已提交
649 650 651
  /*if (tStartEncode(pEncoder) < 0) return -1;*/
  if (tEncodeI64(pEncoder, pTask->streamId) < 0) return -1;
  if (tEncodeI32(pEncoder, pTask->taskId) < 0) return -1;
L
Liu Jicong 已提交
652
  if (tEncodeI8(pEncoder, pTask->inputType) < 0) return -1;
L
Liu Jicong 已提交
653 654 655 656 657 658 659
  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;

L
Liu Jicong 已提交
660 661 662 663
  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 已提交
664 665 666 667
    if (tEncodeI8(pEncoder, pTask->exec.parallelizable) < 0) return -1;
    if (tEncodeCStr(pEncoder, pTask->exec.qmsg) < 0) return -1;
  }

L
Liu Jicong 已提交
668
  if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
669
    if (tEncodeI64(pEncoder, pTask->tbSink.stbUid) < 0) return -1;
L
Liu Jicong 已提交
670
    if (tEncodeSSchemaWrapper(pEncoder, pTask->tbSink.pSchemaWrapper) < 0) return -1;
L
Liu Jicong 已提交
671
  } else if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
672
    if (tEncodeI64(pEncoder, pTask->smaSink.smaId) < 0) return -1;
L
Liu Jicong 已提交
673 674 675 676
  } else if (pTask->sinkType == TASK_SINK__FETCH) {
    if (tEncodeI8(pEncoder, pTask->fetchSink.reserved) < 0) return -1;
  } else {
    ASSERT(pTask->sinkType == TASK_SINK__NONE);
L
Liu Jicong 已提交
677 678 679
  }

  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
L
Liu Jicong 已提交
680
    if (tEncodeI32(pEncoder, pTask->inplaceDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
681
  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
L
Liu Jicong 已提交
682
    if (tEncodeI32(pEncoder, pTask->fixedEpDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
683 684 685
    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) {
L
Liu Jicong 已提交
686 687
    if (tSerializeSUseDbRspImp(pEncoder, &pTask->shuffleDispatcher.dbInfo) < 0) return -1;
    /*if (tEncodeI8(pEncoder, pTask->shuffleDispatcher.hashMethod) < 0) return -1;*/
L
Liu Jicong 已提交
688 689 690 691 692 693
  }

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

H
Hongze Cheng 已提交
694
int32_t tDecodeSStreamTask(SDecoder* pDecoder, SStreamTask* pTask) {
L
Liu Jicong 已提交
695 696 697
  /*if (tStartDecode(pDecoder) < 0) return -1;*/
  if (tDecodeI64(pDecoder, &pTask->streamId) < 0) return -1;
  if (tDecodeI32(pDecoder, &pTask->taskId) < 0) return -1;
L
Liu Jicong 已提交
698
  if (tDecodeI8(pDecoder, &pTask->inputType) < 0) return -1;
L
Liu Jicong 已提交
699 700 701 702 703 704 705
  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;

L
Liu Jicong 已提交
706 707 708 709
  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 已提交
710 711 712 713
    if (tDecodeI8(pDecoder, &pTask->exec.parallelizable) < 0) return -1;
    if (tDecodeCStrAlloc(pDecoder, &pTask->exec.qmsg) < 0) return -1;
  }

L
Liu Jicong 已提交
714
  if (pTask->sinkType == TASK_SINK__TABLE) {
L
Liu Jicong 已提交
715
    if (tDecodeI64(pDecoder, &pTask->tbSink.stbUid) < 0) return -1;
L
Liu Jicong 已提交
716 717 718
    pTask->tbSink.pSchemaWrapper = taosMemoryCalloc(1, sizeof(SSchemaWrapper));
    if (pTask->tbSink.pSchemaWrapper == NULL) return -1;
    if (tDecodeSSchemaWrapper(pDecoder, pTask->tbSink.pSchemaWrapper) < 0) return -1;
L
Liu Jicong 已提交
719
  } else if (pTask->sinkType == TASK_SINK__SMA) {
L
Liu Jicong 已提交
720
    if (tDecodeI64(pDecoder, &pTask->smaSink.smaId) < 0) return -1;
L
Liu Jicong 已提交
721 722 723 724
  } else if (pTask->sinkType == TASK_SINK__FETCH) {
    if (tDecodeI8(pDecoder, &pTask->fetchSink.reserved) < 0) return -1;
  } else {
    ASSERT(pTask->sinkType == TASK_SINK__NONE);
L
Liu Jicong 已提交
725 726 727
  }

  if (pTask->dispatchType == TASK_DISPATCH__INPLACE) {
L
Liu Jicong 已提交
728
    if (tDecodeI32(pDecoder, &pTask->inplaceDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
729
  } else if (pTask->dispatchType == TASK_DISPATCH__FIXED) {
L
Liu Jicong 已提交
730
    if (tDecodeI32(pDecoder, &pTask->fixedEpDispatcher.taskId) < 0) return -1;
L
Liu Jicong 已提交
731 732 733
    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) {
L
Liu Jicong 已提交
734 735
    /*if (tDecodeI8(pDecoder, &pTask->shuffleDispatcher.hashMethod) < 0) return -1;*/
    if (tDeserializeSUseDbRspImp(pDecoder, &pTask->shuffleDispatcher.dbInfo) < 0) return -1;
L
Liu Jicong 已提交
736 737 738 739 740 741 742
  }

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

void tFreeSStreamTask(SStreamTask* pTask) {
L
Liu Jicong 已提交
743 744
  taosCloseQueue(pTask->inputQ);
  taosCloseQueue(pTask->outputQ);
L
Liu Jicong 已提交
745
  // TODO
L
Liu Jicong 已提交
746 747 748 749 750
  if (pTask->exec.qmsg) taosMemoryFree(pTask->exec.qmsg);
  for (int32_t i = 0; i < pTask->exec.numOfRunners; i++) {
    qDestroyTask(pTask->exec.runners[i].executor);
  }
  taosMemoryFree(pTask->exec.runners);
wafwerar's avatar
wafwerar 已提交
751
  /*taosMemoryFree(pTask->executor);*/
L
Liu Jicong 已提交
752
  taosMemoryFree(pTask);
L
Liu Jicong 已提交
753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
}

#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