Skip to content

LocalFileSystem: per-store directory-handle cache for durable writes (0.12.5) - #12

Closed
reidkaufmann wants to merge 17 commits into
base-object-store-0125from
local-dirfd-cache-0125
Closed

reidkaufmann wants to merge 17 commits into
base-object-store-0125from
local-dirfd-cache-0125

Conversation

@reidkaufmann

Copy link
Copy Markdown
Collaborator

Mirror of #7 onto base-object-store-0125 (object_store 0.12.5) for the influxdb_pro 3.12 backport; stacked on #9 until it merges. One commit per original commit (14), cherry-picked with -x; the RAT CI commit is not part of this port. Alternative to #11, as #7 is to #6.

Conflicts resolved (0.12.5 vs 0.13.2): nix stays at 0.30 with the fs/resource features (the *at APIs used are identical in 0.30.1 and 0.31.3); the copy_opts/rename_opts/delete_stream changes applied to 0.12.5's copy, rename, copy_if_not_exists, rename_if_not_exists and delete, with a private cfg-gated CopyMode since 0.12.5 has no crate::CopyMode; delete_location keeps its PathBuf argument; tests call futures::executor::block_on (no futures-executor dev-dependency in 0.12.5); LocalFileSystem is not Clone in 0.12.5, so the strict/lenient root-parent test builds the second store sharing strict.config directly instead of adding a Clone derive.

Checks: cargo test --all-features, cargo test, cargo clippy --all-targets --all-features -- -D warnings, cargo fmt --check, cargo msrv verify -- cargo check --all-features (1.79.0) pass locally.

`create_parent_dirs` recorded the deepest existing ancestor, called
`create_dir_all`, and synced from the new parent up to that ancestor.
A concurrent delete with automatic cleanup can remove the ancestor in
between; `create_dir_all` recreated it, and its new entry in the
directory above was never synced.

Create the missing components one `create_dir` at a time, registering
each before creating it, and sync every directory from the deepest one
created up to the one holding the highest one's entry. That also covers
a directory another creation made in between, which holds an entry made
here.

(cherry picked from commit 4d3a27f)
…ed creation

A creation that failed before creating anything (EACCES, ENOSPC) still
recorded its would-be directories in the process-global unsynced set,
and nothing cleared them. Record only directories that exist, or whose
state cannot be determined.

(cherry picked from commit 1d068f4)
…mplete fails

Mirrors upstream draft local-multipart-staging-cleanup (4577e8a, on apache
main 279572e).

`finish_staged_rename` and `finish_staged_hard_link` return the staging file's
metadata and remove the staging file themselves when the metadata read, sync,
close, hard link or rename fails, so `LocalUpload::complete` no longer leaks the
`#N` file on failure and `put_opts` only cleans up after a failed write.
`rename` returns `RenameError::{Rename, Fsync}`; after a successful rename whose
directory fsync failed the staging path may already belong to a new upload, so
`finish_staged_rename` and `copy_opts` (Overwrite) leave it alone in that case.
`rename_opts` converts the error back to `io::Error`.

`Error::Metadata` from `put_opts` now names the staging path, as `complete`
already did.

(cherry picked from commit 7d61b85)
…y-handle cache

Port of the upstream draft local-dirfd-cache-e (0367694..20f8bb0 on
apache main 279572e + 4577e8a) onto object_store 0.13.2.

With fsync enabled on Linux and Android, put, copy, rename, delete and
multipart uploads change directory entries through directory descriptors
the store keeps open (openat, mkdirat, renameat, linkat, unlinkat), one
cache per store. Each handle records the entry changes made through it
and the changes an fsync has covered; a write fsyncs every handle on the
chains it changed that still owes one, whoever changed it, before
returning, also when it fails. Handles found stale (the directory removed
or replaced, confirmed by inode) are evicted and the operation retried
from the root. The cache holds up to 4096 clean handles, clamped to a
quarter of the soft RLIMIT_NOFILE; dirty handles are never evicted.

This removes the process-wide DirRegistry (creating/unsynced sets and the
condvar wait in fsync_parent_dir), its creation-recording helpers and the
test hooks; nothing is shared between stores or processes any more. Every
other target keeps the path-based fsync code as it was before the
registry. A directory that cannot be opened is an open error, not a sync
error; UnableToSyncFile is only an fsync failure.

Staging entries are removed when publishing fails and left alone after a
successful rename whose fsync failed (the name may belong to a new
upload); the removal is best effort, a crash leaves a `name#N` entry that
`list` hides.

(cherry picked from commit 976aeeb)
…, evict renamed directories

Port of upstream local-dirfd-cache-e 10c6107..4147531 (review findings
S1-S6, N1-N4, N6, N7, N9 on b3402b8):

- Capacity eviction keeps handles an operation still holds; eviction is
  by handle identity, so a late eviction cannot drop a live replacement;
  a cached child must have been opened under the parent in the chain.
- A handle is stale when its inode has no links left (fstat, no path
  lookup); a stale root is reopened or recreated with its entry fsynced;
  a directory removed between mkdirat and openat is made again; retries
  are bounded by handle identity.
- The eviction order holds weak references and is compacted; descriptors
  are closed after the cache lock is released.
- After a rename whose source is a directory, the handles at and under
  the old path are evicted.
- with_fsync states again what is not coordinated (separate stores on one
  root, other processes, symlink/case aliases, stores with fsync off,
  external moves); the chmod rename test is kept for non-Linux Unix.
- Shared staging-file and AlreadyExists helpers.

(cherry picked from commit 164267a)
…nc, fsync a reopened root's entry

Port of upstream local-dirfd-cache-e 2f74a4a (re-review R1-R5 and the
N7 leftover on 2d0366f). A directory counts as removed only when fstat
reports no links left and its path no longer names it; an operation
retries at most 16 times. The handle of a renamed directory is evicted
whenever the rename itself succeeded, checking only the renamed key.
Whoever opens an uncached root fsyncs the directory holding its entry,
and a recreated root has every created ancestor fsynced. A cached child
opened under another parent is replaced only while it is still that entry
and its directory is removed or moved. EEXIST is AlreadyExists in create
mode only. The losing root handle of a concurrent first open is closed
outside the cache lock.

(cherry picked from commit da69d8b)
A cascade of removed cached directories evicts one per retry, deepest
first, so a chain deeper than the fixed bound of 16 failed with NotFound
where the path-based code succeeds. The bound is now the chain depth
plus 16.

(cherry picked from commit 86e39a4)
Every uncached open of the root fsynced the root's parent, so a store
whose root sits under a traverse-only directory (0311, a 0711 home) failed
every durable write with a sync error, which neither the base nor the
cache before the root recreation did. A PermissionDenied from opening
that parent is now skipped unless this call created the root, whose entry
then still has to be durable.

(cherry picked from commit f61c5c8)
A delete without a sync opened and cached a handle on the object's
directory (one openat and one fstat per delete), which automatic cleanup
then removed again. The object and the directories left empty below the
cached chain are now removed through the deepest cached ancestor with the
rest of the path; ENOENT through that handle is checked for staleness as
before. Deletes that sync still resolve the whole chain.

(cherry picked from commit b67d8e7)
…he cached chain

Cleanup removed uncached levels through the deepest cached ancestor, but a
handle cached below such a level (its parent evicted for capacity) stayed
cached with its descriptor and removed inode until the clock reached it,
and for good if it was dirty. Each removed level's handle is evicted.

(cherry picked from commit 1d60a37)
A failure to open or fsync the directory holding the store root, permission
errors included, is an error again whether or not this call created the
root; the error names that directory and the opt-out.
with_best_effort_root_parent_sync(true) skips the fsync on permission
errors only, at the cost that the root's directory entry may not survive
a power loss. Disabled by default; set per store, as with_fsync; no effect on other
targets.

(cherry picked from commit 2547870)
…kipped

The root handle is shared by clones while the best-effort option is per
store, so once a clone with the option had opened and cached the root
without fsyncing its parent, a store without the option found the root
cached and wrote without that fsync. The cache records whether the
parent was fsynced since the root was opened; a caller without the
option fsyncs it on a cache hit when it was not, and the record is
cleared when the root is evicted. One relaxed load per operation.

(cherry picked from commit 44a1087)
… them

A directory rename evicted the moved directory's handle while it, or a
handle under it, still owed an fsync for a change (a staged multipart
upload, a write in flight). The next write into the moved directory opens
new handles that know nothing of those changes and returns without
fsyncing them. The dirty handles at and under the moved path are fsynced
before the eviction; a change still in flight during the rename stays
uncovered. File renames are unchanged; directory renames scan the cache
once and fsync each dirty handle under the moved path.

(cherry picked from commit 6a36c57)
… opened

A directory this call made and then found removed before it could open
it was made again without limit, so a remover winning every round kept
the operation looping, as the path code's directory creation does. After
16 rounds the creation fails with the ENOENT.

(cherry picked from commit 0359b07)
A durable multipart upload whose future was dropped after it had staged
its file (a cancelled put_multipart) removed the staging file through
the Staging guard but left every handle on its chain dirty, and dirty
handles are never evicted: each cancelled upload into a new directory
pinned a descriptor for good. Upload now removes its staging file and
fsyncs its chain when dropped unpublished; complete and discard take the
staging first, so nothing is cleaned up twice.

(cherry picked from commit 6a89cde)
Only the directory-handle cache raises UnableToSyncRootParent; on the
other targets the variant was never constructed, which fails a
-D warnings build there.

(cherry picked from commit 257b341)
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.

1 participant