fix(storage): queue what a delete left unnamed past every undo, and tell a copied database apart

A delete or a replace no longer deletes inside the request. The owner's call
queues the keys in storage_release_queue, due after RELEASE_DELAY_MS (26 h:
the assistant's 24 h approval window plus a margin), and StorageReleaseJob
takes the due rows every ten minutes, at most 200 a tick and 50 keys a
question, asks every reference source again and deletes what none names.
It refuses what the nightly pass refuses (StorageReclaimGuardService: the
ledger, the required sources, a shared location, another installation's
owner marker, cached per category), and waits while a restore or a backup
runs or another storage job holds the slot. An assistant undo or a late
write that names the old value again keeps its object.

The install id lives in data/.trek-install-id, outside any backup and never
written by a restore. At boot and after every reopen the database is
reconciled with it: a database naming another installation (a boot restore,
a restart after a failed restore, a copied travel.db) loses its switch,
confirmations, reports, marks and queue before anything reads them.

A confirmation now stores the count the admin was shown and when; it covers
up to one and a half times that count (at least 100 more) for 14 days, is
used up once the share is back under a fifth, and a count the last report
no longer holds is refused as reclaim_confirm_stale. The admin state folds
a live confirmation in, so the prompt goes at once.

A pass reads each source once for every category and drops each category's
keys once judged; right before a batch it reads the category's sources
again, whole and in memory, instead of a LIKE per key. A narrowed read holds
at most 50 LIKE terms. A pass held off by a backup reports backup_running,
and a category the ledger never reclaims reports not_reclaimed even after a
stop.
This commit is contained in:
Maurice
2026-11-01 18:11:43 +01:00
parent 0a25de2c6d
commit 1c6a175ebc
40 changed files with 2225 additions and 581 deletions
+2 -1
View File
@@ -59,7 +59,8 @@ tokens, `RuntimeEnvService`) and consumes the SAME derive functions.
`resolveDataPaths()` is the only place the server anchors `data/` and
`uploads/` (on `SERVER_ROOT`, the package directory, never on `process.cwd()`)
and names what lives in them: backups, the scratch dir, the logs, the JWT
secret and encryption key files, and the database file (TREK_DB_FILE when set,
secret and encryption key files, the install id file the storage reclaim
tells a copied database apart by, and the database file (TREK_DB_FILE when set,
else `data/travel.db`). Never derive one of these paths from `__dirname` in a
new file: a moved file would quietly move the data.
+7
View File
@@ -29,6 +29,12 @@ export interface DataPaths {
readonly jwtSecretFile: string;
/** `data/.encryption_key`: the at-rest key when ENCRYPTION_KEY does not provide it. */
readonly encryptionKeyFile: string;
/**
* `data/.trek-install-id`: this installation's random id, written on first
* start. It never enters a backup and no restore writes it, so a database
* restored or copied from another install is told apart by it (storage reclaim).
*/
readonly installIdFile: string;
/**
* The SQLite file a non-test process opens: TREK_DB_FILE when set, else
* `data/travel.db`. Under NODE_ENV=test the connection is `:memory:` instead
@@ -58,6 +64,7 @@ export function resolveDataPaths(
logsDir: path.join(dataDir, 'logs'),
jwtSecretFile: path.join(dataDir, '.jwt_secret'),
encryptionKeyFile: path.join(dataDir, '.encryption_key'),
installIdFile: path.join(dataDir, '.trek-install-id'),
dbFile: env.db.trekDbFile || path.join(dataDir, 'travel.db'),
};
}
@@ -0,0 +1,21 @@
import { EntityRepositoryType, PrimaryKeyProp, defineEntity, p } from '@mikro-orm/core';
import { StorageReleaseQueueRepository } from '../repositories/StorageReleaseQueue.repository';
import { DbTimestampType } from '../types';
export class StorageReleaseQueue {
[EntityRepositoryType]?: StorageReleaseQueueRepository;
[PrimaryKeyProp]?: ['category', 'key'];
category!: string;
key!: string;
due_at!: string;
}
export const StorageReleaseQueueSchema = defineEntity({
class: StorageReleaseQueue,
repository: () => StorageReleaseQueueRepository,
properties: {
category: p.text().primary(),
key: p.text().primary(),
due_at: p.type(DbTimestampType),
},
});
+2
View File
@@ -119,6 +119,7 @@ import { SchoolHolidayRegionsSchema } from './SchoolHolidayRegions.entity';
import { SettingsSchema } from './Settings.entity';
import { ShareTokensSchema } from './ShareTokens.entity';
import { StorageReclaimMarksSchema } from './StorageReclaimMarks.entity';
import { StorageReleaseQueueSchema } from './StorageReleaseQueue.entity';
import { TagsSchema } from './Tags.entity';
import { TodoCategoryAssigneesSchema } from './TodoCategoryAssignees.entity';
import { TodoItemsSchema } from './TodoItems.entity';
@@ -287,6 +288,7 @@ export const ALL_ENTITIES: readonly EntitySchema[] = [
SettingsSchema,
ShareTokensSchema,
StorageReclaimMarksSchema,
StorageReleaseQueueSchema,
TagsSchema,
TodoCategoryAssigneesSchema,
TodoItemsSchema,
@@ -0,0 +1,8 @@
// Generated by scripts/generate-db-types.ts from the migrated schema. Do not edit by hand:
// change the migration, then run `npm run gen:db-types`.
export interface StorageReleaseQueueTable {
category: string;
key: string;
due_at: string;
}
+2
View File
@@ -125,6 +125,7 @@ import type { SchoolHolidayRegionsTable } from './SchoolHolidayRegions.table';
import type { SettingsTable } from './Settings.table';
import type { ShareTokensTable } from './ShareTokens.table';
import type { StorageReclaimMarksTable } from './StorageReclaimMarks.table';
import type { StorageReleaseQueueTable } from './StorageReleaseQueue.table';
import type { TagsTable } from './Tags.table';
import type { TodoCategoryAssigneesTable } from './TodoCategoryAssignees.table';
import type { TodoItemsTable } from './TodoItems.table';
@@ -299,6 +300,7 @@ export interface DB {
settings: SettingsTable;
share_tokens: ShareTokensTable;
storage_reclaim_marks: StorageReclaimMarksTable;
storage_release_queue: StorageReleaseQueueTable;
tags: TagsTable;
todo_category_assignees: TodoCategoryAssigneesTable;
todo_items: TodoItemsTable;
@@ -0,0 +1,24 @@
import { Migration } from '@mikro-orm/migrations';
/**
* What a user's delete or replace left unnamed waits here before it goes: one
* row per stored object, due once every undo that can write the old value back
* has run out. A job then asks every reference source again and deletes only
* what none of them names; the row goes either way. A row is all an owner's
* request writes, so a delete stays as cheap as it was before the storage
* reclaim, whatever the request named.
*/
export class Migration20200101046340_a_released_object_outlasts_every_undo_before_it_goes extends Migration {
override name = 'Migration20200101046340_a_released_object_outlasts_every_undo_before_it_goes';
override up(): void {
this.addSql(`
CREATE TABLE IF NOT EXISTS storage_release_queue (
category TEXT NOT NULL,
key TEXT NOT NULL,
due_at DATETIME NOT NULL,
PRIMARY KEY (category, key)
)
`);
}
}
@@ -0,0 +1,86 @@
import type { StorageReleaseQueue } from '../entities/StorageReleaseQueue.entity';
import type { DB } from '../kysely/db';
import { TrekRepository } from './_shared/trek-repository';
type QueueKyselyDB = Pick<DB, 'storage_release_queue'>;
/** One object a user's change left unnamed, and when it may go. */
export interface ReleaseQueueRow {
category: string;
key: string;
due_at: string;
}
/**
* `storage_release_queue`: the objects a delete or a replace left unnamed,
* keyed by category and category-relative key, each with the time it may go.
* Owners only add rows; the release job reads the due ones of the categories
* it may judge, asks every reference source again and drops each row once it
* has decided, deleted or not. A key queued again waits from the later time.
*/
export class StorageReleaseQueueRepository extends TrekRepository<StorageReleaseQueue> {
private db() {
return this.kysely<QueueKyselyDB>();
}
/** Queues these keys, due at `dueAt`; a key queued already waits until the later time. */
async enqueue(rows: Array<{ category: string; key: string }>, dueAt: string): Promise<void> {
if (rows.length === 0) return;
await this.db()
.insertInto('storage_release_queue')
.values(rows.map((row) => ({ category: row.category, key: row.key, due_at: dueAt })))
.onConflict((oc) => oc.columns(['category', 'key']).doUpdateSet({ due_at: dueAt }))
.execute();
}
/** How many rows wait, due or not. */
async size(): Promise<number> {
return await this.count();
}
/** The categories holding a row due at `now`. */
async dueCategories(now: string): Promise<string[]> {
const rows = await this.db()
.selectFrom('storage_release_queue')
.select('category')
.distinct()
.where('due_at', '<=', now)
.execute();
return rows.map((row) => row.category);
}
/** The rows of these categories due at `now`, the longest waiting first, at most `limit`. */
async dueIn(categories: string[], now: string, limit: number): Promise<ReleaseQueueRow[]> {
if (categories.length === 0 || limit <= 0) return [];
return await this.db()
.selectFrom('storage_release_queue')
.select(['category', 'key', 'due_at'])
.where('category', 'in', categories)
.where('due_at', '<=', now)
.orderBy('due_at')
.limit(limit)
.execute();
}
/** Drops the rows of these keys; answers how many went. */
async deleteKeys(category: string, keys: string[]): Promise<number> {
if (keys.length === 0) return 0;
const result = await this.db()
.deleteFrom('storage_release_queue')
.where('category', '=', category)
.where('key', 'in', keys)
.executeTakeFirst();
return Number(result.numDeletedRows);
}
/** Drops the rows due before `cutoff`: what waited that long is the nightly pass's. */
async deleteDueBefore(cutoff: string): Promise<number> {
const result = await this.db().deleteFrom('storage_release_queue').where('due_at', '<', cutoff).executeTakeFirst();
return Number(result.numDeletedRows);
}
/** Drops every row, for a database another installation's history came with. */
async deleteAll(): Promise<number> {
return await this.nativeDelete({});
}
}
@@ -3,7 +3,7 @@ import { Injectable } from '@nestjs/common';
/**
* The data layout for Nest classes: the data directory, uploads, backups,
* scratch space, logs, the key files and the database file.
* scratch space, logs, the key files, the install id and the database file.
*
* Providers that read a data path take this one (AdminService,
* StorageRegistryService). Code that runs before the container exists or is
@@ -23,6 +23,7 @@ export class DataPathsService implements DataPaths {
readonly logsDir: string;
readonly jwtSecretFile: string;
readonly encryptionKeyFile: string;
readonly installIdFile: string;
readonly dbFile: string;
constructor() {
@@ -34,6 +35,7 @@ export class DataPathsService implements DataPaths {
this.logsDir = paths.logsDir;
this.jwtSecretFile = paths.jwtSecretFile;
this.encryptionKeyFile = paths.encryptionKeyFile;
this.installIdFile = paths.installIdFile;
this.dbFile = paths.dbFile;
}
}
@@ -54,7 +54,10 @@ export class JourneySettingsService {
return previousSettings(before as unknown as Readonly<Record<string, unknown>> | undefined, patch);
});
if (written) await this.journeys.broadcastJourneyEvent(journeyId, 'journey:updated', {}, sid);
if (typeof previous.cover_image === 'string') await this.release.releaseValues([previous.cover_image]);
// A replaced cover is queued, never deleted here: the assistant's undo can write it back.
if (typeof previous.cover_image === 'string' && previous.cover_image !== patch.cover_image) {
await this.release.releaseValues([previous.cover_image]);
}
const journey = (await this.journeysRepo.findById(journeyId)) as Journey | undefined;
return journey ? { journey, previous } : null;
}
@@ -26,7 +26,6 @@ import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { isDemoWriteBlocked, DEMO_WRITE_ERROR } from '../common/demo-write';
import { StorageService } from '../storage/storage.service';
import { StorageReleaseService } from '../storage/storage-release.service';
import { journeyThumbName } from '../memories/thumbnail.service';
import { JourneyService } from './journey.service';
import { JourneyBookService } from './journey-book.service';
import { JourneyPhotoCaptureService, type ProviderPhotoBatch, type ProviderPhotoTarget } from './journey-photo-capture.service';
@@ -472,16 +471,13 @@ export class JourneyController {
}
/**
* After a photo's row went: its file and its poster go at once unless another
* row (another journey's photo, a journey cover) still names them, and the
* thumbnail derived from a file that went goes with it (#2924).
* After a photo's row went: its file and its poster are queued for release,
* and go once no other row (another journey's photo, a journey cover) names
* them (#2924). The thumbnail derived from the file is the nightly thumbnail
* sweep's, which drops every thumb no photo row accounts for.
*/
private async releasePhoto(photo: { file_path?: string | null; thumbnail_path?: string | null }): Promise<void> {
const released = await this.release.releaseValues([photo.file_path, photo.thumbnail_path], { bare: 'journey' });
const filePath = photo.file_path;
if (!filePath?.startsWith('journey/')) return;
if (!released.some((ref) => ref.category === 'journey' && `journey/${ref.key}` === filePath)) return;
await this.storage.delete('journey', journeyThumbName(filePath)).catch(() => { /* already gone */ });
await this.release.releaseValues([photo.file_path, photo.thumbnail_path], { bare: 'journey' });
}
// ── Gallery (prefix /:id/gallery — before /:id) ─────────────────────────
@@ -0,0 +1,192 @@
import { AppSettings } from '../../db/entities/AppSettings.entity';
import { StorageReclaimMarks } from '../../db/entities/StorageReclaimMarks.entity';
import { StorageReleaseQueue } from '../../db/entities/StorageReleaseQueue.entity';
import type { AppSettingsRepository } from '../../db/repositories/AppSettings.repository';
import type { StorageReclaimMarksRepository } from '../../db/repositories/StorageReclaimMarks.repository';
import type { StorageReleaseQueueRepository } from '../../db/repositories/StorageReleaseQueue.repository';
import { DataPathsService } from '../app-config/data-paths.service';
import { logError, logInfo, logWarn } from '../audit/audit-log.logger';
import { withRequestContext } from '../database/request-context';
import { UnitOfWork } from '../database/unit-of-work';
import { INSTALL_ID_KEY, RECLAIM_DELETE_KEY, RECLAIM_RESET_KEYS } from './storage-reclaim.settings';
import { MikroORM } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
import { randomUUID } from 'node:crypto';
import fs from 'node:fs';
import path from 'node:path';
const INSTALL_ID = /^[a-z0-9-]{8,64}$/;
/**
* Which installation this is, from a file in the data dir
* (`data/.trek-install-id`, made on the first start with a random id, mode
* 0600 like the at-rest key). No backup archive carries it and no restore
* writes it, so it stays with the data dir while a database can come from
* anywhere: a backup restore (in the app or at boot), a restart after a
* restore that failed half way, a copied `travel.db`.
*
* At boot, and after every reopen of the database, the database is reconciled
* with it: a database that names another id, or none, gets this one, and the
* reclaim state it brought (the deletion switch, the confirmations, both
* reports, the marks and the release queue) is dropped, since it describes
* another installation's storage. After a reopen that happens whatever the id
* says, as before: the rows a restore brings are an older history even when
* they are this installation's own.
*
* What it cannot tell apart is a data dir copied whole, the file with it: the
* wiki asks to give a clone its own data dir or delete the file. When the file
* can be neither read nor written, there is no id: nothing is deleted then.
*/
@Injectable()
export class StorageInstallService implements OnApplicationBootstrap {
/** Undefined until the file was read; null when it cannot be used. */
private fileId: string | null | undefined;
/** Bumped on every reopen: a pass or a release tick that saw another epoch stops. */
private currentEpoch = 0;
constructor(
private readonly dataPaths: DataPathsService,
private readonly uow: UnitOfWork,
private readonly orm: MikroORM,
@InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository,
@InjectRepository(StorageReclaimMarks) private readonly marks: StorageReclaimMarksRepository,
@InjectRepository(StorageReleaseQueue) private readonly queue: StorageReleaseQueueRepository,
) {}
async onApplicationBootstrap(): Promise<void> {
try {
await withRequestContext(this.orm, () => this.reconcile());
} catch (err) {
logError(`[storage] could not reconcile the database with this installation's id: ${messageOf(err)}`);
}
}
/** How many reopens this process has seen. */
get epoch(): number {
return this.currentEpoch;
}
/**
* This installation's id, read from its file once and made there on the
* first start; null when the file holds no id or cannot be read or written,
* which keeps every delete off (fail closed).
*/
installId(): string | null {
if (this.fileId === undefined) this.fileId = this.readOrCreate();
return this.fileId;
}
/**
* Makes the database name this installation. One that names another, or
* none, loses its reclaim state first. Answers whether it reset something
* a database had brought along.
*/
async reconcile(): Promise<boolean> {
const id = this.installId();
if (id === null) return false;
const stored = await this.appSettings.getValue(INSTALL_ID_KEY);
if (stored === id) return false;
const wasOn = await this.reset(id);
if (stored !== null) {
logInfo(
`[storage] the database names another installation (restored, cloned or copied): its reclaim state was ` +
`dropped${wasOn ? ', and deletion is off until an admin turns it on' : ''}`,
);
}
return stored !== null;
}
/**
* The database was swapped (a restore, the demo reset): what ran on the old
* one stops, and the new one gets this installation's id and no reclaim
* state. Contained, since a listener that throws fails the reopen.
*/
async afterReopen(): Promise<void> {
this.currentEpoch += 1;
try {
const id = this.installId();
const wasOn = await withRequestContext(this.orm, () => this.reset(id));
if (wasOn)
logInfo('[storage] the restored database had reclaim deletion on: it is off until an admin turns it on');
} catch (err) {
logError(`[storage] could not reset the reclaim state after the database was reopened: ${messageOf(err)}`);
}
}
/** Drops the reclaim state and writes `id` (when there is one); answers whether deletion was on. */
private async reset(id: string | null): Promise<boolean> {
return await this.uow.transactional(async () => {
const wasOn = (await this.appSettings.getValue(RECLAIM_DELETE_KEY)) !== null;
if (id !== null) await this.appSettings.upsertOrReplace(INSTALL_ID_KEY, id);
await RECLAIM_RESET_KEYS.reduce<Promise<void>>(async (previous, key) => {
await previous;
await this.appSettings.deleteValue(key);
}, Promise.resolve());
await this.marks.deleteAll();
await this.queue.deleteAll();
return wasOn;
});
}
private readOrCreate(): string | null {
const file = this.dataPaths.installIdFile;
const read = readId(file);
if (read !== 'missing') return read;
const id = randomUUID();
try {
fs.mkdirSync(path.dirname(file), { recursive: true });
placeOnce(file, `${id}\n`);
return id;
} catch (err) {
if (codeOf(err) === 'EEXIST') {
const again = readId(file);
return again === 'missing' ? null : again;
}
logWarn(`[storage] could not write the install id file, nothing is reclaimed: ${messageOf(err)}`);
return null;
}
}
}
/**
* Puts `text` at `file` unless a file is there already (EEXIST), whole: it is
* written next to it and linked into place, so two processes starting at once
* on one data dir end up with one id and neither reads a half-written file. A
* volume without hard links gets an exclusive create instead.
*/
function placeOnce(file: string, text: string): void {
const spool = `${file}.${process.pid}.${randomUUID()}.tmp`;
fs.writeFileSync(spool, text, { mode: 0o600 });
try {
fs.linkSync(spool, file);
} catch (err) {
if (codeOf(err) === 'EEXIST') throw err;
fs.writeFileSync(file, text, { mode: 0o600, flag: 'wx' });
} finally {
fs.rmSync(spool, { force: true });
}
}
/** The id the file holds, 'missing' when there is no file, null when it is unreadable or holds no id. */
function readId(file: string): string | null | 'missing' {
try {
const text = fs.readFileSync(file, 'utf8').trim();
if (INSTALL_ID.test(text)) return text;
logWarn(`[storage] '${file}' holds no install id, nothing is reclaimed until it is deleted or fixed`);
return null;
} catch (err) {
if (codeOf(err) === 'ENOENT') return 'missing';
logWarn(`[storage] could not read the install id file, nothing is reclaimed: ${messageOf(err)}`);
return null;
}
}
function codeOf(err: unknown): string | undefined {
return typeof err === 'object' && err !== null && 'code' in err ? String((err as { code: unknown }).code) : undefined;
}
function messageOf(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
@@ -0,0 +1,166 @@
import { logWarn } from '../audit/audit-log.logger';
import { StorageReclaimRegistryService } from './storage-reclaim-registry.service';
import { RECLAIM_LEDGER, sharedLocationsOf } from './storage-reclaim.ledger';
import { storageOwnerMarkerInstall, storageOwnerMarkerKey, type StorageReferenceSource } from './storage-reclaim.types';
import { StorageRegistryService } from './storage-registry.service';
import { StorageService } from './storage.service';
import type { ServedCategory } from './storage.types';
import { Injectable } from '@nestjs/common';
import { Readable } from 'node:stream';
/** Why a category may not be judged, before anything of it is listed. */
type CategoryRefusal = 'not_reclaimed' | 'sources_missing' | 'shared_location';
/** Why the place a category lives in may not be judged: another installation's, or one nobody can tell. */
type OwnerRefusal = 'foreign_install' | 'owner_unknown';
/** A verdict the release queue reuses for this long; the nightly pass renews it every night. */
const OWNER_VERDICT_TTL_MS = 26 * 60 * 60 * 1000;
/** A failed look at the owner markers is not retried before this. */
const OWNER_FAILURE_TTL_MS = 60 * 60 * 1000;
interface CachedVerdict {
verdict: OwnerRefusal | null;
/** Where the category lived when it was judged: a changed storage config judges again. */
location: string;
until: number;
}
/**
* The guards the reclaim pass and the release queue share, so a deferred
* release refuses exactly what the nightly pass refuses:
* - `judgeable`: the ledger reclaims the category, every source it requires
* answers for all of it, and its location overlaps no other category's;
* - `ownerVerdict`: no other installation's owner marker lies where the
* category's objects do, and this one's could be written there.
*
* The owner verdict is cached per category with the location it was judged
* at. The pass judges it from the listing it makes anyway; the release queue
* reuses that verdict for a day and lists the category's root itself only
* when there is none, so a release never deletes in a category the pass
* would refuse as another installation's.
*/
@Injectable()
export class StorageReclaimGuardService {
private readonly verdicts = new Map<ServedCategory, CachedVerdict>();
constructor(
private readonly storage: StorageService,
private readonly registry: StorageRegistryService,
private readonly references: StorageReclaimRegistryService,
) {}
/**
* The sources to ask about a category, or why it may not be judged. Pass
* `sources` and `shared` to judge several categories against one snapshot.
*/
judgeable(
category: ServedCategory,
sources: readonly StorageReferenceSource[] = this.references.sources(),
shared: ReadonlySet<ServedCategory> = sharedLocationsOf(this.registry),
): { refused: CategoryRefusal } | { sources: StorageReferenceSource[] } {
const rule = RECLAIM_LEDGER[category];
if (!rule.reclaim) return { refused: 'not_reclaimed' };
const claiming = sources.filter((source) => source.claims.some((claim) => claim.category === category));
const whole = new Set(
claiming
.filter((source) => source.claims.some((claim) => claim.category === category && claim.prefix === ''))
.map((source) => source.id),
);
if (rule.requiredSources.some((id) => !whole.has(id))) return { refused: 'sources_missing' };
if (shared.has(category)) return { refused: 'shared_location' };
return { sources: claiming };
}
/**
* Whether this installation may judge where a category lives, from the
* owner markers a listing of its root found: not where another one's marker
* lies, and not where its own could not be written, since then a second
* installation cannot see it either. Writes this one's marker when it is
* missing, and caches the verdict.
*/
async ownerVerdict(
category: ServedCategory,
owners: ReadonlySet<string>,
installId: string,
now: number = Date.now(),
): Promise<OwnerRefusal | null> {
const location = this.locationOf(category);
const verdict = await this.judgeOwners(category, owners, installId);
this.verdicts.set(category, { verdict, location, until: now + OWNER_VERDICT_TTL_MS });
return verdict;
}
/**
* The owner verdict for a category, as the last judgement left it when that
* holds for where the category lives now; otherwise its root is listed for
* owner markers and judged afresh. A listing that fails reads as
* `owner_unknown` for an hour.
*/
async cachedOwnerVerdict(
category: ServedCategory,
installId: string,
now: number = Date.now(),
): Promise<OwnerRefusal | null> {
const cached = this.verdicts.get(category);
if (cached && cached.until > now && cached.location === this.locationOf(category)) return cached.verdict;
let owners: Set<string>;
try {
owners = await this.ownersAt(category);
} catch (err) {
logWarn(`[storage] could not look for owner markers in '${category}': ${messageOf(err)}`);
this.verdicts.set(category, {
verdict: 'owner_unknown',
location: this.locationOf(category),
until: now + OWNER_FAILURE_TTL_MS,
});
return 'owner_unknown';
}
return await this.ownerVerdict(category, owners, installId, now);
}
/** Forgets every verdict: the database or the installation they were judged for changed. */
forget(): void {
this.verdicts.clear();
}
private async judgeOwners(
category: ServedCategory,
owners: ReadonlySet<string>,
installId: string,
): Promise<OwnerRefusal | null> {
const foreign = [...owners].some((owner) => owner !== installId);
if (!owners.has(installId)) {
try {
await this.storage.put(category, storageOwnerMarkerKey(installId), Readable.from([installId]), {
contentType: 'text/plain',
});
} catch (err) {
logWarn(`[storage] reclaim could not write its owner marker into '${category}': ${messageOf(err)}`);
return foreign ? 'foreign_install' : 'owner_unknown';
}
}
if (foreign) logWarn(`[storage] another TREK installation keeps its uploads where '${category}' does: not judged`);
return foreign ? 'foreign_install' : null;
}
/** The owner markers at a category's root. */
private async ownersAt(category: ServedCategory): Promise<Set<string>> {
const owners = new Set<string>();
for await (const stat of this.storage.list(category, '', { shallow: true })) {
const owner = storageOwnerMarkerInstall(stat.key);
if (owner !== null) owners.add(owner);
}
return owners;
}
/** Where a category lives now: its backend, prefix and the backends' settings. */
private locationOf(category: ServedCategory): string {
const { backendName, keyPrefix } = this.registry.resolve(category);
return JSON.stringify([backendName, keyPrefix, this.registry.snapshot().backends]);
}
}
function messageOf(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
@@ -1,7 +1,9 @@
import { logWarn } from '../audit/audit-log.logger';
import { StorageReclaimRegistryService } from './storage-reclaim-registry.service';
import {
keySearchCore,
keysNamedIn,
storageKeysIn,
type StorageKeyOptions,
type StorageReferenceClaim,
type StorageReferenceSource,
@@ -22,11 +24,18 @@ import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
* learns which tables those are. A column that takes any string (an absolute
* URL of this instance, a path with a query, another category's object)
* cannot be matched with `IN`, so the values are read and parsed here:
* - `namedKeys` reads every value once, for a pass that judges a whole category;
* - `namedKeysByCategory` reads every value once and parses it for every
* category asked, for a pass that judges them all;
* - `namedKeys` reads every value once for one category, fresh, for the
* question a pass asks right before it deletes;
* - `referenced` reads only the rows whose value holds one of the keys' plain
* runs (`keySearchCore`, a `LIKE '%run%'` each), fresh, for the question a
* delete asks last and for a release right after a user's change. A key with
* no such run reads everything, as `namedKeys` does.
* runs (`keySearchCore`, a `LIKE '%run%'` each, at most MAX_LIKE_TERMS per
* query, as many queries as the keys need), for the release queue. A key
* with no such run is answered from one whole read; the release queue never
* asks about such a key, it leaves it to the nightly pass.
*
* The rows are read through the owner's TrekRepository, whose reads skip the
* identity map, so a pass over a large table keeps no entities alive.
*/
@Injectable()
export abstract class StorageReclaimSource implements StorageReferenceSource, OnApplicationBootstrap {
@@ -49,11 +58,32 @@ export abstract class StorageReclaimSource implements StorageReferenceSource, On
return keysNamedIn(category, await this.storedValues(null), this.keyOptions);
}
async namedKeysByCategory(categories: readonly ServedCategory[]): Promise<Map<ServedCategory, Set<string>>> {
const asked = new Set(categories.filter((category) => this.claimsCategory(category)));
const named = new Map<ServedCategory, Set<string>>([...asked].map((category) => [category, new Set<string>()]));
if (asked.size === 0) return named;
for (const value of await this.storedValues(null)) {
for (const ref of storageKeysIn(value, this.keyOptions)) {
named.get(ref.category as ServedCategory)?.add(ref.key.toLowerCase());
}
}
return named;
}
async referenced(category: ServedCategory, keys: readonly string[]): Promise<Set<string>> {
if (keys.length === 0 || !this.claimsCategory(category)) return new Set();
const cores = keys.map(keySearchCore);
const narrow = cores.every((core): core is string => core !== null) ? [...new Set(cores)] : null;
const named = keysNamedIn(category, await this.storedValues(narrow), this.keyOptions);
const named = new Set<string>();
if (cores.some((core) => core === null)) {
for (const key of keysNamedIn(category, await this.storedValues(null), this.keyOptions)) named.add(key);
} else {
const runs = [...new Set(cores.filter((core): core is string => core !== null))];
await chunked(runs, RUNS_PER_QUERY).reduce<Promise<void>>(async (previous, page) => {
await previous;
const values = await this.storedValues(page);
for (const key of keysNamedIn(category, values, this.keyOptions)) named.add(key);
}, Promise.resolve());
}
return new Set(keys.filter((key) => named.has(key.toLowerCase())));
}
@@ -70,11 +100,21 @@ export abstract class StorageReclaimSource implements StorageReferenceSource, On
}
}
/**
* The most LIKE terms one narrowed read may hold. SQLite parses an OR chain
* as a left-deep tree and refuses one deeper than 1000, so a read is kept
* far below that whatever it is asked about.
*/
export const MAX_LIKE_TERMS = 50;
/** Every source reads at most two columns, so this many runs keep one read within MAX_LIKE_TERMS. */
const RUNS_PER_QUERY = 25;
/**
* The filter a source narrows its columns with: rows where one of `fields`
* holds one of the runs, or every row where one of them is set when there are
* none to narrow by. The runs are `[a-z0-9-]` only, so they need no LIKE
* escaping; SQLite's LIKE ignores ASCII case, as the parser does.
* escaping; SQLite's LIKE ignores ASCII case, as the parser does. A filter
* past MAX_LIKE_TERMS is refused rather than handed to SQLite.
*/
export function likeAny<T>(
fields: ReadonlyArray<keyof T & string>,
@@ -84,5 +124,13 @@ export function likeAny<T>(
containing === null
? fields.map((field) => ({ [field]: { $ne: null } }))
: fields.flatMap((field) => containing.map((run) => ({ [field]: { $like: `%${run}%` } })));
if (terms.length > MAX_LIKE_TERMS) {
logWarn(`[storage] a reference lookup asked for ${terms.length} LIKE terms, more than ${MAX_LIKE_TERMS}`);
throw new Error(`a reference lookup may hold at most ${MAX_LIKE_TERMS} LIKE terms`);
}
return { $or: terms } as unknown as FilterQuery<T>;
}
function chunked<T>(items: readonly T[], size: number): T[][] {
return Array.from({ length: Math.ceil(items.length / size) }, (_page, i) => items.slice(i * size, (i + 1) * size));
}
@@ -49,13 +49,14 @@ export class StorageReclaimController {
/**
* Starts a report-only pass in the background and answers 202 at once; the
* admin state says `running` until its report lands. 409 on a demo or while
* another storage job, a reclaim pass included, runs.
* another storage job, a reclaim pass included, runs (a release tick, which
* takes seconds, is waited for).
*/
@Post('run')
@HttpCode(202)
@ResponseContract(storageReclaimRunStartedSchema)
async run(@CurrentUser() user: User, @Req() req: Request): Promise<{ started: true }> {
this.reclaim.start();
await this.reclaim.start();
await this.audit.writeAudit({
userId: user.id,
action: 'admin.storage_reclaim_run',
@@ -67,7 +68,9 @@ export class StorageReclaimController {
/**
* Lets the pass delete in a category whose unreferenced share is over a
* fifth, up to the count the last report showed. 409 when nothing waits.
* fifth, for the count the admin was shown (`unreferenced`). 409
* `reclaim_nothing_to_confirm` when nothing waits, `reclaim_confirm_stale`
* when the last report's count is no longer the one shown.
*/
@Post('confirm')
@HttpCode(200)
@@ -77,15 +80,12 @@ export class StorageReclaimController {
@Body() body: StorageReclaimConfirmDto,
@Req() req: Request,
): Promise<StorageReclaimState> {
const state = await this.reclaim.confirm(body.category);
const state = await this.reclaim.confirm(body.category, body.unreferenced);
await this.audit.writeAudit({
userId: user.id,
action: 'admin.storage_reclaim_confirm',
ip: getClientIp(req),
details: {
category: body.category,
unreferenced: state.lastRun?.categories[body.category].unreferenced.objects ?? 0,
},
details: { category: body.category, unreferenced: body.unreferenced },
});
return state;
}
@@ -1,3 +1,5 @@
import { confirmationCovers, type ReclaimConfirmations } from './storage-reclaim.settings';
import type { ServedCategory } from './storage.types';
import type { StorageReclaimCategoryReport, StorageReclaimReport } from '@trek/shared';
/** One object a pass looks at. */
@@ -38,6 +40,28 @@ function countOf(candidates: readonly ReclaimCandidate[]): { objects: number; by
return { objects: candidates.length, bytes: candidates.reduce((sum, candidate) => sum + candidate.size, 0) };
}
/**
* The report as the admin state shows it: a category whose live confirmation
* covers its unreferenced count no longer reads as waiting for one, so the
* prompt goes as soon as the admin confirmed, not only after the next pass.
*/
export function withConfirmations(
report: StorageReclaimReport,
confirmations: ReclaimConfirmations,
now: number,
): StorageReclaimReport {
const categories = Object.fromEntries(
Object.entries(report.categories).map(([category, row]) => [
category,
row.confirmationRequired &&
confirmationCovers(confirmations[category as ServedCategory], row.unreferenced.objects, now)
? { ...row, confirmationRequired: false }
: row,
]),
) as StorageReclaimReport['categories'];
return { ...report, categories };
}
/** What a report says the pass deleted, over every category. */
export function deletedTotal(report: StorageReclaimReport): { objects: number; bytes: number } {
const rows = Object.values(report.categories);
+223 -193
View File
@@ -7,72 +7,82 @@ import type {
} from '../../db/repositories/StorageReclaimMarks.repository';
import { dbNow, parseDbTimestamp } from '../../db/types';
import { RuntimeEnvService } from '../app-config/runtime-env.service';
import { logError, logInfo, logWarn } from '../audit/audit-log.logger';
import { logError, logInfo } from '../audit/audit-log.logger';
import { AuditService } from '../audit/audit.service';
import { DomainError } from '../common/domain-error';
import { DatabaseLifecycle } from '../database/database-lifecycle.service';
import { withRequestContext } from '../database/request-context';
import { UnitOfWork } from '../database/unit-of-work';
import { StorageInstallService } from './storage-install.service';
import { StorageJobsService } from './storage-jobs.service';
import { StorageReclaimGuardService } from './storage-reclaim-guard.service';
import { StorageReclaimRegistryService } from './storage-reclaim-registry.service';
import { inReclaimScope, RECLAIM_LEDGER, sharedLocationsOf } from './storage-reclaim.ledger';
import { inReclaimScope, RECLAIM_LEDGER, reclaimableCategories, sharedLocationsOf } from './storage-reclaim.ledger';
import {
deletedTotal,
newProgress,
reportOf,
withConfirmations,
type CategoryProgress,
type ReclaimCandidate,
} from './storage-reclaim.report';
import {
INSTALL_ID_KEY,
confirmationCovers,
confirmationExpired,
confirmationStale,
overConfirmShare,
RECLAIM_DELETE_KEY,
RECLAIM_DELETE_REPORT_KEY,
RECLAIM_REPORT_KEY,
RECLAIM_RESET_KEYS,
readReclaimConfirmations,
readReclaimDeleteEnabled,
readReclaimReport,
writeReclaimConfirmations,
type ReclaimConfirmations,
} from './storage-reclaim.settings';
import { storageOwnerMarkerInstall, storageOwnerMarkerKey, type StorageReferenceSource } from './storage-reclaim.types';
import { storageOwnerMarkerInstall, type StorageReferenceSource } from './storage-reclaim.types';
import { StorageRegistryService } from './storage-registry.service';
import { STORAGE_RELEASE_JOB } from './storage-release.service';
import { StorageService } from './storage.service';
import { SERVED_CATEGORIES, type ServedCategory } from './storage.types';
import { MikroORM } from '@mikro-orm/core';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable, type OnApplicationBootstrap, type OnModuleInit } from '@nestjs/common';
import { Injectable, type OnModuleInit } from '@nestjs/common';
import type { StorageReclaimCategoryReport, StorageReclaimReport, StorageReclaimState } from '@trek/shared';
import { randomUUID } from 'node:crypto';
import { Readable } from 'node:stream';
const HOUR_MS = 60 * 60 * 1000;
/** An object younger than this is never judged: every writer puts the bytes before it inserts the row. */
const RECLAIM_GRACE_MS = 24 * HOUR_MS;
/** How long an object must stay unreferenced, on every pass, before a pass may delete it. */
const RECLAIM_DELAY_MS = 7 * 24 * HOUR_MS;
/** More unreferenced than this share of a category waits for an admin's confirmation before anything goes. */
const CONFIRM_SHARE = 0.2;
/** Keys per re-check right before a delete: one fresh, narrowed question to every source. */
const RECHECK_BATCH = 100;
/** Deletes in flight at once. */
/** Eligible objects per fresh read of every source right before their deletes. */
const FRESH_BATCH = 1000;
/** Stats and deletes in flight at once. */
const DELETE_CONCURRENCY = 20;
/** Marks per statement: four bound values each, well under SQLite's limit. */
const MARK_BATCH = 100;
/** Listed objects between two looks at whether a restore began, so a restore waits seconds, not a whole listing. */
const LIST_CHECK_EVERY = 500;
const JOB_NAME = 'a storage reclaim';
const INSTALL_ID = /^[a-z0-9-]{8,64}$/;
/** What each source names, per category, from the one read a pass makes of it; an Error when that read failed. */
type NamedCache = Map<StorageReferenceSource, Map<ServedCategory, Set<string>> | Error>;
interface PassContext {
now: number;
installId: string;
/** Null when this installation has no id: nothing is judged then. */
installId: string | null;
/** Deletes may go now; turned off for the rest of the pass once the switch reads off or a backup starts. */
deleting: boolean;
/** A backup turned deleting off: every category with something due says so. */
backupHeld: boolean;
sources: readonly StorageReferenceSource[];
shared: ReadonlySet<ServedCategory>;
confirmed: Partial<Record<ServedCategory, number>>;
generation: number;
confirmations: ReclaimConfirmations;
/** Confirmations this pass found used up or run out, by the time they were given. */
spent: Map<ServedCategory, number>;
/** Read once, at the first category judged; each category's sets are dropped once it is judged. */
named: NamedCache | null;
epoch: number;
}
interface ReclaimRunOptions {
@@ -89,37 +99,37 @@ class ReclaimStopped extends Error {}
* more, found in two phases and, only when the admin switched deletion on,
* deleted. A pass lists each category the ledger allows at its root, skips
* objects younger than a day (the bytes of an upload land before its row),
* reads once per category what every registered reference source names, and
* marks the rest with the time it first saw them. An object goes only once its
* mark is a week old, after one more fresh question to every source about its
* batch, through `StorageService.delete` (so replicas follow and a demo keeps
* its bytes). What a user deletes or replaces is released at once instead
* reads every registered reference source once for the whole pass, and marks
* the rest with the time it first saw them. An object goes only once its mark
* is a week old, after a fresh read of every source right before its batch,
* through `StorageService.delete` (so replicas follow and a demo keeps its
* bytes). What a user deletes or replaces is queued for release instead
* (StorageReleaseService); the pass is the net for whatever that left behind.
*
* Safety, each failing closed:
* - a category whose sources are not all registered, whose location overlaps
* another category's, or where another installation's owner marker lies
* (or this one's cannot be written) is not judged;
* (or this one's cannot be written) is not judged (StorageReclaimGuardService);
* - a failed listing or lookup skips the category for the night; no skip
* touches the marks, so one bad night does not restart the week;
* - more than a fifth unreferenced deletes nothing until an admin confirmed
* that count (`confirm`), which no user action can do;
* the count they were shown (`confirm`), which no user action can do; the
* confirmation covers half as much again for two weeks, and is used up once
* the share is back under a fifth;
* - a delete that fails on the backend stops deleting in that category;
* - nothing is deleted while a backup reads the uploads, once the switch is
* turned off mid-pass, or on a demo instance;
* - a pass never runs beside a backfill, a category migration or a restore
* (the one-job slot of StorageJobsService, which a restore waits on), and
* stops when a restore begins or the database is reopened under it.
* - nothing is deleted while a backup reads the uploads (`backup_running`),
* once the switch is turned off mid-pass, or on a demo instance;
* - a pass never runs beside a backfill, a category migration, a release tick
* or a restore (the one-job slot of StorageJobsService, which a restore
* waits on), and stops when a restore begins or the database is reopened.
*
* Every restore turns deletion off and drops the marks, the confirmations and
* the reports: the restored rows may come from another installation, and this
* one keeps its own install id (written back over the restored one).
* Which installation this is comes from a file in the data dir, not from the
* database (StorageInstallService): a database restored, cloned or copied from
* another one loses its switch, marks, confirmations and reports at boot, and
* every reopen does the same.
*/
@Injectable()
export class StorageReclaimService implements OnModuleInit, OnApplicationBootstrap {
private generation = 0;
private knownInstallId: string | null = null;
export class StorageReclaimService implements OnModuleInit {
constructor(
private readonly storage: StorageService,
private readonly registry: StorageRegistryService,
@@ -128,64 +138,74 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
private readonly lifecycle: DatabaseLifecycle,
private readonly runtimeEnv: RuntimeEnvService,
private readonly audit: AuditService,
private readonly uow: UnitOfWork,
private readonly orm: MikroORM,
private readonly install: StorageInstallService,
private readonly guard: StorageReclaimGuardService,
@InjectRepository(StorageReclaimMarks) private readonly marks: StorageReclaimMarksRepository,
@InjectRepository(AppSettings) private readonly appSettings: AppSettingsRepository,
) {}
onModuleInit(): void {
this.lifecycle.onReopened(() => this.afterReopen());
this.lifecycle.onReopened(async () => {
this.guard.forget();
await this.install.afterReopen();
});
}
/** Makes the install id on the first boot, so a restore later has one of this install's own to keep. */
async onApplicationBootstrap(): Promise<void> {
try {
await withRequestContext(this.orm, () => this.installId());
} catch (err) {
logWarn(`[storage] could not read or make the install id: ${messageOf(err)}`);
}
}
/** Read only, so the admin state's GET never writes: an install id not made yet reads as deletion off. */
/**
* Read only, so the admin state's GET never writes (the install id file is
* made at boot). A category whose live confirmation covers its count reads
* as no longer waiting for one, before the next pass says so too.
*/
async state(): Promise<StorageReclaimState> {
const installId = this.knownInstallId ?? (await this.appSettings.getValue(INSTALL_ID_KEY));
const installId = this.install.installId();
const lastRun = await readReclaimReport(this.appSettings, RECLAIM_REPORT_KEY);
const confirmations = installId === null ? {} : await readReclaimConfirmations(this.appSettings, installId);
return {
deleteEnabled: installId !== null && (await readReclaimDeleteEnabled(this.appSettings, installId)),
running: this.jobs.exclusiveHolder === JOB_NAME,
lastRun: await readReclaimReport(this.appSettings, RECLAIM_REPORT_KEY),
lastRun: lastRun === null ? null : withConfirmations(lastRun, confirmations, Date.now()),
lastDeleteRun: await readReclaimReport(this.appSettings, RECLAIM_DELETE_REPORT_KEY),
};
}
/** 409 `reclaim_install_unknown` when this installation has no id to bind the switch to. */
async setDeleteEnabled(enabled: boolean): Promise<StorageReclaimState> {
if (enabled) await this.appSettings.upsertOrReplace(RECLAIM_DELETE_KEY, await this.installId());
if (enabled) await this.appSettings.upsertOrReplace(RECLAIM_DELETE_KEY, this.requireInstallId());
else await this.appSettings.deleteValue(RECLAIM_DELETE_KEY);
return await this.state();
}
/**
* The admin lets the pass delete in a category whose unreferenced share is
* over a fifth, up to the count the last report showed. 409 when the last
* report holds nothing that waits for it.
* over a fifth, for the count they were shown (`shown`). 409
* `reclaim_nothing_to_confirm` when the last report holds nothing waiting
* there, `reclaim_confirm_stale` when its count is no longer the one shown.
*/
async confirm(category: ServedCategory): Promise<StorageReclaimState> {
async confirm(category: ServedCategory, shown: number): Promise<StorageReclaimState> {
const installId = this.requireInstallId();
const row = (await readReclaimReport(this.appSettings, RECLAIM_REPORT_KEY))?.categories[category];
if (!row?.confirmationRequired) {
throw refusal('reclaim_nothing_to_confirm', 'nothing in this category waits for a confirmation');
}
const installId = await this.installId();
const counts = await readReclaimConfirmations(this.appSettings, installId);
await writeReclaimConfirmations(this.appSettings, installId, { ...counts, [category]: row.unreferenced.objects });
if (confirmationStale(shown, row.unreferenced.objects)) {
throw refusal('reclaim_confirm_stale', 'the last check found another count since, reload and confirm again');
}
const confirmations = await readReclaimConfirmations(this.appSettings, installId);
await writeReclaimConfirmations(this.appSettings, installId, {
...confirmations,
[category]: { count: shown, confirmedAt: Date.now() },
});
return await this.state();
}
/**
* One pass over every category in the caller, its report persisted and
* returned: the nightly job's. Refuses with a 409 on a demo instance and
* while another storage job holds the slot.
* while another storage job holds the slot; a release tick is waited for.
*/
async run(options: ReclaimRunOptions = {}): Promise<StorageReclaimReport> {
await this.releaseTickDone();
this.claimSlot();
try {
return await this.pass(options.reportOnly === true, options.now ?? new Date());
@@ -199,7 +219,8 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
* longer than a proxy waits for an answer. The same refusals as `run`, at
* once; the report lands on the admin state, which says `running` meanwhile.
*/
start(): void {
async start(): Promise<void> {
await this.releaseTickDone();
this.claimSlot();
void withRequestContext(this.orm, () => this.pass(true, new Date()))
.catch((err: unknown) => {
@@ -210,6 +231,11 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
});
}
/** A release tick holds the slot for seconds: a pass waits for it rather than losing its night. */
private async releaseTickDone(): Promise<void> {
if (this.jobs.exclusiveHolder === STORAGE_RELEASE_JOB) await this.jobs.exclusiveIdle();
}
private claimSlot(): void {
if (this.runtimeEnv.isDemoMode()) throw refusal('reclaim_demo', 'storage reclaim does not run on a demo instance');
if (!this.jobs.claimExclusive(JOB_NAME)) {
@@ -217,47 +243,12 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
}
}
/**
* This install's id, made and stored on first use.
*
* @txStandalone an idempotent lazy provision: the row is made once and only
* read after, whatever the caller writes besides.
*/
private async installId(): Promise<string> {
if (this.knownInstallId) return this.knownInstallId;
let id = await this.appSettings.getValue(INSTALL_ID_KEY);
if (id === null || !INSTALL_ID.test(id)) {
id = randomUUID();
await this.appSettings.upsertOrReplace(INSTALL_ID_KEY, id);
}
this.knownInstallId = id;
return id;
}
/**
* A restore or a reopen swapped the database: a pass in flight stops, and
* the restored rows get this install's id back, deletion off and no marks,
* confirmations or reports. Contained: a listener that throws fails the reopen.
*/
private async afterReopen(): Promise<void> {
this.generation += 1;
try {
await withRequestContext(this.orm, () =>
this.uow.transactional(async () => {
const wasOn = this.knownInstallId !== null && (await this.appSettings.getValue(RECLAIM_DELETE_KEY)) !== null;
if (this.knownInstallId) await this.appSettings.upsertOrReplace(INSTALL_ID_KEY, this.knownInstallId);
await RECLAIM_RESET_KEYS.reduce<Promise<void>>(async (previous, key) => {
await previous;
await this.appSettings.deleteValue(key);
}, Promise.resolve());
await this.marks.deleteAll();
if (wasOn)
logInfo('[storage] the restored database had reclaim deletion on: it is off until an admin turns it on');
}),
);
} catch (err) {
logError(`[storage] could not reset the reclaim state after the database was reopened: ${messageOf(err)}`);
private requireInstallId(): string {
const installId = this.install.installId();
if (installId === null) {
throw refusal('reclaim_install_unknown', 'this installation has no install id file, so nothing is reclaimed');
}
return installId;
}
/**
@@ -266,23 +257,30 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
*/
private async pass(reportOnly: boolean, now: Date): Promise<StorageReclaimReport> {
const wallStart = Date.now();
const installId = await this.installId();
const deleting = !reportOnly && (await readReclaimDeleteEnabled(this.appSettings, installId));
const installId = this.install.installId();
const deleting = !reportOnly && installId !== null && (await readReclaimDeleteEnabled(this.appSettings, installId));
const ctx: PassContext = {
now: now.getTime(),
installId,
deleting,
backupHeld: false,
sources: [...this.references.sources()],
shared: sharedLocationsOf(this.registry),
confirmed: await readReclaimConfirmations(this.appSettings, installId),
generation: this.generation,
confirmations: installId === null ? {} : await readReclaimConfirmations(this.appSettings, installId),
spent: new Map(),
named: null,
epoch: this.install.epoch,
};
const categories = {} as Record<ServedCategory, StorageReclaimCategoryReport>;
let stopped: StorageReclaimReport['stopped'] = null;
await SERVED_CATEGORIES.reduce<Promise<void>>(async (previous, category) => {
await previous;
const progress = newProgress();
if (stopped === null) {
if (!RECLAIM_LEDGER[category].reclaim) {
progress.skipped = 'not_reclaimed';
} else if (stopped !== null) {
progress.skipped = 'interrupted';
} else {
try {
await this.reclaimCategory(category, ctx, progress);
} catch (err) {
@@ -290,11 +288,10 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
if (stopped === 'failed') logError(`[storage] the reclaim pass failed in '${category}': ${messageOf(err)}`);
progress.skipped = 'interrupted';
}
} else {
progress.skipped = 'interrupted';
}
categories[category] = reportOf(progress);
}, Promise.resolve());
ctx.named = null;
const report: StorageReclaimReport = {
startedAt: ctx.now,
finishedAt: ctx.now + (Date.now() - wallStart),
@@ -302,10 +299,29 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
stopped,
categories,
};
if (stopped !== 'database_reopened') await this.pruneConfirmations(ctx);
await this.persist(report);
return report;
}
/**
* Drops the confirmations the pass found used up (the share is back under
* a fifth) or run out, unless an admin gave a new one meanwhile.
*
* @txStandalone one read and one write of the confirmations row, complete on its own.
*/
private async pruneConfirmations(ctx: PassContext): Promise<void> {
if (ctx.spent.size === 0 || ctx.installId === null) return;
const current = await readReclaimConfirmations(this.appSettings, ctx.installId);
const kept: ReclaimConfirmations = {};
for (const [category, confirmation] of Object.entries(current) as Array<
[ServedCategory, ReclaimConfirmations[ServedCategory]]
>) {
if (confirmation && ctx.spent.get(category) !== confirmation.confirmedAt) kept[category] = confirmation;
}
await writeReclaimConfirmations(this.appSettings, ctx.installId, kept);
}
/**
* The report as the last pass's, and as the last deleting pass's when it ran
* with deletion on; an audit entry (the system as actor) when it deleted.
@@ -344,21 +360,12 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
* delete stands alone, so a failure part way keeps what came before.
*/
private async reclaimCategory(category: ServedCategory, ctx: PassContext, progress: CategoryProgress): Promise<void> {
const rule = RECLAIM_LEDGER[category];
if (!rule.reclaim) {
progress.skipped = 'not_reclaimed';
return;
}
const sources = ctx.sources.filter((source) => claimsAny(source, category));
const answering = new Set(sources.filter((source) => answersFor(source, category)).map((source) => source.id));
if (rule.requiredSources.some((id) => !answering.has(id))) {
progress.skipped = 'sources_missing';
return;
}
if (ctx.shared.has(category)) {
progress.skipped = 'shared_location';
const judged = this.guard.judgeable(category, ctx.sources, ctx.shared);
if ('refused' in judged) {
progress.skipped = judged.refused;
return;
}
const { sources } = judged;
let listed: { objects: number; old: ReclaimCandidate[]; owners: Set<string> };
try {
@@ -370,7 +377,10 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
return;
}
progress.objects = listed.objects;
const owner = await this.ownerVerdict(category, listed.owners, ctx.installId);
const owner =
ctx.installId === null
? 'owner_unknown'
: await this.guard.ownerVerdict(category, listed.owners, ctx.installId, ctx.now);
if (owner !== null) {
progress.skipped = owner;
return;
@@ -378,7 +388,7 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
let named: Set<string>;
try {
named = await this.namedIn(category, sources);
named = await this.namedIn(category, sources, ctx);
} catch (err) {
logError(`[storage] reclaim could not check the references of '${category}': ${messageOf(err)}`);
progress.skipped = 'source_failed';
@@ -392,9 +402,20 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
await this.writeMarks(category, unreferenced, marks, ctx.now);
const cutoff = ctx.now - RECLAIM_DELAY_MS;
progress.eligible = unreferenced.filter((candidate) => markedBefore(marks.get(candidate.key), cutoff));
const over = overConfirmShare(unreferenced.length, listed.objects);
this.noteConfirmation(category, over, ctx);
progress.confirmationRequired =
unreferenced.length > listed.objects * CONFIRM_SHARE && unreferenced.length > (ctx.confirmed[category] ?? 0);
if (ctx.deleting && !progress.confirmationRequired) await this.deleteEligible(category, sources, ctx, progress);
over && !confirmationCovers(ctx.confirmations[category], unreferenced.length, ctx.now);
if (progress.confirmationRequired || progress.eligible.length === 0) return;
if (ctx.deleting) await this.deleteEligible(category, sources, ctx, progress);
else if (ctx.backupHeld) progress.skipped = 'backup_running';
}
/** A confirmation is spent once the share is back under a fifth, and void once it ran out. */
private noteConfirmation(category: ServedCategory, over: boolean, ctx: PassContext): void {
const confirmation = ctx.confirmations[category];
if (!confirmation) return;
if (!over || confirmationExpired(confirmation, ctx.now)) ctx.spent.set(category, confirmation.confirmedAt);
}
/** The root-level objects of a category, of them the ones past the grace period, and the owner markers there. */
@@ -424,53 +445,49 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
}
/**
* Whether this installation may judge the place a category lives in: not
* where another one's owner marker lies, and not where its own could not be
* written, since then a second installation cannot see it either.
* Every key of the category some source names, lowercased, from the one read
* of each source the pass makes at its first judged category; the category's
* sets are dropped once they are merged, so memory falls as the pass goes.
*/
private async ownerVerdict(
private async namedIn(
category: ServedCategory,
owners: ReadonlySet<string>,
installId: string,
): Promise<'foreign_install' | 'owner_unknown' | null> {
const foreign = [...owners].some((owner) => owner !== installId);
if (!owners.has(installId)) {
try {
await this.storage.put(category, storageOwnerMarkerKey(installId), Readable.from([installId]), {
contentType: 'text/plain',
});
} catch (err) {
logWarn(`[storage] reclaim could not write its owner marker into '${category}': ${messageOf(err)}`);
return foreign ? 'foreign_install' : 'owner_unknown';
}
}
if (foreign) logWarn(`[storage] another TREK installation keeps its uploads where '${category}' does: not judged`);
return foreign ? 'foreign_install' : null;
}
/** Every key of the category some source names, lowercased: one read per source for the whole category. */
private async namedIn(category: ServedCategory, sources: readonly StorageReferenceSource[]): Promise<Set<string>> {
const answers = await Promise.all(sources.map((source) => source.namedKeys(category)));
sources: readonly StorageReferenceSource[],
ctx: PassContext,
): Promise<Set<string>> {
const cache = (ctx.named ??= await this.readNamed(ctx.sources));
const named = new Set<string>();
for (const answer of answers) for (const key of answer) named.add(key);
for (const source of sources) {
const answer = cache.get(source);
if (answer === undefined) throw new Error(`the source '${source.id}' was not read`);
if (answer instanceof Error) throw answer;
for (const key of answer.get(category) ?? []) named.add(key);
}
for (const answer of cache.values()) if (!(answer instanceof Error)) answer.delete(category);
return named;
}
/** The keys some source names right now, lowercased: the fresh question before a delete. */
private async referencedIn(
category: ServedCategory,
keys: readonly string[],
sources: readonly StorageReferenceSource[],
): Promise<Set<string>> {
const answers = await Promise.all(
sources.map(async (source) => {
const asked = keys.filter((key) => claimsKey(source, category, key));
return asked.length === 0 ? new Set<string>() : await source.referenced(category, asked);
}),
);
const referenced = new Set<string>();
for (const answer of answers) for (const key of answer) referenced.add(key.toLowerCase());
return referenced;
/** One read of each source for every reclaimable category, one source after another to bound memory. */
private async readNamed(sources: readonly StorageReferenceSource[]): Promise<NamedCache> {
const categories = reclaimableCategories();
const cache: NamedCache = new Map();
await sources.reduce<Promise<void>>(async (previous, source) => {
await previous;
try {
cache.set(source, await source.namedKeysByCategory(categories));
} catch (err) {
logError(`[storage] reclaim could not read the reference source '${source.id}': ${messageOf(err)}`);
cache.set(source, err instanceof Error ? err : new Error(String(err)));
}
}, Promise.resolve());
return cache;
}
/** Every key of the category some source names right now, lowercased: the fresh read before deletes. */
private async namedNow(category: ServedCategory, sources: readonly StorageReferenceSource[]): Promise<Set<string>> {
const answers = await Promise.all(sources.map((source) => source.namedKeys(category)));
const named = new Set<string>();
for (const answer of answers) for (const key of answer) named.add(key.toLowerCase());
return named;
}
/**
@@ -499,7 +516,8 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
/**
* The eligible objects a batch at a time, while deleting may still go; a
* batch whose backend failed stops the category with `delete_failed`.
* batch whose backend failed stops the category with `delete_failed`, a
* backup that started stops it with `backup_running`.
*
* @txIndependent a batch at a time, each delete standing alone, so a failure
* leaves the others' work standing.
@@ -510,9 +528,13 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
ctx: PassContext,
progress: CategoryProgress,
): Promise<void> {
await chunk(progress.eligible, RECHECK_BATCH).reduce<Promise<void>>(async (previous, batch) => {
await chunk(progress.eligible, FRESH_BATCH).reduce<Promise<void>>(async (previous, batch) => {
await previous;
if (progress.skipped !== null || !(await this.mayDelete(ctx))) return;
if (progress.skipped !== null) return;
if (!(await this.mayDelete(ctx))) {
if (ctx.backupHeld) progress.skipped = 'backup_running';
return;
}
this.assertSameDatabase(ctx);
const failed = await this.deleteBatch(category, batch, sources, ctx, progress);
if (failed) progress.skipped = 'delete_failed';
@@ -521,18 +543,22 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
/** Deleting goes on while the switch stays on and no backup reads the uploads; once off, off for the pass. */
private async mayDelete(ctx: PassContext): Promise<boolean> {
if (!ctx.deleting) return false;
if (this.jobs.backupRunning || !(await readReclaimDeleteEnabled(this.appSettings, ctx.installId))) {
if (!ctx.deleting || ctx.installId === null) return false;
if (this.jobs.backupRunning) {
ctx.deleting = false;
ctx.backupHeld = true;
logInfo('[storage] a backup is reading the uploads: this reclaim pass deletes nothing more, the next one will');
} else if (!(await readReclaimDeleteEnabled(this.appSettings, ctx.installId))) {
ctx.deleting = false;
}
return ctx.deleting;
}
/**
* Checks each object is still the old one the pass judged, asks every source
* about the batch again, fresh, and deletes what none of them names. The
* question goes last so nothing slower than the deletes sits between the
* answer and them. True when the backend failed, so the category stops.
* Checks each object is still the old one the pass judged, reads every
* source again, fresh and whole, and deletes what none of them names. The
* read goes last so nothing slower than the deletes sits between the answer
* and them. True when the backend failed, so the category stops.
*
* @txIndependent every object goes on its own; its mark goes after it.
*/
@@ -545,7 +571,7 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
): Promise<boolean> {
let stats: Awaited<ReturnType<StorageService['stat']>>[];
try {
stats = await Promise.all(batch.map((candidate) => this.storage.stat(category, candidate.key)));
stats = await inGroups(batch, (candidate) => this.storage.stat(category, candidate.key));
} catch (err) {
logError(`[storage] reclaim stopped deleting in '${category}', the backend failed: ${messageOf(err)}`);
return true;
@@ -562,7 +588,7 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
if (due.length === 0) return false;
let referenced: Set<string>;
try {
referenced = await this.referencedIn(category, keysOf(due), sources);
referenced = await this.namedNow(category, sources);
} catch (err) {
logError(
`[storage] reclaim kept ${due.length} '${category}' object(s), their re-check failed: ${messageOf(err)}`,
@@ -594,12 +620,20 @@ export class StorageReclaimService implements OnModuleInit, OnApplicationBootstr
/** A restore that began, or a reopen that happened, since the pass started ends it. */
private assertSameDatabase(ctx: PassContext): void {
if (this.jobs.restoreRunning) throw new ReclaimStopped('a restore began during the pass');
if (this.generation !== ctx.generation) throw new ReclaimStopped('the database was reopened during the pass');
if (this.install.epoch !== ctx.epoch) throw new ReclaimStopped('the database was reopened during the pass');
}
}
/** A 409 whose body names the refusal in `code` next to the text, so the client can say it in the user's language. */
function refusal(code: 'reclaim_busy' | 'reclaim_demo' | 'reclaim_nothing_to_confirm', message: string): DomainError {
function refusal(
code:
| 'reclaim_busy'
| 'reclaim_demo'
| 'reclaim_nothing_to_confirm'
| 'reclaim_confirm_stale'
| 'reclaim_install_unknown',
message: string,
): DomainError {
return new DomainError(409, message, { code, details: { code } });
}
@@ -608,20 +642,6 @@ function markedBefore(mark: ReclaimMarkRow | undefined, cutoff: number): boolean
return firstSeen !== null && firstSeen.getTime() <= cutoff;
}
/** The source answers for some part of the category. */
function claimsAny(source: StorageReferenceSource, category: ServedCategory): boolean {
return source.claims.some((claim) => claim.category === category);
}
/** The source answers for the whole category: what the ledger's required sources must do. */
function answersFor(source: StorageReferenceSource, category: ServedCategory): boolean {
return source.claims.some((claim) => claim.category === category && claim.prefix === '');
}
function claimsKey(source: StorageReferenceSource, category: ServedCategory, key: string): boolean {
return source.claims.some((claim) => claim.category === category && key.startsWith(claim.prefix));
}
function keysOf(candidates: readonly ReclaimCandidate[]): string[] {
return candidates.map((candidate) => candidate.key);
}
@@ -630,6 +650,16 @@ function chunk<T>(items: readonly T[], size: number): T[][] {
return Array.from({ length: Math.ceil(items.length / size) }, (_page, i) => items.slice(i * size, (i + 1) * size));
}
/** `work` over every item, DELETE_CONCURRENCY at a time, the answers in order; the first failure rejects. */
async function inGroups<T, R>(items: readonly T[], work: (item: T) => Promise<R>): Promise<R[]> {
const answers: R[] = [];
await chunk(items, DELETE_CONCURRENCY).reduce<Promise<void>>(async (previous, group) => {
await previous;
answers.push(...(await Promise.all(group.map(work))));
}, Promise.resolve());
return answers;
}
function messageOf(err: unknown): string {
return err instanceof Error ? err.message : String(err);
}
@@ -6,13 +6,13 @@ import { storageReclaimReportSchema, type StorageReclaimReport } from '@trek/sha
import { z } from 'zod';
/**
* This installation's random id, made on its first boot. A backup restore
* brings back the app_settings of whatever install made the backup, so the
* reclaim pass keeps the id it booted with and writes it back over the
* restored one (StorageReclaimService, on every database reopen). Outside the
* `storage.` prefix on purpose: the documented reset of the storage config
* (deleting every `storage.%` row) must not make this install a stranger to
* the owner markers it wrote.
* The install id the database was last reconciled with: a copy of the one in
* `data/.trek-install-id` (StorageInstallService), which no backup carries and
* no restore writes. A database whose row names another id came from another
* installation (a restore, a clone, a copied file), and its reclaim state is
* reset before anything reads it. Outside the `storage.` prefix on purpose:
* the documented reset of the storage config (deleting every `storage.%` row)
* must not read as a database from elsewhere.
*/
export const INSTALL_ID_KEY = 'install_id';
/**
@@ -26,10 +26,15 @@ export const RECLAIM_REPORT_KEY = 'storage.reclaim_report';
export const RECLAIM_DELETE_REPORT_KEY = 'storage.reclaim_delete_report';
/**
* Per category, the unreferenced count an admin confirmed when that was more
* than a fifth of the category, for the install it was confirmed on.
* than a fifth of the category, with when, for the install it was confirmed on.
*/
const RECLAIM_CONFIRMED_KEY = 'storage.reclaim_confirmed';
/** More unreferenced than this share of a category waits for an admin's confirmation before anything goes. */
const CONFIRM_SHARE = 0.2;
/** How long a confirmation lets a pass delete, unless the share fell under the line before. */
const CONFIRM_TTL_MS = 14 * 24 * 60 * 60 * 1000;
/** Every row a restore or a foreign database must not carry over into this install's reclaim. */
export const RECLAIM_RESET_KEYS = [
RECLAIM_DELETE_KEY,
@@ -40,9 +45,55 @@ export const RECLAIM_RESET_KEYS = [
const confirmationsSchema = z.object({
installId: z.string(),
counts: z.record(z.string(), z.number().int().nonnegative()),
categories: z.record(
z.string(),
z.object({ count: z.number().int().nonnegative(), confirmedAt: z.number().int().nonnegative() }),
),
});
/** One admin confirmation: the unreferenced count the admin was shown, and when (epoch ms). */
interface ReclaimConfirmation {
count: number;
confirmedAt: number;
}
export type ReclaimConfirmations = Partial<Record<ServedCategory, ReclaimConfirmation>>;
/** More of the category is unreferenced than a fifth of it. */
export function overConfirmShare(unreferenced: number, objects: number): boolean {
return unreferenced > objects * CONFIRM_SHARE;
}
/** The confirmation ran out: older than CONFIRM_TTL_MS. */
export function confirmationExpired(confirmation: ReclaimConfirmation, now: number): boolean {
return now - confirmation.confirmedAt >= CONFIRM_TTL_MS;
}
/**
* A live confirmation covers this many unreferenced objects: up to one and a
* half times the count the admin confirmed, and at least 100 more, so the
* orphans a busy instance adds before the backlog has drained do not undo it,
* while a surge well past it waits for a new one.
*/
export function confirmationCovers(
confirmation: ReclaimConfirmation | undefined,
unreferenced: number,
now: number,
): boolean {
if (!confirmation || confirmationExpired(confirmation, now)) return false;
return unreferenced <= Math.max(confirmation.count * 1.5, confirmation.count + 100);
}
/**
* The count the admin was shown is not the one the last report holds: more
* than a tenth of it, or more than 50 objects, apart. Then a pass ran since
* the page loaded, and the admin would confirm a number they never saw.
*/
export function confirmationStale(shown: number, current: number): boolean {
const apart = Math.abs(current - shown);
return apart > shown * 0.1 || apart > 50;
}
/** Fail closed: only this install's own id switches deletion on. */
export async function readReclaimDeleteEnabled(
appSettings: AppSettingsRepository,
@@ -66,26 +117,31 @@ export async function readReclaimReport(
}
}
/** The confirmed counts of this install; none for a row another install wrote or that no longer parses. */
/** The confirmations of this install; none for a row another install wrote or that no longer parses. */
export async function readReclaimConfirmations(
appSettings: AppSettingsRepository,
installId: string,
): Promise<Partial<Record<ServedCategory, number>>> {
): Promise<ReclaimConfirmations> {
const value = await appSettings.getValue(RECLAIM_CONFIRMED_KEY);
if (value == null) return {};
try {
const parsed = confirmationsSchema.parse(JSON.parse(value));
return parsed.installId === installId ? (parsed.counts as Partial<Record<ServedCategory, number>>) : {};
return parsed.installId === installId ? (parsed.categories as ReclaimConfirmations) : {};
} catch {
logWarn('[storage] the stored reclaim confirmations are unparseable, treating them as none');
return {};
}
}
/** Stores this install's confirmations; none left drops the row. */
export async function writeReclaimConfirmations(
appSettings: AppSettingsRepository,
installId: string,
counts: Partial<Record<ServedCategory, number>>,
categories: ReclaimConfirmations,
): Promise<void> {
await appSettings.upsertOrReplace(RECLAIM_CONFIRMED_KEY, JSON.stringify({ installId, counts }));
if (Object.keys(categories).length === 0) {
await appSettings.deleteValue(RECLAIM_CONFIRMED_KEY);
return;
}
await appSettings.upsertOrReplace(RECLAIM_CONFIRMED_KEY, JSON.stringify({ installId, categories }));
}
@@ -52,16 +52,21 @@ export interface StorageReferenceSource {
*/
readonly claims: ReadonlyArray<StorageReferenceClaim>;
/**
* Every key of `category` the rows name, lowercased: one read of the whole
* column set, which a pass makes once per category. A row that is trashed,
* pending or otherwise not gone counts. Throwing stops the pass for the
* category; it never deletes on a failed answer.
* Every key of `category` the rows name, lowercased: one fresh read of the
* whole column set, the question a pass asks right before it deletes. A row
* that is trashed, pending or otherwise not gone counts. Throwing stops the
* pass for the category; it never deletes on a failed answer.
*/
namedKeys(category: ServedCategory): Promise<Set<string>>;
/**
* Of these category-relative keys (as `StorageService.list` yields them, a
* batch at a time), the ones a row names right now: the question a delete
* asks last, read fresh and narrowed to the rows that can name them.
* The same for several categories from one read, each the source claims
* keyed in the answer: what a pass reads once and judges every category by.
*/
namedKeysByCategory(categories: readonly ServedCategory[]): Promise<Map<ServedCategory, Set<string>>>;
/**
* Of these category-relative keys (as `StorageService.list` yields them), the
* ones a row names right now, read narrowed to the rows that can name them:
* the question the release queue asks before it deletes.
*/
referenced(category: ServedCategory, keys: readonly string[]): Promise<Set<string>>;
}
@@ -0,0 +1,39 @@
import { logDebug, logError } from '../audit/audit-log.logger';
import { CronRegistrarService } from '../scheduling/cron-registrar.service';
import { StorageReleaseService } from './storage-release.service';
import { Injectable, type OnApplicationBootstrap } from '@nestjs/common';
/**
* Every ten minutes, what users' deletes and replaces queued and what is due
* now (StorageReleaseService): each object asked about once more and deleted
* when no row names it. The registrar is handed the tick's own promise, so
* its lease covers the whole tick.
*/
@Injectable()
export class StorageReleaseJob implements OnApplicationBootstrap {
constructor(
private readonly registrar: CronRegistrarService,
private readonly release: StorageReleaseService,
) {}
onApplicationBootstrap(): void {
if (!this.registrar.isEnabled()) return;
this.registrar.register('storage-release', '*/10 * * * *', () => this.tick());
}
/**
* @txIndependent every delete and every dropped row stands on its own
* (StorageReleaseService.processDue).
*/
async tick(now: Date = new Date()): Promise<void> {
try {
const result = await this.release.processDue(now);
if (result.skipped !== null) logDebug(`Storage release skipped: ${result.skipped}`);
else if (result.deleted + result.kept > 0) {
logDebug(`Storage release: deleted ${result.deleted}, kept ${result.kept} still named`);
}
} catch (err: unknown) {
logError(`Storage release: ${err instanceof Error ? err.message : String(err)}`);
}
}
}
@@ -1,38 +1,105 @@
import { StorageReleaseQueue } from '../../db/entities/StorageReleaseQueue.entity';
import type {
ReleaseQueueRow,
StorageReleaseQueueRepository,
} from '../../db/repositories/StorageReleaseQueue.repository';
import { dbNow } from '../../db/types';
import { logDebug, logWarn } from '../audit/audit-log.logger';
import { UnitOfWork } from '../database/unit-of-work';
import { StorageInstallService } from './storage-install.service';
import { StorageJobsService } from './storage-jobs.service';
import { isValidKey } from './storage-keys';
import { StorageReclaimGuardService } from './storage-reclaim-guard.service';
import { StorageReclaimRegistryService } from './storage-reclaim-registry.service';
import { inReclaimScope, RECLAIM_LEDGER, sharedLocationsOf } from './storage-reclaim.ledger';
import { inReclaimScope, RECLAIM_LEDGER, reclaimableCategories, sharedLocationsOf } from './storage-reclaim.ledger';
import {
keySearchCore,
storageKeysIn,
storageOwnerMarkerInstall,
type StorageKeyOptions,
type StorageKeyRef,
type StorageReferenceSource,
} from './storage-reclaim.types';
import { StorageRegistryService } from './storage-registry.service';
import { StorageService } from './storage.service';
import { SERVED_CATEGORIES, type ServedCategory } from './storage.types';
import { InjectRepository } from '@mikro-orm/nestjs';
import { Injectable } from '@nestjs/common';
const HOUR_MS = 60 * 60 * 1000;
/**
* How long a released object waits before the queue may delete it: longer
* than every window in TREK that can write a value back that names it.
* - the assistant: a change card's undo lasts 15 minutes (UNDO_WINDOW_MS), and
* a proposed change may be approved for 24 hours (APPROVAL_TTL_MS), so a
* change proposed with an old cover path can still be applied a day later;
* - the offline mutation queue retries a write the server failed with a 5xx
* for about two hours (MAX_SERVER_ERROR_ATTEMPTS, doubling from 30 s);
* - the planner's own undo (usePlannerHistory) and a write queued while
* offline are bounded by nothing but the tab and the outage: those are
* unbounded, and the place undo does not offer to undo a delete that took an
* uploaded image (placeUndo.ts) for that reason.
* So the longest bounded window is a day; two hours on top cover the clock
* and the job's interval. What comes back later than that finds the object
* gone, as it did before the reclaim when every delete was immediate.
*/
export const RELEASE_DELAY_MS = 26 * HOUR_MS;
/** A queued row older than this past its time belongs to the nightly pass: its category stayed refused. */
const QUEUE_MAX_AGE_MS = 30 * 24 * HOUR_MS;
/** Keys one stored value may queue: a value naming more than that is no upload path of ours. */
const MAX_KEYS_PER_VALUE = 16;
/** Keys one call may queue; the rest is the nightly pass's. */
const MAX_KEYS_PER_CALL = 1000;
/** No more rows than this wait at once; a release past it is the nightly pass's. */
const QUEUE_LIMIT = 50_000;
/** Rows per insert: three bound values each. */
const ENQUEUE_BATCH = 100;
/** Rows one tick looks at. */
const TICK_LIMIT = 200;
/** Keys per question to the sources. */
const ASK_BATCH = 50;
/** Deletes in flight at once. */
const DELETE_CONCURRENCY = 10;
const SERVED = new Set<string>(SERVED_CATEGORIES);
/** The name a release tick holds the one-job slot under. */
export const STORAGE_RELEASE_JOB = 'a storage release';
export interface ReleaseTickResult {
/** Why the tick did nothing, or null when it ran. */
skipped: 'busy' | 'no_install_id' | null;
deleted: number;
/** Rows dropped because a row still names the object, or the instance keeps it. */
kept: number;
}
/** A restore began, a backup started or the database was swapped mid-tick: the rest waits for the next one. */
class ReleaseStopped extends Error {}
/**
* Deletes what a user's change left unnamed, right after it: a replaced or
* removed cover, place image or avatar, a deleted journey photo or trip file.
* The owner calls it once its transaction committed, with the values the rows
* held, and every registered reference source (the nightly pass's sources and
* parser) is asked whether anything still names each object. Only what no row
* names goes, through `StorageService.delete` (replicas follow, a demo keeps
* its bytes), so an object a trip copy, a journey or a saved place still shows
* stays, unlike before (#2924), while a deleted photo is gone at once as it
* always was.
* What a user's change left unnamed: a replaced or removed cover, place image
* or avatar, a deleted journey photo or trip file. The owner hands the values
* its rows held to `releaseValues` once its transaction committed, and every
* object they name is queued, which is all the request pays. Nothing goes
* before RELEASE_DELAY_MS, so an undo or a late write that brings the value
* back keeps its object.
*
* The pass's own guards hold: only the root of a reclaimable category, never
* an owner marker, never while a restore runs, never where a required source
* is missing or the category's location overlaps another's. It never fails
* the caller's request: anything it leaves (an error, a guard) is the nightly
* pass's to find. Releases are logged at debug, not audited one by one.
* A job (StorageReleaseJob) then takes the due rows, asks every registered
* reference source again about them (the nightly pass's sources and parser)
* and deletes only what none of them names, through `StorageService.delete`
* (replicas follow, a demo keeps its bytes); the row goes either way. So an
* object a trip copy, a journey or a saved place still shows stays (#2924),
* while a deleted photo still goes as it did before, a day later.
*
* The queue refuses what the pass refuses (StorageReclaimGuardService): only
* the root of a reclaimable category, never an owner marker, never where a
* required source is missing, where the category's location overlaps another
* one's, or where another installation's owner marker lies (or this one's
* cannot be written). It does nothing while a restore or a backup runs or
* another storage job holds the slot, and without an install id. A refused
* category's rows wait, up to QUEUE_MAX_AGE_MS. Releases run whatever the
* nightly deletion switch says: they are what a delete always did. Queueing
* never fails the caller's request.
*/
@Injectable()
export class StorageReleaseService {
@@ -41,75 +108,186 @@ export class StorageReleaseService {
private readonly registry: StorageRegistryService,
private readonly references: StorageReclaimRegistryService,
private readonly jobs: StorageJobsService,
private readonly install: StorageInstallService,
private readonly guard: StorageReclaimGuardService,
private readonly uow: UnitOfWork,
@InjectRepository(StorageReleaseQueue) private readonly queue: StorageReleaseQueueRepository,
) {}
/**
* The objects these stored values name (any spelling a column holds), each
* deleted when no row names it any more. Answers the ones that went.
* Queues the objects these stored values name (any spelling a column holds)
* for a release once they are due; answers how many were queued. Never
* throws: what it cannot queue is the nightly pass's.
*
* @txStandalone a queue row stands on its own whatever the caller wrote:
* the job asks every source again before anything goes.
*/
async releaseValues(
values: ReadonlyArray<string | null | undefined>,
options: StorageKeyOptions = {},
): Promise<StorageKeyRef[]> {
const byCategory = new Map<ServedCategory, Set<string>>();
for (const value of values) {
for (const ref of storageKeysIn(value, options)) {
if (!SERVED.has(ref.category)) continue;
const category = ref.category as ServedCategory;
const keys = byCategory.get(category) ?? new Set<string>();
keys.add(ref.key);
byCategory.set(category, keys);
}
}
const released = await Promise.all(
[...byCategory].map(async ([category, keys]) =>
(await this.releaseIfUnreferenced(category, [...keys])).map((key) => ({ category, key })),
),
);
return released.flat();
}
/** These keys of one category, each deleted when it exists and no row names it. Answers the ones that went. */
async releaseIfUnreferenced(category: ServedCategory, keys: readonly string[]): Promise<string[]> {
): Promise<number> {
try {
const candidates = [...new Set(keys)].filter(
(key) => inReclaimScope(key) && isValidKey(key) && storageOwnerMarkerInstall(key) === null,
);
if (candidates.length === 0) return [];
const sources = this.sourcesFor(category);
if (sources === null) return [];
const referenced = await this.referencedIn(category, candidates, sources);
const unnamed = candidates.filter(
(key) => !referenced.has(key.toLowerCase()) && !this.storage.keepsOnDelete(category, key),
);
const released = await Promise.all(unnamed.map((key) => this.deleteIfPresent(category, key)));
return unnamed.filter((_key, i) => released[i]);
const rows = releasable(values, options);
if (rows.length === 0) return 0;
if ((await this.queue.size()) + rows.length > QUEUE_LIMIT) {
logWarn(`[storage] the release queue is full, ${rows.length} object(s) are left to the nightly pass`);
return 0;
}
const dueAt = dbNow(new Date(Date.now() + RELEASE_DELAY_MS));
await this.uow.transactional(async () => {
await chunk(rows, ENQUEUE_BATCH).reduce<Promise<void>>(async (previous, page) => {
await previous;
await this.queue.enqueue(page, dueAt);
}, Promise.resolve());
});
return rows.length;
} catch (err) {
logWarn(`[storage] could not release '${category}' objects, the nightly pass will: ${messageOf(err)}`);
return [];
logWarn(`[storage] could not queue released objects, the nightly pass will find them: ${messageOf(err)}`);
return 0;
}
}
/** The sources to ask, or null when the category may not be released from now. */
private sourcesFor(category: ServedCategory): readonly StorageReferenceSource[] | null {
const rule = RECLAIM_LEDGER[category];
if (!rule.reclaim || this.jobs.restoreRunning) return null;
const sources = this.references.sources().filter((source) => source.claims.some((c) => c.category === category));
const whole = new Set(
sources
.filter((source) => source.claims.some((c) => c.category === category && c.prefix === ''))
.map((source) => source.id),
);
if (rule.requiredSources.some((id) => !whole.has(id))) return null;
if (sharedLocationsOf(this.registry).has(category)) return null;
return sources;
/**
* One tick of the release job: the due rows of every category the guards
* let through, at most TICK_LIMIT, each object deleted when no source names
* it. Holds the one-job slot while it runs, so a restore waits for it and a
* backfill or a migration never runs beside it.
*
* @txIndependent every delete and every dropped row stands on its own: a
* tick cut short leaves the rest queued.
*/
async processDue(now: Date = new Date()): Promise<ReleaseTickResult> {
const result: ReleaseTickResult = { skipped: null, deleted: 0, kept: 0 };
const installId = this.install.installId();
if (installId === null) return { ...result, skipped: 'no_install_id' };
if (this.jobs.backupRunning || !this.jobs.claimExclusive(STORAGE_RELEASE_JOB))
return { ...result, skipped: 'busy' };
try {
await this.drain(installId, now.getTime(), this.install.epoch, result);
} finally {
this.jobs.releaseExclusive(STORAGE_RELEASE_JOB);
}
return result;
}
private async referencedIn(
/** @txIndependent see processDue. */
private async drain(installId: string, now: number, epoch: number, result: ReleaseTickResult): Promise<void> {
await this.queue.deleteDueBefore(dbNow(new Date(now - QUEUE_MAX_AGE_MS)));
const due = new Set(await this.queue.dueCategories(dbNow(new Date(now))));
if (due.size === 0) return;
const allowed = await this.allowedCategories(due, installId, now);
const rows = await this.queue.dueIn([...allowed.keys()], dbNow(new Date(now)), TICK_LIMIT);
const byCategory = new Map<ServedCategory, string[]>();
for (const row of rows) {
const category = row.category as ServedCategory;
byCategory.set(category, [...(byCategory.get(category) ?? []), row.key]);
}
try {
await [...byCategory].reduce<Promise<void>>(async (previous, [category, keys]) => {
await previous;
await this.releaseCategory(category, keys, allowed.get(category) ?? [], epoch, result);
}, Promise.resolve());
} catch (err) {
if (!(err instanceof ReleaseStopped)) throw err;
logDebug(`[storage] a release tick stopped: ${err.message}`);
}
}
/** Of the categories with rows due, the ones the guards let a release judge now, each with the sources to ask. */
private async allowedCategories(
due: ReadonlySet<string>,
installId: string,
now: number,
): Promise<Map<ServedCategory, StorageReferenceSource[]>> {
const sources = this.references.sources();
const shared = sharedLocationsOf(this.registry);
const allowed = new Map<ServedCategory, StorageReferenceSource[]>();
const asked = reclaimableCategories().filter((category) => due.has(category));
await asked.reduce<Promise<void>>(async (previous, category) => {
await previous;
const judged = this.guard.judgeable(category, sources, shared);
if ('refused' in judged) return;
if ((await this.guard.cachedOwnerVerdict(category, installId, now)) !== null) return;
allowed.set(category, judged.sources);
}, Promise.resolve());
return allowed;
}
/**
* The due keys of one category, ASK_BATCH at a time: asked about, then
* deleted when unnamed. A failed question or delete stops the category for
* this tick and leaves its rows queued.
*
* @txIndependent every delete stands alone, its row goes after it.
*/
private async releaseCategory(
category: ServedCategory,
keys: readonly string[],
sources: readonly StorageReferenceSource[],
epoch: number,
result: ReleaseTickResult,
): Promise<void> {
let failed = false;
await chunk(keys, ASK_BATCH).reduce<Promise<void>>(async (previous, batch) => {
await previous;
if (failed) return;
this.assertUndisturbed(epoch);
failed = await this.releaseBatch(category, batch, sources, epoch, result);
}, Promise.resolve());
}
/** True when the batch failed part way, so the category stops for this tick. */
private async releaseBatch(
category: ServedCategory,
batch: readonly string[],
sources: readonly StorageReferenceSource[],
epoch: number,
result: ReleaseTickResult,
): Promise<boolean> {
let referenced: Set<string>;
try {
referenced = await this.referencedIn(category, batch, sources);
} catch (err) {
logWarn(`[storage] could not ask about released '${category}' objects, they wait: ${messageOf(err)}`);
return true;
}
const kept = batch.filter((key) => referenced.has(key.toLowerCase()) || this.storage.keepsOnDelete(category, key));
const unnamed = batch.filter((key) => !kept.includes(key));
this.assertUndisturbed(epoch);
const deleted: string[] = [];
let failure: unknown = null;
await chunk(unnamed, DELETE_CONCURRENCY).reduce<Promise<void>>(async (previous, group) => {
await previous;
if (failure !== null) return;
const results = await Promise.allSettled(group.map((key) => this.storage.delete(category, key)));
results.forEach((settled, i) => {
if (settled.status === 'fulfilled') deleted.push(group[i] as string);
else failure ??= settled.reason;
});
}, Promise.resolve());
await this.queue.deleteKeys(category, [...kept, ...deleted]);
result.kept += kept.length;
result.deleted += deleted.length;
if (deleted.length > 0) logDebug(`[storage] released ${deleted.length} unreferenced '${category}' object(s)`);
if (failure === null) return false;
logWarn(`[storage] could not release a '${category}' object, it waits: ${messageOf(failure)}`);
return true;
}
/**
* The keys some source names right now, lowercased. A key no source can
* narrow its read to (no plain run of 8 characters, which every name TREK
* writes has) counts as named without a question: asking about it would read
* whole tables, so it is the nightly pass's.
*/
private async referencedIn(
category: ServedCategory,
batch: readonly string[],
sources: readonly StorageReferenceSource[],
): Promise<Set<string>> {
const referenced = new Set(batch.filter((key) => keySearchCore(key) === null).map((key) => key.toLowerCase()));
const keys = batch.filter((key) => keySearchCore(key) !== null);
if (keys.length === 0) return referenced;
const answers = await Promise.all(
sources.map(async (source) => {
const asked = keys.filter((key) =>
@@ -118,22 +296,50 @@ export class StorageReleaseService {
return asked.length === 0 ? new Set<string>() : await source.referenced(category, asked);
}),
);
const referenced = new Set<string>();
for (const answer of answers) for (const key of answer) referenced.add(key.toLowerCase());
return referenced;
}
private async deleteIfPresent(category: ServedCategory, key: string): Promise<boolean> {
try {
if ((await this.storage.stat(category, key)) === null) return false;
await this.storage.delete(category, key);
logDebug(`[storage] released an unreferenced '${category}' object`);
return true;
} catch (err) {
logWarn(`[storage] could not release a '${category}' object, the nightly pass will: ${messageOf(err)}`);
return false;
/** A restore or a backup that began, or a reopen that happened, since the tick started ends it. */
private assertUndisturbed(epoch: number): void {
if (this.jobs.restoreRunning) throw new ReleaseStopped('a restore began');
if (this.jobs.backupRunning) throw new ReleaseStopped('a backup began');
if (this.install.epoch !== epoch) throw new ReleaseStopped('the database was reopened');
}
}
/**
* The rows these values queue: root-level keys of reclaimable categories,
* never an owner marker, at most MAX_KEYS_PER_VALUE per value and
* MAX_KEYS_PER_CALL in all.
*/
function releasable(
values: ReadonlyArray<string | null | undefined>,
options: StorageKeyOptions,
): Array<Pick<ReleaseQueueRow, 'category' | 'key'>> {
const seen = new Set<string>();
const rows: Array<Pick<ReleaseQueueRow, 'category' | 'key'>> = [];
for (const value of values) {
const refs = storageKeysIn(value, options).filter(
(ref) =>
SERVED.has(ref.category) &&
RECLAIM_LEDGER[ref.category as ServedCategory].reclaim &&
inReclaimScope(ref.key) &&
isValidKey(ref.key) &&
storageOwnerMarkerInstall(ref.key) === null,
);
for (const ref of refs.slice(0, MAX_KEYS_PER_VALUE)) {
const id = `${ref.category}\n${ref.key}`;
if (seen.has(id) || rows.length >= MAX_KEYS_PER_CALL) continue;
seen.add(id);
rows.push({ category: ref.category, key: ref.key });
}
}
return rows;
}
function chunk<T>(items: readonly T[], size: number): T[][] {
return Array.from({ length: Math.ceil(items.length / size) }, (_page, i) => items.slice(i * size, (i + 1) * size));
}
function messageOf(err: unknown): string {
+12 -2
View File
@@ -1,5 +1,6 @@
import { AppSettings } from '../../db/entities/AppSettings.entity';
import { StorageReclaimMarks } from '../../db/entities/StorageReclaimMarks.entity';
import { StorageReleaseQueue } from '../../db/entities/StorageReleaseQueue.entity';
import { AppConfigModule } from '../app-config/app-config.module';
import { AuditModule } from '../audit/audit.module';
import { DatabaseLifecycleModule } from '../database/database-lifecycle.module';
@@ -7,12 +8,15 @@ import { SchedulingModule } from '../scheduling/scheduling.module';
import { StorageAdminController } from './storage-admin.controller';
import { StorageAdminService } from './storage-admin.service';
import { StorageEventsService } from './storage-events.service';
import { StorageInstallService } from './storage-install.service';
import { StorageJobsService } from './storage-jobs.service';
import { StorageReclaimGuardService } from './storage-reclaim-guard.service';
import { StorageReclaimRegistryService } from './storage-reclaim-registry.service';
import { StorageReclaimController } from './storage-reclaim.controller';
import { StorageReclaimJob } from './storage-reclaim.job';
import { StorageReclaimService } from './storage-reclaim.service';
import { StorageRegistryService } from './storage-registry.service';
import { StorageReleaseJob } from './storage-release.job';
import { StorageReleaseService } from './storage-release.service';
import { StorageStatsService } from './storage-stats.service';
import { StorageUsageScanJob } from './storage-usage-scan.job';
@@ -42,7 +46,10 @@ import { Module } from '@nestjs/common';
* running there and waits for a reclaim pass in flight to let go, a backup
* marks itself so the pass deletes nothing it still reads.
* StorageReleaseService is exported for the owners, which hand it the values a
* delete or a replace left unnamed right after their write.
* delete or a replace left unnamed right after their write; it queues them,
* and StorageReleaseJob deletes what is still unnamed once every undo window
* has passed. StorageInstallService keeps this installation's id in the data
* dir and resets the reclaim state of a database that came from elsewhere.
*/
@Module({
imports: [
@@ -50,7 +57,7 @@ import { Module } from '@nestjs/common';
AuditModule,
DatabaseLifecycleModule,
SchedulingModule,
MikroOrmModule.forFeature([AppSettings, StorageReclaimMarks]),
MikroOrmModule.forFeature([AppSettings, StorageReclaimMarks, StorageReleaseQueue]),
],
controllers: [StorageAdminController, StorageReclaimController],
providers: [
@@ -62,9 +69,12 @@ import { Module } from '@nestjs/common';
StorageStatsService,
StorageUsageScanJob,
StorageReclaimRegistryService,
StorageInstallService,
StorageReclaimGuardService,
StorageReclaimService,
StorageReclaimJob,
StorageReleaseService,
StorageReleaseJob,
],
exports: [
StorageService,
+1 -1
View File
@@ -537,7 +537,7 @@ export class TripsService {
}
});
if (vacayId !== undefined) await this.vacay.notifyPlanUsers(vacayId, undefined);
if (cover_image !== undefined) await this.release.releaseValues([trip.cover_image]); // kept while any row names it
if (cover_image !== undefined && cover_image !== trip.cover_image) await this.release.releaseValues([trip.cover_image]);
const changes: Record<string, unknown> = {};
if (title && title !== trip.title) changes.title = title;