Skip to content

feat(raft): add snapshot and recovery mechanism#112

Merged
slow2342 merged 1 commit into
mainfrom
feat/raft-snapshot-recovery
Jun 11, 2026
Merged

feat(raft): add snapshot and recovery mechanism#112
slow2342 merged 1 commit into
mainfrom
feat/raft-snapshot-recovery

Conversation

@slow2342

Copy link
Copy Markdown
Owner

Summary

  • Implement leader-initiated snapshots: serialize all user-data CFs (default, mvcc, meta, lease, lease_keys, key_lease) into a single blob, triggered every N applied entries (configurable via snapshot_trigger_log_entries)
  • Implement follower snapshot receive and restore: atomic clear + replay CF data in a single WriteBatch, rebuild in-memory indexes
  • Stop node on snapshot restore failure to prevent operating on inconsistent state
  • 512 MiB snapshot size limit on both create and restore, term() invariant assertion, compact index covers all snapshot-covered entries

Changes

  • src/raft/state_machine.rscreate_snapshot() iterates CFs without lock and checks size limit; restore_snapshot() atomic WriteBatch clear+write+rebuild in-memory state
  • src/raft/raftrs_store.rssave_snapshot_data() persists snapshot data and metadata; apply_snapshot() saves data and purges old log entries; snapshot() includes actual snapshot data; term() adds invariant assertion
  • src/raft/node.rs — Leader-side snapshot trigger (configurable threshold); follower-side snapshot application; snapshot restore failure stops event loop
  • src/config.rssnapshot_trigger_log_entries config field (default 10000)
  • src/auth/role.rs — Remove unused write_perm test helper

Testing

  • 79 unit tests pass
  • cargo clippy -- -D warnings clean
  • cargo fmt -- --check clean

Refs #83

Implement leader-initiated snapshots and follower snapshot application
for Raft log compaction and state transfer.

Key changes:
- State machine: create_snapshot() serializes all user-data CFs (default,
  mvcc, meta, lease, lease_keys, key_lease) into a single blob.
  restore_snapshot() atomically clears + replays CF data in a single
  WriteBatch, then rebuilds in-memory indexes.
- Raft store: save_snapshot_data() persists snapshot data and metadata.
  apply_snapshot() saves data, purges superseded log entries, and
  updates conf state. snapshot() now includes actual snapshot data.
- Event loop: leader triggers snapshot every N applied entries
  (configurable via snapshot_trigger_log_entries). Follower applies
  received snapshots and stops the node on failure to prevent
  inconsistent state.
- Safety: 512 MiB snapshot size limit on both create and restore,
  debug_assert on term() invariant, compact index covers all
  snapshot-covered entries.
@slow2342
slow2342 merged commit 400b614 into main Jun 11, 2026
6 checks passed
@slow2342
slow2342 deleted the feat/raft-snapshot-recovery branch June 11, 2026 15:14
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