Skip to content

feat: clean up old revisions according to a cleanup strategy - #895

Merged
pbleser-oc merged 29 commits into
opencloud-eu:mainfrom
pbleser-oc:feat/1419-clean-up-file-versions
Nov 19, 2026
Merged

pbleser-oc merged 29 commits into
opencloud-eu:mainfrom
pbleser-oc:feat/1419-clean-up-file-versions

Conversation

@pbleser-oc

@pbleser-oc pbleser-oc commented Oct 21, 2026 •

Copy link
Copy Markdown
Member

Implements the reva side of opencloud-eu/opencloud#1419: old revisions (versions) are deleted according to a configurable cleanup strategy, for the decomposed, decomposeds3 and posix drivers.

The age of a revision is the time it was created, i.e. when its content was replaced, by the clock of the server, as agreed in the team on @aduffeck's proposal.

Fixes opencloud-eu/opencloud#3735

The OpenCloud side (configuration and the opencloud storage-users revisions cleanup command) follows in a separate PR in opencloud-eu/opencloud, once this one is merged.

What it does

Cleanup whenever a file gets a new revision

Triggers:

  • after an upload replaced an existing file, once it has finished: the upload coordinator (pkg/upload) calls the new optional storage.UploadFinishedHandler of the driver from finished(), which every successful upload passes through, whether it was committed right away or after its postprocessing. Rolled back uploads never get there. Decomposedfs cleans up if the upload replaced existing content (versionCreated), so uploads of new files queue nothing;
  • when an older upload that a newer one superseded finishes and writes its content into the revision the newer upload kept for it: that revision only becomes a version then, so CommitUpload triggers the cleanup once the locks are released;
  • after a revision was restored; the revision holding the content from before the restore is kept by every cleanup, as long as the file still has the restored content (see "Restores" below);
  • with posix and enable_fs_revisions, after a change on disk created a revision, once the new content has been copied to .CURRENT.

Files with an upload still in postprocessing are skipped.

Revisions that are not versions are left alone and not counted: a revision that keeps the processing mark of an older upload (the older upload writes its content into it once its postprocessing is done), and revisions the coordinator keeps only to roll back an upload (node.KeptForRollback). They are reported as protected, with the reason. The blob the file itself uses is never deleted with a revision.

Strategies

  • auto (keeps one revision per time slot, with longer slots for older revisions),
  • a number of revisions (10),
  • a duration (30d; with an uppercase unit, 1D or 1W, counting from midnight),
    and combinations: 14d, auto, auto, 90d, 5, 30d, 7d, auto, 90d.

Empty or disabled (the default) keeps everything. An invalid strategy makes the driver fail at startup.

Restores

RestoreRevision marks the revision it creates of the content it replaced with node.KeepAfterRestoreAttr, while it still holds the node lock. The value is the blob id of the restored content, and every cleanup keeps the revision as long as the file has that blob id: also a cleanup in the other driver instance of the process, one on demand in a process of its own, and one after a request was dropped from a full queue. The next upload or restore, or a change on disk with posix, gives the file a new blob id: from then on the revision is a version like any other, and the next cleanup that isn't a dry run removes the mark. Until then, the strategy doesn't apply to that one revision, also not an age limit like 30d.

In the background by default

  • a queue and workers per storage, so the cleanup doesn't slow down the write that triggered it. The storage provider and the data provider each create an instance of the driver: the instances of a storage in a process share one Service, which stops with the last Shutdown. Instances with different cleanup settings for the same storage fail at startup.
  • requests are de-duplicated per file,
  • and a file never occupies more than one worker

The worker count, the queue size and what happens when the queue is full are configurable. By default, a cleanup that finds the queue full is skipped (drop, logged with the file's location on disk), and happens with the file's next revision, so that a write never waits for the cleanup. With block, the write waits for room, but at most for a timeout (5s by default, 0 for as long as it takes), after which the cleanup is skipped as with drop: blocking without a bound would, under sustained load, slow down every write, e.g. the consumer of the postprocessing events.

A file whose space was disabled (moved to the trash) between being queued and being cleaned up is skipped, and its files are left alone.

Decomposedfs.Shutdown stops the workers.

A queued cleanup doesn't keep the request that triggered it alive: its context only carries the logger, with the request's fields and id, and it gets a trace of its own, a span revision cleanup linked to the request's span.

Node Lock

The node lock is held briefly: the blobs of deleted revisions are deleted after the lock is released, as with decomposeds3 every blob is a request. A revision only counts as deleted once its blob is; otherwise it is reported and counted as a failure.

Posix with use_space_groups

The cleanup switches to the group of the space before it touches its files (revisioncleanup.SpaceScoper), as the middleware does for user requests: in background workers already to read the file, and in on-demand runs already to find the file or folder to start from, as well as to walk the folders. A path on disk is resolved through the id cache only, which needs no group.

On Demand

Decomposedfs.CleanupRevisionsUnchecked (the revisioncleanup.OnDemand interface) cleans up all spaces, given spaces, or a file or folder within a space.

With posix, it can also clean up a file or folder given by its path on disk.

It supports

  • a dry run, which changes nothing on disk: it doesn't take the node lock, and reads under metadata.WithReadOnly, so that no lock files are created (see below); its decisions are therefore a snapshot,
  • its own strategy and async settings,
  • a timeout with a graceful stop (a space whose lookup the stop interrupted isn't reported as failed),
  • and a random order for runs that can't cover everything at once.

It reports a Result for every file.

It needs no user in the context and checks no permissions, as it is meant for command line tools.

Such a tool runs in a process of its own next to the server, and with a metadata cache local to the process, like the default memory cache, its cache doesn't see what the server changes. The cleanup of a file therefore reads the file's status and blob id from storage, under the file's lock (Backend.Reload, see below): it sees an upload the server started after the walk read the file, and keeps the revision that upload rolls back to should it fail.

Prometheus Metrics

reva_revision_cleanup_*: deleted revisions and bytes, failed deletions, queue length, capacity and full events, workers.

A Reason for Every Decision

Logged at debug level for every deleted revision, e.g. deleted by "auto" (last hour): its slot of 1 minute starting … already keeps a newer revision.

Configuration

New decomposedfs options, which posix inherits:

option default
revisions_cleanup_strategy "" (disabled)
revisions_cleanup_async true
revisions_cleanup_async_workers 0 (half the CPUs, at least 2)
revisions_cleanup_async_queue_size 0 (64 per worker)
revisions_cleanup_async_queue_full drop
revisions_cleanup_async_queue_full_timeout 5s (for block; 0 waits as long as it takes)

Changes to shared code

  • node.KeptForRollback: the coordinator's check for revisions kept only for a rollback (Decomposedfs.keptForRollback, with its two attributes) moved to package node, as node.KeptForRollback, node.UploadSnapshotAttr and node.PurgeOnRestoreAttr, so that the cleanup uses the same definition. A separate commit, no functional change.
  • Naming of functions without permission checks: they are named after their checked counterpart with an Unchecked suffix, so that they aren't used by mistake where permissions must be checked. For that, a separate commit renames the existing Node.DeleteRevision to Node.DeleteRevisionUnchecked, as Decomposedfs.DeleteRevision, of the same name, checks permissions. The functions of the cleanup that keep their names, e.g. Cleaner.Cleanup, say in their doc comment that they check no permissions.
  • Permission checks of revisions in Decomposedfs: Decomposedfs.ListRevisions and DownloadRevision check the permissions, like RestoreRevision and DeleteRevision already did, instead of the two trees. node.Tree.ListRevisions now takes the node and checks none, so that the cleanup can use it, and Tree.DownloadRevision takes the node and the user's permissions, for the resource info. There is also a new ListFolderKnownUnchecked for the on-demand walk, which, unlike ListFolder, checks no permissions and lists only the children known to the storage: with posix, it skips files created on disk that aren't assimilated yet instead of assimilating them, so that a cleanup, a dry run in particular, changes nothing but the revisions it deletes.
  • Deleting a revision needs no driver-specific code. revisioncleanup.PurgeRevisionUnchecked validates the key (node.ValidateRevisionKey, which rejects keys that would turn into an empty version), then calls Node.DeleteRevisionUnchecked and Tree.DeleteBlob.
  • aspects.Aspects has a new field, RevisionsCleanup. The posix test env passes it on, as posix.New does.
  • storage.UploadFinishedHandler, new: an optional interface of the storage drivers, which the upload coordinator calls from finished(). The middleware and posixFS pass it on. CommitUpload keeps a trigger of its own for an older upload that writes its content into the revision a newer one kept for it, as only then does that revision become a version. It and RestoreRevision trigger the cleanup after the node lock is released (a cleanup waiting for room in a full queue must not hold the lock).
  • RestoreRevision and DownloadRevision now check that the revision still exists on disk and return "not found" otherwise. Its metadata may come from a stale cache entry: with the default in-memory metadata cache, a revision deleted by another process, e.g. the OpenCloud command that cleans up on demand, stays in the server's cache. Restoring it reported success on decomposed but pointed the file to the deleted blob. A commit of its own, with tests on decomposed and posix.
  • metadata.Backend.Reload, new: reads all attributes of a node from storage, never from the cache, and updates the cache. The messagepack backend answers Get from its cache alone once it holds an entry for the node, even for an attribute the entry lacks, so a separate process can't see a processing mark the server set later. Implemented by the three backends, with the mock regenerated; a commit of its own, with tests that use two backends with caches of their own.
  • metadata.WithReadOnly, new: a context for reads that must not change anything on disk. Under it, the backends that lock to read (the hybrid backend of posix in All and Reload, and the xattrs backend in its locked retry) lock a node's lock file only if it exists, and read without a lock otherwise, instead of creating the lock file. Only the dry run uses it, also in the background workers, whose context doesn't carry the marker of the request: a read without a lock can see a write halfway through. For the same reason, what the hybrid backend read without a lock isn't cached, where it could replace what a writer cached meanwhile in a cache shared with the server. A commit of its own, the last one.
  • RestoreRevision keeps the node's lock file. It removed the lock file after releasing the lock, which the metadata backends warn against (Treesize propagation seems to be broken opencloud#1793): whoever waits for the lock then locks the removed file while the next writer locks a new one. The cleanup that runs after a restore, also in the background, is such a waiter. A commit of its own, before the cleanup is wired in.
  • UnixMapper.ScopeUserByIds, which the cleanup calls for every file with use_space_groups, leaves the goroutine locked to its OS thread when it fails. That would pin a background worker to its thread for good; the fix is in its own PR, fix: unlock the OS thread when ScopeUserByIds fails #899.
  • spaceidindex.Index.Indexes lists the indexes, so that "all spaces" covers every space type.
  • posixFS forwards CleanupRevisionsUnchecked and HandleUploadFinished.
  • The strategies live in the new package pkg/storage/revisions, which only plans what to keep and what to delete and touches no storage. It rejects X1, X2 pairs of the same kind, with the equivalent single value in the error. A number of revisions is counted in absolute terms. Only days and weeks can count from midnight, with the uppercase units D and W.

Documentation

  • pkg/storage/pkg/decomposedfs/revisioncleanup/README.md, for operators: options, strategies with examples, how the age of a revision is measured, when the cleanup runs, metrics with PromQL examples, logging.
  • pkg/storage/pkg/decomposedfs/README.md, new: how the three drivers are built on decomposedfs, with diagrams, including the steps of an upload the upload coordinator calls. It documents the code as it is on main, so it has a commit of its own, the first one, together with package comments for decomposedfs, decomposed, decomposeds3 and posix that point to it. The feature commits then add the few lines about the revision cleanup.

Review notes

  • Twenty-nine commits. The last one makes the cleanup go by the time a revision was created (@aduffeck's review, see below). The seven before it address the review of 2026-11-18, one point each: the hook in the coordinator, the mark of a restore, one Service per storage, the permission checks in Decomposedfs, and three small ones. Before them: the documentation of the existing drivers, the Node.DeleteRevision rename and the move of keptForRollback first, then one per step of the feature, with two fixes of shared code where they are needed (the lock file of RestoreRevision, Backend.Reload), and, at the end, a fix of the posix test environment (it ignored the configured file metadata cache, so no posix test could run without one) and metadata.WithReadOnly. The first four steps build the cleanup bottom up: the strategies, listing and deleting revisions, the cleaner that applies a strategy to a node, and only then the wiring that calls it. Each commit builds and passes its tests. Reviewing commit by commit is probably easier than the whole diff (72 files, ~10100 lines, more than half of them tests).
  • The default is disabled, while the issue names auto as the default. Switching the cleanup on deletes versions, so I kept it off in reva; whether OpenCloud should default to auto is still to be decided.
  • Overlapping async uploads: the processing mark of a file only tracks its latest upload, so the cleanup can run while an older upload is still in postprocessing, e.g. when an empty file is uploaded meanwhile, which the coordinator finishes right away. The revision the newer upload kept for the older one carries the older upload's processing mark, and the older upload writes its content into it when it is done; CommitUpload would discard that content if the revision were gone. The cleanup therefore never deletes, nor counts, such a revision. Once the older upload has written its content into it, the revision is a version, and CommitUpload runs the cleanup for it (tested end to end through the coordinator).
  • Changes to the upload code of the coordinator (coordinator.go, commit_upload.go, @butonic): the call of UploadFinishedHandler in finished(), the trigger in CommitUpload, and the move of keptForRollback. The recovery of an interrupted synchronous upload (recover.go) triggers nothing: it is rare, and the file's next revision triggers the cleanup.
  • The age of a revision is the time it was created, i.e. when its content was replaced. CreateRevision (decomposed and posix) and the posix assimilation record it with the clock of the server, in the new attribute user.oc.revision.created (node.SetRevisionCreated, node.RevisionCreated), and the cleanup orders the revisions and measures their age by it. The content mtime in the name of a revision, which clients choose with X-OC-Mtime, would let anyone who can write a file push other revisions out of their slots of auto, or, dated years ahead, out of a number of revisions to keep, and it made 30d delete the previous content of a file unchanged for two months as soon as it was edited. auto keeps the oldest revision of each slot, which holds the content the file had when the slot began. Revisions created before get an estimate of when they were replaced: the content mtime of the next newer revision, or the mtime of the file for the newest one, never later than now. The names of the revisions, the version list and X-OC-Mtime stay as they are, so the version list still shows the content mtime (Revision cleanup: should the age of a revision be the age of its content, or the time it was replaced? opencloud#3735).

Follow-ups

Tests

Unit tests for the strategies and the parser (including the reasons), the Service (de-duplication, queue full policies, shutdown, Wait, the context of a queued cleanup), the metrics, and the cleanup on real decomposedfs and posix trees: uploads through the upload coordinator (finished right away, after postprocessing that succeeds or fails, and an empty file uploaded while an older upload is in postprocessing, whose revision is cleaned up once the older upload is done), the coordinator telling the driver that an upload finished (with and without a new version, and not for a rolled back upload), restores, the mark of a restore (kept by every cleanup, also by another cleaner, until the file gets other content; stale marks removed, but not in a dry run), one Service per storage in a process, changes on disk, revisions that are not versions, a revision sharing the blob of the file, blobs deleted only after the node lock is released, on-demand runs including dry runs, timeouts (also during the lookup of a space), paths on disk and spaces given twice, an upload the server started after an on-demand run had read the file, Backend.Reload on all three backends, dry runs that create no lock files (also with posix, and in background workers that read every node from disk), the lock file kept by a restore, and restoring and downloading a revision deleted by another process, on decomposed and posix. The tests of new code paths were checked to fail without the change they test. Every commit builds and passes go vet; make test and make lint pass on a37f627; on the last commit, 39a0177, the tests of pkg/storage/... and pkg/upload/... pass with -race. The integration tests (tests/integration) fail locally in the same way with and without this PR: 8 public-link specs fail on main too, and the whole suite runs into its 15 minute timeout. End to end, the OpenCloud PR adds Behat acceptance tests for the command and for the configured strategy, also with the default cleanup in background workers. reva's own acceptance runs (OpenCloud's coreApiVersions suite on decomposed, decomposeds3 and posix) run with the cleanup off, its default, so they show that versions behave as before; the cleanup itself is tested end to end only in the OpenCloud PR.

Not tested: the actual switch to the group of a space with use_space_groups. The tests check that the cleanup asks for it (with a stand-in), also to read a file in a background worker and to find where an on-demand run starts, and that the posix tree puts the group of the space root into the context, but switching the fsgid needs CAP_SETGID, and as root file permissions don't apply. The switch itself is the existing UnixMapper.ScopeUserByIds, which the middleware and the assimilation use too; what's new is only that the cleanup calls it. A check on a server with use_space_groups is still to be done.

@pbleser-oc pbleser-oc self-assigned this Oct 21, 2026
@github-project-automation github-project-automation Bot moved this to Qualification in OpenCloud Team Board Oct 21, 2026
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Oct 21, 2026
Points github.com/opencloud-eu/reva/v2 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva 4ebaa069b), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.

@butonic butonic left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, I could lament that some of the changes are not really necessary for the retention, but then again, they all prepare the code to be testable. Ultimately, they make the retention feature properly testable.

I'm happy to merge this now and change the implementation after we decided opencloud-eu/opencloud#3735, unless someone thinks we should have a non-surprising retention policy from the start.

@butonic

butonic commented Oct 21, 2026

Copy link
Copy Markdown
Member

two comments from a local review:

pkg/storage/pkg/decomposedfs/revisioncleanup/service.go, Service.send:

With the default block policy, a driver Service blocks the calling request goroutine (FinishUploadDecomposed, MarkProcessing, CommitUpload, RestoreRevision) until a worker frees a queue slot: stopped is only set for on-demand runs, so the request's context is never considered. This is documented as intentional backpressure and can't deadlock (the node lock is released before enqueueing, workers never block in send), but under sustained load with a slow blobstore (decomposeds3) it directly throttles uploads — and with FinishUploadDecomposed's context.WithoutCancel, even a disconnected client keeps waiting.

Options worth considering: default to drop (nothing is lost permanently, the node is cleaned up again at its next revision, and the on-demand run covers the rest), or make block bounded (wait for room up to a timeout, then drop and count it on reva_revision_cleanup_queue_full_total).

Since this can be changed and is disabled by default, not a merge blocker and we can revisit this after a loadtest.


pkg/storage/pkg/decomposedfs/revisioncleanup/service.go, Service.cleanupNode:

node.ReadNode(ctx, s.lu, spaceID, nodeID, "", false, nil, false) passes canListDisabledSpace=false, so a queued cleanup of a file in a disabled (trashed) space fails with "could not read node" and is reported as an error result — while the on-demand path (Decomposedfs.revisionCleanupRoot) explicitly distinguishes disabled spaces and reports them as skipped. Cosmetic, but the worker path could do the same (read with true, check IsDisabled, report Skipped) so disabled spaces don't show up as cleanup failures in the results/logs.

@pbleser-oc
pbleser-oc force-pushed the feat/1419-clean-up-file-versions branch from 4ebaa06 to 6083983 Compare October 21, 2026 16:03
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Oct 21, 2026
Points github.com/opencloud-eu/reva/v2 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva 608398352), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.
@pbleser-oc
pbleser-oc force-pushed the feat/1419-clean-up-file-versions branch from 6083983 to eba5978 Compare October 21, 2026 16:49
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Oct 21, 2026
Points github.com/opencloud-eu/reva/v2 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva eba5978bc), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.
@pbleser-oc

Copy link
Copy Markdown
Member Author

Thanks, both fixed, amended into the commits they belong to:

  1. Unbounded block: block now waits at most revisions_cleanup_async_queue_full_timeout (default 5s, 0 for as long as it takes), then skips the cleanup of the node like drop, logs it with the node's location on disk, and counts it as dropped in reva_revision_cleanup_queue_full_total (after blocked when it started to wait). In addition, drop is now the default, so that a write never waits for the cleanup unless configured to. The on-demand runs are the exception: they queue files as fast as the walk finds them, so dropping them would skip most of them. Without AsyncOptions of their own, they wait for room as long as it takes, and the opencloud storage-users revisions cleanup command defaults to --queue-full block with --queue-full-timeout 0.
  2. Disabled space: cleanupNode now reads the node with canListDisabledSpace=true, and skips it if its space is disabled. On-demand runs report it as skipped ("the space is disabled") instead of as an error.

@pbleser-oc
pbleser-oc force-pushed the feat/1419-clean-up-file-versions branch from eba5978 to 60d1d78 Compare October 23, 2026 06:17
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Oct 23, 2026
Points github.com/opencloud-eu/reva/v2 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva 60d1d7896), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.
@pbleser-oc

pbleser-oc commented Oct 23, 2026 •

Copy link
Copy Markdown
Member Author

Issues and PRs connected to this one, grouped by how they relate to it:

Merge order

  1. this PR
  2. feat: clean up old revisions according to a cleanup strategy opencloud#3778, configuration and opencloud storage-users revisions cleanup command. It temporarily uses this PR's branch from my fork; once this PR is merged, that becomes a regular reva bump.
  3. docs(admin): document the cleanup of old file versions docs#1222, admin docs for the configuration and the command of feat: clean up old revisions according to a cleanup strategy opencloud#3778

Independent, in any order: #899, ScopeUserByIds leaves the goroutine locked to its OS thread on errors. Found here, as the cleanup's background workers call it for every file with use_space_groups.

Follow-ups of this PR

Overlapping uploads

Found while checking how the cleanup copes with a file that gets a new upload while an older one is still in postprocessing:

posix

pbleser-oc added a commit to pbleser-oc/reva that referenced this pull request Oct 23, 2026
UnixMapper.ScopeUserByIds locks the goroutine to its OS thread before it
switches the fsuid and the fsgid, as they belong to the thread. The
function it returns restores them and unlocks the thread again. On an
error, though, it returned without that function and without unlocking:
the goroutine stayed locked to its thread for good, and a fsuid it had
already switched before the fsgid failed was not restored.

For a short-lived goroutine of a request, Go discards the locked thread
when the goroutine ends. Long-lived goroutines would stay pinned to their
thread after a single failure, with one more lock nested for every
further one. The background workers of the revision cleanup proposed in
opencloud-eu#895 are such goroutines: with use_space_groups, they
switch to the group of a space for every file.

ScopeUserByIds now restores the fsuid and unlocks the thread on every
error path.

Tests: the fsuid and fsgid are restored after switching, and unchanged
after a failed switch to a group the process is not in (skipped where
the switch succeeds, e.g. as root). Whether the thread was unlocked
can't be observed from Go, there is no API to query it.
@pbleser-oc
pbleser-oc force-pushed the feat/1419-clean-up-file-versions branch from 60d1d78 to e42dd53 Compare October 23, 2026 06:49
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Oct 23, 2026
Points github.com/opencloud-eu/reva/v2 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva e42dd538c), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.
@pbleser-oc

Copy link
Copy Markdown
Member Author

I added one commit after approval, 2bcbce0

Why: opencloud storage-users revisions cleanup (opencloud-eu/opencloud#3778) runs its own storage driver in its own process, next to the running server. With the default in-memory file metadata cache (STORAGE_USERS_FILEMETADATA_CACHE_STORE=memory), the server doesn't learn about the revisions the command deletes: their entries stay in the server's cache, for up to 24 hours. The same problem with opencloud posixfs scan is opencloud-eu/opencloud#3804.

The command only deletes revisions, so the only stale entries are those of deleted revisions. But RestoreRevision and DownloadRevision trusted that metadata without checking the disk. Reproduced locally with a server and the command built from these branches:

restore of a revision the command deleted restore file afterwards
decomposed, the server still has the revision's metadata cached (e.g. the Versions panel listed it) HTTP 204, reported as successful broken: GET returns 500, the node points to the revision's deleted blob; the previous content survives as a new revision
decomposed, server restarted before the restore HTTP 500 intact
posix, still cached HTTP 500 intact

All three cases also left a revision of the current content behind, as RestoreRevision created it before reading the revision.

The fix:

  • Decomposedfs.RestoreRevision checks that the revision exists on disk, under the node's lock (which deleting a revision takes too), before it creates the revision of the current content; otherwise it drops the stale cache entry and returns NotFound.
  • Tree.DownloadRevision (decomposed and posix) checks the same and returns NotFound.

With the fix, the stale-cache restore above returns HTTP 404, the file stays intact, and no revision is left behind. The new tests in revisions_test.go simulate a revision deleted by another process (cache entry kept, files removed) and fail without the fix. They cover decomposed only: the posix test helper builds its tree without permissions, so the posix DownloadRevision change has no test of its own.

This affects any stale metadata cache (e.g. several storage-users instances with the memory store), not only the cleanup command. The remaining parts belong to the other PRs: in opencloud-eu/opencloud#3778, failing fast without NATS and reacting to Ctrl+C while the command opens the driver (it has the problem of opencloud-eu/opencloud#3805), and in opencloud-eu/docs#1222, describing what running the command next to the server implies.

@pbleser-oc
pbleser-oc requested a review from aduffeck October 25, 2026 01:03
@pbleser-oc

Copy link
Copy Markdown
Member Author

@aduffeck could you review 2bcbce0? It touches RestoreRevision for both drivers and the posix DownloadRevision, see the comment above.

pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Oct 25, 2026
Points github.com/opencloud-eu/reva/v2 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva 2bcbce084), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.
A dry run of the revision cleanup doesn't take the node lock, but with
posix, it still created lock files: the hybrid metadata backend locks
to read the metadata of a node that isn't cached, and creates the lock
file (and the folder for it) to do so. The on-demand cleanup runs in a
process of its own, with a cache of its own, so it reads, and locked,
every file it visited.

metadata.WithReadOnly marks a context for reads that must not change
anything on disk; metadata.IsReadOnly tells whether a context is one.
Under it, the backends that lock to read (the hybrid backend in All and
Reload, and the xattrs backend in its locked retry of a failed listing)
lock the lock file of a node only if it exists, and read without a lock
otherwise. A read without a lock can see a write to the node halfway
through, which is acceptable for the decisions of a dry run, which are
a snapshot anyway, but never for the server: only the dry run uses it.
What the hybrid backend read without a lock isn't cached, as it could
replace what a writer cached meanwhile in a cache shared with the
server. Writes still lock, and create lock files, as before.

A dry run (Cleaner.DryRun, and the on-demand cleanup of decomposedfs and
posix, including the resolution of a path on disk and the reads of the
background workers, whose context doesn't carry the marker of the
request) reads under it. The
messagepack backend of decomposed and decomposeds3 doesn't lock to read,
so there, not taking the node lock was enough already.

Tests: the hybrid backend, and the locked retry of the xattrs backend
(for a missing file, whose attributes can't be listed), read without
creating the lock file under WithReadOnly, still create it otherwise,
and still wait for a lock that is held when the lock file exists; a dry
run of the on-demand cleanup with posix leaves no lock files, also in
the background with no file metadata cache, where the workers read
every node from disk; a read without a lock isn't cached, one under the
lock is. They fail without the change.

Assisted-by: Claude Code:claude-opus-5-5
…s an upload

The upload coordinator now tells drivers that implement the new optional
storage.UploadFinishedHandler interface when an upload has finished:
committed right away, synchronously, or after its postprocessing. Rolled
back uploads never get there.

Decomposedfs uses it to apply the revision cleanup strategy when the upload
replaced existing content, instead of triggering it at the end of
PrepareUpload and when MarkProcessing clears the processing mark. The latter
also fired after a rollback and for uploads that created a new file, so a
bulk upload of new files filled the cleanup queue with nodes that have no
revisions at all. It also removes the unlocking before the trigger in
MarkProcessing.

CommitUpload keeps its own trigger for an older upload that writes into the
revision a newer upload kept for it: only then does that revision become a
version, which the versionCreated of the older upload doesn't tell.

The middleware and the posix driver pass the call on to the wrapped driver.

Assisted-by: Claude Code:claude-opus-5-5
…eanup request

RestoreRevision protected the revision of the content it replaced only by
passing its key along with the request for the cleanup. That protection was
lost whenever the request was: when a full queue dropped it, after the
timeout of a blocked caller, at a shutdown, for a cleanup by the other
instance of the driver in the same process (the storage provider and the
data provider each create one), and for a cleanup on demand in a process of
its own. It also lost a race: a worker that had taken the request of an
earlier upload before the restore listed the new revision without its key,
and a strategy deleting by age removed it right away, as the revision is
named after the mtime of the content it holds.

RestoreRevision now marks that revision with node.KeepAfterRestoreAttr while
it still holds the node lock. The value is the blob id of the restored
content: every cleanup keeps a marked revision as long as the file still has
that blob id. The next upload or restore, or a change on disk with posix,
gives the file a new blob id; from then on the revision is a version like
any other, and the next cleanup that isn't a dry run removes the mark.

The revisions to keep are gone from the Service, its requests and the
Cleaner, which now only read the marks.

Assisted-by: Claude Code:claude-opus-5-5
… instances

A process may set up the same storage more than once: the storage provider
and the data provider each create an instance of the storage driver. Each
instance got a revision cleanup Service of its own, so a storage ran twice
the configured workers, the gauges added up, and the de-duplication of the
requests per node didn't work across the two: a node could be queued and
cleaned up by both at the same time.

NewServiceFromOptions now returns one Service per storage root in a
process. The first instance creates it, with its lookup and revision store,
the others reuse it. Each instance shuts it down once, and only the last
Shutdown stops the workers. Instances that disagree on the strategy or the
background settings are rejected at startup, as the storage would otherwise
be cleaned up with the settings of whichever came first.

Assisted-by: Claude Code:claude-opus-5-5
…in Decomposedfs

The trees of decomposedfs and posix checked the permissions in
ListRevisions and DownloadRevision themselves, while RestoreRevision and
DeleteRevision check them in Decomposedfs. That is why the revision
cleanup, which lists revisions on behalf of the system, needed a
ListRevisionsUnchecked twin in both trees.

Decomposedfs.ListRevisions and DownloadRevision now check the permissions,
like the other revision operations, and Tree.ListRevisions and
Tree.DownloadRevision check none: Tree.ListRevisions takes the node and
replaces ListRevisionsUnchecked, and Tree.DownloadRevision takes the node
and the permissions of the user, for the resource info. This also removes
the duplicated check from the two trees.

Assisted-by: Claude Code:claude-opus-5-5
… node

With posix and use_space_groups, a background worker switches to the
group of the space before it reads the node, and the Cleaner then
switched again: two stats of the root of the space and two pairs of
system calls per node.

scopeToSpace now records in the context the space the goroutine switched
to, and doesn't switch again for the same space. The worker and the
Cleaner run in the same goroutine; a request handed to a worker gets a
context of its own, so the record never reaches another goroutine.

Assisted-by: Claude Code:claude-opus-5-5
…ports them

The background cleanup has no one to report to and throws its Result
away, but it still built a RevisionResult for every revision of the file,
with the reason of the strategy for each, on every upload.

The Service now only asks the Cleaner for them when it reports its
results, i.e. in an on-demand run. Cleaner.Cleanup and DryRun still
return them.

Assisted-by: Claude Code:claude-opus-5-5
RestoreRevision releases the node lock before it triggers the revision
cleanup, or on any return before that. sync.OnceValue makes sure it is
released only once, instead of a hand-written flag.

Assisted-by: Claude Code:claude-opus-5-5
@pbleser-oc
pbleser-oc force-pushed the feat/1419-clean-up-file-versions branch from 2aeb7ff to a37f627 Compare November 18, 2026 17:55
@pbleser-oc

Copy link
Copy Markdown
Member Author

@butonic thanks for the thorough review. I went through all of it; the branch is rebased onto main (now /v3) and every point has a commit of its own, so they can be reviewed one by one:

  • Hook in coordinator.finished() (3f156b0): done as you proposed.
    • The coordinator calls the new optional storage.UploadFinishedHandler for every successful upload, the middleware and posix pass it on.
    • Decomposedfs cleans up when versionCreated is set, and the triggers in PrepareUpload and MarkProcessing are gone, including the unlock juggling.
    • CommitUpload keeps its trigger for the superseded case.
  • Persistent marker instead of keep (ccf1dd3): see my reply on RestoreRevision.
  • Two services per process (14b962e): NewServiceFromOptions now returns one Service per storage root in a process.
    • The first driver instance creates it, the other reuses it, and only the last Shutdown stops the workers.
    • Instances with different cleanup settings for the same storage fail at startup, as the storage would otherwise be cleaned up with the settings of whichever came first.
    • In OpenCloud both get the same settings.
  • Permission checks (077dbdf): moved up into Decomposedfs.ListRevisions and DownloadRevision, as you suggested.
    • Tree.ListRevisions now takes the node and checks nothing, so ListRevisionsUnchecked is gone from both trees.
  • Unchecked for the functions without a checked twin: I'd keep these three.
    • The suffix is meant as a warning at the call site for a function that skips the checks a user request needs, next to one that has them:
      • PurgeRevisionUnchecked does what Decomposedfs.DeleteRevision does, without its check,
      • ListFolderKnownUnchecked is ListFolder without the filtering per user,
      • and CleanupRevisionsUnchecked sits on the driver next to the methods that serve users, where the suffix says that it must not be wired to a user request.
    • You're right that the commit message only talks about the twins, though.
  • Smaller points: the second switch to the space group (8d69aaf), the per revision results (2d36401) and sync.OnceValue (a37f627), see the replies inline.

The PR description is updated.

pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Nov 18, 2026
Points github.com/opencloud-eu/reva/v3 at the head of
opencloud-eu/reva#895 in its fork (pbleser-oc/reva a37f627e2), which
adds the revision cleanup this branch configures and calls, and
re-vendors.

Temporary: to be replaced by a regular reva bump once
opencloud-eu/reva#895 is merged.

Assisted-by: Claude Code:claude-opus-5-5
@aduffeck

Copy link
Copy Markdown
Member

I would like to argue for one change before this is merged. The cleanup currently decides what to delete from timestamps the client chooses.

X-OC-Mtime becomes the file's mtime, PrepareUpload names the revision after that mtime, and revisionTime takes the revision's age and order from that name. Anyone who can upload to a file can therefore place revisions anywhere on the timeline. We want to keep X-OC-Mtime, so the cleanup has to stop relying on it.

I ran the PR's strategies against a few revision sets to see what that allows:

  • auto: a revision dated one second after an existing one lands in the same slot and wins. Uploading content with mtime T+1s and then uploading again deletes the version dated T. That works for any version of the file.
  • 3, auto, 3: three revisions dated years ahead hold the three newest places. Every real version is deleted on each cleanup, including versions other people create later. The README describes this for a client with a wrong clock and advises checking the client or the clock. That advice doesn't help when someone sets the dates on purpose, and correcting a clock doesn't remove the revisions that already exist. No API deletes a version, so the file stays in that state until those dates have passed.
  • 5, 30d, 3, auto, 7d, 10: future-dated revisions take the protected places or use up the count, and real versions are deleted that the configuration was meant to keep.

We agreed that writers can erase history in some configurations and that the docs will say so. This goes further. It affects every configuration, including the ones meant to protect versions. It can remove one specific version. The effect also outlasts the writer's access. The future-dated revisions stay in place, so the file keeps losing every new version after the share is revoked. Public links with edit rights go through the same handlePut, so the header is accepted there too.

I propose recording when a revision was created and letting the cleanup use only that time.

  • CreateRevision (decomposed and posix) and the rename in the posix assimilation store the current server time in a new attribute on the revision.
  • revisionTime returns that attribute. Counts order by it, durations measure from it, and auto assigns tiers and slots by it.
  • auto then keeps the oldest revision per slot instead of the newest. With creation times, the oldest revision in a slot holds the state the file had when the slot began. Keeping the newest would drop a version that was current for months as soon as the file is saved twice within the same ten-second slot.
  • Revisions without the attribute get an estimate: the content mtime of the next newer revision, or the file's mtime for the newest one, capped at the current time. That is roughly when they were replaced.

X-OC-Mtime keeps working as before. The file mtime, the revision key, the versions listing and everything clients see stay unchanged, and existing revisions need no migration. As a side effect 30d then means 30 days after a version was replaced, which is what opencloud-eu/opencloud#3735 is about.

The cleanup took the time of a revision from its name, the modification
time of the content it holds, which whoever writes the file can choose,
e.g. with X-OC-Mtime. A revision dated just after an existing one pushed
that one out of its slot of "auto", and revisions dated years ahead took
the places of a number of revisions to keep, so that every new version
was deleted, also after the writer lost access to the file.

CreateRevision (decomposed and posix) and the posix assimilation now
record the time of the server when they create a revision, in
node.RevisionCreatedAttr ("user.oc.revision.created"), with
node.SetRevisionCreated, which node.RevisionCreated reads it back with:
the two decide how the time is stored. The cleanup orders
the revisions and measures their age by it. The names of the revisions,
the versions listing and X-OC-Mtime stay as they are.
revisions.Revision.LastModificationTime is renamed to CreationTime
accordingly.

Revisions without the attribute, created before it was introduced, get
an estimate of when they were replaced: the content time of the next
newer revision, or the mtime of the file for the newest one, capped at
the current time.

"auto" now keeps the oldest revision of each slot instead of the newest:
with creation times, the oldest holds the content the file had when the
slot began, while keeping the newest would drop a version that was
current for months as soon as the file is saved twice within a slot.

A duration like "30d" now counts from when a version was replaced rather
than from when its content was written, which is the question of
opencloud-eu/opencloud#3735.

Assisted-by: Claude Code:claude-opus-5-5
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Nov 18, 2026
Points the temporary replace at the head of opencloud-eu/reva#895, whose
last commit makes the revision cleanup go by the time a revision was
created, recorded by the server, instead of the modification time of its
content, which clients choose with X-OC-Mtime. "auto" keeps the oldest
revision of each slot.

- The storage-users README explains the new age of a revision.
- The acceptance scenario outline that checked the age as the time the
  content was written is replaced by two scenarios: versions whose
  content is dated 60 and 50 days ago are kept by "30d", and a version
  with content dated 10 years ahead doesn't push out the newest one
  under "1".

Assisted-by: Claude Code:claude-opus-5-5
@pbleser-oc

Copy link
Copy Markdown
Member Author

@aduffeck thanks, that's a good catch, and I agree with the reasoning, including keeping the oldest revision per slot.

I've implemented it as a separate commit on top, 39a0177, so that it can still be dropped or moved to a follow-up:

  • CreateRevision (decomposed and posix) and the posix assimilation record the server time in user.oc.revision.created, written and read only through node.SetRevisionCreated and node.RevisionCreated.
  • The cleanup orders revisions and measures their age by it. revisions.Revision.LastModificationTime is renamed to CreationTime accordingly.
  • auto keeps the oldest revision per slot.
  • Revisions without the attribute get the estimate you described: the content mtime of the next newer revision, or the file's mtime for the newest one, capped at the current time.
  • X-OC-Mtime, the revision names and the versions listing are unchanged.

A test now has a revision named a hundred years ahead but created 200 days ago: 1 deletes it and keeps the real newest one.

Since it also changes what 30d means (opencloud-eu/opencloud#3735), I'd like to hear more opinions from the team before it's merged, so I've put a note at the top of the description: please don't merge before that's settled. The matching changes for opencloud-eu/opencloud#3778 and the admin docs are separate commits there as well.

@pbleser-oc

Copy link
Copy Markdown
Member Author

For the record, we decided to go with the recommendation of @aduffeck here, and implement the revision creation time change as well.

@pbleser-oc
pbleser-oc merged commit 5a237e9 into opencloud-eu:main Nov 19, 2026
19 checks passed
@openclouders openclouders mentioned this pull request Nov 18, 2026
1 task
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Nov 19, 2026
opencloud-eu/reva#895 (the revision cleanup) was merged into reva main
with 5a237e94c, so the temporary replace of its fork is no longer
needed. Along with it comes the other reva change merged since
37496dc6a, opencloud-eu/reva#1027 (OCM invite and provider fixes): an
invalid invitation token is now rejected with the actual reason, so the
acceptance test of an expired token expects "token not valid" instead of
"token has expired", as in opencloud-eu#4065.

Assisted-by: Claude Code:claude-opus-5-5
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Nov 19, 2026
Follows opencloud-eu/reva#895, which makes the revision cleanup go by
the time a revision was created, recorded by the server, instead of the
modification time of its content, which clients choose with X-OC-Mtime.
"auto" keeps the oldest revision of each slot (opencloud-eu#3735).

- The storage-users README explains the new age of a revision.
- The acceptance scenario outline that checked the age as the time the
  content was written is replaced by two scenarios: versions whose
  content is dated 60 and 50 days ago are kept by "30d", and a version
  with content dated 10 years ahead doesn't push out the newest one
  under "1".

Assisted-by: Claude Code:claude-opus-5-5
@pbleser-oc
pbleser-oc deleted the feat/1419-clean-up-file-versions branch November 19, 2026 10:16
butonic added a commit to opencloud-eu/opencloud that referenced this pull request Nov 19, 2026
We bump reva to 0093d72f4c9d, the merge of
opencloud-eu/reva#1035 on reva main. This
brings in the OCM unshare and permission change notifications, the ocm
core share events and the fixes to the permissions of OCM shares, on
top of the invite and provider fixes already on reva main.

It also pulls in the revision cleanup from
opencloud-eu/reva#895. Its strategy defaults
to empty, which keeps all revisions, so nothing changes until we wire
up the option.

Assisted-by: Claude Code:claude-opus-5-5
Signed-off-by: Jörn Friedrich Dreyer <[email protected]>
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Nov 19, 2026
Follows opencloud-eu/reva#895, which makes the revision cleanup go by
the time a revision was created, recorded by the server, instead of the
modification time of its content, which clients choose with X-OC-Mtime.
"auto" keeps the oldest revision of each slot (opencloud-eu#3735).

- The storage-users README explains the new age of a revision.
- The acceptance scenario outline that checked the age as the time the
  content was written is replaced by two scenarios: versions whose
  content is dated 60 and 50 days ago are kept by "30d", and a version
  with content dated 10 years ahead doesn't push out the newest one
  under "1".

Assisted-by: Claude Code:claude-opus-5-5
pbleser-oc added a commit to pbleser-oc/opencloud that referenced this pull request Nov 23, 2026
Follows opencloud-eu/reva#895, which makes the revision cleanup go by
the time a revision was created, recorded by the server, instead of the
modification time of its content, which clients choose with X-OC-Mtime.
"auto" keeps the oldest revision of each slot (opencloud-eu#3735).

- The storage-users README explains the new age of a revision.
- The acceptance scenario outline that checked the age as the time the
  content was written is replaced by two scenarios: versions whose
  content is dated 60 and 50 days ago are kept by "30d", and a version
  with content dated 10 years ahead doesn't push out the newest one
  under "1".

Assisted-by: Claude Code:claude-opus-5-5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Revision cleanup: should the age of a revision be the age of its content, or the time it was replaced?

3 participants