TDengine/source/libs/sync/inc/syncInt.h

342 lines
12 KiB
C
Raw Normal View History

2022-02-22 03:28:15 +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_LIBS_SYNC_INT_H
#define _TD_LIBS_SYNC_INT_H
#ifdef __cplusplus
extern "C" {
#endif
2022-02-26 18:24:50 +00:00
#include "sync.h"
2022-10-31 15:40:43 +00:00
#include "taosdef.h"
2022-11-02 02:24:55 +00:00
#include "trpc.h"
2022-02-28 06:10:34 +00:00
#include "ttimer.h"
2022-02-26 18:24:50 +00:00
2022-05-20 02:23:48 +00:00
typedef struct SyncTimeout SyncTimeout;
typedef struct SyncClientRequest SyncClientRequest;
typedef struct SyncRequestVote SyncRequestVote;
typedef struct SyncRequestVoteReply SyncRequestVoteReply;
typedef struct SyncAppendEntries SyncAppendEntries;
2022-02-26 18:24:50 +00:00
typedef struct SyncAppendEntriesReply SyncAppendEntriesReply;
2022-05-20 02:23:48 +00:00
typedef struct SSyncEnv SSyncEnv;
typedef struct SVotesGranted SVotesGranted;
typedef struct SVotesRespond SVotesRespond;
typedef struct SSyncIndexMgr SSyncIndexMgr;
typedef struct SSyncRespMgr SSyncRespMgr;
2022-05-27 07:35:26 +00:00
typedef struct SSyncSnapshotSender SSyncSnapshotSender;
typedef struct SSyncSnapshotReceiver SSyncSnapshotReceiver;
typedef struct SSyncTimer SSyncTimer;
typedef struct SSyncHbTimerData SSyncHbTimerData;
2022-11-11 06:35:16 +00:00
typedef struct SyncSnapshotSend SyncSnapshotSend;
typedef struct SyncSnapshotRsp SyncSnapshotRsp;
typedef struct SyncLocalCmd SyncLocalCmd;
typedef struct SyncAppendEntriesBatch SyncAppendEntriesBatch;
typedef struct SyncPreSnapshotReply SyncPreSnapshotReply;
typedef struct SyncHeartbeatReply SyncHeartbeatReply;
typedef struct SyncHeartbeat SyncHeartbeat;
typedef struct SyncPreSnapshot SyncPreSnapshot;
typedef struct SSyncLogBuffer SSyncLogBuffer;
typedef struct SSyncLogReplMgr SSyncLogReplMgr;
2022-11-11 06:35:16 +00:00
#define MAX_CONFIG_INDEX_COUNT 256
typedef struct SRaftCfg {
2023-04-18 11:03:45 +00:00
SSyncCfg cfg;
int32_t batchSize;
int8_t isStandBy;
int8_t snapshotStrategy;
SyncIndex lastConfigIndex;
int32_t configIndexCount;
SyncIndex configIndexArr[MAX_CONFIG_INDEX_COUNT];
} SRaftCfg;
2022-11-11 06:35:16 +00:00
typedef struct SRaftId {
SyncNodeId addr;
SyncGroupId vgId;
} SRaftId;
2023-01-09 04:01:36 +00:00
typedef struct SRaftStore {
SyncTerm currentTerm;
SRaftId voteFor;
2023-02-13 11:00:10 +00:00
TdThreadMutex mutex;
2023-01-09 04:01:36 +00:00
} SRaftStore;
struct SSyncHbTimerData {
int64_t syncNodeRid;
SSyncTimer* pTimer;
SRaftId destId;
uint64_t logicClock;
int64_t execTime;
int64_t rid;
};
struct SSyncTimer {
void* pTimer;
TAOS_TMR_CALLBACK timerCb;
uint64_t logicClock;
uint64_t counter;
int32_t timerMS;
2022-11-25 10:19:25 +00:00
int64_t timeStamp;
SRaftId destId;
int64_t hbDataRid;
};
2022-11-16 06:05:34 +00:00
typedef struct SElectTimerParam {
2022-10-25 11:56:49 +00:00
uint64_t logicClock;
SSyncNode* pSyncNode;
2022-11-16 06:05:34 +00:00
int64_t executeTime;
2022-10-25 11:56:49 +00:00
void* pData;
2022-11-16 06:05:34 +00:00
} SElectTimerParam;
2022-10-16 04:07:02 +00:00
typedef struct SPeerState {
SyncIndex lastSendIndex;
int64_t lastSendTime;
} SPeerState;
struct SSyncNode {
2022-03-03 09:28:00 +00:00
// init by SSyncInfo
2022-02-26 16:02:18 +00:00
SyncGroupId vgId;
SRaftCfg raftCfg;
2022-02-26 16:02:18 +00:00
char path[TSDB_FILENAME_LEN];
2022-03-09 10:33:41 +00:00
char raftStorePath[TSDB_FILENAME_LEN * 2];
2022-04-18 13:50:56 +00:00
char configPath[TSDB_FILENAME_LEN * 2];
2022-03-14 08:27:25 +00:00
// sync io
2022-10-31 04:59:42 +00:00
SSyncLogBuffer* pLogBuf;
struct SWal* pWal;
2023-01-09 04:01:36 +00:00
const SMsgCb* msgcb;
2022-11-01 07:40:23 +00:00
int32_t (*syncSendMSg)(const SEpSet* pEpSet, SRpcMsg* pMsg);
int32_t (*syncEqMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
int32_t (*syncEqCtrlMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
2022-03-01 12:29:49 +00:00
2022-03-03 09:28:00 +00:00
// init internal
2022-03-08 05:55:13 +00:00
SNodeInfo myNodeInfo;
SRaftId myRaftId;
2022-03-05 07:03:49 +00:00
2022-02-28 08:36:57 +00:00
int32_t peersNum;
2023-04-18 11:03:45 +00:00
SNodeInfo peersNodeInfo[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
SEpSet peersEpset[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
SRaftId peersId[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
2022-03-05 07:03:49 +00:00
int32_t replicaNum;
2023-04-18 11:03:45 +00:00
int32_t totalReplicaNum;
SRaftId replicasId[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
2022-02-28 08:36:57 +00:00
2022-03-03 09:28:00 +00:00
// raft algorithm
SSyncFSM* pFsm;
int32_t quorum;
2022-03-06 04:25:12 +00:00
SRaftId leaderCache;
ESyncFsmState fsmState;
2022-03-03 09:28:00 +00:00
// life cycle
int64_t rid;
// tla+ server vars
2023-01-09 04:01:36 +00:00
ESyncState state;
SRaftStore raftStore;
2022-03-03 09:28:00 +00:00
// tla+ candidate vars
2022-03-08 02:52:18 +00:00
SVotesGranted* pVotesGranted;
SVotesRespond* pVotesRespond;
2022-02-28 06:10:34 +00:00
2022-03-03 09:28:00 +00:00
// tla+ leader vars
2022-03-09 03:27:22 +00:00
SSyncIndexMgr* pNextIndex;
SSyncIndexMgr* pMatchIndex;
2022-03-03 09:28:00 +00:00
// tla+ log vars
SSyncLogStore* pLogStore;
SyncIndex commitIndex;
// assigned leader log vars
SyncIndex assignedCommitIndex;
SyncTerm arbTerm;
TdThreadMutex arbTokenMutex;
char arbToken[TSDB_ARB_TOKEN_SIZE];
2022-03-22 02:42:02 +00:00
// timer ms init
int32_t pingBaseLine;
int32_t electBaseLine;
int32_t hbBaseLine;
2022-03-08 06:19:50 +00:00
// ping timer
2022-03-07 06:18:46 +00:00
tmr_h pPingTimer;
int32_t pingTimerMS;
2022-03-06 09:59:24 +00:00
uint64_t pingTimerLogicClock;
uint64_t pingTimerLogicClockUser;
2022-03-14 08:27:25 +00:00
TAOS_TMR_CALLBACK FpPingTimerCB; // Timer Fp
2022-02-28 08:36:57 +00:00
uint64_t pingTimerCounter;
2022-03-08 06:19:50 +00:00
// elect timer
2022-02-28 08:36:57 +00:00
tmr_h pElectTimer;
int32_t electTimerMS;
2022-03-07 06:18:46 +00:00
uint64_t electTimerLogicClock;
2022-03-14 08:27:25 +00:00
TAOS_TMR_CALLBACK FpElectTimerCB; // Timer Fp
2022-02-28 08:36:57 +00:00
uint64_t electTimerCounter;
2022-11-16 06:05:34 +00:00
SElectTimerParam electTimerParam;
2022-02-28 08:36:57 +00:00
2022-03-08 06:19:50 +00:00
// heartbeat timer
2022-02-28 08:36:57 +00:00
tmr_h pHeartbeatTimer;
int32_t heartbeatTimerMS;
2022-03-07 06:18:46 +00:00
uint64_t heartbeatTimerLogicClock;
uint64_t heartbeatTimerLogicClockUser;
2022-03-14 08:27:25 +00:00
TAOS_TMR_CALLBACK FpHeartbeatTimerCB; // Timer Fp
2022-02-28 08:36:57 +00:00
uint64_t heartbeatTimerCounter;
// peer heartbeat timer
2023-04-18 11:03:45 +00:00
SSyncTimer peerHeartbeatTimerArr[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
2022-04-18 13:50:56 +00:00
// tools
SSyncRespMgr* pSyncRespMgr;
// restore state
2022-11-11 14:55:21 +00:00
bool restoreFinish;
// SSnapshot* pSnapshot;
2023-04-18 11:03:45 +00:00
SSyncSnapshotSender* senders[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
2022-06-01 13:23:39 +00:00
SSyncSnapshotReceiver* pNewNodeReceiver;
2022-11-11 14:55:21 +00:00
// log replication mgr
2023-04-18 11:03:45 +00:00
SSyncLogReplMgr* logReplMgrs[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
2022-11-11 14:55:21 +00:00
2023-04-18 11:03:45 +00:00
SPeerState peerStates[TSDB_MAX_REPLICA + TSDB_MAX_LEARNER_REPLICA];
2022-10-16 04:07:02 +00:00
2022-06-20 09:48:56 +00:00
// is config changing
bool changing;
int64_t snapshottingIndex;
2022-10-20 06:53:03 +00:00
int64_t snapshottingTime;
int64_t minMatchIndex;
2022-08-08 11:46:37 +00:00
int64_t startTime;
int64_t roleTimeMs;
2022-08-08 11:46:37 +00:00
int64_t lastReplicateTime;
int32_t electNum;
int32_t becomeLeaderNum;
int32_t becomeAssignedLeaderNum;
int32_t configChangeNum;
int32_t hbSlowNum;
int32_t hbrSlowNum;
2022-11-30 03:20:03 +00:00
int32_t tmrRoutineNum;
bool isStart;
};
2022-02-22 03:28:15 +00:00
2022-03-14 08:27:25 +00:00
// open/close --------------
2023-07-21 02:31:53 +00:00
SSyncNode* syncNodeOpen(SSyncInfo* pSyncInfo, int32_t vnodeVersion);
2022-10-31 04:59:42 +00:00
int32_t syncNodeStart(SSyncNode* pSyncNode);
int32_t syncNodeStartStandBy(SSyncNode* pSyncNode);
2022-03-03 09:28:00 +00:00
void syncNodeClose(SSyncNode* pSyncNode);
2022-11-03 01:39:20 +00:00
void syncNodePreClose(SSyncNode* pSyncNode);
void syncNodePostClose(SSyncNode* pSyncNode);
2023-01-09 04:01:36 +00:00
int32_t syncNodePropose(SSyncNode* pSyncNode, SRpcMsg* pMsg, bool isWeak, int64_t* seq);
2022-10-31 04:59:42 +00:00
int32_t syncNodeRestore(SSyncNode* pSyncNode);
void syncHbTimerDataFree(SSyncHbTimerData* pData);
2022-03-07 08:06:07 +00:00
2023-07-18 08:09:38 +00:00
// config
int32_t syncNodeChangeConfig(SSyncNode* ths, SSyncRaftEntry* pEntry, char* str);
2022-11-13 09:21:30 +00:00
// on message ---------------------
int32_t syncNodeOnTimeout(SSyncNode* ths, const SRpcMsg* pMsg);
int32_t syncNodeOnClientRequest(SSyncNode* ths, SRpcMsg* pMsg, SyncIndex* pRetIndex);
int32_t syncNodeOnRequestVote(SSyncNode* pNode, const SRpcMsg* pMsg);
int32_t syncNodeOnRequestVoteReply(SSyncNode* pNode, const SRpcMsg* pMsg);
int32_t syncNodeOnAppendEntries(SSyncNode* pNode, const SRpcMsg* pMsg);
int32_t syncNodeOnAppendEntriesReply(SSyncNode* ths, const SRpcMsg* pMsg);
int32_t syncNodeOnSnapshot(SSyncNode* ths, SRpcMsg* pMsg);
int32_t syncNodeOnSnapshotRsp(SSyncNode* ths, SRpcMsg* pMsg);
2022-11-13 09:21:30 +00:00
int32_t syncNodeOnHeartbeat(SSyncNode* ths, const SRpcMsg* pMsg);
int32_t syncNodeOnHeartbeatReply(SSyncNode* ths, const SRpcMsg* pMsg);
int32_t syncNodeOnLocalCmd(SSyncNode* ths, const SRpcMsg* pMsg);
2022-03-04 07:48:09 +00:00
2022-03-14 08:27:25 +00:00
// timer control --------------
2022-03-04 07:48:09 +00:00
int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode);
2022-03-07 06:18:46 +00:00
int32_t syncNodeStartElectTimer(SSyncNode* pSyncNode, int32_t ms);
2022-03-04 07:48:09 +00:00
int32_t syncNodeStopElectTimer(SSyncNode* pSyncNode);
2022-03-07 06:18:46 +00:00
int32_t syncNodeRestartElectTimer(SSyncNode* pSyncNode, int32_t ms);
void syncNodeResetElectTimer(SSyncNode* pSyncNode);
2022-03-04 07:48:09 +00:00
int32_t syncNodeStartHeartbeatTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode);
int32_t syncNodeRestartHeartbeatTimer(SSyncNode* pSyncNode);
2022-03-14 08:27:25 +00:00
// utils --------------
2022-10-20 06:53:03 +00:00
int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg);
SyncIndex syncMinMatchIndex(SSyncNode* pSyncNode);
int32_t syncCacheEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry, LRUHandle** h);
bool syncNodeHeartbeatReplyTimeout(SSyncNode* pSyncNode);
bool syncNodeSnapshotSending(SSyncNode* pSyncNode);
bool syncNodeSnapshotRecving(SSyncNode* pSyncNode);
bool syncNodeIsReadyForRead(SSyncNode* pSyncNode);
2022-03-10 11:21:02 +00:00
2022-03-14 10:44:53 +00:00
// raft state change --------------
void syncNodeUpdateTerm(SSyncNode* pSyncNode, SyncTerm term);
void syncNodeUpdateTermWithoutStepDown(SSyncNode* pSyncNode, SyncTerm term);
2022-10-19 08:08:42 +00:00
void syncNodeStepDown(SSyncNode* pSyncNode, SyncTerm newTerm);
2022-06-10 07:19:11 +00:00
void syncNodeBecomeFollower(SSyncNode* pSyncNode, const char* debugStr);
2023-04-18 11:03:45 +00:00
void syncNodeBecomeLearner(SSyncNode* pSyncNode, const char* debugStr);
2022-06-10 07:19:11 +00:00
void syncNodeBecomeLeader(SSyncNode* pSyncNode, const char* debugStr);
void syncNodeBecomeAssignedLeader(SSyncNode* pSyncNode);
2022-03-14 10:44:53 +00:00
void syncNodeCandidate2Leader(SSyncNode* pSyncNode);
void syncNodeFollower2Candidate(SSyncNode* pSyncNode);
void syncNodeLeader2Follower(SSyncNode* pSyncNode);
void syncNodeCandidate2Follower(SSyncNode* pSyncNode);
int32_t syncNodeAssignedLeader2Leader(SSyncNode* pSyncNode);
2022-03-14 10:44:53 +00:00
// raft vote --------------
void syncNodeVoteForTerm(SSyncNode* pSyncNode, SyncTerm term, SRaftId* pRaftId);
2023-02-13 11:00:10 +00:00
void syncNodeVoteForSelf(SSyncNode* pSyncNode, SyncTerm term);
2022-03-14 10:44:53 +00:00
2022-11-11 14:55:21 +00:00
// log replication
SSyncLogReplMgr* syncNodeGetLogReplMgr(SSyncNode* pNode, SRaftId* pDestId);
2022-05-30 09:31:55 +00:00
// snapshot --------------
2022-11-13 04:21:14 +00:00
bool syncNodeHasSnapshot(SSyncNode* pSyncNode);
void syncNodeMaybeUpdateCommitBySnapshot(SSyncNode* pSyncNode);
int32_t syncNodeStartSnapshot(SSyncNode* pSyncNode, SRaftId* pDestId);
2022-06-06 03:24:25 +00:00
SyncIndex syncNodeGetLastIndex(const SSyncNode* pSyncNode);
2022-06-05 11:47:54 +00:00
SyncTerm syncNodeGetLastTerm(SSyncNode* pSyncNode);
int32_t syncNodeGetLastIndexTerm(SSyncNode* pSyncNode, SyncIndex* pLastIndex, SyncTerm* pLastTerm);
2022-06-06 03:24:25 +00:00
SyncIndex syncNodeSyncStartIndex(SSyncNode* pSyncNode);
2022-06-05 11:47:54 +00:00
SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index);
2022-06-06 03:24:25 +00:00
SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index);
2022-06-05 11:47:54 +00:00
int32_t syncNodeGetPreIndexTerm(SSyncNode* pSyncNode, SyncIndex index, SyncIndex* pPreIndex, SyncTerm* pPreTerm);
2022-05-30 09:31:55 +00:00
2022-10-18 07:24:00 +00:00
int32_t syncNodeDoCommit(SSyncNode* ths, SyncIndex beginIndex, SyncIndex endIndex, uint64_t flag);
2022-10-18 05:53:03 +00:00
int32_t syncNodeFollowerCommit(SSyncNode* ths, SyncIndex newCommitIndex);
int32_t syncNodePreCommit(SSyncNode* ths, SSyncRaftEntry* pEntry, int32_t code);
2022-06-06 08:02:25 +00:00
2022-06-08 03:03:28 +00:00
bool syncNodeInRaftGroup(SSyncNode* ths, SRaftId* pRaftId);
SSyncSnapshotSender* syncNodeGetSnapshotSender(SSyncNode* ths, SRaftId* pDestId);
SSyncTimer* syncNodeGetHbTimer(SSyncNode* ths, SRaftId* pDestId);
2022-10-16 04:07:02 +00:00
SPeerState* syncNodeGetPeerState(SSyncNode* ths, const SRaftId* pDestId);
bool syncNodeNeedSendAppendEntries(SSyncNode* ths, const SRaftId* pDestId, const SyncAppendEntries* pMsg);
2022-06-20 09:48:56 +00:00
int32_t syncGetSnapshotMeta(int64_t rid, struct SSnapshotMeta* sMeta);
int32_t syncGetSnapshotMetaByIndex(int64_t rid, SyncIndex snapshotIndex, struct SSnapshotMeta* sMeta);
int32_t syncNodeDynamicQuorum(const SSyncNode* pSyncNode);
2022-10-16 04:07:02 +00:00
bool syncNodeIsMnode(SSyncNode* pSyncNode);
int32_t syncNodePeerStateInit(SSyncNode* pSyncNode);
2022-10-15 01:28:55 +00:00
2022-02-22 03:28:15 +00:00
#ifdef __cplusplus
}
#endif
#endif /*_TD_LIBS_SYNC_INT_H*/