I am trying to implement the Raft consensus algorithm. Here is my general understanding around all the times we set the term
and votedFor
attributes of a server's state:
- Upon startup
term
is 0 andvotedFor
isnull
- Upon a server's election timeout, the server becomes a Candidate, increments its
term
by 1, and sets itsvotedFor
to itself. - When a server receives a
RequestVote
RPC with aterm
higher than its own, it should update theterm
to the number observed, then updatevotedFor
to the sender iff the receiving server has avotedFor
ofnull
and its log is not more up-to-date than the sender's log. - When a Candidate receives an
AppendEntries
RPC, and the sender'sterm
is higher than or equal to its own, the Candidate should update itsterm
to the sender'sterm
then set itsvotedFor
to the sender and have it's state become Follower, thereby acknowledging the sender as its legitimate leader. - In all other cases when a server receives any RPC request or response containing a
term
higher than its own, it should set its ownterm
to the received server'sterm
and setvotedFor
tonull
.
Together, do these make up the only 5 ways that term
and votedFor
are set in a correct implementation of Raft, and are these cases correctly summarized? I am confused about this because the paper only mentions that at certain times a node will go through a conversion to a follower which does not specify whether votedFor
should be the value of the sender with the higher term or null
. I am worried that case 4 is wrong and should be this instead: on AppendEntries
if the sender's term is greater, then the receiver should update their term
to that of the sender, then set votedFor
to the sender, regardless of whether or not the receiver is a Follower, Candidate, or stale Leader.