From 54925de9354717ec7937797d260c41dc55c7ff6c Mon Sep 17 00:00:00 2001 From: Pavel Kalinnikov Date: Sat, 3 Feb 2024 17:44:28 +0000 Subject: [PATCH] log: consolidate the commited entry rewrite check The append function call right after this does the same check, we don't need to do this in two places. TestLogMaybeAppend exercises these panics, and confirms the behaviour is the same after the first panic is removed. Signed-off-by: Pavel Kalinnikov --- log.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/log.go b/log.go index a9416caf..f1b02d67 100644 --- a/log.go +++ b/log.go @@ -109,9 +109,6 @@ func (l *raftLog) maybeAppend(a logSlice, committed uint64) (lastnewi uint64, ok if !ok { return 0, false } - if match.index < a.lastIndex() && match.index < l.committed { - l.logger.Panicf("entry %d is already committed [committed(%d)]", match.index+1, l.committed) - } // Fast-forward to the first mismatching or missing entry. // NB: prev.index <= match.index <= a.lastIndex(), so the sub-slicing is safe. @@ -129,8 +126,8 @@ func (l *raftLog) append(ents ...pb.Entry) uint64 { if len(ents) == 0 { return l.lastIndex() } - if after := ents[0].Index - 1; after < l.committed { - l.logger.Panicf("after(%d) is out of range [committed(%d)]", after, l.committed) + if mismatch := ents[0].Index; mismatch <= l.committed { + l.logger.Panicf("entry %d is already committed [committed(%d)]", mismatch, l.committed) } l.unstable.truncateAndAppend(ents) return l.lastIndex()