Repository navigation
Description
With enable_fs_revisions, posix keeps a copy of each file's current content in <space>/.oc-nodes/<pathified id>.CURRENT, from which it creates a revision when the file is changed on disk. Uploads (Finalize → Tree.WriteBlob) and restores (RestoreRevision) write that copy while holding the node lock. The assimilation of a change on disk doesn't:
Tree.updateFile (pkg/storage/fs/posix/tree/assimilation.go) computes the file's checksums and then starts a goroutine that
- renames
.CURRENTto the revision<id>.REV.<mtime of .CURRENT>, - writes a new
.CURRENTwith the file's new content (io.Copy), - copies the checksums, type, blob id and size to it (
CopyMetadata).
Nothing waits for that goroutine: assimilate releases the node lock when it returns, independently of it. The goroutine also marks its node as lock held (currentNode.SetLockHeld(true)), so the metadata backend doesn't lock for it either, although no lock is held.
What can go wrong
All of this concerns a single file; different files never share a .CURRENT.
- A process exits while the goroutine is still copying. Nothing tracks the goroutines, so nothing waits for them.
opencloud posixfs scanreturns afterWarmupIDCacheand exits; the same applies to a server that is stopped while assimilating. A goroutine killed- after the rename and before the copy is done leaves
.CURRENTtruncated (or missing): the next change of the file creates its revision from that, so the previous content is lost as a version, and the revision is listed with wrong (or no) size and checksums; - before
CopyMetadataleaves.CURRENTwith the new content and the previous checksums and size.
- after the rename and before the copy is done leaves
- A change on disk overlapping an upload or a restore of the same file. The upload or restore holds the node lock, the goroutine doesn't, so both write
.CURRENTat the same time, and the result is a mix of both. - Two changes on disk in quick succession. The scan debouncer serializes the two assimilations of a path, but not their goroutines. If the second goroutine starts while the first is still copying, it renames the half-written
.CURRENTinto a revision, while the first keeps writing into it through its open file handle, and then copies its metadata onto the second's new.CURRENT. - Several processes.
opencloud posixfs scancreates a posix driver of its own and assimilates throughWarmupIDCache→assimilate, without the debouncer, which only works within one process anyway. The node lock is an OS file lock (lockedfile) and does serializeassimilateacross processes, so a second process that sees the same change usually skips the file; but it doesn't serialize the goroutines, so a scan running next to the server, or two scans, make 3. possible across processes.
Reproduction
Script oc-posix-current-races, run against an OpenCloud single binary server with the posix driver and STORAGE_USERS_POSIX_ENABLE_FS_REVISIONS=true, built with the reva branch of #1419 (current main plus the revision cleanup, whose trigger for changes on disk runs in that goroutine). The goroutine's rename, copy and metadata copy are the same on main, and the reproduction patch below applies to main as well; it was not run there.
Without help, the windows are too small on a fast local file system. On btrfs on NVMe, neither problem occurred, with files of 2 and 6 GiB: io.Copy between two files uses copy_file_range, which btrfs serves by sharing extents (1 GiB in 0.35 s), and the checksums are computed before the goroutine starts, so the copy finishes before the rest of the assimilation and the process do. The windows open where copying is slow compared to that, e.g. on network file systems, or with large files on slow disks.
So the window was widened: a reproduction-only patch makes the goroutine copy the first half, pause, then copy the rest (REVA_REPRO_CURRENT_COPY_PAUSE=20s), which is what a slow copy looks like to the rest of the system. With a 512 MiB file:
opencloud posixfs scanexits during the copy (server without watcher,STORAGE_USERS_POSIX_WATCH_FS=false, so that only the CLI assimilates; the file is uploaded, then replaced on disk by the 512 MiB file and scanned):- the scan exited after 0.95 s, with
.CURRENTat 268435456 of 536870912 bytes and nouser.oc.blobsize, and it stayed that way; - after the next change on disk and scan, the revision created from it holds those 268435456 bytes (sha1
ecdbdf10…instead ofbe0ce46f…), withoutuser.oc.blobsize: the version with the 512 MiB content is lost.
- the scan exited after 0.95 s, with
- An upload during the server's copy (server with the inotify watcher; the file is uploaded, replaced on disk by the 512 MiB file, and while the server's goroutine pauses halfway, a 9 byte file is uploaded):
- the file then holds the 9 bytes, as it should, but
.CURRENThas 536870912 bytes: the uploadedv3-small\n, zeros up to 256 MiB (the upload truncated the file the goroutine was writing into), and then the second half of the 512 MiB content, written by the goroutine at its old offset; - its metadata says 9 bytes, with the checksums of the upload, so the next change on disk turns 512 MiB of mixed content into a revision listed as the 9 byte version.
- the file then holds the 9 bytes, as it should, but
Cases 3 and 4 were not reproduced separately; they rest on the same unguarded goroutine.
Possible fix
- Take the node lock in the goroutine for the rename, the copy and the metadata, instead of
SetLockHeld(true). The lock is reentrant per goroutine only, so it has to be acquired there; uploads, restores and further assimilations of the file then wait for it. - Track the goroutines (e.g. a
sync.WaitGroupin the tree) and wait for them on shutdown, and letopencloud posixfs scanshut the driver down before it exits. That would also let it wait for the revision cleanup of 🧹 Clean-Up File Versions #1419, which runs in that goroutine and is, in its background mode, abandoned (or interrupted while deleting a revision) when the process exits. - Alternatively, do the
.CURRENTwork synchronously withinassimilate, while it still holds the node lock, at the cost of a slower assimilation of large files.
The goroutine predates #1419; its latest restructuring is opencloud-eu/reva@cc4bd5aab ("Properly handle MovedFrom events").
A patch to simulate slow storage during assimilation in reva can be found here: https://gist.github.com/pbleser-oc/7cc4c77818637808d51601cfb47a3c39
Metadata
Metadata
Assignees
Type
Projects
- StatusShow more project fieldsPrio <= 3
Activity