Skip to content

fix: fsync keyspaces directory after creating keyspace folders - #357

Open
hcrosse wants to merge 1 commit into
fjall-rs:mainfrom
hcrosse:fsync-keyspaces-dir
Open

hcrosse wants to merge 1 commit into
fjall-rs:mainfrom
hcrosse:fsync-keyspaces-dir

Conversation

@hcrosse

@hcrosse hcrosse commented Nov 21, 2026 •

Copy link
Copy Markdown

Closes #356

Database::create_new called fsync_directory on keyspaces/ before creating keyspaces/0, and Keyspace::create_new never called fsync_directory on keyspaces/ after creating keyspaces/<id>. On Unix, losing either unsynced directory entry on power loss could drop committed keys after PersistMode::SyncAll or cause recover_keyspaces to remove intact user keyspaces.

Changes:

  • In Database::create_new, open the meta tree (keyspaces/0) and call fsync_directory on keyspaces/ before creating 0.jnl and writing the version marker, so keyspaces/0 is durable before bootstrap completes and a failed meta tree creation does not strand 0.jnl on retry.
  • In Keyspace::create_new, call fsync_directory on keyspaces/ after opening the new keyspace tree and before recording it in meta_keyspace.
  • Added an integration test covering retry after a failed meta tree creation.

Verified on Linux with strace (keyspaces/ is now synced after keyspaces/0 before version, and after keyspaces/1), cargo fmt --all -- --check, cargo clippy, cargo test --features lz4, and whitebox tests.

Summary by CodeRabbit

  • Bug Fixes
    • Improved database setup recovery after an initial creation failure. Data written and synchronously saved after retrying can now be verified after reopening the database.

Database::create_new fsynced keyspaces/ before the meta keyspace
(keyspaces/0) existed, and wrote the version marker before creating it,
so the keyspaces/0 directory entry was never durable. It also created
0.jnl before keyspaces/0, so a failure to create the meta keyspace left
a journal behind without a version marker; retrying then took the
create_new path again and failed with AlreadyExists on 0.jnl.

Create the meta keyspace and fsync keyspaces/ before creating the
journal and the version marker, so the 0.jnl window is gone and both
directory entries (keyspaces/0 and keyspaces/<id>) are durable.

Keyspace::create_new now fsyncs keyspaces/ after creating
keyspaces/<id>; without it a power loss after creating a keyspace and
persisting writes could lose the keyspace directory entry, causing
recovery to omit the keyspace and skip its journal records.

Add a test that obstructs keyspaces/0, asserts creation fails, and
asserts that a retry succeeds and reads back the persisted data.
@coderabbitai

coderabbitai Bot commented Nov 21, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

Database initialization and user keyspace creation now sync the keyspaces directory after opening new trees. A database-open test covers retrying after an obstructed metadata-tree creation and verifies persisted data after reopening.

Changes: Keyspace directory durability

Layer / File(s) Summary
Metadata tree initialization and retry test
src/db.rs, tests/db_open.rs
Database creation opens the metadata tree before creating the journal and version marker, then syncs the keyspaces directory. The test retries after an obstructed metadata-tree creation and verifies a synchronously persisted value after reopening.
User keyspace directory sync
src/keyspace/mod.rs
Keyspace creation derives the keyspace folder from the keyspaces directory and syncs that directory after opening the LSM tree.

Estimated code review effort: 2 (Simple) | ~12 minutes

Change: Bug fix · Severity of issue fixed: High

Suggested labels: fix

Suggested reviewers: marvin-j97

Merge Risk: Minimal · up to f4e9a

This change makes keyspace directory creation durable across power loss and lets database creation be retried cleanly after a failed bootstrap. No concrete merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: fsync keyspaces directory after creating keyspace folders" names the main change: a directory fsync of keyspaces/ after creating keyspace folders. The raw summary confirms this for Key…
Linked Issues check Passed Issue #356 is the only direct link and has two coding requirements. (1) Sync keyspaces/ after creating keyspaces/0 and before writing the version marker. The raw summary of src/db.rs states that `…
Out of Scope Changes check Passed All changes described for this PR relate to #356. The src/db.rs change reorders the keyspaces/ fsync and the meta tree open in Database::create_new. The src/keyspace/mod.rs change adds the parent …
Full details: Title check

Explanation

The title "fix: fsync keyspaces directory after creating keyspace folders" names the main change: a directory fsync of keyspaces/ after creating keyspace folders. The raw summary confirms this for Keyspace::create_new and Database::create_new, and the objectives describe the same sync ordering for keyspaces/0 and user keyspaces. The title is specific and matches the changeset.

Full details: Linked Issues check

Explanation

Issue #356 is the only direct link and has two coding requirements. (1) Sync keyspaces/ after creating keyspaces/0 and before writing the version marker. The raw summary of src/db.rs states that Database::create_new now opens the meta tree, then fsyncs keyspaces/, then creates 0.jnl and writes and syncs the V3 version marker. This meets the requirement. (2) Sync keyspaces/ after creating keyspaces/&lt;id&gt; and before recording it in meta_keyspace. The raw summary of src/keyspace/mod.rs states that Keyspace::create_new fsyncs the parent keyspaces/ directory after opening the new LSM tree. This meets the requirement. The PR adds a retry test in tests/db_open.rs for a failed meta tree creation, which covers the related failure path. The repository diff could not be read (changed blobs missing from the checkout), so this verdict relies on the AI-generated summary and the PR description rather than direct source inspection. The ordering claims were not confirmed in code.

Full details: Out of Scope Changes check

Explanation

All changes described for this PR relate to #356. The src/db.rs change reorders the keyspaces/ fsync and the meta tree open in Database::create_new. The src/keyspace/mod.rs change adds the parent directory fsync in Keyspace::create_new and derives the folder from a stored keyspaces_folder path. The tests/db_open.rs change adds the retry test and its imports. No unrelated functional change appears in the summaries. The repository diff could not be read, so this verdict relies on the summaries and PR description. Unrelated edits outside the summarized files cannot be ruled out from direct evidence.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 5c0eb254-6df2-4ffc-aad9-93af977daaaf
Commits

Reviewing files that changed from the base of the PR and between 8700604 and f4e9a17.

Files selected for processing: 3

  • src/db.rs
  • src/keyspace/mod.rs
  • tests/db_open.rs

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@hcrosse
hcrosse marked this pull request as ready for review November 21, 2026 02:46
Copilot AI balanced review requested due to automatic review settings November 21, 2026 02:46

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

No active review issues remain for the keyspace directory-sync durability changes.

No open issues 🎉

What changed in this PR

This PR addresses #356 by syncing the keyspaces/ directory after creating metadata and user keyspace folders, closing a Unix power-loss durability gap.

Changes:

  • Creates the metadata tree and syncs its parent directory before creating the journal and version marker.
  • Syncs the parent directory of new user keyspaces before registering them in metadata.
  • Adds an integration test verifying retry after metadata-tree creation fails, followed by successful persistence and reopening.
File Description
tests/​db_open.rs Tests retry after failed metadata-tree creation and data recovery after reopening.
src/​keyspace/​mod.rs Syncs keyspaces/ after opening a new keyspace tree.
src/​db.rs Moves metadata-tree creation and directory syncing before journal and version-marker creation.

Review effort: Balanced

This branch has not been deployed

No deployments
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.

keyspaces/ directory is not fsynced after creating keyspaces/0 or user keyspaces

2 participants