Skip to content

feat(selfupdate): gzip-compress snapshot state.db (closes #147) - #263

Closed
frahlg wants to merge 1 commit into
masterfrom
claude/exciting-ramanujan-CW35o
Closed

frahlg wants to merge 1 commit into
masterfrom
claude/exciting-ramanujan-CW35o

Conversation

@frahlg

@frahlg frahlg commented May 7, 2026

Copy link
Copy Markdown
Member

Summary

Closes #147. Pre-update snapshots used to land at ~200 MB raw on a mature site, making the five-slot retention budget ~1 GB on disk. SQLite's hot-tier tables compress 5–10× with gzip, so we now VACUUM INTO a temp file and gzip-stream it into snapshots/<id>/state.db.gz. Expected steady-state on the same site: ~25–40 MB per snapshot, ~150–200 MB total for five slots.

What changes

  • state.SnapshotToCompressed(dst) (go/internal/state/store.go) — wraps the existing SnapshotTo (which can't stream — SQLite's VACUUM INTO requires a file path) with a gzip stream into the final .gz path. The intermediate raw temp is removed on success and on every error path. Refuses to overwrite an existing destination, matching SnapshotTo.
  • Server.createPreUpdateSnapshot (go/internal/api/snapshots.go) now writes state.db.gz and records that name in meta.json#files, so the existing meta.Files-driven rollback flow picks up the new format with no code change in selfupdate.TriggerRollback.
  • ftw-updater rollback (go/cmd/ftw-updater/main.go) — for each entry in Files that ends with .gz, decompress to a sibling <plain>.rollback.tmp in the snapshot dir, docker cp that into the container at /app/data/<plain> (suffix stripped), then unconditionally remove the temp. Files without .gz take the legacy direct-cp path unchanged, so older snapshots still restore.

The schema version in SnapshotMeta doesn't change because nothing in the snapshot dir layout is renamed — the file list is data, and readers already iterate it.

Tests

  • state.TestSnapshotToCompressedRoundTrip — gzip → gunzip → reopen, asserts seeded rows survive and the raw temp is cleaned up.
  • state.TestSnapshotToCompressedRefusesExistingFile — second call to the same path errors instead of stomping.
  • ftw-updater.TestHandleUpdate_RollbackDecompressesGz — end-to-end: a state.db.gz entry in the snapshot is decompressed, docker cp targets /app/data/state.db (no .gz on the container side), and no *.rollback.tmp leaks after the rollback completes.
  • api.TestVersionUpdate_CreatesSnapshotBeforeTrigger updated to look for state.db.gz and assert that the raw state.db is NOT left next to it. The test also gunzips the snapshot in-process and reopens it as a state.Store to prove the bytes round-trip.

Verified

  • go build ./... clean
  • go vet ./internal/state/ ./internal/api/ ./cmd/ftw-updater/ clean
  • go test ./... -count=1 -short — full module green (state, api, ftw-updater + e2e)

Test plan

  • Unit tests for the new compressed snapshot path.
  • Unit test for the rollback decompression path.
  • Existing pre-update snapshot test updated for the new filename.
  • On a real device: run a self-update, verify du -sh snapshots/ is ~5–10× smaller than before; trigger a rollback, verify the main service comes up on the restored state.

Out of scope

  • Migrating older state.db (uncompressed) snapshots already on disk. Existing snapshots stay valid because the rollback path falls through to the legacy direct-cp branch when the file list lacks .gz entries; we don't rewrite history.
  • The "sidecar-free" preference from the issue stays: gzip is stdlib, no new dependency.

https://claude.ai/code/session_01SQtNeXFhRoEiwjLghE69KR


Generated by Claude Code

Pre-update snapshots used to land at ~200 MB raw on a mature site,
making the five-slot retention budget ~1 GB on disk. SQLite's hot-tier
tables compress 5–10x with gzip, so we now VACUUM INTO a temp file and
gzip-stream it into snapshots/<id>/state.db.gz. The temp is removed on
success and on every error path.

The rollback sidecar transparently decompresses any *.gz entry in a
snapshot's Files list to a sibling temp before docker cp, then strips
the .gz suffix on the container destination so the main service still
finds plain /app/data/state.db. Snapshots without .gz entries (older
formats) take the legacy direct-cp path unchanged.

Tests cover the round-trip and the existing-file refusal on the
compressed variant, plus a new ftw-updater rollback test that verifies
state.db.gz is decompressed and the rollback temp is cleaned up after.

https://claude.ai/code/session_01SQtNeXFhRoEiwjLghE69KR

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bd4af1f18c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

var tempToRemove string
if strings.HasSuffix(f, ".gz") {
plain := strings.TrimSuffix(f, ".gz")
tmp := filepath.Join(hostSnapDir, plain+".rollback.tmp")

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Decompress rollback payload into a writable directory

For .gz snapshot entries this now writes a temp file under hostSnapDir, but in the default deployment that path is mounted read-only in the updater container (docker-compose.yml mounts ${PWD} as :ro). As a result, rollback of state.db.gz will fail at temp-file creation with a read-only filesystem error before docker cp runs, so the new compressed snapshots cannot be restored in production unless the mount policy changes.

Useful? React with 👍 / 👎.

@frahlg

frahlg commented May 26, 2026

Copy link
Copy Markdown
Member Author

Superseded by #309 (merged 2026-05-25), which made snapshots skip the TS tables and brought the snapshot size from ~198 MB down to ~50 MB — that fix removes the original motivation for issue #147. Adding gzip on top of a 50 MB snapshot only saves single-digit MB at the cost of extra rollback complexity in ftw-updater. Closing this; #147 should likely be closed too.

@frahlg frahlg closed this May 26, 2026
@frahlg
frahlg deleted the claude/exciting-ramanujan-CW35o branch November 11, 2026 09:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Compress snapshot state.db to cut disk usage

2 participants