← Back to Pull requests

fix: state-machine: stage metadata into the apply batch #6

Closed
unknown closed this pull request without merging on Nov 02, 2026 08:36 UTC
View on GitHub
unknown unknown commented on Oct 31, 2026 16:09 UTC
Author

Finding

P1 — metadata escapes the apply WriteBatch, breaking the atomicity the state
machine promises.

apply() buffers every KV write into one WriteBatch and commits it once, but
three metadata paths write straight to RocksDB and mutate the in-memory cache
immediately:

  • Cmd::AddNode → add_node() (put_cf(NODES_KEY) + cache update),

  • Cmd::RemoveNode → remove_node(),

  • EntryPayload::Membership → set_last_membership()

  • (put_cf(LAST_MEMBERSHIP_KEY) + put_cf(NODES_KEY) + cache update).

    So within one batch, the metadata is durable before the data is, and the cache
    is visible before either is. If db.write(batch) fails (disk full, I/O error)
    the process is left with a node table and a membership that describe entries
    that were never applied, and last_applied still pointing at the previous
    position. On restart those entries are replayed on top of metadata that already
    includes them -- harmless for the idempotent membership write, but the module
    comment's invariant ("every mutation updates both this cache and RocksDB
    atomically") does not hold across the batch.

    How to reproduce

    Force the batch write to fail (e.g. run with a read-only DB directory, or
    inject a failure between the metadata write and the commit) and apply one batch
    containing Cmd::AddNode followed by a KV write, then reopen:

  • get_nodes() (backed by _sm_meta) already lists the node,

  • last_applied and the KV data show the entry was never applied.
  • Without fault injection the same inconsistency shows up as a test-order
    dependency: a state machine that is cloned or inspected between the metadata
    write and the batch commit sees the node before the data.

    The fix

    apply() now keeps a working copy of sys_data for the duration of the batch:

  • apply_add_node / apply_remove_node / apply_membership mutate that copy

  • instead of the database (the immediate-write helpers set_last_membership
    and remove_node are gone; add_node remains for the init_cluster
    bootstrap path),
  • after the loop, NODES_KEY and LAST_MEMBERSHIP_KEY are staged into the

  • same WriteBatch as the KV data and LAST_APPLIED_LOG_KEY, and only when the
    batch actually changed them,
  • the working copy is published to sys_data only after db.write() succeeds.
  • As a side effect this removes the redundant second put_cf(LAST_APPLIED_LOG_KEY)
    that apply() used to issue after the batch (the cache is updated from the
    working copy instead), so set_last_applied_log_id becomes test-only and moves
    behind #[cfg(test)]. That also fixes finding P2-1 in the review; a
    separate PR for it would touch the same lines for no benefit.

    Tests: test_apply_add_node_is_persisted_with_the_batch (new) and the existing
    test_apply_membership_updates_nodes_and_survives_reopen /
    test_recover_sys_data_nodes now exercise the staged path; all 83 lib tests,
    cargo fmt --check and cargo clippy --all-features -- -D warnings pass.

    Note for the reviewer: the membership is encoded without its Option

    wrapper, matching what recover_sys_data() decodes. Encoding the Option

    makes every reopen fail with Hit the end of buffer — the existing

    membership test catches it.

    Diff: 402 lines (+301 / -101) across 5 files Reviewer: lichuang Review duration: 2426m
    All Vouch review confidence checks passed
    Residual risk 0.18 = Change Risk (0.45) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.45 · 402 lines diff across 5 files
    0.45
    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.359
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #6"
    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: 44.7% (MEDIUM) · Higher = larger blast radius
    44.7%
    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: 17.7% · Safe to merge
    17.7%