TDengine/include/libs/sync/sync.h

230 lines
7.3 KiB
C
Raw Normal View History

2022-02-09 06:51:23 +00:00
/*
2022-02-12 13:11:21 +00:00
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
2022-02-09 06:51:23 +00:00
*
* 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_LIBS_SYNC_H
#define _TD_LIBS_SYNC_H
#ifdef __cplusplus
extern "C" {
#endif
2022-04-18 13:50:56 +00:00
#include "cJSON.h"
2022-04-22 09:11:56 +00:00
#include "tdef.h"
2022-05-19 11:44:01 +00:00
#include "tmsgcb.h"
2022-02-09 06:51:23 +00:00
2022-06-20 09:48:56 +00:00
extern bool gRaftDetailLog;
2022-07-06 02:32:17 +00:00
#define SYNC_MAX_BATCH_SIZE 500
2022-06-25 17:16:11 +00:00
#define SYNC_INDEX_BEGIN 0
2022-05-20 02:23:48 +00:00
#define SYNC_INDEX_INVALID -1
2022-06-25 17:16:11 +00:00
#define SYNC_TERM_INVALID 0xFFFFFFFFFFFFFFFF
2022-05-20 02:23:48 +00:00
2022-07-04 06:55:26 +00:00
typedef enum {
SYNC_STRATEGY_NO_SNAPSHOT = 0,
SYNC_STRATEGY_STANDARD_SNAPSHOT = 1,
SYNC_STRATEGY_WAL_FIRST = 2,
} ESyncStrategy;
2022-02-12 13:11:21 +00:00
typedef uint64_t SyncNodeId;
2022-02-09 06:51:23 +00:00
typedef int32_t SyncGroupId;
typedef int64_t SyncIndex;
typedef uint64_t SyncTerm;
2022-05-20 02:23:48 +00:00
typedef struct SSyncNode SSyncNode;
typedef struct SSyncBuffer SSyncBuffer;
typedef struct SWal SWal;
typedef struct SSyncRaftEntry SSyncRaftEntry;
2022-02-09 06:51:23 +00:00
typedef enum {
2022-03-08 05:43:54 +00:00
TAOS_SYNC_STATE_FOLLOWER = 100,
TAOS_SYNC_STATE_CANDIDATE = 101,
TAOS_SYNC_STATE_LEADER = 102,
2022-03-22 08:17:17 +00:00
TAOS_SYNC_STATE_ERROR = 103,
2022-03-03 09:28:00 +00:00
} ESyncState;
2022-02-09 06:51:23 +00:00
2022-02-25 09:55:20 +00:00
typedef struct SNodeInfo {
2022-04-18 13:50:56 +00:00
uint16_t nodePort;
char nodeFqdn[TSDB_FQDN_LEN];
2022-02-09 06:51:23 +00:00
} SNodeInfo;
2022-02-25 09:55:20 +00:00
typedef struct SSyncCfg {
2022-02-12 13:11:21 +00:00
int32_t replicaNum;
2022-02-25 09:55:20 +00:00
int32_t myIndex;
2022-02-09 06:51:23 +00:00
SNodeInfo nodeInfo[TSDB_MAX_REPLICA];
2022-02-12 13:11:21 +00:00
} SSyncCfg;
2022-02-09 06:51:23 +00:00
2022-04-18 13:50:56 +00:00
typedef struct SFsmCbMeta {
2022-06-20 09:48:56 +00:00
int32_t code;
2022-04-18 13:50:56 +00:00
SyncIndex index;
2022-06-20 09:48:56 +00:00
SyncTerm term;
uint64_t seqNum;
SyncIndex lastConfigIndex;
2022-04-18 13:50:56 +00:00
ESyncState state;
SyncTerm currentTerm;
2022-06-20 09:48:56 +00:00
bool isWeak;
uint64_t flag;
2022-04-18 13:50:56 +00:00
} SFsmCbMeta;
typedef struct SReConfigCbMeta {
2022-06-20 09:48:56 +00:00
int32_t code;
SyncIndex index;
SyncTerm term;
uint64_t seqNum;
SyncIndex lastConfigIndex;
ESyncState state;
SyncTerm currentTerm;
bool isWeak;
uint64_t flag;
// config info
2022-05-26 06:21:24 +00:00
SSyncCfg oldCfg;
SSyncCfg newCfg;
2022-06-20 09:48:56 +00:00
SyncIndex newCfgIndex;
SyncTerm newCfgTerm;
uint64_t newCfgSeqNum;
} SReConfigCbMeta;
typedef struct SSnapshotParam {
SyncIndex start;
SyncIndex end;
} SSnapshotParam;
typedef struct SSnapshot {
2022-05-30 04:26:15 +00:00
void* data;
SyncIndex lastApplyIndex;
SyncTerm lastApplyTerm;
2022-06-06 09:23:59 +00:00
SyncIndex lastConfigIndex;
} SSnapshot;
typedef struct SSnapshotMeta {
SyncIndex lastConfigIndex;
} SSnapshotMeta;
2022-02-12 13:11:21 +00:00
typedef struct SSyncFSM {
void* data;
2022-04-18 13:50:56 +00:00
void (*FpCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta);
void (*FpPreCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta);
void (*FpRollBackCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta);
void (*FpRestoreFinishCb)(struct SSyncFSM* pFsm);
void (*FpReConfigCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SReConfigCbMeta cbMeta);
2022-06-13 07:47:43 +00:00
void (*FpLeaderTransferCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta);
2022-05-25 06:43:45 +00:00
2022-06-20 09:48:56 +00:00
int32_t (*FpGetSnapshot)(struct SSyncFSM* pFsm, SSnapshot* pSnapshot, void* pReaderParam, void** ppReader);
int32_t (*FpGetSnapshotInfo)(struct SSyncFSM* pFsm, SSnapshot* pSnapshot);
int32_t (*FpSnapshotStartRead)(struct SSyncFSM* pFsm, void* pReaderParam, void** ppReader);
int32_t (*FpSnapshotStopRead)(struct SSyncFSM* pFsm, void* pReader);
int32_t (*FpSnapshotDoRead)(struct SSyncFSM* pFsm, void* pReader, void** ppBuf, int32_t* len);
int32_t (*FpSnapshotStartWrite)(struct SSyncFSM* pFsm, void* pWriterParam, void** ppWriter);
int32_t (*FpSnapshotStopWrite)(struct SSyncFSM* pFsm, void* pWriter, bool isApply);
int32_t (*FpSnapshotDoWrite)(struct SSyncFSM* pFsm, void* pWriter, void* pBuf, int32_t len);
2022-02-09 06:51:23 +00:00
} SSyncFSM;
2022-02-12 13:11:21 +00:00
// abstract definition of log store in raft
// SWal implements it
2022-02-09 06:51:23 +00:00
typedef struct SSyncLogStore {
2022-02-12 13:11:21 +00:00
void* data;
// append one log entry
2022-03-09 12:24:27 +00:00
int32_t (*appendEntry)(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
2022-02-09 06:51:23 +00:00
2022-03-09 07:07:43 +00:00
// get one log entry, user need to free pEntry->pCont
2022-03-09 12:24:27 +00:00
SSyncRaftEntry* (*getEntry)(struct SSyncLogStore* pLogStore, SyncIndex index);
2022-02-09 06:51:23 +00:00
2022-03-09 07:07:43 +00:00
// truncate log with index, entries after the given index (>=index) will be deleted
int32_t (*truncate)(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);
2022-02-09 06:51:23 +00:00
2022-02-12 13:11:21 +00:00
// return index of last entry
SyncIndex (*getLastIndex)(struct SSyncLogStore* pLogStore);
// return term of last entry
SyncTerm (*getLastTerm)(struct SSyncLogStore* pLogStore);
2022-02-09 06:51:23 +00:00
2022-03-09 07:07:43 +00:00
// update log store commit index with "index"
int32_t (*updateCommitIndex)(struct SSyncLogStore* pLogStore, SyncIndex index);
// return commit index of log
SyncIndex (*getCommitIndex)(struct SSyncLogStore* pLogStore);
2022-06-05 11:47:54 +00:00
SyncIndex (*syncLogBeginIndex)(struct SSyncLogStore* pLogStore);
SyncIndex (*syncLogEndIndex)(struct SSyncLogStore* pLogStore);
bool (*syncLogIsEmpty)(struct SSyncLogStore* pLogStore);
int32_t (*syncLogEntryCount)(struct SSyncLogStore* pLogStore);
int32_t (*syncLogRestoreFromSnapshot)(struct SSyncLogStore* pLogStore, SyncIndex index);
bool (*syncLogExist)(struct SSyncLogStore* pLogStore, SyncIndex index);
2022-06-05 11:47:54 +00:00
2022-06-05 13:48:50 +00:00
SyncIndex (*syncLogWriteIndex)(struct SSyncLogStore* pLogStore);
2022-06-05 11:47:54 +00:00
SyncIndex (*syncLogLastIndex)(struct SSyncLogStore* pLogStore);
SyncTerm (*syncLogLastTerm)(struct SSyncLogStore* pLogStore);
int32_t (*syncLogAppendEntry)(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
int32_t (*syncLogGetEntry)(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry);
int32_t (*syncLogTruncate)(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);
2022-02-09 06:51:23 +00:00
} SSyncLogStore;
2022-02-25 09:55:20 +00:00
typedef struct SSyncInfo {
2022-07-04 06:55:26 +00:00
bool isStandBy;
ESyncStrategy snapshotStrategy;
SyncGroupId vgId;
int32_t batchSize;
SSyncCfg syncCfg;
char path[TSDB_FILENAME_LEN];
SWal* pWal;
SSyncFSM* pFsm;
SMsgCb* msgcb;
2022-05-19 11:44:01 +00:00
int32_t (*FpSendMsg)(const SEpSet* pEpSet, SRpcMsg* pMsg);
int32_t (*FpEqMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
2022-02-09 06:51:23 +00:00
} SSyncInfo;
2022-04-18 13:50:56 +00:00
int32_t syncInit();
void syncCleanUp();
int64_t syncOpen(const SSyncInfo* pSyncInfo);
void syncStart(int64_t rid);
void syncStop(int64_t rid);
2022-05-26 07:08:20 +00:00
int32_t syncSetStandby(int64_t rid);
2022-04-18 13:50:56 +00:00
ESyncState syncGetMyRole(int64_t rid);
2022-06-16 08:16:40 +00:00
bool syncIsReady(int64_t rid);
2022-04-18 13:50:56 +00:00
const char* syncGetMyRoleStr(int64_t rid);
SyncTerm syncGetMyTerm(int64_t rid);
2022-06-20 09:48:56 +00:00
SyncGroupId syncGetVgId(int64_t rid);
2022-04-27 11:28:01 +00:00
void syncGetEpSet(int64_t rid, SEpSet* pEpSet);
void syncGetRetryEpSet(int64_t rid, SEpSet* pEpSet);
2022-06-22 12:23:49 +00:00
int32_t syncPropose(int64_t rid, SRpcMsg* pMsg, bool isWeak);
int32_t syncProposeBatch(int64_t rid, SRpcMsg* pMsgArr, bool* pIsWeakArr, int32_t arrSize);
2022-05-20 02:23:48 +00:00
bool syncEnvIsStart();
2022-04-22 09:11:56 +00:00
const char* syncStr(ESyncState state);
2022-05-23 15:53:15 +00:00
bool syncIsRestoreFinish(int64_t rid);
int32_t syncGetSnapshotByIndex(int64_t rid, SyncIndex index, SSnapshot* pSnapshot);
int32_t syncReconfig(int64_t rid, const SSyncCfg* pNewCfg);
2022-06-12 07:21:56 +00:00
// build SRpcMsg, need to call syncPropose with SRpcMsg
int32_t syncReconfigBuild(int64_t rid, const SSyncCfg* pNewCfg, SRpcMsg* pRpcMsg);
2022-06-11 04:44:58 +00:00
int32_t syncLeaderTransfer(int64_t rid);
int32_t syncLeaderTransferTo(int64_t rid, SNodeInfo newLeader);
2022-02-09 06:51:23 +00:00
#ifdef __cplusplus
}
#endif
#endif /*_TD_LIBS_SYNC_H*/