Repository navigation
fix(state): pace the recovery snapshot behind live commits - #2099
Merged
Merged
Conversation
SnapshotState copied state.db with one INSERT ... SELECT per table. Each table went over in one transaction, so tens of megabytes stayed dirty until a single fsync at commit. On an SD card, live history commits and config writes waited behind that fsync for seconds, past the five-second history deadline. This is the snapshot at boot plus two minutes and at each 24 h tick (#1554). SnapshotTo now uses the live backup's state copy: one read transaction on state.db, rows in short synced batches checked against the source, and the backup's pause between batches. The snapshot therefore reads one point in time across all tables, and it keeps sqlite_sequence, so a restored control journal no longer reuses an id. quick_check reads only the new file and holds no lock on state.db or history.db; it stays. SnapshotState also removes a journal left by an interrupted copy. On ext4 over a loop device throttled to 8 MiB/s writes, history commits during a 150 MiB snapshot fell from p50 2.7 s / max 5.3 s to p50 97 ms / max 481 ms; config writes from max 5.6 s to 66 ms. Refs #1554 Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Fredrik Ahlgren <[email protected]>
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Refs #1554
Problem
Field support files from two ARM64 sites show most history commit failures in the minute the state snapshot runs: two minutes after boot and at each 24 h tick. Each failure passed on retry and no tick was lost, but #1554 holds stable until the cause is found and fixed.
SnapshotStatecopied state.db with oneINSERT ... SELECTper table into an attached file. Each table went over in one transaction, so a whole table stayed dirty until one fsync at commit. On a slow card, live history commits (synchronous FULL) and config writes waited behind that fsync. The backup path already avoids this: short synced batches and a pause between them.Change
SnapshotTonow uses the live backup's state copy (copyStateForBackup, split intocopyStateTablesso each caller names its own excluded tables). It copies from one read transaction on state.db, writes rows in batches of at most 1,024 rows or 256 KiB, commits and syncs each batch, checks every table against the source by row count and hash, and callsbackupCopyYieldbetween batches. That pause ispauseMaintenance, the same 100 ms yield backups and archives use. It comes about once per second of copying, and after every batch while 16 or more history ticks wait.snapshotExcludedTables) and the samequick_checkbefore the rename.sqlite_sequence, so a restored control journal no longer hands out an id a deleted row had (the old copy set it to the highest id still present).quick_checkstays. It reads only the new file and holds no lock on state.db or history.db. In the rig, commits that started while it ran took at most 565 ms.SnapshotStatealso removes a-journal,-walor-shmleft beside the temp file by an interrupted copy.snapshotTableCopyOrder,rewriteCreateForAttachedDB) go. The destination has foreign keys off, as in the backup, so copy order no longer matters.Evidence
Slow-storage rig
TestStateSnapshotLiveHistoryLatency(new, skipped unlessFTW_SNAPSHOT_LATENCY_DIRis set) builds a state.db with a 150 MiB snapshot: the forecast tables about halfway to their retention budgets, plus events and the control journal. It also builds a 267 MiB history.db with five days of 10 s buckets for 60 series. It then runs the real history writer with its real 5 s deadline. One tick of 60 samples comes every 2 s, Core's default control interval, and each tick is committed on its own so every interval gives a sample. A config write runs every second. Between 20 s quiet windows the test copies the snapshot and runsquick_check.The rig is arm64 Linux 7.2 in a privileged Docker container on an Apple-silicon Mac (
--cpus=2, thepython:3.12-slim-bookwormimage because it hasmkfs.ext4): ext4 on a loop device backed by tmpfs. Between the phases the test remounts the file system to drop its page cache. It then throttles the loop device through the container's cgroup v2io.max. A 16 MiBdd conv=fsynctakes 2.0 s at the 8 MiB/s setting and 4.0 s at 4 MiB/s, as set.These are commits and writes that started during the snapshot copy. Before is master at 97c2d34 with the new test file; after is this branch.
Outside the snapshot, the median commit took 10–40 ms in every run, before and after. No commit failed in any run: on this rig a late commit still lands once its fsync returns. In the field, some ran out the deadline and were retried.
quick_checktook 83–127 ms in five of the six runs. In the slow-card run on old code it took 21 s, and the five commits that started in that window took at most 565 ms.Suite
New tests in
snapshot_pace_test.go, all failing onorigin/masterand passing here:TestSnapshotYieldsBetweenShortSyncedBatches: copying 4 MiB yields at least 8 times, with at most 1 MiB written between yields. Old code: "snapshot yielded 0 times".TestSnapshotCopiesOnePointInTime: writes made during a pause, to a table copied earlier and to one copied later, are both absent from the snapshot. Old code: "snapshot never yielded".TestSnapshotKeepsAutoincrementHighWaterMark: old code: "sequence = 2 ... want 3".Runs:
go test ./internal/state/ -count=1: ok (49 s)go test -race ./internal/state/ -run 'Snapshot|Backup|Recover|Marker' -count=2: okmake verify(rebased on d43e80d): ok, "verify: vet + test + build clean"Not tested
io.max; it does not show SD card firmware stalls, reads and writes sharing one budget (blk-throttle keeps them apart), or jbd2 journal writes (they run in the root cgroup and are not throttled). It also runs on Apple-silicon cores that are faster than a Pi's.writer_lockwait just after the hour and a forecast-archive write timeout, are not addressed here.For Fredrik
Close. If cut short, it leaves the previous snapshot in place.snapshotExcludedTables, as before. The backup also leaves out the history tables when history.db exists; the snapshot does not. I left that as it was.🤖 Generated with Claude Code