fix(raft): make Txn sub-operations atomic via shared WriteBatch#110
Merged
Conversation
Refactor state machine apply path so that all sub-operations within a Txn share a single WriteBatch and commit atomically. Previously each sub-operation created and committed its own WriteBatch independently, causing partial application on mid-sequence failures. Key changes: - Add TxnSnapshot for rollback of in-memory state on batch write failure - Extract batch_put/batch_delete helpers that defer db.write to caller - Add key_leases in-memory map to track lease associations accurately - Add value_cache to fix prev_kv inconsistency for repeated key writes - Persist applied_index to raft_state CF to prevent re-apply on restart - Call save_global_revision once per operation instead of per sub-op
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Txn sub-operations now share a single
WriteBatchand commit atomically, matching etcd semantics. Previously each sub-operation committed independently, causing partial application on mid-sequence failures.Changes
src/raft/state_machine.rs— Core refactor of the apply path:TxnSnapshotfor rollback of in-memory state (key_indexes,key_metas,key_leases,lease_manager) on batch write failurebatch_put/batch_deletehelpers that append to a sharedWriteBatchwithout callingdb.write()key_leasesin-memory map to track lease associations accurately within multi-op Txnsvalue_cacheto fixprev_kvinconsistency when the same key is written multiple times in one Txnsave_global_revisioncall out of sub-ops to caller level (called once per operation)test_txn_multi_put_atomic,test_txn_put_delete_atomic,test_txn_watch_events_emittedsrc/raft/node.rs— Persist and restore applied index:applied_indexfromraft_stateCF on startupindex <= applied_indexto prevent re-apply on restartapplied_indexafter applying each batch of committed entriessrc/raft/raftrs_store.rs— Applied index persistence:save_applied_index()andload_applied_index()methods for theraft_stateCFTesting
cargo clippy -- -D warningspassescargo fmt -- --checkpassesRelated issues
Refs #108