Manifest

The manifest is the authoritative record of which SSTables make up the store, at which level, plus the sequence counter and the file-number allocator. Flush, compaction, bulk ingest and the at-rest rewrite change the live set only by appending an edit to it, so a crash leaves the store at either the old set or the new one (source: src/storage/manifest.rs, module doc, Manifest, VersionEdit).

Files

File in data_dir Content
MANIFEST Snapshot: a header and one serialized VersionState
MANIFEST.log Edit log: VersionEdit records appended since the snapshot
MANIFEST.tmp Temporary file used while a snapshot is written; renamed over MANIFEST

(source: src/storage/manifest.rs, Manifest::save, log_path; src/storage/mod.rs, StorageEngine::open)

The current state is always MANIFEST with MANIFEST.log replayed over it.

In-memory state

VersionState {
    levels:           Vec<Vec<SSTableMeta>>   // 7 levels for a new manifest
    sequence:         u64                     // highest SetSequence seen
    next_file_number: u64                     // starts at 1
}

SSTableMeta { filename, level, file_size, min_key, max_key, entry_count }

min_key and max_key are the first and last encoded internal keys written to the file (see SSTable format). Manifest wraps the state in a parking_lot::RwLock next to the open log writer and the byte offset of the next log record (source: src/storage/manifest.rs, VersionState, SSTableMeta, Manifest).

Because the bounds are row keys, the Debug output of Manifest prints only the path, the file count and the log offset; the comment says this keeps row-identifying keys out of logs and panic messages (source: src/storage/manifest.rs, impl Debug for Manifest).

Snapshot format

+-------------------------+-------------+-------------+----------------------------+
| magic u64 LE            | version u32 | length u32  | payload (length bytes)     |
| 0x4D414E49464553        | LE          | LE          | bincode VersionState       |
+-------------------------+-------------+-------------+----------------------------+
Version Payload
1 (MANIFEST_VERSION) Plain bincode VersionState
2 (MANIFEST_VERSION_SEALED) Sealed with the at-rest keyring, Purpose::Manifest, bound to the file id of MANIFEST and offset 0

(source: src/storage/manifest.rs, MANIFEST_MAGIC, MANIFEST_VERSION, MANIFEST_VERSION_SEALED, Manifest::save, Manifest::load)

load rejects a wrong magic, an unknown version, and a version 2 file when no keyring is installed, all as Error::Corruption. The version is chosen on each save: with a keyring installed a version 1 manifest is rewritten as version 2, which is the upgrade path. Tested by plaintext_manifest_still_loads_and_upgrades_to_sealed (tests/at_rest_manifest_upgrade.rs). Key handling is in Encryption at rest internals.

Edit log format

Each record is [length u32 LE][payload]. The payload is a bincode VersionEdit, sealed with Purpose::Manifest and bound to the log's file id and the record's own byte offset when a keyring is installed. Binding the offset means a record moved, duplicated or reordered fails to open (source: src/storage/manifest.rs, log_edit, decode_log_record). Tested by reordered_sealed_log_records_are_rejected (tests/at_rest_manifest_tamper.rs).

There is no per-record checksum. A plaintext record is protected only by its length prefix and by bincode refusing to parse garbage; a sealed record is protected by its authentication tag (source: src/storage/manifest.rs, replay_log doc comment).

When reading, a sealed open is tried first and a plain parse is the fallback, so a log written before encryption was turned on still replays (source: src/storage/manifest.rs, decode_log_record).

Version edits

bincode encodes the variant index, so variants are only ever appended; reordering them would break old logs (source: src/storage/manifest.rs, VersionEdit::CompactionV2 doc comment).

Index Variant Written by Effect on VersionState
0 AddSSTable(SSTableMeta) Flush, recovery flush, bulk ingest Push the file at meta.level
1 RemoveSSTable { level, filename } No engine caller Remove the file from that level
2 SetSequence(seq) StorageEngine::sync, shutdown sequence = max(sequence, seq)
3 CompactionComplete { level, inputs, outputs } Older binaries only Remove every input from whichever level holds it
4 CompactionV2 { level, inputs, outputs } Compaction, replace_range_bulk, at-rest rewrite Add every output, remove every input, in one record
5 MoveSSTable { filename, from_level, to_level } Trivial move in compaction Move the file; a no-op if it is no longer at from_level

(source: src/storage/manifest.rs, VersionEdit, VersionState::apply, add_sstable, record_compaction, record_move, set_sequence; src/storage/mod.rs, flush_memtable_to_sst, ingest_bulk_prefixed, replace_range_bulk, sync, shutdown; src/storage/at_rest.rs)

CompactionComplete was not crash-atomic: older binaries logged each output as its own AddSSTable record before it, so a crash in between left both inputs and outputs listed. It is kept so those logs still replay. Restoring the inputs in that state is safe because a rename does not change file contents and reads resolve by sequence across all files (source: src/storage/manifest.rs, CompactionComplete doc comment; src/storage/compaction.rs, reconcile_delete_files doc comment).

MoveSSTable applied to a stale job is a no-op instead of a duplicate entry. Tested by record_move_replays_from_log (src/storage/manifest.rs, tests).

Appending an edit

log_edit(edit):

  1. Serialize the edit with bincode and seal it if a keyring is installed, using the current log_offset.
  2. Under the log write lock, write the length and payload, flush the BufWriter, and sync_all the file.
  3. Advance log_offset by 4 + payload length.
  4. Apply the edit to the in-memory VersionState.

(source: src/storage/manifest.rs, log_edit)

The edit is applied in memory only after it is durable on disk. If the write or fsync fails, the error is returned and the in-memory state is unchanged (source: src/storage/manifest.rs, log_edit).

Replacing the live set atomically

Every change to the live set is one log_edit call, and so one fsynced record:

Operation Ordering that makes it crash-safe
Flush The SSTable is written and fsynced (file and directory) before AddSSTable is logged, and the memtable leaves the immutable queue only after that (see Memtable)
Compaction Output fsynced, inputs renamed to .delete, one CompactionV2 record, then the .delete files unlinked (see Compaction)
Trivial move One MoveSSTable record; no file changes
replace_range_bulk Same rename, one CompactionV2 at level 0, unlink sequence as compaction

(source: src/storage/mod.rs, flush_memtable_to_sst, replace_range_bulk; src/storage/sst.rs, SSTableWriter::finish; src/storage/compaction.rs, execute_compaction_throttled)

So on disk, a crash can leave: an SSTable the manifest does not list (removed at open by purge_orphan_sstables), .delete files (reconciled at open by reconcile_delete_files), or a torn last log record (dropped at load). See Crash recovery.

The engine's in-memory list of open readers (levels) is a separate structure derived from the manifest. Flush updates both; compaction updates only the manifest and the engine re-derives levels afterwards (see Compaction).

Snapshot (save)

save():

  1. Serialize the state (sealed if a keyring is installed) into MANIFEST.tmp, flush and sync_all.
  2. rename MANIFEST.tmp to MANIFEST.
  3. Close the log writer, delete MANIFEST.log, reset log_offset to 0 and reopen an empty log.

(source: src/storage/manifest.rs, Manifest::save)

The parent directory is not fsynced after the rename or the log delete (source: src/storage/manifest.rs, Manifest::save). Not verified: no test covers a power loss between the rename and the log delete; after a process crash at that point, replaying the old log over the new snapshot repeats edits the snapshot already holds, and AddSSTable pushes without a duplicate check (source: src/storage/manifest.rs, VersionState::apply).

save is called by Manifest::new and by StorageEngine::shutdown. Nothing else folds the log into the snapshot, so MANIFEST.log grows by one record per flush, compaction, move and sync() for the life of the process and is replayed in full after an unclean stop (source: src/storage/manifest.rs, Manifest::new; src/storage/mod.rs, shutdown).

Load and torn-tail tolerance

Manifest::load reads the snapshot, then replay_log:

flowchart TD A["read 4-byte length"] -->|"fewer than 4 bytes left"| T["stop: torn or clean end"] A --> B{"record fits in file?"} B -->|"no"| T B -->|"yes"| C{"decodes?"} C -->|"yes"| D["apply, advance valid offset"] --> A C -->|"no, and it is the last record"| T C -->|"no, and more data follows"| E["Error::Corruption, open fails"] T --> U["truncate log to last valid offset, fsync"]
(source: src/storage/manifest.rs, replay_log)

The reasoning in the code: log_edit appends and fsyncs one record at a time, so a crash can tear only the last record. A bad record with valid data after it is not a crash artifact of that protocol, and load refuses to guess. For a sealed log the same rule covers a record that authenticates but sits at the wrong offset (source: src/storage/manifest.rs, replay_log doc comment).

Tested by torn_trailing_log_record_is_tolerated_and_truncated, torn_trailing_length_prefix_is_tolerated_and_truncated, corrupt_trailing_full_length_record_is_tolerated_and_truncated, corrupt_mid_log_record_still_fails_load, test_manifest_log_replay, test_manifest_create_load and test_manifest_compaction (src/storage/manifest.rs, tests), and end to end by engine_opens_with_torn_trailing_manifest_log_record (src/storage/mod.rs, crash_reopen_durability_tests).

File numbering

SSTables are named sst-<n as 16 lowercase hex digits>.sst. Flush, recovery flush, compaction and bulk ingest all take n from the one allocator Manifest::next_file_number(), which returns the current value and increments it under the state write lock (source: src/storage/manifest.rs, next_file_number; src/storage/mod.rs, background_flush_loop, flush_memtable_to_sst; src/storage/compaction.rs, execute_compaction_throttled).

The counter is not logged per allocation. It is persisted only inside a snapshot, so after an unclean stop the value in MANIFEST is old. At every open, before WAL replay, the engine computes the highest id among the manifest's files and every sst-*.sst name in data_dir and calls advance_file_number_to(max + 1), which only raises the counter (source: src/storage/mod.rs, StorageEngine::open, parse_sstable_file_id; src/storage/manifest.rs, advance_file_number_to). The scan runs after orphan SSTables were removed and .delete files restored, so restored files count. The open-time comment explains the other reason for the scan: older data directories named flush files from the sequence counter and compaction files from this allocator, and the two could collide.

Sequence number

VersionState::sequence only moves up (SetSequence keeps the maximum). It is written by sync() and shutdown(). At open the engine starts from it and then raises the sequence above every SSTable's maximum sequence and every replayed WAL entry, so a stale manifest value cannot make a new write sort below a persisted one (source: src/storage/mod.rs, StorageEngine::open, sync, shutdown). Tested by restart_without_sync_loses_writes_after_wal_drop (src/storage/property_oracle_tests.rs), whose doc comment describes the bug this fixed.

Level count

A new manifest has 7 levels (VersionState::new(7)), and an edit for a higher level extends the vector. StorageConfig::num_levels defaults to 7 and has no config-file key, so the two match in practice (source: src/storage/manifest.rs, Manifest::new, VersionState::apply; src/config.rs, StorageConfigFile::to_storage_config).

Read API

Method Returns
sstables_at_level(level) Clone of that level's SSTableMeta list, in insertion order
all_sstables() Clone of every SSTableMeta, level by level
num_levels() Length of levels
sequence_number() Stored sequence
level_sizes(), level_bytes() File count and byte total per level
at_rest_key_ids(bytes) Key ids and plaintext record count of a MANIFEST or MANIFEST.log, for synergydb encryption status

(source: src/storage/manifest.rs, Manifest)

StorageEngine::manifest() exposes a read-only handle for tooling and tests (source: src/storage/mod.rs, manifest). A backup copies both MANIFEST and MANIFEST.log, because the snapshot alone misses every edit since the last clean shutdown, and records a checksum of the log (source: src/storage/backup.rs, create_backup); see Backup and restore internals.