Skip to content

Filesystem usage ceiling with oldest-day pruning (#14) - #23

Merged
mouldybread merged 12 commits into
mainfrom
feature/14-cp2-filesystem-usage
Nov 29, 2026
Merged

mouldybread merged 12 commits into
mainfrom
feature/14-cp2-filesystem-usage

Conversation

@mouldybread

@mouldybread mouldybread commented Nov 28, 2026 •

Copy link
Copy Markdown
Owner

Closes #14

This PR carries all checkpoints of #14, one at a time. The agreed design and checkpoint plan are in #14 (comment) and the CP1 findings are in #14 (comment). The approved CP3 names are in #14 (comment), amended in #14 (comment). Running status is in #14 (comment).

This PR deletes recordings (CP4 ceiling pruning and CP5 age cleanup). Deleted footage is not recoverable.

Feature summary (as agreed in #14)

  • Measure the used % of the filesystem holding /recordings, every 5 minutes (first check 5 minutes after startup), in its own background job.
  • A configurable maximum used % (default 80). At ceiling minus 2, remove the oldest day folder that still has .mp4 files from each camera, never the newest UTC day, and stop pruning if a pass frees less than 1 MiB.
  • Event records for a pruned day are removed with it. Unknown measurement means no pruning, and the failure is logged.
  • Retention days is an optional maximum age (0 = off); whichever limit applies first wins.
  • Recording is never stopped or rejected by the ceiling. SIMPLENVR_MIN_FREE_BYTES is unchanged. Estimated days remaining was dropped.

Checkpoint status

Checkpoint Status
CP1: re-baseline and read-only analysis Done (findings on #14)
CP2: filesystem measurement and 5-minute job Done (CI green on 60df3d7)
CP3: ceiling setting and settings display Done (CI green on e58835a, owner confirmed)
CP4: prune pass Done (CI green on 13f84b5, owner signed off)
CP5: retention days as optional maximum age Done (CI green on 4d24b37, owner signed off)
CP6: end-to-end acceptance Done: automated acceptance passed on d86f255; owner marked complete

All checkpoints are complete. Merging this PR closes #14.

CP6: end-to-end acceptance

  • Result: all nine check runs passed on exact head d86f255fab03936b4a57b7174eb22e8e44743f8d, including the new real-container filesystem-pruning acceptance. Run: https://github.com/mouldybread/SimpleNVR/actions/runs/46217386829. I read check conclusions only, not per-test output.
  • Defects found and fixed:
    • Ceiling POST was blocked by the auth method policy (fixed in fd8cbd6).
    • Per-camera retention update read the hidden duplicate input instead of the settings-dialog input (fixed in f9b1dc2).
    • Repeated ceiling-not-met logging when the same ceiling was saved again (fixed in d86f255).
  • Manual TrueNAS/ZFS runbook: not a completion requirement. ZFS snapshot and quota behavior is untested.
  • Not claimed: live RTSP qualification, real ZFS behavior, scheduled tick timing in the container acceptance, large-tree performance.

CP2

  • Measures used % with the df convention used / (used + available). Blocks reserved for privileged users count as used. Inconsistent block counts are an error, never a guessed percentage.
  • Runs through the shutdown gate. Shutdown cancels and joins it. On failure, usage becomes unknown and the failure is logged without the path.
  • One shared raw statfs reader backs both readRecordingStorageCapacity and the usage reader. /api/storage/status output and errors are unchanged.
  • Files: cmd/simplenvr/storage_usage.go, cmd/simplenvr/storage_usage_test.go, docs/FILESYSTEM_USAGE.md, cmd/simplenvr/storage_status.go, cmd/simplenvr/main.go.

CP3

  • Setting: top-level max_filesystem_used_percent in cameras.json. Default 80; whole numbers 10 to 95 only; invalid stored values make the configuration invalid. The trigger (ceiling minus 2) is derived, never stored.
  • API: GET /api/storage/ceiling and POST /api/storage/ceiling with {"max_filesystem_used_percent": <int>}. Invalid input is 400; failed save restores the previous value and returns a generic 500.
  • Settings: a "Filesystem Usage" row shows used %, ceiling and trigger, refreshed each time settings open.
  • Files: filesystem_ceiling.go and tests, web/static/js/filesystem-ceiling.js, tests/frontend/filesystem-ceiling.cjs, plus changes to persistence.go, main.go and index.html.

CP4

  • Job: cmd/simplenvr/filesystem_prune.go, a separate job every 5 minutes (first pass after 5 minutes) and soon after a successful ceiling change. Opening settings never prunes.
  • Pass: per camera, the oldest day folder with at least one regular .mp4; the newest day is never removed. Only that day's .mp4 files are unlinked; the folder is removed only if empty.
  • Guard: a gain under 1 MiB pauses pruning until available bytes rise 1 MiB above the post-pass reading. "Ceiling not met" is log-only.
  • Locking: media lease, then retentionPolicyMu (read), then briefly NVR.mu; prune and retention cleanup cannot delete at the same time.
  • Events: removed by camera name and UTC start day once every .mp4 of the pruned day is gone; kept if any file failed.
  • Never touched: .tmp, temp/, buffer/, staging, recovery evidence, other days, recorders.
  • Accepted at sign-off: events kept on a partly removed day; ceiling-not-met is log-only; unmeasurable post-pass usage uses the pre-pass baseline; a ceiling change does not bypass an active pause.

CP5

  • retention_days is a camera's maximum age: 0 = no age limit, 1 to 365 = days. No migration; new cameras register with 7.
  • /api/cameras/update-retention and /api/cameras/update-retention-all accept 0 to 365 and require the field.
  • Event cleanup keeps events for a camera name when any camera with that name has age off. Age cleanup and ceiling pruning are independent jobs behind the same exclusive media lease.
  • New web/static/js/retention-days.js replaces the retention functions from cameras.js.
  • docs/audit/RETENTION_POLICY.md and docs/audit/EVENT_RETENTION.md still describe 0 as the seven-day default; docs/RETENTION_MAX_AGE.md supersedes that.

Verification history

  • CP6: 9 of 9 checks passed on d86f255.
  • CP5: 8 of 8 required checks passed on 4d24b37.
  • CP4: 8 of 8 passed on 13f84b5.
  • CP3: 8 of 8 passed on e58835a.
  • CP2: 8 of 8 passed on 60df3d7.
  • Earlier per-checkpoint tests used temporary directories and a fake usage reader; the CP6 container job is the first end-to-end run, on a disposable loopback filesystem rather than a real recordings volume.

 CP2)

Read-only: measures used % of the /recordings filesystem every 5 minutes (first check 5 minutes after startup) in its own background job joined on shutdown. Failures are logged and leave usage unknown. No deletion, setting or UI yet.
 CP2)

Add readRecordingFilesystemStats (no-follow open + Fstatfs returning raw counts). readRecordingStorageCapacity keeps its result and errors; readFilesystemUsage now uses the same reader instead of duplicating it.
@mouldybread mouldybread added the run-checkpoint-ci Explicitly request full checkpoint verification label Nov 28, 2026
@mouldybread mouldybread changed the title Recording filesystem usage measurement and 5-minute monitor (#14 CP2) Filesystem usage ceiling with oldest-day pruning (#14) Nov 28, 2026
Add the persisted max_filesystem_used_percent setting (default 80, 10 to 95,
written explicitly on every save), GET/POST /api/storage/ceiling, and a
Filesystem Usage row in camera settings. No deletion yet.
Register the ceiling handler in main.go and describe the setting, the API and
the settings row in docs/FILESYSTEM_USAGE.md. Nothing is deleted yet.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 28, 2026
Load filesystem-ceiling.js into a fake DOM and check that opening settings
adds one row and refreshes it, that unknown usage and failed loads are shown
plainly, that only valid whole numbers from 10 to 95 are sent, that failures
never announce success, that a pending save blocks a second request, and that
the script loads once, after modals.js.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 28, 2026
Add a separate prune job that, when filesystem usage reaches the ceiling minus
2 points, removes the oldest day with published MP4s from each camera, never
the newest day folder. Deletion takes the media lease and reuses the published
deletion engine, which gains an opt-in day-scoped, continue-on-entry-error mode.
A 1 MiB no-effect guard pauses repeats, a ceiling change triggers a pass, and
event records for a fully pruned day are removed.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 28, 2026
retention_days 0 now means no age limit; 1 to 365 is the maximum age. The
retention handlers require the field, so only an explicit 0 turns age cleanup
off. Cameras with 0 are not retention cleanup targets, and events for any camera
name that has an age-off camera are not expired by age. The settings inputs allow
0. No migration: every install is new.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 29, 2026
Retention keeps names shorter than 15 characters, so s.mp4 was never expired by
age. Use a full stamped name so the limited camera's old day is actually due.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 29, 2026
Run the real runtime image on a loopback ext4 volume and drive it with headless
Chrome and the real HTTP API: settings rows, retention 0, ceiling pruning one day
per camera per pass, the 1 MiB no-effect guard (hard links stand in for
snapshots), a partial failure (chattr +i), ceiling-not-met, untouched evidence,
running recorder, clean shutdown and restart. Add the job to the required gate and
an owner runbook for a real ZFS volume.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 29, 2026
The auth layer allowed only GET and HEAD on paths outside mutationPaths and only
POST on paths inside it, so the ceiling route (GET and POST) was rejected with 405
on every POST in a running server. CP3 tests called the handler directly and
bypassed auth. Add a small read-write mutation allowlist, register the ceiling
route as a tracked mutation that also serves reads, test it through the real auth
handler, and print the response status when the acceptance save assertion fails.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 29, 2026
The camera list is rendered twice with the same element ids (a hidden section and
the settings dialog). getElementById returns the hidden copy, so Update sent its
stale value instead of what was typed in the settings dialog. Look the input up in
the settings dialog's list first, falling back to the old lookup, and test it.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 29, 2026
requestPass re-armed the message on every ceiling save, so saving the same value
twice logged it twice, contradicting the documented behavior. Remember the
ceiling the state was logged for: the same ceiling stays silent, a different one
logs again, and falling below the trigger or pruning something still resets it.
@mouldybread mouldybread added run-checkpoint-ci Explicitly request full checkpoint verification and removed run-checkpoint-ci Explicitly request full checkpoint verification labels Nov 29, 2026
@mouldybread
mouldybread merged commit b6f7a86 into main Nov 29, 2026
9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

run-checkpoint-ci Explicitly request full checkpoint verification

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Explore disk-usage limits and estimated storage remaining

1 participant