wal.h 5.4 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
#include "tarray.h"
L
Liu Jicong 已提交
20 21
#include "tdef.h"
#include "tlog.h"
H
refact  
Hongze Cheng 已提交
22 23 24 25
#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
26 27
extern int32_t wDebugFlag;

L
Liu Jicong 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
#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__); \
    }                                                \
  }

#define WAL_HEAD_VER 0
L
Liu Jicong 已提交
66
#define WAL_NOSUFFIX_LEN 20
L
Liu Jicong 已提交
67 68
#define WAL_SUFFIX_AT (WAL_NOSUFFIX_LEN + 1)
#define WAL_LOG_SUFFIX "log"
L
Liu Jicong 已提交
69
#define WAL_INDEX_SUFFIX "idx"
L
Liu Jicong 已提交
70 71 72 73
#define WAL_REFRESH_MS 1000
#define WAL_MAX_SIZE (TSDB_MAX_WAL_SIZE + sizeof(SWalHead))
#define WAL_PATH_LEN (TSDB_FILENAME_LEN + 12)
#define WAL_FILE_LEN (WAL_PATH_LEN + 32)
L
Liu Jicong 已提交
74

L
Liu Jicong 已提交
75
#define WAL_CUR_FAILED 1
L
Liu Jicong 已提交
76

L
Liu Jicong 已提交
77
#pragma pack(push, 1)
L
Liu Jicong 已提交
78
typedef enum { TAOS_WAL_NOLOG = 0, TAOS_WAL_WRITE = 1, TAOS_WAL_FSYNC = 2 } EWalType;
79

L
Liu Jicong 已提交
80
typedef struct SWalReadHead {
L
Liu Jicong 已提交
81 82 83 84 85 86 87
  int8_t  headVer;
  uint8_t msgType;
  int8_t  reserved[2];
  int32_t len;
  int64_t ingestTs;  // not implemented
  int64_t version;
  char    body[];
L
Liu Jicong 已提交
88 89 90 91 92 93 94
} SWalReadHead;

typedef struct {
  int32_t  vgId;
  int32_t  fsyncPeriod;      // millisecond
  int32_t  retentionPeriod;  // secs
  int32_t  rollPeriod;       // secs
L
Liu Jicong 已提交
95
  int64_t  retentionSize;
L
Liu Jicong 已提交
96
  int64_t  segSize;
L
Liu Jicong 已提交
97
  EWalType level;  // wal level
L
Liu Jicong 已提交
98 99
} SWalCfg;

100
typedef struct {
L
Liu Jicong 已提交
101 102
  uint32_t     cksumHead;
  uint32_t     cksumBody;
L
Liu Jicong 已提交
103
  SWalReadHead head;
104 105
} SWalHead;

L
Liu Jicong 已提交
106 107 108 109 110 111 112 113
typedef struct SWalVer {
  int64_t firstVer;
  int64_t verInSnapshotting;
  int64_t snapshotVer;
  int64_t commitVer;
  int64_t lastVer;
} SWalVer;

L
Liu Jicong 已提交
114
typedef struct SWal {
L
Liu Jicong 已提交
115
  // cfg
L
Liu Jicong 已提交
116
  SWalCfg cfg;
L
Liu Jicong 已提交
117
  int32_t fsyncSeq;
L
Liu Jicong 已提交
118
  // meta
L
Liu Jicong 已提交
119
  SWalVer vers;
L
Liu Jicong 已提交
120 121
  int64_t writeLogTfd;
  int64_t writeIdxTfd;
L
Liu Jicong 已提交
122
  int32_t writeCur;
L
Liu Jicong 已提交
123 124
  SArray *fileInfoSet;
  // status
L
Liu Jicong 已提交
125 126
  int64_t totSize;
  int64_t lastRollSeq;
L
Liu Jicong 已提交
127 128
  // ctl
  int64_t         refId;
L
Liu Jicong 已提交
129
  pthread_mutex_t mutex;
L
Liu Jicong 已提交
130
  // path
L
Liu Jicong 已提交
131
  char path[WAL_PATH_LEN];
L
Liu Jicong 已提交
132
  // reusable write head
L
Liu Jicong 已提交
133
  SWalHead writeHead;
L
Liu Jicong 已提交
134 135
} SWal;  // WAL HANDLE

L
Liu Jicong 已提交
136
typedef struct SWalReadHandle {
L
Liu Jicong 已提交
137 138 139 140 141 142 143 144
  SWal     *pWal;
  int64_t   readLogTfd;
  int64_t   readIdxTfd;
  int64_t   curFileFirstVer;
  int64_t   curVersion;
  int64_t   capacity;
  int64_t   status;  // if cursor valid
  SWalHead *pHead;
L
Liu Jicong 已提交
145
} SWalReadHandle;
L
Liu Jicong 已提交
146
#pragma pack(pop)
L
Liu Jicong 已提交
147

L
Liu Jicong 已提交
148
// typedef int32_t (*FWalWrite)(void *ahandle, void *pHead);
149

S
Shengliang Guan 已提交
150 151 152
// module initialization
int32_t walInit();
void    walCleanUp();
153

S
Shengliang Guan 已提交
154
// handle open and ctl
L
Liu Jicong 已提交
155
SWal   *walOpen(const char *path, SWalCfg *pCfg);
S
Shengliang Guan 已提交
156 157
int32_t walAlter(SWal *, SWalCfg *pCfg);
void    walClose(SWal *);
158

S
Shengliang Guan 已提交
159
// write
L
Liu Jicong 已提交
160
int64_t walWrite(SWal *, int64_t index, uint8_t msgType, const void *body, int32_t bodyLen);
L
Liu Jicong 已提交
161
void    walFsync(SWal *, bool force);
L
Liu Jicong 已提交
162

S
Shengliang Guan 已提交
163 164 165 166
// apis for lifecycle management
int32_t walCommit(SWal *, int64_t ver);
// truncate after
int32_t walRollback(SWal *, int64_t ver);
L
Liu Jicong 已提交
167
// notify that previous logs can be pruned safely
L
Liu Jicong 已提交
168 169
int32_t walBeginSnapshot(SWal *, int64_t ver);
int32_t walEndSnapshot(SWal *);
L
Liu Jicong 已提交
170
// int32_t  walDataCorrupted(SWal*);
171

S
Shengliang Guan 已提交
172
// read
L
Liu Jicong 已提交
173 174 175
SWalReadHandle *walOpenReadHandle(SWal *);
void            walCloseReadHandle(SWalReadHandle *);
int32_t         walReadWithHandle(SWalReadHandle *pRead, int64_t ver);
L
Liu Jicong 已提交
176

L
Liu Jicong 已提交
177 178
// deprecated
#if 0
S
Shengliang Guan 已提交
179
int32_t walRead(SWal *, SWalHead **, int64_t ver);
L
Liu Jicong 已提交
180 181
int32_t walReadWithFp(SWal *, FWalWrite writeFp, int64_t verStart, int32_t readNum);
#endif
182

S
Shengliang Guan 已提交
183
// lifecycle check
L
Liu Jicong 已提交
184 185 186
int64_t walGetFirstVer(SWal *);
int64_t walGetSnapshotVer(SWal *);
int64_t walGetLastVer(SWal *);
187

H
refact  
Hongze Cheng 已提交
188 189 190 191
#ifdef __cplusplus
}
#endif

192
#endif  // _TD_WAL_H_