Skip to content

feat(persistence): coordinated AOF background rewrite engine and atomic cutover - #7

Merged
vubon merged 1 commit into
mainfrom
feat/coordinated-aof-rewrite
Nov 21, 2026
Merged

vubon merged 1 commit into
mainfrom
feat/coordinated-aof-rewrite

Conversation

@vubon

@vubon vubon commented Nov 21, 2026

Copy link
Copy Markdown
Owner

Summary

This PR delivers Finding C (Step 7) of Architecture Decision Record 6 (improvements/improvement-6-design-decision.md), completing the entire implementation of ADR 6 with a production-grade, crash-resilient background rewrite engine for KacheDB.

Key Deliverables

  1. Consistent Base Snapshot:
    • Captured under commit fence (durability::with_commit_fence) in crates/kachedb-server/src/snapshot.rs, anchoring KV table entries, expiration deadlines, and HNSW/Flat vector graphs at an exact log sequence.
    • Written to a temporary file (.rewrite-<hex>.tmp) with strict 0o600 permissions.
    • Encodes a self-contained generation marker and checkpoint sequence in BaseHeader.
  2. Ordered Delta Buffering:
    • Concurrent mutations during rewrite are collected in sequence inside a bounded delta buffer (DELTA_BYTES = 64 MiB, DELTA_RECORDS = 65,536).
    • The active journal remains 100% authoritative for client mutations and durability acknowledgments throughout the rewrite.
    • Delta exhaustion cleanly cancels the rewrite via AtomicBool without impacting active writes.
  3. Atomic Cutover & Directory Sync on Writer Thread:
    • Writer thread pauses at an ordered boundary, flushes trailing deltas, syncs the replacement file, atomically renames it over the active journal path, and syncs the parent directory.
    • Descriptor and advisory lock are seamlessly retained on the replacement file (*active = replacement.file).
    • Directory sync failure after rename (InstallError::AfterRename) transitions to dispatch.fail() to halt writes and prevent filesystem divergence.
  4. Self-Contained Log Recovery:
    • Replay scanner in crates/kachedb-server/src/aof_recovery.rs detects embedded base (BASE_KIND) via scan_base.
    • Streams base data through ChecksummedReader validating IEEE 802.3 CRC32 before hydrating SwissTable and vector indices.
    • Replays subsequent mutation frames on top of the hydrated base state.
  5. Re-enabled BGREWRITEAOF Command:
    • Clients invoking BGREWRITEAOF receive +Background append only file rewriting started.
    • Replaces the temporary disabled stub from Step 1.

Verification

  • cargo fmt --all -- --check: PASS (0 diffs)
  • cargo clippy --workspace --all-targets --all-features -- -D warnings: PASS (0 warnings across all 11 crates)
  • cargo test -p kachedb-server -- aof_rewrite: PASS (6/6 tests passed in 0.13s)
  • cargo test -p kachedb-server --test durability background_rewrite: PASS (passed in 1.75s)
  • cargo test --workspace: PASS (100% pass across all unit, doc, and integration tests)
  • prv Staff Review: APPROVE (11/11 dimensions passed)
  • AI-DLC 5-Layer Governance Review: PASS & COMPLIANT

…ic cutover

- Capture consistent base snapshot under commit fence (with_commit_fence) covering KV entries, expiration deadlines, and HNSW/Flat vector graphs
- Buffer concurrent mutations in bounded delta queue (64 MiB / 65,536 records); delta exhaustion cleanly cancels rewrite via AtomicBool without impacting active writes
- Execute atomic cutover on writer thread: flush deltas, sync replacement file, rename over active journal path, swap descriptor while retaining lock, and fsync parent directory
- Handle directory sync failure after rename with InstallError::AfterRename, halting writes via dispatch.fail() to prevent filesystem divergence
- Support self-contained log recovery via scan_base and ChecksummedReader with IEEE 802.3 CRC32 verification before state hydration
- Verify dev and inode in open_locked to guard against replacement races
- Re-enable live BGREWRITEAOF command returning +Background append only file rewriting started
- Fully resolves Finding C in ADR 6 Step 7, completing all ADR 6 milestones
@vubon
vubon merged commit a4ddcbf into main Nov 21, 2026
6 checks passed
@vubon
vubon deleted the feat/coordinated-aof-rewrite branch November 21, 2026 04:23
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