Close stage 1d of the job-store plan
Stage 1d is done: jobs survive a restart in the SQLite store, and a restart never prints a receipt twice. Its paper checks passed on the TM-T88IV, with the owner's OK for each: - A receipt printed once, read done, and still read done after restarts. - With the printer off, a receipt was sent and the daemon stopped gracefully while it retried; with the printer on again, the restart printed it once. - With the cover open, the daemon was killed while a 128 KB receipt waited for the printer; after the restart it read uncertain, was never sent again, and printed once, cut short, when the cover closed. The printer then needed switching off and on. - With confirmation, a receipt read done on the printer's answer, and one sent with the cover open waited, with nothing sent, and printed once when the cover closed. - With GS a 0 and DLE EOT 1, a receipt printed once with nothing extra and read done on the printer's answer; after a power cycle, with /healthz polled every 250 ms, one sent with the cover open waited 15.7 s with nothing sent and printed once when the cover closed. - With each connection opened by DLE EOT 1, GS a 0 and GS r 1, and everything before the DLE EOT answer dropped, a receipt printed once with nothing extra and read done on the printer's answer. The plan's status says so, and stage 1d records the confirmation the owner had built after the first try at the third check. The Investigation findings are folded into design.md, apart from Epson's acknowledgement and its sources, which phase 5 still needs and which stay under Findings for phase 5. The references to them point to design.md. The job states, which receipts printed, decision 16's count of uncertain jobs, decision 21 and phase 5's config key now match the confirmation, and the Architecture says the connector runs the pragmas in order and that Idle checkpoints once 64 commits have gathered. The RECORDS schema keeps refs in the order they were kept, by kept, as the store does, since job IDs need not sort in that order. docs/plans/printers.md notes that confirmation takes only a GS r 1 answer with bits 4 and 7 clear, which stage 4a checks on the TM-T88VII. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
d3c3025e64
commit
bdda410deb
2 files changed
+31
-183
No files matched your search
+30
-182
@@ -1,6 +1,6 @@
|
||||
# Plan: persistent job store and image previews
|
||||
|
||||
Status: **in progress; stage 1a's measurements are done (Investigation findings), and so are stage 1b's `[state] dir` and stage 1c's state machine on the memory store. The store on disk is not built.** The owner accepted every proposed default on 2026-12-01 (Decisions), and design.md's Planned next item 1 records them. Each stage runs in its own session, briefed from its section: stage 1a follows decisions 1-3, stage 1b decision 4, stage 1c decisions 5-12, stage 1d decisions 13-15, phase 2 decision 16, phase 2b decisions 17 and 18, phase 3 decision 19 and phase 4 decision 20; phase 5 holds decisions 21-23. Phases 3a-3c need nothing from phases 1 and 2, so they can come first if the owner wants previews sooner. Phase 2b needs phase 1 but not phase 2, so it can come before phase 2. Every check on the live printer needs the owner's OK.
|
||||
Status: **in progress; phase 1 is done: stages 1a-1d measured SQLite on leil-rig, added `[state] dir`, built the state machine and the SQLite store, so jobs survive a restart and a restart never prints a receipt twice. Phases 2 to 5 are not built.** The owner accepted every proposed default on 2026-12-01 (Decisions). design.md records what phase 1 built (Decisions, "Jobs survive a restart") and, as its Planned next item 1, what is left. Each stage runs in its own session, briefed from its section: stage 1a follows decisions 1-3, stage 1b decision 4, stage 1c decisions 5-12, stage 1d decisions 13-15, phase 2 decision 16, phase 2b decisions 17 and 18, phase 3 decision 19 and phase 4 decision 20; phase 5 holds decisions 21-23. Phases 3a-3c need nothing from phases 1 and 2, so they can come first if the owner wants previews sooner. Phase 2b needs phase 1 but not phase 2, so it can come before phase 2. Every check on the live printer needs the owner's OK.
|
||||
|
||||
## Decisions (owner, 2026-11-30)
|
||||
|
||||
@@ -31,7 +31,7 @@ Status: **in progress; stage 1a's measurements are done (Investigation findings)
|
||||
- **Waiting jobs can be cancelled** (phase 2b). An ntfy `message_delete`
|
||||
event cancels a job of its notification that is still waiting.
|
||||
|
||||
The owner also asked a question, which is not a decision: **does a store that keeps data in memory and syncs it to disk later, such as BadgerDB, cut disk IO?** Hosts may be Raspberry-Pi-class with SD cards, which wear. "Does keeping data in memory cut IO?" under Investigation findings answers it.
|
||||
The owner also asked a question, which is not a decision: **does a store that keeps data in memory and syncs it to disk later, such as BadgerDB, cut disk IO?** Hosts may be Raspberry-Pi-class with SD cards, which wear. The answer: only by not syncing. A store that keeps commits in memory saves writes by leaving them to the kernel, which writes within about 30 s, and any store can do the same; but a power cut can then roll back a `sending` mark, and the receipt prints again after the restart. So the saving comes from the sync policy, not from the store, and the `sending` mark must be synced whatever the store. design.md's sync policy is that policy.
|
||||
|
||||
The owner accepted the details below on 2026-12-01. They are numbered, so the rest of the plan can refer to them. Decisions 7 and 21 follow docs/plans/remote-printers.md decisions 17 and 24.
|
||||
|
||||
@@ -40,9 +40,9 @@ For stage 1a:
|
||||
1. **The store.** SQLite through `modernc.org/sqlite` (v1.63.0 today) and `database/sql`, with about 8 hand-written statements; not GORM.
|
||||
- What SQLite buys here: crash recovery and torn writes are handled by SQLite rather than by our code; it is the owner's stated preference; and it leaves room for queries later. The design itself runs none: every status lives in memory, the store is read by sequence number only, and exclusive locking (decision 3) keeps even the `sqlite3` CLI from reading `jobs.db` while the daemon runs.
|
||||
- Not GORM, though it writes the same bytes and fsyncs as plain SQL (measured). Its pure-Go dialector, `github.com/glebarez/sqlite` v1.11.0, has had no release since 2024-03-14 and asks for maintainers (issue #152). A plain `go get` of it pulls in SQLite 3.41.2, and it cannot share a binary with `modernc.org/sqlite`. GORM's default logger prints SQL with its values, which would log print contents. It adds about 10 MB to the daemon against 4 MB, because one reflective call in GORM stops the linker dropping unused methods anywhere in the binary; Expr makes the same call (docs/plans/printers.md, Investigation findings, EXPR), so once Expr is in the binary (docs/plans/templates.md, decision 1) that part of the gap goes.
|
||||
- Trade-off: on the measures alone the home-grown journal wins: the fewest bytes and syncs, no dependency, the same behaviour on every platform. At the device the gap is smaller than the logical bytes suggest, because each sync also rewrites a 4 KiB block and roughly 12-20 KiB of ext4 journal: SQLite writes about 3-5× the journal's bytes, not about 30× (estimates, COMPARED). At 500 jobs a day the candidates write between about 8 and 120 MiB a day.
|
||||
- Trade-off: on the measures alone the home-grown journal wins: the fewest bytes and syncs, no dependency, the same behaviour on every platform. At the device the gap is smaller than the logical bytes suggest, because each sync also rewrites a 4 KiB block and roughly 12-20 KiB of ext4 journal: SQLite writes about 3-5× the journal's bytes, not about 30× (estimates). At 500 jobs a day the candidates write between about 8 and 120 MiB a day.
|
||||
- The store comes in at its latest stable release.
|
||||
2. **Hardware for the measurements.** The owner has no ARM boards and no Pi-class host, so stage 1a ran its probe on leil-test-1, an x86_64 VM on leil-rig with a virtual disk (Investigation findings, MEASURED ON LEIL-TEST-1). That measured Linux, device writes and flushes, and the unit's sandbox, but not an SD card or ARM. No build of the store runs on ARM hardware: modernc's own CI names no ARMv6 target, and there are old SIGSEGV reports for linux/arm. So the armv6, armv7 and arm64 tarballs ship the store untested on hardware, and the CHANGELOG says so.
|
||||
2. **Hardware for the measurements.** The owner has no ARM boards and no Pi-class host, so stage 1a ran its probe on leil-test-1, an x86_64 VM on leil-rig with a virtual disk (design.md, "Jobs survive a restart", Measured). That measured Linux, device writes and flushes, and the unit's sandbox, but not an SD card or ARM. No build of the store runs on ARM hardware: modernc's own CI names no ARMv6 target, and there are old SIGSEGV reports for linux/arm. So the armv6, armv7 and arm64 tarballs ship the store untested on hardware, and the CHANGELOG says so.
|
||||
3. **SQLite's locking mode.** `locking_mode=EXCLUSIVE` and one connection. There is no `-shm` file and no mmap, and a second daemon on the same directory fails at start. Trade-off: reads wait behind writes and their fsyncs, and no other process, the `sqlite3` CLI or a later read-only `uberbringer` command included, can read the database while the daemon runs. Statuses stay in memory, so only the worker and previews by ID read the database.
|
||||
|
||||
For stage 1b:
|
||||
@@ -99,7 +99,7 @@ For phase 2:
|
||||
16. **A history listing.** `GET /api/v1/jobs`, newest first, for full tokens only. A listing shows every client's jobs, so tokens limited to modules do not get it. Trade-off: more API to keep. Its details:
|
||||
- `status`, `source`, `before` (a job ID; the page starts after it) and `limit` (1-100, default 50) in the query. A `before` that names a job no longer kept answers `invalid_query`, so paging never skips jobs silently.
|
||||
- Job status gains `ref`, so a client can find the job an ntfy message became.
|
||||
- `/healthz`'s `queue` gains `uncertain`: the jobs found `uncertain` at the latest start, those with a delivery that `Load` turned from `sending`. From remote-printers stage 4 it also counts the jobs a remote send has left `uncertain` since that start. A clean restart sets it back to 0, so it does not stay up for about 1,000 jobs.
|
||||
- `/healthz`'s `queue` gains `uncertain`: the jobs found `uncertain` at the latest start, those with a delivery that `Load` turned from `sending`, and those the printer has not confirmed since (stage 1d). From remote-printers stage 4 it also counts the jobs a remote send has left `uncertain` since that start. A clean restart sets it back to 0, so it does not stay up for about 1,000 jobs.
|
||||
|
||||
For phase 2b:
|
||||
|
||||
@@ -124,153 +124,16 @@ For phase 4:
|
||||
|
||||
For phase 5, later:
|
||||
|
||||
21. **Epson's process-ID acknowledgement** (`GS ( H` fn 48) on models that have it: not in phases 1-4; phase 5, if the owner says yes after a paper test. docs/plans/remote-printers.md decision 24 builds it at the agent with this phase 5 (its stage 6). The state machine is safe without it. Trade-off: it would turn most `uncertain` jobs into confirmed prints, but it holds the printer connection until the receipt is done, so `/healthz` needs another way to read status mid-job, and whether its answer gets through the printer's network board is unverified.
|
||||
21. **Epson's process-ID acknowledgement** (`GS ( H` fn 48) on models that have it: not in phases 1-4; phase 5, if the owner says yes after a paper test. docs/plans/remote-printers.md decision 24 builds it at the agent with this phase 5 (its stage 6). Stage 1d already confirms each job with `GS r 1`, which holds the connection until the job has run (design.md, "Done means the printer confirmed it", owner, 2026-12-01); `GS ( H` would add an ID of the host's choosing. The state machine is safe without it. Trade-off: it would turn most `uncertain` jobs into confirmed prints, but it holds the printer connection until the receipt is done, so `/healthz` needs another way to read status mid-job, and whether its answer gets through the printer's network board is unverified.
|
||||
22. **ntfy state stays out of the store.** The JSON files stay. They write within a second of a change, and a keepalive-only cursor at most every 10 minutes, and the subscriber is built and tested on them. On a WebSocket each save also writes every seen ID within `max_backlog`, up to some 700 KB on a busy topic with a 12-hour backlog (docs/plans/ntfy.md decision 12). Trade-off: one file and one sync policy, against coupling ntfy to the store, which could write only the IDs that changed.
|
||||
23. **A write error after part of a receipt went out.** Today such a send is retried, and design.md accepts that part of a receipt can then print twice, because retries let a job survive a printer that is briefly offline. That stays. docs/plans/remote-printers.md decision 17 builds on it: a remote `partial` outcome is today's write error. Once `Send` reports how many bytes `Write` took (docs/plans/printers.md stage 3's `SendError`, or phase 5's confirmed send), the owner may choose that such a job becomes `uncertain` and is never resent. Trade-off: fewer doubled fragments, against more jobs a person must check. With several printers, docs/plans/printers.md decision 22 fails such a delivery over to the next printer of an anycast list after the retries on the first printer (Architecture, DELIVERIES AND SEVERAL PRINTERS). That is a second double-print path: the next printer gets the whole receipt, so a receipt the first printer took in full before the write error prints in full on both.
|
||||
|
||||
## Investigation findings
|
||||
## Findings for phase 5
|
||||
|
||||
Four research tracks ran on 2026-11-30: SQLite, key-value stores, a build of each candidate and Epson's print acknowledgement, with a map of the repository; a review of the plan added a race-detector run. Runs were light and on the Windows workstation (NVMe SSD, NTFS), so latencies are not those of an SD card. Nothing ran against the printer, on a Pi or on an SD card. Stage 1a then ran a probe on leil-test-1, a Linux VM (MEASURED ON LEIL-TEST-1). Versions are the latest on 2026-11-30, from proxy.golang.org; stage 1a used `modernc.org/sqlite` v1.64.0, released on 2026-12-01.
|
||||
|
||||
THE ONE WRITE THAT MATTERS
|
||||
- A TCP write returns when the kernel has the bytes, not when they print. No store can record "printed" at the moment of sending.
|
||||
- What a store can do is record `sending` durably before the first byte goes out. A job found in `sending` after a restart is in doubt and is never resent; a job still `queued` never reached the printer.
|
||||
- A process crash (panic, OOM kill, kill -9) loses nothing already written, because the page cache survives. A power cut or a kernel crash loses writes not yet synced: Linux writes dirty data back about 30-35 s after it changed (a 30-s expiry, checked every 5 s). On a Pi, pulling the plug is the common crash.
|
||||
- So the `sending` mark must be synced before the first byte, whatever the store. Most other writes may be lazy: losing one makes a job read as `uncertain`, or loses a job never sent, but never prints one twice. The exception is `failed`: rolled back to `queued`, a job its client was told had failed would print after the restart (Architecture). The minimum is one fsync per job and one per failure, or two per job if an accepted job must survive a power cut (decision 5).
|
||||
- Every durability claim assumes the device honours a flush. SQLite's docs warn that "most consumer-grade mass storage devices lie about syncing". SD cards are suspect, and no source names specific cards.
|
||||
|
||||
SQLITE IN WAL MODE
|
||||
- A commit appends one frame per changed page to the `-wal` file: a 24-byte header and the 4,096-byte page. The last frame carries the commit mark. Frames are checksummed in a chain, so recovery keeps the longest valid run of commits.
|
||||
- `synchronous` in WAL mode:
|
||||
|
||||
| Setting | Syncs | Process crash | Power cut |
|
||||
|---|---|---|---|
|
||||
| FULL (modernc's compiled default; EXTRA acts the same in WAL mode) | the WAL after each commit | every commit survives | every commit survives, if the device flushes |
|
||||
| NORMAL | none per commit; the WAL before each checkpoint, the database after it | every commit survives | the database stays consistent; commits since the last sync may roll back |
|
||||
| OFF | none | every commit survives | the database may be corrupted |
|
||||
|
||||
- **Checkpoints.** One runs (PASSIVE) on the commit that takes the WAL to 1000 pages, about 4 MB, and one when the last connection closes. It runs on the committing thread, so it can delay that commit. It costs 2 fsyncs, the WAL then the database, and a third for the WAL header when the log restarts. It copies only the latest version of each page. The WAL is reused from the start, not truncated, unless `journal_size_limit` is set.
|
||||
- **Sync levels can be mixed.** `synchronous` belongs to the connection, is not persisted, and cannot change inside a transaction. The worker can run `PRAGMA synchronous=FULL`, commit the `sending` mark, then return to NORMAL, all between transactions. The WAL is one append-only file, so that sync also makes every earlier NORMAL commit durable.
|
||||
- **So SQLite can record `sending` durably before the bytes go out, for one fsync per job**, plus about 0.04 per job in checkpoints. On the workstation's NVMe, a FULL commit took about 1.1 ms and a NORMAL one 0.02-0.05 ms. On leil-test-1's virtual disk, a FULL commit took 5.0 ms (median), or 2.9 ms with fdatasync, and a NORMAL one about 0.6 ms. An SD card's flush time is unmeasured.
|
||||
- fsyncs per job, with `done` and the pruning in one commit:
|
||||
|
||||
| Policy | fsyncs per job |
|
||||
|---|---|
|
||||
| FULL for `sending`, `failed` and inserts from ntfy (decision 5) | 1 + 0.04, plus 1 per failed job and per ntfy job |
|
||||
| FULL for every insert too (not decision 5) | 2 + 0.04, plus 1 per failed job |
|
||||
| FULL for every commit | 3 + 0.04, or 4 with the pruning as its own commit |
|
||||
| NORMAL for every commit | 0.04, with no guard against a second print after a power cut |
|
||||
|
||||
- **Bytes per job.** The SQLite track measured with a full history of 1000 jobs, bodies in a table of their own, and pruning:
|
||||
- A text job (1.5 KB of JSON) appends 14.35 frames, about 57.7 KiB, to the WAL, and later writes 0.7 pages (2.8 KiB) into the database. A checkpoint comes about every 70 jobs, and the file levels off at 2.1 MiB.
|
||||
- An image job (50 KB of JSON) appends 28 frames (112.8 KiB), and the file levels off at 48 MiB.
|
||||
- Page 1 changes in almost every commit, because it holds the database size and the free-page count.
|
||||
- The spike, with a small table and no pruning, measured 2.67 frames for an insert of 1 KiB and 1 frame for an update: about 15 KB for an insert and one change.
|
||||
- Frame counts shift with the schema. Every run here kept a job's status in one table. The schema under RECORDS, which splits it into `jobs` and `deliveries`, appended 11.46 frames (46.1 KiB) per text job on leil-test-1.
|
||||
- At 100 text jobs a day that is about 5.6 MiB of WAL; at 1000, about 56 MiB.
|
||||
- NORMAL saves flush barriers and ext4 journal commits, not bytes, since the kernel still writes the WAL within about 30-35 s. An fsync that changes a file's size or mtime also commits roughly 12-20 KiB of ext4 journal (an estimate, not measured).
|
||||
- The rollback journal (`journal_mode=DELETE`) is worse: 2.4× the bytes per insert, 23 file-control calls and about 4 ms per commit (spike).
|
||||
- Ways to cut IO: keep bodies out of the status row, since a row that changes size rewrites its overflow pages; merge `done` and the pruning into one commit; prune in batches; optionally compress bodies; optionally checkpoint while the queue is idle.
|
||||
- **Operations.**
|
||||
- The database, `-wal` and `-shm` belong together. Never delete the WAL or copy the `.db` alone; `VACUUM INTO` makes a backup.
|
||||
- `locking_mode=EXCLUSIVE`, set before the first access, means no `-shm` and no mmap. A second process then gets `SQLITE_BUSY` at once, provided the daemon writes once at open.
|
||||
- `temp_store=MEMORY` avoids temporary files.
|
||||
- `auto_vacuum=NONE` reuses freed pages, so the file levels off. A VACUUM rewrites the whole database through the WAL.
|
||||
- SQLite creates files 0644 less the umask, and gives `-wal` and `-shm` the database file's mode.
|
||||
- `secure_delete=FAST` scrubs b-tree pages at no extra IO, but not free overflow pages, where image bodies end up. `ON` costs extra writes.
|
||||
- WAL does not work over a network filesystem, and Docker Desktop bind mounts pass through a VM file share.
|
||||
|
||||
MEASURED ON LEIL-TEST-1 (stage 1a, 2026-12-01)
|
||||
- **The host.** leil-test-1, the test node of leil-rig: an AlmaLinux 9.8 VM (kernel 5.14, x86_64, 16 cores) under KVM on pve-1. The store's directory was on xfs on a virtual QEMU SCSI disk, with `write_cache` "write back" and FUA on; what pve-1's storage does with a flush is unknown. So the times are a VM's, not an SD card's, and nothing ran on ARM (decision 2). The node was idle: load average 0.1 on 16 cores, no build running.
|
||||
- **The probe.** `jobprobe`, built with `CGO_ENABLED=0` and Go 1.27.4 on `modernc.org/sqlite` v1.64.0 (SQLite 3.54.1, libc v1.80.1, the same pin as v1.63.0). It sets the pragmas and creates the schema under RECORDS. It stores 1000 finished jobs, then runs 200 text jobs of 1,500 bytes, one delivery each: the insert at NORMAL, the `sending` mark at FULL, then `done` and the pruning of the oldest job in one commit at NORMAL. Device counters come from `/sys/dev/block`, read after a `syncfs`, less an idle baseline taken over a minute first.
|
||||
- **The sandbox allows the store.** Both runs ran as transient units with the unit's sandbox: `DynamicUser`, `ProtectSystem=strict`, `PrivateDevices`, `MemoryDenyWriteExecute`, `SystemCallFilter=@system-service` with `SystemCallErrorNumber=EPERM`, and `SystemCallArchitectures=native`. SELinux was enforcing, and both runs finished without an error. `@system-service` takes in `@sync` (`fsync`, `fdatasync`, `syncfs`), `@file-system` (`fcntl`, `ftruncate`, `openat`, `unlink`) and `@basic-io` (`pread64`, `pwrite64`). The database and the WAL were 0600, and there was no `-shm` file.
|
||||
|
||||
| Per job | fsync | fdatasync |
|
||||
|---|---|---|
|
||||
| Device writes, less the baseline | 53.8 KiB in 2.37 requests | 53.4 KiB in 2.38 requests |
|
||||
| Device flushes | 1.04 | 1.04 |
|
||||
| `write_bytes` in `/proc/self/io` | 52.6 KiB | 52.6 KiB |
|
||||
| WAL frames | 11.46 (46.1 KiB) | 11.46 (46.1 KiB) |
|
||||
| FULL commit (`sending`): median, p99, max | 4.98, 8.36, 9.87 ms | 2.89, 6.15, 6.76 ms |
|
||||
| NORMAL commit: median, p99, max | 0.55, 1.06, 15.12 ms | 0.61, 1.09, 24.01 ms |
|
||||
|
||||
- **Checkpoints.** Two automatic checkpoints ran in 200 jobs, about one every 87 jobs, and both ran inside an insert's commit. The slowest inserts, those that ran a checkpoint or restarted the log and synced its header, took 10-24 ms. The other NORMAL commits took about 1 ms or less. The final PASSIVE checkpoint, of 291 frames, took 6-7 ms and wrote 109.5 KiB to the device with 2 flushes. The database levelled off at 2.12 MiB and the WAL file at 3.93 MiB, within `journal_size_limit`.
|
||||
- **The baseline.** The node's own services wrote about 2 MiB a minute, in about 250 write requests and 85 flushes. Over a run of about 1.5 s that is about 0.3 KiB and 0.01 flushes per job. The baseline takes out steady writes, not a burst.
|
||||
- **Against the estimates above:**
|
||||
- Flushes: 1.04 per job, as predicted: one for the `sending` mark and about 0.04 for checkpoints and restarts of the log.
|
||||
- WAL frames: 11.46 per text job, against 14.35 in the SQLite track's runs with one table.
|
||||
- Device writes: 53.8 KiB per job, against the 75-80 KiB estimated for ext4. The device wrote about 8 KiB per job beyond the WAL frames: the xfs log of each sync and the job's share of the checkpoints. At 500 jobs a day that is about 26 MiB a day.
|
||||
- `fdatasync` left the writes and flushes the same and cut the synced commit's median by about 40%.
|
||||
- None of this argues against decision 1: the writes and flushes are at or below the findings' figures. An SD card's flush time and wear, ext4, and ARM remain unmeasured.
|
||||
|
||||
PURE-GO SQLITE: DRIVERS AND LAYERS
|
||||
|
||||
| | `modernc.org/sqlite` | `github.com/glebarez/sqlite` with GORM | `github.com/ncruces/go-sqlite3` |
|
||||
|---|---|---|---|
|
||||
| Latest | v1.63.0 (2026-11-22), SQLite 3.54.1 | v1.11.0 (2024-03-14), nothing tagged since; GORM v1.31.2 (2026-06-22) | v0.38.2 (2026-11-30), before 1.0; its GORM driver, gormlite, v0.34.0 |
|
||||
| How | SQLite's C transpiled to Go | a fork of GORM's SQLite driver, over a fork of modernc's driver | SQLite compiled to Wasm, translated to Go |
|
||||
| Added to the daemon (stripped, measured) | +4.0 MB (+26-27%) | +9.8-10.2 MB (+66-67%) | not measured |
|
||||
| Upkeep | active: 8 releases from August to November 2026 | issue #152 asks for maintainers; arm5 and arm9 panics open (#145, #108) | active; minor releases break its API |
|
||||
|
||||
- The daemon is 15,278,240 bytes on linux/amd64, 14,876,832 on armv6 and 14,090,400 on arm64 (commit ee65e72, stripped).
|
||||
- GORM adds no disk IO over plain SQL: the same bytes and flushes per commit, and 0.01-0.02 ms more CPU. Its default transaction around a single statement adds no fsync. `Save` rewrites every column. AutoMigrate rebuilds an SQLite table to change a column, and the table it made in the spike has a separate primary-key index, so each insert changes two b-trees.
|
||||
- GORM's size comes from `schema.go` calling `reflect.Value.MethodByName` with a name that is not a constant. That stops the linker removing unused methods anywhere in the binary; about 6 MB of the cost is other packages' methods (Fiber's text symbols rose from 641 to 1,441).
|
||||
- A plain `go get` of glebarez's dialector resolves SQLite 3.41.2 (modernc v1.23.1). Moving its driver, `github.com/glebarez/go-sqlite`, to v1.23.0 gives SQLite 3.53.3. Forcing modernc v1.63.0 under it works, but upstream has not tested that pairing. glebarez and modernc both register the driver name `sqlite`, so a binary with both panics at start.
|
||||
- GORM's own SQLite driver, `gorm.io/driver/sqlite` v1.6.0, needs cgo.
|
||||
- `sqlc` v1.31.1 calls its SQLite engine Beta, and pinning it as a tool would pull wazero and pg_query_go into go.mod.
|
||||
- modernc notes:
|
||||
- It syncs with fsync, not fdatasync, unless `sqlite.Fdatasync(true)` (v1.62.0, Linux) is set.
|
||||
- Any `close()` of any descriptor of the database file anywhere in the process drops SQLite's POSIX locks. OFD locks are opt-in.
|
||||
- Its CPU cost is 1.3-2.0× that of C SQLite, which does not matter at this workload.
|
||||
- Downstream go.mod files must pin its exact libc (cznic issue #177).
|
||||
- Historical SIGSEGV reports exist for linux/arm (GitLab #132), and a 32-bit `va_list` overrun was fixed around v1.53.
|
||||
- v1.63.0 passed `go test -race` (with checkptr) on windows/amd64: 50 jobs with WAL, exclusive locking, one connection and `synchronous` switched between FULL and NORMAL between commits. The race detector does not run on 32-bit ARM, and nothing has run the store on ARM hardware (decision 2).
|
||||
- ncruces fixed WAL-mode corruption in two optional VFSes in v0.37.0. Its v0.38.2 go.mod has no wazero, so it should run under `MemoryDenyWriteExecute=true` (an inference).
|
||||
|
||||
BBOLT (`go.etcd.io/bbolt` v1.5.0, 2026-06-03)
|
||||
- One file, pure Go, +0.25 MB to the daemon and no new dependency (`golang.org/x/sys` is already linked). Its CI cross-builds linux/386 and linux/arm and runs tests on Windows.
|
||||
- A commit rewrites every dirty page whole, from leaf to root, plus the free list and the meta page, with 2 fdatasyncs: the pages, then the meta page.
|
||||
- Measured with 1000 history jobs: 124.6 KiB and 6 fdatasyncs per job in 3 commits, or 100.5 KiB and 4 with commits merged; 2.1-2.7 ms per commit on NVMe. The spike, on a small file, measured 26 KB per insert and 21 KB per update, 2.4-5× SQLite's WAL bytes.
|
||||
- `NoSync` cannot be switched safely between commits. Unsynced pages and meta pages reach the disk in any order, so after a power cut the file can point at pages never written.
|
||||
- The file never shrinks without an offline `bbolt compact`. On 32-bit ARM its map is capped at 2 GB.
|
||||
- Its README lists possible corruption with ext4's `fast_commit` (kernel 5.10 and later; not on by default, as far as the track knows; `dumpe2fs -h` shows it) and with a power cut while an empty database initialises.
|
||||
|
||||
BADGERDB (`github.com/dgraph-io/badger/v4` v4.9.6, 2026-08-05): not recommended by the research
|
||||
- It adds 8.4 MB to the daemon on amd64 (+55%) and 7.9 MB on armv6.
|
||||
- With default options, Open allocates 86 MiB of heap and maps a 2 GiB value log and a 128 MiB memtable file; the block cache can grow by 256 MiB more. Tuned options cut the heap to 11.5 MiB, but one combination of options panics inside Open.
|
||||
- Its CI never builds GOARCH=arm or 386. armv6 and armv7 builds succeed, but a 2 GiB mapping is very unlikely to fit a 32-bit process (unverified).
|
||||
- On Windows `SyncWrites` does nothing: the `msync` of its mmap helper (ristretto, `z/mmap_windows.go`) is an empty function.
|
||||
- Space comes back only through compaction. Values of 1 MB or more go to the value log, which then needs garbage collection.
|
||||
|
||||
PEBBLE (`github.com/cockroachdb/pebble/v2` v2.1.7) is an LSM engine aimed at CockroachDB, with compactions and a heavy dependency tree. It is too much for about 1,100 records and was not measured.
|
||||
|
||||
A HOME-GROWN JOURNAL
|
||||
- Append-only JSON lines (accepted about 1.7 KB, sending about 85 B, done about 95 B), with snapshots for compaction written through the temp-file, fsync and rename pattern the ntfy state already uses.
|
||||
- One fdatasync makes every earlier append durable, so 1 fsync per job, for `sending`, is enough. Each sync costs roughly 12-20 KiB of device writes on ext4 (an estimate).
|
||||
- Recovery loads the snapshot, replays the journal and cuts a torn last line. It needs no dependency and behaves the same on every platform. The cost is about a package of our own code with crash-recovery tests, and no ad-hoc queries.
|
||||
- A snapshot-only store, like the ntfy state, would rewrite 0.2-1.9 MB per change: fine for coalesced state, not for per-job writes.
|
||||
|
||||
COMPARED (per job: accepted, sending, done with pruning; a 1.5 KB body; 1000 jobs of history)
|
||||
|
||||
| | SQLite WAL, FULL for `sending` | bbolt | Journal | Badger, `SyncWrites` |
|
||||
|---|---|---|---|---|
|
||||
| Added to the daemon | +4.0 MB | +0.25 MB | 0 | +8.4 MB |
|
||||
| fsyncs per job | 1.04, plus failed and ntfy jobs | 4-6 | 1-3 | 3 on Linux; none on Windows |
|
||||
| Bytes per job, logical | 15-58 KiB of WAL, then checkpoints | 100-125 KiB | about 2 KB, then compaction | about 2.5 KB, then flushes and compaction |
|
||||
| Device writes per job (estimate) | about 75-80 KiB | about 150-245 KiB | about 16-26 KiB | about 12-16 KiB, before ext4's journal, flushes and compaction |
|
||||
| Device writes a day at 500 jobs (estimate) | about 38 MiB | about 75-120 MiB | about 8-13 MiB | about 6-8 MiB and more |
|
||||
| `sending` durable before the first byte | yes | yes, at 2 fsyncs per commit | yes | on Linux only |
|
||||
| 32-bit ARM | builds; ARMv6 not in upstream's tests | builds; CI cross-builds arm | no issue | not built by upstream's CI |
|
||||
| Queries | SQL | key order; TypeIDs sort by time | our own maps | key order |
|
||||
|
||||
- The device estimates add, for each sync, a rewritten 4 KiB block and roughly 12-20 KiB of ext4 journal to the logical bytes. On that measure SQLite writes about 3-5× the journal's bytes, where the logical bytes differ about 30×. On xfs on leil-test-1, SQLite's device writes came to 53.8 KiB per job, below its estimate (MEASURED ON LEIL-TEST-1); ext4 and the other columns are unmeasured.
|
||||
- At 500 jobs a day, fsyncs a day: SQLite with decision 5's policy about 520, plus failures and ntfy jobs; the journal 500-1,500; SQLite with FULL for everything about 1,500 plus checkpoints; bbolt 2,000-3,000. For scale, saving every ntfy keepalive would have cost about 1,900 synced writes a day per subscription, which is why that cursor is saved at most every 10 minutes.
|
||||
|
||||
DOES KEEPING DATA IN MEMORY CUT IO?
|
||||
Only by not syncing. Badger's saving with `SyncWrites=false` comes from leaving its memory-mapped log to kernel writeback, so that several commits landing in one 4 KiB page within the 30-35 s window reach the disk once. Every candidate can make the same trade: SQLite with `synchronous=NORMAL`, or a journal without fdatasync (bbolt's `NoSync` can corrupt its file). A process crash loses nothing in any of them; a power cut loses up to about 30-35 s of commits. If those include a `sending` mark, the restarted daemon sees the job as `queued` and prints it again. So the saving comes from the sync policy, not from the store, and the `sending` mark must be synced regardless. The cheapest correct policy, syncing `sending` and letting most other writes ride on it, is natural in SQLite's WAL and in a journal, works in Badger on Linux only, and cannot be done safely in bbolt.
|
||||
What stage 1d decided is in design.md ("Jobs survive a restart"), with stage 1a's measurements on leil-test-1. Phase 5's acknowledgement still needs what the research found on Epson's side (2026-11-30):
|
||||
|
||||
PRINT ACKNOWLEDGEMENT ON EPSON PRINTERS
|
||||
- design.md says ESC/POS gives no acknowledgement of what printed. Epson documents one: `GS ( H` fn 48, the process ID response. The host sends `1D 28 48 06 00 30 30 d1 d2 d3 d4` after the data. For print data, the printer answers `37 22 d1 d2 d3 d4 00` "when the printing is completed" (ESC/POS reference Ver. 10.01, p. 428-429).
|
||||
- Epson documents one: `GS ( H` fn 48, the process ID response. The host sends `1D 28 48 06 00 30 30 d1 d2 d3 d4` after the data. For print data, the printer answers `37 22 d1 d2 d3 d4 00` "when the printing is completed" (ESC/POS reference Ver. 10.01, p. 428-429).
|
||||
- The TM-T88IV Technical Reference Guide (Rev. C) lists the command, and so does the TM-T88V's (Rev. E). Epson's model pages list it for the TM-T88IV to TM-T88VII, seen only in search extracts, since the pages refuse direct fetches. The TM-m50II's command list includes it too (docs/plans/printers.md, Investigation findings), still unverified over port 9100.
|
||||
- Limits:
|
||||
- If the host misses responses, the printer sends only the latest ID.
|
||||
@@ -282,24 +145,7 @@ PRINT ACKNOWLEDGEMENT ON EPSON PRINTERS
|
||||
- Even a perfect acknowledgement leaves a window between the printer finishing and the daemon recording it, so the store must be safe without one.
|
||||
- A side finding: the UB-E02 and UB-E03 boards turn automatic status on at power-on, and the `ESC @` that starts each job turns it off, as Epson's guides confirm (docs/plans/printers.md, Investigation findings). Until the first job after a power-on, the printer may send unrequested 4-byte status frames. `printer.Client.Status` and `Send` pass over them, by the fixed bits of a frame's first byte, so they read neither as "status unsupported" nor as an answer to `GS r 1`, and with confirmation each `Send` turns them off with `GS a 0`. Whether the frames reach a port 9100 client is unverified.
|
||||
|
||||
WHERE THE REPORTS DISAGREE OR ARE UNSURE
|
||||
- WAL bytes per job: about 15 KB (spike: small table, no pruning), 20-40 KiB (the key-value track's estimate) and 57.7 KiB (SQLite track: full history with pruning). The setups differ. The schema under RECORDS appended 46.1 KiB (11.46 frames) on leil-test-1.
|
||||
- Badger's size: +5.6-6.0 MB on a small test program, +7.9-8.4 MB in the daemon.
|
||||
- modernc's size: +3.8 MB (v1.60.1, SQLite track) and +4.0 MB (v1.63.0, spike).
|
||||
- An acknowledgement on the TM-T88IV: design.md ("The queue lives in memory, on purpose") says ESC/POS gives none; Epson documents one (`GS ( H` fn 48). It is unverified on this printer.
|
||||
- Stage 1a measured on Linux what no track could: device writes and flushes, and whether `SystemCallFilter=@system-service` allows what the store calls. Not measured by anything: an SD card, ARM hardware, ext4, ncruces' size, Pebble, and fsync counts by strace (they come from SQLite's docs and bbolt's code).
|
||||
|
||||
SOURCES
|
||||
- SQLite: sqlite.org/wal.html, /pragma.html, /fileformat2.html, /atomiccommit.html, /howtocorrupt.html.
|
||||
- modernc: pkg.go.dev/modernc.org/sqlite; its CHANGELOG, builder.json, fdatasync.go and ofd.go; gitlab.com/cznic/sqlite issues #132 and #177.
|
||||
- glebarez: github.com/glebarez/sqlite, its issues and commits; github.com/glebarez/go-sqlite. ncruces: github.com/ncruces/go-sqlite3, its releases, support matrix and VFS README.
|
||||
- GORM: gorm.io/docs/performance.html, /transactions.html, /migration.html. sqlc: docs.sqlc.dev.
|
||||
- bbolt: github.com/etcd-io/bbolt: README, CHANGELOG-1.5, db.go, tx.go, bolt_windows.go, internal/common/bolt_arm.go, the CI workflows.
|
||||
- Badger: github.com/dgraph-io/badger: options.go, db.go, memtable.go, value.go, skl/arena.go, docs/quickstart.md, docs/troubleshooting.md, the CI workflows, issue #2002; ristretto's z/file.go and z/mmap_windows.go.
|
||||
- Pebble: github.com/cockroachdb/pebble: README, options.go.
|
||||
- Linux writeback: docs.kernel.org/admin-guide/sysctl/vm.html, mm/page-writeback.c.
|
||||
- systemd: systemd.exec(5); the 90 s stop default is `DefaultTimeoutStopSec=` in systemd-system.conf(5), https://www.freedesktop.org/software/systemd/man/latest/systemd-system.conf.html. Docker's 10 s stop default: https://docs.docker.com/reference/cli/docker/container/stop/.
|
||||
- TypeID: github.com/jetify-com/typeid, README and spec.
|
||||
- Epson:
|
||||
- ESC/POS reference Ver. 10.01, `GS ( H`, mirrored in the TM-U210 manual: https://cellphone.manualsonline.com/manuals/mfg/epson/tmu210.html?p=428 (format and timing), ?p=429 (response, latest-only rule, clearing), ?p=430 (fn 49).
|
||||
- TM-T88IV TRG Rev. C: https://files.support.epson.com/pdf/pos/bulk/tm-t88iv_eng_trg_revc_02.pdf
|
||||
@@ -310,7 +156,6 @@ SOURCES
|
||||
- TM-T20 quick reference: http://www.novopos.ch/client/EPSON/TM-T20/TM-T20_eng_qr.pdf
|
||||
- ePOS-Print API User's Manual Rev. K: https://files.support.epson.com/pdf/pos/bulk/tm-i_epos-print_um_en_revk.pdf
|
||||
- The command and model pages under https://download4.epson.biz/sec_pubs/pos/reference_en/escpos/, such as gs_lparen_ch_fn48.html and tmt88iv.html (search extracts only; they refuse direct fetches).
|
||||
- Versions and go.mod files: proxy.golang.org.
|
||||
|
||||
## Overview
|
||||
|
||||
@@ -348,10 +193,10 @@ A job has one or more deliveries, one per printer it goes to (DELIVERIES AND SEV
|
||||
|---|---|---|---|---|
|
||||
| `queued` | on `Submit`, with its job, before the `202` | for ntfy jobs only (decision 5) | `queued` | queued again, in submission order |
|
||||
| (taken: rendering, waiting for the printer's slot, dialling, or between retries that never reached `sending`) | not stored; the stored state stays `queued` | – | `printing` | still `queued`: nothing was sent |
|
||||
| `sending`, attempt n | after the connection opens, before the first byte; the first sets `sent_at` | **yes** | `printing` | `uncertain`, never resent |
|
||||
| `done` | after the write returned in full | no | `done` | history |
|
||||
| `sending`, attempt n | after the connection opens and, with `printer.confirm`, the printer answered that it can print; before the first byte. The first sets `sent_at` | **yes** | `printing` | `uncertain`, never resent |
|
||||
| `done` | after the write returned in full and the printer answered the `GS r 1` sent after it (`printer.confirm`; without it, once the write returned) | no | `done` | history |
|
||||
| `failed` | a render error, every attempt failed, or a stored job that no longer decodes | **yes** | `failed` | history |
|
||||
| `uncertain` | at start, for each delivery found in `sending`; during a run, for a remote attempt whose outcome stays unknown (docs/plans/remote-printers.md, decision 17) | no | `uncertain` | history |
|
||||
| `uncertain` | at start, for each delivery found in `sending`; during a run, for an attempt written in full that the printer did not confirm within `printer.write_timeout`, and for a remote attempt whose outcome stays unknown (docs/plans/remote-printers.md, decision 17) | no | `uncertain` | history |
|
||||
| `cancelled` (phase 2b) | on a cancel, only for a delivery that has not reached `sending` | **yes** | `cancelled` | history |
|
||||
|
||||
- A dial failure writes nothing, because the mark is written only once a connection is open. Retries while the printer is offline cost no disk writes, and such a job stays safe to retry.
|
||||
@@ -374,7 +219,7 @@ STORE FAILURES (decision 11)
|
||||
|
||||
TELLING WHICH RECEIPTS PRINTED BEFORE A CRASH
|
||||
Each delivery's state tells what reached its printer:
|
||||
- `done`: written in full to the printer's connection. It printed unless the printer itself failed, as today.
|
||||
- `done`: written in full, and the printer answered the `GS r 1` sent after it, which it does only once it has run every byte before it. It printed unless the printer itself failed. With `printer.confirm` off, `done` says only that the operating system took the bytes, and a stop or a crash can still lose part of them.
|
||||
- `queued`: never reached the printer.
|
||||
- `failed`: reached the printer only if `sent_at` is set, and then part of it may have printed. The error does not tell: it is the last attempt's, so a first attempt that wrote part of the receipt and a second that failed to dial end with a dial error.
|
||||
- `uncertain`: may have printed. At start the daemon logs one WARN line per delivery that start turned `uncertain` (`Load` says which) with its `job_id`, `printer`, `source` and `ref`, never its content, and from phase 2 `/healthz` counts their jobs. A delivery found `uncertain` at an earlier start is not logged again. A remote attempt whose outcome stays unknown turns `uncertain` during the run, with a WARN line of its own (docs/plans/remote-printers.md, decision 17). A person checks the paper; a reprint is always a new request.
|
||||
@@ -463,7 +308,7 @@ type Store interface {
|
||||
- The waiting list is a slice in sequence order, guarded by the status lock, with a channel that wakes the worker. It replaces today's `jobs` channel, whose capacity is `Size`, so it can hold a restored backlog. `Pending` reads its length and the capacity check runs against it. If more jobs were stored than `queue.size` now allows (after the key was lowered), all of them load, and `Submit` answers `queue_full` until the backlog falls below the size.
|
||||
- `Submit` takes no context, here or in the server's `Jobs` and `ntfySubmitter`. `Add` runs on a background context: an insert that has begun finishes or fails, whatever the client does meanwhile.
|
||||
- `Meta` gains a flag, set by ntfy subscriptions only, that marks its `Ref` as unique at its source, and `Sequence`, a card's ntfy notification (RECORDS), which phase 2b's cancel matches. A unique `Submit` commits with a sync (decision 5). If the queue already holds that source and reference (decision 10), `Submit` stores nothing and returns the earlier job's status, or a status with only its ID and source once that was pruned.
|
||||
- `Sender.Send(ctx, data, beforeWrite func() error)`: `printer.Client` calls `beforeWrite` after the dial, before it computes the write deadline and writes, so a slow sync does not use up `write_timeout`. The queue passes a function that calls `Store.Sending` for the delivery and sets its `sent_at` on the first attempt. The synced commit runs while the printer's connection slot is held, so a slow flush, or a checkpoint that commit triggers, also delays `/healthz` (3 s). The store checkpoints when `Idle` is called, so an automatic checkpoint seldom lands on a `sending` commit.
|
||||
- `Sender.Send(ctx, data, beforeWrite func() error)`: `printer.Client` calls `beforeWrite` after the dial, before it computes the write deadline and writes, so a slow sync does not use up `write_timeout`. The queue passes a function that calls `Store.Sending` for the delivery and sets its `sent_at` on the first attempt. The synced commit runs while the printer's connection slot is held, so a slow flush, or a checkpoint that commit triggers, also delays `/healthz` (3 s). The store checkpoints when `Idle` is called and enough commits have gathered, so an automatic checkpoint seldom lands on a `sending` commit.
|
||||
- Restore before start: serve.go opens the store, which takes its lock, and the queue loads it before `group.Go` starts the worker, the server and the subscribers. So restored jobs go ahead of new ones, the sequence continues from the highest stored, restored statuses answer at once, and a second instance on the same directory fails before it reads the `ntfy-<name>.json` files.
|
||||
- `Load` returns no content, so a backlog of up to `queue.size` × 4 MiB is not read into memory at start; the worker reads each job's content when it takes it. A stored job that no longer passes strict `DecodeJob` (after a downgrade or a field change) then fails with an error, logged by `job_id`, and is never dropped silently. Its content is never logged.
|
||||
- `Durable()` decides shutdown: with a durable store, waiting jobs and a job that has not reached `sending` stay queued; with the memory store, shutdown is as today.
|
||||
@@ -498,30 +343,31 @@ CREATE TABLE content (
|
||||
job BLOB NOT NULL -- the receipt.Job as JSON
|
||||
);
|
||||
CREATE TABLE refs ( -- unique jobs, for the duplicate check
|
||||
kept INTEGER PRIMARY KEY, -- the order the references were kept in
|
||||
source TEXT NOT NULL,
|
||||
ref TEXT NOT NULL,
|
||||
id TEXT NOT NULL, -- the job_ TypeID, which sorts by time
|
||||
PRIMARY KEY (source, ref)
|
||||
) WITHOUT ROWID;
|
||||
id TEXT NOT NULL, -- the job_ TypeID
|
||||
UNIQUE (source, ref)
|
||||
);
|
||||
```
|
||||
|
||||
- A job's own fields live in `jobs`, and each delivery's, those of one printer, in `deliveries`. Until the printers plan lands, every job has exactly one delivery, for the printer `default`. A multicast or broadcast job later gets one row per printer; an anycast job has one, whose `printer` the `sending` mark sets to the printer used (DELIVERIES AND SEVERAL PRINTERS).
|
||||
- The `jobs` row is written once, with its deliveries and its content in the insert's commit, and is deleted at pruning. A status update rewrites only its delivery's row.
|
||||
- IO: the split costs one more small row per job at insert, in the same commit. `deliveries` is a b-tree of its own, so an insert can change one page more than with one table. On leil-test-1 this schema appended 11.46 WAL frames per text job, against 14.35 in the SQLite track's runs with one table (Investigation findings).
|
||||
- IO: the split costs one more small row per job at insert, in the same commit. `deliveries` is a b-tree of its own, so an insert can change one page more than with one table. On leil-test-1 this schema appended 11.46 WAL frames per text job, against 14.35 in the SQLite track's runs with one table.
|
||||
- Statuses are looked up in memory, so neither table needs an index on `id`, `state` or `job_seq`, and updates go by a delivery's `seq`. Each index left out is a b-tree fewer per insert. Pruning deletes a job's row and its deliveries' rows by their `seq`s, which the queue passes to `Finish`.
|
||||
- `Load` reads `deliveries` in `seq` order and attaches each row to its job by `job_seq` in memory.
|
||||
- `sequence` lets an ntfy `message_delete` find a waiting job after a restart too (phase 2b). It holds the topic and the sequence ID as `<topic>/<sequence ID>`, since ntfy's sequence IDs are per topic (`DELETE /<topic>/<sequence_id>`) and one subscription reads several topics; a topic name cannot hold `/`. It is written from stage 1d, so phase 2b needs no schema step and can cancel a job stored before it.
|
||||
- Content has its own table, so a status update never rewrites a body. `Load` reads each content's length, not the content.
|
||||
- `refs` is pruned to the newest 512 per source by `id`, apart from `jobs`, so pruning statuses never drops a reference. It is written only for unique jobs.
|
||||
- `refs` is pruned to the newest 512 per source in the order they were kept, by `kept`, as the queue forgets them, apart from `jobs`, so pruning statuses never drops a reference. Job IDs do not give that order: one minted after the clock was set back, as on a board whose clock does not run while it is off, sorts before those kept. A reference kept for an earlier job is dropped and kept again as the newest. `refs` is written only for unique jobs, and its unique index on `source` and `ref` is the only index in the store.
|
||||
- Times are Unix nanoseconds, so a status reads the same after a restart as before it.
|
||||
- A `seq`, a job's or a delivery's, can repeat after every row was pruned (`queue.history = 0`). Nothing outside the store's own rows refers to a `seq`, so that is harmless; `refs` holds job IDs, and a later counter, such as the acknowledgement's, keeps its own.
|
||||
- A delivery's `done` and the pruning of the oldest jobs share one commit.
|
||||
- One connection: the store takes a `*sql.Conn` at Open and holds it for its life, with the pool at one open and one idle connection and no lifetime limits, since `synchronous`, `locking_mode` and the lock belong to a connection. The pragmas are set by the connector (or the DSN's `_pragma`), so any new connection gets them too. An error on the held connection is a store failure (decision 11).
|
||||
- One connection: the store takes a `*sql.Conn` at Open and holds it for its life, with the pool at one open and one idle connection and no lifetime limits, since `synchronous`, `locking_mode` and the lock belong to a connection. The pragmas are run by the connector, in order, so any new connection gets them too; the DSN's `_pragma` list would run them sorted by name, setting `locking_mode` only after the first read. An error on the held connection is a store failure (decision 11).
|
||||
- The DSN is built through the connector API, or escaped, since `state.dir` may hold spaces, `#` or `?`.
|
||||
- Pragmas at open: `journal_mode=WAL`, `locking_mode=EXCLUSIVE` (decision 3), `synchronous=NORMAL`, `temp_store=MEMORY`, `journal_size_limit` of 4 MiB, `secure_delete=FAST`, and `auto_vacuum=NONE` before the tables exist. `wal_autocheckpoint` stays at 1000.
|
||||
- A synced commit runs `PRAGMA synchronous=FULL`, commits, and restores NORMAL in a defer, so a failed statement never leaves the connection at FULL. `Sending`, `failed`, inserts of unique jobs and, from phase 2b, `cancelled` commit this way.
|
||||
- Open, in order: create the file 0600 if it is missing; set the pragmas; run `PRAGMA quick_check`, which only reads (the whole file, 2-48 MiB at the sizes in the findings), since SQLite otherwise finds a damaged page only when a statement reads it, mid-run; refuse a `user_version` newer than the binary knows, naming both; run each schema step in one synced transaction that also sets `user_version`; write once, so a second instance fails at once.
|
||||
- `Idle` runs `PRAGMA wal_checkpoint(PASSIVE)`.
|
||||
- Open, in order: create the file 0600 if it is missing; set the pragmas; run `PRAGMA quick_check`, which only reads (the whole file, about 2 MiB with text jobs and up to about 48 MiB with images, as the research measured), since SQLite otherwise finds a damaged page only when a statement reads it, mid-run; refuse a `user_version` newer than the binary knows, naming both; run each schema step in one synced transaction that also sets `user_version`; write once, so a second instance fails at once.
|
||||
- `Idle` runs `PRAGMA wal_checkpoint(PASSIVE)` once 64 commits, about 20 jobs, have gathered since its last checkpoint. The queue is idle after nearly every job when receipts come one at a time, and a checkpoint costs three syncs (the log, the database, and the log's header at the next commit), so one each time would make a job cost four.
|
||||
- `sqlite.Fdatasync(true)` on Linux. The unit's syscall filter allows `fdatasync`, and on leil-test-1 it cut a synced commit's median from 5.0 to 2.9 ms with the same device writes and flushes (stage 1a).
|
||||
- Nothing else in the process opens the file, since any close drops SQLite's locks.
|
||||
- A test hook, an unexported function the package's own tests set, runs inside each commit and reads `PRAGMA synchronous`, so tests see which commits ran at FULL.
|
||||
@@ -597,7 +443,7 @@ THE PREVIEW IMAGE (phase 3)
|
||||
|
||||
### Stage 1a: Measurements on leil-rig
|
||||
|
||||
Done on 2026-12-01. The numbers are under Investigation findings, MEASURED ON LEIL-TEST-1.
|
||||
Done on 2026-12-01. Its numbers are in design.md ("Jobs survive a restart", Measured).
|
||||
|
||||
**Goal.** Measure on Linux what the workstation cannot: device writes and flushes per job, the time of a synced commit and of a checkpoint, and whether the unit's sandbox allows the calls the store makes. The owner has no ARM boards, so nothing runs on ARM hardware (decision 2).
|
||||
|
||||
@@ -627,7 +473,7 @@ On leil-test-1, otherwise idle, since the device counters include every process'
|
||||
**Tests.** A 20-job run on the workstation (Windows, without device counters) shows that the probe works. Nothing heavier runs there.
|
||||
|
||||
**Done when.**
|
||||
- The probe has run on leil-test-1, and its numbers are in Investigation findings.
|
||||
- The probe has run on leil-test-1, and its numbers are in design.md.
|
||||
- If a call fails with EPERM, or the numbers differ much from the findings, the owner has looked at decision 1 again.
|
||||
|
||||
### Stage 1b: `[state] dir`
|
||||
@@ -749,6 +595,8 @@ internal/app/uberbringer
|
||||
|
||||
### Stage 1d: The SQLite store
|
||||
|
||||
Done on 2026-12-01. Its third paper check showed that `done` meant only that the operating system held a job's bytes: with the cover open, Windows took a whole 128 KB receipt at once, and only its first third printed. The owner had the fix built in this stage: the client opens each connection with `DLE EOT 1`, `GS a 0` and `GS r 1`, drops whatever the printer sent before the `DLE EOT` answer, and sends nothing until the printer answers the `GS r 1` and a second `DLE EOT 1` says it is online, and it asks `GS r 1` again after the job, recording `done` only on the answer and `uncertain` without it (design.md, "Done means the printer confirmed it"; `printer.confirm`). Paper checks 4 and 5 passed on it: a receipt read `done` on the printer's answer, and one sent with the cover open waited, with nothing sent, and printed once when the cover closed. Checks 6 and 7 passed on the final client: a receipt printed once with nothing extra and read `done` on the answer, and after a power cycle, with `/healthz` polled every 250 ms, one sent with the cover open waited 15.7 s with nothing sent and printed once when the cover closed. Check 8 passed with the connection opened that way: a receipt printed once with nothing extra and read `done` on the answer.
|
||||
|
||||
**Goal.** Jobs survive a restart on disk. A restart never prints a receipt twice. Each job costs one synced write, plus one per failure and per ntfy job.
|
||||
|
||||
**Work.**
|
||||
@@ -797,7 +645,7 @@ internal/pkg/jobstore. Each test calls `t.TempDir()` before `Open`, so on Window
|
||||
- The file is 0600 (Linux only).
|
||||
- `Finish` drops jobs, their deliveries and content in the same transaction.
|
||||
- `Load` turns every delivery in `sending` into `uncertain` in one commit, leaves the other deliveries of the same job alone, returns the deliveries it turned, and returns content lengths without content. A second `Load` turns and returns none.
|
||||
- `refs` keeps the newest 512 per source and outlives the pruning of their statuses.
|
||||
- `refs` keeps the 512 kept last per source, whatever their job IDs, and outlives the pruning of their statuses.
|
||||
- A damaged page in the middle of the file fails `Open` with an error naming the file, and so does a damaged header. A `user_version` newer than the binary's is refused, naming both.
|
||||
- With `wal_autocheckpoint=0`, 30 jobs: WAL frames per job are logged and held to an upper bound with headroom, not to an exact number.
|
||||
|
||||
@@ -815,7 +663,7 @@ Crash tests use fault injection like the above, never kill loops or soaks on the
|
||||
2. With the printer switched off, a receipt is sent, and the daemon is stopped while the log still shows `send failed, retrying`. With the printer on again, the daemon starts, and the receipt prints once.
|
||||
3. A receipt is killed while its write waits (`systemctl kill -s KILL uberbringer` on a host, `taskkill /F` on the workstation); one way to make the write wait is an open cover and a receipt larger than the printer's buffers, such as one with an image. After the restart it reads `uncertain` and is not sent again; once the cover is closed, what the printer had buffered prints, and only once.
|
||||
- The full gate passes, with the arm builds and the libc check; govulncheck is clean; the snapshot builds every target.
|
||||
- The Investigation findings section is deleted from this plan, now that design.md holds what it decided.
|
||||
- The Investigation findings section is folded into design.md, apart from what phase 5 needs (Findings for phase 5).
|
||||
|
||||
### Phase 2: The API for stored jobs
|
||||
|
||||
@@ -1058,7 +906,7 @@ Docs
|
||||
- escpos: `ProcessIDRequest(id)` and a frame reader for real-time status, automatic status, process IDs, clear responses and unknown blocks.
|
||||
- printer: a confirmed send with its four outcomes, using the count `Write` returns. `Status` reads through the frame reader and skips frames it did not ask for.
|
||||
- `/healthz` while a job holds the connection.
|
||||
- Config: `printer.confirm` (`auto`, `process_id` or `off`) and `printer.confirm_timeout`, in each printer's table (docs/plans/printers.md decision 3). Status gains `confirmed_at`. The store keeps the ID counter.
|
||||
- Config: a key for the process ID beside stage 1d's `printer.confirm`, a bool that stays as it is, and `printer.confirm_timeout`, in each printer's table (docs/plans/printers.md decision 3). Status gains `confirmed_at`. The store keeps the ID counter.
|
||||
- The partial-write choice (decision 23), once the confirmed send reports the bytes written.
|
||||
- ntfy state in the store (decision 22).
|
||||
- An `Idempotency-Key` header on `POST /api/v1/print`.
|
||||
|
||||
@@ -354,7 +354,7 @@ WHERE THE REPORTS DISAGREE OR ARE UNSURE
|
||||
- A rule's runtime error in the log: with its message (Expr track), or never (config track), since the message quotes env values. Decision 15.
|
||||
- The name pattern: the module names' form (Expr track) or the subscriptions' (config track). Decision 2.
|
||||
- Whether an offline printer behind a UB-E02 or UB-E03 still accepts the connection and holds the bytes: an inference, unverified. Decision 23's paper check, before stage 3.
|
||||
- `GS r 1`'s fixed bits: Epson's page contradicts itself. Stage 4a's first step reads what the TM-T88VII sends.
|
||||
- `GS r 1`'s fixed bits: Epson's page contradicts itself. Stage 4a's first step reads what the TM-T88VII sends. Job confirmation (job-store stage 1d, `printer.confirm`) takes only an answer with bits 4 and 7 clear, as the TM-T88IV sends; a model that answers otherwise needs `printer.confirm = false` until that changes.
|
||||
- Whether automatic-status frames reach a port 9100 client: unverified (job-store.md says the same). Stage 4a's first step.
|
||||
- Whether the TM-T88VII's TM-T88V emulation brings near-end back to `DLE EOT 4`: unknown.
|
||||
- Which board the owner's TM-T88IV has, a UB-E02 or a UB-E03, and how its DIP switch 1-2 is set: unchecked.
|
||||
|
||||
Reference in new issue
Block a user