fix: log-store: keep the anchor entry in truncate_after #2
All Vouch review confidence checks passed
Residual risk 0.19 = Change Risk (0.48) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 541 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.377
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #2"
general
weight: 0.35
lichuang
Finding
P0 —
truncate_afterdeletes one entry too many, leaving a hole in the raft log.src/raft/store/log_store.rs:267-283builds the range-delete start from theanchor itself:
openraft defines this method as "Truncate logs after
last_log_id,exclusive" (
openraft-0.10.0-alpha.14,src/storage/v2/raft_log_storage.rs:134)and its own storage conformance suite asserts that shape —
testing/log/suite.rs::delete_logs_after_5keeps indexes0..=5aftertruncate_after(Some(log_id(1,5))).The engine depends on it. On a follower conflict,
FollowingHandler::truncate_logs(since)emitsCommand::TruncateLog { after: prev_log_id(since) }(
src/engine/handler/following_handler/mod.rs:170-183), i.e. the anchor is thelast entry the follower shares with the leader — normally a committed one —
and it must survive so that the entries the leader appends from
sinceslot indirectly behind it.
Deleting the anchor violates the trait's "must not leave a hole in logs"
clause. The hole is invisible: openraft tracks log liveness by
last_log_idalone and never re-reads an entry it has already accepted, so the follower
believes it holds
since-1while the key is gone. When the apply stream laterreads
[last_applied+1, committed], the missing entry is silently skipped andlast_appliedjumps past it. Result: the follower's state machine permanentlylacks a committed write, and if that follower later wins an election it can
commit on top of that state — cluster-wide data loss.
How to reproduce
1. Append entries
1..=10to aRocksLogStore.2. Call
truncate_after(Some(log_id(1,1,5)))— openraft's contract saysentries
1..=5must remain.3. Read the log back:
The pre-existing
test_raft_log_operationsencoded the buggy behaviour(
truncate_after(11)was expected to leave 10 entries and to make index 11unreadable), which is why the unit suite never caught it.
The fix
from = id_to_bin(id.index() + 1)for theSomecase, so the range deletecovers
(log_id, +oo);truncate_after(None)still clears everything.test_truncate_after_keeps_the_anchor_entry, which asserts the anchorsurvives, that
get_log_state().last_log_idis the anchor, and thattruncate_after(None)still empties the log.test_raft_log_operationsexpectations to the contract(11 entries survive; index 11 stays readable).
Verified: the new test fails with the old line and passes with the new one;
cargo fmt --all -- --check,cargo clippy --all-features -- -D warningsandcargo test --lib(83 tests) all pass.---
Changelog
##### fix: log-store: keep the anchor entry in truncate_after
Summary
RocksLogStore::truncate_afterdeleted the entry named by itslog_idargument as well as every entry after it, so truncating a log to the last entry
shared with the leader removed that shared entry too.
Details
openraft documents the method as "Truncate logs after
last_log_id,exclusive" and its log-store conformance suite asserts exactly that
(
testing/log/suite.rs::delete_logs_after_5keeps indexes 0..=5 aftertruncate_after(log_id(1,5))). The engine relies on it: when a follower's logconflicts with the leader,
FollowingHandler::truncate_logsemitsCommand::TruncateLog { after: prev_log_id(since) }, so the anchor is the lastentry the two logs agree on and it must survive.
Deleting it breaks the trait's "must not leave a hole in logs" clause. openraft
never re-reads an accepted entry -- it tracks liveness with
last_log_idalone-- so the hole is invisible to the engine and shows up only as a committed
entry that the follower never applies. A follower that later wins an election
can then commit on top of a state machine that is missing a committed write.
truncate_after(None)still clears the log, andNoneremains the only way toask for that.