← Back to Pull requests

fix: log-store: keep the anchor entry in truncate_after #2

Closed
unknown closed this pull request without merging on Nov 01, 2026 01:13 UTC
View on GitHub
unknown unknown commented on Oct 31, 2026 15:58 UTC
Author

Finding

P0 — truncate_after deletes one entry too many, leaving a hole in the raft log.

src/raft/store/log_store.rs:267-283 builds the range-delete start from the
anchor itself:

let from = match log_id {
  Some(ref id) => id_to_bin(id.index()),   // should be id.index() + 1
  None => id_to_bin(0),
};
db.delete_range_cf(&self.cf_logs(), &from, &to)

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_5 keeps indexes 0..=5 after
truncate_after(Some(log_id(1,5))).

The engine depends on it. On a follower conflict,
FollowingHandler::truncate_logs(since) emits
Command::TruncateLog { after: prev_log_id(since) }
(src/engine/handler/following_handler/mod.rs:170-183), i.e. the anchor is the
last entry the follower shares with the leader — normally a committed one —
and it must survive so that the entries the leader appends from since slot in
directly 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_id
alone and never re-reads an entry it has already accepted, so the follower
believes it holds since-1 while the key is gone. When the apply stream later
reads [last_applied+1, committed], the missing entry is silently skipped and
last_applied jumps past it. Result: the follower's state machine permanently
lacks 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..=10 to a RocksLogStore.
2. Call truncate_after(Some(log_id(1,1,5))) — openraft's contract says
entries 1..=5 must remain.
3. Read the log back:

$ cargo test --lib test_truncate_after_keeps_the_anchor_entry   # after this PR's test, before the fix
panicked at src/raft/store/log_store.rs:
assertion `left == right` failed: the anchor entry must survive
  left: [1, 2, 3, 4]
 right: [1, 2, 3, 4, 5]

The pre-existing test_raft_log_operations encoded the buggy behaviour
(truncate_after(11) was expected to leave 10 entries and to make index 11
unreadable), which is why the unit suite never caught it.

The fix

  • from = id_to_bin(id.index() + 1) for the Some case, so the range delete

  • covers (log_id, +oo); truncate_after(None) still clears everything.
  • Added test_truncate_after_keeps_the_anchor_entry, which asserts the anchor

  • survives, that get_log_state().last_log_id is the anchor, and that
    truncate_after(None) still empties the log.
  • Updated the two test_raft_log_operations expectations 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 warnings and
    cargo test --lib (83 tests) all pass.

    ---

    Changelog

    ##### fix: log-store: keep the anchor entry in truncate_after

    Summary

    RocksLogStore::truncate_after deleted the entry named by its log_id
    argument 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_5 keeps indexes 0..=5 after
    truncate_after(log_id(1,5))). The engine relies on it: when a follower's log
    conflicts with the leader, FollowingHandler::truncate_logs emits
    Command::TruncateLog { after: prev_log_id(since) }, so the anchor is the last
    entry 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_id alone
    -- 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, and None remains the only way to
    ask for that.

    Diff: 541 lines (+405 / -136) across 6 files Reviewer: lichuang Review duration: 554m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.0% (MEDIUM) · Higher = larger blast radius
    48.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 18.9% · Safe to merge
    18.9%