stream.c 13.5 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

dengyihao's avatar
dengyihao 已提交
19
#define STREAM_TASK_INPUT_QUEUEU_CAPACITY         20480
H
Haojun Liao 已提交
20 21
#define STREAM_TASK_INPUT_QUEUEU_CAPACITY_IN_SIZE (50)
#define ONE_MB_F                                  (1048576.0)
dengyihao's avatar
dengyihao 已提交
22
#define QUEUE_MEM_SIZE_IN_MB(_q)                  (taosQueueMemorySize(_q) / ONE_MB_F)
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 53 54
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);
  }
}

L
Liu Jicong 已提交
55
void streamSchedByTimer(void* param, void* tmrId) {
56 57
  SStreamTask* pTask = (void*)param;

L
liuyao 已提交
58
  if (streamTaskShouldStop(&pTask->status) || streamTaskShouldPause(&pTask->status)) {
L
Liu Jicong 已提交
59
    streamMetaReleaseTask(NULL, pTask);
L
Liu Jicong 已提交
60 61 62
    return;
  }

63
  if (atomic_load_8(&pTask->triggerStatus) == TASK_TRIGGER_STATUS__ACTIVE) {
S
Shengliang Guan 已提交
64
    SStreamTrigger* trigger = taosAllocateQitem(sizeof(SStreamTrigger), DEF_QITEM, 0);
65
    if (trigger == NULL) return;
L
Liu Jicong 已提交
66
    trigger->type = STREAM_INPUT__GET_RES;
67 68 69 70 71 72
    trigger->pBlock = taosMemoryCalloc(1, sizeof(SSDataBlock));
    if (trigger->pBlock == NULL) {
      taosFreeQitem(trigger);
      return;
    }

73
    trigger->pBlock->info.type = STREAM_GET_ALL;
74
    atomic_store_8(&pTask->triggerStatus, TASK_TRIGGER_STATUS__INACTIVE);
75

76
    if (tAppendDataToInputQueue(pTask, (SStreamQueueItem*)trigger) < 0) {
L
Liu Jicong 已提交
77 78 79
      taosTmrReset(streamSchedByTimer, (int32_t)pTask->triggerParam, pTask, streamEnv.timer, &pTask->timer);
      return;
    }
80

L
Liu Jicong 已提交
81
    streamSchedExec(pTask);
82 83
  }

L
Liu Jicong 已提交
84
  taosTmrReset(streamSchedByTimer, (int32_t)pTask->triggerParam, pTask, streamEnv.timer, &pTask->timer);
85 86 87 88
}

int32_t streamSetupTrigger(SStreamTask* pTask) {
  if (pTask->triggerParam != 0) {
L
Liu Jicong 已提交
89 90
    int32_t ref = atomic_add_fetch_32(&pTask->refCnt, 1);
    ASSERT(ref == 2);
L
Liu Jicong 已提交
91
    pTask->timer = taosTmrStart(streamSchedByTimer, (int32_t)pTask->triggerParam, pTask, streamEnv.timer);
92
    pTask->triggerStatus = TASK_TRIGGER_STATUS__INACTIVE;
93 94 95 96
  }
  return 0;
}

L
Liu Jicong 已提交
97
int32_t streamSchedExec(SStreamTask* pTask) {
dengyihao's avatar
dengyihao 已提交
98 99
  int8_t schedStatus = atomic_val_compare_exchange_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE,
                                                     TASK_SCHED_STATUS__WAITING);
100

L
Liu Jicong 已提交
101 102 103
  if (schedStatus == TASK_SCHED_STATUS__INACTIVE) {
    SStreamTaskRunReq* pRunReq = rpcMallocCont(sizeof(SStreamTaskRunReq));
    if (pRunReq == NULL) {
104
      terrno = TSDB_CODE_OUT_OF_MEMORY;
105
      atomic_store_8(&pTask->status.schedStatus, TASK_SCHED_STATUS__INACTIVE);
L
Liu Jicong 已提交
106 107
      return -1;
    }
108

L
Liu Jicong 已提交
109
    pRunReq->head.vgId = pTask->nodeId;
110 111
    pRunReq->streamId = pTask->id.streamId;
    pRunReq->taskId = pTask->id.taskId;
112

dengyihao's avatar
dengyihao 已提交
113
    SRpcMsg msg = {.msgType = TDMT_STREAM_TASK_RUN, .pCont = pRunReq, .contLen = sizeof(SStreamTaskRunReq)};
L
Liu Jicong 已提交
114
    tmsgPutToQueue(pTask->pMsgCb, STREAM_QUEUE, &msg);
115
    qDebug("trigger to run s-task:%s", pTask->id.idStr);
L
Liu Jicong 已提交
116
  }
117

L
Liu Jicong 已提交
118 119 120
  return 0;
}

121
int32_t streamTaskEnqueueBlocks(SStreamTask* pTask, const SStreamDispatchReq* pReq, SRpcMsg* pRsp) {
122
  int8_t status = 0;
123 124 125

  SStreamDataBlock* pBlock = createStreamDataFromDispatchMsg(pReq, STREAM_INPUT__DATA_BLOCK, pReq->dataSrcVgId);
  if (pBlock == NULL) {
126 127
    streamTaskInputFail(pTask);
    status = TASK_INPUT_STATUS__FAILED;
128
    qError("vgId:%d, s-task:%s failed to receive dispatch msg, reason: out of memory", pTask->pMeta->vgId,
129
           pTask->id.idStr);
130
  } else {
131 132 133
    int32_t code = tAppendDataToInputQueue(pTask, (SStreamQueueItem*)pBlock);
    // input queue is full, upstream is blocked now
    status = (code == TSDB_CODE_SUCCESS)? TASK_INPUT_STATUS__NORMAL:TASK_INPUT_STATUS__BLOCKED;
L
Liu Jicong 已提交
134 135 136
  }

  // rsp by input status
137
  void* buf = rpcMallocCont(sizeof(SMsgHead) + sizeof(SStreamDispatchRsp));
L
Liu Jicong 已提交
138
  ((SMsgHead*)buf)->vgId = htonl(pReq->upstreamNodeId);
139
  SStreamDispatchRsp* pDispatchRsp = POINTER_SHIFT(buf, sizeof(SMsgHead));
140

141 142 143 144 145 146 147 148
  pDispatchRsp->inputStatus = status;
  pDispatchRsp->streamId = htobe64(pReq->streamId);
  pDispatchRsp->upstreamNodeId = htonl(pReq->upstreamNodeId);
  pDispatchRsp->upstreamTaskId = htonl(pReq->upstreamTaskId);
  pDispatchRsp->downstreamNodeId = htonl(pTask->nodeId);
  pDispatchRsp->downstreamTaskId = htonl(pTask->id.taskId);

  pRsp->pCont = buf;
L
Liu Jicong 已提交
149 150
  pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamDispatchRsp);
  tmsgSendRsp(pRsp);
151

L
Liu Jicong 已提交
152 153 154 155
  return status == TASK_INPUT_STATUS__NORMAL ? 0 : -1;
}

int32_t streamTaskEnqueueRetrieve(SStreamTask* pTask, SStreamRetrieveReq* pReq, SRpcMsg* pRsp) {
S
Shengliang Guan 已提交
156
  SStreamDataBlock* pData = taosAllocateQitem(sizeof(SStreamDataBlock), DEF_QITEM, 0);
L
Liu Jicong 已提交
157 158 159 160
  int8_t            status = TASK_INPUT_STATUS__NORMAL;

  // enqueue
  if (pData != NULL) {
H
Haojun Liao 已提交
161
    qDebug("s-task:%s (child %d) recv retrieve req from task:0x%x, reqId %" PRId64, pTask->id.idStr, pTask->selfChildId,
L
Liu Jicong 已提交
162 163
           pReq->srcTaskId, pReq->reqId);

164
    pData->type = STREAM_INPUT__DATA_RETRIEVE;
L
Liu Jicong 已提交
165 166 167 168 169
    pData->srcVgId = 0;
    // decode
    /*pData->blocks = pReq->data;*/
    /*pBlock->sourceVer = pReq->sourceVer;*/
    streamRetrieveReqToData(pReq, pData);
170
    if (tAppendDataToInputQueue(pTask, (SStreamQueueItem*)pData) == 0) {
L
Liu Jicong 已提交
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186
      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;
187
  pRsp->pCont = buf;
188
  pRsp->contLen = sizeof(SMsgHead) + sizeof(SStreamRetrieveRsp);
L
Liu Jicong 已提交
189 190 191 192
  tmsgSendRsp(pRsp);
  return status == TASK_INPUT_STATUS__NORMAL ? 0 : -1;
}

193 194
// todo add log
int32_t streamTaskOutputResultBlock(SStreamTask* pTask, SStreamDataBlock* pBlock) {
dengyihao's avatar
dengyihao 已提交
195
  int32_t code = 0;
L
Liu Jicong 已提交
196 197
  if (pTask->outputType == TASK_OUTPUT__TABLE) {
    pTask->tbSink.tbSinkFunc(pTask, pTask->tbSink.vnode, 0, pBlock->blocks);
198
    destroyStreamDataBlock(pBlock);
L
Liu Jicong 已提交
199 200
  } else if (pTask->outputType == TASK_OUTPUT__SMA) {
    pTask->smaSink.smaSink(pTask->smaSink.vnode, pTask->smaSink.smaId, pBlock->blocks);
201
    destroyStreamDataBlock(pBlock);
L
Liu Jicong 已提交
202 203
  } else {
    ASSERT(pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH || pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH);
dengyihao's avatar
dengyihao 已提交
204
    code = taosWriteQitem(pTask->outputQueue->queue, pBlock);
H
Haojun Liao 已提交
205
    if (code != 0) {  // todo failed to add it into the output queue, free it.
dengyihao's avatar
dengyihao 已提交
206 207
      return code;
    }
208

209
    streamDispatchStreamBlock(pTask);
L
Liu Jicong 已提交
210
  }
211

L
Liu Jicong 已提交
212 213 214
  return 0;
}

215 216 217
int32_t streamProcessDispatchMsg(SStreamTask* pTask, SStreamDispatchReq* pReq, SRpcMsg* pRsp, bool exec) {
  qDebug("s-task:%s receive dispatch msg from taskId:0x%x(vgId:%d), msgLen:%" PRId64, pTask->id.idStr,
         pReq->upstreamTaskId, pReq->upstreamNodeId, pReq->totalLen);
L
Liu Jicong 已提交
218

219
  // todo add the input queue buffer limitation
220
  streamTaskEnqueueBlocks(pTask, pReq, pRsp);
L
Liu Jicong 已提交
221
  tDeleteStreamDispatchReq(pReq);
L
Liu Jicong 已提交
222

L
Liu Jicong 已提交
223
  if (exec) {
L
Liu Jicong 已提交
224 225 226
    if (streamTryExec(pTask) < 0) {
      return -1;
    }
L
Liu Jicong 已提交
227
  } else {
L
Liu Jicong 已提交
228
    streamSchedExec(pTask);
229
  }
L
Liu Jicong 已提交
230 231 232 233

  return 0;
}

234
int32_t streamProcessDispatchRsp(SStreamTask* pTask, SStreamDispatchRsp* pRsp, int32_t code) {
235
  qDebug("s-task:%s receive dispatch rsp, output status:%d code:%d", pTask->id.idStr, pRsp->inputStatus, code);
L
Liu Jicong 已提交
236

237
  if (pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {
L
Liu Jicong 已提交
238
    int32_t leftRsp = atomic_sub_fetch_32(&pTask->shuffleDispatcher.waitingRspCnt, 1);
239
    qDebug("s-task:%s is shuffle, left waiting rsp %d", pTask->id.idStr, leftRsp);
240 241 242
    if (leftRsp > 0) {
      return 0;
    }
L
Liu Jicong 已提交
243 244
  }

245 246
  int8_t old = atomic_exchange_8(&pTask->outputStatus, pRsp->inputStatus);
  ASSERT(old == TASK_OUTPUT_STATUS__WAIT);
247 248 249

  // the input queue of the (down stream) task that receive the output data is full, so the TASK_INPUT_STATUS_BLOCKED is rsp
  // todo we need to send EMPTY PACKAGE to detect if the input queue is available for output of upstream task, every 50 ms.
L
Liu Jicong 已提交
250 251
  if (pRsp->inputStatus == TASK_INPUT_STATUS__BLOCKED) {
    // TODO: init recover timer
252
    qError("s-task:%s inputQ of downstream task:0x%x is full, need to block output", pTask->id.idStr, pRsp->downstreamTaskId);
253 254 255 256

    atomic_store_8(&pTask->outputStatus, TASK_OUTPUT_STATUS__NORMAL);
    qError("s-task:%s ignore error, and reset task output status:%d", pTask->id.idStr, pTask->outputStatus);

257
    return 0;
L
Liu Jicong 已提交
258
  }
H
Haojun Liao 已提交
259

260
  // otherwise, continue dispatch the first block to down stream task in pipeline
261
  streamDispatchStreamBlock(pTask);
L
Liu Jicong 已提交
262 263 264
  return 0;
}

L
Liu Jicong 已提交
265
int32_t streamProcessRunReq(SStreamTask* pTask) {
L
Liu Jicong 已提交
266 267 268
  if (streamTryExec(pTask) < 0) {
    return -1;
  }
L
Liu Jicong 已提交
269

L
Liu Jicong 已提交
270
  /*if (pTask->outputType == TASK_OUTPUT__FIXED_DISPATCH || pTask->outputType == TASK_OUTPUT__SHUFFLE_DISPATCH) {*/
271
  /*streamDispatchStreamBlock(pTask);*/
L
Liu Jicong 已提交
272
  /*}*/
L
Liu Jicong 已提交
273 274 275
  return 0;
}

L
Liu Jicong 已提交
276
int32_t streamProcessRetrieveReq(SStreamTask* pTask, SStreamRetrieveReq* pReq, SRpcMsg* pRsp) {
H
Haojun Liao 已提交
277
  qDebug("s-task:%s receive retrieve req from node %d taskId:0x%x", pTask->id.idStr, pReq->srcNodeId, pReq->srcTaskId);
L
Liu Jicong 已提交
278 279
  streamTaskEnqueueRetrieve(pTask, pReq, pRsp);

280
  ASSERT(pTask->taskLevel != TASK_LEVEL__SINK);
L
Liu Jicong 已提交
281
  streamSchedExec(pTask);
L
Liu Jicong 已提交
282 283 284
  return 0;
}

285
bool tInputQueueIsFull(const SStreamTask* pTask) {
dengyihao's avatar
dengyihao 已提交
286
  bool   isFull = taosQueueItemSize((pTask->inputQueue->queue)) >= STREAM_TASK_INPUT_QUEUEU_CAPACITY;
287 288
  double size = QUEUE_MEM_SIZE_IN_MB(pTask->inputQueue->queue);
  return (isFull || size >= STREAM_TASK_INPUT_QUEUEU_CAPACITY_IN_SIZE);
289 290
}

291
int32_t tAppendDataToInputQueue(SStreamTask* pTask, SStreamQueueItem* pItem) {
292 293 294
  int8_t  type = pItem->type;
  int32_t total = taosQueueItemSize(pTask->inputQueue->queue) + 1;
  double  size = QUEUE_MEM_SIZE_IN_MB(pTask->inputQueue->queue);
295 296

  if (type == STREAM_INPUT__DATA_SUBMIT) {
297
    SStreamDataSubmit* px = (SStreamDataSubmit*)pItem;
298
    if ((pTask->taskLevel == TASK_LEVEL__SOURCE) && tInputQueueIsFull(pTask)) {
299
      qError("s-task:%s input queue is full, capacity(size:%d num:%dMiB), current(blocks:%d, size:%.2fMiB) stop to push data",
300
             pTask->id.idStr, STREAM_TASK_INPUT_QUEUEU_CAPACITY, STREAM_TASK_INPUT_QUEUEU_CAPACITY_IN_SIZE, total,
dengyihao's avatar
dengyihao 已提交
301 302 303
             size);
      streamDataSubmitDestroy(px);
      taosFreeQitem(pItem);
304 305
      return -1;
    }
306

307 308 309 310 311 312
    int32_t code = taosWriteQitem(pTask->inputQueue->queue, pItem);
    if (code != TSDB_CODE_SUCCESS) {
      streamDataSubmitDestroy(px);
      taosFreeQitem(pItem);
      return code;
    }
313 314 315

    qDebug("s-task:%s submit enqueue msgLen:%d ver:%" PRId64 ", total in queue:%d, size:%.2fMiB", pTask->id.idStr,
           px->submit.msgLen, px->submit.ver, total, size + px->submit.msgLen/1048576.0);
316 317
  } else if (type == STREAM_INPUT__DATA_BLOCK || type == STREAM_INPUT__DATA_RETRIEVE ||
             type == STREAM_INPUT__REF_DATA_BLOCK) {
318
    if ((pTask->taskLevel == TASK_LEVEL__SOURCE) && (tInputQueueIsFull(pTask))) {
H
Haojun Liao 已提交
319
      qError("s-task:%s input queue is full, capacity:%d size:%d MiB, current(blocks:%d, size:%.2fMiB) abort",
320
             pTask->id.idStr, STREAM_TASK_INPUT_QUEUEU_CAPACITY, STREAM_TASK_INPUT_QUEUEU_CAPACITY_IN_SIZE, total,
H
Haojun Liao 已提交
321
             size);
322
      destroyStreamDataBlock((SStreamDataBlock*) pItem);
323 324 325
      return -1;
    }

H
Haojun Liao 已提交
326
    qDebug("s-task:%s data block enqueue, current(blocks:%d, size:%.2fMiB)", pTask->id.idStr, total, size);
327 328 329 330 331
    int32_t code = taosWriteQitem(pTask->inputQueue->queue, pItem);
    if (code != TSDB_CODE_SUCCESS) {
      destroyStreamDataBlock((SStreamDataBlock*) pItem);
      return code;
    }
332 333 334
  } else if (type == STREAM_INPUT__CHECKPOINT) {
    taosWriteQitem(pTask->inputQueue->queue, pItem);
  } else if (type == STREAM_INPUT__GET_RES) {
335
    // use the default memory limit, refactor later.
336
    taosWriteQitem(pTask->inputQueue->queue, pItem);
H
Haojun Liao 已提交
337
    qDebug("s-task:%s data res enqueue, current(blocks:%d, size:%.2fMiB)", pTask->id.idStr, total, size);
338 339 340 341 342 343 344
  }

  if (type != STREAM_INPUT__GET_RES && type != STREAM_INPUT__CHECKPOINT && pTask->triggerParam != 0) {
    atomic_val_compare_exchange_8(&pTask->triggerStatus, TASK_TRIGGER_STATUS__INACTIVE, TASK_TRIGGER_STATUS__ACTIVE);
  }

  return 0;
345 346
}

347 348
static void* streamQueueCurItem(SStreamQueue* queue) { return queue->qItem; }

349 350 351 352 353 354 355 356 357 358 359 360 361 362
void* streamQueueNextItem(SStreamQueue* queue) {
  int8_t dequeueFlag = atomic_exchange_8(&queue->status, STREAM_QUEUE__PROCESSING);
  if (dequeueFlag == STREAM_QUEUE__FAILED) {
    ASSERT(queue->qItem != NULL);
    return streamQueueCurItem(queue);
  } else {
    queue->qItem = NULL;
    taosGetQitem(queue->qall, &queue->qItem);
    if (queue->qItem == NULL) {
      taosReadAllQitems(queue->queue, queue->qall);
      taosGetQitem(queue->qall, &queue->qItem);
    }
    return streamQueueCurItem(queue);
  }
363 364
}

dengyihao's avatar
dengyihao 已提交
365
void streamTaskInputFail(SStreamTask* pTask) { atomic_store_8(&pTask->inputStatus, TASK_INPUT_STATUS__FAILED); }