streamExec.c 4.8 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 18 19 20 21

static int32_t streamTaskExecImpl(SStreamTask* pTask, void* data, SArray* pRes) {
  void* exec = pTask->exec.executor;

  // set input
22 23 24 25 26
  SStreamQueueItem* pItem = (SStreamQueueItem*)data;
  if (pItem->type == STREAM_INPUT__TRIGGER) {
    SStreamTrigger* pTrigger = (SStreamTrigger*)data;
    qSetMultiStreamInput(exec, pTrigger->pBlock, 1, STREAM_DATA_TYPE_SSDATA_BLOCK, false);
  } else if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
27
    ASSERT(pTask->isDataScan);
L
Liu Jicong 已提交
28 29
    SStreamDataSubmit* pSubmit = (SStreamDataSubmit*)data;
    qSetStreamInput(exec, pSubmit->data, STREAM_DATA_TYPE_SUBMIT_BLOCK, false);
30
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK) {
L
Liu Jicong 已提交
31
    SStreamDataBlock* pBlock = (SStreamDataBlock*)data;
32
    SArray*           blocks = pBlock->blocks;
L
Liu Jicong 已提交
33
    qSetMultiStreamInput(exec, blocks->pData, blocks->size, STREAM_DATA_TYPE_SSDATA_BLOCK, false);
L
Liu Jicong 已提交
34 35 36
  } else if (pItem->type == STREAM_INPUT__DROP) {
    // TODO exec drop
    return 0;
L
Liu Jicong 已提交
37 38 39 40 41 42 43 44 45 46
  }

  // exec
  while (1) {
    SSDataBlock* output = NULL;
    uint64_t     ts = 0;
    if (qExecTask(exec, &output, &ts) < 0) {
      ASSERT(false);
    }
    if (output == NULL) break;
L
Liu Jicong 已提交
47 48 49 50 51 52 53 54

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

L
Liu Jicong 已提交
55 56
    // TODO: do we need free memory?
    SSDataBlock* outputCopy = createOneDataBlock(output, true);
L
Liu Jicong 已提交
57
    outputCopy->info.childId = pTask->selfChildId;
L
Liu Jicong 已提交
58 59 60 61 62 63 64 65 66 67 68 69
    taosArrayPush(pRes, outputCopy);
  }
  return 0;
}

static SArray* streamExecForQall(SStreamTask* pTask, SArray* pRes) {
  while (1) {
    void* data = streamQueueNextItem(pTask->inputQueue);
    if (data == NULL) break;

    streamTaskExecImpl(pTask, data, pRes);

L
Liu Jicong 已提交
70 71 72 73
    if (pTask->taskStatus == TASK_STATUS__DROPPING) {
      return NULL;
    }

L
Liu Jicong 已提交
74 75 76 77 78 79 80 81 82
    if (taosArrayGetSize(pRes) != 0) {
      SStreamDataBlock* qRes = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM);
      if (qRes == NULL) {
        streamQueueProcessFail(pTask->inputQueue);
        taosArrayDestroy(pRes);
        return NULL;
      }
      qRes->type = STREAM_INPUT__DATA_BLOCK;
      qRes->blocks = pRes;
L
Liu Jicong 已提交
83
      /*qRes->sourceVg = pTask->nodeId;*/
L
Liu Jicong 已提交
84 85 86 87 88 89 90
      if (streamTaskOutput(pTask, qRes) < 0) {
        streamQueueProcessFail(pTask->inputQueue);
        taosArrayDestroy(pRes);
        taosFreeQitem(qRes);
        return NULL;
      }

91 92
      int8_t type = ((SStreamQueueItem*)data)->type;
      if (type == STREAM_INPUT__TRIGGER) {
L
Liu Jicong 已提交
93
        blockDataDestroy(((SStreamTrigger*)data)->pBlock);
L
Liu Jicong 已提交
94
        taosFreeQitem(data);
95 96 97 98 99 100 101
      } else if (type == STREAM_INPUT__DATA_BLOCK) {
        taosArrayDestroyEx(((SStreamDataBlock*)data)->blocks, (FDelete)tDeleteSSDataBlock);
        taosFreeQitem(data);
      } else if (type == STREAM_INPUT__DATA_SUBMIT) {
        ASSERT(pTask->isDataScan);
        streamDataSubmitRefDec((SStreamDataSubmit*)data);
        taosFreeQitem(data);
L
Liu Jicong 已提交
102 103 104 105 106 107 108 109 110 111 112 113 114
      }
      streamQueueProcessSuccess(pTask->inputQueue);
      return taosArrayInit(0, sizeof(SSDataBlock));
    }
  }
  return pRes;
}

// TODO: handle version
int32_t streamExec(SStreamTask* pTask, SMsgCb* pMsgCb) {
  SArray* pRes = taosArrayInit(0, sizeof(SSDataBlock));
  if (pRes == NULL) return -1;
  while (1) {
L
Liu Jicong 已提交
115 116 117
    int8_t execStatus =
        atomic_val_compare_exchange_8(&pTask->execStatus, TASK_EXEC_STATUS__IDLE, TASK_EXEC_STATUS__EXECUTING);
    if (execStatus == TASK_EXEC_STATUS__IDLE) {
L
Liu Jicong 已提交
118 119 120 121 122
      // first run
      pRes = streamExecForQall(pTask, pRes);
      if (pRes == NULL) goto FAIL;

      // set status closing
L
Liu Jicong 已提交
123
      atomic_store_8(&pTask->execStatus, TASK_EXEC_STATUS__CLOSING);
L
Liu Jicong 已提交
124 125 126 127 128

      // second run, make sure inputQ and qall are cleared
      pRes = streamExecForQall(pTask, pRes);
      if (pRes == NULL) goto FAIL;

L
Liu Jicong 已提交
129
      taosArrayDestroy(pRes);
L
Liu Jicong 已提交
130
      atomic_store_8(&pTask->execStatus, TASK_EXEC_STATUS__IDLE);
L
Liu Jicong 已提交
131
      return 0;
L
Liu Jicong 已提交
132
    } else if (execStatus == TASK_EXEC_STATUS__CLOSING) {
L
Liu Jicong 已提交
133
      continue;
L
Liu Jicong 已提交
134
    } else if (execStatus == TASK_EXEC_STATUS__EXECUTING) {
L
Liu Jicong 已提交
135 136 137
      ASSERT(taosArrayGetSize(pRes) == 0);
      taosArrayDestroy(pRes);
      return 0;
L
Liu Jicong 已提交
138 139 140 141 142 143
    } else {
      ASSERT(0);
    }
  }
FAIL:
  if (pRes) taosArrayDestroy(pRes);
L
Liu Jicong 已提交
144
  atomic_store_8(&pTask->execStatus, TASK_EXEC_STATUS__IDLE);
L
Liu Jicong 已提交
145 146 147
  return -1;
}