TDengine/source/libs/wal/inc/walInt.h

178 lines
6 KiB
C
Raw Normal View History

2021-09-22 09:18:10 +00:00
/*
* 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_
2022-01-11 07:40:10 +00:00
#include "taoserror.h"
2021-12-11 09:17:21 +00:00
#include "tchecksum.h"
2022-01-11 07:40:10 +00:00
#include "tcoding.h"
2022-07-07 05:56:47 +00:00
#include "tcommon.h"
2022-04-24 05:42:54 +00:00
#include "tcompare.h"
2021-12-17 05:40:27 +00:00
#include "wal.h"
2021-11-23 07:46:23 +00:00
2021-09-22 09:18:10 +00:00
#ifdef __cplusplus
extern "C" {
#endif
2024-07-18 02:29:25 +00:00
// clang-format off
#define wFatal(...) { if (wDebugFlag & DEBUG_FATAL) { taosPrintLog("WAL FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }}
#define wError(...) { if (wDebugFlag & DEBUG_ERROR) { taosPrintLog("WAL ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }}
#define wWarn(...) { if (wDebugFlag & DEBUG_WARN) { taosPrintLog("WAL WARN ", DEBUG_WARN, 255, __VA_ARGS__); }}
#define wInfo(...) { if (wDebugFlag & DEBUG_INFO) { taosPrintLog("WAL ", DEBUG_INFO, 255, __VA_ARGS__); }}
#define wDebug(...) { if (wDebugFlag & DEBUG_DEBUG) { taosPrintLog("WAL ", DEBUG_DEBUG, wDebugFlag, __VA_ARGS__); }}
#define wTrace(...) { if (wDebugFlag & DEBUG_TRACE) { taosPrintLog("WAL ", DEBUG_TRACE, wDebugFlag, __VA_ARGS__); }}
// clang-format on
2021-12-17 05:40:27 +00:00
// meta section begin
2022-07-01 06:39:21 +00:00
typedef struct {
2021-12-09 10:32:38 +00:00
int64_t firstVer;
int64_t lastVer;
int64_t createTs;
int64_t closeTs;
int64_t fileSize;
int64_t syncedOffset;
2021-12-17 05:40:27 +00:00
} SWalFileInfo;
2021-12-09 10:32:38 +00:00
2021-12-11 09:17:21 +00:00
typedef struct WalIdxEntry {
int64_t ver;
int64_t offset;
2021-12-17 05:40:27 +00:00
} SWalIdxEntry;
2021-12-11 09:17:21 +00:00
2022-01-11 07:40:10 +00:00
static inline int tSerializeWalIdxEntry(void** buf, SWalIdxEntry* pIdxEntry) {
2022-04-24 05:42:54 +00:00
int tlen = 0;
2022-01-11 07:40:10 +00:00
tlen += taosEncodeFixedI64(buf, pIdxEntry->ver);
tlen += taosEncodeFixedI64(buf, pIdxEntry->offset);
2022-04-24 05:42:54 +00:00
return tlen;
2022-01-11 07:40:10 +00:00
}
static inline void* tDeserializeWalIdxEntry(void* buf, SWalIdxEntry* pIdxEntry) {
buf = taosDecodeFixedI64(buf, &pIdxEntry->ver);
buf = taosDecodeFixedI64(buf, &pIdxEntry->offset);
return buf;
}
2021-12-09 10:32:38 +00:00
static inline int32_t compareWalFileInfo(const void* pLeft, const void* pRight) {
2021-12-17 05:40:27 +00:00
SWalFileInfo* pInfoLeft = (SWalFileInfo*)pLeft;
SWalFileInfo* pInfoRight = (SWalFileInfo*)pRight;
2021-12-09 10:32:38 +00:00
return compareInt64Val(&pInfoLeft->firstVer, &pInfoRight->firstVer);
}
static inline int64_t walGetLastFileSize(SWal* pWal) {
2022-07-27 08:49:03 +00:00
if (taosArrayGetSize(pWal->fileInfoSet) == 0) return 0;
2021-12-17 05:40:27 +00:00
SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
2021-12-09 10:32:38 +00:00
return pInfo->fileSize;
}
static inline int64_t walGetLastFileCachedSize(SWal* pWal) {
if (taosArrayGetSize(pWal->fileInfoSet) == 0) return 0;
SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
return (pInfo->fileSize - pInfo->syncedOffset);
}
2021-12-09 10:32:38 +00:00
static inline int64_t walGetLastFileFirstVer(SWal* pWal) {
2022-07-27 08:49:03 +00:00
if (taosArrayGetSize(pWal->fileInfoSet) == 0) return -1;
2021-12-17 05:40:27 +00:00
SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGetLast(pWal->fileInfoSet);
2021-12-09 10:32:38 +00:00
return pInfo->firstVer;
}
static inline int64_t walGetCurFileFirstVer(SWal* pWal) {
2022-07-27 08:49:03 +00:00
if (pWal->writeCur == -1) return -1;
2021-12-17 05:40:27 +00:00
SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
2021-12-09 10:32:38 +00:00
return pInfo->firstVer;
}
static inline int64_t walGetCurFileLastVer(SWal* pWal) {
2022-07-27 08:49:03 +00:00
if (pWal->writeCur == -1) return -1;
2021-12-17 05:40:27 +00:00
SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
2021-12-09 10:32:38 +00:00
return pInfo->firstVer;
}
static inline int64_t walGetCurFileOffset(SWal* pWal) {
2022-07-27 08:49:03 +00:00
if (pWal->writeCur == -1) return -1;
2021-12-17 05:40:27 +00:00
SWalFileInfo* pInfo = (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
2021-12-09 10:32:38 +00:00
return pInfo->fileSize;
}
2021-12-17 05:40:27 +00:00
static inline bool walCurFileClosed(SWal* pWal) { return taosArrayGetSize(pWal->fileInfoSet) != pWal->writeCur; }
2021-12-09 10:32:38 +00:00
2021-12-17 05:40:27 +00:00
static inline SWalFileInfo* walGetCurFileInfo(SWal* pWal) {
2022-07-27 08:49:03 +00:00
if (pWal->writeCur == -1) return NULL;
2021-12-17 05:40:27 +00:00
return (SWalFileInfo*)taosArrayGet(pWal->fileInfoSet, pWal->writeCur);
2021-12-09 10:32:38 +00:00
}
2022-10-10 03:06:57 +00:00
static inline void walBuildLogName(SWal* pWal, int64_t fileFirstVer, char* buf) {
2024-07-25 07:54:46 +00:00
TAOS_UNUSED(sprintf(buf, "%s/%020" PRId64 "." WAL_LOG_SUFFIX, pWal->path, fileFirstVer));
2021-12-09 10:32:38 +00:00
}
2022-10-10 03:06:57 +00:00
static inline void walBuildIdxName(SWal* pWal, int64_t fileFirstVer, char* buf) {
2024-07-25 07:54:46 +00:00
TAOS_UNUSED(sprintf(buf, "%s/%020" PRId64 "." WAL_INDEX_SUFFIX, pWal->path, fileFirstVer));
2021-12-09 10:32:38 +00:00
}
2022-07-01 06:39:21 +00:00
static inline int walValidHeadCksum(SWalCkHead* pHead) {
return taosCheckChecksum((uint8_t*)&pHead->head, sizeof(SWalCont), pHead->cksumHead);
2021-12-11 09:17:21 +00:00
}
2022-07-01 06:39:21 +00:00
static inline int walValidBodyCksum(SWalCkHead* pHead) {
2022-04-24 05:42:54 +00:00
return taosCheckChecksum((uint8_t*)pHead->head.body, pHead->head.bodyLen, pHead->cksumBody);
2021-12-11 09:17:21 +00:00
}
2022-07-01 06:39:21 +00:00
static inline int walValidCksum(SWalCkHead* pHead, void* body, int64_t bodyLen) {
2021-12-11 09:17:21 +00:00
return walValidHeadCksum(pHead) && walValidBodyCksum(pHead);
}
2022-07-01 06:39:21 +00:00
static inline uint32_t walCalcHeadCksum(SWalCkHead* pHead) {
return taosCalcChecksum(0, (uint8_t*)&pHead->head, sizeof(SWalCont));
2021-12-11 09:17:21 +00:00
}
static inline uint32_t walCalcBodyCksum(const void* body, uint32_t len) {
return taosCalcChecksum(0, (uint8_t*)body, len);
}
2021-12-15 09:17:50 +00:00
static inline int64_t walGetVerIdxOffset(SWal* pWal, int64_t ver) {
2021-12-17 05:40:27 +00:00
return (ver - walGetCurFileFirstVer(pWal)) * sizeof(SWalIdxEntry);
2021-12-15 09:17:50 +00:00
}
2021-12-14 10:14:45 +00:00
static inline void walResetVer(SWalVer* pVer) {
pVer->firstVer = -1;
pVer->verInSnapshotting = -1;
pVer->snapshotVer = -1;
pVer->commitVer = -1;
pVer->lastVer = -1;
}
2024-07-22 06:51:22 +00:00
int32_t walLoadMeta(SWal* pWal);
int32_t walSaveMeta(SWal* pWal);
int32_t walRemoveMeta(SWal* pWal);
int32_t walRollFileInfo(SWal* pWal);
2021-12-09 10:32:38 +00:00
2024-07-22 06:51:22 +00:00
int32_t walCheckAndRepairMeta(SWal* pWal);
2021-12-15 09:17:50 +00:00
2024-07-22 06:51:22 +00:00
int32_t walCheckAndRepairIdx(SWal* pWal);
2021-12-15 09:17:50 +00:00
2024-07-22 06:51:22 +00:00
int32_t walMetaSerialize(SWal* pWal, char** serialized);
int32_t walMetaDeserialize(SWal* pWal, const char* bytes);
2021-12-17 05:40:27 +00:00
// meta section end
2021-11-26 06:22:16 +00:00
2024-07-24 02:53:12 +00:00
int32_t decryptBody(SWalCfg* cfg, SWalCkHead* pHead, int32_t plainBodyLen, const char* func);
2024-07-22 07:28:04 +00:00
2021-12-03 06:28:54 +00:00
int64_t walGetSeq();
2021-09-22 09:18:10 +00:00
#ifdef __cplusplus
}
#endif
2021-11-23 07:46:23 +00:00
#endif /*_TD_WAL_INT_H_*/