wal.h 4.6 KB
Newer Older
H
refact  
Hongze Cheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * 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_WAL_H_
#define _TD_WAL_H_

L
Liu Jicong 已提交
18
#include "os.h"
L
Liu Jicong 已提交
19 20
#include "tdef.h"
#include "tlog.h"
L
Liu Jicong 已提交
21
#include "tarray.h"
H
refact  
Hongze Cheng 已提交
22 23 24 25
#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
26 27 28 29 30 31 32 33 34
extern int32_t wDebugFlag;

#define wFatal(...) { if (wDebugFlag & DEBUG_FATAL) { taosPrintLog("WAL FATAL ", 255, __VA_ARGS__); }}
#define wError(...) { if (wDebugFlag & DEBUG_ERROR) { taosPrintLog("WAL ERROR ", 255, __VA_ARGS__); }}
#define wWarn(...)  { if (wDebugFlag & DEBUG_WARN)  { taosPrintLog("WAL WARN ", 255, __VA_ARGS__); }}
#define wInfo(...)  { if (wDebugFlag & DEBUG_INFO)  { taosPrintLog("WAL ", 255, __VA_ARGS__); }}
#define wDebug(...) { if (wDebugFlag & DEBUG_DEBUG) { taosPrintLog("WAL ", wDebugFlag, __VA_ARGS__); }}
#define wTrace(...) { if (wDebugFlag & DEBUG_TRACE) { taosPrintLog("WAL ", wDebugFlag, __VA_ARGS__); }}

35 36
typedef enum {
  TAOS_WAL_NOLOG = 0,
L
Liu Jicong 已提交
37 38
  TAOS_WAL_WRITE = 1,
  TAOS_WAL_FSYNC = 2
39 40
} EWalType;

L
Liu Jicong 已提交
41 42 43 44 45 46 47 48 49 50 51 52 53 54
typedef struct SWalReadHead {
  int8_t   sver;
  uint8_t  msgType;
  int8_t   reserved[2];
  int32_t  len;
  int64_t  version;
  char     cont[];
} SWalReadHead;

typedef struct {
  int32_t  vgId;
  int32_t  fsyncPeriod;      // millisecond
  int32_t  retentionPeriod;  // secs
  int32_t  rollPeriod;       // secs
L
Liu Jicong 已提交
55
  int32_t  retentionSize;       // secs
L
Liu Jicong 已提交
56 57 58 59
  int64_t  segSize;
  EWalType walLevel;         // wal level
} SWalCfg;

60
typedef struct {
L
Liu Jicong 已提交
61 62 63 64 65 66 67 68
  //union {
    //uint32_t info;
    //struct {
      //uint32_t sver:3;
      //uint32_t msgtype: 5;
      //uint32_t reserved : 24;
    //};
  //};
L
Liu Jicong 已提交
69 70
  uint32_t cksumHead;
  uint32_t cksumBody;
L
Liu Jicong 已提交
71
  SWalReadHead head;
72 73
} SWalHead;

L
Liu Jicong 已提交
74
#define WAL_PREFIX       "wal"
L
Liu Jicong 已提交
75 76 77
#define WAL_PREFIX_LEN   3
#define WAL_NOSUFFIX_LEN 20
#define WAL_SUFFIX_AT    (WAL_NOSUFFIX_LEN+1)
L
Liu Jicong 已提交
78 79 80 81 82 83 84 85
#define WAL_LOG_SUFFIX   "log"
#define WAL_INDEX_SUFFIX "idx"
#define WAL_REFRESH_MS   1000
#define WAL_MAX_SIZE     (TSDB_MAX_WAL_SIZE + sizeof(SWalHead) + 16)
#define WAL_SIGNATURE    ((uint32_t)(0xFAFBFDFEUL))
#define WAL_PATH_LEN     (TSDB_FILENAME_LEN + 12)
#define WAL_FILE_LEN     (WAL_PATH_LEN + 32)
//#define WAL_FILE_NUM     1 // 3
L
Liu Jicong 已提交
86
#define WAL_FILESET_MAX  128
L
Liu Jicong 已提交
87

L
Liu Jicong 已提交
88
#define WAL_IDX_ENTRY_SIZE    (sizeof(int64_t)*2)
L
Liu Jicong 已提交
89 90 91
#define WAL_CUR_POS_WRITABLE  1
#define WAL_CUR_FILE_WRITABLE 2
#define WAL_CUR_FAILED        4
L
Liu Jicong 已提交
92

L
Liu Jicong 已提交
93
typedef struct SWal {
L
Liu Jicong 已提交
94 95 96
  // cfg
  int32_t  vgId;
  int32_t  fsyncPeriod;  // millisecond
L
Liu Jicong 已提交
97 98
  int32_t  rollPeriod;  // second
  int64_t  segSize;
L
Liu Jicong 已提交
99 100
  int64_t  retentionSize;
  int32_t  retentionPeriod;
L
Liu Jicong 已提交
101
  EWalType level;
L
Liu Jicong 已提交
102 103 104 105
  //total size
  int64_t  totSize;
  //fsync seq
  int32_t  fsyncSeq;
L
Liu Jicong 已提交
106 107
  //reference
  int64_t refId;
L
Liu Jicong 已提交
108 109 110
  //write tfd
  int64_t writeLogTfd;
  int64_t writeIdxTfd;
L
Liu Jicong 已提交
111
  //wal lifecycle
L
Liu Jicong 已提交
112 113
  int64_t firstVersion;
  int64_t snapshotVersion;
L
Liu Jicong 已提交
114
  int64_t commitVersion;
L
Liu Jicong 已提交
115
  int64_t lastVersion;
L
Liu Jicong 已提交
116 117
  //snapshotting version
  int64_t snapshottingVer;
L
Liu Jicong 已提交
118 119
  //roll status
  int64_t lastRollSeq;
L
Liu Jicong 已提交
120
  //file set
L
Liu Jicong 已提交
121
  int32_t writeCur;
L
Liu Jicong 已提交
122
  SArray* fileInfoSet;
L
Liu Jicong 已提交
123 124
  //ctl
  int32_t curStatus;
L
Liu Jicong 已提交
125
  pthread_mutex_t mutex;
L
Liu Jicong 已提交
126 127
  //path
  char path[WAL_PATH_LEN];
L
Liu Jicong 已提交
128 129
  //reusable write head
  SWalHead head;
L
Liu Jicong 已提交
130 131
} SWal;  // WAL HANDLE

L
Liu Jicong 已提交
132
typedef int32_t (*FWalWrite)(void *ahandle, void *pHead);
133

S
Shengliang Guan 已提交
134 135 136
// module initialization
int32_t walInit();
void    walCleanUp();
137

S
Shengliang Guan 已提交
138
// handle open and ctl
L
Liu Jicong 已提交
139
SWal   *walOpen(const char *path, SWalCfg *pCfg);
S
Shengliang Guan 已提交
140 141
int32_t walAlter(SWal *, SWalCfg *pCfg);
void    walClose(SWal *);
142

S
Shengliang Guan 已提交
143
// write
L
Liu Jicong 已提交
144
int64_t walWrite(SWal *, int64_t index, uint8_t msgType, const void *body, int32_t bodyLen);
L
Liu Jicong 已提交
145
void    walFsync(SWal *, bool force);
L
Liu Jicong 已提交
146

S
Shengliang Guan 已提交
147 148 149 150
// apis for lifecycle management
int32_t walCommit(SWal *, int64_t ver);
// truncate after
int32_t walRollback(SWal *, int64_t ver);
L
Liu Jicong 已提交
151
// notify that previous logs can be pruned safely
L
Liu Jicong 已提交
152 153
int32_t walBeginTakeSnapshot(SWal *, int64_t ver);
int32_t walEndTakeSnapshot(SWal *);
L
Liu Jicong 已提交
154
//int32_t  walDataCorrupted(SWal*);
155

S
Shengliang Guan 已提交
156 157 158
// read
int32_t walRead(SWal *, SWalHead **, int64_t ver);
int32_t walReadWithFp(SWal *, FWalWrite writeFp, int64_t verStart, int32_t readNum);
159

S
Shengliang Guan 已提交
160
// lifecycle check
L
Liu Jicong 已提交
161 162 163
int64_t walGetFirstVer(SWal *);
int64_t walGetSnapshotVer(SWal *);
int64_t walGetLastVer(SWal *);
164

H
refact  
Hongze Cheng 已提交
165 166 167 168
#ifdef __cplusplus
}
#endif

169
#endif  // _TD_WAL_H_