RocksDB Session Management: Backup, Restore, and WAL Recovery#
HugeGraph's RocksDB backend manages database sessions through a ThreadLocal pool that separates data and write-ahead log (WAL) directories. This architecture enables snapshot-based backup and restore while requiring careful handling of directory switching on Windows and WAL recovery when data and WAL directories differ.
Session Lifecycle#
BackendSessionPool assigns one session per thread via ThreadLocal storage. Each session tracks a reference count; when the last reference is released, the pool closes the underlying RocksDB instance . RocksDBStdSessions stores both dataPath and walPath fields, allowing the WAL to reside on separate storage from data files. When opening RocksDB, openRocksDB explicitly calls options.setWalDir(walPath) to direct write-ahead logs to the configured location.
Backup: Snapshot Creation#
createSnapshot delegates to OpenedRocksDB.createCheckpoint(snapshotPath), which produces a consistent RocksDB checkpoint—a hard-linked copy of SST files and a copy of the current WAL . The store-level createSnapshot iterates over each RocksDB instance (one per table disk), building separate checkpoints under a snapshot prefix. On return, the checkpoint directory contains both data files and a snapshot-time WAL tail, ready for atomic restore.
Restore: Snapshot Resume and Force-Close on Windows#
resumeSnapshot performs a destructive restore: it closes RocksDB via forceCloseRocksDB, deletes the origin data directory, moves the snapshot directory into its place, and reopens RocksDB. The force-close step is essential on Windows, where an open session holds a LOCK file that prevents directory moves (PR #235 motivation). Without force-close, the restore fails with a locked-file error.
PR #235 extends this mechanism with a state-machine approach (switching/switched/committed) to survive crashes mid-restore. The RocksDbGraphBackupService orchestrates session draining, directory switching, and reopening across all RocksDB instances. This PR is currently OPEN and not yet merged .
Store-level resumeSnapshot optionally uses hard-links to avoid destructive restore: hardLinkSnapshot creates a temporary linked copy before moving the snapshot into place, preserving the original snapshot on disk.
WAL Recovery During Restore#
When dataPath and walPath are separate directories, restoring only the data directory causes RocksDB to replay the old WAL tail on open—logs written after the snapshot was taken. A fix introduced in commit 457295ac8 added replaceSeparateWalDirectory() to copy checkpoint WAL logs into the separate WAL directory before reloadRocksDB. Evidence from the integration test log (state.md) confirms that after this fix, both standard RocksDB and ToplingDB standalone modes passed snapshot_create and snapshot_resume with "Replaced separate WAL" in logs.
Current status: this WAL recovery fix was subsequently reverted in HEAD (2f827d6e8), so the current codebase does not include it . The separate-WAL recovery problem remains unresolved in the live source.
ToplingDB Compatibility#
PR #179 adds optional ToplingDB integration via rocksdb.provider=topling configuration. Standard RocksDB remains the default; ToplingDB is JNI-swapped at startup via preload-topling.sh. A critical compatibility requirement is setting memtable_as_log_index=false for ToplingDB to avoid "mmap WAL" errors during batch writes. On graceful shutdown, ToplingDB produces a cosmetic db not closed log (standard RocksDB does not), but this does not indicate a resource leak .
The session management, snapshot, and restore workflows operate identically under both providers, with the same separate-WAL recovery gap affecting ToplingDB deployments. PR #179 is also OPEN and not yet merged.
Key Source References#
| Component | File |
|---|---|
| Session pool | BackendSessionPool.java |
| RocksDB sessions | RocksDBStdSessions.java |
| Store-level snapshot | RocksDBStore.java |
| Windows restore fix (OPEN) | PR #235 |
| ToplingDB integration (OPEN) | PR #179 |