diff --git a/raft/raft.go b/raft/raft.go index e9d8e6b9f..04b4ac97a 100644 --- a/raft/raft.go +++ b/raft/raft.go @@ -59,8 +59,12 @@ type progress struct { } 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) { diff --git a/raft/raft_test.go b/raft/raft_test.go index d29fa3548..8f51250f0 100644 --- a/raft/raft_test.go +++ b/raft/raft_test.go @@ -47,6 +47,34 @@ func (r *raft) readMessages() []pb.Message { 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) { tests := []struct { m uint64