tsdb.h 13.3 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
hzcheng 已提交
15
#ifndef _TD_TSDB_H_
H
Hongze Cheng 已提交
16 17 18 19 20 21
#define _TD_TSDB_H_

#include <pthread.h>
#include <stdbool.h>
#include <stdint.h>

H
hzcheng 已提交
22
#include "taosdef.h"
H
hzcheng 已提交
23
#include "taosmsg.h"
H
hjxilinx 已提交
24
#include "tarray.h"
H
Hongze Cheng 已提交
25 26
#include "tdataformat.h"
#include "tname.h"
H
Haojun Liao 已提交
27
#include "hash.h"
28 29
#include "tlockfree.h"
#include "tlist.h"
H
Hongze Cheng 已提交
30

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

H
Hongze Cheng 已提交
35 36 37
#define TSDB_VERSION_MAJOR 1
#define TSDB_VERSION_MINOR 0

H
hzcheng 已提交
38 39
#define TSDB_INVALID_SUPER_TABLE_ID -1

40
#define TSDB_STATUS_COMMIT_START 1
H
Hongze Cheng 已提交
41
#define TSDB_STATUS_COMMIT_OVER 2
42
#define TSDB_STATUS_COMMIT_NOBLOCK 3 //commit no block, need to be solved 
43

H
TD-1027  
Hongze Cheng 已提交
44 45
// TSDB STATE DEFINITION
#define TSDB_STATE_OK 0x0
H
Hongze Cheng 已提交
46 47
#define TSDB_STATE_BAD_META 0x1
#define TSDB_STATE_BAD_DATA 0x2
H
TD-1027  
Hongze Cheng 已提交
48

H
hzcheng 已提交
49 50 51
// --------- TSDB APPLICATION HANDLE DEFINITION
typedef struct {
  void *appH;
J
jtao1735 已提交
52
  void *cqH;
53
  int (*notifyStatus)(void *, int status, int eno);
H
hzcheng 已提交
54
  int (*eventCallBack)(void *);
R
fix bug  
root 已提交
55
  void *(*cqCreateFunc)(void *handle, uint64_t uid, int32_t sid, const char *dstTable, char *sqlStr, STSchema *pSchema, int start);
H
TD-354  
Hongze Cheng 已提交
56
  void (*cqDropFunc)(void *handle);
H
hzcheng 已提交
57 58
} STsdbAppH;

H
hzcheng 已提交
59 60 61
// --------- TSDB REPOSITORY CONFIGURATION DEFINITION
typedef struct {
  int32_t tsdbId;
S
slguan 已提交
62 63
  int32_t cacheBlockSize;
  int32_t totalBlocks;
H
Hongze Cheng 已提交
64 65
  int32_t daysPerFile;  // day per file sharding policy
  int32_t keep;         // day of data to keep
S
slguan 已提交
66 67
  int32_t keep1;
  int32_t keep2;
H
hzcheng 已提交
68 69
  int32_t minRowsPerFileBlock;  // minimum rows per file block
  int32_t maxRowsPerFileBlock;  // maximum rows per file block
S
slguan 已提交
70 71
  int8_t  precision;
  int8_t  compression;
H
TD-1438  
Hongze Cheng 已提交
72
  int8_t  update;
L
lichuang 已提交
73
  int8_t  cacheLastRow;    // 0:no cache, 1: cache last row, 2: cache last NULL column 3: 1&2
H
hzcheng 已提交
74 75
} STsdbCfg;

L
lichuang 已提交
76
#define CACHE_NO_LAST(c)          ((c)->cacheLastRow == 0)
77 78
#define CACHE_LAST_ROW(c)         (((c)->cacheLastRow & 1) > 0)
#define CACHE_LAST_NULL_COLUMN(c) (((c)->cacheLastRow & 2) > 0)
L
lichuang 已提交
79

T
Tao Liu 已提交
80 81
// --------- TSDB REPOSITORY USAGE STATISTICS
typedef struct {
82 83 84 85
  int64_t totalStorage;  // total bytes occupie
  int64_t compStorage;
  int64_t pointsWritten;  // total data points written
} STsdbStat;
T
Tao Liu 已提交
86

H
Hongze Cheng 已提交
87
typedef struct STsdbRepo STsdbRepo;
88

H
Hongze Cheng 已提交
89
STsdbCfg *tsdbGetCfg(const STsdbRepo *repo);
H
hzcheng 已提交
90 91

// --------- TSDB REPOSITORY DEFINITION
H
Hongze Cheng 已提交
92 93 94 95 96 97
int32_t    tsdbCreateRepo(int repoid);
int32_t    tsdbDropRepo(int repoid);
STsdbRepo *tsdbOpenRepo(STsdbCfg *pCfg, STsdbAppH *pAppH);
int        tsdbCloseRepo(STsdbRepo *repo, int toCommit);
int32_t    tsdbConfigRepo(STsdbRepo *repo, STsdbCfg *pCfg);
int        tsdbGetState(STsdbRepo *repo);
98
int8_t     tsdbGetCompactState(STsdbRepo *repo);
H
hzcheng 已提交
99
// --------- TSDB TABLE DEFINITION
H
more  
hzcheng 已提交
100
typedef struct {
101 102
  uint64_t uid;  // the unique table ID
  int32_t  tid;  // the table ID in the repository.
H
more  
hzcheng 已提交
103 104
} STableId;

H
hzcheng 已提交
105 106
// --------- TSDB TABLE configuration
typedef struct {
H
hzcheng 已提交
107 108 109 110 111
  ETableType type;
  char *     name;
  STableId   tableId;
  int32_t    sversion;
  char *     sname;  // super table name
112
  uint64_t   superUid;
H
hzcheng 已提交
113 114
  STSchema * schema;
  STSchema * tagSchema;
115
  SKVRow     tagValues;
H
TD-354  
Hongze Cheng 已提交
116
  char *     sql;
H
hzcheng 已提交
117 118 119 120
} STableCfg;

void tsdbClearTableCfg(STableCfg *config);

wmmhello's avatar
wmmhello 已提交
121
void *tsdbGetTableTagVal(const void *pTable, int32_t colId, int16_t type);
H
Hongze Cheng 已提交
122
char *tsdbGetTableName(void *pTable);
H
Haojun Liao 已提交
123 124

#define TSDB_TABLEID(_table) ((STableId*) (_table))
125 126
#define TSDB_PREV_ROW  0x1
#define TSDB_NEXT_ROW  0x2
H
Haojun Liao 已提交
127

H
Hongze Cheng 已提交
128
STableCfg *tsdbCreateTableCfgFromMsg(SMDCreateTableMsg *pMsg);
129

H
Hongze Cheng 已提交
130 131 132
int tsdbCreateTable(STsdbRepo *repo, STableCfg *pCfg);
int tsdbDropTable(STsdbRepo *pRepo, STableId tableId);
int tsdbUpdateTableTagValue(STsdbRepo *repo, SUpdateTableTagValMsg *pMsg);
H
hzcheng 已提交
133

H
Hongze Cheng 已提交
134
uint32_t tsdbGetFileInfo(STsdbRepo *repo, char *name, uint32_t *index, uint32_t eindex, int64_t *size);
135

H
Hongze Cheng 已提交
136
// the TSDB repository info
H
more  
hzcheng 已提交
137
typedef struct STsdbRepoInfo {
H
more  
hzcheng 已提交
138
  STsdbCfg tsdbCfg;
S
TD-1919  
Shengliang Guan 已提交
139
  uint64_t version;            // version of the repository
H
Hongze Cheng 已提交
140 141 142
  int64_t  tsdbTotalDataSize;  // the original inserted data size
  int64_t  tsdbTotalDiskSize;  // the total disk size taken by this TSDB repository
  // TODO: Other informations to add
H
more  
hzcheng 已提交
143
} STsdbRepoInfo;
H
Hongze Cheng 已提交
144
STsdbRepoInfo *tsdbGetStatus(STsdbRepo *pRepo);
H
hzcheng 已提交
145

H
Hongze Cheng 已提交
146 147 148
// the meter information report structure
typedef struct {
  STableCfg tableCfg;
S
TD-1919  
Shengliang Guan 已提交
149
  uint64_t  version;
H
more  
hzcheng 已提交
150 151
  int64_t   tableTotalDataSize;  // In bytes
  int64_t   tableTotalDiskSize;  // In bytes
H
Hongze Cheng 已提交
152 153 154 155 156 157 158 159 160 161
} STableInfo;

// -- FOR INSERT DATA
/**
 * Insert data to a table in a repository
 * @param pRepo the TSDB repository handle
 * @param pData the data to insert (will give a more specific description)
 *
 * @return the number of points inserted, -1 for failure and the error number is set
 */
H
Hongze Cheng 已提交
162
int32_t tsdbInsertData(STsdbRepo *repo, SSubmitMsg *pMsg, SShellSubmitRspMsg *pRsp);
H
Hongze Cheng 已提交
163 164 165

// -- FOR QUERY TIME SERIES DATA

H
hzcheng 已提交
166
typedef void *TsdbQueryHandleT;  // Use void to hide implementation details
H
Hongze Cheng 已提交
167

H
Haojun Liao 已提交
168 169 170 171 172
#define BLOCK_LOAD_OFFSET_SEQ_ORDER   1
#define BLOCK_LOAD_TABLE_SEQ_ORDER    2
#define BLOCK_LOAD_TABLE_RR_ORDER     3

// query condition to build multi-table data block iterator
H
hjxilinx 已提交
173
typedef struct STsdbQueryCond {
H
Hongze Cheng 已提交
174
  STimeWindow  twindow;
175
  int32_t      order;             // desc|asc order to iterate the data block
176
  int64_t      offset;            // skip offset put down to tsdb
H
Hongze Cheng 已提交
177 178
  int32_t      numOfCols;
  SColumnInfo *colList;
179
  bool         loadExternalRows;  // load external rows or not
H
Haojun Liao 已提交
180
  int32_t      type;              // data block load type:
H
hjxilinx 已提交
181
} STsdbQueryCond;
H
Hongze Cheng 已提交
182

183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
typedef struct STableData STableData;
typedef struct {
  T_REF_DECLARE()
  SRWLatch     latch;
  TSKEY        keyFirst;
  TSKEY        keyLast;
  int64_t      numOfRows;
  int32_t      maxTables;
  STableData **tData;
  SList *      actList;
  SList *      extraBuffList;
  SList *      bufBlockList;
  int64_t      pointsAdd;   // TODO
  int64_t      storageAdd;  // TODO
} SMemTable;

typedef struct {
  SMemTable* mem;
  SMemTable* imem;
  SMemTable  mtable;
  SMemTable* omem;
} SMemSnapshot;

H
Haojun Liao 已提交
206
typedef struct SMemRef {
207 208
  int32_t      ref;
  SMemSnapshot snapshot;
H
Haojun Liao 已提交
209 210
} SMemRef;

H
hjxilinx 已提交
211 212
typedef struct SDataBlockInfo {
  STimeWindow window;
213
  int32_t     rows;
H
hjxilinx 已提交
214 215
  int32_t     numOfCols;
  int64_t     uid;
H
hjxilinx 已提交
216
  int32_t     tid;
H
hjxilinx 已提交
217
} SDataBlockInfo;
H
Hongze Cheng 已提交
218

H
Haojun Liao 已提交
219
typedef struct SFileBlockInfo {
220
  int32_t numBlocksOfStep;
H
Haojun Liao 已提交
221 222
} SFileBlockInfo;

223
typedef struct {
H
Hongze Cheng 已提交
224 225
  void *pTable;
  TSKEY lastKey;
H
Haojun Liao 已提交
226 227 228
} STableKeyInfo;

typedef struct {
H
Haojun Liao 已提交
229
  uint32_t  numOfTables;
230
  SArray   *pGroupList;
H
Hongze Cheng 已提交
231
  SHashObj *map;  // speedup acquire the tableQueryInfo by table uid
232 233
  int32_t sVersion;
  int32_t tVersion;
234 235
} STableGroupInfo;

236
#define TSDB_BLOCK_DIST_STEP_ROWS 16
H
Haojun Liao 已提交
237 238 239 240 241
typedef struct {
  uint16_t  rowSize;
  uint16_t  numOfFiles;
  uint32_t  numOfTables;
  uint64_t  totalSize;
242 243 244
  uint64_t  totalRows;
  int32_t   maxRows;
  int32_t   minRows;
H
Haojun Liao 已提交
245 246
  int32_t   firstSeekTimeUs;
  uint32_t  numOfRowsInMemTable;
247
  uint32_t  numOfSmallBlocks;
H
Haojun Liao 已提交
248 249 250
  SArray   *dataBlockInfos;
} STableBlockDist;

H
Hongze Cheng 已提交
251 252
/**
 * Get the data block iterator, starting from position according to the query condition
H
hjxilinx 已提交
253 254 255
 *
 * @param tsdb       tsdb handle
 * @param pCond      query condition, including time window, result set order, and basic required columns for each block
H
Haojun Liao 已提交
256 257
 * @param tableInfoGroup  table object list in the form of set, grouped into different sets according to the
 *                        group by condition
H
Haojun Liao 已提交
258
 * @param qinfo      query info handle from query processor
H
Hongze Cheng 已提交
259 260
 * @return
 */
H
Haojun Liao 已提交
261
TsdbQueryHandleT *tsdbQueryTables(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfoGroup, uint64_t qId,
H
Hongze Cheng 已提交
262
                                  SMemRef *pRef);
H
Hongze Cheng 已提交
263 264

/**
H
hjxilinx 已提交
265 266 267 268
 * Get the last row of the given query time window for all the tables in STableGroupInfo object.
 * Note that only one data block with only row will be returned while invoking retrieve data block function for
 * all tables in this group.
 *
H
Haojun Liao 已提交
269 270 271
 * @param tsdb   tsdb handle
 * @param pCond  query condition, including time window, result set order, and basic required columns for each block
 * @param tableInfo  table list.
H
hjxilinx 已提交
272 273
 * @return
 */
H
Haojun Liao 已提交
274
TsdbQueryHandleT tsdbQueryLastRow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *tableInfo, uint64_t qId,
H
Hongze Cheng 已提交
275
                                  SMemRef *pRef);
H
hjxilinx 已提交
276

D
fix bug  
dapan1121 已提交
277 278 279

TsdbQueryHandleT tsdbQueryCacheLast(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList, uint64_t qId, SMemRef* pMemRef);

D
fix bug  
dapan1121 已提交
280 281
bool isTsdbCacheLastRow(TsdbQueryHandleT* pQueryHandle);

D
fix bug  
dapan1121 已提交
282

283 284 285 286 287
/**
 * get the queried table object list
 * @param pHandle
 * @return
 */
H
Hongze Cheng 已提交
288
SArray *tsdbGetQueriedTableList(TsdbQueryHandleT *pHandle);
289

290 291 292 293 294 295 296 297
/**
 * get the group list according to table id from client
 * @param tsdb
 * @param pCond
 * @param groupList
 * @param qinfo
 * @return
 */
H
Hongze Cheng 已提交
298
TsdbQueryHandleT tsdbQueryRowsInExternalWindow(STsdbRepo *tsdb, STsdbQueryCond *pCond, STableGroupInfo *groupList,
H
Haojun Liao 已提交
299
                                               uint64_t qId, SMemRef *pRef);
300

Y
yihaoDeng 已提交
301 302 303 304 305 306 307 308 309 310

/**
 * get num of rows in mem table 
 *
 * @param pHandle
 * @return row size
 */

int64_t tsdbGetNumOfRowsInMemTable(TsdbQueryHandleT* pHandle);

H
hjxilinx 已提交
311
/**
Y
yihaoDeng 已提交
312
 * move to next block if exists 
H
hjxilinx 已提交
313
 *
H
hjxilinx 已提交
314
 * @param pQueryHandle
H
Hongze Cheng 已提交
315 316
 * @return
 */
H
Haojun Liao 已提交
317
bool tsdbNextDataBlock(TsdbQueryHandleT pQueryHandle);
H
Hongze Cheng 已提交
318 319

/**
H
hjxilinx 已提交
320
 * Get current data block information
H
Hongze Cheng 已提交
321 322
 *
 * @param pQueryHandle
H
Haojun Liao 已提交
323
 * @param pBlockInfo
H
Hongze Cheng 已提交
324 325
 * @return
 */
H
Hongze Cheng 已提交
326
void tsdbRetrieveDataBlockInfo(TsdbQueryHandleT *pQueryHandle, SDataBlockInfo *pBlockInfo);
H
Hongze Cheng 已提交
327 328

/**
H
hjxilinx 已提交
329 330 331 332 333 334 335
 *
 * Get the pre-calculated information w.r.t. current data block.
 *
 * In case of data block in cache, the pBlockStatis will always be NULL.
 * If a block is not completed loaded from disk, the pBlockStatis will be NULL.

 * @pBlockStatis the pre-calculated value for current data blocks. if the block is a cache block, always return 0
H
Hongze Cheng 已提交
336 337
 * @return
 */
H
hzcheng 已提交
338
int32_t tsdbRetrieveDataBlockStatisInfo(TsdbQueryHandleT *pQueryHandle, SDataStatis **pBlockStatis);
H
Hongze Cheng 已提交
339 340

/**
H
hjxilinx 已提交
341
 *
H
hjxilinx 已提交
342 343 344
 * The query condition with primary timestamp is passed to iterator during its constructor function,
 * the returned data block must be satisfied with the time window condition in any cases,
 * which means the SData data block is not actually the completed disk data blocks.
H
Hongze Cheng 已提交
345
 *
H
hjxilinx 已提交
346 347
 * @param pQueryHandle      query handle
 * @param pColumnIdList     required data columns id list
H
Hongze Cheng 已提交
348 349
 * @return
 */
H
hjxilinx 已提交
350
SArray *tsdbRetrieveDataBlock(TsdbQueryHandleT *pQueryHandle, SArray *pColumnIdList);
H
Hongze Cheng 已提交
351 352

/**
353
 * Get the qualified table id for a super table according to the tag query expression.
H
Hongze Cheng 已提交
354 355 356
 * @param stableid. super table sid
 * @param pTagCond. tag query condition
 */
H
Hongze Cheng 已提交
357
int32_t tsdbQuerySTableByTagCond(STsdbRepo *tsdb, uint64_t uid, TSKEY key, const char *pTagCond, size_t len,
W
wpan 已提交
358
                                 STableGroupInfo *pGroupList, SColIndex *pColIndex, int32_t numOfCols);
H
hjxilinx 已提交
359

360
/**
H
Haojun Liao 已提交
361
 * destroy the created table group list, which is generated by tag query
362 363
 * @param pGroupList
 */
H
Haojun Liao 已提交
364
void tsdbDestroyTableGroup(STableGroupInfo *pGroupList);
365

H
hjxilinx 已提交
366 367 368 369 370 371 372 373
/**
 * create the table group result including only one table, used to handle the normal table query
 *
 * @param tsdb        tsdbHandle
 * @param uid         table uid
 * @param pGroupInfo  the generated result
 * @return
 */
H
Hongze Cheng 已提交
374
int32_t tsdbGetOneTableGroup(STsdbRepo *tsdb, uint64_t uid, TSKEY startKey, STableGroupInfo *pGroupInfo);
H
Hongze Cheng 已提交
375

376 377 378 379 380 381 382
/**
 *
 * @param tsdb
 * @param pTableIdList
 * @param pGroupInfo
 * @return
 */
H
Hongze Cheng 已提交
383
int32_t tsdbGetTableGroupFromIdList(STsdbRepo *tsdb, SArray *pTableIdList, STableGroupInfo *pGroupInfo);
384

385 386 387 388
/**
 * clean up the query handle
 * @param queryHandle
 */
H
hzcheng 已提交
389
void tsdbCleanupQueryHandle(TsdbQueryHandleT queryHandle);
390

H
Haojun Liao 已提交
391 392
void tsdbResetQueryHandle(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond);

H
Haojun Liao 已提交
393 394
void tsdbResetQueryHandleForNewTable(TsdbQueryHandleT queryHandle, STsdbQueryCond *pCond, STableGroupInfo* groupList);

H
Haojun Liao 已提交
395
int32_t tsdbGetFileBlocksDistInfo(TsdbQueryHandleT* queryHandle, STableBlockDist* pTableBlockInfo);
H
Haojun Liao 已提交
396

397 398 399
// obtain queryHandle attribute
int64_t tsdbSkipOffset(TsdbQueryHandleT queryHandle);

T
Tao Liu 已提交
400 401 402 403 404 405 406 407 408
/**
 * get the statistics of repo usage
 * @param repo. point to the tsdbrepo
 * @param totalPoints. total data point written
 * @param totalStorage. total bytes took by the tsdb
 * @param compStorage. total bytes took by the tsdb after compressed
 */
void tsdbReportStat(void *repo, int64_t *totalPoints, int64_t *totalStorage, int64_t *compStorage);

409
int  tsdbInitCommitQueue();
H
Hongze Cheng 已提交
410
void tsdbDestroyCommitQueue();
H
Hongze Cheng 已提交
411
int  tsdbSyncCommit(STsdbRepo *repo);
S
TD-2072  
Shengliang Guan 已提交
412
void tsdbIncCommitRef(int vgId);
S
TD-2072  
Shengliang Guan 已提交
413
void tsdbDecCommitRef(int vgId);
D
dapan1121 已提交
414
void tsdbSwitchTable(TsdbQueryHandleT pQueryHandle);
H
Hongze Cheng 已提交
415

H
Hongze Cheng 已提交
416
// For TSDB file sync
S
TD-1207  
Shengliang Guan 已提交
417 418
int tsdbSyncSend(void *pRepo, SOCKET socketFd);
int tsdbSyncRecv(void *pRepo, SOCKET socketFd);
H
Hongze Cheng 已提交
419

H
Hongze Cheng 已提交
420 421 422
// For TSDB Compact
int tsdbCompact(STsdbRepo *pRepo);

423
// For TSDB Health Monitor
A
AlexDuan 已提交
424 425 426

// no problem return true
bool tsdbNoProblem(STsdbRepo* pRepo);
C
Cary Xu 已提交
427 428
// unit of walSize: MB
int tsdbCheckWal(STsdbRepo *pRepo, uint32_t walSize);
429

wmmhello's avatar
wmmhello 已提交
430 431 432 433 434
// for json tag
void* getJsonTagValueElment(void* data, char* key, int32_t keyLen, char* out, int16_t bytes);
void getJsonTagValueAll(void* data, void* dst, int16_t bytes);
char* parseTagDatatoJson(void *p);

435 436 437 438 439 440 441 442 443 444
//
// scan callback 
//

// type define
#define READ_TABLE    1
#define READ_QUERY    2
typedef bool (*readover_callback)(void* param, int8_t type, int32_t tid);
void tsdbAddScanCallback(TsdbQueryHandleT* queryHandle, readover_callback callback, void* param);

H
more  
hzcheng 已提交
445 446 447 448
#ifdef __cplusplus
}
#endif

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
449
#endif  // _TD_TSDB_H_