Browse Source

Merge pull request #5366 from xiang90/fix_restore

raft: do not panic when removing all the nodes from cluster
Xiang Li 9 years ago
parent
commit
a663828a32
2 changed files with 13 additions and 0 deletions
  1. 6 0
      raft/raft.go
  2. 7 0
      raft/raft_test.go

+ 6 - 0
raft/raft.go

@@ -893,6 +893,12 @@ func (r *raft) addNode(id uint64) {
 func (r *raft) removeNode(id uint64) {
 	r.delProgress(id)
 	r.pendingConf = false
+
+	// do not try to commit or abort transferring if there is no nodes in the cluster.
+	if len(r.prs) == 0 {
+		return
+	}
+
 	// The quorum size is now smaller, so see if any pending entries can
 	// be committed.
 	if r.maybeCommit() {

+ 7 - 0
raft/raft_test.go

@@ -1782,6 +1782,13 @@ func TestRemoveNode(t *testing.T) {
 	if g := r.nodes(); !reflect.DeepEqual(g, w) {
 		t.Errorf("nodes = %v, want %v", g, w)
 	}
+
+	// remove all nodes from cluster
+	r.removeNode(1)
+	w = []uint64{}
+	if g := r.nodes(); !reflect.DeepEqual(g, w) {
+		t.Errorf("nodes = %v, want %v", g, w)
+	}
 }
 
 func TestPromotable(t *testing.T) {