Skip to content

fix(storage): list s3 before hasura when looking for orphans - #5267

Merged
dbarrosop merged 3 commits into
nhost:mainfrom
shubhamkapoor01:storage-orphans-list-s3-first
Oct 29, 2026
Merged

dbarrosop merged 3 commits into
nhost:mainfrom
shubhamkapoor01:storage-orphans-list-s3-first

Conversation

@shubhamkapoor01

Copy link
Copy Markdown
Contributor

Uploads create the metadata row before writing the object to S3. listOrphans listed Hasura first and S3 second, so a file uploaded between the two calls was in S3 but not in the Hasura list, and delete-orphans removed it. Listing S3 first means every object in that list already has its row.

The new test simulates an upload landing between the two listings.

Fixes #5186

Checklist

  • No breaking changes
  • Tests pass
  • New features have new tests
  • Documentation is updated (if applicable)
  • Title of the PR is in the correct format

shubhamkapoor01 and others added 3 commits October 29, 2026 06:20
Review issue: PR_5267/RUN-16792637555358421441/WU-001 / finding 001 — .nhost-code/review/PR_5267_COMMENT_001.md — ADDRESSED, ACCEPT
Resolution:
- Shared orphan listing performs expected O(n+m) ID comparison with one metadata-indexing pass and one S3 pass, benefiting both list and delete endpoints.
- Membership still uses each object's basename while results preserve original S3 keys and listing order.
- Expanded existing endpoint tests exercise pending metadata, reordered and duplicate inputs, and empty/all-matched results.
Why:
- The existing controller owns this comparison; a local set removes repeated scanning without a helper, interface change, dependency, or altered external call contract.
- Using the same set idiom as listBrokenMetadata keeps the two reconciliation paths consistent.
Decisions:
- Accept O(m) additional memory for metadata IDs in exchange for expected linear comparison CPU; continue iterating S3 rather than the map to retain observable ordering and duplicate occurrences.
Review-plan variance:
- The suggested plan's optional BenchmarkListOrphans was not added; the reviewer verified linear scaling out of tree instead (40k files in about 5 ms versus about 32 s measured in the finding).
Checks:
- `nix develop '.#storage' -c golines -l --base-formatter=gofumpt .` — exit 0: passed; reviewed state unchanged.
- `nix develop '.#storage' -c golangci-lint run --timeout 600s ./...` — exit 0: passed; reviewed state unchanged.
- `nix develop '.#storage' -c go test -count=1 -race ./services/storage/controller/...` — exit 0: passed; reviewed state unchanged.
Review issue: PR_5267/RUN-16792637555358421441/WU-002 / finding 002 — .nhost-code/review/PR_5267_COMMENT_002.md — ADDRESSED, ACCEPT
Resolution:
- Re-check UUID candidates with admin metadata access immediately before deletion. Skip existing rows, proceed only on ErrFileNotFound, fail closed on other lookup errors, and report only deleted keys. Non-UUID garbage remains directly deletable.
- Convert nullable metadata scalar fields safely so pending uploads no longer panic; populated values and existing pointer/map semantics remain unchanged.
- Add stateful re-upload tests, nullable-conversion cases, and an HTTP fixture exercising the real Hasura adapter while cleanup skips a pending row and continues to later orphans.
Why:
- Snapshot membership can become stale before deletion. Any metadata row must protect its content because InitializeFile precedes PutFile.
- Pending rows have NULL etag until PopulateMetadata; the new lookup must decode them without aborting cleanup.
Decisions:
- Use the operator-selected bounded re-check, not atomic coordination. An upload starting after the absence check can still race with DeleteObject; this residual risk is accepted and documented in the code and Storage README.
- Nullable fields map to zero values required by the existing API. All GetFileByID consumers now handle pending rows: normal read paths return their existing not-uploaded error, while ReplaceFile can proceed as designed.
Review-plan variance:
- Include the supporting metadata conversion fix and regression tests now, as explicitly approved after review reproduced the NULL-etag panic.
Checks:
- `nix develop '.#storage' -c golines -l --base-formatter=gofumpt .` — exit 0: passed; reviewed state unchanged.
- `nix develop '.#storage' -c golangci-lint run --timeout 600s ./...` — exit 0: passed; reviewed state unchanged.
- `nix develop '.#storage' -c go test -count=1 -race ./services/storage/controller/... ./services/storage/metadata/...` — exit 0: passed; reviewed state unchanged.
- `git diff --cached --check b94e02c` — exit 0: passed; reviewed state unchanged.
Validation limit:
- Live Hasura integration suite unavailable; real-adapter HTTP fixture and controller/metadata race suites passed.
@dbarrosop
dbarrosop force-pushed the storage-orphans-list-s3-first branch from 5302898 to bc17ad1 Compare October 29, 2026 09:00
@dbarrosop

Copy link
Copy Markdown
Member

Thanks!

@dbarrosop
dbarrosop merged commit c0e5cbc into nhost:main Oct 29, 2026
12 of 15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Storage: delete-orphans can delete a file that is being uploaded at the same time

2 participants