fix: state-machine: stage metadata into the apply batch #6
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
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.359
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #6"
general
weight: 0.35
lichuang
Finding
P1 — metadata escapes the apply
WriteBatch, breaking the atomicity the statemachine promises.
apply()buffers every KV write into oneWriteBatchand commits it once, butthree 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_appliedstill pointing at the previousposition. 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::AddNodefollowed by a KV write, then reopen:get_nodes()(backed by_sm_meta) already lists the node,last_appliedand 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 ofsys_datafor the duration of the batch:apply_add_node/apply_remove_node/apply_membershipmutate that copyinstead of the database (the immediate-write helpers
set_last_membershipand
remove_nodeare gone;add_noderemains for theinit_clusterbootstrap path),
NODES_KEYandLAST_MEMBERSHIP_KEYare staged into thesame
WriteBatchas the KV data andLAST_APPLIED_LOG_KEY, and only when thebatch actually changed them,
sys_dataonly afterdb.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 theworking copy instead), so
set_last_applied_log_idbecomes test-only and movesbehind
#[cfg(test)]. That also fixes finding P2-1 in the review; aseparate PR for it would touch the same lines for no benefit.
Tests:
test_apply_add_node_is_persisted_with_the_batch(new) and the existingtest_apply_membership_updates_nodes_and_survives_reopen/test_recover_sys_data_nodesnow exercise the staged path; all 83 lib tests,cargo fmt --checkandcargo clippy --all-features -- -D warningspass.