Skip to content

fix(nameservice): record paths come from the storage address by one rule - #2097

Merged
aaj3f merged 2 commits into
mainfrom
fix/ns-record-paths
Oct 14, 2026
Merged

aaj3f merged 2 commits into
mainfrom
fix/ns-record-paths

Conversation

@aaj3f

@aaj3f aaj3f commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

The file nameservice now takes every file path it uses for a ledger from the ledger's storage address: the record, the index record, the commit-index file, and the directory a ledger's branches are listed from. File storage resolves an address by one rule, for every read and every write: the path is relative to the storage root, has no .. component, and doesn't name the write-ahead log's directory. A ledger or branch name the rule refuses names no file, and the nameservice reports the storage error, as reading the record by its address already did.

FileStorage::list_prefix and the write-ahead log's replay and segment retirement now resolve keys by that same rule too, and the rule lives in one function, so there's one place to reason about it.

Changes

  • fluree-db-core:
    • New: FileStorage::path_of(address), the file an address names under the storage root, or the storage error.
    • FileStorage::list_prefix resolves its prefix by the rule.
    • The write-ahead log refuses a record whose key the rule refuses (InvalidData), on replay and on retirement.
  • fluree-db-nameservice (FileNameService):
    • The record, index-record and commit-index paths are the paths of their addresses.
    • list_branches lists the directory its address names.
    • When its path is refused, the best-effort commit-index append is skipped (debug log), as other append failures are.

Behavior

  • Paths are unchanged for every ledger id a create path produces, so for ordinary ledgers nothing moves.
  • For a branch whose name begins with / (creatable from v4.0.0 through v4.2.1), the nameservice's file operations act on the record stored under the ledger's directory, where its address put it. branch drop removes that record.
  • For a ledger or branch name with a .. component, the nameservice's file operations return the storage error.

Tests

  • fluree-db-nameservice:
    • record_paths_stay_inside_the_data_directory: every path for ids with / in the name, a branch beginning with /, and // is under the root and equals the path of its address. Names with .. are refused, by each path and by list_branches.
    • a_branch_beginning_with_a_slash_drops_its_own_record.
  • fluree-db-core:
    • addresses_name_files_under_the_root_only: path_of and list_prefix.
    • a_record_key_names_a_file_under_the_root: the write-ahead log's flush_keys, and apply for each record kind that names a file (write, insert, compare-and-swap, delete).

Each fails with its site reverted to a plain join (eight mutations, each restored).

Gates

Run locally at 140805f4a unless noted. The later commit only adds to a fluree-db-core test, so the runs at 7c5c5f4ca (4 build jobs, 4 test threads) still cover the rest.

  • cargo fmt --all --check is clean.
  • clippy -D warnings, --no-deps:
    • with --all-features, all targets, over fluree-db-core and fluree-db-nameservice: clean;
    • with default features, over fluree-db-nameservice (all targets) and fluree-db-core (library): clean.
    • Default-feature clippy on fluree-db-core's test target stops on an unused import in commit.rs, which this PR doesn't touch. The import is used only by feature-gated tests. That file and the crate manifest are identical to current main's.
  • The wasm32 engine clippy is clean (at 7c5c5f4ca).
  • Tests, all passing:
    • fluree-db-core (--all-features) and fluree-db-nameservice: 1,177 (5 ignored);
    • fluree-db-api's ledger and misc groups: 578 (at 7c5c5f4ca);
    • fluree-db-server: 704 (at 7c5c5f4ca).

Not run: the other fluree-db-api groups, the CLI suite and the workspace --all-features test run (CI's).

Persisted data

Nothing persisted changes format. Every record is where its address has always stored it, and existing data is read without a migration.

One file can be left behind. A branch whose name begins with / (creatable from v4.0.0 through v4.2.1) may, on versions with the commit index (v4.0.6 onward), have had its .commits.jsonl written at a path outside the data directory. That file is no longer read: the commit index inside the data directory starts empty, and readers fall back to walking the commit chain. It's safe to delete.

The file nameservice's record, index and commit-index paths, and the
directory it lists a ledger's branches from, are now the paths of their
storage addresses, resolved by the rule file storage applies to every
read and write: relative to the root, with no `..` component, and not
the log's directory. A ledger or branch name that the rule refuses names
no file, and the operation reports the storage error.

`FileStorage::list_prefix` and the write-ahead log's replay and segment
retirement resolve keys by the same rule, which now lives in one
function.

New: `FileStorage::path_of`, the file an address names under the root.
@aaj3f aaj3f added bug Something isn't working as expected area:storage Storage backends, nameservice, ledger lifecycle, index storage, caches labels Oct 12, 2026
@aaj3f
aaj3f requested a review from bplatz October 12, 2026 13:44

@bplatz bplatz left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Approving, but please go through the two inline comments before merging. One is a small test gap and the other is a correction to the PR description's "Persisted data" section.

What I checked locally: the four new tests pass, and putting the old join back into ns_path fails both nameservice tests. Replay's new refusal can't trip on existing logs, since every logged key comes from resolve_key, which already applied this rule. Every list_prefix caller builds its prefix from a storage address, so the stricter listing doesn't break any of them. The remaining base_path() joins in the nameservice walk the whole ns@v2 directory and take no ledger name, so "every path for a ledger" holds.

Comment on lines +2917 to +2925
for op in [
OwnedOp::Write {
key: "../beside".into(),
bytes: b"x".to_vec(),
},
OwnedOp::Delete {
key: "../beside".into(),
},
] {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This loop covers the Write and Delete arms of apply, but Insert and Cas also go through record_path now. Reverting either of those two arms to base.join(key) leaves every test green. Adding an OwnedOp::Insert and an OwnedOp::Cas with the same ../beside key to this list would pin them.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Good catch, the loop only pinned two of the four arms that go through record_path. In 140805f I added an OwnedOp::Insert and an OwnedOp::Cas (with expected: None), both on the same ../beside key. Reverting either arm to base.join(key) now fails a_record_key_names_a_file_under_the_root, each on its own.

Comment on lines +210 to +215
/// The file of the append-only commit-CID index.
///
/// One JSON line per advanced commit head (`{"t":<t>,"cid":"<cid>"}`).
/// Append-only on the write path; full-rewrite only on prune. Readers
/// dedup by `t` (last-wins), so re-appending the same `t` is harmless.
fn commits_path(&self, ledger_name: &str, branch: &str) -> PathBuf {
self.storage
.base_path()
.join(NS_VERSION)
.join(ledger_name)
.join(format!("{branch}.commits.jsonl"))
fn commits_path(&self, ledger_name: &str, branch: &str) -> Result<PathBuf> {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The "Persisted data" section says nothing persisted moves, but this file does for a branch whose name begins with /. The commit index shipped in v4.0.6, so from v4.0.6 through v4.2.1 such a branch appended it at an absolute path outside the data directory. append_commit_index_line creates missing directories, so that append succeeded wherever the process could write, for example in a container running as root. After this PR those files are orphaned; the in-root index starts empty and readers fall back to the chain walk, so nothing breaks. Could you add a sentence to the description saying those stray .commits.jsonl files may exist and are safe to delete? It's worth having in the release notes for operators.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

You're right. "Nothing moves" isn't true for that one file. I've added a paragraph to "Persisted data". It says that a branch whose name begins with /, on v4.0.6 through v4.2.1, may have a .commits.jsonl outside the data directory. Nothing reads that file now: the index inside the data directory starts empty and readers fall back to walking the commit chain. So it's safe to delete. Thanks for tracing the version range, that's the part operators will need.

Replay's Insert and Cas arms resolve their keys through `record_path`
like Write and Delete. The refusal test now applies all four with a key
outside the root.
@aaj3f

aaj3f commented Oct 14, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for going through the replay and list_prefix callers. Both inline notes are addressed. The WAL test covers all four arms in 140805f, and the description now mentions the stray commit-index files.

@aaj3f
aaj3f merged commit c1a61af into main Oct 14, 2026
19 checks passed
@aaj3f
aaj3f deleted the fix/ns-record-paths branch October 14, 2026 13:14
bplatz added a commit that referenced this pull request Oct 20, 2026
…cord kept

#2097 published and dropped such a branch. On this branch the id no
longer parses, so the test now pins that: publish, lookup and drop are
refused, nothing is written for it, and a record an older version wrote
stays where it is.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:storage Storage backends, nameservice, ledger lifecycle, index storage, caches bug Something isn't working as expected

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants