Skip to content

3.x.x: rehearse the 2.x.x cutover on a mainnet-shaped ledger and on a pg_dump, with each step's time and longest lock (#575) - #587

Merged
djh58 merged 3 commits into
3.x.xfrom
djh58/prism-b575-cutover-rehearsal
Sep 29, 2026
Merged

djh58 merged 3 commits into
3.x.xfrom
djh58/prism-b575-cutover-rehearsal

Conversation

@djh58

@djh58 djh58 commented Sep 29, 2026 •

Copy link
Copy Markdown
Member

Refs #575 (item 1: cutover rehearsal on 2.x.x data). Found #582.

What

Until now, migrations were tested only on the synthetic tests/fixtures/schema_2x fixture. This adds a cutover rehearsal on a mainnet-shaped 2.x.x ledger, built on #545's lifecycle scenario (#565):

  • Generator (tests/support/ledger_2x_seed.rs). A seeded, deterministic 2.x.x database from the frozen release SQL, in the shape of 3.x.x: pre-mainnet end-to-end scenarios on regtest, plus a nightly load-harness job #521's mainnet aggregates:

    • 257 identities over 76 days in three eras, 4 to 129 active a day;
    • a whale holding 82-98% of the work on most days over a Zipf(1.1) tail, and worker difficulty over three orders of magnitude;
    • bursty 5-minute rates (8/92/1.24, 25/394/0.65 and 1.3/17/0.77 mean/max/CV);
    • 13,566 blocks, each with a signed audit in one of the three body layouts;
    • PPLNS carry chains and payout entries; immature (pending), inactive, reversed and rejected blocks;
    • CTV rows, v1 and v2 terminal outbox rows, rollups, worker difficulty, an expired lease, and the credit-policy CHECK left NOT VALID.

    scale is 1 or 3. density thins share rows only: each kept row carries 1/density of the difficulty, so work, window weight and balances keep the mainnet shape. 3.x.x: Migration lifecycle: a 2.x.x schema through migration, mining and rollback (#487 S20) #545's seed moves into the same module unchanged; a scratch harness showed byte-identical rows and audit files on equal inputs, and 3.x.x: Migration lifecycle: a 2.x.x schema through migration, mining and rollback (#487 S20) #545's weekly test passes on it.

  • Driver (tests/support/cutover_rehearsal.rs). It runs check-config, migrate and import-audits as the migration guide does. Each step is timed, and a side connection samples pg_locks every 10 ms for the longest lock per step (split into migrate's transaction, 013, 017 prepare/validate/swap, and the statements migrate spent most time in). It then checks:

    • the recovery evidence (scripts/prism-recovery-evidence.*) is unchanged;
    • every 2.x.x table's counts and sums are unchanged;
    • every address's owed, lifetime and pending balance, by the API's SQL and through the running frontend's /public/v1/miners/<address>;
    • the payout window (2.x.x qbit_prism_window against the native snapshot) and its prior balances;
    • one frontend's readiness, first Stratum job and self-check.
  • Per PR: migration_rollback's cutover_rehearsal_tests::mainnet_shaped_2x_dump_rehearses_the_cutover_and_keeps_every_balance pg_dumps a PR-sized mainnet-shaped ledger (about 25k rows and 40 blocks, with a payout window of more than one 4,096-row page) and rehearses the dump through the operator path.

  • Weekly: live_regtest's weekly_mainnet_shaped_2x_ledger_cuts_over_measured_mines_and_restores_in_isolation. It writes 1x at 1/16 of the rows over a real regtest chain, mined along the history's own timestamps. It then cuts over with the measured steps and checks, and mines, reconciles and rolls back as 3.x.x: Migration lifecycle: a 2.x.x schema through migration, mining and rollback (#487 S20) #545 does.

  • Operator mode: make prism-cutover-rehearsal DUMP=... LEDGER_KEY=... restores a supplied pg_dump (custom, directory, tar or plain format) into a private, password-protected PostgreSQL 16 cluster it creates and removes, rehearses it, and prints a PASS/FAIL report (JSON with REPORT=). By default it runs a lab configuration against a node that serves the dump's own chain. ENV_FILE= runs it with the operator's reviewed production environment instead, so check-config and self-check check the real configuration. make prism-cutover-rehearsal-dump writes a mainnet-shaped dump. Both are documented in docs/prism-rust-migration.md#rehearse-the-cutover-on-a-snapshot.

  • test/e2e-scenarios.toml gains the cutover-rehearsal scenario (lane pr).

Finding

The rehearsal found that migrate cannot complete on a mainnet-sized 2.x.x ledger. 002_multi_instance.sql backfills qbit_prism_share_hashes with one statement over every accepted share, inside the migration transaction, under PRISM_DATABASE_STATEMENT_TIMEOUT_MS (default 15 s, at most 600 s):

  • at 1.03M shares, migrate fails with "canceling statement due to statement timeout";
  • at 600 s it passes, but 002 takes 38 s at 1.03M shares, so mainnet's 65.5M rows would take about 40 minutes.

Tracked in #582. Until it is fixed, the weekly scenario runs migrate at the 600 s maximum. It first asserts that migrate at the default timeout still refuses the ledger with this statement timeout and changes nothing, so the check fails as soon as #582 is fixed.

Rerunning the rehearsal on a fix branch

One command writes a mainnet-shaped dump and rehearses it:

make prism-cutover-rehearsal-generated DENSITY=0.0625 [SCALE=1] [STATEMENT_TIMEOUT_MS=15000] [REPORT=rehearsal.json] [KEEP=1 WORKDIR=dir]

The weekly scenario at a local density:

PRISM_REHEARSAL_WEEKLY_DENSITY=0.015625 cargo test --release -p qbit-prism-server --test live_regtest -- --ignored --exact migration_lifecycle_tests::weekly_mainnet_shaped_2x_ledger_cuts_over_measured_mines_and_restores_in_isolation --nocapture

Verification

  • Per-PR test: passes in 19.5 s on 2 CPUs. CI cost: about +20 s on one prism-native-postgres shard, and no new test binary, so the shards are not reassigned.
  • Mutants, each failing the per-PR test on its own check:
    • a migration deleting rollup rows fails "row counts and sums";
    • the native window reader dropping a share fails "payout window";
    • the API's pending balance reading mature rows fails "API balances".
  • Weekly scenario at 1/64 of the rows (1.03M rows, 13,566 blocks, release): passes in 378 s.
  • 3.x.x: Migration lifecycle: a 2.x.x schema through migration, mining and rollback (#487 S20) #545's weekly scenario still passes; migration_rollback passes in full.
  • make prism-cutover-rehearsal on generated dumps: 259k rows PASS in 122 s; 1.03M rows FAIL at the default statement timeout, as above, and PASS at 600 s. make prism-cutover-rehearsal-generated DENSITY=0.0625 (4.13M rows) PASSES at 600 s in 640 s, of which 002 takes 321 s (timings on 3.x.x: migration 002's share-hash backfill cannot complete on a production-sized ledger (statement timeout) #582).
  • cargo fmt --check, cargo clippy -p qbit-prism-server --all-targets -D warnings, shellcheck, check_gate_env_reads.py.

Workflow

The weekly job needs --release and a 120-minute timeout for the 1/16 scenario. That change to prism-load-nightly.yml is pushed separately (it must stay identical on main and 3.x.x).

🤖 Generated with Claude Code

… pg_dump, with each step's time and longest lock (#575)

#575 item 1. Migrations were tested only on the synthetic schema_2x
fixture. This adds a cutover rehearsal on #545's lifecycle scenario:

- A seeded generator for a mainnet-shaped 2.x.x database, from the frozen
  release SQL. The shape is #521's aggregates: 257 identities over 76 days in
  three eras, a whale at 82-98% over a Zipf tail, bursty 5-minute rates
  (peak 394/s), and 13,566 blocks with signed audits in the three body
  layouts, PPLNS carry chains, payout entries, immature, inactive, reversed
  and rejected blocks, CTV rows, v1/v2 outbox rows, rollups and worker
  difficulty. Scale is 1x or 3x; a share-density knob thins rows only.
  #545's seed moves into the same module unchanged (byte-identical rows and
  files on equal inputs).
- A rehearsal driver: check-config, migrate and import-audits exactly as
  the migration guide runs them, timed and with the longest lock per step
  sampled from pg_locks. It then checks the recovery evidence, every 2.x.x
  table's counts and sums, every address's balances (SQL and the running
  API), the payout window against the native snapshot, a first Stratum
  job and self-check.
- Per PR, migration_rollback runs it on a pg_dump of a PR-sized
  mainnet-shaped ledger. The weekly live_regtest scenario cuts over 1x at
  1/16 of the rows on a real regtest chain, then mines and reconciles as
  #545 does.
- `make prism-cutover-rehearsal DUMP=... LEDGER_KEY=...` rehearses an
  operator's snapshot in a private PostgreSQL cluster and prints a
  pass/fail report. `make prism-cutover-rehearsal-dump` writes a
  mainnet-shaped dump, and `make prism-cutover-rehearsal-generated` does
  both in one command. All three are documented in the migration guide.
- The weekly job builds in release and allows 120 minutes for the 1x/16
  scenario.

The rehearsal found #582: 002's share-hash backfill cannot finish within
the migrate transaction's statement timeout on a production-sized ledger.
The weekly scenario runs migrate at the 600 s maximum until it is fixed.
It first asserts that migrate at the default timeout still refuses the
ledger and changes nothing, so the check fails once #582 is fixed.

Refs #575

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58
djh58 requested a review from kiwidream September 29, 2026 17:46
@blacksmith-sh

This comment has been minimized.

…r invariants, operator environment, dump formats (#575)

Review findings on the cutover rehearsal:

- The lock sampler now reports continuous holds: a lock released and taken
  again is two holds, keyed by relation OID and full advisory key. Online
  steps are recognised only by the statement that is the call, so 016's
  whole-file text no longer reads as 017's swap. The per-PR test no longer
  requires the millisecond 013/017 steps to have been sampled.
- The native window is read as an operator tool, which registers no
  frontend heartbeat. It is compared in its own order with each share's
  credit policy, and must be non-empty. The per-PR ledger has 40 blocks, so
  the window spans more than one 4,096-row page of both readers.
- The table facts now cover the pool rollups row by row, the writer lease,
  worker difficulty's update time, and every balance recomputed from the
  carry rows. They are read again after the frontend has run. The per-PR
  test checks that the restore reproduces the seeded source.
- Operator mode: ENV_FILE runs the rehearsal with the operator's reviewed
  production environment. NODE_RPC could not pass: a mainnet node forces
  production mode, which refuses test keys. The private cluster requires a
  random password and is stopped even after a partial start. Directory,
  tar and plain dumps restore. Relative paths resolve from where make
  runs, and SCHEMA must be an identifier. The generator's pg_dump keeps the
  URL's other connection parameters.
- Weekly: #545's carry-continuation check now also runs on the mainnet
  ledger. The #582 check runs from 2M rows (the 1x/16 ledger), says when it
  is skipped, and requires that no scratch schema or native table
  survived. The generator bounds its immature blocks.

Refs #575

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@djh58 djh58 closed this Sep 29, 2026
@djh58 djh58 reopened this Sep 29, 2026
@djh58

djh58 commented Sep 29, 2026

Copy link
Copy Markdown
Member Author

Review ledger

Head dff4634: a091b1a with 3.x.x merged in (#584, #592). CI is green on it: all four Rust PRISM shards, the gated-test proof, the Python shards and the aggregate check.

Reviews run (proportionate for test and tooling work, as agreed): /code-review at high (10 findings), and one adversarial review of whether the invariants could pass on a wrong migration and of the #582 check (3 bugs, 7 weak checks, 3 nits). Fixed in a091b1a except as noted.

# Finding Resolution
1 The lock sampler merged a lock released and taken again into one long hold, and keyed relations by name Fixed: continuous holds, keyed by relation OID and the full advisory key
2 016's whole-file text named convert_swap and VALIDATE CONSTRAINT, so its time was labelled 017 swap Fixed: a step counts only when the statement is the call
3 The per-PR test required millisecond 013/017 steps to be sampled (flaky) Fixed: requires only the transaction, migrate (total), import-audits and the first job
4 The window check's Ledger::connect left a frontend heartbeat row the real cutover never writes Fixed: connect_tool
5 The window check was weak: re-sorted, no credit policy, empty passes, under one 4,096-row page Fixed: native order, credit policy, non-empty required; the PR ledger's window is now 4,178 shares
6 Directory and tar dumps did not restore; relative AUDIT_ROOT/WORKDIR broke; plain dumps with owners fail Fixed: format detection and absolute paths; plain dumps documented to need --no-owner --no-privileges, verified end to end
7 NODE_RPC mode could never pass: a main node forces production mode, which refuses test keys Fixed: replaced by ENV_FILE, the operator's reviewed production environment; with a synced node, pending balances are compared loosely because the node may mature blocks
8 Facts missed the pool rollup rows, the writer lease, worker difficulty's updated_at and an independent balance Fixed, including qbit_recomputed_carry_forward_balances()
9 Nothing re-read history after the frontend ran; the restore was not compared with the source Fixed: facts re-read after the frontend (worker difficulty excepted, which it prunes); restore equals source in the per-PR test
10 Weekly mainnet scenario skipped #545's continued-carry check Fixed: query generalised to the ledger's own recipients, on for both scenarios
11 The #582 check could be skipped silently or be speed-dependent at about 1M rows; its "unchanged" proof was narrow Fixed: runs from 2M rows (the weekly 1x/16), logs when skipped, asserts no scratch schema or native table survived
12 The private cluster used trust auth, left a postmaster after a partial start, and blocked the runtime in initdb Fixed: random scram password, stop after any start attempt, spawn_blocking
13 Nits: SCHEMA unvalidated; immature count unbounded; the generator's pg_dump dropped URL parameters Fixed
— The #582 match is on the error text, not the statement Not fixed: from 2M rows, 002 takes minutes and every other statement seconds, so a timeout at 15 s is 002's
— fixture.tool in #545's code has no kill_on_drop Not fixed: #565's code, outside this change
— ENV_FILE is not exercised end to end Not fixed: needs a production environment and synced node, which CI and this lane do not have. It shares the command path the lab mode exercises; the env-file parser itself is untested

Mutants, each failing the per-PR test on its own check:

Verification:

@djh58
djh58 merged commit a46040f into 3.x.x Sep 29, 2026
21 checks passed
djh58 added a commit that referenced this pull request Sep 29, 2026
Resolves test/prism-nightly-gated-tests.txt: keeps the soak smoke id and #586's clock-jump and disk-exhaustion ids, sorted.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
djh58 added a commit that referenced this pull request Sep 29, 2026
Resolves test/e2e-scenarios.toml: keeps #547's real-node scenarios and #586's #575 scenarios; check_e2e_scenarios and its unit tests pass.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
djh58 added a commit that referenced this pull request Sep 29, 2026
…587, #593, #594)

Adds #586's disk-full and clock-jump tool installs, #587's weekly cutover rehearsal settings, #593's comment and #594's nightly Stratum fuzz job. Byte-identical to 3.x.x at bcb7749.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
djh58 added a commit that referenced this pull request Sep 29, 2026
…587, #593, #594) (#595)

Adds #586's disk-full and clock-jump tool installs, #587's weekly cutover rehearsal settings, #593's comment and #594's nightly Stratum fuzz job. Byte-identical to 3.x.x.

Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
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