tsdb.h 10.8 KB
Newer Older
L
Liu Jicong 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * 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/>.
 */
H
Hongze Cheng 已提交
15 16 17 18

#ifndef _TD_VNODE_TSDB_H_
#define _TD_VNODE_TSDB_H_

H
Hongze Cheng 已提交
19 20
#include "vnodeInt.h"

H
Hongze Cheng 已提交
21 22 23 24
#ifdef __cplusplus
extern "C" {
#endif

H
Hongze Cheng 已提交
25 26 27 28 29 30 31 32 33 34
// tsdbDebug ================
// clang-format off
#define tsdbFatal(...) do { if (tsdbDebugFlag & DEBUG_FATAL) { taosPrintLog("TSDB FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }}     while(0)
#define tsdbError(...) do { if (tsdbDebugFlag & DEBUG_ERROR) { taosPrintLog("TSDB ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }}     while(0)
#define tsdbWarn(...)  do { if (tsdbDebugFlag & DEBUG_WARN)  { taosPrintLog("TSDB WARN ", DEBUG_WARN, 255, __VA_ARGS__); }}       while(0)
#define tsdbInfo(...)  do { if (tsdbDebugFlag & DEBUG_INFO)  { taosPrintLog("TSDB ", DEBUG_INFO, 255, __VA_ARGS__); }}            while(0)
#define tsdbDebug(...) do { if (tsdbDebugFlag & DEBUG_DEBUG) { taosPrintLog("TSDB ", DEBUG_DEBUG, tsdbDebugFlag, __VA_ARGS__); }} while(0)
#define tsdbTrace(...) do { if (tsdbDebugFlag & DEBUG_TRACE) { taosPrintLog("TSDB ", DEBUG_TRACE, tsdbDebugFlag, __VA_ARGS__); }} while(0)
// clang-format on

H
more  
Hongze Cheng 已提交
35
typedef struct TSDBROW TSDBROW;
H
Hongze Cheng 已提交
36
typedef struct TSDBKEY TSDBKEY;
H
Hongze Cheng 已提交
37
typedef struct TABLEID TABLEID;
H
more  
Hongze Cheng 已提交
38
typedef struct SDelOp  SDelOp;
H
Hongze Cheng 已提交
39

H
Hongze Cheng 已提交
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
// tsdbMemTable ==============================================================================================
typedef struct STbData     STbData;
typedef struct SMemTable   SMemTable;
typedef struct STbDataIter STbDataIter;
typedef struct SMergeInfo  SMergeInfo;
typedef struct STable      STable;

// SMemTable
int32_t tsdbMemTableCreate(STsdb *pTsdb, SMemTable **ppMemTable);
void    tsdbMemTableDestroy(SMemTable *pMemTable);
void    tsdbGetTbDataFromMemTable(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, STbData **ppTbData);

// STbDataIter
int32_t tsdbTbDataIterCreate(STbData *pTbData, TSDBKEY *pFrom, int8_t backward, STbDataIter **ppIter);
void   *tsdbTbDataIterDestroy(STbDataIter *pIter);
void    tsdbTbDataIterOpen(STbData *pTbData, TSDBKEY *pFrom, int8_t backward, STbDataIter *pIter);
bool    tsdbTbDataIterNext(STbDataIter *pIter);
bool    tsdbTbDataIterGet(STbDataIter *pIter, TSDBROW *pRow);

H
refact  
Hongze Cheng 已提交
59
// tsdbFile.c ==============================================================================================
H
Hongze Cheng 已提交
60 61 62 63 64 65 66 67
typedef struct SDelFile       SDelFile;
typedef struct STsdbCacheFile STsdbCacheFile;
typedef struct STsdbIndexFile STsdbIndexFile;
typedef struct STsdbDataFile  STsdbDataFile;
typedef struct STsdbLastFile  STsdbLastFile;
typedef struct STsdbSmaFile   STsdbSmaFile;
typedef struct STsdbSmalFile  STsdbSmalFile;
typedef struct SDFileSet      SDFileSet;
H
Hongze Cheng 已提交
68 69

// tsdbFS.c ==============================================================================================
H
Hongze Cheng 已提交
70 71 72 73 74 75 76
typedef struct STsdbFS STsdbFS;

int32_t tsdbFSOpen(STsdb *pTsdb, STsdbFS **ppFS);
int32_t tsdbFSClose(STsdbFS *pFS);
int32_t tsdbFSStart(STsdbFS *pFS);
int32_t tsdbFSEnd(STsdbFS *pFS, int8_t rollback);

H
Hongze Cheng 已提交
77
// tsdbReaderWriter.c ==============================================================================================
H
Hongze Cheng 已提交
78 79 80 81
typedef struct SDelDataItem SDelDataItem;
typedef struct SDelData     SDelData;
typedef struct SDelIdxItem  SDelIdxItem;
typedef struct SDelIdx      SDelIdx;
H
Hongze Cheng 已提交
82

H
Hongze Cheng 已提交
83 84 85 86 87 88
// SDataFWriter
typedef struct SDataFWriter SDataFWriter;

// SDataFReader
typedef struct SDataFReader SDataFReader;

H
Hongze Cheng 已提交
89 90 91
// SDelFWriter
typedef struct SDelFWriter SDelFWriter;

H
Hongze Cheng 已提交
92
int32_t tsdbDelFWriterOpen(SDelFWriter **ppWriter, SDelFile *pFile, STsdb *pTsdb);
H
Hongze Cheng 已提交
93
int32_t tsdbDelFWriterClose(SDelFWriter *pWriter);
H
Hongze Cheng 已提交
94
int32_t tsdbWriteDelData(SDelFWriter *pWriter, SDelData *pDelData, uint8_t **ppBuf, SDelIdxItem *pItem);
H
Hongze Cheng 已提交
95 96 97 98 99
int32_t tsdbWriteDelIdx(SDelFWriter *pWriter, SDelIdx *pDelIdx, uint8_t **ppBuf);

// SDelFReader
typedef struct SDelFReader SDelFReader;

H
Hongze Cheng 已提交
100
int32_t tsdbDelFReaderOpen(SDelFReader **ppReader, SDelFile *pFile, STsdb *pTsdb, uint8_t **ppBuf);
H
Hongze Cheng 已提交
101 102 103
int32_t tsdbDelFReaderClose(SDelFReader *pReader);
int32_t tsdbReadDelData(SDelFReader *pReader, SDelData *pDelData, uint8_t **ppBuf);
int32_t tsdbReadDelIdx(SDelFReader *pReader, SDelIdx *pDelIdx, uint8_t **ppBuf);
H
Hongze Cheng 已提交
104

H
Hongze Cheng 已提交
105 106 107 108 109 110
// SCacheFWriter
typedef struct SCacheFWriter SCacheFWriter;

// SCacheFReader
typedef struct SCacheFReader SCacheFReader;

H
Hongze Cheng 已提交
111 112
// tsdbCommit.c ==============================================================================================

H
refact  
Hongze Cheng 已提交
113 114 115 116 117 118 119 120 121 122
// tsdbReadImpl.c ==============================================================================================
typedef struct SBlockIdx    SBlockIdx;
typedef struct SBlockInfo   SBlockInfo;
typedef struct SBlock       SBlock;
typedef struct SBlockCol    SBlockCol;
typedef struct SBlockStatis SBlockStatis;
typedef struct SAggrBlkCol  SAggrBlkCol;
typedef struct SBlockData   SBlockData;
typedef struct SReadH       SReadH;

H
Hongze Cheng 已提交
123 124
typedef struct SDFileSetReader SDFileSetReader;
typedef struct SDFileSetWriter SDFileSetWriter;
H
refact  
Hongze Cheng 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139

// SDFileSetWriter
int32_t tsdbDFileSetWriterOpen(SDFileSetWriter *pWriter, STsdb *pTsdb, SDFileSet *pSet);
int32_t tsdbDFileSetWriterClose(SDFileSetWriter *pWriter);
int32_t tsdbWriteBlockData(SDFileSetWriter *pWriter, SDataCols *pDataCols, SBlock *pBlock);
int32_t tsdbWriteSBlockInfo(SDFileSetWriter *pWriter, SBlockInfo *pBlockInfo, SBlockIdx *pBlockIdx);
int32_t tsdbWriteSBlockIdx(SDFileSetWriter *pWriter, SBlockIdx *pBlockIdx);

// SDFileSetReader
int32_t tsdbDFileSetReaderOpen(SDFileSetReader *pReader, STsdb *pTsdb, SDFileSet *pSet);
int32_t tsdbDFileSetReaderClose(SDFileSetReader *pReader);
int32_t tsdbLoadSBlockIdx(SDFileSetReader *pReader, SArray *pArray);
int32_t tsdbLoadSBlockInfo(SDFileSetReader *pReader, SBlockIdx *pBlockIdx, SBlockInfo *pBlockInfo);
int32_t tsdbLoadSBlockStatis(SDFileSetReader *pReader, SBlock *pBlock, SBlockStatis *pBlockStatis);

H
Hongze Cheng 已提交
140
// SDelFWriter
H
refact  
Hongze Cheng 已提交
141

H
Hongze Cheng 已提交
142
// SDelFReader
H
refact  
Hongze Cheng 已提交
143

H
Hongze Cheng 已提交
144
// tsdbUtil.c ==============================================================================================
H
Hongze Cheng 已提交
145 146 147
int32_t tsdbRealloc(uint8_t **ppBuf, int64_t size);
void    tsdbFree(uint8_t *pBuf);

H
Hongze Cheng 已提交
148 149 150
int32_t tTABLEIDCmprFn(const void *p1, const void *p2);
int32_t tsdbKeyCmprFn(const void *p1, const void *p2);

H
Hongze Cheng 已提交
151 152 153 154 155 156
int32_t tPutDelIdx(uint8_t *p, SDelIdx *pDelIdx);
int32_t tGetDelIdx(uint8_t *p, SDelIdx *pDelIdx);

int32_t tPutDelData(uint8_t *p, SDelData *pDelData);
int32_t tGetDelData(uint8_t *p, SDelData *pDelData);

H
Hongze Cheng 已提交
157 158 159 160 161 162 163 164
// structs
typedef struct {
  int   minFid;
  int   midFid;
  int   maxFid;
  TSKEY minKey;
} SRtn;

C
Cary Xu 已提交
165
#define TSDB_DATA_DIR_LEN 6  // adapt accordingly
H
Hongze Cheng 已提交
166
struct STsdb {
H
refact  
Hongze Cheng 已提交
167 168 169 170 171 172 173 174 175 176
  char         *path;
  SVnode       *pVnode;
  TdThreadMutex mutex;
  char          dir[TSDB_DATA_DIR_LEN];
  bool          repoLocked;
  STsdbKeepCfg  keepCfg;
  SMemTable    *mem;
  SMemTable    *imem;
  SRtn          rtn;
  STsdbFS      *fs;
H
Hongze Cheng 已提交
177 178
};

H
Hongze Cheng 已提交
179
struct STable {
H
Hongze Cheng 已提交
180
  uint64_t  suid;
H
Hongze Cheng 已提交
181
  uint64_t  uid;
182 183
  STSchema *pSchema;       // latest schema
  STSchema *pCacheSchema;  // cached cache
H
Hongze Cheng 已提交
184
};
H
Hongze Cheng 已提交
185

H
refact  
Hongze Cheng 已提交
186 187 188 189 190
struct TSDBKEY {
  int64_t version;
  TSKEY   ts;
};

H
Hongze Cheng 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204
typedef struct SMemSkipListNode SMemSkipListNode;
struct SMemSkipListNode {
  int8_t            level;
  SMemSkipListNode *forwards[0];
};
typedef struct SMemSkipList {
  uint32_t          seed;
  int64_t           size;
  int8_t            maxLevel;
  int8_t            level;
  SMemSkipListNode *pHead;
  SMemSkipListNode *pTail;
} SMemSkipList;

H
Hongze Cheng 已提交
205
struct STbData {
H
Hongze Cheng 已提交
206 207 208 209 210 211 212
  tb_uid_t     suid;
  tb_uid_t     uid;
  TSDBKEY      minKey;
  TSDBKEY      maxKey;
  SDelOp      *pHead;
  SDelOp      *pTail;
  SMemSkipList sl;
H
Hongze Cheng 已提交
213
};
H
Hongze Cheng 已提交
214

H
refact  
Hongze Cheng 已提交
215
struct SMemTable {
H
Hongze Cheng 已提交
216 217 218 219 220 221
  SRWLatch latch;
  STsdb   *pTsdb;
  int32_t  nRef;
  TSDBKEY  minKey;
  TSDBKEY  maxKey;
  int64_t  nRow;
H
Hongze Cheng 已提交
222
  int64_t  nDel;
H
Hongze Cheng 已提交
223
  SArray  *aTbData;  // SArray<STbData*>
H
Hongze Cheng 已提交
224
};
H
Hongze Cheng 已提交
225 226 227 228

int tsdbLockRepo(STsdb *pTsdb);
int tsdbUnlockRepo(STsdb *pTsdb);

H
Hongze Cheng 已提交
229 230 231 232 233
struct TSDBROW {
  int64_t version;
  STSRow *pTSRow;
};

H
refact  
Hongze Cheng 已提交
234
struct SBlockIdx {
H
Hongze Cheng 已提交
235 236 237 238 239 240
  int64_t suid;
  int64_t uid;
  int64_t maxVersion;
  int64_t minVersion;
  int64_t offset;
  int64_t size;
H
refact  
Hongze Cheng 已提交
241
};
H
Hongze Cheng 已提交
242 243 244 245 246 247 248 249

typedef enum {
  TSDB_SBLK_VER_0 = 0,
  TSDB_SBLK_VER_MAX,
} ESBlockVer;

#define SBlockVerLatest TSDB_SBLK_VER_0

H
refact  
Hongze Cheng 已提交
250
struct SBlock {
H
Hongze Cheng 已提交
251 252 253 254 255
  TSDBKEY minKey;
  TSDBKEY maxKey;
  int64_t maxVersion;
  int64_t minVersion;
  uint8_t flags;  // last, algorithm
H
refact  
Hongze Cheng 已提交
256
};
H
Hongze Cheng 已提交
257

H
refact  
Hongze Cheng 已提交
258
struct SBlockInfo {
H
Hongze Cheng 已提交
259
  int32_t  delimiter;  // For recovery usage
H
Hongze Cheng 已提交
260
  uint64_t suid;
H
Hongze Cheng 已提交
261 262
  uint64_t uid;
  SBlock   blocks[];
H
refact  
Hongze Cheng 已提交
263
};
H
Hongze Cheng 已提交
264

H
refact  
Hongze Cheng 已提交
265
struct SBlockCol {
H
Hongze Cheng 已提交
266 267
  int16_t  colId;
  uint16_t type : 6;
C
Cary Xu 已提交
268 269
  uint16_t blen : 10;  // 0 no bitmap if all rows are NORM, > 0 bitmap length
  uint32_t len;        // data length + bitmap length
H
Hongze Cheng 已提交
270
  uint32_t offset;
H
refact  
Hongze Cheng 已提交
271
};
H
Hongze Cheng 已提交
272

H
refact  
Hongze Cheng 已提交
273
struct SAggrBlkCol {
H
Hongze Cheng 已提交
274 275 276 277 278 279 280
  int16_t colId;
  int16_t maxIndex;
  int16_t minIndex;
  int16_t numOfNull;
  int64_t sum;
  int64_t max;
  int64_t min;
H
refact  
Hongze Cheng 已提交
281
};
H
Hongze Cheng 已提交
282

H
refact  
Hongze Cheng 已提交
283
struct SBlockData {
H
Hongze Cheng 已提交
284 285 286 287
  int32_t   delimiter;  // For recovery usage
  int32_t   numOfCols;  // For recovery usage
  uint64_t  uid;        // For recovery usage
  SBlockCol cols[];
H
refact  
Hongze Cheng 已提交
288
};
H
Hongze Cheng 已提交
289 290 291

typedef void SAggrBlkData;  // SBlockCol cols[];

H
Hongze Cheng 已提交
292
static FORCE_INLINE int TSDB_KEY_FID(TSKEY key, int32_t minutes, int8_t precision) {
H
Hongze Cheng 已提交
293
  if (key < 0) {
H
Hongze Cheng 已提交
294
    return (int)((key + 1) / tsTickPerMin[precision] / minutes - 1);
H
Hongze Cheng 已提交
295
  } else {
H
Hongze Cheng 已提交
296
    return (int)((key / tsTickPerMin[precision] / minutes));
H
Hongze Cheng 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
  }
}

static FORCE_INLINE int tsdbGetFidLevel(int fid, SRtn *pRtn) {
  if (fid >= pRtn->maxFid) {
    return 0;
  } else if (fid >= pRtn->midFid) {
    return 1;
  } else if (fid >= pRtn->minFid) {
    return 2;
  } else {
    return -1;
  }
}

// ================== TSDB global config
extern bool tsdbForceKeepFile;

H
Hongze Cheng 已提交
315
#define TSDB_FS_ITER_FORWARD  TSDB_ORDER_ASC
H
Hongze Cheng 已提交
316 317
#define TSDB_FS_ITER_BACKWARD TSDB_ORDER_DESC

H
Hongze Cheng 已提交
318 319 320 321 322
struct TABLEID {
  tb_uid_t suid;
  tb_uid_t uid;
};

H
Hongze Cheng 已提交
323 324 325 326 327 328
struct STbDataIter {
  STbData          *pTbData;
  int8_t            backward;
  SMemSkipListNode *pNode;
};

H
more  
Hongze Cheng 已提交
329 330 331 332
struct SDelOp {
  int64_t version;
  TSKEY   sKey;  // included
  TSKEY   eKey;  // included
H
Hongze Cheng 已提交
333
  SDelOp *pNext;
H
more  
Hongze Cheng 已提交
334 335
};

H
Hongze Cheng 已提交
336
struct SDelDataItem {
H
Hongze Cheng 已提交
337 338 339
  int64_t version;
  TSKEY   sKey;
  TSKEY   eKey;
H
Hongze Cheng 已提交
340
};
H
Hongze Cheng 已提交
341 342

struct SDelData {
H
Hongze Cheng 已提交
343 344 345 346 347 348 349 350
  uint32_t delimiter;
  tb_uid_t suid;
  tb_uid_t uid;
  uint8_t  flags;
  uint32_t nOffset;
  uint8_t *pOffset;
  uint32_t nData;
  uint8_t *pData;
H
Hongze Cheng 已提交
351
};
H
Hongze Cheng 已提交
352

H
Hongze Cheng 已提交
353
struct SDelIdxItem {
H
Hongze Cheng 已提交
354 355 356 357 358
  tb_uid_t suid;
  tb_uid_t uid;
  TSKEY    minKey;
  TSKEY    maxKey;
  int64_t  minVersion;
H
Hongze Cheng 已提交
359
  int64_t  maxVersion;
H
Hongze Cheng 已提交
360 361
  int64_t  offset;
  int64_t  size;
H
Hongze Cheng 已提交
362
};
H
Hongze Cheng 已提交
363

H
Hongze Cheng 已提交
364 365
struct SDelIdx {
  uint32_t delimiter;
H
Hongze Cheng 已提交
366 367
  uint8_t  flags;
  uint32_t nOffset;
H
Hongze Cheng 已提交
368
  uint8_t *pOffset;
H
Hongze Cheng 已提交
369 370
  uint32_t nData;
  uint8_t *pData;
H
Hongze Cheng 已提交
371 372
};

H
Hongze Cheng 已提交
373 374 375 376 377 378 379 380
struct SDelFile {
  TSKEY   minKey;
  TSKEY   maxKey;
  int64_t minVersion;
  int64_t maxVersion;
  int64_t size;
  int64_t offset;
};
H
Hongze Cheng 已提交
381

H
Hongze Cheng 已提交
382 383 384 385 386
#ifdef __cplusplus
}
#endif

#endif /*_TD_VNODE_TSDB_H_*/