mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-29 08:00:06 +00:00
fix(storage): keep the reclaim pass and a backup restore apart
A restore wipes and rehydrates the uploads under a swapped database, but it never took the storage job slot, so a reclaim pass could start or carry on beside it, and a delete already past the generation check could land on an object the restore had just put back. BackupService now marks a restore as running on StorageJobsService and waits for a pass in flight to let go; a pass refuses to start while a restore runs and stops at its next check when one begins. Backfills and migrations are not affected. The service test also reads its fake source sets through a helper instead of non-null assertions.
This commit is contained in:
@@ -6,6 +6,8 @@ import { BackupController } from '../../../src/nest/backup/backup.controller';
|
||||
import * as backupSvc from '../../../src/nest/backup/backup.impl';
|
||||
import { BackupService as RealBackupService } from '../../../src/nest/backup/backup.service';
|
||||
import type { BackupService } from '../../../src/nest/backup/backup.service';
|
||||
import { StorageJobsService } from '../../../src/nest/storage/storage-jobs.service';
|
||||
import type { StorageRegistryService } from '../../../src/nest/storage/storage-registry.service';
|
||||
import { StorageNotFoundError } from '../../../src/nest/storage/storage.types';
|
||||
import type { User } from '../../../src/types';
|
||||
import { HttpException } from '@nestjs/common';
|
||||
@@ -374,7 +376,9 @@ describe('BackupService (wrapper)', () => {
|
||||
const database = {
|
||||
__sentinel: 'database',
|
||||
} as unknown as import('../../../src/nest/database/database-backup.interface').DatabaseBackupStrategy;
|
||||
const wrapper = new RealBackupService(storage, database);
|
||||
// The real one-job slot: the restore marks itself running there.
|
||||
const jobs = new StorageJobsService({} as StorageRegistryService);
|
||||
const wrapper = new RealBackupService(storage, database, jobs);
|
||||
|
||||
it('forwards every call straight to the legacy backup service', async () => {
|
||||
expect(await wrapper.listBackups()).toEqual([{ filename: 'svc.zip' }]);
|
||||
@@ -409,6 +413,33 @@ describe('BackupService (wrapper)', () => {
|
||||
it('exposes the legacy rate window', () => {
|
||||
expect(wrapper.rateWindow).toBe(backupSvc.BACKUP_RATE_WINDOW);
|
||||
});
|
||||
|
||||
it('BKS-RESTORE-001: a restore runs marked as such, so the reclaim pass can neither start nor go on beside it', async () => {
|
||||
let runningInside = false;
|
||||
vi.mocked(backupSvc.restoreFromZip).mockImplementationOnce(async () => {
|
||||
runningInside = jobs.restoreRunning;
|
||||
expect(jobs.claimExclusive('a storage reclaim')).toBe(false);
|
||||
return { success: true };
|
||||
});
|
||||
await expect(wrapper.restoreFromZip('/tmp/a.zip')).resolves.toEqual({ success: true });
|
||||
expect(runningInside).toBe(true);
|
||||
expect(jobs.restoreRunning).toBe(false);
|
||||
});
|
||||
|
||||
it('BKS-RESTORE-002: a restore waits for a reclaim pass in flight to let go, and a failed one still ends', async () => {
|
||||
expect(jobs.claimExclusive('a storage reclaim')).toBe(true);
|
||||
vi.mocked(backupSvc.restoreBackup).mockRejectedValueOnce(new Error('broken archive'));
|
||||
const restore = wrapper.restoreBackup('svc.zip');
|
||||
await Promise.resolve();
|
||||
expect(jobs.restoreRunning).toBe(true);
|
||||
expect(backupSvc.restoreBackup).not.toHaveBeenCalled();
|
||||
jobs.releaseExclusive('a storage reclaim');
|
||||
await expect(restore).rejects.toThrow('broken archive');
|
||||
expect(backupSvc.restoreBackup).toHaveBeenCalledWith({ storage, database }, 'svc.zip');
|
||||
expect(jobs.restoreRunning).toBe(false);
|
||||
expect(jobs.claimExclusive('a storage reclaim')).toBe(true);
|
||||
jobs.releaseExclusive('a storage reclaim');
|
||||
});
|
||||
});
|
||||
|
||||
describe('BackupModule', () => {
|
||||
|
||||
@@ -62,6 +62,16 @@ let named: Map<ServedCategory, Set<string>>;
|
||||
/** Called on every question a source is asked, before it answers. */
|
||||
let onAsk: ((category: ServedCategory, keys: readonly string[]) => void | Promise<void>) | null;
|
||||
|
||||
/** The keys the fake sources name in `category`, created empty on first use. */
|
||||
function namedIn(category: ServedCategory): Set<string> {
|
||||
let set = named.get(category);
|
||||
if (!set) {
|
||||
set = new Set<string>();
|
||||
named.set(category, set);
|
||||
}
|
||||
return set;
|
||||
}
|
||||
|
||||
function fakeSource(id: StorageReferenceSourceId): StorageReferenceSource {
|
||||
const categories: readonly ServedCategory[] =
|
||||
id === 'auth' ? ['avatars'] : id === 'files' ? ['files'] : STORAGE_RECLAIMABLE_CATEGORIES;
|
||||
@@ -208,12 +218,12 @@ describe('StorageReclaimService: mark, then sweep', () => {
|
||||
await seedCovers(['orphan.jpg']);
|
||||
await reclaim.setDeleteEnabled(true);
|
||||
await reclaim.run({ now: T0 });
|
||||
named.get('covers')!.add('orphan.jpg');
|
||||
namedIn('covers').add('orphan.jpg');
|
||||
const renamed = await reclaim.run({ now: new Date(T0.getTime() + DAY) });
|
||||
expect(renamed.categories.covers.unreferenced.objects).toBe(0);
|
||||
expect(await marksOf('covers')).toEqual([]);
|
||||
|
||||
named.get('covers')!.delete('orphan.jpg');
|
||||
namedIn('covers').delete('orphan.jpg');
|
||||
const again = await reclaim.run({ now: WEEK_LATER });
|
||||
expect(again.categories.covers.eligible.objects).toBe(0);
|
||||
expect(fs.existsSync(objectPath('covers', 'orphan.jpg'))).toBe(true);
|
||||
@@ -225,7 +235,8 @@ describe('StorageReclaimService: mark, then sweep', () => {
|
||||
await reclaim.setDeleteEnabled(true);
|
||||
await reclaim.run({ now: T0 });
|
||||
onAsk = (category, keys) => {
|
||||
if (category === 'covers' && keys.length === 1) named.get('covers')!.add(keys[0]!);
|
||||
const [only] = keys;
|
||||
if (category === 'covers' && keys.length === 1 && only !== undefined) namedIn('covers').add(only);
|
||||
};
|
||||
const report = await reclaim.run({ now: WEEK_LATER });
|
||||
expect(report.categories.covers.eligible.objects).toBe(1);
|
||||
@@ -462,4 +473,41 @@ describe('StorageReclaimService: the switch and the stored report', () => {
|
||||
await setAppSetting(orm, 'storage.reclaim_report', JSON.stringify({ startedAt: 'yesterday' }));
|
||||
expect((await reclaim.state()).lastRun).toBeNull();
|
||||
});
|
||||
|
||||
it('RCLS-022: a restore that begins mid-pass stops it before the delete, and waits for it to let go', async () => {
|
||||
await seedCovers(['orphan.jpg']);
|
||||
await reclaim.setDeleteEnabled(true);
|
||||
await reclaim.run({ now: T0 });
|
||||
const jobs = t.get(StorageJobsService);
|
||||
let restoreStarted: Promise<void> | null = null;
|
||||
let restoreWent = false;
|
||||
onAsk = (_category, keys) => {
|
||||
if (keys.length !== 1 || restoreStarted) return;
|
||||
restoreStarted = jobs.beginRestore().then(() => {
|
||||
restoreWent = true;
|
||||
});
|
||||
};
|
||||
try {
|
||||
const report = await reclaim.run({ now: WEEK_LATER });
|
||||
expect(report.stopped).toBe('database_reopened');
|
||||
expect(fs.existsSync(objectPath('covers', 'orphan.jpg'))).toBe(true);
|
||||
expect(restoreStarted).not.toBeNull();
|
||||
await restoreStarted;
|
||||
expect(restoreWent).toBe(true);
|
||||
} finally {
|
||||
jobs.endRestore();
|
||||
}
|
||||
});
|
||||
|
||||
it('RCLS-023: a pass refuses to start while a restore runs, and starts again once it ended', async () => {
|
||||
const jobs = t.get(StorageJobsService);
|
||||
await jobs.beginRestore();
|
||||
try {
|
||||
await expect(reclaim.run({ now: T0 })).rejects.toMatchObject({ code: 'reclaim_busy' });
|
||||
} finally {
|
||||
jobs.endRestore();
|
||||
}
|
||||
expect(jobs.restoreRunning).toBe(false);
|
||||
expect((await reclaim.run({ now: T0 })).stopped).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user