fix(files): purge files and refund quota per tenant - #1419
Merged
Merged
Conversation
PurgeOrphanedFilesJob and PurgeDeletedFilesJob run from Hangfire with no tenant context, so FilesDbContext only ever reached the shared database (tenants with a dedicated database were never purged), and the deleted-files purge refunded storage quota under an empty tenant id, which the quota service rejects, so the refund was lost. Both jobs now load every tenant from the tenant store, open a scope per tenant, set the Finbuckle tenant context, and purge that tenant's FilesDbContext. Only the SoftDelete filter is lifted; the tenant filter stays on. The quota refund is recorded under the owning tenant's id after the rows are deleted. Failures are logged per tenant so one bad tenant does not block the rest. Closes #1418 Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
PurgeOrphanedFilesJobandPurgeDeletedFilesJobare Hangfire recurring jobs registered without a tenant, so they run with no Finbuckle tenant context. They got by withIgnoreQueryFilters(), but:FilesDbContextconnects to the default database, so only rows (and blobs) in the shared database were cleaned.PurgeDeletedFilesJobcalledquotas.RecordAsync("", ...). Both quota backends (InMemoryQuotaService,RedisQuotaService) key counters by the explicittenantIdandThrowIfNullOrWhiteSpaceon it, so the call threw, the catch logged a warning, and no tenant got its bytes back.Failing tests on
main(before the fix):Fix
Same pattern as #1406 / #1413 (
AuditRetentionJob):IMultiTenantStore<AppTenantInfo>.GetAllAsync(), open a scope per tenant, setIMultiTenantContextSetter, and resolveFilesDbContextin that scope, so a dedicated-database tenant is purged in its own database.IgnoreQueryFilters()is narrowed toIgnoreQueryFilters([QueryFilters.SoftDelete]). The tenant filter stays on.PurgeDeletedFilesJobrefundsStorageBytesundertenant.Id. The refund now runs after the rows are deleted, so a failed delete can't refund bytes that are still stored.OperationCanceledExceptionis not caught), so one unreachable tenant database doesn't block the rest.FilesModule.MapEndpoints) is unchanged.src/BuildingBlocksis untouched.Tests
New
Integration.Tests/Tests/Files/PurgeJobsPerTenantTests.cs. It seeds purgeable files over the real HTTP upload flow (RustFS + Postgres) in three tenants: root, a shared-database tenant, and a dedicated-database tenant (its own Postgres database on the test container, provisioned by the normal tenant-provisioning flow). It then runs each job from a fresh scope with no tenant, which is how Hangfire runs it, and asserts:StorageBytesquota counter goes back to 0, and nothing is recorded under an empty tenant idQuota is disabled in the integration harness, so the test gives the job a real
InMemoryQuotaServiceand replays the finalize debit on it.Results:
dotnet build src/FSH.Starter.slnx: 0 warnings, 0 errorsFiles.Tests: 23/23 passed ·Architecture.Tests: 55/55 passedCloses #1418
🤖 Generated with Claude Code