tqInt.h 6.2 KB
Newer Older
H
refact  
Hongze Cheng 已提交
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/>.
 */

#ifndef _TD_TQ_INT_H_
#define _TD_TQ_INT_H_

L
Liu Jicong 已提交
19
#include "meta.h"
L
Liu Jicong 已提交
20
#include "tlog.h"
L
Liu Jicong 已提交
21
#include "tqPush.h"
L
Liu Jicong 已提交
22
#include "vnd.h"
L
Liu Jicong 已提交
23

H
refact  
Hongze Cheng 已提交
24 25 26 27
#ifdef __cplusplus
extern "C" {
#endif

28 29 30 31 32
#define tqFatal(...)                                             \
  {                                                              \
    if (tqDebugFlag & DEBUG_FATAL) {                             \
      taosPrintLog("TQ  FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); \
    }                                                            \
L
Liu Jicong 已提交
33
  }
L
Liu Jicong 已提交
34

35 36 37 38 39
#define tqError(...)                                             \
  {                                                              \
    if (tqDebugFlag & DEBUG_ERROR) {                             \
      taosPrintLog("TQ  ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); \
    }                                                            \
L
Liu Jicong 已提交
40
  }
L
Liu Jicong 已提交
41

42 43 44 45 46
#define tqWarn(...)                                            \
  {                                                            \
    if (tqDebugFlag & DEBUG_WARN) {                            \
      taosPrintLog("TQ  WARN ", DEBUG_WARN, 255, __VA_ARGS__); \
    }                                                          \
L
Liu Jicong 已提交
47
  }
L
Liu Jicong 已提交
48

49 50 51 52 53
#define tqInfo(...)                                       \
  {                                                       \
    if (tqDebugFlag & DEBUG_INFO) {                       \
      taosPrintLog("TQ  ", DEBUG_INFO, 255, __VA_ARGS__); \
    }                                                     \
L
Liu Jicong 已提交
54
  }
L
Liu Jicong 已提交
55

56 57 58 59 60
#define tqDebug(...)                                               \
  {                                                                \
    if (tqDebugFlag & DEBUG_DEBUG) {                               \
      taosPrintLog("TQ  ", DEBUG_DEBUG, tqDebugFlag, __VA_ARGS__); \
    }                                                              \
L
Liu Jicong 已提交
61
  }
L
Liu Jicong 已提交
62

63 64 65 66 67
#define tqTrace(...)                                               \
  {                                                                \
    if (tqDebugFlag & DEBUG_TRACE) {                               \
      taosPrintLog("TQ  ", DEBUG_TRACE, tqDebugFlag, __VA_ARGS__); \
    }                                                              \
L
Liu Jicong 已提交
68 69
  }

L
Liu Jicong 已提交
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#define TQ_BUFFER_SIZE 8

#define TQ_BUCKET_MASK 0xFF
#define TQ_BUCKET_SIZE 256

#define TQ_PAGE_SIZE 4096
// key + offset + size
#define TQ_IDX_SIZE 24
// 4096 / 24
#define TQ_MAX_IDX_ONE_PAGE 170
// 24 * 170
#define TQ_IDX_PAGE_BODY_SIZE 4080
// 4096 - 4080
#define TQ_IDX_PAGE_HEAD_SIZE 16

L
Liu Jicong 已提交
85 86
#define TQ_ACTION_CONST      0
#define TQ_ACTION_INUSE      1
L
Liu Jicong 已提交
87
#define TQ_ACTION_INUSE_CONT 2
L
Liu Jicong 已提交
88
#define TQ_ACTION_INTXN      3
L
Liu Jicong 已提交
89 90 91 92 93

#define TQ_SVER 0

// TODO: inplace mode is not implemented
#define TQ_UPDATE_INPLACE 0
L
Liu Jicong 已提交
94
#define TQ_UPDATE_APPEND  1
L
Liu Jicong 已提交
95 96

#define TQ_DUP_INTXN_REWRITE 0
L
Liu Jicong 已提交
97
#define TQ_DUP_INTXN_REJECT  2
L
Liu Jicong 已提交
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116

static inline bool tqUpdateAppend(int32_t tqConfigFlag) { return tqConfigFlag & TQ_UPDATE_APPEND; }

static inline bool tqDupIntxnReject(int32_t tqConfigFlag) { return tqConfigFlag & TQ_DUP_INTXN_REJECT; }

static const int8_t TQ_CONST_DELETE = TQ_ACTION_CONST;

#define TQ_DELETE_TOKEN (void*)&TQ_CONST_DELETE

typedef enum { TQ_ITEM_READY, TQ_ITEM_PROCESS, TQ_ITEM_EMPTY } STqItemStatus;

typedef struct {
  int16_t ver;
  int16_t action;
  int32_t checksum;
  int64_t ssize;
  char    content[];
} STqSerializedHead;

L
Liu Jicong 已提交
117 118
typedef int32_t (*FTqSerialize)(const void* pObj, STqSerializedHead** ppHead);
typedef int32_t (*FTqDeserialize)(void* self, const STqSerializedHead* pHead, void** ppObj);
L
Liu Jicong 已提交
119
typedef void (*FTqDelete)(void*);
L
Liu Jicong 已提交
120 121

typedef struct {
L
Liu Jicong 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
  int64_t key;
  int64_t offset;
  int64_t serializedSize;
  void*   valueInUse;
  void*   valueInTxn;
} STqMetaHandle;

typedef struct STqMetaList {
  STqMetaHandle       handle;
  struct STqMetaList* next;
  // struct STqMetaList* inTxnPrev;
  // struct STqMetaList* inTxnNext;
  struct STqMetaList* unpersistPrev;
  struct STqMetaList* unpersistNext;
} STqMetaList;

typedef struct {
L
Liu Jicong 已提交
139
  STQ*         pTq;
L
Liu Jicong 已提交
140 141 142 143 144 145
  STqMetaList* bucket[TQ_BUCKET_SIZE];
  // a table head
  STqMetaList* unpersistHead;
  // topics that are not connectted
  STqMetaList* unconnectTopic;

146 147
  TdFilePtr pFile;
  TdFilePtr pIdxFile;
L
Liu Jicong 已提交
148 149 150 151 152 153 154 155

  char*          dirPath;
  int32_t        tqConfigFlag;
  FTqSerialize   pSerializer;
  FTqDeserialize pDeserializer;
  FTqDelete      pDeleter;
} STqMetaStore;

L
Liu Jicong 已提交
156 157 158 159 160
typedef struct {
  SMemAllocatorFactory* pAllocatorFactory;
  SMemAllocator*        pAllocator;
} STqMemRef;

L
Liu Jicong 已提交
161 162 163 164 165 166 167
struct STQ {
  // the collection of groups
  // the handle of meta kvstore
  char*         path;
  STqCfg*       tqConfig;
  STqMemRef     tqMemRef;
  STqMetaStore* tqMeta;
L
Liu Jicong 已提交
168
  STqPushMgr*   tqPushMgr;
L
Liu Jicong 已提交
169
  SHashObj*     pStreamTasks;
L
Liu Jicong 已提交
170
  SWal*         pWal;
L
Liu Jicong 已提交
171
  SMeta*        pVnodeMeta;
L
Liu Jicong 已提交
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
};

typedef struct {
  int8_t inited;
  tmr_h  timer;
} STqMgmt;

static STqMgmt tqMgmt;

typedef struct {
  int8_t         status;
  int64_t        offset;
  qTaskInfo_t    task;
  STqReadHandle* pReadHandle;
} STqTaskItem;

// new version
typedef struct {
  int64_t     firstOffset;
  int64_t     lastOffset;
  STqTaskItem output[TQ_BUFFER_SIZE];
} STqBuffer;

typedef struct {
  char            topicName[TSDB_TOPIC_FNAME_LEN];
  char*           sql;
  char*           logicalPlan;
  char*           physicalPlan;
  char*           qmsg;
  STqBuffer       buffer;
  SWalReadHandle* pReadhandle;
L
Liu Jicong 已提交
203
} STqTopic;
L
Liu Jicong 已提交
204 205 206 207 208

typedef struct {
  int64_t consumerId;
  int64_t epoch;
  char    cgroup[TSDB_TOPIC_FNAME_LEN];
L
Liu Jicong 已提交
209
  SArray* topics;  // SArray<STqTopic>
L
Liu Jicong 已提交
210
} STqConsumer;
L
Liu Jicong 已提交
211

L
Liu Jicong 已提交
212 213
int32_t tqSerializeConsumer(const STqConsumer*, STqSerializedHead**);
int32_t tqDeserializeConsumer(STQ*, const STqSerializedHead*, STqConsumer**);
L
Liu Jicong 已提交
214

L
Liu Jicong 已提交
215
static int FORCE_INLINE tqQueryExecuting(int32_t status) { return status; }
L
Liu Jicong 已提交
216

H
refact  
Hongze Cheng 已提交
217 218 219 220
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
221
#endif /*_TD_TQ_INT_H_*/