Skip to content

fix(raft): make Txn sub-operations atomic via shared WriteBatch#110

Merged
slow2342 merged 1 commit into
mainfrom
fix/raft-txn-atomic-write-batch
Jun 11, 2026
Merged

fix(raft): make Txn sub-operations atomic via shared WriteBatch#110
slow2342 merged 1 commit into
mainfrom
fix/raft-txn-atomic-write-batch

Conversation

@slow2342

Copy link
Copy Markdown
Owner

Summary

Txn sub-operations now share a single WriteBatch and 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:

    • Added TxnSnapshot for rollback of in-memory state (key_indexes, key_metas, key_leases, lease_manager) on batch write failure
    • Extracted batch_put/batch_delete helpers that append to a shared WriteBatch without calling db.write()
    • Added key_leases in-memory map to track lease associations accurately within multi-op Txns
    • Added value_cache to fix prev_kv inconsistency when the same key is written multiple times in one Txn
    • Moved save_global_revision call out of sub-ops to caller level (called once per operation)
    • Added 3 new tests: test_txn_multi_put_atomic, test_txn_put_delete_atomic, test_txn_watch_events_emitted
  • src/raft/node.rs — Persist and restore applied index:

    • Load applied_index from raft_state CF on startup
    • Skip entries with index <= applied_index to prevent re-apply on restart
    • Persist applied_index after applying each batch of committed entries
  • src/raft/raftrs_store.rs — Applied index persistence:

    • Added save_applied_index() and load_applied_index() methods for the raft_state CF

Testing

  • All 58 existing tests pass
  • 3 new tests verify Txn atomicity (multi-put, put-delete, watch events)
  • cargo clippy -- -D warnings passes
  • cargo fmt -- --check passes

Related issues

Refs #108

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
@slow2342
slow2342 merged commit cbef5bc into main Jun 11, 2026
4 of 6 checks passed
@slow2342
slow2342 deleted the fix/raft-txn-atomic-write-batch branch June 11, 2026 08:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant