0

i have a doubt in undersanting the leader election algorithm of the Raft algorithm. In the paper i read that each node con give only one vote for each term. My doubt is because the terms in each node can be different, what is referred is the term of the node asking for vote or the term of the node giving the node ? Thanks

pinotto
  • 45
  • 7

1 Answers1

2

In a raft cluster, let's have a node that wants to become the leader and call it a candidate. All other nodes, including the current leader, we'll simply call peers.

Let's see what happens when a peer receives a RequestVoteRPC from a candidate.

If the term of the peer is greater than the one received in the request (candidates term), the vote will be denied. The response will contain the term of the peer, so the candidate will convert to the follower state and update it's term as soon as it receives the response. This will ensure that the term of the peer asking for a vote is the same as the term of the peer giving the vote.

Otherwise the peer needs to decide what to do.

If the term of the peer is less than the one received in the request (candidates term), this peer will convert to a follower and will update it's current term to the one received from the candidate, before continuing with the decision. This will ensure that the term of the peer asking for a vote is the same as the term of the peer giving the vote.

There is a chance that multiple peers converted to the candidate state, and the peer we're observing already voted for a candidate. In that case, the votedFor variable will not be null and we can't grant the vote to the second (or any other) candidate. But we also need to be careful here because we can receive a duplicate RequestVoteRPC request (from the same candidate) and because of that we need to check if votedFor is equal to the candidate id whose request we're observing. If votedFor is equal to the current candidate id, we'll grant the vote.

To summarize, we'll grant the vote if the votedFor variable is null or equal to the candidate id.

I purposely excluded the condition that in order to grant the vote, the candidate log needs to be at least up to date in the example, but don't forget that this is also important.

Each peer can grant one vote during one term and since the terms will sync during the communication, the term of the peer giving the vote will be the same as the term asking for a vote.

msantl
  • 371
  • 2
  • 6
  • "To summarize, we'll grant the vote if the votedFor variable is null or equal to the candidate id" or the term of the candidate(asking for vote) is higher of the term of the peer. It is correct to say so? – pinotto Feb 11 '20 at 17:03
  • If the term of the candidate (asking for vote) is higher than the term of the peer, the peer will "reset" it's `votedFor` variable during the transition to the follower together with setting the term to the one of the candidate, and because of that the peer will find itself in a state where the term of the peer is the same as the term of the candidate and because it didn't vote for anyone yet, the vote can be granted to the candidate. In the end, what you're suggesting is correct. – msantl Feb 11 '20 at 18:39
  • "If the term of the peer is greater than the one received in the request (candidates term), the vote will be denied. The response will contain the term of the peer, so the candidate will convert to the follower state and update it's term as soon as it receives the response. This will ensure that the term of the peer asking for a vote is the same as the term of the peer giving the vote." In this case the candidate that now is become a follower, also grant his vote for the other node or only means that it will discard the responses from his others RequestVoteRPC made towards the others nodes? – pinotto Feb 11 '20 at 20:05
  • The candidate that started an election with a term number that is smaller than the term currently in the cluster, will become a follower of the current leader in the cluster. That peer won't be granting his vote because the election was already held. The responses from other `RequestVoteRPC`, at least in the majority` should contain the same response (the vote is denied and the current term of the cluster), but if any other node granted the vote, it should be ignored after the switch to the follower mode. – msantl Feb 12 '20 at 12:33