From c18d79df3770437b907b3e77e92df9bbe09a838c Mon Sep 17 00:00:00 2001 From: Nathan VanBenschoten Date: Sun, 25 Sep 2022 20:46:03 -0400 Subject: [PATCH] raft: clarify conditions in unstable.stableTo No change in behavior, but clarify interaction with unstable snapshot. Signed-off-by: Nathan VanBenschoten --- raft/log_unstable.go | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/raft/log_unstable.go b/raft/log_unstable.go index 230fd21f9..73641b7a9 100644 --- a/raft/log_unstable.go +++ b/raft/log_unstable.go @@ -75,16 +75,20 @@ func (u *unstable) maybeTerm(i uint64) (uint64, bool) { func (u *unstable) stableTo(i, t uint64) { gt, ok := u.maybeTerm(i) if !ok { + // Unstable entry missing. Ignore. return } - // if i < offset, term is matched with the snapshot - // only update the unstable entries if term is matched with - // an unstable entry. - if gt == t && i >= u.offset { - u.entries = u.entries[i+1-u.offset:] - u.offset = i + 1 - u.shrinkEntriesArray() + if i < u.offset { + // Index matched unstable snapshot, not unstable entry. Ignore. + return } + if gt != t { + // Term mismatch between unstable entry and specified entry. Ignore. + return + } + u.entries = u.entries[i+1-u.offset:] + u.offset = i + 1 + u.shrinkEntriesArray() } // shrinkEntriesArray discards the underlying array used by the entries slice