From 6d3efd1b1afd1c6cbf199ef50bd6bc8600a6ede4 Mon Sep 17 00:00:00 2001 From: lichuang Date: Fri, 12 Nov 2021 10:20:03 +0800 Subject: [PATCH] [TD-10645][raft]refactor sync typedef --- source/libs/sync/inc/raft.h | 3 ++- source/libs/sync/inc/raft_log.h | 6 +++--- source/libs/sync/inc/raft_message.h | 16 ++++++++-------- source/libs/sync/inc/sync_raft_impl.h | 4 ++-- source/libs/sync/inc/sync_raft_progress.h | 12 ++++++------ .../libs/sync/inc/sync_raft_progress_tracker.h | 6 +++--- source/libs/sync/inc/sync_raft_quorum.h | 6 +++--- source/libs/sync/inc/sync_raft_quorum_joint.h | 2 +- source/libs/sync/inc/sync_raft_quorum_majority.h | 2 +- source/libs/sync/inc/sync_type.h | 4 ++-- source/libs/sync/src/raft.c | 4 ++-- source/libs/sync/src/raft_election.c | 6 +++--- .../sync/src/raft_handle_vote_resp_message.c | 2 +- source/libs/sync/src/sync_raft_impl.c | 4 ++-- source/libs/sync/src/sync_raft_progress.c | 8 ++++---- .../libs/sync/src/sync_raft_progress_tracker.c | 4 ++-- source/libs/sync/src/sync_raft_quorum_joint.c | 6 +++--- source/libs/sync/src/sync_raft_quorum_majority.c | 2 +- 18 files changed, 49 insertions(+), 48 deletions(-) diff --git a/source/libs/sync/inc/raft.h b/source/libs/sync/inc/raft.h index 5d85ed3cd4..5b6efb95e5 100644 --- a/source/libs/sync/inc/raft.h +++ b/source/libs/sync/inc/raft.h @@ -65,7 +65,8 @@ struct SSyncRaft { SSyncRaftLog *log; - int maxMsgSize; + uint64_t maxMsgSize; + uint64_t maxUncommittedSize; SSyncRaftProgressTracker *tracker; ESyncState state; diff --git a/source/libs/sync/inc/raft_log.h b/source/libs/sync/inc/raft_log.h index a44f5a7273..b36f0da3be 100644 --- a/source/libs/sync/inc/raft_log.h +++ b/source/libs/sync/inc/raft_log.h @@ -19,16 +19,16 @@ #include "sync.h" #include "sync_type.h" -typedef enum SyncEntryType { +typedef enum ESyncRaftEntryType { SYNC_ENTRY_TYPE_LOG = 1, -}SyncEntryType; +} ESyncRaftEntryType; struct SSyncRaftEntry { SyncTerm term; SyncIndex index; - SyncEntryType type; + ESyncRaftEntryType type; SSyncBuffer buffer; }; diff --git a/source/libs/sync/inc/raft_message.h b/source/libs/sync/inc/raft_message.h index 2cb625d1fb..0d81511756 100644 --- a/source/libs/sync/inc/raft_message.h +++ b/source/libs/sync/inc/raft_message.h @@ -28,7 +28,7 @@ * outter message start with RAFT_MSG_*, which communicate between cluster peers, * need to implement its decode/encode functions. **/ -typedef enum RaftMessageType { +typedef enum ESyncRaftMessageType { // client propose a cmd RAFT_MSG_INTERNAL_PROP = 1, @@ -40,7 +40,7 @@ typedef enum RaftMessageType { RAFT_MSG_APPEND = 5, RAFT_MSG_APPEND_RESP = 6, -} RaftMessageType; +} ESyncRaftMessageType; typedef struct RaftMsgInternal_Prop { const SSyncBuffer *pBuf; @@ -53,14 +53,14 @@ typedef struct RaftMsgInternal_Election { } RaftMsgInternal_Election; typedef struct RaftMsg_Vote { - SyncRaftElectionType cType; + ESyncRaftElectionType cType; SyncIndex lastIndex; SyncTerm lastTerm; } RaftMsg_Vote; typedef struct RaftMsg_VoteResp { bool rejected; - SyncRaftElectionType cType; + ESyncRaftElectionType cType; } RaftMsg_VoteResp; typedef struct RaftMsg_Append_Entries { @@ -85,7 +85,7 @@ typedef struct RaftMsg_Append_Resp { } RaftMsg_Append_Resp; typedef struct SSyncMessage { - RaftMessageType msgType; + ESyncRaftMessageType msgType; SyncTerm term; SyncGroupId groupId; SyncNodeId from; @@ -131,7 +131,7 @@ static FORCE_INLINE SSyncMessage* syncInitElectionMsg(SSyncMessage* pMsg, SyncNo } static FORCE_INLINE SSyncMessage* syncNewVoteMsg(SyncGroupId groupId, SyncNodeId from, - SyncTerm term, SyncRaftElectionType cType, + SyncTerm term, ESyncRaftElectionType cType, SyncIndex lastIndex, SyncTerm lastTerm) { SSyncMessage* pMsg = (SSyncMessage*)malloc(sizeof(SSyncMessage)); if (pMsg == NULL) { @@ -153,7 +153,7 @@ static FORCE_INLINE SSyncMessage* syncNewVoteMsg(SyncGroupId groupId, SyncNodeId } static FORCE_INLINE SSyncMessage* syncNewVoteRespMsg(SyncGroupId groupId, SyncNodeId from, - SyncRaftElectionType cType, bool rejected) { + ESyncRaftElectionType cType, bool rejected) { SSyncMessage* pMsg = (SSyncMessage*)malloc(sizeof(SSyncMessage)); if (pMsg == NULL) { return NULL; @@ -213,7 +213,7 @@ static FORCE_INLINE SSyncMessage* syncNewEmptyAppendRespMsg(SyncGroupId groupId, return pMsg; } -static FORCE_INLINE bool syncIsInternalMsg(RaftMessageType msgType) { +static FORCE_INLINE bool syncIsInternalMsg(ESyncRaftMessageType msgType) { return msgType == RAFT_MSG_INTERNAL_PROP || msgType == RAFT_MSG_INTERNAL_ELECTION; } diff --git a/source/libs/sync/inc/sync_raft_impl.h b/source/libs/sync/inc/sync_raft_impl.h index 26af06866b..2f96b970dc 100644 --- a/source/libs/sync/inc/sync_raft_impl.h +++ b/source/libs/sync/inc/sync_raft_impl.h @@ -26,7 +26,7 @@ void syncRaftBecomePreCandidate(SSyncRaft* pRaft); void syncRaftBecomeCandidate(SSyncRaft* pRaft); void syncRaftBecomeLeader(SSyncRaft* pRaft); -void syncRaftStartElection(SSyncRaft* pRaft, SyncRaftElectionType cType); +void syncRaftStartElection(SSyncRaft* pRaft, ESyncRaftElectionType cType); void syncRaftTriggerHeartbeat(SSyncRaft* pRaft); @@ -35,7 +35,7 @@ bool syncRaftIsPromotable(SSyncRaft* pRaft); bool syncRaftIsPastElectionTimeout(SSyncRaft* pRaft); int syncRaftQuorum(SSyncRaft* pRaft); -SSyncRaftVoteResult syncRaftPollVote(SSyncRaft* pRaft, SyncNodeId id, +ESyncRaftVoteResult syncRaftPollVote(SSyncRaft* pRaft, SyncNodeId id, bool preVote, bool accept, int* rejectNum, int *granted); diff --git a/source/libs/sync/inc/sync_raft_progress.h b/source/libs/sync/inc/sync_raft_progress.h index fff0c13e31..6a376ad710 100644 --- a/source/libs/sync/inc/sync_raft_progress.h +++ b/source/libs/sync/inc/sync_raft_progress.h @@ -34,7 +34,7 @@ * * PROGRESS_STATE_PROBE is the initial state. **/ -typedef enum RaftProgressState { +typedef enum ESyncRaftProgressState { /** * StateProbe indicates a follower whose last index isn't known. Such a * follower is "probed" (i.e. an append sent periodically) to narrow down @@ -56,7 +56,7 @@ typedef enum RaftProgressState { * return to StateReplicate. **/ PROGRESS_STATE_SNAPSHOT, -} RaftProgressState; +} ESyncRaftProgressState; /** * Progress represents a follower’s progress in the view of the leader. Leader maintains @@ -82,7 +82,7 @@ struct SSyncRaftProgress { * When in StateSnapshot, leader should have sent out snapshot * before and stops sending any replication message. **/ - RaftProgressState state; + ESyncRaftProgressState state; /** * pendingSnapshotIndex is used in PROGRESS_STATE_SNAPSHOT. @@ -187,15 +187,15 @@ static FORCE_INLINE SyncIndex syncRaftProgressNextIndex(SSyncRaftProgress* progr return progress->nextIndex; } -static FORCE_INLINE RaftProgressState syncRaftProgressInReplicate(SSyncRaftProgress* progress) { +static FORCE_INLINE ESyncRaftProgressState syncRaftProgressInReplicate(SSyncRaftProgress* progress) { return progress->state == PROGRESS_STATE_REPLICATE; } -static FORCE_INLINE RaftProgressState syncRaftProgressInSnapshot(SSyncRaftProgress* progress) { +static FORCE_INLINE ESyncRaftProgressState syncRaftProgressInSnapshot(SSyncRaftProgress* progress) { return progress->state == PROGRESS_STATE_SNAPSHOT; } -static FORCE_INLINE RaftProgressState syncRaftProgressInProbe(SSyncRaftProgress* progress) { +static FORCE_INLINE ESyncRaftProgressState syncRaftProgressInProbe(SSyncRaftProgress* progress) { return progress->state == PROGRESS_STATE_PROBE; } diff --git a/source/libs/sync/inc/sync_raft_progress_tracker.h b/source/libs/sync/inc/sync_raft_progress_tracker.h index 887aeb2377..acfa38c378 100644 --- a/source/libs/sync/inc/sync_raft_progress_tracker.h +++ b/source/libs/sync/inc/sync_raft_progress_tracker.h @@ -87,8 +87,8 @@ struct SSyncRaftProgressTracker { SSyncRaftProgress progressMap[TSDB_MAX_REPLICA]; - SyncRaftVoteResult votes[TSDB_MAX_REPLICA]; - int maxInflight; + ESyncRaftVoteResult votes[TSDB_MAX_REPLICA]; + int maxInflightMsgs; }; SSyncRaftProgressTracker* syncRaftOpenProgressTracker(); @@ -108,6 +108,6 @@ void syncRaftRecordVote(SSyncRaftProgressTracker* tracker, int i, bool grant); * syncRaftTallyVotes returns the number of granted and rejected Votes, and whether the * election outcome is known. **/ -SyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted); +ESyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted); #endif /* _TD_LIBS_SYNC_RAFT_PROGRESS_TRACKER_H */ diff --git a/source/libs/sync/inc/sync_raft_quorum.h b/source/libs/sync/inc/sync_raft_quorum.h index 42f65c9806..16ac1cd029 100644 --- a/source/libs/sync/inc/sync_raft_quorum.h +++ b/source/libs/sync/inc/sync_raft_quorum.h @@ -17,11 +17,11 @@ #define TD_SYNC_RAFT_QUORUM_H /** - * SSyncRaftVoteResult indicates the outcome of a vote. + * ESyncRaftVoteResult indicates the outcome of a vote. **/ typedef enum { /** - * SYNC_RAFT_VOTE_PENDING indicates that the decision of the vote depends on future + * SYNC_RAFT_VOTE_PENDING indicates that the decision of the vote depends on future * votes, i.e. neither "yes" or "no" has reached quorum yet. **/ SYNC_RAFT_VOTE_PENDING = 1, @@ -35,6 +35,6 @@ typedef enum { * SYNC_RAFT_VOTE_WON indicates that the quorum has voted "yes". **/ SYNC_RAFT_VOTE_WON = 3, -} SSyncRaftVoteResult; +} ESyncRaftVoteResult; #endif /* TD_SYNC_RAFT_QUORUM_H */ \ No newline at end of file diff --git a/source/libs/sync/inc/sync_raft_quorum_joint.h b/source/libs/sync/inc/sync_raft_quorum_joint.h index 14c1f63754..dec1c39d90 100644 --- a/source/libs/sync/inc/sync_raft_quorum_joint.h +++ b/source/libs/sync/inc/sync_raft_quorum_joint.h @@ -33,6 +33,6 @@ typedef struct SSyncRaftQuorumJointConfig { * a result indicating whether the vote is pending, lost, or won. A joint quorum * requires both majority quorums to vote in favor. **/ -SyncRaftVoteResult syncRaftVoteResult(SSyncRaftQuorumJointConfig* config, const SyncRaftVoteResult* votes); +ESyncRaftVoteResult syncRaftVoteResult(SSyncRaftQuorumJointConfig* config, const ESyncRaftVoteResult* votes); #endif /* _TD_LIBS_SYNC_RAFT_QUORUM_JOINT_H */ diff --git a/source/libs/sync/inc/sync_raft_quorum_majority.h b/source/libs/sync/inc/sync_raft_quorum_majority.h index b1857ef056..6955c7a072 100644 --- a/source/libs/sync/inc/sync_raft_quorum_majority.h +++ b/source/libs/sync/inc/sync_raft_quorum_majority.h @@ -25,6 +25,6 @@ * yes/no has been reached), won (a quorum of yes has been reached), or lost (a * quorum of no has been reached). **/ -SyncRaftVoteResult syncRaftMajorityVoteResult(SSyncCluster* config, const SyncRaftVoteResult* votes); +ESyncRaftVoteResult syncRaftMajorityVoteResult(SSyncCluster* config, const ESyncRaftVoteResult* votes); #endif /* _TD_LIBS_SYNC_RAFT_QUORUM_MAJORITY_H */ diff --git a/source/libs/sync/inc/sync_type.h b/source/libs/sync/inc/sync_type.h index 525623b4cf..3925323415 100644 --- a/source/libs/sync/inc/sync_type.h +++ b/source/libs/sync/inc/sync_type.h @@ -50,7 +50,7 @@ typedef enum { SYNC_RAFT_CAMPAIGN_PRE_ELECTION = 0, SYNC_RAFT_CAMPAIGN_ELECTION = 1, SYNC_RAFT_CAMPAIGN_TRANSFER = 2, -} SyncRaftElectionType; +} ESyncRaftElectionType; typedef enum { // the init vote resp status @@ -61,6 +61,6 @@ typedef enum { //reject the vote request SYNC_RAFT_VOTE_RESP_REJECT = 2, -} SyncRaftVoteResult; +} ESyncRaftVoteResult; #endif /* _TD_LIBS_SYNC_TYPE_H */ diff --git a/source/libs/sync/src/raft.c b/source/libs/sync/src/raft.c index 20d24e3267..cdf2abebea 100644 --- a/source/libs/sync/src/raft.c +++ b/source/libs/sync/src/raft.c @@ -101,7 +101,7 @@ int32_t syncRaftStep(SSyncRaft* pRaft, const SSyncMessage* pMsg) { return 0; } - RaftMessageType msgType = pMsg->msgType; + ESyncRaftMessageType msgType = pMsg->msgType; if (msgType == RAFT_MSG_INTERNAL_ELECTION) { syncRaftHandleElectionMessage(pRaft, pMsg); } else if (msgType == RAFT_MSG_VOTE) { @@ -140,7 +140,7 @@ static bool preHandleMessage(SSyncRaft* pRaft, const SSyncMessage* pMsg) { static bool preHandleNewTermMessage(SSyncRaft* pRaft, const SSyncMessage* pMsg) { SyncNodeId leaderId = pMsg->from; - RaftMessageType msgType = pMsg->msgType; + ESyncRaftMessageType msgType = pMsg->msgType; if (msgType == RAFT_MSG_VOTE) { // TODO diff --git a/source/libs/sync/src/raft_election.c b/source/libs/sync/src/raft_election.c index 1ca3326810..eb310c31ec 100644 --- a/source/libs/sync/src/raft_election.c +++ b/source/libs/sync/src/raft_election.c @@ -18,10 +18,10 @@ #include "raft_log.h" #include "raft_message.h" -void syncRaftStartElection(SSyncRaft* pRaft, SyncRaftElectionType cType) { +void syncRaftStartElection(SSyncRaft* pRaft, ESyncRaftElectionType cType) { SyncTerm term; bool preVote; - RaftMessageType voteMsgType; + ESyncRaftMessageType voteMsgType; if (syncRaftIsPromotable(pRaft)) { syncDebug("[%d:%d] is unpromotable; campaign() should have been called", pRaft->selfGroupId, pRaft->selfId); @@ -41,7 +41,7 @@ void syncRaftStartElection(SSyncRaft* pRaft, SyncRaftElectionType cType) { } int quorum = syncRaftQuorum(pRaft); - SSyncRaftVoteResult result = syncRaftPollVote(pRaft, pRaft->selfId, preVote, true, NULL, NULL); + ESyncRaftVoteResult result = syncRaftPollVote(pRaft, pRaft->selfId, preVote, true, NULL, NULL); if (result == SYNC_RAFT_VOTE_WON) { /** * We won the election after voting for ourselves (which must mean that diff --git a/source/libs/sync/src/raft_handle_vote_resp_message.c b/source/libs/sync/src/raft_handle_vote_resp_message.c index 4ec5b88eb5..1781205ec0 100644 --- a/source/libs/sync/src/raft_handle_vote_resp_message.c +++ b/source/libs/sync/src/raft_handle_vote_resp_message.c @@ -36,7 +36,7 @@ int syncRaftHandleVoteRespMessage(SSyncRaft* pRaft, const SSyncMessage* pMsg) { return 0; } - SSyncRaftVoteResult result = syncRaftPollVote(pRaft, pMsg->from, + ESyncRaftVoteResult result = syncRaftPollVote(pRaft, pMsg->from, pMsg->voteResp.cType == SYNC_RAFT_CAMPAIGN_PRE_ELECTION, !pMsg->voteResp.rejected, &rejected, &granted); diff --git a/source/libs/sync/src/sync_raft_impl.c b/source/libs/sync/src/sync_raft_impl.c index 618efed570..4d1f66a474 100644 --- a/source/libs/sync/src/sync_raft_impl.c +++ b/source/libs/sync/src/sync_raft_impl.c @@ -127,7 +127,7 @@ int syncRaftQuorum(SSyncRaft* pRaft) { return pRaft->cluster.replica / 2 + 1; } -SSyncRaftVoteResult syncRaftPollVote(SSyncRaft* pRaft, SyncNodeId id, +ESyncRaftVoteResult syncRaftPollVote(SSyncRaft* pRaft, SyncNodeId id, bool preVote, bool grant, int* rejected, int *granted) { int voterIndex = syncRaftConfigurationIndexOfNode(pRaft, id); @@ -186,7 +186,7 @@ static int stepCandidate(SSyncRaft* pRaft, const SSyncMessage* pMsg) { * StateCandidate, we may get stale MsgPreVoteResp messages in this term from * our pre-candidate state). **/ - RaftMessageType msgType = pMsg->msgType; + ESyncRaftMessageType msgType = pMsg->msgType; if (msgType == RAFT_MSG_INTERNAL_PROP) { return 0; diff --git a/source/libs/sync/src/sync_raft_progress.c b/source/libs/sync/src/sync_raft_progress.c index 437c083b4d..df954402f8 100644 --- a/source/libs/sync/src/sync_raft_progress.c +++ b/source/libs/sync/src/sync_raft_progress.c @@ -20,13 +20,13 @@ #include "sync.h" #include "syncInt.h" -static void resetProgressState(SSyncRaftProgress* progress, RaftProgressState state); +static void resetProgressState(SSyncRaftProgress* progress, ESyncRaftProgressState state); static void probeAcked(SSyncRaftProgress* progress); static void resumeProgress(SSyncRaftProgress* progress); void syncRaftInitProgress(int i, SSyncRaft* pRaft, SSyncRaftProgress* progress) { - SSyncRaftInflights* inflights = syncRaftOpenInflights(pRaft->tracker->maxInflight); + SSyncRaftInflights* inflights = syncRaftOpenInflights(pRaft->tracker->maxInflightMsgs); if (inflights == NULL) { return; } @@ -153,7 +153,7 @@ void syncRaftProgressBecomeSnapshot(SSyncRaftProgress* progress, SyncIndex snaps * ResetState moves the Progress into the specified State, resetting ProbeSent, * PendingSnapshot, and Inflights. **/ -static void resetProgressState(SSyncRaftProgress* progress, RaftProgressState state) { +static void resetProgressState(SSyncRaftProgress* progress, ESyncRaftProgressState state) { progress->probeSent = false; progress->pendingSnapshotIndex = 0; progress->state = state; @@ -233,7 +233,7 @@ void syncRaftProgressAbortSnapshot(SSyncRaft* pRaft, int i) { progress->state = PROGRESS_STATE_PROBE; } -RaftProgressState syncRaftProgressState(SSyncRaft* pRaft, int i) { +ESyncRaftProgressState syncRaftProgressState(SSyncRaft* pRaft, int i) { return pRaft->leaderState.progress[i].state; } diff --git a/source/libs/sync/src/sync_raft_progress_tracker.c b/source/libs/sync/src/sync_raft_progress_tracker.c index 43b68a4b08..f5da538e1a 100644 --- a/source/libs/sync/src/sync_raft_progress_tracker.c +++ b/source/libs/sync/src/sync_raft_progress_tracker.c @@ -25,7 +25,7 @@ SSyncRaftProgressTracker* syncRaftOpenProgressTracker() { } void syncRaftResetVotes(SSyncRaftProgressTracker* tracker) { - memset(tracker->votes, SYNC_RAFT_VOTE_RESP_UNKNOWN, sizeof(SyncRaftVoteResult) * TSDB_MAX_REPLICA); + memset(tracker->votes, SYNC_RAFT_VOTE_RESP_UNKNOWN, sizeof(ESyncRaftVoteResult) * TSDB_MAX_REPLICA); } void syncRaftProgressVisit(SSyncRaftProgressTracker* tracker, visitProgressFp visit, void* arg) { @@ -48,7 +48,7 @@ void syncRaftRecordVote(SSyncRaftProgressTracker* tracker, int i, bool grant) { * syncRaftTallyVotes returns the number of granted and rejected Votes, and whether the * election outcome is known. **/ -SyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted) { +ESyncRaftVoteResult syncRaftTallyVotes(SSyncRaftProgressTracker* tracker, int* rejected, int *granted) { int i; SSyncRaftProgress* progress; int r, g; diff --git a/source/libs/sync/src/sync_raft_quorum_joint.c b/source/libs/sync/src/sync_raft_quorum_joint.c index a0e6a6782a..9084e1868a 100644 --- a/source/libs/sync/src/sync_raft_quorum_joint.c +++ b/source/libs/sync/src/sync_raft_quorum_joint.c @@ -22,9 +22,9 @@ * a result indicating whether the vote is pending, lost, or won. A joint quorum * requires both majority quorums to vote in favor. **/ -SyncRaftVoteResult syncRaftVoteResult(SSyncRaftQuorumJointConfig* config, const SyncRaftVoteResult* votes) { - SyncRaftVoteResult r1 = syncRaftMajorityVoteResult(&(config->majorityConfig[0]), votes); - SyncRaftVoteResult r2 = syncRaftMajorityVoteResult(&(config->majorityConfig[1]), votes); +ESyncRaftVoteResult syncRaftVoteResult(SSyncRaftQuorumJointConfig* config, const ESyncRaftVoteResult* votes) { + ESyncRaftVoteResult r1 = syncRaftMajorityVoteResult(&(config->majorityConfig[0]), votes); + ESyncRaftVoteResult r2 = syncRaftMajorityVoteResult(&(config->majorityConfig[1]), votes); if (r1 == r2) { // If they agree, return the agreed state. diff --git a/source/libs/sync/src/sync_raft_quorum_majority.c b/source/libs/sync/src/sync_raft_quorum_majority.c index ea543d7335..0d13998112 100644 --- a/source/libs/sync/src/sync_raft_quorum_majority.c +++ b/source/libs/sync/src/sync_raft_quorum_majority.c @@ -22,7 +22,7 @@ * yes/no has been reached), won (a quorum of yes has been reached), or lost (a * quorum of no has been reached). **/ -SyncRaftVoteResult syncRaftMajorityVoteResult(SSyncCluster* config, const SyncRaftVoteResult* votes) { +ESyncRaftVoteResult syncRaftMajorityVoteResult(SSyncCluster* config, const ESyncRaftVoteResult* votes) { if (config->replica == 0) { return SYNC_RAFT_VOTE_WON; }