From f54f01a1b74a1cb2c65ecd912bc8f09c805414a8 Mon Sep 17 00:00:00 2001 From: Michal Klos Date: Tue, 21 Oct 2025 17:43:49 +0200 Subject: [PATCH 1/2] feat: CLI, ocis shares move-stuck-upload-blobs --- .../unreleased/add-cli-move-stuck-uploads.md | 5 + ocis/pkg/command/shares.go | 224 +++++++++++++++++- 2 files changed, 219 insertions(+), 10 deletions(-) create mode 100644 changelog/unreleased/add-cli-move-stuck-uploads.md diff --git a/changelog/unreleased/add-cli-move-stuck-uploads.md b/changelog/unreleased/add-cli-move-stuck-uploads.md new file mode 100644 index 00000000000..2f49c3dadf5 --- /dev/null +++ b/changelog/unreleased/add-cli-move-stuck-uploads.md @@ -0,0 +1,5 @@ +Enhancement: Add Cli to move stuck uploads + +In some cases of saturated disk usage ocis metadata may get stuck. This command relieves this case. + +https://github.com/owncloud/ocis/pull/11762 diff --git a/ocis/pkg/command/shares.go b/ocis/pkg/command/shares.go index 52faaf165df..2cfb8d10bdf 100644 --- a/ocis/pkg/command/shares.go +++ b/ocis/pkg/command/shares.go @@ -1,9 +1,15 @@ package command import ( + "encoding/json" "errors" + "fmt" + "os" + "path/filepath" + "strings" "github.com/rs/zerolog" + "github.com/shamaton/msgpack/v2" "github.com/urfave/cli/v2" "github.com/owncloud/reva/v2/pkg/rgrpc/todo/pool" @@ -25,18 +31,9 @@ func SharesCommand(cfg *config.Config) *cli.Command { Name: "shares", Usage: `cli tools to manage entries in the share manager.`, Category: "maintenance", - Before: func(c *cli.Context) error { - // Parse base config - if err := parser.ParseConfig(cfg, true); err != nil { - return configlog.ReturnError(err) - } - - // Parse sharing config - cfg.Sharing.Commons = cfg.Commons - return configlog.ReturnError(sharingparser.ParseConfig(cfg.Sharing)) - }, Subcommands: []*cli.Command{ cleanupCmd(cfg), + moveStuckUploadBlobsCmd(cfg), }, } } @@ -126,3 +123,210 @@ func cleanup(c *cli.Context, cfg *config.Config) error { return nil } + +// oCIS directory structure for share-manager metadata and user spaces: +// +// ocisHome/storage/ +// ... +// └── metadata/ +// ├── spaces/js/oncs3-share-manager-metadata/ (rootMetadata - Phase 1,3,4) +// │ ├── blobs/ +// │ │ ├── 9c/a3/b2/f5/-42a1-4b8e-9123-456789abcdef (Phase 4: MISSING received.json blob - reconstructed here) +// │ │ │ {"Spaces": {"215fee7a-...:480049db-...": {"States": {"...:...:84652da9-...": {State: 2, MountPoint: {path: "file.txt"}}}}}} +// │ │ └── d7/02/d7/e1/-37b0-4d41-b8dc-4b90c1d1f907 (Phase 1: read .json blob for Shares data) +// │ │ {"Shares": {"215fee7a-...:480049db-...:84652da9-...": {resource_id: {...}, grantee: {...}, creator: {...}}}} +// │ └── nodes/ +// │ ├── 3a/5f/c2/d8/-1234-5678-abcd-ef0123456789.mpk (Phase 4: received.json MPK → points to MISSING blob) +// │ │ {"user.ocis.name": "received.json", "user.ocis.blobid": "9ca3b2f5-42a1-4b8e-9123-456789abcdef", "user.ocis.parentid": "a9a54ce7-..."} +// │ ├── 99/98/b8/bf/-6871-49cc-aca9-dab4984dc1e4.mpk (Phase 1: .json MPK → points to Shares blob) +// │ │ {"user.ocis.name": "480049db-...-...-....json", "user.ocis.blobid": "d702d7e1-37b0-4d41-b8dc-4b90c1d1f907"} +// │ └── a9/a5/4c/e7/-de30-4d27-94f8-10e4612c66c2.mpk (Phase 3: parent node for ancestry lookup) +// │ {"user.ocis.name": "einstein", "user.ocis.id": "a9a54ce7-...", "user.ocis.parentid": "...users-node-id..."} +// └── uploads/ (rootMetadataUploads) +// └── d702d7e1-37b0-4d41-b8dc-4b90c1d1f907 (Phase 1: read .json blob for Shares data; blobUploadsPath = filepath.Join(rootMetadataUploads, blobID)) +// {"Shares": {"215fee7a-...:480049db-...:84652da9-...": {resource_id: {...}, grantee: {...}, creator: {...}}}} + +func moveStuckUploadBlobsCmd(cfg *config.Config) *cli.Command { + return &cli.Command{ + Name: "move-stuck-upload-blobs", + Usage: `move stuck upload blobs to the jsoncs3 share-manager metadata`, + Flags: []cli.Flag{ + &cli.BoolFlag{ + Name: "dry-run", + Value: false, + Usage: "Dry run mode enabled", + }, + &cli.StringFlag{ + Name: "ocis-home", + Value: "~/.ocis", + Usage: "oCIS home directory", + }, + &cli.StringFlag{ + Name: "filename", + Value: "received.json", + Usage: "file to move from uploads/ to share manager metadata blobs/", + }, + &cli.BoolFlag{ + Name: "verbose", + Value: false, + Usage: "Verbose logging enabled", + }, + }, + Before: func(c *cli.Context) error { + // Parse base config to align with other shares subcommands; no config fields are required here + if err := parser.ParseConfig(cfg, true); err != nil { + return configlog.ReturnError(err) + } + return nil + }, + Action: func(c *cli.Context) error { + filename := c.String("filename") + verbose := c.Bool("verbose") + + dryRun := true + if c.IsSet("dry-run") { + dryRun = c.Bool("dry-run") + } + if dryRun { + fmt.Print("Dry run mode enabled\n\n") + } + + home, err := os.UserHomeDir() + if err != nil { + return configlog.ReturnError(err) + } + + ocisHome := filepath.Join(home, ".ocis") + if c.IsSet("ocis-home") { + ocisHome = c.String("ocis-home") + } + + rootMetadata := filepath.Join(ocisHome, "storage", "metadata") + rootMetadataBlobs := filepath.Join(rootMetadata, "spaces", "js", "oncs3-share-manager-metadata") + + fmt.Printf("Scanning for missing blobs in: %s \n\n", rootMetadataBlobs) + missingBlobs, err := scanMissingBlobs(rootMetadataBlobs, filename) + if err != nil { + return err + } + if verbose { + printJSON(missingBlobs, "missingBlobs") + } + + if len(missingBlobs) == 0 { + fmt.Println("No missing blobs found") + return nil + } + + rootMetadataUploads := filepath.Join(rootMetadata, "uploads") + fmt.Printf("Found %d missing blobs. Restoring from %s\n", len(missingBlobs), rootMetadataUploads) + remainingBlobIDs := restoreFromUploads(rootMetadataUploads, missingBlobs, dryRun) + + if verbose { + printJSON(remainingBlobIDs, "remainingBlobIDs") + } + + return nil + }, + } +} + +func printJSON(v any, label string) { + jbs, err := json.MarshalIndent(v, "", " ") + if err != nil { + fmt.Printf("Error marshalling JSON: %v\n", err) + return + } + fmt.Println(label, string(jbs)) +} + +// Scan for missing received.json blobs +func scanMissingBlobs(rootMetadata, filename string) (map[string]string, error) { + missingBlobs := make(map[string]string) // blobID -> blobPathAbs + nodesRoot := filepath.Join(rootMetadata, "nodes") + + _ = filepath.WalkDir(nodesRoot, func(path string, dir os.DirEntry, err error) error { + if err != nil || dir.IsDir() || filepath.Ext(path) != ".mpk" { + return nil + } + mpkBin, rerr := os.ReadFile(path) + if rerr != nil { + return nil + } + mpk := unmarshalMPK(mpkBin) + if mpk["user.ocis.name"] != filename { + return nil + } + blobID := mpk["user.ocis.blobid"] + blobPathRel, ok := computeBlobPathRelative(blobID) + if !ok { + return nil + } + blobPathAbs := filepath.Join(rootMetadata, blobPathRel) + if _, statErr := os.Stat(blobPathAbs); statErr == nil { + return nil + } + missingBlobs[blobID] = blobPathAbs + return nil + }) + + return missingBlobs, nil +} + +// Attempt fast path restoration from uploads/ folder +func restoreFromUploads(rootMetadataUploads string, missing map[string]string, dryRun bool) map[string]bool { + remainingBlobIDs := make(map[string]bool) + + for blobID, blobPathAbs := range missing { + remainingBlobIDs[blobID] = true + + blobUploadsPath := filepath.Join(rootMetadataUploads, blobID) + if dryRun { + fmt.Printf(" DRY RUN: move %s to %s\n", blobUploadsPath, blobPathAbs) + continue + } + + if _, err := os.Stat(blobUploadsPath); err != nil { + fmt.Printf(" Blob %s: not found in %s\n", blobID, blobUploadsPath) + continue + } + + fmt.Printf(" Move %s to %s\n", blobUploadsPath, blobPathAbs) + if err := os.MkdirAll(filepath.Dir(blobPathAbs), 0755); err != nil { + fmt.Printf(" Warning: Failed to create dir: %v\n", err) + continue + } + if err := os.Rename(blobUploadsPath, blobPathAbs); err != nil { + fmt.Printf(" Warning: Failed to move blob: %v\n", err) + continue + } + + remainingBlobIDs[blobID] = false + } + + return remainingBlobIDs +} + +func computeBlobPathRelative(bid string) (string, bool) { + hyphen := strings.Index(bid, "-") + if hyphen < 0 || hyphen < 8 { + return "", false + } + prefix8 := bid[:hyphen] + if len(prefix8) < 8 { + return "", false + } + d1, d2, d3, d4 := prefix8[0:2], prefix8[2:4], prefix8[4:6], prefix8[6:8] + suffix := bid[hyphen:] + return filepath.Join("blobs", d1, d2, d3, d4, suffix), true +} + +func unmarshalMPK(bin []byte) map[string]string { + keyValue := map[string][]byte{} + _ = msgpack.Unmarshal(bin, &keyValue) + out := make(map[string]string, len(keyValue)) + for k, v := range keyValue { + out[k] = string(v) + } + return out +} From 1e4ed4f069b5321ceb40f8f2e97487fd81cf4eae Mon Sep 17 00:00:00 2001 From: Michal Klos Date: Thu, 30 Oct 2025 12:17:31 +0100 Subject: [PATCH 2/2] feat: CLI, ocis shares move-stuck-upload-blobs --- ocis/pkg/command/shares.go | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/ocis/pkg/command/shares.go b/ocis/pkg/command/shares.go index 2cfb8d10bdf..a66f7bb489c 100644 --- a/ocis/pkg/command/shares.go +++ b/ocis/pkg/command/shares.go @@ -143,8 +143,10 @@ func cleanup(c *cli.Context, cfg *config.Config) error { // │ └── a9/a5/4c/e7/-de30-4d27-94f8-10e4612c66c2.mpk (Phase 3: parent node for ancestry lookup) // │ {"user.ocis.name": "einstein", "user.ocis.id": "a9a54ce7-...", "user.ocis.parentid": "...users-node-id..."} // └── uploads/ (rootMetadataUploads) -// └── d702d7e1-37b0-4d41-b8dc-4b90c1d1f907 (Phase 1: read .json blob for Shares data; blobUploadsPath = filepath.Join(rootMetadataUploads, blobID)) -// {"Shares": {"215fee7a-...:480049db-...:84652da9-...": {resource_id: {...}, grantee: {...}, creator: {...}}}} +// ├── d702d7e1-37b0-4d41-b8dc-4b90c1d1f907 (Phase 1: read .json blob for Shares data; blobUploadsPath = filepath.Join(rootMetadataUploads, blobID)) +// │ {"Shares": {"215fee7a-...:480049db-...:84652da9-...": {resource_id: {...}, grantee: {...}, creator: {...}}}} +// └── 1c93b82b-d22d-41e0-8038-5a706e9b409e.info +// {"MetaData": {"dir": "/users/4c510ada-c86b-4815-8820-42cdf82c3d51", "filename": "received.json", ...}, "Storage": {"NodeName": "received.json", "SpaceRoot": "jsoncs3-share-manager-metadata", ...}} func moveStuckUploadBlobsCmd(cfg *config.Config) *cli.Command { return &cli.Command{ @@ -286,11 +288,11 @@ func restoreFromUploads(rootMetadataUploads string, missing map[string]string, d continue } + // Check if the blob exists in the uploads folder and move it to the share manager metadata blobs/ folder if _, err := os.Stat(blobUploadsPath); err != nil { fmt.Printf(" Blob %s: not found in %s\n", blobID, blobUploadsPath) continue } - fmt.Printf(" Move %s to %s\n", blobUploadsPath, blobPathAbs) if err := os.MkdirAll(filepath.Dir(blobPathAbs), 0755); err != nil { fmt.Printf(" Warning: Failed to create dir: %v\n", err) @@ -301,6 +303,17 @@ func restoreFromUploads(rootMetadataUploads string, missing map[string]string, d continue } + // Remove the info file after the blob is moved + infoPath := blobUploadsPath + ".info" + if _, err := os.Stat(infoPath); err != nil { + fmt.Printf(" Info file %s: not found\n", infoPath) + continue + } + if err := os.Remove(infoPath); err != nil { + fmt.Printf(" Warning: Failed to remove info file: %v\n", err) + continue + } + remainingBlobIDs[blobID] = false }