Repository navigation
fix(nameservice): record paths come from the storage address by one rule - #2097
Conversation
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.
bplatz
left a comment
There was a problem hiding this comment.
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.
| for op in [ | ||
| OwnedOp::Write { | ||
| key: "../beside".into(), | ||
| bytes: b"x".to_vec(), | ||
| }, | ||
| OwnedOp::Delete { | ||
| key: "../beside".into(), | ||
| }, | ||
| ] { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
| /// 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> { |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
Thanks for going through the replay and |
…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.
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_prefixand 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:FileStorage::path_of(address), the file an address names under the storage root, or the storage error.FileStorage::list_prefixresolves its prefix by the rule.InvalidData), on replay and on retirement.fluree-db-nameservice(FileNameService):list_brancheslists the directory its address names.Behavior
/(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 dropremoves that record...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 bylist_branches.a_branch_beginning_with_a_slash_drops_its_own_record.fluree-db-core:addresses_name_files_under_the_root_only:path_ofandlist_prefix.a_record_key_names_a_file_under_the_root: the write-ahead log'sflush_keys, andapplyfor 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
140805f4aunless noted. The later commit only adds to afluree-db-coretest, so the runs at7c5c5f4ca(4 build jobs, 4 test threads) still cover the rest.cargo fmt --all --checkis clean.-D warnings,--no-deps:--all-features, all targets, overfluree-db-coreandfluree-db-nameservice: clean;fluree-db-nameservice(all targets) andfluree-db-core(library): clean.fluree-db-core's test target stops on an unused import incommit.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 currentmain's.7c5c5f4ca).fluree-db-core(--all-features) andfluree-db-nameservice: 1,177 (5 ignored);fluree-db-api's ledger and misc groups: 578 (at7c5c5f4ca);fluree-db-server: 704 (at7c5c5f4ca).Not run: the other
fluree-db-apigroups, the CLI suite and the workspace--all-featurestest 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.jsonlwritten 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.