tstream.h 9.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 "os.h"
L
Liu Jicong 已提交
17 18 19
#include "tdatablock.h"
#include "tmsg.h"
#include "tmsgcb.h"
L
Liu Jicong 已提交
20
#include "tqueue.h"
L
Liu Jicong 已提交
21 22 23 24 25 26
#include "trpc.h"

#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
27 28
#ifndef _STREAM_H_
#define _STREAM_H_
L
Liu Jicong 已提交
29

L
Liu Jicong 已提交
30 31
typedef struct SStreamTask SStreamTask;

L
Liu Jicong 已提交
32
enum {
L
Liu Jicong 已提交
33 34 35 36 37 38 39 40 41
  TASK_STATUS__IDLE = 1,
  TASK_STATUS__EXECUTING,
  TASK_STATUS__CLOSING,
};

enum {
  TASK_INPUT_STATUS__NORMAL = 1,
  TASK_INPUT_STATUS__BLOCKED,
  TASK_INPUT_STATUS__RECOVER,
L
Liu Jicong 已提交
42
  TASK_INPUT_STATUS__PROCESSING,
L
Liu Jicong 已提交
43
  TASK_INPUT_STATUS__STOP,
L
Liu Jicong 已提交
44
  TASK_INPUT_STATUS__FAILED,
L
Liu Jicong 已提交
45 46 47 48 49 50
};

enum {
  TASK_OUTPUT_STATUS__NORMAL = 1,
  TASK_OUTPUT_STATUS__WAIT,
  TASK_OUTPUT_STATUS__BLOCKED,
L
Liu Jicong 已提交
51 52
};

L
Liu Jicong 已提交
53 54 55 56 57
enum {
  STREAM_CREATED_BY__USER = 1,
  STREAM_CREATED_BY__SMA,
};

L
Liu Jicong 已提交
58 59 60 61 62 63
enum {
  STREAM_INPUT__DATA_SUBMIT = 1,
  STREAM_INPUT__DATA_BLOCK,
  STREAM_INPUT__CHECKPOINT,
};

L
Liu Jicong 已提交
64 65 66 67
typedef struct {
  int8_t type;
} SStreamQueueItem;

L
Liu Jicong 已提交
68
typedef struct {
L
Liu Jicong 已提交
69 70
  int8_t      type;
  int64_t     ver;
L
Liu Jicong 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
  int32_t*    dataRef;
  SSubmitReq* data;
} SStreamDataSubmit;

typedef struct {
  int8_t type;

  int32_t sourceVg;
  int64_t sourceVer;

  SArray* blocks;  // SArray<SSDataBlock*>
} SStreamDataBlock;

typedef struct {
  int8_t type;
} SStreamCheckpoint;

L
Liu Jicong 已提交
88 89 90 91 92 93
enum {
  STREAM_QUEUE__SUCESS = 1,
  STREAM_QUEUE__FAILED,
  STREAM_QUEUE__PROCESSING,
};

L
Liu Jicong 已提交
94 95 96 97
typedef struct {
  STaosQueue* queue;
  STaosQall*  qall;
  void*       qItem;
L
Liu Jicong 已提交
98 99
  int8_t      status;
} SStreamQueue;
L
Liu Jicong 已提交
100

L
Liu Jicong 已提交
101 102 103 104 105 106 107
SStreamQueue* streamQueueOpen();
void          streamQueueClose(SStreamQueue* queue);

static FORCE_INLINE void streamQueueProcessSuccess(SStreamQueue* queue) {
  ASSERT(atomic_load_8(&queue->status) == STREAM_QUEUE__PROCESSING);
  queue->qItem = NULL;
  atomic_store_8(&queue->status, STREAM_QUEUE__SUCESS);
L
Liu Jicong 已提交
108 109
}

L
Liu Jicong 已提交
110 111 112 113 114 115 116 117 118 119
static FORCE_INLINE void streamQueueProcessFail(SStreamQueue* queue) {
  ASSERT(atomic_load_8(&queue->status) == STREAM_QUEUE__PROCESSING);
  atomic_store_8(&queue->status, STREAM_QUEUE__FAILED);
}

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

static FORCE_INLINE void* streamQueueNextItem(SStreamQueue* queue) {
  int8_t dequeueFlag = atomic_exchange_8(&queue->status, STREAM_QUEUE__PROCESSING);
  if (dequeueFlag == STREAM_QUEUE__FAILED) {
L
Liu Jicong 已提交
120
    ASSERT(queue->qItem != NULL);
L
Liu Jicong 已提交
121
    return streamQueueCurItem(queue);
L
Liu Jicong 已提交
122 123 124 125 126 127
  } else {
    taosGetQitem(queue->qall, &queue->qItem);
    if (queue->qItem == NULL) {
      taosReadAllQitems(queue->queue, queue->qall);
      taosGetQitem(queue->qall, &queue->qItem);
    }
L
Liu Jicong 已提交
128
    return streamQueueCurItem(queue);
L
Liu Jicong 已提交
129 130 131
  }
}

L
Liu Jicong 已提交
132
SStreamDataSubmit* streamDataSubmitNew(SSubmitReq* pReq);
L
Liu Jicong 已提交
133 134 135 136 137 138 139 140 141 142

static FORCE_INLINE void streamDataSubmitRefDec(SStreamDataSubmit* pDataSubmit) {
  int32_t ref = atomic_sub_fetch_32(pDataSubmit->dataRef, 1);
  ASSERT(ref >= 0);
  if (ref == 0) {
    taosMemoryFree(pDataSubmit->data);
    taosMemoryFree(pDataSubmit->dataRef);
  }
}

L
Liu Jicong 已提交
143 144
SStreamDataSubmit* streamSubmitRefClone(SStreamDataSubmit* pSubmit);

L
Liu Jicong 已提交
145
#if 0
L
Liu Jicong 已提交
146 147
int32_t streamDataBlockEncode(void** buf, const SStreamDataBlock* pOutput);
void*   streamDataBlockDecode(const void* buf, SStreamDataBlock* pInput);
L
Liu Jicong 已提交
148 149
#endif

L
Liu Jicong 已提交
150 151 152 153
typedef struct {
  int8_t parallelizable;
  char*  qmsg;
  // followings are not applicable to encoder and decoder
L
Liu Jicong 已提交
154 155
  void* inputHandle;
  void* executor;
L
Liu Jicong 已提交
156 157 158
} STaskExec;

typedef struct {
L
Liu Jicong 已提交
159
  int32_t taskId;
L
Liu Jicong 已提交
160 161 162
} STaskDispatcherInplace;

typedef struct {
L
Liu Jicong 已提交
163
  int32_t taskId;
L
Liu Jicong 已提交
164 165 166 167 168
  int32_t nodeId;
  SEpSet  epSet;
} STaskDispatcherFixedEp;

typedef struct {
L
Liu Jicong 已提交
169 170 171
  // int8_t  hashMethod;
  char      stbFullName[TSDB_TABLE_FNAME_LEN];
  SUseDbRsp dbInfo;
L
Liu Jicong 已提交
172 173
} STaskDispatcherShuffle;

L
Liu Jicong 已提交
174
typedef void FTbSink(SStreamTask* pTask, void* vnode, int64_t ver, void* data);
L
Liu Jicong 已提交
175

L
Liu Jicong 已提交
176
typedef struct {
L
Liu Jicong 已提交
177
  int64_t         stbUid;
L
Liu Jicong 已提交
178
  char            stbFullName[TSDB_TABLE_FNAME_LEN];
L
Liu Jicong 已提交
179
  SSchemaWrapper* pSchemaWrapper;
L
Liu Jicong 已提交
180
  // not applicable to encoder and decoder
L
Liu Jicong 已提交
181 182
  void*     vnode;
  FTbSink*  tbSinkFunc;
L
Liu Jicong 已提交
183
  STSchema* pTSchema;
L
Liu Jicong 已提交
184 185 186
  SHashObj* pHash;  // groupId to tbuid
} STaskSinkTb;

L
Liu Jicong 已提交
187
typedef void FSmaSink(void* vnode, int64_t smaId, const SArray* data);
L
Liu Jicong 已提交
188

L
Liu Jicong 已提交
189
typedef struct {
L
Liu Jicong 已提交
190 191
  int64_t smaId;
  // following are not applicable to encoder and decoder
L
Liu Jicong 已提交
192
  FSmaSink* smaSink;
L
Liu Jicong 已提交
193 194 195 196 197 198 199 200
} STaskSinkSma;

typedef struct {
  int8_t reserved;
} STaskSinkFetch;

enum {
  TASK_SOURCE__SCAN = 1,
L
Liu Jicong 已提交
201 202
  TASK_SOURCE__PIPE,
  TASK_SOURCE__MERGE,
L
Liu Jicong 已提交
203 204 205 206
};

enum {
  TASK_EXEC__NONE = 1,
L
Liu Jicong 已提交
207 208
  TASK_EXEC__PIPE,
  TASK_EXEC__MERGE,
L
Liu Jicong 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
};

enum {
  TASK_DISPATCH__NONE = 1,
  TASK_DISPATCH__INPLACE,
  TASK_DISPATCH__FIXED,
  TASK_DISPATCH__SHUFFLE,
};

enum {
  TASK_SINK__NONE = 1,
  TASK_SINK__TABLE,
  TASK_SINK__SMA,
  TASK_SINK__FETCH,
};

L
Liu Jicong 已提交
225 226 227 228 229
enum {
  TASK_INPUT_TYPE__SUMBIT_BLOCK = 1,
  TASK_INPUT_TYPE__DATA_BLOCK,
};

L
Liu Jicong 已提交
230
struct SStreamTask {
L
Liu Jicong 已提交
231 232
  int64_t streamId;
  int32_t taskId;
L
Liu Jicong 已提交
233
  int8_t  inputType;
L
Liu Jicong 已提交
234 235 236 237 238 239 240 241
  int8_t  status;

  int8_t  sourceType;
  int8_t  execType;
  int8_t  sinkType;
  int8_t  dispatchType;
  int16_t dispatchMsgType;

L
Liu Jicong 已提交
242 243
  // node info
  int32_t childId;
L
Liu Jicong 已提交
244 245 246
  int32_t nodeId;
  SEpSet  epSet;

L
Liu Jicong 已提交
247 248 249
  // exec
  STaskExec exec;

L
Liu Jicong 已提交
250 251 252
  // TODO: merge sink and dispatch

  //  local sink
L
Liu Jicong 已提交
253 254 255 256 257 258 259 260 261 262 263 264 265
  union {
    STaskSinkTb    tbSink;
    STaskSinkSma   smaSink;
    STaskSinkFetch fetchSink;
  };

  // dispatch
  union {
    STaskDispatcherInplace inplaceDispatcher;
    STaskDispatcherFixedEp fixedEpDispatcher;
    STaskDispatcherShuffle shuffleDispatcher;
  };

L
Liu Jicong 已提交
266 267
  int8_t inputStatus;
  int8_t outputStatus;
L
Liu Jicong 已提交
268 269 270

  SStreamQueue* inputQueue;
  SStreamQueue* outputQueue;
L
Liu Jicong 已提交
271

L
Liu Jicong 已提交
272 273
  // application storage
  void* ahandle;
L
Liu Jicong 已提交
274
};
L
Liu Jicong 已提交
275

L
Liu Jicong 已提交
276
SStreamTask* tNewSStreamTask(int64_t streamId, int32_t childId);
H
Hongze Cheng 已提交
277 278
int32_t      tEncodeSStreamTask(SEncoder* pEncoder, const SStreamTask* pTask);
int32_t      tDecodeSStreamTask(SDecoder* pDecoder, SStreamTask* pTask);
L
Liu Jicong 已提交
279 280
void         tFreeSStreamTask(SStreamTask* pTask);

L
Liu Jicong 已提交
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
static FORCE_INLINE int32_t streamTaskInput(SStreamTask* pTask, SStreamQueueItem* pItem) {
  while (1) {
    int8_t inputStatus =
        atomic_val_compare_exchange_8(&pTask->inputStatus, TASK_INPUT_STATUS__NORMAL, TASK_INPUT_STATUS__PROCESSING);
    if (inputStatus == TASK_INPUT_STATUS__NORMAL) {
      break;
    }
    ASSERT(0);
  }

  if (pItem->type == STREAM_INPUT__DATA_SUBMIT) {
    SStreamDataSubmit* pSubmitClone = streamSubmitRefClone((SStreamDataSubmit*)pItem);
    if (pSubmitClone == NULL) {
      atomic_store_8(&pTask->inputStatus, TASK_INPUT_STATUS__FAILED);
      return -1;
    }
    taosWriteQitem(pTask->inputQueue->queue, pSubmitClone);
  } else if (pItem->type == STREAM_INPUT__DATA_BLOCK) {
    taosWriteQitem(pTask->inputQueue->queue, pItem);
  } else if (pItem->type == STREAM_INPUT__CHECKPOINT) {
    taosWriteQitem(pTask->inputQueue->queue, pItem);
  }

  // TODO: back pressure
  atomic_store_8(&pTask->inputStatus, TASK_INPUT_STATUS__NORMAL);
  return 0;
}

static FORCE_INLINE void streamTaskInputFail(SStreamTask* pTask) {
  atomic_store_8(&pTask->inputStatus, TASK_INPUT_STATUS__FAILED);
}

static FORCE_INLINE int32_t streamTaskOutput(SStreamTask* pTask, SStreamDataBlock* pBlock) {
314 315 316 317 318 319 320 321 322 323
  if (pTask->sinkType == TASK_SINK__TABLE) {
    ASSERT(pTask->dispatchType == TASK_DISPATCH__NONE);
    pTask->tbSink.tbSinkFunc(pTask, pTask->tbSink.vnode, 0, pBlock->blocks);
  } else if (pTask->sinkType == TASK_SINK__SMA) {
    ASSERT(pTask->dispatchType == TASK_DISPATCH__NONE);
    pTask->smaSink.smaSink(pTask->ahandle, pTask->smaSink.smaId, pBlock->blocks);
  } else {
    ASSERT(pTask->dispatchType != TASK_DISPATCH__NONE);
    taosWriteQitem(pTask->outputQueue->queue, pBlock);
  }
L
Liu Jicong 已提交
324 325
  return 0;
}
L
Liu Jicong 已提交
326 327 328 329 330 331 332 333 334 335 336 337

typedef struct {
  int32_t reserved;
} SStreamTaskDeployRsp;

typedef struct {
  // SMsgHead head;
  int64_t streamId;
  int32_t taskId;
  SArray* data;  // SArray<SSDataBlock>
} SStreamTaskExecReq;

L
Liu Jicong 已提交
338 339 340 341 342
typedef struct {
  // SMsgHead     head;
  SStreamTask* task;
} SStreamTaskDeployReq;

L
Liu Jicong 已提交
343 344 345 346 347 348 349 350
int32_t tEncodeSStreamTaskExecReq(void** buf, const SStreamTaskExecReq* pReq);
void*   tDecodeSStreamTaskExecReq(const void* buf, SStreamTaskExecReq* pReq);
void    tFreeSStreamTaskExecReq(SStreamTaskExecReq* pReq);

typedef struct {
  int32_t reserved;
} SStreamTaskExecRsp;

L
Liu Jicong 已提交
351 352 353 354 355 356
typedef struct {
  SMsgHead head;
  int64_t  streamId;
  int32_t  taskId;
} SStreamTaskRunReq;

L
Liu Jicong 已提交
357 358 359 360 361
typedef struct {
  int64_t streamId;
  int32_t taskId;
  int32_t sourceTaskId;
  int32_t sourceVg;
L
Liu Jicong 已提交
362
  int32_t sourceChildId;
L
Liu Jicong 已提交
363 364 365
#if 0
  int64_t sourceVer;
#endif
L
Liu Jicong 已提交
366 367 368
  int32_t blockNum;
  SArray* dataLen;  // SArray<int32_t>
  SArray* data;     // SArray<SRetrieveTableRsp*>
L
Liu Jicong 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389
} SStreamDispatchReq;

typedef struct {
  int64_t streamId;
  int32_t taskId;
  int8_t  inputStatus;
} SStreamDispatchRsp;

typedef struct {
  int64_t streamId;
  int32_t taskId;
  int32_t sourceTaskId;
  int32_t sourceVg;
} SStreamTaskRecoverReq;

typedef struct {
  int64_t streamId;
  int32_t taskId;
  int8_t  inputStatus;
} SStreamTaskRecoverRsp;

390 391
int32_t tDecodeStreamDispatchReq(SDecoder* pDecoder, SStreamDispatchReq* pReq);

L
Liu Jicong 已提交
392 393
int32_t streamTriggerByWrite(SStreamTask* pTask, int32_t vgId, SMsgCb* pMsgCb);

L
Liu Jicong 已提交
394
int32_t streamTaskRun(SStreamTask* pTask);
L
Liu Jicong 已提交
395

L
Liu Jicong 已提交
396
int32_t streamTaskProcessRunReq(SStreamTask* pTask, SMsgCb* pMsgCb);
L
Liu Jicong 已提交
397 398 399 400
int32_t streamProcessDispatchReq(SStreamTask* pTask, SMsgCb* pMsgCb, SStreamDispatchReq* pReq, SRpcMsg* pMsg);
int32_t streamProcessDispatchRsp(SStreamTask* pTask, SMsgCb* pMsgCb, SStreamDispatchRsp* pRsp);
int32_t streamProcessRecoverReq(SStreamTask* pTask, SMsgCb* pMsgCb, SStreamTaskRecoverReq* pReq, SRpcMsg* pMsg);
int32_t streamProcessRecoverRsp(SStreamTask* pTask, SStreamTaskRecoverRsp* pRsp);
L
Liu Jicong 已提交
401

L
Liu Jicong 已提交
402 403 404 405
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
406
#endif /* ifndef _STREAM_H_ */