mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-28 23:50:08 +00:00
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:
@@ -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),
|
||||
},
|
||||
});
|
||||
@@ -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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
+24
@@ -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({});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user