tsdbMain.h 11.0 KB
Newer Older
H
more  
hzcheng 已提交
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
TD-34  
hzcheng 已提交
15 16
#ifndef _TD_TSDB_MAIN_H_
#define _TD_TSDB_MAIN_H_
H
more  
Hongze Cheng 已提交
17

H
TD-353  
Hongze Cheng 已提交
18 19
#include "hash.h"
#include "tcoding.h"
S
slguan 已提交
20
#include "tglobal.h"
H
TD-353  
Hongze Cheng 已提交
21
#include "tkvstore.h"
H
hzcheng 已提交
22
#include "tlist.h"
H
TD-353  
Hongze Cheng 已提交
23 24
#include "tlog.h"
#include "tref.h"
H
hzcheng 已提交
25
#include "tsdb.h"
H
TD-34  
hzcheng 已提交
26 27
#include "tskiplist.h"
#include "tutil.h"
H
more  
Hongze Cheng 已提交
28

H
more  
hzcheng 已提交
29 30 31 32
#ifdef __cplusplus
extern "C" {
#endif

H
hzcheng 已提交
33 34
extern int tsdbDebugFlag;

S
Shengliang Guan 已提交
35 36 37 38
#define tsdbError(...) { if (tsdbDebugFlag & DEBUG_ERROR) { taosPrintLog("ERROR TDB ", tsdbDebugFlag, __VA_ARGS__); }}
#define tsdbWarn(...)  { if (tsdbDebugFlag & DEBUG_WARN)  { taosPrintLog("WARN TDB ", tsdbDebugFlag, __VA_ARGS__); }}
#define tsdbTrace(...) { if (tsdbDebugFlag & DEBUG_TRACE) { taosPrintLog("TDB ", tsdbDebugFlag, __VA_ARGS__); }}
#define tsdbPrint(...) { taosPrintLog("TDB ", 255, __VA_ARGS__); }
H
hzcheng 已提交
39

H
TD-90  
Hongze Cheng 已提交
40
#define TSDB_MAX_TABLE_SCHEMAS 16
H
TD-353  
Hongze Cheng 已提交
41 42 43 44 45
#define TSDB_FILE_HEAD_SIZE 512
#define TSDB_FILE_DELIMITER 0xF00AFA0F

// Definitions
// ------------------ tsdbMeta.c
H
TD-34  
hzcheng 已提交
46
typedef struct STable {
H
TD-353  
Hongze Cheng 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
  ETableType     type;
  tstr*          name;  // NOTE: there a flexible string here
  STableId       tableId;
  uint64_t       suid;
  struct STable* pSuper;  // super table pointer
  uint8_t        numOfSchemas;
  STSchema       schema[TSDB_MAX_TABLE_SCHEMAS];
  STSchema*      tagSchema;
  SKVRow         tagVal;
  void*          pIndex;         // For TSDB_SUPER_TABLE, it is the skiplist index
  void*          eventHandler;   // TODO
  void*          streamHandler;  // TODO
  TSKEY          lastKey;        // lastkey inserted in this table, initialized as 0, TODO: make a structure
  char*          sql;
  void*          cqhandle;
H
TD-353  
Hongze Cheng 已提交
62
  T_REF_DECLARE();
H
TD-34  
hzcheng 已提交
63 64 65
} STable;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
66 67 68 69 70 71 72
  pthread_rwlock_t rwLock;

  int32_t   nTables;
  STable**  tables;
  SList*    superList;
  SHashObj* uidMap;
  SKVStore* pStore;
H
TD-34  
hzcheng 已提交
73 74
} STsdbMeta;

H
TD-353  
Hongze Cheng 已提交
75
// ------------------ tsdbBuffer.c
H
TD-34  
hzcheng 已提交
76
typedef struct {
H
TD-353  
Hongze Cheng 已提交
77 78 79 80 81
  int64_t blockId;
  int     offset;
  int     remain;
  char    data[];
} STsdbBufBlock;
H
TD-34  
hzcheng 已提交
82 83

typedef struct {
H
TD-353  
Hongze Cheng 已提交
84 85 86 87 88 89 90 91 92
  pthread_cond_t poolNotEmpty;
  int            bufBlockSize;
  int            tBufBlocks;
  int            nBufBlocks;
  int64_t        index;
  SList*         bufBlockList;
} STsdbBufPool;

// ------------------ tsdbMemTable.c
H
TD-34  
hzcheng 已提交
93
typedef struct {
H
TD-353  
Hongze Cheng 已提交
94 95 96 97 98 99
  uint64_t   uid;
  TSKEY      keyFirst;
  TSKEY      keyLast;
  int64_t    numOfRows;
  SSkipList* pData;
} STableData;
H
TD-34  
hzcheng 已提交
100 101

typedef struct {
H
TD-353  
Hongze Cheng 已提交
102 103 104 105 106 107 108
  T_REF_DECLARE();
  TSKEY        keyFirst;
  TSKEY        keyLast;
  int64_t      numOfRows;
  STableData** tData;
  SList*       actList;
  SList*       bufBlockList;
H
TD-353  
Hongze Cheng 已提交
109 110
  int          maxCols;
  int          maxRowBytes;
H
TD-353  
Hongze Cheng 已提交
111
} SMemTable;
H
TD-34  
hzcheng 已提交
112

H
TD-353  
Hongze Cheng 已提交
113
// ------------------ tsdbFile.c
H
TD-353  
Hongze Cheng 已提交
114 115 116 117 118 119 120
typedef enum {
  TSDB_FILE_TYPE_HEAD = 0,
  TSDB_FILE_TYPE_DATA,
  TSDB_FILE_TYPE_LAST,
  TSDB_FILE_TYPE_NHEAD,
  TSDB_FILE_TYPE_NLAST
} TSDB_FILE_TYPE;
H
Hongze Cheng 已提交
121

H
more  
Hongze Cheng 已提交
122
typedef struct {
H
TD-353  
Hongze Cheng 已提交
123 124 125 126 127 128
  uint32_t offset;
  uint32_t len;
  uint64_t size;      // total size of the file
  uint64_t tombSize;  // unused file size
  uint32_t totalBlocks;
  uint32_t totalSubBlocks;
129
} STsdbFileInfo;
H
TD-34  
hzcheng 已提交
130 131

typedef struct {
H
TD-353  
Hongze Cheng 已提交
132 133 134
  char* fname;
  int   fd;

H
TD-353  
Hongze Cheng 已提交
135
  STsdbFileInfo info;
H
hzcheng 已提交
136
} SFile;
H
more  
Hongze Cheng 已提交
137

H
hzcheng 已提交
138
typedef struct {
H
TD-353  
Hongze Cheng 已提交
139 140 141 142
  int fileId;
  SFile headF;
  SFile dataF;
  SFile lastF;
H
hzcheng 已提交
143 144 145
} SFileGroup;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
146 147
  pthread_rwlock_t fhlock;

H
TD-353  
Hongze Cheng 已提交
148 149 150
  int         maxFGroups;
  int         nFGroups;
  SFileGroup* pFGroup;
H
hzcheng 已提交
151
} STsdbFileH;
H
more  
Hongze Cheng 已提交
152

H
TD-34  
hzcheng 已提交
153 154 155 156 157 158 159
typedef struct {
  int         numOfFGroups;
  SFileGroup *base;
  SFileGroup *pFileGroup;
  int         direction;
} SFileGroupIter;

H
TD-353  
Hongze Cheng 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
// ------------------ tsdbMain.c
typedef struct {
  int8_t state;

  char*           rootDir;
  STsdbCfg        config;
  STsdbAppH       appH;
  STsdbStat       stat;
  STsdbMeta*      tsdbMeta;
  STsdbBufPool*   pPool;
  SMemTable*      mem;
  SMemTable*      imem;
  STsdbFileH*     tsdbFileH;
  int             commit;
  pthread_t       commitThread;
  pthread_mutex_t mutex;
  bool            repoLocked;
} STsdbRepo;

H
TD-353  
Hongze Cheng 已提交
179
// ------------------ tsdbRWHelper.c
H
TD-34  
hzcheng 已提交
180
typedef struct {
H
TD-185  
Hongze Cheng 已提交
181 182
  uint32_t len;
  uint32_t offset;
H
TD-353  
Hongze Cheng 已提交
183
  // uint32_t padding;
H
TD-185  
Hongze Cheng 已提交
184 185
  uint32_t hasLast : 2;
  uint32_t numOfBlocks : 30;
186 187
  uint64_t uid;
  TSKEY    maxKey;
H
TD-353  
Hongze Cheng 已提交
188 189
} SCompIdx;

190
typedef struct {
H
TD-353  
Hongze Cheng 已提交
191 192 193 194 195 196 197
  int64_t last : 1;
  int64_t offset : 63;
  int32_t algorithm : 8;
  int32_t numOfRows : 24;
  int32_t sversion;
  int32_t len;
  int16_t numOfSubBlocks;
198 199 200 201 202
  int16_t numOfCols;
  TSKEY   keyFirst;
  TSKEY   keyLast;
} SCompBlock;

H
TD-34  
hzcheng 已提交
203 204 205
typedef struct {
  int32_t    delimiter;  // For recovery usage
  int32_t    checksum;   // TODO: decide if checksum logic in this file or make it one API
206
  uint64_t   uid;
H
TD-34  
hzcheng 已提交
207 208
  SCompBlock blocks[];
} SCompInfo;
H
hzcheng 已提交
209

H
TD-34  
hzcheng 已提交
210
typedef struct {
H
TD-353  
Hongze Cheng 已提交
211 212
  int16_t colId;
  int16_t len;
H
TD-34  
hzcheng 已提交
213 214
  int32_t type : 8;
  int32_t offset : 24;
H
TD-321  
Hongze Cheng 已提交
215 216 217 218 219 220 221
  int64_t sum;
  int64_t max;
  int64_t min;
  int16_t maxIndex;
  int16_t minIndex;
  int16_t numOfNull;
  char    padding[2];
H
TD-34  
hzcheng 已提交
222 223 224 225 226
} SCompCol;

typedef struct {
  int32_t  delimiter;  // For recovery usage
  int32_t  numOfCols;  // For recovery usage
227
  uint64_t uid;        // For recovery usage
H
TD-34  
hzcheng 已提交
228 229
  SCompCol cols[];
} SCompData;
H
TD-34  
hzcheng 已提交
230

H
hzcheng 已提交
231 232 233
typedef enum { TSDB_WRITE_HELPER, TSDB_READ_HELPER } tsdb_rw_helper_t;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
234
  int   fid;
H
hzcheng 已提交
235 236 237 238 239 240 241 242 243 244 245 246
  TSKEY minKey;
  TSKEY maxKey;
  // For read/write purpose
  SFile headF;
  SFile dataF;
  SFile lastF;
  // For write purpose only
  SFile nHeadF;
  SFile nLastF;
} SHelperFile;

typedef struct {
247 248 249
  uint64_t uid;
  int32_t  tid;
  int32_t  sversion;
H
hzcheng 已提交
250 251 252
} SHelperTable;

typedef struct {
H
TD-353  
Hongze Cheng 已提交
253
  tsdb_rw_helper_t type;
H
TD-100  
hzcheng 已提交
254

H
TD-353  
Hongze Cheng 已提交
255 256
  STsdbRepo* pRepo;
  int8_t     state;
H
TD-100  
hzcheng 已提交
257
  // For file set usage
H
hzcheng 已提交
258
  SHelperFile files;
H
TD-353  
Hongze Cheng 已提交
259
  SCompIdx*   pCompIdx;
H
TD-100  
hzcheng 已提交
260
  // For table set usage
H
hzcheng 已提交
261
  SHelperTable tableInfo;
H
TD-353  
Hongze Cheng 已提交
262
  SCompInfo*   pCompInfo;
H
TD-100  
hzcheng 已提交
263 264
  bool         hasOldLastBlock;
  // For block set usage
H
TD-353  
Hongze Cheng 已提交
265
  SCompData* pCompData;
H
TD-353  
Hongze Cheng 已提交
266 267 268
  SDataCols* pDataCols[2];
  void*      pBuffer;     // Buffer to hold the whole data block
  void*      compBuffer;  // Buffer for temperary compress/decompress purpose
H
hzcheng 已提交
269 270
} SRWHelper;

H
TD-353  
Hongze Cheng 已提交
271 272 273 274 275 276 277 278

// Operations
// ------------------ tsdbMeta.c
#define TABLE_TYPE(t) (t)->type
#define TABLE_NAME(t) (t)->name
#define TABLE_CHAR_NAME(t) TABLE_NAME(t)->data
#define TALBE_UID(t) (t)->tableId.uid
#define TABLE_TID(t) (t)->tableId.tid
H
TD-353  
Hongze Cheng 已提交
279
#define TABLE_SUID(t) (t)->suid
H
TD-353  
Hongze Cheng 已提交
280 281 282 283
#define TABLE_LASTKEY(t) (t)->lastKey

STsdbMeta* tsdbNewMeta(STsdbCfg* pCfg);
void       tsdbFreeMeta(STsdbMeta* pMeta);
H
TD-353  
Hongze Cheng 已提交
284 285 286 287 288 289 290 291 292 293
int        tsdbOpenMeta(STsdbRepo* pRepo);
int        tsdbCloseMeta(STsdbRepo* pRepo);
STSchema*  tsdbGetTableSchema(STable* pTable);
STable*    tsdbGetTableByUid(STsdbMeta* pMeta, uint64_t uid);
STSchema*  tsdbGetTableSchemaByVersion(STable* pTable, int16_t version);
STSchema*  tsdbGetTableTagSchema(STable* pTable);
int        tsdbUpdateTable(STsdbMeta* pMeta, STable* pTable, STableCfg* pCfg);
int        tsdbWLockRepoMeta(STsdbRepo* pRepo);
int        tsdbRLockRepoMeta(STsdbRepo* pRepo);
int        tsdbUnlockRepoMeta(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
294 295
void       tsdbRefTable(STable* pTable);
void       tsdbUnRefTable(STable* pTable);
H
TD-353  
Hongze Cheng 已提交
296 297 298 299 300

// ------------------ tsdbBuffer.c
STsdbBufPool* tsdbNewBufPool();
void          tsdbFreeBufPool(STsdbBufPool* pBufPool);
int           tsdbOpenBufPool(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
301
void          tsdbCloseBufPool(STsdbRepo* pRepo);
H
TD-353  
Hongze Cheng 已提交
302 303 304
SListNode*    tsdbAllocBufBlockFromPool(STsdbRepo* pRepo);

// ------------------ tsdbMemTable.c
H
TD-353  
Hongze Cheng 已提交
305 306 307
int tsdbInsertRowToMem(STsdbRepo* pRepo, SDataRow row, STable* pTable);
int tsdbRefMemTable(STsdbRepo* pRepo, SMemTable* pMemTable);
int tsdbUnRefMemTable(STsdbRepo* pRepo, SMemTable* pMemTable);
H
TD-353  
Hongze Cheng 已提交
308
int tsdbTakeMemSnapshot(STsdbRepo* pRepo, SMemTable** pMem, SMemTable** pIMem);
H
TD-353  
Hongze Cheng 已提交
309 310 311 312 313 314 315 316 317

// ------------------ tsdbFile.c
#define TSDB_KEY_FILEID(key, daysPerFile, precision) ((key) / tsMsPerDay[(precision)] / (daysPerFile))
#define TSDB_MAX_FILE(keep, daysPerFile) ((keep) / (daysPerFile) + 3)
#define TSDB_MIN_FILE_ID(fh) (fh)->pFGroup[0].fileId
#define TSDB_MAX_FILE_ID(fh) (fh)->pFGroup[(fh)->nFGroups - 1].fileId
#define TSDB_FGROUP_ITER_FORWARD TSDB_ORDER_ASC
#define TSDB_FGROUP_ITER_BACKWARD TSDB_ORDER_DESC

H
TD-353  
Hongze Cheng 已提交
318 319
STsdbFileH* tsdbNewFileH(STsdbCfg* pCfg);
void        tsdbFreeFileH(STsdbFileH* pFileH);
H
TD-353  
Hongze Cheng 已提交
320 321

// ------------------ tsdbRWHelper.c
H
TD-353  
Hongze Cheng 已提交
322 323 324 325 326 327
#define TSDB_HELPER_CLEAR_STATE 0x0        // Clear state
#define TSDB_HELPER_FILE_SET_AND_OPEN 0x1  // File is set
#define TSDB_HELPER_IDX_LOAD 0x2           // SCompIdx part is loaded
#define TSDB_HELPER_TABLE_SET 0x4          // Table is set
#define TSDB_HELPER_INFO_LOAD 0x8          // SCompInfo part is loaded
#define TSDB_HELPER_FILE_DATA_LOAD 0x10    // SCompData part is loaded
H
TD-353  
Hongze Cheng 已提交
328 329 330 331
#define helperSetState(h, s) (((h)->state) |= (s))
#define helperClearState(h, s) ((h)->state &= (~(s)))
#define helperHasState(h, s) ((((h)->state) & (s)) == (s))
#define blockAtIdx(h, idx) ((h)->pCompInfo->blocks + idx)
H
TD-353  
Hongze Cheng 已提交
332 333
#define TSDB_MAX_SUBBLOCKS 8
#define IS_SUB_BLOCK(pBlock) ((pBlock)->numOfSubBlocks == 0)
H
TD-353  
Hongze Cheng 已提交
334 335 336 337
#define helperType(h) (h)->type
#define helperRepo(h) (h)->pRepo
#define helperState(h) (h)->state

H
TD-353  
Hongze Cheng 已提交
338 339
// ------------------ tsdbMain.c
#define REPO_ID(r) (r)->config.tsdbId
H
TD-353  
Hongze Cheng 已提交
340
#define IS_REPO_LOCKED(r) (r)->repoLocked
H
TD-353  
Hongze Cheng 已提交
341 342 343 344 345 346 347

char* tsdbGetMetaFileName(char* rootDir);
int   tsdbLockRepo(STsdbRepo* pRepo);
int   tsdbUnlockRepo(STsdbRepo* pRepo);

#if 0

H
hzcheng 已提交
348 349 350
// --------- Helper state


H
TD-100  
hzcheng 已提交
351 352
int  tsdbInitReadHelper(SRWHelper *pHelper, STsdbRepo *pRepo);
int  tsdbInitWriteHelper(SRWHelper *pHelper, STsdbRepo *pRepo);
H
hzcheng 已提交
353
void tsdbDestroyHelper(SRWHelper *pHelper);
H
TD-100  
hzcheng 已提交
354
void tsdbResetHelper(SRWHelper *pHelper);
H
hzcheng 已提交
355 356

// --------- For set operations
H
TD-100  
hzcheng 已提交
357 358
int tsdbSetAndOpenHelperFile(SRWHelper *pHelper, SFileGroup *pGroup);
void tsdbSetHelperTable(SRWHelper *pHelper, STable *pTable, STsdbRepo *pRepo);
H
hzcheng 已提交
359 360 361
int  tsdbCloseHelperFile(SRWHelper *pHelper, bool hasError);

// --------- For read operations
H
Hongze Cheng 已提交
362 363 364 365 366 367
int  tsdbLoadCompIdx(SRWHelper *pHelper, void *target);
int  tsdbLoadCompInfo(SRWHelper *pHelper, void *target);
int  tsdbLoadCompData(SRWHelper *pHelper, SCompBlock *pCompBlock, void *target);
int  tsdbLoadBlockDataCols(SRWHelper *pHelper, SDataCols *pDataCols, int blkIdx, int16_t *colIds, int numOfColIds);
int  tsdbLoadBlockData(SRWHelper *pHelper, SCompBlock *pCompBlock, SDataCols *target);
void tsdbGetDataStatis(SRWHelper *pHelper, SDataStatis *pStatis, int numOfCols);
H
hzcheng 已提交
368 369 370 371 372 373

// --------- For write operations
int tsdbWriteDataBlock(SRWHelper *pHelper, SDataCols *pDataCols);
int tsdbMoveLastBlockIfNeccessary(SRWHelper *pHelper);
int tsdbWriteCompInfo(SRWHelper *pHelper);
int tsdbWriteCompIdx(SRWHelper *pHelper);
H
TD-34  
hzcheng 已提交
374

H
hzcheng 已提交
375
// --------- Other functions need to further organize
H
TD-90  
Hongze Cheng 已提交
376 377 378 379 380
void      tsdbFitRetention(STsdbRepo *pRepo);
int       tsdbAlterCacheTotalBlocks(STsdbRepo *pRepo, int totalBlocks);
void      tsdbAdjustCacheBlocks(STsdbCache *pCache);
int32_t   tsdbGetMetaFileName(char *rootDir, char *fname);
int       tsdbUpdateFileHeader(SFile *pFile, uint32_t version);
H
TD-90  
Hongze Cheng 已提交
381

H
hzcheng 已提交
382

H
Hongze Cheng 已提交
383
int compFGroupKey(const void *key, const void *fgroup);
H
TD-353  
Hongze Cheng 已提交
384
#endif
H
Hongze Cheng 已提交
385

H
more  
hzcheng 已提交
386 387 388 389
#ifdef __cplusplus
}
#endif

H
TD-34  
hzcheng 已提交
390
#endif