feat(storage): find and reclaim upload objects no row names any more

Domains whose rows name stored objects register a reference source with
StorageReclaimRegistryService; storage never imports them. A ledger in
storage decides per category whether it is reclaimed at all and which
sources must have answered first, and a category whose resolved location
overlaps another one is left alone.

A nightly pass at 04:05 lists the root level of files, journey, covers,
avatars and places, skips objects younger than a day or without a
modification time, asks every source in pages of 500 and marks the rest.
Only with deletion switched on, and only for marks a week old, it asks every
source about the single key again and deletes through StorageService. A
category is skipped and its marks start over when a source is missing or
fails, the listing fails, or more than a fifth of its objects look
unreferenced. A pass shares the one-job slot with backfills and migrations,
never runs on a demo instance and stops when the database is reopened.

Admins switch deletion and run a pass on demand under
/api/admin/storage/reclaim; both are audited, and GET /api/admin/storage
carries the switch and the last report. Deletion is off after install and
upgrade.
This commit is contained in:
Maurice
2026-11-01 13:37:53 +01:00
parent 29298dcfe5
commit fb2429ebb2
19 changed files with 2239 additions and 8 deletions
@@ -0,0 +1,58 @@
/**
* RECLAIM-SRC: the full app registers exactly the reference sources the
* reclaim ledger waits for, and each answers for every category that waits on
* it. Registration cannot fail a boot (the partial e2e graphs lack most
* owners), so this is the one place a domain that forgot to register, or a
* source that stopped claiming a category its columns can point into, shows:
* on a real install that category would be skipped as `sources_missing`
* forever, or swept while a domain's rows were never asked.
*/
import { buildApp } from '../../src/bootstrap';
import { db as testDb } from '../../src/db/database';
import { StorageReclaimRegistryService } from '../../src/nest/storage/storage-reclaim-registry.service';
import {
RECLAIM_LEDGER,
reclaimableCategories,
requiredReclaimSources,
} from '../../src/nest/storage/storage-reclaim.ledger';
import type { INestApplication } from '@nestjs/common';
import { afterAll, beforeAll, describe, expect, it, vi } from 'vitest';
vi.mock('../../src/db/database', async () => {
const { createSnapshotTestDb, buildDbMock } = await import('../helpers/db-mock');
return buildDbMock(createSnapshotTestDb());
});
describe('RECLAIM-SRC: the app registers what the reclaim ledger waits for', () => {
let app: INestApplication;
beforeAll(async () => {
app = await buildApp();
});
afterAll(async () => {
await app.close();
testDb.close();
});
it('RECLAIM-SRC-001: the registered source ids are exactly the ones the ledger requires', () => {
const registry = app.get(StorageReclaimRegistryService);
expect([...registry.ids()].sort()).toEqual([...requiredReclaimSources()].sort());
});
it('RECLAIM-SRC-002: every required source answers for the whole of each category waiting on it', () => {
const sources = app.get(StorageReclaimRegistryService).sources();
for (const category of reclaimableCategories()) {
const rule = RECLAIM_LEDGER[category];
if (!rule.reclaim) continue;
for (const id of rule.requiredSources) {
const answering = sources.filter(
(source) =>
source.id === id && source.claims.some((claim) => claim.category === category && claim.prefix === ''),
);
expect(answering.length, `${id} answers for ${category}`).toBeGreaterThan(0);
}
}
});
});