Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignore non-voters in leadership transfer #398

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions raft.go
Original file line number Diff line number Diff line change
Expand Up @@ -1734,13 +1734,13 @@ func (r *Raft) lookupServer(id ServerID) *Server {
return nil
}

// pickServer returns the follower that is most up to date. Because it accesses
// leaderstate, it should only be called from the leaderloop.
// pickServer returns the follower that is most up to date and participating in quorum.
// Because it accesses leaderstate, it should only be called from the leaderloop.
func (r *Raft) pickServer() *Server {
var pick *Server
var current uint64
for _, server := range r.configurations.latest.Servers {
if server.ID == r.localID {
if server.ID == r.localID || server.Suffrage != Voter {
continue
}
state, ok := r.leaderState.replState[server.ID]
Expand Down
23 changes: 23 additions & 0 deletions raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,29 @@ func TestRaft_LeadershipTransferLeaderReplicationTimeout(t *testing.T) {
}
}

func TestRaft_LeadershipTransferIgnoresNonvoters(t *testing.T) {
c := MakeCluster(2, t, nil)
defer c.Close()

follower := c.Followers()[0]

demoteFuture := c.Leader().DemoteVoter(follower.localID, 0, 0)
if demoteFuture.Error() != nil {
t.Fatalf("demote voter err'd: %v", demoteFuture.Error())
}

future := c.Leader().LeadershipTransfer()
if future.Error() == nil {
t.Fatal("leadership transfer should err")
}

expected := "cannot find peer"
actual := future.Error().Error()
if !strings.Contains(actual, expected) {
t.Errorf("leadership transfer should err with: %s", expected)
}
}

func TestRaft_LeadershipTransferStopRightAway(t *testing.T) {
r := Raft{leaderState: leaderState{}}
r.setupLeaderState()
Expand Down