fix(audit-5): export disk preflight, media reclaim, low-disk warning, restore docs
Fifth round, all storage. The keepsake could not fit on the documented hardware
and failed halfway through a multi-GB write, leaving the deliverable stuck; the
quota had stopped bounding the disk; and the backup had no restore procedure.
Squashed from 6 commits, original messages preserved below.
──────── fix(export): refuse an export that cannot fit, and stop peaking at two generations
Nothing in export.rs ever asked whether the keepsake would fit. Both archives write
their media `Compression::Stored`, so each is essentially a byte-for-byte second copy
of the originals -- Gallery.zip always, and Memories.zip for every video and every
image at or under 5 MB. On the documented CX33 (80 GB, all three volumes on one
filesystem) the upload quota's fixed point leaves ~40 GB free, and a release spawns
BOTH halves concurrently against it.
The failure is not "the export failed", it is "the deliverable is stuck":
1. ENOSPC lands partway through a multi-GB write.
2. The epoch has already moved, so the job row is `failed` at the CURRENT
generation and readiness (epoch = event.export_epoch AND status = 'done') is
false -- GET /export/zip 404s.
3. The last good archive sits on disk, unreferenced and unreachable.
4. POST /host/export/rebuild, the only escape, re-arms the same doomed write.
Three changes.
Reclaim before building. `prune_stale_export_files` ran only after the new archive
was written, renamed and finalised. That reads as durability but buys nothing: the
moment `invalidate_and_arm` bumps the epoch the old archive is ALREADY unreachable,
so keeping it reserves gigabytes for a download nobody can perform -- and for a
takedown it is content someone explicitly asked to have removed. Peak usage is now
one generation. Narrower than the post-finalize prune on purpose: final archives
only, never a `.tmp` or a `viewer_tmp_` dir, since a superseded worker can still be
streaming into those and at build START is far more likely to be alive.
Preflight the space. SUM(original_size_bytes) over exactly `query_uploads`'
visibility filter, +10% for ZIP overhead, multiplied by the number of armed jobs --
without that multiplier each of the two concurrent halves independently sees "it
fits" and together they don't. Runs AFTER claim_job, not before as reported: bailing
before the claim leaves the row `pending` with no worker and no error, the
spinner-forever state `mark_failed`'s status guard exists to prevent. Fails open when
the mount can't be read, exactly as the upload quota does.
Show the host the reason. /export/status returned {status, progress_pct} and nothing
else, so the host dashboard could only render "fehlgeschlagen" next to the retry
button. The message was written to the row and surfaced solely in the ADMIN job list
-- a different screen, possibly a different person. It now travels with the status,
and only on a failure, so a message left on a since-succeeded row can't appear beside
a green "ist bereit".
Tests: 10 unit (the u128 clamp caught a real bug in the first draft -- saturating_mul
then /100 turns an overflow into a number ~100x too small, the one direction that
authorises the write being guarded against; the carried-forward archive must survive
its own older epoch in the filename), 4 DB-backed (the estimate is asserted against
the row set the archive actually contains, not against a restatement of the WHERE
clause, so the two queries cannot drift), 3 e2e over the four-hop plumbing.
──────── fix(maintenance): reclaim the media of deliberately deleted uploads
The quota stopped bounding the disk. `soft_delete_in_event` stamps `deleted_at` and
refunds `total_upload_bytes`, but nothing ever removed the bytes, and the hourly
sweep reached only `compression_status = 'failed'`. Upload 500 MB, delete, quota back
to zero, upload another 500 MB. Not an attack -- a guest curating their camera roll,
which is what people do. The host then sees guests hitting "Du hast dein Upload-Limit
erreicht" while the admin widget shows a disk full of files no upload row points at,
and the quota message is actively misleading because the space really is gone, just
not to anyone the accounting can name.
Two retention windows, because the two deletes mean different things. A compression
failure keeps its 14 days: the guest didn't ask for it and may not be able to retake
the photo. A deliberate removal gets 24 hours -- 14 days outlives the whole event, so
a deliberate delete would never reclaim anything while it mattered, and a day still
covers a mis-tap.
Wider than reported: ALL FOUR paths are reclaimed, not just the original. Preview,
display and thumbnail are each a separate file, none counted in
`original_size_bytes`, and nothing ever removed them either. That was invisible while
the sweep only saw failed compressions (which produce no derivatives) and becomes
three leaked files per upload the moment it reaches a successful one. A row is
re-selected until every path is cleared, and the columns are cleared only once every
file for that upload is gone -- clearing after a partial success would strand the
survivors in exactly the unowned state this drains.
`backfill_stale_derivatives` selects on `display_path IS NULL AND preview_path IS NOT
NULL`, which is close enough to the post-sweep state to be worth pinning: it is
guarded on `deleted_at IS NULL`, so it cannot re-decode an original that is no longer
on disk. Covered.
Residual, deliberately: within the 24h window the bytes are still spent and still
unaccounted, so delete-and-re-upload through an eight-hour event can outrun the
sweep. Bounding that means holding the quota until the file is reclaimed rather than
refunding at `deleted_at`. The low-disk warning is the net under it.
Tests: 6 DB-backed, replacing 3. The one asserting an owner-deleted upload IS
reclaimed is the exact inverse of what this file used to assert.
──────── feat(host): warn about low disk before it becomes unrecoverable
Storage visibility existed in exactly one place: a passive Speicherauslastung widget
on the ADMIN dashboard. A host who isn't the admin had no view of it, and nothing
warned anyone. README carried "Low-disk alert (< 10 GB free)" under Planned since v1.
Two things make this a safety net rather than a nice-to-have. postgres_data,
media_data and exports_data are all Docker named volumes on ONE filesystem, so
running out doesn't degrade a subsystem -- Postgres stops being able to write and the
whole event goes down. And the keepsake needs room for two gallery-sized archives,
which the export preflight can only ever refuse AFTER the release, when the event is
over and every remedy is harder.
So the threshold is not a fixed number alone. It fires on the 10 GB floor the README
always named, OR on "you could not build the keepsake right now" -- the trigger a
host can still act on, computed with the same arithmetic the preflight uses. Unknown
free space is NOT low: it fails open like the upload quota and the preflight do,
because a banner that cries wolf on an unreadable mount is a banner nobody reads.
Carried on GET /host/event, which the dashboard already fetches on load and on every
reload -- no new endpoint, no new poll. Rendered above everything else including the
PIN-reset queue, and it names the consequence (the event, not just the download)
rather than only the number.
Also fixes the host page's formatBytes, which topped out at MB: 30 GB free would have
rendered as "30720.0 MB", and a guest with 2 GB of uploads was already being shown
that way in the user list.
Tests: 5 unit on the threshold (including that plenty of free space is still low when
the keepsake wouldn't fit -- the case a fixed threshold misses entirely), 3 e2e.
The e2e drives it through `original_size_bytes` rather than a genuinely full disk:
the estimate is pure SQL over that column, so overstating one row moves the
accounting without touching a byte on disk.
──────── docs: add a restore procedure, fix the backup cadence, and correct quota_tolerance
Four things, all found by the same question: what does an operator standing at the
venue actually need?
A RESTORE PROCEDURE. There was none anywhere, and a backup you have never restored
isn't a backup. Two hazards worth writing down: media must be extracted preserving
ownership (the app runs as uid 100 / gid 101, and a root-owned restore makes every
upload fail with EACCES surfacing as a generic 500), and the app must be STOPPED
first, because migrations run on boot and a live pool will fight the restore.
Both the backup and the restore commands were run against the real stack before being
written down, which caught two that would have failed:
- The plain `pg_dump` did not restore: `psql` aborted on `ERROR: schema
"_sqlx_test" already exists`. pg_dump emits no DROPs without --clean --if-exists,
so the documented dump could only ever be restored into an empty database. Fixed
at the source (the dump is now self-cleaning) and verified end to end: 16 tables
back, exit 0.
- `--same-owner` does not exist in BusyBox tar, which is what `alpine` ships, so
the extract aborted before unpacking anything. `--numeric-owner` plus the
explicit chown, verified to land 100:101.
BACKUP CADENCE. "Weekly offsite" is the wrong shape when every irreplaceable byte is
created in one eight-hour window and nobody can retake a wedding. The backup that
matters runs that night, and again after the release so the keepsake is captured.
Also: take the DB dump and the media tarball back to back, or you get rows pointing
at files the dump doesn't know about.
quota_tolerance WAS DOCUMENTED AS SOMETHING IT ISN'T. .env.example called it "fraction
of disk that triggers the low-storage warning". It is the multiplier in
`floor(free_disk * tolerance / active_uploaders)` -- so an operator who wants "warn me
later" and sets 0.95 is actually authorising guests to fill 95% of the disk, moving
the fixed point from 43% to ~49% and eating the export headroom. The admin UI labelled
it "Toleranz (0-1)" with no explanation at all, which invites exactly that reading;
it is now "Speicher-Anteil für Gäste" with the formula in the hint. Wrong docs on a
tuning knob are worse than no docs.
SIZING. New section with the arithmetic: three volumes on one filesystem, the quota
fixed point at tolerance/(1+tolerance), and the fact the 80 GB baseline does not cover
the keepsake -- both archives are built concurrently and each is roughly a second copy
of every original. Provision ~3x expected media, or give exports its own volume.
Also ticks the low-disk alert off the roadmap, since it now exists.
──────── chore: raise the db memory limit and rate-limit social writes
Two smaller operational items.
POSTGRES 512M -> 1G. DATABASE_MAX_CONNECTIONS is 30 for a ~100-guest event (feed
polling + SSE + uploads at once), and 30 backends plus Postgres 16's default
shared_buffers leaves very little headroom at 512M. An OOM here doesn't degrade one
feature -- every request path touches the database, so it takes the event down.
Memory is the cheaper knob than shrinking the pool back and reintroducing the
queueing it was raised to fix. .env.example now names the pairing explicitly, the way
it already does for COMPRESSION_WORKER_CONCURRENCY.
SOCIAL WRITES WERE UNTHROTTLED. toggle_like, add_comment and delete_comment were the
only mutating endpoints in the app with no limit at all -- upload, join, recover,
export and admin login all carry one. Asymmetric coverage rather than a deliberate
decision.
Low severity, and honestly so: a like fans an SSE broadcast to every client, but the
export regeneration a comment deletion triggers is contained (REGEN_DEBOUNCE 20s,
workers born with their epoch, superseded ones inert). So the ceiling is 120/min --
far above anything a real guest produces. This bounds a script, not an enthusiastic
double-tapper.
ONE bucket across all three actions: separate buckets would let a caller triple the
aggregate write rate by alternating between them. Keyed per USER, matching the feed
and upload limits -- at a venue every guest is behind one NAT, and an IP key is what
made the /join and /feed limits turn guests away in the first place.
Migration 020 seeds both keys, and both are wired into the admin allowlist, the
config UI and the e2e reseed -- the step two earlier per-area toggles missed, which
left switches that existed in code and could never be flipped.
Tests: 4 e2e, including that the shared bucket really is shared (the part most likely
to be lost in a refactor) and that one guest hitting the ceiling doesn't block
another behind the same IP.
──────── fix(e2e): stop the video poster assertion racing the ffmpeg thumbnail
Pre-existing, and it fired for real during the full-suite run on a cold stack.
The lightbox binds `poster={upload.thumbnail_url ?? undefined}`, so the attribute is
absent until compression produces the thumbnail. This test asserted on it immediately
after seeding, never waiting for the worker -- unlike the Range test further down the
same file, which does poll. Against a warm stack the worker usually wins; against a
freshly rebuilt one (`stack:down -v`, cold ffmpeg) it doesn't.
That is the worst possible time for a false failure: the first run after a rebuild is
exactly when you are trying to establish whether a change broke something. Poll for
`compression_status = 'done'` before the poster assertion. The `src` assertion needs
no wait and keeps none.
Verified with --repeat-each=3.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
@@ -1,19 +1,27 @@
|
||||
//! DB-backed tests for the failed-original sweep (`services/maintenance.rs`).
|
||||
//! DB-backed tests for the deleted-media sweep (`services/maintenance.rs`).
|
||||
//!
|
||||
//! Context: the compression worker deliberately no longer deletes an upload's original when
|
||||
//! its transcode fails — a transient ENOSPC or a codec panic must never destroy the only
|
||||
//! copy of a photo a guest cannot retake. But the row is soft-deleted and the uploader's
|
||||
//! quota IS refunded, so those bytes become invisible, unowned and free. A guest hitting a
|
||||
//! reproducible codec failure could accumulate orphans at no personal cost, and since
|
||||
//! `active_uploaders` counts only users with non-deleted uploads, dropping out of that count
|
||||
//! actually RAISES everyone's per-user ceiling while the disk fills.
|
||||
//! Context, in two halves.
|
||||
//!
|
||||
//! The sweep reclaims them after a retention window. Its selection predicate is the whole
|
||||
//! safety argument — it must reach the give-up path's leftovers and nothing else — so that
|
||||
//! is what these tests pin, following the same "reproduce the SQL verbatim" pattern as
|
||||
//! `upload_concurrency.rs`.
|
||||
//! The compression worker deliberately no longer deletes an upload's original when its transcode
|
||||
//! fails — a transient ENOSPC or a codec panic must never destroy the only copy of a photo a guest
|
||||
//! cannot retake. But the row is soft-deleted and the uploader's quota IS refunded, so those bytes
|
||||
//! become invisible, unowned and free.
|
||||
//!
|
||||
//! `#[sqlx::test]` gives each test a fresh database with the real migrations applied.
|
||||
//! The SAME hole was reachable by the ordinary path, and that one is not an edge case at all:
|
||||
//! `soft_delete_in_event` refunds `total_upload_bytes` on every guest or host delete and nothing
|
||||
//! removed the files, so the quota stopped bounding the disk. Upload 500 MB, delete, quota back to
|
||||
//! zero, upload another 500 MB — a guest curating their camera roll, which is what people do. The
|
||||
//! sweep used to reach only `compression_status = 'failed'`, so it never touched this case; the
|
||||
//! test below that now asserts an owner-deleted upload IS reclaimed is the one that used to assert
|
||||
//! the opposite.
|
||||
//!
|
||||
//! Two windows, because the two deletes mean different things: 14 days for a failure an operator
|
||||
//! may want to investigate, 24 hours for a removal someone asked for (14 days outlives the whole
|
||||
//! event, so a deliberate delete would never reclaim anything while it mattered).
|
||||
//!
|
||||
//! The selection predicate is the whole safety argument — it must reach both leftovers and never a
|
||||
//! live upload — so that is what these pin, following the same "reproduce the SQL verbatim" pattern
|
||||
//! as `upload_concurrency.rs`. `#[sqlx::test]` gives each test a fresh, migrated database.
|
||||
|
||||
mod common;
|
||||
|
||||
@@ -21,195 +29,324 @@ use common::*;
|
||||
use sqlx::PgPool;
|
||||
use uuid::Uuid;
|
||||
|
||||
/// SRC: `services/maintenance.rs::cleanup_failed_originals` — the selection, verbatim.
|
||||
async fn sweep_selects(pool: &PgPool, retention_days: i64) -> Vec<Uuid> {
|
||||
sqlx::query_as::<_, (Uuid, String)>(
|
||||
"SELECT id, original_path FROM upload
|
||||
WHERE compression_status = 'failed'
|
||||
AND deleted_at IS NOT NULL
|
||||
AND deleted_at < NOW() - ($1 || ' days')::interval
|
||||
AND original_path <> ''",
|
||||
const FAILED_DAYS: i64 = 14;
|
||||
const DELETED_HOURS: i64 = 24;
|
||||
|
||||
/// SRC: `services/maintenance.rs::cleanup_deleted_media` — the selection, verbatim.
|
||||
async fn sweep_selects(pool: &PgPool, failed_days: i64, deleted_hours: i64) -> Vec<Uuid> {
|
||||
type Row = (Uuid, String, Option<String>, Option<String>, Option<String>);
|
||||
sqlx::query_as::<_, Row>(
|
||||
"SELECT id, original_path, preview_path, display_path, thumbnail_path FROM upload
|
||||
WHERE deleted_at IS NOT NULL
|
||||
AND CASE WHEN compression_status = 'failed'
|
||||
THEN deleted_at < NOW() - ($1 || ' days')::interval
|
||||
ELSE deleted_at < NOW() - ($2 || ' hours')::interval
|
||||
END
|
||||
AND (original_path <> '' OR preview_path IS NOT NULL
|
||||
OR display_path IS NOT NULL OR thumbnail_path IS NOT NULL)",
|
||||
)
|
||||
.bind(retention_days.to_string())
|
||||
.bind(failed_days.to_string())
|
||||
.bind(deleted_hours.to_string())
|
||||
.fetch_all(pool)
|
||||
.await
|
||||
.expect("sweep query")
|
||||
.into_iter()
|
||||
.map(|(id, _)| id)
|
||||
.map(|(id, ..)| id)
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
async fn seed_upload(
|
||||
/// Seed an upload aged `deleted_hours_ago` (None = live), with optional derivative paths.
|
||||
async fn seed_aged_upload(
|
||||
pool: &PgPool,
|
||||
event_id: Uuid,
|
||||
user_id: Uuid,
|
||||
status: &str,
|
||||
deleted_days_ago: Option<i64>,
|
||||
deleted_hours_ago: Option<i64>,
|
||||
original_path: &str,
|
||||
derivatives: bool,
|
||||
) -> Uuid {
|
||||
let id: Uuid = sqlx::query_scalar(
|
||||
sqlx::query_scalar(
|
||||
"INSERT INTO upload (event_id, user_id, original_path, mime_type, original_size_bytes,
|
||||
compression_status, deleted_at)
|
||||
compression_status, deleted_at,
|
||||
preview_path, display_path, thumbnail_path)
|
||||
VALUES ($1, $2, $3, 'image/jpeg', 1000, $4,
|
||||
CASE WHEN $5::bigint IS NULL THEN NULL
|
||||
ELSE NOW() - ($5::text || ' days')::interval END)
|
||||
ELSE NOW() - ($5::text || ' hours')::interval END,
|
||||
CASE WHEN $6 THEN 'previews/p.jpg' END,
|
||||
CASE WHEN $6 THEN 'displays/d.jpg' END,
|
||||
CASE WHEN $6 THEN 'thumbs/t.jpg' END)
|
||||
RETURNING id",
|
||||
)
|
||||
.bind(event_id)
|
||||
.bind(user_id)
|
||||
.bind(original_path)
|
||||
.bind(status)
|
||||
.bind(deleted_days_ago)
|
||||
.bind(deleted_hours_ago)
|
||||
.bind(derivatives)
|
||||
.fetch_one(pool)
|
||||
.await
|
||||
.expect("seed upload");
|
||||
id
|
||||
.expect("seed upload")
|
||||
}
|
||||
|
||||
/// A live upload is untouchable no matter how the windows are configured.
|
||||
///
|
||||
/// PREVENTS: the catastrophic loosening. Everything else here is about reclaiming more; this is the
|
||||
/// one assertion that must never bend.
|
||||
#[sqlx::test]
|
||||
async fn sweeps_only_long_failed_soft_deleted_uploads(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-event").await;
|
||||
async fn a_live_upload_is_never_selected(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-live").await;
|
||||
let user_id = seed_user(&pool, event_id, "Sweeper").await;
|
||||
|
||||
// The one and only thing the sweep may touch: the exact state the compression worker's
|
||||
// give-up path leaves behind, aged past the window.
|
||||
let target = seed_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
Some(30),
|
||||
"originals/e/target.jpg",
|
||||
)
|
||||
.await;
|
||||
|
||||
// Everything below is a near-miss that must survive.
|
||||
// A healthy live upload — the catastrophic case if the predicate were ever loosened.
|
||||
let live = seed_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"done",
|
||||
None,
|
||||
"originals/e/live.jpg",
|
||||
)
|
||||
.await;
|
||||
// Failed but still inside the retention window: the recovery window is the entire point
|
||||
// of keeping the file, so reclaiming it early would defeat the fix it protects.
|
||||
let recent = seed_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
Some(1),
|
||||
"originals/e/recent.jpg",
|
||||
)
|
||||
.await;
|
||||
// Failed but NOT soft-deleted — not the give-up path; something else set this status.
|
||||
let failed_live = seed_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
None,
|
||||
"originals/e/failed-live.jpg",
|
||||
)
|
||||
.await;
|
||||
// Soft-deleted by the OWNER, compression fine. Its file is already gone; this row must
|
||||
// never be re-processed.
|
||||
let owner_deleted = seed_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"done",
|
||||
Some(30),
|
||||
"originals/e/owner.jpg",
|
||||
)
|
||||
.await;
|
||||
// Already swept: `original_path` cleared. Re-selecting it every hour would log a
|
||||
// phantom reclaim forever.
|
||||
let already_swept = seed_upload(&pool, event_id, user_id, "failed", Some(30), "").await;
|
||||
|
||||
let selected = sweep_selects(&pool, 14).await;
|
||||
|
||||
assert_eq!(
|
||||
selected,
|
||||
vec![target],
|
||||
"the sweep must select exactly the aged give-up-path leftovers"
|
||||
);
|
||||
for (id, what) in [
|
||||
(live, "a live upload"),
|
||||
(recent, "a failure still inside the retention window"),
|
||||
(failed_live, "a failed but not soft-deleted upload"),
|
||||
(owner_deleted, "an owner-deleted upload"),
|
||||
(already_swept, "an already-swept row"),
|
||||
] {
|
||||
assert!(!selected.contains(&id), "the sweep must not touch {what}");
|
||||
for status in ["done", "failed", "processing", "pending"] {
|
||||
let live = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
status,
|
||||
None,
|
||||
"originals/e/live.jpg",
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
assert!(
|
||||
!sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS)
|
||||
.await
|
||||
.contains(&live),
|
||||
"a non-deleted upload with status {status} must never be swept"
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// THE FIX. An upload a guest or host deliberately deleted is reclaimed once past 24 hours.
|
||||
///
|
||||
/// PREVENTS: the regression back to a sweep scoped to `compression_status = 'failed'`, which is
|
||||
/// what let the quota stop bounding the disk. This assertion is the inverse of the one this file
|
||||
/// used to make.
|
||||
#[sqlx::test]
|
||||
async fn retention_window_is_honoured_at_the_boundary(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-boundary").await;
|
||||
let user_id = seed_user(&pool, event_id, "Boundary").await;
|
||||
async fn a_deliberately_deleted_upload_is_reclaimed_after_a_day(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-deleted").await;
|
||||
let user_id = seed_user(&pool, event_id, "Curator").await;
|
||||
|
||||
let inside = seed_upload(
|
||||
let deleted = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
Some(13),
|
||||
"originals/e/inside.jpg",
|
||||
"done",
|
||||
Some(48),
|
||||
"originals/e/owner.jpg",
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
let outside = seed_upload(
|
||||
// Still inside the window — a mis-tap is recoverable for a day.
|
||||
let recent = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
Some(15),
|
||||
"originals/e/outside.jpg",
|
||||
"done",
|
||||
Some(2),
|
||||
"originals/e/recent.jpg",
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
|
||||
let selected = sweep_selects(&pool, 14).await;
|
||||
let selected = sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS).await;
|
||||
assert!(
|
||||
selected.contains(&outside),
|
||||
"15 days old must be past a 14-day window"
|
||||
selected.contains(&deleted),
|
||||
"a deliberate delete past the window must be reclaimed — this is the leak"
|
||||
);
|
||||
assert!(
|
||||
!selected.contains(&inside),
|
||||
"13 days old must still be retained"
|
||||
!selected.contains(&recent),
|
||||
"a delete inside the window keeps its recovery grace"
|
||||
);
|
||||
}
|
||||
|
||||
/// The two windows are independent: a failure is retained far longer than a deliberate delete.
|
||||
///
|
||||
/// PREVENTS: collapsing them into one. Applying 24h to failures would destroy the recovery window
|
||||
/// the retained-original fix exists to provide; applying 14 days to deliberate deletes would mean
|
||||
/// nothing is ever reclaimed during an event.
|
||||
#[sqlx::test]
|
||||
async fn clearing_original_path_makes_the_sweep_idempotent(pool: PgPool) {
|
||||
// The sweep clears `original_path` after reclaiming the file. Without that, a row whose
|
||||
// file is already gone is re-selected on every hourly tick forever.
|
||||
let event_id = seed_event(&pool, "sweep-idempotent").await;
|
||||
let user_id = seed_user(&pool, event_id, "Idem").await;
|
||||
let id = seed_upload(
|
||||
async fn the_two_retention_windows_do_not_bleed_into_each_other(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-windows").await;
|
||||
let user_id = seed_user(&pool, event_id, "Windows").await;
|
||||
|
||||
// 48h old: past the deliberate window, nowhere near the failure window.
|
||||
let failed_recent = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
Some(30),
|
||||
"originals/e/once.jpg",
|
||||
Some(48),
|
||||
"originals/e/f-recent.jpg",
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
let deleted_same_age = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"done",
|
||||
Some(48),
|
||||
"originals/e/d-same.jpg",
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
// 30 days old: past both.
|
||||
let failed_old = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"failed",
|
||||
Some(30 * 24),
|
||||
"originals/e/f-old.jpg",
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(sweep_selects(&pool, 14).await, vec![id]);
|
||||
let selected = sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS).await;
|
||||
assert!(
|
||||
!selected.contains(&failed_recent),
|
||||
"a 2-day-old compression failure is still inside its 14-day recovery window"
|
||||
);
|
||||
assert!(
|
||||
selected.contains(&deleted_same_age),
|
||||
"a deliberate delete of the same age is past its 24-hour window"
|
||||
);
|
||||
assert!(
|
||||
selected.contains(&failed_old),
|
||||
"a 30-day-old failure is past both windows"
|
||||
);
|
||||
}
|
||||
|
||||
/// Boundary behaviour on both windows.
|
||||
#[sqlx::test]
|
||||
async fn retention_windows_are_honoured_at_the_boundary(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-boundary").await;
|
||||
let user_id = seed_user(&pool, event_id, "Boundary").await;
|
||||
|
||||
let cases = [
|
||||
("failed", 13 * 24, false, "13 days"),
|
||||
("failed", 15 * 24, true, "15 days"),
|
||||
("done", 23, false, "23 hours"),
|
||||
("done", 25, true, "25 hours"),
|
||||
];
|
||||
for (status, hours, expected, label) in cases {
|
||||
let id = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
status,
|
||||
Some(hours),
|
||||
"originals/e/b.jpg",
|
||||
false,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(
|
||||
sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS)
|
||||
.await
|
||||
.contains(&id),
|
||||
expected,
|
||||
"a {status} upload deleted {label} ago: expected swept={expected}"
|
||||
);
|
||||
sqlx::query("DELETE FROM upload WHERE id = $1")
|
||||
.bind(id)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("clean up");
|
||||
}
|
||||
}
|
||||
|
||||
/// A row is re-selected until EVERY one of its paths is cleared.
|
||||
///
|
||||
/// PREVENTS: two failures at once. The sweep used to clear `original_path` alone, which was right
|
||||
/// for its only case (a failed compression produces no derivatives) but leaves preview, display and
|
||||
/// thumbnail on disk the moment it reaches a successfully processed upload — three files per
|
||||
/// upload, none of them counted in `original_size_bytes`, that nothing else ever removes. And a row
|
||||
/// whose paths are all cleared must stop coming back, or every hourly tick logs a phantom reclaim
|
||||
/// forever.
|
||||
#[sqlx::test]
|
||||
async fn a_row_is_reselected_until_every_path_is_cleared(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-idempotent").await;
|
||||
let user_id = seed_user(&pool, event_id, "Idem").await;
|
||||
let id = seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"done",
|
||||
Some(48),
|
||||
"originals/e/once.jpg",
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
|
||||
assert_eq!(sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS).await, [id]);
|
||||
|
||||
// Clearing only the original is NOT enough — the derivatives are still on disk.
|
||||
sqlx::query("UPDATE upload SET original_path = '' WHERE id = $1")
|
||||
.bind(id)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("clear path");
|
||||
.expect("clear original");
|
||||
assert_eq!(
|
||||
sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS).await,
|
||||
[id],
|
||||
"derivatives left behind must keep the row selected"
|
||||
);
|
||||
|
||||
sqlx::query(
|
||||
"UPDATE upload SET preview_path = NULL, display_path = NULL, thumbnail_path = NULL
|
||||
WHERE id = $1",
|
||||
)
|
||||
.bind(id)
|
||||
.execute(&pool)
|
||||
.await
|
||||
.expect("clear derivatives");
|
||||
assert!(
|
||||
sweep_selects(&pool, 14).await.is_empty(),
|
||||
"a swept row must not come back"
|
||||
sweep_selects(&pool, FAILED_DAYS, DELETED_HOURS)
|
||||
.await
|
||||
.is_empty(),
|
||||
"a fully swept row must not come back"
|
||||
);
|
||||
}
|
||||
|
||||
/// The derivative backfill must never resurrect what the sweep just reclaimed.
|
||||
///
|
||||
/// PREVENTS: an interaction, not a bug in either piece. The sweep nulls `preview_path`, and
|
||||
/// `backfill_stale_derivatives` selects on `display_path IS NULL AND preview_path IS NOT NULL` —
|
||||
/// close enough that a future edit to either could have the backfill re-decode an original that is
|
||||
/// no longer on disk, on every boot. `deleted_at IS NULL` is what keeps them apart.
|
||||
#[sqlx::test]
|
||||
async fn the_backfill_ignores_swept_rows(pool: PgPool) {
|
||||
let event_id = seed_event(&pool, "sweep-backfill").await;
|
||||
let user_id = seed_user(&pool, event_id, "Backfill").await;
|
||||
seed_aged_upload(
|
||||
&pool,
|
||||
event_id,
|
||||
user_id,
|
||||
"done",
|
||||
Some(48),
|
||||
"originals/e/gone.jpg",
|
||||
true,
|
||||
)
|
||||
.await;
|
||||
|
||||
// SRC: `services/compression.rs::backfill_stale_derivatives` — the selection, verbatim.
|
||||
let backfilled: Vec<(Uuid, String, String)> = sqlx::query_as(
|
||||
"SELECT id, original_path, mime_type FROM upload
|
||||
WHERE deleted_at IS NULL AND mime_type LIKE 'image/%'
|
||||
AND original_path IS NOT NULL
|
||||
AND (
|
||||
(display_path IS NULL AND preview_path IS NOT NULL)
|
||||
OR derivatives_rev < $1
|
||||
)",
|
||||
)
|
||||
.bind(1i16)
|
||||
.fetch_all(&pool)
|
||||
.await
|
||||
.expect("backfill query");
|
||||
|
||||
assert!(
|
||||
backfilled.is_empty(),
|
||||
"a soft-deleted row must be invisible to the backfill, before or after sweeping"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user