Skip to content

Commit

Permalink
fix comments and remove extra fields
Browse files Browse the repository at this point in the history
  • Loading branch information
dhiaayachi committed Mar 26, 2024
1 parent 43974d2 commit 7543a3d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 27 deletions.
4 changes: 0 additions & 4 deletions commands.go
Expand Up @@ -92,7 +92,6 @@ type RequestVoteRequest struct {
// transfer. It is required for leadership transfer to work, because servers
// wouldn't vote otherwise if they are aware of an existing leader.
LeadershipTransfer bool
PreVote bool
}

// GetRPCHeader - See WithRPCHeader.
Expand All @@ -114,9 +113,6 @@ type RequestVoteResponse struct {

// Is the vote granted.
Granted bool

// Is it a preVote response
PreVote bool
}

// GetRPCHeader - See WithRPCHeader.
Expand Down
41 changes: 19 additions & 22 deletions raft.go
Expand Up @@ -325,8 +325,7 @@ func (r *Raft) runCandidate() {
r.mainThreadSaturation.working()
r.processRPC(rpc)
case preVote := <-prevoteCh:
// This a pre-preVote case but could lead to winning an election, in the case that majority of other nodes
// don't support pre-preVote or have pre-preVote deactivated.
// This a pre-vote case it should trigger a "real" election if the pre-vote is won.
r.mainThreadSaturation.working()
r.logger.Debug("got a prevote!!", "from", preVote.voterID, "term", preVote.Term, "tally", preVoteGrantedVotes)
// Check if the term is greater than ours, bail
Expand All @@ -346,7 +345,7 @@ func (r *Raft) runCandidate() {
r.logger.Debug("prevote refused", "from", preVote.voterID, "term", preVote.Term, "tally", preVoteGrantedVotes)
}

// Check if we've won the pre-preVote and proceed to election if so
// Check if we've won the pre-vote and proceed to election if so
if preVoteGrantedVotes >= votesNeeded {
r.logger.Info("pre election won", "term", preVote.Term, "tally", preVoteGrantedVotes, "votesNeeded", votesNeeded-1)
preVoteGrantedVotes = 0
Expand All @@ -355,7 +354,7 @@ func (r *Raft) runCandidate() {
prevoteCh = nil
voteCh = r.electSelf()
}
// Check if we've lost the pre-Vote and wait for the election to timeout so we can do another time of
// Check if we've lost the pre-vote and wait for the election to timeout so we can do another time of
// prevote.
if preVoteRefusedVotes >= votesNeeded {
r.logger.Info("pre election lost, wait for election to timeout", "term", preVote.Term, "tally", preVoteGrantedVotes, "votesNeeded", votesNeeded-1)
Expand All @@ -373,7 +372,7 @@ func (r *Raft) runCandidate() {
// Check if the preVote is granted
if vote.Granted {
grantedVotes++
r.logger.Debug("v ote granted", "from", vote.voterID, "term", vote.Term, "tally", grantedVotes)
r.logger.Debug("vote granted", "from", vote.voterID, "term", vote.Term, "tally", grantedVotes)
}

// Check if we've become the leader
Expand Down Expand Up @@ -1762,16 +1761,16 @@ func (r *Raft) requestPreVote(rpc RPC, req *RequestPreVoteRequest) {

// For older raft version ID is not part of the packed message
// We assume that the peer is part of the configuration and skip this check
if len(req.ID) > 0 {
candidateID := ServerID(req.ID)
// if the Servers list is empty that mean the cluster is very likely trying to bootstrap,
// Grant the vote
if len(r.configurations.latest.Servers) > 0 && !inConfiguration(r.configurations.latest, candidateID) {
r.logger.Warn("rejecting pre-vote request since node is not in configuration",
"from", candidate)
return
}

candidateID := ServerID(req.ID)
// if the Servers list is empty that mean the cluster is very likely trying to bootstrap,
// Grant the vote
if len(r.configurations.latest.Servers) > 0 && !inConfiguration(r.configurations.latest, candidateID) {
r.logger.Warn("rejecting pre-vote request since node is not in configuration",
"from", candidate)
return
}

if leaderAddr, leaderID := r.LeaderWithID(); leaderAddr != "" && leaderAddr != candidate && !req.LeadershipTransfer {
r.logger.Warn("rejecting pre-vote request since we have a leader",
"from", candidate,
Expand All @@ -1787,23 +1786,21 @@ func (r *Raft) requestPreVote(rpc RPC, req *RequestPreVoteRequest) {

// Increase the term if we see a newer one
if req.Term > r.getCurrentTerm() {
// Ensure we grant the pre-vote as in a "real" vote this will transition us to follower
// continue processing here to possibly grant the pre-vote as in a "real" vote this will transition us to follower
r.logger.Debug("received a requestPreVote with a newer term, grant the pre-vote")
resp.Term = req.Term
resp.Granted = true
}

// if we get a request for vote from a nonVoter and the request term is higher,
// step down and update term, but reject the vote request
// This could happen when a node, previously voter, is converted to non-voter
// The reason we need to step in is to permit to the cluster to make progress in such a scenario
// More details about that in https://github.com/hashicorp/raft/pull/526
if len(req.ID) > 0 {
candidateID := ServerID(req.ID)
if len(r.configurations.latest.Servers) > 0 && !hasVote(r.configurations.latest, candidateID) {
r.logger.Warn("rejecting pre-vote request since node is not a voter", "from", candidate)
return
}

candidateID := ServerID(req.ID)

Check failure on line 1800 in raft.go

View workflow job for this annotation

GitHub Actions / go-fmt-and-vet

no new variables on left side of :=
if len(r.configurations.latest.Servers) > 0 && !hasVote(r.configurations.latest, candidateID) {
r.logger.Warn("rejecting pre-vote request since node is not a voter", "from", candidate)
return
}

// Reject if their term is older
Expand Down
2 changes: 1 addition & 1 deletion raft_test.go
Expand Up @@ -2143,7 +2143,7 @@ func TestRaft_PreVoteAvoidElectionWithPartition(t *testing.T) {
c.FullyConnect()
time.Sleep(3 * c.propagateTimeout)

// Check that the number of followers increase and
// Check that the number of followers increase and the term is not increased
require.Len(t, c.Followers(), 4)
leaderTerm = c.Leader().getCurrentTerm()
require.Equal(t, leaderTerm, oldLeaderTerm)
Expand Down

0 comments on commit 7543a3d

Please sign in to comment.