Browse Source

Merge pull request #1761 from xiang90/fix_raft

raft: should not decrease match and next when handling out of order msgAppResp
Xiang Li 11 years ago
parent
commit
66c30f28d6
2 changed files with 34 additions and 2 deletions
  1. 6 2
      raft/raft.go
  2. 28 0
      raft/raft_test.go

+ 6 - 2
raft/raft.go

@@ -59,8 +59,12 @@ type progress struct {
 }
 }
 
 
 func (pr *progress) update(n uint64) {
 func (pr *progress) update(n uint64) {
-	pr.match = n
-	pr.next = n + 1
+	if pr.match < n {
+		pr.match = n
+	}
+	if pr.next < n+1 {
+		pr.next = n + 1
+	}
 }
 }
 
 
 func (pr *progress) optimisticUpdate(n uint64) {
 func (pr *progress) optimisticUpdate(n uint64) {

+ 28 - 0
raft/raft_test.go

@@ -47,6 +47,34 @@ func (r *raft) readMessages() []pb.Message {
 	return msgs
 	return msgs
 }
 }
 
 
+func TestProgressUpdate(t *testing.T) {
+	prevM, prevN := uint64(3), uint64(5)
+	tests := []struct {
+		update uint64
+
+		wm uint64
+		wn uint64
+	}{
+		{prevM - 1, prevM, prevN},         // do not decrease match, next
+		{prevM, prevM, prevN},             // do not decrease next
+		{prevM + 1, prevM + 1, prevN},     // increase match, do not decrease next
+		{prevM + 2, prevM + 2, prevN + 1}, // increase match, next
+	}
+	for i, tt := range tests {
+		p := &progress{
+			match: prevM,
+			next:  prevN,
+		}
+		p.update(tt.update)
+		if p.match != tt.wm {
+			t.Errorf("#%d: match=%d, want %d", i, p.match, tt.wm)
+		}
+		if p.next != tt.wn {
+			t.Errorf("#%d: next=%d, want %d", i, p.next, tt.wn)
+		}
+	}
+}
+
 func TestProgressMaybeDecr(t *testing.T) {
 func TestProgressMaybeDecr(t *testing.T) {
 	tests := []struct {
 	tests := []struct {
 		m  uint64
 		m  uint64