diff --git a/changelog/unreleased/bugfix-orphaned-upload-sessions.md b/changelog/unreleased/bugfix-orphaned-upload-sessions.md new file mode 100644 index 00000000000..b13cafcc038 --- /dev/null +++ b/changelog/unreleased/bugfix-orphaned-upload-sessions.md @@ -0,0 +1,28 @@ +Bugfix: Release the quota of upload sessions with unreadable node metadata + +When an upload's target node lost its metadata, e.g. because an ancestor was +moved to the trash while the upload was still in flight, the upload could never +finish processing. It stayed in "Processing" forever, could not be downloaded or +deleted, and kept consuming the space quota. + +Cleaning such a session up with `ocis storage-users uploads sessions --clean` +did not help either: it removed the uploaded bytes and the session info file +before failing to revert the node, so it destroyed the only copy of the data +without releasing any quota. + +Cleanup now reverts the node before removing anything irreversible and falls +back to the session metadata when the node cannot be read, so the quota is +released and the orphaned node is removed. If the quota cannot be released the +upload is kept so it can be retried instead of being lost. + +A new `--orphaned` filter lists the affected sessions: + +``` +ocis storage-users uploads sessions --orphaned +ocis storage-users uploads sessions --orphaned --clean +``` + +Note that evaluating the filter reads the node metadata of every session, so it +is only done when the flag is set. + +https://github.com/owncloud/ocis/pull/12739 diff --git a/go.mod b/go.mod index a9cf4582b70..5b8118295cf 100644 --- a/go.mod +++ b/go.mod @@ -67,7 +67,7 @@ require ( github.com/open-policy-agent/opa v1.19.0 github.com/orcaman/concurrent-map v1.0.0 github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245 - github.com/owncloud/reva/v2 v2.0.0-20260805165023-441d6e77b33d + github.com/owncloud/reva/v2 v2.0.0-20260806084131-831afd01e5d4 github.com/pkg/errors v0.9.1 github.com/pkg/xattr v0.4.12 github.com/prometheus/client_golang v1.24.1 diff --git a/go.sum b/go.sum index 364eb711c63..7843fca9729 100644 --- a/go.sum +++ b/go.sum @@ -736,8 +736,8 @@ github.com/orcaman/concurrent-map v1.0.0 h1:I/2A2XPCb4IuQWcQhBhSwGfiuybl/J0ev9HD github.com/orcaman/concurrent-map v1.0.0/go.mod h1:Lu3tH6HLW3feq74c2GC+jIMS/K2CFcDWnWD9XkenwhI= github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245 h1:JRidLTAKhnvyLMRtVtSF4lhBa0NSAOs6fof+d6JnKII= github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245/go.mod h1:z61VMGAJRtR1nbgXWiNoCkxUXP1B3Je9rMuJbnGd+Og= -github.com/owncloud/reva/v2 v2.0.0-20260805165023-441d6e77b33d h1:auQ960+PZrtSuX67xizNIhP1PPgYsL/mt/1Ea9NMtO4= -github.com/owncloud/reva/v2 v2.0.0-20260805165023-441d6e77b33d/go.mod h1:XBnHPPDOGYq+Uf/PD1wGXne3xTvoghBxXPUULcSZjlc= +github.com/owncloud/reva/v2 v2.0.0-20260806084131-831afd01e5d4 h1:Cnq24qhteHD30DfTG8o0bf3cnVL/2b+afz//0qGjAeU= +github.com/owncloud/reva/v2 v2.0.0-20260806084131-831afd01e5d4/go.mod h1:XBnHPPDOGYq+Uf/PD1wGXne3xTvoghBxXPUULcSZjlc= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c h1:rp5dCmg/yLR3mgFuSOe4oEnDDmGLROTvMragMUXpTQw= github.com/oxtoacart/bpool v0.0.0-20190530202638-03653db5a59c/go.mod h1:X07ZCGwUbLaax7L0S3Tw4hpejzu63ZrrQiUe6W0hcy0= github.com/pablodz/inotifywaitgo v0.0.12 h1:DVJJTFMDlSAdO46yisPF2vzozs/r+4k6ih8MVghq78M= diff --git a/services/storage-users/README.md b/services/storage-users/README.md index 77def6b3a55..a494aea127e 100644 --- a/services/storage-users/README.md +++ b/services/storage-users/README.md @@ -86,6 +86,7 @@ OPTIONS: --processing filter sessions by processing status (default: unset) --expired filter sessions by expired status (default: unset) --has-virus filter sessions by virus scan result (default: unset) + --orphaned filter sessions by whether their node metadata is unreadable (default: unset) --json output as json (default: false) --restart send restart event for all listed sessions (default: false) --resume send resume event for all listed sessions (default: false) @@ -140,6 +141,23 @@ ocis storage-users uploads sessions --expired=true --clean ocis storage-users uploads sessions --processing=true --has-virus=false --resume ``` +Uploads whose node metadata can no longer be read are orphaned: they can never finish +postprocessing, because the destination of the upload cannot be resolved. Such uploads stay +in processing state indefinitely and keep consuming the quota of their space. Use the +`--orphaned` filter to list them, and `--clean` to remove them and release the quota. + +```bash +# lists all orphaned upload sessions +ocis storage-users uploads sessions --orphaned=true + +# removes them and releases the quota they consume +ocis storage-users uploads sessions --orphaned=true --clean +``` + +Note: `--orphaned` reads the node metadata of every upload session, so it is slower than the +other filters. Cleaning an orphaned session deletes the uploaded bytes, which are the only +copy as long as postprocessing has not finished. Run the command without `--clean` first. + #### Delete Stale Nodes command diff --git a/services/storage-users/pkg/command/uploads.go b/services/storage-users/pkg/command/uploads.go index 318ad7231de..5980fe2be94 100644 --- a/services/storage-users/pkg/command/uploads.go +++ b/services/storage-users/pkg/command/uploads.go @@ -93,6 +93,11 @@ func ListUploadSessions(cfg *config.Config) *cli.Command { DefaultText: "unset", Usage: "filter sessions by virus scan result", }, + &cli.BoolFlag{ + Name: "orphaned", + DefaultText: "unset", + Usage: "filter sessions by whether their node metadata is unreadable. Such sessions can never finish processing and can be removed with --clean", + }, &cli.BoolFlag{ Name: "json", Usage: "output as json", @@ -259,6 +264,10 @@ func buildFilter(c *cli.Context) storage.UploadSessionFilter { infectedValue := c.Bool("has-virus") filter.HasVirus = &infectedValue } + if c.IsSet("orphaned") { + orphanedValue := c.Bool("orphaned") + filter.Orphaned = &orphanedValue + } if c.IsSet("id") { idValue := c.String("id") filter.ID = &idValue @@ -315,6 +324,24 @@ func buildInfo(filter storage.UploadSessionFilter) string { } } + if filter.Orphaned != nil { + if b.Len() != 0 { + b.WriteString(", ") + } + if !*filter.Orphaned { + if b.Len() == 0 { + b.WriteString("Not ") + } else { + b.WriteString("not ") + } + } + if b.Len() == 0 { + b.WriteString("Orphaned") + } else { + b.WriteString("orphaned") + } + } + if b.Len() == 0 { b.WriteString("Session") } else { diff --git a/services/storage-users/pkg/command/uploads_test.go b/services/storage-users/pkg/command/uploads_test.go index 4c6229e4fe8..e4d9ae17ab0 100644 --- a/services/storage-users/pkg/command/uploads_test.go +++ b/services/storage-users/pkg/command/uploads_test.go @@ -63,6 +63,26 @@ func TestBuildInfo(t *testing.T) { filter: storage.UploadSessionFilter{Processing: boolPtr(true), Expired: boolPtr(false), HasVirus: boolPtr(true), ID: strPtr("123")}, expectedInfo: "Processing, not expired, virusinfected session with id '123':", }, + { + alias: "orphaned", + filter: storage.UploadSessionFilter{Orphaned: boolPtr(true)}, + expectedInfo: "Orphaned sessions:", + }, + { + alias: "not orphaned", + filter: storage.UploadSessionFilter{Orphaned: boolPtr(false)}, + expectedInfo: "Not orphaned sessions:", + }, + { + alias: "processing and orphaned", + filter: storage.UploadSessionFilter{Processing: boolPtr(true), Orphaned: boolPtr(true)}, + expectedInfo: "Processing, orphaned sessions:", + }, + { + alias: "expired, not virus infected and orphaned", + filter: storage.UploadSessionFilter{Expired: boolPtr(true), HasVirus: boolPtr(false), Orphaned: boolPtr(true)}, + expectedInfo: "Expired, not virusinfected, orphaned sessions:", + }, } for _, tc := range testCases { diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/uploads.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/uploads.go index 59ff5088550..44b70274534 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/uploads.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/storage/uploads.go @@ -87,4 +87,8 @@ type UploadSessionFilter struct { Processing *bool Expired *bool HasVirus *bool + // Orphaned filters sessions by whether their target node can still be + // resolved. Evaluating it requires reading the node metadata of every + // session, so it is only evaluated when set. + Orphaned *bool } diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/decomposedfs.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/decomposedfs.go index 9c7862d9bb9..a0fb931893b 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/decomposedfs.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/decomposedfs.go @@ -314,7 +314,12 @@ func (fs *Decomposedfs) processEvent(evCtx context.Context, event events.Event, n, err := session.Node(ctx) if err != nil { - sublog.Error().Err(err).Msg("could not read node") + // The node metadata is unreadable, so this upload can never finish: + // the destination cannot be resolved. Clean the session up instead of + // leaving it behind to be retried forever. Cleanup falls back to the + // session metadata to release the quota. + sublog.Error().Err(err).Msg("could not read node, cleaning up orphaned session") + session.Cleanup(true, true, true, false) return } sublog = log.With().Str("spaceid", session.SpaceID()).Str("nodeid", session.NodeID()).Logger() diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload.go index 93e9674671d..888967b51ce 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload.go @@ -751,6 +751,11 @@ func (fs *Decomposedfs) ListUploadSessions(ctx context.Context, filter storage.U continue } } + // evaluated last: unlike the other filters this reads the node metadata + // from disk, so it is only done for sessions that passed all other filters + if filter.Orphaned != nil && *filter.Orphaned != session.IsOrphaned(ctx) { + continue + } filteredSessions = append(filteredSessions, session) } return filteredSessions, nil diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/session.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/session.go index 8d7a58ce68a..73c0e280347 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/session.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/session.go @@ -34,6 +34,7 @@ import ( typespb "github.com/cs3org/go-cs3apis/cs3/types/v1beta1" "github.com/owncloud/reva/v2/pkg/appctx" ctxpkg "github.com/owncloud/reva/v2/pkg/ctx" + "github.com/owncloud/reva/v2/pkg/errtypes" "github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/node" "github.com/owncloud/reva/v2/pkg/utils" ) @@ -166,6 +167,43 @@ func (s *OcisSession) Node(ctx context.Context) (*node.Node, error) { return node.ReadNode(ctx, s.store.lu, s.SpaceID(), s.info.Storage["NodeId"], false, nil, true) } +// IsOrphaned returns true if the session's target node can no longer be +// resolved. This happens when the node file still exists but its metadata is +// gone, e.g. because an ancestor was moved to the trash while the upload was in +// flight. Such a session can never finish postprocessing: reading the node +// fails before the destination can be determined. +func (s *OcisSession) IsOrphaned(ctx context.Context) bool { + _, err := s.Node(ctx) + return err != nil +} + +// syntheticNode builds a node from the session metadata alone, without reading +// the node from disk. It is used to clean up sessions whose node metadata is +// unreadable: the parent id is still recorded in the session, which is all that +// is needed to walk up the tree and revert the size propagation. +func (s *OcisSession) syntheticNode(ctx context.Context) (*node.Node, error) { + if s.NodeID() == "" || s.NodeParentID() == "" { + return nil, errtypes.InternalError("session has no node and parent id") + } + n := node.New( + s.SpaceID(), + s.NodeID(), + s.NodeParentID(), + s.Filename(), + s.Size(), + s.ID(), + provider.ResourceType_RESOURCE_TYPE_FILE, + nil, + s.store.lu, + ) + spaceRoot, err := node.ReadNode(ctx, s.store.lu, s.SpaceID(), s.SpaceID(), false, nil, false) + if err != nil { + return nil, err + } + n.SpaceRoot = spaceRoot + return n, nil +} + // ID returns the upload session id func (s *OcisSession) ID() string { return s.info.ID diff --git a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go index 0450ffe34bf..c20004bc2f6 100644 --- a/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go +++ b/vendor/github.com/owncloud/reva/v2/pkg/storage/utils/decomposedfs/upload/upload.go @@ -330,10 +330,71 @@ func (session *OcisSession) removeNode(ctx context.Context) { } } +// revertNode undoes the node changes made when the upload was initiated. For a +// readable node this restores the previous revision. When the node metadata can +// no longer be read the node is orphaned and can never finish postprocessing; in +// that case the node is removed and the optimistic size propagation is reverted +// using the parent id recorded in the session, so the space quota is released. +func (session *OcisSession) revertNode(ctx context.Context) error { + n, err := session.Node(ctx) + if err == nil { + curUpload, perr := n.ProcessingID(ctx) + if perr == nil && curUpload == session.ID() { + if rerr := n.RevertCurrentRevision(ctx, true); rerr != nil { + return rerr + } + } + return nil + } + + // The node is unreadable. Fall back to the session metadata, which still + // carries the node and parent ids needed to release the quota. + log := appctx.GetLogger(ctx) + log.Info().Err(err).Str("sessionid", session.ID()).Msg("node unreadable, cleaning up orphaned upload") + + sn, serr := session.syntheticNode(ctx) + if serr != nil { + return serr + } + + if sizeDiff := session.SizeDiff(); sizeDiff != 0 { + if perr := session.store.tp.Propagate(ctx, sn, -sizeDiff); perr != nil { + // Without the propagation the quota would stay consumed. Stop here + // so the session can be retried instead of losing the upload. + return perr + } + } + + // The orphaned node file cannot be resolved by any other means, remove it + // together with its metadata files. A missing node is not an error here: + // the node may never have been created. + nodePath := sn.InternalPath() + if rerr := utils.RemoveItem(nodePath); rerr != nil && !errors.Is(rerr, fs.ErrNotExist) { + log.Error().Err(rerr).Str("nodepath", nodePath).Msg("removing orphaned node failed") + } + if perr := session.store.lu.MetadataBackend().Purge(ctx, nodePath); perr != nil && !errors.Is(perr, fs.ErrNotExist) { + log.Error().Err(perr).Str("nodepath", nodePath).Msg("purging orphaned node metadata failed") + } + + return nil +} + // cleanup cleans up after the upload is finished func (session *OcisSession) Cleanup(revertNodeMetadata, cleanBin, cleanInfo, unmarkPostprocessing bool) { ctx := session.Context(context.Background()) + if revertNodeMetadata { + // Revert before removing the bin and info files. Both are needed to + // recover from a failure here: the bin file holds the only copy of the + // uploaded data as long as the blob has not been written, and the info + // file is the only remaining source of the node's parent id once the + // node metadata is gone. + if err := session.revertNode(ctx); err != nil { + appctx.GetLogger(ctx).Error().Err(err).Str("sessionid", session.ID()).Msg("reverting node failed, keeping upload") + return + } + } + if cleanBin { if err := os.Remove(session.binPath()); err != nil && !errors.Is(err, fs.ErrNotExist) { appctx.GetLogger(ctx).Error().Str("path", session.binPath()).Err(err).Msg("removing upload failed") @@ -346,22 +407,6 @@ func (session *OcisSession) Cleanup(revertNodeMetadata, cleanBin, cleanInfo, unm } } - if revertNodeMetadata { - n, err := session.Node(ctx) - if err != nil { - appctx.GetLogger(ctx).Error().Err(err).Str("sessionid", session.ID()).Msg("reading node for session failed") - return - } - - curUpload, err := n.ProcessingID(ctx) - if err == nil && curUpload == session.ID() { - if err := n.RevertCurrentRevision(ctx, true); err != nil { - appctx.GetLogger(ctx).Error().Err(err).Str("nodepath", n.InternalPath()).Msg("reverting node metadata failed") - return - } - } - } - if unmarkPostprocessing && !revertNodeMetadata { // node reverting automatically unmarks processing n, err := session.Node(ctx) if err != nil { diff --git a/vendor/modules.txt b/vendor/modules.txt index 2a2a505a521..3e2503d5cc2 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -1323,7 +1323,7 @@ github.com/orcaman/concurrent-map # github.com/owncloud/libre-graph-api-go v1.0.5-0.20260216101009-eeac018af245 ## explicit; go 1.18 github.com/owncloud/libre-graph-api-go -# github.com/owncloud/reva/v2 v2.0.0-20260805165023-441d6e77b33d +# github.com/owncloud/reva/v2 v2.0.0-20260806084131-831afd01e5d4 ## explicit; go 1.26 github.com/owncloud/reva/v2/cmd/revad/internal/grace github.com/owncloud/reva/v2/cmd/revad/runtime