Skip to content

TC-2917: persist SQL and DuckDB artifacts in bounded parts - #647

Merged
samgbafa merged 33 commits into
mainfrom
feat/tc-2917-train
Nov 24, 2026
Merged

samgbafa merged 33 commits into
mainfrom
feat/tc-2917-train

Conversation

@samgbafa

@samgbafa samgbafa commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Changes

Add bounded checkpoint and WAL artifact parts for SQL and DuckDB, including replacement lifecycle, cache cleanup and DuckDB disk headroom handling.

Includes the complete heads of #589 (aa76999b), #591 (c251b720), #594 (726ee4fa), #597 (c441b371), #601 (327b09c0), #603 (a485ceb9) and #604 (7e036fcc). Each is an ancestor of this train; those PRs are closed as included here.

Defaults and remaining enablement work

  • chunked_artifacts remains false. Artifact/account capacities remain zero and must be configured explicitly before enabling parts.
  • The default single-row read and write limits remain 64 MiB. No compose or production configuration change.
  • Existing oversized legacy artifacts still fail their single-row read limit even with parts enabled. This train does not include a converter. Already-published parts load without that single-row limit, including with the switch off.
  • Conversion, SQL export buffering and ceiling-load acceptance remain prerequisites to enabling parts. See docs/operations/chunked-sql-artifacts.md.

Client-visible behavior

With parts enabled only:

  • SQL writes racing another replacement of the same database: previously 402 storage-quota-exceeded (StorageLimit), now 503 unavailable_retryable (ReplacementBusy) with Retry-After: 1. The SDK maps this to NODE_BUSY and retries it.
  • DuckDB replacement contention: 503 now starts unavailable_retryable: and carries Retry-After: 1, rather than Database artifact replacement is temporarily unavailable without that header. The SDK maps the former to NODE_BUSY, rather than NETWORK_ERROR.
  • Parts capacity refusals: previously 402, now 507 sql_database_size_limit. The SDK 507 storage-full mapping must be released before enabling parts (TC-3354).
  • Flag-off behavior is unchanged.

Migrations

Two PostgreSQL-only, forward-only migrations, each with a 500 ms migration lock wait:

  • m20261113_020000_artifact_chunk_wal: WAL replacement references and lifecycle functions.
  • m20261113_030000_artifact_part_lifecycle: database-local reclamation index and bounded reservation contention handling.

Verification and CI fixes

At 6697e849, CI's DuckDB leg passed 1,225 tests and failed the owner-reservation race test because bounded lock contention returned ReplacementBusy, while the test required StorageLimit. The test now permits that transient race result but requires a subsequent non-concurrent reservation to fail specifically with StorageLimit, preserving the quota invariant.

The same CI run's unit leg timed out after 25 minutes while the actor-retirement test was running. That unmodified test passed the targeted local PostgreSQL run; no cause for the CI timeout has been established.

A targeted local run exposed a separate race between aborted-upload cleanup and explicit collection in the delete-during-staging test. Collection is bounded and best effort, so the test now requires eventual zero reserved bytes and zero credit within five seconds instead of assuming its first collection pass cannot contend.

The predecessor-progress test now retries only ReplacementBusy within a bounded deadline: reclaiming its own 40 MiB predecessor can exceed one two-second request budget. It still requires revision 3, exact quota accounting, and no collection of the other owner's locked garbage.

At ae6e4944541bb384a4d894b6fe215350d0f7dece (11-19), main was merged without conflicts; main has since moved (TC-2871 purge fence, TC-1491 sql_store). Head 04f4fe78 merges main b3f945bc (7 conflicted files resolved, both sides kept) and adds: WAL parts read by the shadow source (read_chunk_layout part filter), buffers_in_memory on the shadow sinks, DuckDB headroom accounting (an in-flight claim that shrinks as the write's own files grow, so concurrent writes do not trip 507), snapshot copies beside the cache file instead of $TMPDIR, and three new tests (purge racing a WAL publish, shadow read of a parts row with a WAL, two concurrent DuckDB writes with the headroom watcher). On isolated local PostgreSQL 18 at the earlier head, all three then-changed tests passed with no skips. Earlier targeted runs also passed the over-64-MiB parts/legacy read-cap distinction, flag-off reopen/write, WAL lifecycle, quota and actor-retirement tests. cargo clippy -p tinycloud-core --lib --tests -- -D warnings and cargo fmt --all -- --check passed; the dependency proc-macro-error2 emits an existing future-incompatibility notice.

Exact-head CI at the older head ae6e4944 was not green: the CI and Docker workflows were cancelled at approximately 17:41 UTC on November 19 (CI at the current head is running; a complete successful run remains required). The test aggregate failed with TEST_RESULT=cancelled; the unfinished test legs did not report a code failure. Workspace build, fmt, at-rest, threshold, renewal scheduler, real-data and other completed gates passed. A complete successful CI run remains required before merge.

test-ruling: carried TC-2917 train test edits remain as documented in the original PRs. The owner-reservation test distinguishes retryable owner-lock contention from capacity refusal and requires capacity refusal after publication. The delete-during-staging test retains stale-publication and zero-quota invariants while allowing contention with automatic abort cleanup. The predecessor-progress test retries bounded own-predecessor contention while preserving cross-owner isolation and exact revision/quota assertions.

Logship note (Naomi 10:12Z): the warn at tinycloud-core/src/duckdb/database.rs ("Database cache cleanup failed", event duckdb_cache_cleanup_failed) is DEMOTED to debug! in this PR. The logship gate requires every shipped warn/error to map to a named event_code, none fits, and a new code needs a tinycloud-dev schema commit plus a log-shipper pin and N0 template-hash change (deploy-lock path). Cost: no shipped operator signal when DuckDB cache cleanup fails (silent, as on main). Follow-up TC-4190 adds the code and promotes it back to warn!, riding the ingress-pin compose deploy.

Head 830a0358 is the demotion only (one file) on top of 04f4fe78. Head 6d580eb7 (via a97b6ab5, plus two docs-only wording commits) fixes the opus review: the two new migrations are re-runnable (IF NOT EXISTS / OR REPLACE / guarded FK; CHANGELOG notes rollback is fix-forward only), the DuckDB headroom 507 logs ErrorCode::DiskHeadroom, and two comment/doc fixes. No existing test edited in that commit.

test-ruling: TC-2917 type-only: Vec -> bytes::Bytes in 4 existing tests (service.rs:7598, chunks.rs:2096/2702/2711) after main's save/save_delta signature change; no assertion changed (Naomi 09:33Z)
test-ruling: TC-2917 m20261121_000000_session_retire tests: assert_up_down_up takes forward_only_tail; PG leg migrates through len-2, rolls back only what's after session_retire, then up() applies #647's two forward-only migrations; SQLite passes 0; no assertion changed (Naomi 09:52Z)

Details: TC-2917

Evicted actors rename their cache files aside under the registration
and delete them after releasing it; leftover spill directories no longer
block hydration or discard; failed imports leave no staged or imported
file; shutting down an exited actor succeeds.
Renaming aside needs the held registration, releasing it consumes the
entry, and only the released form can be deleted: reordering either
step no longer compiles (#601 review C2).
Writes reserve their local file size against the node-wide disk budget
and are refused (507 duckdb_disk_headroom) below storage.duckdb.min_free_bytes;
a running write is interrupted if free space drops below the floor; a
statement spilling past storage.duckdb.max_temp_directory_size is the
same 507. Defaults (2 GiB floor, 1 GiB spill) apply only with chunked
artifacts enabled. At startup the node removes DuckDB spill directories
and staging or renamed-aside cache names left by a stopped process.
… declare buffering

Review of #582: a new test pins that a parts read falling back to the
single-row layout releases its slot first and keeps the legacy cap and
slot; the streaming read in the in-memory slot test is bounded so a
regression fails instead of hanging; ArtifactSink::buffers_in_memory has
no default, so a new or wrapping sink must declare it; docs say the
capacities are checked at write time and name the in-memory case
correctly.
…AL fast path and same-length reuse

Review of #594: with no recorded lineage a WAL is attached only to the
checkpoint it was verified against; new tests pin that repeated small
writes stay on the WAL path and that a same-length rewrite of freed
blocks recovers exact rows.
…d by the parser

Review of #603: a write refused for headroom drops the database's local
copy, so a COMMIT interrupted after it committed (or earlier statements of
a batch) is never kept or saved later; a query whose SQL writes reserves
and runs under the watcher; the watcher keeps interrupting while free
space is short; the startup sweep does not descend into linked space
directories; with chunked artifacts the node refuses a DuckDB floor below
the artifact ceiling. Tests adopted from the review.
…eck test

Review of #603 (32cdd06): a query counts as read-only for the disk floor
only when every statement is a plain query or an EXPLAIN that does not
run its statement, so an admin's PREPARE/EXECUTE is held to the floor;
requests are not classified when no disk limit is set; the limit defaults
key on the configured flag, like the startup check, which now has a test;
docs say the floor cannot be 0 while parts are on and that refused SQL
answers 507 below the floor.
# Conflicts:
#	CHANGELOG.md
#	tinycloud-core/src/database_artifacts/chunks.rs
#	tinycloud-core/src/migrations/mod.rs
Overlap with #597: #597 already carries #594's first commit (670c5e9,
adapted to the owned transfer path), so the conflicting hunks in
duckdb/database.rs, duckdb/service.rs and CHANGELOG.md keep #597's version.

#594's second commit (726ee4f) is not in #597 and is carried here:
the legacy WAL path binds an unrecorded lineage to the checkpoint it was
just verified against, plus its two tests
(duckdb_repeated_small_writes_stay_on_the_wal_path,
duckdb_same_length_rewrite_of_freed_blocks_recovers_exact_rows).
Train resolution of a semantic conflict between #589 and #604.
#589 turned read_chunk_layout's `checkpoint_part_id IS NULL -> Ok(false)`
(read the row as legacy, as on main) into ArtifactChanged; #604's
postgres_chunked_fallback_to_legacy_holds_no_parts_slot pins the fallback
and failed on CI. Keep main's fallback: such a row is a legacy row, and the
legacy read's revision/hash guard still restarts a stale read.
- DuckDbError::Unavailable (a write racing another checkpoint replacement) now reads
  `unavailable_retryable: ...`, so the node's 503 carries Retry-After: 1 and the
  js-sdk classifies it NODE_BUSY, as for SQL's ReplacementBusy.
- New tests: core duckdb::service::replacement_busy_tests (mapping, and a real
  PostgreSQL race through DuckDbService); node routes::duckdb_replacement_busy_tests
  (503 + body prefix + Retry-After through the real fairing, SQL alongside).
- CHANGELOG: client-visible statuses for chunked-artifact contention and capacity.

Agent: a52900e0-cafb-4057-b626-cc211f9a25ef
- Moved replacement_busy_tests.rs to duckdb/service/ and dropped its #[path]
  attribute. did_resolution_single_entry exempts #[cfg(test)] out-of-line modules
  by their standard path; under #[path] it scanned the file as production code and
  flagged its offline DID_METHODS key generation.

Agent: a52900e0-cafb-4057-b626-cc211f9a25ef
@samgbafa
samgbafa marked this pull request as ready for review November 19, 2026 16:42
@samgbafa samgbafa changed the title TC-2917: chunked artifact parts train (#589 #591 #594 #597 #601 #603 #604) TC-2917: persist SQL and DuckDB artifacts in bounded parts Nov 19, 2026
- Require quota refusal after racing reservation contention clears.
- Preserve deletion and cross-owner progress invariants across bounded collection passes.
- Document the remaining oversized-legacy conversion gap; production defaults unchanged.
test-ruling: reservation contention may be ReplacementBusy before capacity is checked; require StorageLimit after the winning publication. Abort cleanup and predecessor collection are bounded and best effort; retain stale-publication, zero-quota, revision and cross-owner isolation assertions across bounded retries.
# Conflicts:
#	CHANGELOG.md
#	tinycloud-core/src/database_artifacts/chunks.rs
#	tinycloud-core/src/duckdb/service.rs
#	tinycloud-core/src/migrations/mod.rs
#	tinycloud-core/src/sql/service.rs
#	tinycloud-node-server/src/lib.rs
#	tinycloud-node-server/src/routes/mod.rs
…droom accounting; snapshot copies on the cache fs

- chunks.rs/database_artifacts.rs: read_chunk_layout takes the part to read; read_part_chunks reads a parts row's checkpoint or WAL (was checkpoint only).
- shadow/source.rs: FileSink and RangeSink define buffers_in_memory (false).
- duckdb/disk.rs, durability.rs, service.rs: bytes a write has written leave its claim (watcher), after the statement the claim is the snapshot-copy need; consumed once the copy is on disk.
- duckdb/database.rs: snapshot copies are made beside the cache files (the filesystem headroom measures); leftovers are swept at startup.
- node-server lib.rs: comment on the chunked-artifacts floor now matches the accounting.
- New tests: WAL publish vs purge fence (PG), shadow source reads a parts row with a WAL (PG), concurrent DuckDB writes with the watcher on.
- test-ruling: TC-2917 type-only: Vec<u8> -> bytes::Bytes in 4 existing tests (service.rs:7598, chunks.rs:2096/2702/2711) after main's save/save_delta signature change; no assertion changed (Naomi 09:33Z)
…d-only PG migrations

- assert_up_down_up takes forward_only_tail; the PG leg migrates through len-2, rolls back only what is applied after session_retire, and the final up applies the two forward-only migrations.
- test-ruling: TC-2917 m20261121_000000_session_retire tests: assert_up_down_up takes forward_only_tail; PG leg migrates through len-2, rolls back only what's after session_retire; assertions unchanged (Naomi)
…unnamed warn

- logship-coverage requires every shipped warn!/error! to map to a named event_code; this line has none in the pinned schema.
- Message and event field unchanged; a follow-up adds the code and promotes it.
…code, comment/doc fixes

- chunk_wal.sql / part_lifecycle.sql: ADD COLUMN IF NOT EXISTS, guarded FK, CREATE OR REPLACE FUNCTION, CREATE INDEX IF NOT EXISTS, so a deregister-only rollback (TC-3755) can go forward again; down stays forward-only.
- New PG test runs Migrator::up after deleting both seaql rows.
- DuckDB DiskHeadroom logs ErrorCode::DiskHeadroom like SQL.
- Comment/doc: chunked WAL publish writes no mark; DuckDB floor 0 exception; CHANGELOG rollback note.
… proves

- Say re-running up after deleting BOTH seaql rows is tested and booting the previous image is proposed but untested.
…k test proves

- Same wording as the CHANGELOG: up re-run after deleting BOTH rows is tested; booting the previous image is proposed but untested.
@samgbafa
samgbafa merged commit 7da17fe into main Nov 24, 2026
38 of 40 checks passed
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.

1 participant