Skip to content

Commit

Permalink
raft: never remove the last voter
Browse files Browse the repository at this point in the history
Before this change, it was possible for a Raft group to apply a
configuration change that removes the last voter. Such a group
is stuck and it's unclear that this operation is ever useful.
On the flip side, it makes an awkward edge case to allow, one
that will be more awkward in the context of joint consensus.

Remove it and thereby reduce the mental overhead of reasoning
about configuration changes.
  • Loading branch information
tbg committed Jul 9, 2019
1 parent eb7dd97 commit 99ae34d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
22 changes: 13 additions & 9 deletions raft/raft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1139,10 +1139,16 @@ func TestCommit(t *testing.T) {
storage.Append(tt.logs)
storage.hardState = pb.HardState{Term: tt.smTerm}

sm := newTestRaft(1, []uint64{1}, 10, 2, storage)
sm.prs.RemoveAny(1)
var ids []uint64
for j := 0; j < len(tt.matches); j++ {
sm.prs.InitProgress(uint64(j)+1, tt.matches[j], tt.matches[j]+1, false)
ids = append(ids, uint64(j+1))
}
sm := newTestRaft(1, ids, 10, 2, storage)

for j := 0; j < len(tt.matches); j++ {
id := uint64(j + 1)
sm.prs.Progress[id].Match = tt.matches[j]
sm.prs.Progress[id].Next = tt.matches[j] + 1
}
sm.maybeCommit()
if g := sm.raftLog.committed; g != tt.w {
Expand Down Expand Up @@ -3142,9 +3148,8 @@ func TestRemoveNode(t *testing.T) {
t.Errorf("nodes = %v, want %v", g, w)
}

// remove all nodes from cluster
// The last remaining node will refuse to remove itself.
r.applyConfChange(pb.ConfChange{NodeID: 1, Type: pb.ConfChangeRemoveNode})
w = []uint64{}
if g := r.prs.VoterNodes(); !reflect.DeepEqual(g, w) {
t.Errorf("nodes = %v, want %v", g, w)
}
Expand All @@ -3160,14 +3165,13 @@ func TestRemoveLearner(t *testing.T) {
t.Errorf("nodes = %v, want %v", g, w)
}

w = []uint64{}
if g := r.prs.LearnerNodes(); !reflect.DeepEqual(g, w) {
if w, g := []uint64{}, r.prs.LearnerNodes(); !reflect.DeepEqual(g, w) {
t.Errorf("nodes = %v, want %v", g, w)
}

// remove all nodes from cluster
// The remaining voter will refuse to remove itself.
r.applyConfChange(pb.ConfChange{NodeID: 1, Type: pb.ConfChangeRemoveNode})
if g := r.prs.VoterNodes(); !reflect.DeepEqual(g, w) {
if w, g := []uint64{1}, r.prs.VoterNodes(); !reflect.DeepEqual(g, w) {
t.Errorf("nodes = %v, want %v", g, w)
}
}
Expand Down
5 changes: 5 additions & 0 deletions raft/tracker/tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func (p *ProgressTracker) RemoveAny(id uint64) {
panic(fmt.Sprintf("peer %x is both voter and learner", id))
}

if okV1 && len(p.Voters[0]) == 1 {
// Never remove the last voter.
return
}

delete(p.Voters[0], id)
delete(p.Voters[1], id)
delete(p.Learners, id)
Expand Down

0 comments on commit 99ae34d

Please sign in to comment.