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

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
int32_t streamInit() {
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&streamEnv.inited, 0, 2);
    if (old != 2) break;
  }

  if (old == 0) {
    streamEnv.timer = taosTmrInit(10000, 100, 10000, "STREAM");
    if (streamEnv.timer == NULL) {
      atomic_store_8(&streamEnv.inited, 0);
      return -1;
    }
    atomic_store_8(&streamEnv.inited, 1);
  }
  return 0;
}

void streamCleanUp() {
  int8_t old;
  while (1) {
    old = atomic_val_compare_exchange_8(&streamEnv.inited, 1, 2);
    if (old != 2) break;
  }

  if (old == 1) {
    taosTmrCleanUp(streamEnv.timer);
    atomic_store_8(&streamEnv.inited, 0);
  }
}

void streamTriggerByTimer(void* param, void* tmrId) {
  SStreamTask* pTask = (void*)param;

L
Liu Jicong 已提交
53 54 55 56
  if (atomic_load_8(&pTask->taskStatus) == TASK_STATUS__DROPPING) {
    return;
  }

57 58 59 60 61 62 63 64 65 66 67 68 69 70
  if (atomic_load_8(&pTask->triggerStatus) == TASK_TRIGGER_STATUS__ACTIVE) {
    SStreamTrigger* trigger = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM);
    if (trigger == NULL) return;
    trigger->type = STREAM_INPUT__TRIGGER;
    trigger->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
    if (trigger->pBlock == NULL) {
      taosFreeQitem(trigger);
      return;
    }
    trigger->pBlock->info.type = STREAM_GET_ALL;

    atomic_store_8(&pTask->triggerStatus, TASK_TRIGGER_STATUS__IN_ACTIVE);

    streamTaskInput(pTask, (SStreamQueueItem*)trigger);
L
Liu Jicong 已提交
71
    streamLaunchByWrite(pTask, pTask->nodeId);
72 73 74 75 76 77 78 79 80 81 82 83 84
  }

  taosTmrReset(streamTriggerByTimer, (int32_t)pTask->triggerParam, pTask, streamEnv.timer, &pTask->timer);
}

int32_t streamSetupTrigger(SStreamTask* pTask) {
  if (pTask->triggerParam != 0) {
    pTask->timer = taosTmrStart(streamTriggerByTimer, (int32_t)pTask->triggerParam, pTask, streamEnv.timer);
    pTask->triggerStatus = TASK_TRIGGER_STATUS__IN_ACTIVE;
  }
  return 0;
}

L
Liu Jicong 已提交
85
int32_t streamLaunchByWrite(SStreamTask* pTask, int32_t vgId) {
L
Liu Jicong 已提交
86 87
  int8_t execStatus = atomic_load_8(&pTask->execStatus);
  if (execStatus == TASK_EXEC_STATUS__IDLE || execStatus == TASK_EXEC_STATUS__CLOSING) {
L
Liu Jicong 已提交
88 89 90 91 92 93 94 95
    SStreamTaskRunReq* pRunReq = rpcMallocCont(sizeof(SStreamTaskRunReq));
    if (pRunReq == NULL) return -1;

    // TODO: do we need htonl?
    pRunReq->head.vgId = vgId;
    pRunReq->streamId = pTask->streamId;
    pRunReq->taskId = pTask->taskId;
    SRpcMsg msg = {
96
        .msgType = TDMT_STREAM_TASK_RUN,
L
Liu Jicong 已提交
97 98 99
        .pCont = pRunReq,
        .contLen = sizeof(SStreamTaskRunReq),
    };
L
Liu Jicong 已提交
100
    tmsgPutToQueue(pTask->pMsgCb, FETCH_QUEUE, &msg);
L
Liu Jicong 已提交
101 102 103 104 105
  }
  return 0;
}

int32_t streamTaskEnqueue(SStreamTask* pTask, SStreamDispatchReq* pReq, SRpcMsg* pRsp) {
L
Liu Jicong 已提交
106
  SStreamDataBlock* pData = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM);
L
Liu Jicong 已提交
107 108 109
  int8_t            status;

  // enqueue
L
Liu Jicong 已提交
110
  if (pData != NULL) {
111
    pData->type = STREAM_INPUT__DATA_BLOCK;
L
Liu Jicong 已提交
112
    pData->srcVgId = pReq->dataSrcVgId;
L
Liu Jicong 已提交
113 114
    // decode
    /*pData->blocks = pReq->data;*/
L
Liu Jicong 已提交
115
    /*pBlock->sourceVer = pReq->sourceVer;*/
L
Liu Jicong 已提交
116 117
    streamDispatchReqToData(pReq, pData);
    if (streamTaskInput(pTask, (SStreamQueueItem*)pData) == 0) {
L
Liu Jicong 已提交
118 119 120 121 122 123 124 125 126 127
      status = TASK_INPUT_STATUS__NORMAL;
    } else {
      status = TASK_INPUT_STATUS__FAILED;
    }
  } else {
    streamTaskInputFail(pTask);
    status = TASK_INPUT_STATUS__FAILED;
  }

  // rsp by input status
128
  void* buf = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
L
Liu Jicong 已提交
129
  ((SMsgHead*)buf)->vgId = htonl(pReq->upstreamNodeId);
130
  SStreamDispatchRsp* pCont = POINTER_SHIFT(buf, sizeof(SMsgHead));
L
Liu Jicong 已提交
131 132
  pCont->inputStatus = status;
  pCont->streamId = pReq->streamId;
L
Liu Jicong 已提交
133 134 135 136 137 138 139 140 141 142 143 144 145
  pCont->taskId = pReq->upstreamTaskId;
  pRsp->pCont = buf;
  pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp);
  tmsgSendRsp(pRsp);
  return status == TASK_INPUT_STATUS__NORMAL ? 0 : -1;
}

int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq, SRpcMsg* pRsp) {
  SStreamDataBlock* pData = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM);
  int8_t            status = TASK_INPUT_STATUS__NORMAL;

  // enqueue
  if (pData != NULL) {
146
    pData->type = STREAM_INPUT__DATA_RETRIEVE;
L
Liu Jicong 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    pData->srcVgId = 0;
    // decode
    /*pData->blocks = pReq->data;*/
    /*pBlock->sourceVer = pReq->sourceVer;*/
    streamRetrieveReqToData(pReq, pData);
    if (streamTaskInput(pTask, (SStreamQueueItem*)pData) == 0) {
      status = TASK_INPUT_STATUS__NORMAL;
    } else {
      status = TASK_INPUT_STATUS__FAILED;
    }
  } else {
    /*streamTaskInputFail(pTask);*/
    /*status = TASK_INPUT_STATUS__FAILED;*/
  }

  // rsp by input status
  void* buf = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamRetrieveRsp));
  ((SMsgHead*)buf)->vgId = htonl(pReq->srcNodeId);
  SStreamRetrieveRsp* pCont = POINTER_SHIFT(buf, sizeof(SMsgHead));
  pCont->streamId = pReq->streamId;
  pCont->rspToTaskId = pReq->srcTaskId;
  pCont->rspFromTaskId = pReq->dstTaskId;
169
  pRsp->pCont = buf;
170
  pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamRetrieveRsp);
L
Liu Jicong 已提交
171 172 173 174
  tmsgSendRsp(pRsp);
  return status == TASK_INPUT_STATUS__NORMAL ? 0 : -1;
}

L
Liu Jicong 已提交
175
int32_t streamProcessDispatchReq(SStreamTask* pTask, SStreamDispatchReq* pReq, SRpcMsg* pRsp) {
L
Liu Jicong 已提交
176
  qInfo("task %d receive dispatch req from node %d task %d", pTask->taskId, pReq->upstreamNodeId, pReq->upstreamTaskId);
L
Liu Jicong 已提交
177

L
Liu Jicong 已提交
178 179 180 181 182 183 184
  // 1. handle input
  streamTaskEnqueue(pTask, pReq, pRsp);

  // 2. try exec
  // 2.1. idle: exec
  // 2.2. executing: return
  // 2.3. closing: keep trying
185
  if (pTask->execType != TASK_EXEC__NONE) {
L
Liu Jicong 已提交
186
    streamExec(pTask, pTask->pMsgCb);
187 188 189 190 191 192 193 194 195 196
  } else {
    ASSERT(pTask->sinkType != TASK_SINK__NONE);
    while (1) {
      void* data = streamQueueNextItem(pTask->inputQueue);
      if (data == NULL) return 0;
      if (streamTaskOutput(pTask, data) < 0) {
        ASSERT(0);
      }
    }
  }
L
Liu Jicong 已提交
197 198 199 200

  // 3. handle output
  // 3.1 check and set status
  // 3.2 dispatch / sink
201
  if (pTask->dispatchType != TASK_DISPATCH__NONE) {
L
Liu Jicong 已提交
202
    streamDispatch(pTask, pTask->pMsgCb);
203
  }
L
Liu Jicong 已提交
204 205 206 207

  return 0;
}

L
Liu Jicong 已提交
208
int32_t streamProcessDispatchRsp(SStreamTask* pTask, SStreamDispatchRsp* pRsp) {
209
  ASSERT(pRsp->inputStatus == TASK_OUTPUT_STATUS__NORMAL || pRsp->inputStatus == TASK_OUTPUT_STATUS__BLOCKED);
L
Liu Jicong 已提交
210 211 212

  qInfo("task %d receive dispatch rsp", pTask->taskId);

213 214
  int8_t old = atomic_exchange_8(&pTask->outputStatus, pRsp->inputStatus);
  ASSERT(old == TASK_OUTPUT_STATUS__WAIT);
L
Liu Jicong 已提交
215 216
  if (pRsp->inputStatus == TASK_INPUT_STATUS__BLOCKED) {
    // TODO: init recover timer
L
Liu Jicong 已提交
217
    ASSERT(0);
218
    return 0;
L
Liu Jicong 已提交
219 220
  }
  // continue dispatch
L
Liu Jicong 已提交
221
  streamDispatch(pTask, pTask->pMsgCb);
L
Liu Jicong 已提交
222 223 224
  return 0;
}

L
Liu Jicong 已提交
225 226
int32_t streamProcessRunReq(SStreamTask* pTask) {
  streamExec(pTask, pTask->pMsgCb);
L
Liu Jicong 已提交
227

228
  if (pTask->dispatchType != TASK_DISPATCH__NONE) {
L
Liu Jicong 已提交
229
    streamDispatch(pTask, pTask->pMsgCb);
230
  }
L
Liu Jicong 已提交
231 232 233
  return 0;
}

L
Liu Jicong 已提交
234
int32_t streamProcessRecoverReq(SStreamTask* pTask, SStreamTaskRecoverReq* pReq, SRpcMsg* pMsg) {
L
Liu Jicong 已提交
235 236 237 238 239 240 241 242
  //
  return 0;
}

int32_t streamProcessRecoverRsp(SStreamTask* pTask, SStreamTaskRecoverRsp* pRsp) {
  //
  return 0;
}
L
Liu Jicong 已提交
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261

int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq, SRpcMsg* pRsp) {
  qInfo("task %d receive retrieve req from node %d task %d", pTask->taskId, pReq->srcNodeId, pReq->srcTaskId);

  streamTaskEnqueueRetrieve(pTask, pReq, pRsp);

  ASSERT(pTask->execType != TASK_EXEC__NONE);
  streamExec(pTask, pTask->pMsgCb);

  ASSERT(pTask->dispatchType != TASK_DISPATCH__NONE);
  streamDispatch(pTask, pTask->pMsgCb);

  return 0;
}

int32_t streamProcessRetrieveRsp(SStreamTask* pTask, SStreamRetrieveRsp* pRsp) {
  //
  return 0;
}