Skip to content

0.3.10:1: store result databases nodatacow - #6

Merged
MattDHill merged 2 commits into
mainfrom
fix/nocow-result-databases
Nov 9, 2026
Merged

MattDHill merged 2 commits into
mainfrom
fix/nocow-result-databases

Conversation

@MattDHill

@MattDHill MattDHill commented Nov 9, 2026 •

Copy link
Copy Markdown
Member

A StartOS 0.4.1.2 server running Monetary View's first full scan had its package-data btrfs volume forced read-only, which took every service down. Kernel 7.2.11, with 522 GiB unallocated and no I/O or checksum errors. The abort came from the app's scanner thread fsyncing (btrfs_sync_file → btrfs_log_inode → log_csums → btrfs_del_csums, Transaction aborted (error -28)), with metadata space reservations at 7.9 GB against a 7.5 GB metadata allocation, almost all of it the delayed-refs reserve.

The scan writes a multi-GB SQLite WAL database in small random chunks every 2 s. On copy-on-write btrfs that fragments the file into a vast number of extents, and every fsync then logs their checksums. The btrfs failure to grow metadata into free space is a kernel problem that this doesn't fix, but this workload is what drove it.

  • nocow.py (copied into the image) sets FS_NOCOW_FL on /data, so the result databases the app creates inherit it. Given network directories, it byte-copies any copy-on-write files into a nodatacow directory and swaps it in. It checks free space first and recovers an interrupted swap. A failed copy or swap removes the staging copy, puts the original directory back, logs, and lets the service start on its data unchanged. Only when the original directory cannot be put back does it fail the migration, keeping both directories for recovery, rather than start with no data.
  • Install and restore set the attribute on /data (init/noCowData.ts). The 0.3.10:1 migration sets it and rewrites the existing /data/<network>/ directories once.
  • The databases lose btrfs checksums. They are rebuildable from the node and already excluded from backups.

Verified on a StartOS 0.4.1.2 box (kernel 7.2.11): the script on a btrfs scratch directory (attribute set, content byte-identical, second run a no-op, new files inherit it), then 0.3.10:0 installed from community-beta and scanned, and updated to this build. Every file under /data ended up C, set from inside the subcontainer through the idmapped mount, and the app reopened its database and resumed scanning. Injected failures (copy, promotion rename, rollback rename, and recovery of an interrupted swap, with and without a failing rename) each end in the outcome above. Not exercised: a rewrite of a database large enough to take minutes, and aarch64/riscv64.

🤖 Generated with Claude Code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@helix-white helix-white left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Two failure paths need fixing before this migration ships:

  • P2 — Restore the live directory before treating a failed swap as non-fatal (nocow.py, the two renames). If os.rename(path, old) succeeds and os.rename(tmp, path) raises (for example, a filesystem metadata-allocation error), main() catches the error and completes the migration successfully with /data/<network> missing. The scanner then creates a fresh database there (upstream/bitcoinmonetaryview/store.py), abandoning the existing scan in .<network>.old. The migration does not run again on ordinary starts. Roll back the first rename before continuing; if rollback fails, fail the migration rather than start with missing data. The same rule must cover failure to restore an interrupted swap at the beginning of rewrite(). The proposal records that startup-safety contract at the recovery helper; the PR's unconditional “never fails a start” statement should be qualified accordingly.

  • P2 — Clean up an incomplete staging copy on conversion failure (nocow.py, the copy loop). A write/fsync failure leaves .<network>.nocow behind while the migration reports success. This can retain almost another database's worth of disk space indefinitely, and these hidden directories are not excluded by startos/backups.ts, so subsequent backups can include the supposedly excluded scan results. The proposal removes the staging directory on handled conversion failures while preserving recovery artifacts if restoring the live directory fails.

Proposed changes: 4861637...proposal/6

The head merges cleanly into main; existing CI passed discovery and x86/arm/riscv builds. Review and proposal are code-only, not executed. Please verify adopted changes with injected copy, promotion-rename, and recovery-rename failures.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@MattDHill
MattDHill requested a review from helix-white November 9, 2026 00:52

@helix-white helix-white left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Both earlier findings are resolved at b7c39dd. Failed promotion restores the original live directory before continuing; failed rollback or interrupted-swap recovery propagates a fatal error and preserves recovery artifacts. Handled conversion failures remove the staging copy. The startup-safety contract is recorded at the recovery helper, and the PR description now reflects it.

The adopted code matches the proposal. The head merges cleanly into main; discovery and all x86/arm/riscv build checks passed (the separate Retarget Build job was skipped). Reviewed code only, without executing the package; the author reports the requested fault-injection cases passed. No remaining blockers.

Helix-Harness: pi
Helix-Model: openai-codex/gpt-6.1-sol

@MattDHill

Copy link
Copy Markdown
Member Author

@MarkSierra, could you look this over before we merge? It touches your users' scan data, so we'd like your eyes on it first.

Some context: a StartOS user's data drive went read-only overnight while Monetary View's first full scan was running, and every service on the server went down with it. The kernel trace points at the app's scanner thread: an fsync of the result database aborted a btrfs transaction. The underlying fault is a btrfs bug (it reported "no space left" with 522 GiB free), and we're tracking that on the StartOS side. But the scan's write pattern is what drove it: a multi-GB SQLite WAL database rewritten in small random chunks every couple of seconds. On copy-on-write btrfs, that fragments the file badly.

This PR stores the result databases without copy-on-write (chattr +C). New installs and restores get it on /data before the app creates anything. The 0.3.10:1 update copies existing databases once into nodatacow files, so nobody loses scan progress. The PR description has the details, including how it handles failures and what we tested.

Things worth your judgement:

  • Whether you'd rather handle this in the app itself (setting FS_NOCOW_FL on the data directory when Store creates it) instead of in the wrapper. That would also cover other btrfs hosts, and we'd drop nocow.py here.
  • The trade-off: nodatacow files lose btrfs checksums. That seemed fine since the results are rebuildable and already excluded from backups, but tell us if you see it differently.
  • If you can, try the update on a box with a large existing scan. We've only exercised small databases, so a timing figure for the one-time copy would be useful.

@helix-white

Copy link
Copy Markdown
Member

One migration detail for the upstream option: setting FS_NOCOW_FL on the directory covers newly created databases, but does not convert existing database files. If this moves upstream, existing installations still need the one-time rewrite here or an equivalent upstream migration before nocow.py can be dropped. My approval of b7c39dd stands; awaiting @MarkSierra’s feedback on the approach and large-scan update.

@MarkSierra

Copy link
Copy Markdown

Thanks Matt, and sorry this took a server down. I've looked through the change and I'm happy with it: it only touches Monetary View's own data, the swap and rollback are careful, and losing btrfs checksums is fine since the results are rebuildable and not in backups.

Please merge it as is. I'd keep the migration in the package, since only it can rewrite existing databases.

For the next app release I'll also set nodatacow on the data directory from the app itself, so other btrfs hosts are covered, and reduce how often the full scan forces writes to disk (fewer commits and larger WAL checkpoints), which should take a lot of pressure off btrfs.

I have a finished mainnet scan on my StartOS box, so I'll time the update once it's on Community Beta and post the result here.

MarkSierra pushed a commit to MarkSierra/BitcoinMonetaryView that referenced this pull request Nov 9, 2026
On a StartOS server, the first full scan drove btrfs into a fault that made the
data drive read-only (Start9-Community/bitcoinmonetaryview-startos#6): the scan
rewrites a multi-GB SQLite database in small pieces, which fragments it on a
copy-on-write file system, and each flush then logs the checksums of all its
extents.

- The writer marks its network directory nodatacow (chattr +C) before creating
  the database, so new files skip copy-on-write. Best effort: other file systems
  refuse the ioctl and nothing changes. Existing files keep their attribute.
- WAL autocheckpoint 16384 pages (64 MB) instead of 1,000, and the WAL file is
  kept at most 64 MB: with synchronous=NORMAL each checkpoint flushes the
  database, so a fast scan now flushes about 16 times less often.
- New CI job mounts a btrfs file system and checks the database files are
  created nodatacow.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01E7wzJobv8ioFW8hq9u9yGK
@MattDHill
MattDHill merged commit 8ad349f into main Nov 9, 2026
5 checks passed
@MattDHill
MattDHill deleted the fix/nocow-result-databases branch November 9, 2026 15:18
@MarkSierra

Copy link
Copy Markdown

Timing from my StartOS server (x86_64, kernel from StartOS 0.4.1.2), updating from 0.3.10:0 with a finished mainnet scan of 3.67 GB: the migration including the nocow rewrite took 19 seconds ("Migrating 0.3.10:0 -> 0.3.10:1 -> ..." at 17:12:12, "Initialization complete" at 17:12:31). The app reopened its database and resumed without rescanning. Thanks again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants