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

L
Liu Jicong 已提交
19
#include "compare.h"
L
Liu Jicong 已提交
20
#include "tchecksum.h"
L
Liu Jicong 已提交
21
#include "wal.h"
L
Liu Jicong 已提交
22

H
refact  
Hongze Cheng 已提交
23 24 25 26
#ifdef __cplusplus
extern "C" {
#endif

L
Liu Jicong 已提交
27
// meta section begin
L
Liu Jicong 已提交
28 29 30 31 32 33
typedef struct WalFileInfo {
  int64_t firstVer;
  int64_t lastVer;
  int64_t createTs;
  int64_t closeTs;
  int64_t fileSize;
L
Liu Jicong 已提交
34
} SWalFileInfo;
L
Liu Jicong 已提交
35

L
Liu Jicong 已提交
36 37 38
typedef struct WalIdxEntry {
  int64_t ver;
  int64_t offset;
L
Liu Jicong 已提交
39
} SWalIdxEntry;
L
Liu Jicong 已提交
40

L
Liu Jicong 已提交
41
static inline int32_t compareWalFileInfo(const void* pLeft, const void* pRight) {
L
Liu Jicong 已提交
42 43
  SWalFileInfo* pInfoLeft = (SWalFileInfo*)pLeft;
  SWalFileInfo* pInfoRight = (SWalFileInfo*)pRight;
L
Liu Jicong 已提交
44 45 46 47
  return compareInt64Val(&pInfoLeft->firstVer, &pInfoRight->firstVer);
}

static inline int64_t walGetLastFileSize(SWal* pWal) {
L
Liu Jicong 已提交
48
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
49 50 51 52
  return pInfo->fileSize;
}

static inline int64_t walGetLastFileFirstVer(SWal* pWal) {
L
Liu Jicong 已提交
53
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
L
Liu Jicong 已提交
54 55 56 57
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileFirstVer(SWal* pWal) {
L
Liu Jicong 已提交
58
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
59 60 61 62
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileLastVer(SWal* pWal) {
L
Liu Jicong 已提交
63
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
64 65 66 67
  return pInfo->firstVer;
}

static inline int64_t walGetCurFileOffset(SWal* pWal) {
L
Liu Jicong 已提交
68
  SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
69 70 71
  return pInfo->fileSize;
}

L
Liu Jicong 已提交
72
static inline bool walCurFileClosed(SWal* pWal) { return taosArrayGetSize(pWal->fileInfoSet) != pWal->writeCur; }
L
Liu Jicong 已提交
73

L
Liu Jicong 已提交
74 75
static inline SWalFileInfo* walGetCurFileInfo(SWal* pWal) {
  return (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
L
Liu Jicong 已提交
76 77
}

L
Liu Jicong 已提交
78
static inline int walBuildLogName(SWal* pWal, int64_t fileFirstVer, char* buf) {
L
Liu Jicong 已提交
79
  return sprintf(buf, "%s/%020" PRId64 "." WAL_LOG_SUFFIX, pWal->path, fileFirstVer);
L
Liu Jicong 已提交
80 81
}

L
Liu Jicong 已提交
82
static inline int walBuildIdxName(SWal* pWal, int64_t fileFirstVer, char* buf) {
L
Liu Jicong 已提交
83
  return sprintf(buf, "%s/%020" PRId64 "." WAL_INDEX_SUFFIX, pWal->path, fileFirstVer);
L
Liu Jicong 已提交
84 85
}

L
Liu Jicong 已提交
86 87 88 89 90
static inline int walValidHeadCksum(SWalHead* pHead) {
  return taosCheckChecksum((uint8_t*)&pHead->head, sizeof(SWalReadHead), pHead->cksumHead);
}

static inline int walValidBodyCksum(SWalHead* pHead) {
L
Liu Jicong 已提交
91
  return taosCheckChecksum((uint8_t*)pHead->head.body, pHead->head.len, pHead->cksumBody);
L
Liu Jicong 已提交
92 93
}

L
Liu Jicong 已提交
94
static inline int walValidCksum(SWalHead* pHead, void* body, int64_t bodyLen) {
L
Liu Jicong 已提交
95 96 97
  return walValidHeadCksum(pHead) && walValidBodyCksum(pHead);
}

L
Liu Jicong 已提交
98
static inline uint32_t walCalcHeadCksum(SWalHead* pHead) {
L
Liu Jicong 已提交
99 100 101 102 103 104 105
  return taosCalcChecksum(0, (uint8_t*)&pHead->head, sizeof(SWalReadHead));
}

static inline uint32_t walCalcBodyCksum(const void* body, uint32_t len) {
  return taosCalcChecksum(0, (uint8_t*)body, len);
}

L
Liu Jicong 已提交
106
static inline int64_t walGetVerIdxOffset(SWal* pWal, int64_t ver) {
L
Liu Jicong 已提交
107
  return (ver - walGetCurFileFirstVer(pWal)) * sizeof(SWalIdxEntry);
L
Liu Jicong 已提交
108 109
}

L
Liu Jicong 已提交
110 111 112 113 114 115 116 117 118 119
static inline void walResetVer(SWalVer* pVer) {
  pVer->firstVer = -1;
  pVer->verInSnapshotting = -1;
  pVer->snapshotVer = -1;
  pVer->commitVer = -1;
  pVer->lastVer = -1;
}

int walLoadMeta(SWal* pWal);
int walSaveMeta(SWal* pWal);
L
Liu Jicong 已提交
120 121
int walRollFileInfo(SWal* pWal);

L
Liu Jicong 已提交
122 123 124 125
int walCheckAndRepairMeta(SWal* pWal);

int walCheckAndRepairIdx(SWal* pWal);

126
char* walMetaSerialize(SWal* pWal);
L
Liu Jicong 已提交
127 128
int   walMetaDeserialize(SWal* pWal, const char* bytes);
// meta section end
L
Liu Jicong 已提交
129

L
Liu Jicong 已提交
130 131 132
// seek section
int walChangeFile(SWal* pWal, int64_t ver);
// seek section end
L
Liu Jicong 已提交
133

L
Liu Jicong 已提交
134
int64_t walGetSeq();
L
Liu Jicong 已提交
135 136
int     walSeekVer(SWal* pWal, int64_t ver);
int     walRoll(SWal* pWal);
L
Liu Jicong 已提交
137

H
refact  
Hongze Cheng 已提交
138 139 140 141
#ifdef __cplusplus
}
#endif

L
Liu Jicong 已提交
142
#endif /*_TD_WAL_INT_H_*/