Replies: 1 comment 3 replies
-
What if the node with high random number is failed just before starting re-election i.e in your Specific Node Re-Voting Process? |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
For the Raft algorithm, the leader election process is as follows:
Node Roles
Nodes in the Raft algorithm have three roles:
(1) Leader: Responsible for handling client requests, managing log replication, and sending heartbeats.
(2) Follower: Passively accepts logs and heartbeats from the Leader and does not initiate requests.
(3) Candidate: During the election process, a Follower can transition to a Candidate and initiate an election.
Election Trigger Conditions
Leader elections are typically triggered under the following conditions:
(1) Leader failure: A Follower does not receive a heartbeat from the Leader within a certain period (election timeout).
(2) New node joining: A newly joined node may trigger an election.
(3) Partition recovery: An election may be triggered after network partition recovery.
Election Process
The election process consists of the following steps:
(1) Follower Transitions to Candidate
When a Follower does not receive a heartbeat from the Leader within the election timeout period, it considers the Leader to have failed.
The Follower transitions its role to Candidate and initiates a new election.
(2) Candidate Initiates Voting
The Candidate first increments its term number (Term) by 1, indicating a new round of elections.
The Candidate sends a RequestVote RPC to other nodes in the cluster to request votes.
The Candidate votes for itself.
(3) Other Nodes Vote
Upon receiving the RequestVote RPC, nodes check the following conditions:
• Whether the Candidate's Term is greater than or equal to their own Term.
• Whether they have already voted for another Candidate.
• Whether the Candidate's log is at least as up-to-date as their own.
If the conditions are met, the node votes for the Candidate and resets its election timeout.
(4) Election Result
If the Candidate receives votes from a majority of nodes, it becomes the new Leader.
The new Leader immediately sends heartbeats to other nodes to prevent them from initiating new elections.
(5) Election Failure
If the Candidate does not receive enough votes within the election timeout period, the election fails.
The Candidate waits for a random period before re-initiating the election (to avoid split votes caused by multiple nodes initiating elections simultaneously).
Election Timeout
(1) To prevent multiple nodes from initiating elections simultaneously, Raft uses randomized election timeout periods (typically 150ms-300ms).
(2) Randomized timeout periods reduce the probability of election conflicts.
Log Consistency Guarantee
(1) During the election process, Raft ensures that only nodes with sufficiently up-to-date logs can become Leaders by comparing the Term and Index of logs.
(2) This guarantees that the Leader's log contains all committed log entries, ensuring consistency.
Election Optimization
(1) Pre-Vote Mechanism: Before formally initiating an election, a Candidate can first send a Pre-Vote request to confirm whether it has a chance to win the election, avoiding unnecessary Term increments.
(2) Leader Transfer: In certain situations, the Leader can proactively transfer leadership to another node to avoid frequent elections.
Modified Election Process:
The election process is divided into the following steps:
(1) Follower Transitions to Candidate (unchanged)
(2) Candidate Initiates Voting (modified)
(3) Other Nodes Vote
• Whether the Candidate's Term is greater than or equal to their own Term.
• Whether they have already voted for another Candidate.
• Whether the Candidate's log is at least as up-to-date as their own.
• The Candidate's Term is greater than or equal to their own Term.
• The Candidate's log offset is at least as up-to-date as their own.
(4) Voting Result (Receiving Rejected Votes)
• If the message informs the voting node that the log is outdated,
• If the message informs the voting node that it has already voted for another node, then the Candidate node will record these nodes in a list (VoteOtherList) until the number of rejected nodes exceeds a majority (meaning the node cannot be elected according to the standard Raft algorithm, in which case it needs to self-rescue). If the Candidate's nodePriority is less than the recorded maxNodePriority or less than the maximum nodePriority in VoteOtherList, the node will initiate a specific node re-voting process.
• Actively relinquish the Candidate role for this round.
• Send a message to all nodes that voted for it, requesting them to re-vote (the message includes the Candidate node with the maximum nodePriority recorded, obtained from VoteOtherList and maxNodePriority).
• Upon receiving this message, nodes compare the maxNodePriority in the message with the locally recorded maxNodePriority and send a vote to the node with the larger value.
• The specific node re-voting process aims to concentrate votes on the node with the highest nodePriority, thereby completing the election process.
(5) Voting Result (Receiving an Accepted Vote)
(6) Election Failure
Leader Election Examples
Example 1: Raft Election Succeeds in One Round
Assume a 5-node cluster (A, B, C, D, E):
Example 2: Raft Election Fails in One Round
Example 3: Another Raft Scenario
Correctness of the Idea
At any moment, each node will only vote for one Candidate, so at any moment, there is at most one node with votes from a majority of nodes.
Efficiency
Under normal Raft operation, the new modifications have no impact. However, when the Raft algorithm cannot elect a Leader in one election round, the new modifications may facilitate the election of a Leader.
Thank you for your opinions, and welcome to talk about it.
Beta Was this translation helpful? Give feedback.
All reactions