Skip to content

3.x.x: stamp each share with a clock derived from its node's newest share, so no append writes the cluster row (#738) - #801

Merged
kiwidream merged 1 commit into
3.x.xfrom
orca/738-ledger-clock-append-only
Oct 28, 2026
Merged

kiwidream merged 1 commit into
3.x.xfrom
orca/738-ledger-clock-append-only

Conversation

@kiwidream

@kiwidream kiwidream commented Oct 28, 2026 •

Copy link
Copy Markdown
Member

Fixes #738. Every share append ran UPDATE qbit_prism_cluster SET ledger_clock_ms=... under ORDER_LOCK. Any snapshot held on the primary kept every dead version of that singleton row, so each later append walked them and the hold grew until ORDER_LOCK saturated: about 20 s at 400 shares/s.

What changes

  • No more per-append writes. A share is stamped GREATEST(stored ledger_clock_ms, the database clock, this node's newest own share stamp). The newest own share is found on the (origin_node, share_seq) index from 031, and only own shares count. The append reads the cluster row and writes nothing to it.
  • The anchor takes the same GREATEST plus one (the issue's 2026-10-08 correction), so a backward step of the database clock never puts an anchor below a committed share. It and the peer-sync own-log floor are the only remaining clock writers, and both hold ORDER_LOCK.
  • Mixed versions: a 3.1.0 frontend's stored stamp is read as the floor.

Tests

  • New ledger_clock tests: an anchor, the clock behind the newest share, two frontends, the peer-sync floor, peer rows, exact read-back at 2^43, and a 3.1.0 frontend interleaved with this one.
  • append_admission checks that appends under a held REPEATABLE READ snapshot leave the cluster row's xmin, ctid and heap unchanged.
  • window_read_oracle, b602_order_lock_hold, ledger_postgres 253/253 and offer_lifecycle pass.

#711 (one server-side append call) builds on this branch and follows as a stacked PR.

🤖 Generated with Claude Code


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

…hare, so no append writes the cluster row (#738)

Every share append ran `UPDATE qbit_prism_cluster SET ledger_clock_ms=...`
under ORDER_LOCK. Each one left a dead version of the singleton, and any
snapshot held on the primary kept them all, so every later append walked
them and the hold grew until ORDER_LOCK saturated.

The clock (ledger/window.rs, ledger_clock_sql):
- A share is stamped with GREATEST(stored ledger_clock_ms, the database's
  clock, the newest stamp among this node's own accepted shares). The
  append reads the cluster row and writes nothing to it.
- The newest own share is found on the (origin_node, share_seq) index
  (031), between the share probe's bounds first, then, only when none is
  there, in every partition. Only own shares count: a peer's rows carry
  the peer's clock, and a peer row above an own share can be stamped
  either side of it.
- Its millisecond is the least one whose to_timestamp(ms/1000) is at or
  above the row's stamp, so a stamp stored a microsecond off past 2^43 ms
  reads back as itself, and a legacy row's fraction rounds up.
- The snapshot anchor takes the same GREATEST plus one (the issue's
  2026-10-08 correction), so a backward step of the database's clock
  never puts an anchor below a committed share. It and the peer sync's
  own-log floor are the only clock writers left; both hold ORDER_LOCK.
- Stamps and anchors are the ones the per-append UPDATE gave: the stored
  clock was always the newest own stamp or an anchor or floor above it.
- The FOR KEY SHARE job fences stay; the anchor still updates the row.

Mixed versions: a 3.1.0 frontend stores each stamp, which this one reads
as its floor; this one stamps that floor, the database's clock or a stamp
the floor covers. Only a backward step of the database's own clock while
both serve can put an older frontend's share below a newer one's.

Tests:
- ledger_clock (new): a share after an anchor is stamped after it; the
  clock behind the newest share (the issue's skew case) and the anchor
  covering it; two frontends; the peer-sync floor; peer rows move no own
  stamp; exact read-back at 2^43; the fallback below the probe floor; a
  3.1.0 frontend's statements interleaved with this one.
- append_admission: appends under a held REPEATABLE READ snapshot leave
  the cluster row's xmin, ctid and heap unchanged and send no UPDATE of
  it; the hold test pins the new write statement.
- window_read_oracle: the share after the anchor is appended after the
  snapshot (an anchor can no longer be below an appended share), and the
  rows stamped after an anchor in the empty-window case are a peer's.
- share_partitions_postgres: the #479 scan check takes its first append
  after each sequence jump unobserved, the clock's one fallback.
- migration_rollback: the acknowledged append leaves the stored clock at
  its default, and a window's anchor then stores it.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 28, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-28T17:27:37.500899Z b72586a PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@qbit-reviewer qbit-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

No findings.

@kiwidream
kiwidream merged commit 5628aed into 3.x.x Oct 28, 2026
41 checks passed
kiwidream added a commit that referenced this pull request Nov 11, 2026
…3.1.1

Since 3.1.1 (#801) an append only reads ledger_clock_ms; the cluster row's
remaining writers are the snapshot anchor and the peer sync's own-log
floor. The backfill cannot tell which releases serve, so its 5 s cap stays
as a conservative choice. Prose only.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kiwidream added a commit that referenced this pull request Nov 13, 2026
## Problem

#814: after 3.1.1, several operator docs and the #731 parts-digest checker still described pre-3.1.1 behaviour. #819 shipped part of the fix; this PR does the rest.

- The docs said every share append updates the cluster row, which stopped being true in #801. They gave that as the reason for the long-snapshot guidance and the backfill's 5 s cap.
- The docs called the peer-sync prepared apply one transaction. Since #799 it runs in chunks.
- The docs priced the share path at "ten statements and a ping", which #802 changed.
- On 3.1.1, the checker reported `import_refuses` and exited 3 for 2.x v2 bodies that the import accepts.
- Some references were already wrong in 3.1.0.

## The change

**Cluster row (#801).** Each passage is now scoped to "while a frontend before 3.1.1 serves the database":
- the files: prism-rust-migration.md, prism-ledger-ops.md (including the line next to the `/audit` routes) and the admission-gate comment;
- the backfill's 5 s cap stays, explicitly as a conservative choice. The backfill can't tell which releases are serving, and a rolling upgrade mixes them;
- MultiXact estimate: the clock `UPDATE`s left are the snapshot anchor's and the own-log floor's. A fenced append still locks the row `FOR SHARE` (032), so the MultiXact rate still grows with the share rate.

**Prepared apply (#785/#799).** It is now described as chunks of at most 1,000 jobs and 2 MiB; a job whose blobs alone are larger goes in a chunk by itself. Each chunk is its own transaction under the cluster row's `FOR KEY SHARE`.
- Files: prism-ledger-ops.md, prism-configuration.md, and the `config/dual_writer.rs` doc comment (comment only).
- New peer-sync rows in the lock matrix and the lock-order table: prepared apply, and the own-log floor (SETTLEMENT → ORDER → cluster `NO KEY UPDATE`).

**Share path (#802).** A share without a block takes three round trips and four statements: `BEGIN; SET LOCAL`, one call, `COMMIT`. A share that carries a block keeps the ten.

**#731 checker.** `scripts/prism_legacy_parts_digest_check.py` gets an opt-in `--with-731-fix` mode, which models 3.1.1's import:
- what it recomputes: 2.x's parts digest over the parts as 3.x parses them. That means known fields in declaration order with `kind` first, and inline shares in `AcceptedShare` form, using the sidecar tool's model. Non-ASCII is escaped;
- statuses: `ok-in-3x` or `matches-neither`;
- if `prism_legacy_range_sidecars.py` is not beside the checker, the mode refuses up front;
- the default mode is byte-identical to before, with the same statuses, output and exit codes;
- the docstring's "3.x refuses every 2.x v2 body" is corrected;
- the #731 runbook gets a 3.1.1 command variant.

**References.**
- `/audit/latest-evidence` → `/audit/latest`.
- Migration 17's refusals are no longer quoted as "migration 16".
- The truncated native-collision quote is completed.
- prism-alert-migration.md now says eight native rules page, with `critical` and `page: "true"`.
- tests/perf/window_paging_allocation.md's re-run recipe starts from a worktree of #241's commit, which holds the harness that today's tree lacks.
  The image is built from 527b3ac's tree by path, since today's Dockerfile builds a different image.

**#820 follow-up.** The `PrismAcceptedRevisionWorkTrackingUnknown` description now says an idle backup's unknown wait also ends when the block matures, if no authorized miner was connected since its acceptance. The text was changed at its source in `scripts/generate_prism_alerts.py`. The rule JSON, the migration doc tables and the qbit-tools patch were regenerated with `--snapshot` from the pinned qbit-tools a007a142 templates.

## Tests run (minidev-alex1)

- `PRISM_AUDIT_CANONICALIZE_BIN=target/debug/qbit-prism-audit-canonicalize python3 -m unittest tests.test_prism_legacy_range_sidecars tests.test_prism_legacy_parts_digest_check tests.test_docs_lab_prism_references tests.test_prism_legacy_import_admission_gate`: OK (1 skipped). The canonicalizer was built with `cargo build --locked -p qbit-prism --bin qbit-prism-audit-canonicalize --bin qbit-prism-build-audit-bundle`.
  - New tests cover the fixed mode on both 2.x fixtures and on sorted-key and other digests.
  - They cover unknown keys, null bounds and escaping.
  - Inline-share variants are checked against the real canonicalizer: null `credit_policy`, an unknown key, reordered keys.
  - They check that the other statuses are unchanged, that `--jobs 3` parity holds, and that the checker runs when copied alone.
- `python3 scripts/generate_prism_alerts.py --check`, `generate_prism_metrics.py --check`, `check_prism_settings.py`, `check_prism_alert_uids.py`, `check_gate_env_reads.py`: pass.
- `python3 scripts/test_prism_alert_deployment.py --snapshot <qbit-tools a007a142 templates>`: patch applies, 10 Jinja gate combinations pass.
- `cargo fmt --all -- --check`: pass. `cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings`: pass (only the 3 known vendored sqlx-core warnings).
- `bash -n scripts/prism_legacy_import_admission_gate.sh`: pass.

## What CI must cover

- the Python test shards;
- `make test-prism-legacy-range-sidecars`, which uses the real canonicalizer;
- the alert PromQL check with promtool, not run here (only a description changed);
- the full Rust suites. No Rust code changed, only one doc comment.

Refs #814

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Squashed commits:
27d51e6 3.x.x: operator docs and the #731 checker catch up with 3.1.1 (#814)
f84b602 3.x.x: --with-731-fix parses inline shares as 3.x does; review fixes (#814)
88f3ee3 3.x.x: the 3.1.1 #731 runbook still proves listed rows; checker loads its share model lazily (#814)
36f0c92 3.x.x: --with-731-fix refuses up front without its share model; wording fixes (#814)
99ada68 Merge remote-tracking branch 'origin/3.x.x' into orca/814-docs-catch-up
da36f25 3.x.x: the window-paging re-run starts from #241's commit, which holds the harness (#814)

Co-Authored-By: Claude Opus 5.5 <[email protected]>
kiwidream added a commit that referenced this pull request Nov 13, 2026
…#801's note and #824's SQLSTATE rule

The two edits to the same paragraph of prism-ledger-ops.md are both kept,
and the deployment patch is regenerated from the pinned a007a142 template.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
kiwidream added a commit that referenced this pull request Nov 13, 2026
## Problem

#814: after 3.1.1, several operator docs and the #731 parts-digest checker still described pre-3.1.1 behaviour. #819 shipped part of the fix; this PR does the rest.

- The docs said every share append updates the cluster row, which stopped being true in #801. They gave that as the reason for the long-snapshot guidance and the backfill's 5 s cap.
- The docs called the peer-sync prepared apply one transaction. Since #799 it runs in chunks.
- The docs priced the share path at "ten statements and a ping", which #802 changed.
- On 3.1.1, the checker reported `import_refuses` and exited 3 for 2.x v2 bodies that the import accepts.
- Some references were already wrong in 3.1.0.

## The change

**Cluster row (#801).** Each passage is now scoped to "while a frontend before 3.1.1 serves the database":
- the files: prism-rust-migration.md, prism-ledger-ops.md (including the line next to the `/audit` routes) and the admission-gate comment;
- the backfill's 5 s cap stays, explicitly as a conservative choice. The backfill can't tell which releases are serving, and a rolling upgrade mixes them;
- MultiXact estimate: the clock `UPDATE`s left are the snapshot anchor's and the own-log floor's. A fenced append still locks the row `FOR SHARE` (032), so the MultiXact rate still grows with the share rate.

**Prepared apply (#785/#799).** It is now described as chunks of at most 1,000 jobs and 2 MiB; a job whose blobs alone are larger goes in a chunk by itself. Each chunk is its own transaction under the cluster row's `FOR KEY SHARE`.
- Files: prism-ledger-ops.md, prism-configuration.md, and the `config/dual_writer.rs` doc comment (comment only).
- New peer-sync rows in the lock matrix and the lock-order table: prepared apply, and the own-log floor (SETTLEMENT → ORDER → cluster `NO KEY UPDATE`).

**Share path (#802).** A share without a block takes three round trips and four statements: `BEGIN; SET LOCAL`, one call, `COMMIT`. A share that carries a block keeps the ten.

**#731 checker.** `scripts/prism_legacy_parts_digest_check.py` gets an opt-in `--with-731-fix` mode, which models 3.1.1's import:
- what it recomputes: 2.x's parts digest over the parts as 3.x parses them. That means known fields in declaration order with `kind` first, and inline shares in `AcceptedShare` form, using the sidecar tool's model. Non-ASCII is escaped;
- statuses: `ok-in-3x` or `matches-neither`;
- if `prism_legacy_range_sidecars.py` is not beside the checker, the mode refuses up front;
- the default mode is byte-identical to before, with the same statuses, output and exit codes;
- the docstring's "3.x refuses every 2.x v2 body" is corrected;
- the #731 runbook gets a 3.1.1 command variant.

**References.**
- `/audit/latest-evidence` → `/audit/latest`.
- Migration 17's refusals are no longer quoted as "migration 16".
- The truncated native-collision quote is completed.
- prism-alert-migration.md now says eight native rules page, with `critical` and `page: "true"`.
- tests/perf/window_paging_allocation.md's re-run recipe starts from a worktree of #241's commit, which holds the harness that today's tree lacks.
  The image is built from 527b3ac's tree by path, since today's Dockerfile builds a different image.

**#820 follow-up.** The `PrismAcceptedRevisionWorkTrackingUnknown` description now says an idle backup's unknown wait also ends when the block matures, if no authorized miner was connected since its acceptance. The text was changed at its source in `scripts/generate_prism_alerts.py`. The rule JSON, the migration doc tables and the qbit-tools patch were regenerated with `--snapshot` from the pinned qbit-tools a007a142 templates.

## Tests run (minidev-alex1)

- `PRISM_AUDIT_CANONICALIZE_BIN=target/debug/qbit-prism-audit-canonicalize python3 -m unittest tests.test_prism_legacy_range_sidecars tests.test_prism_legacy_parts_digest_check tests.test_docs_lab_prism_references tests.test_prism_legacy_import_admission_gate`: OK (1 skipped). The canonicalizer was built with `cargo build --locked -p qbit-prism --bin qbit-prism-audit-canonicalize --bin qbit-prism-build-audit-bundle`.
  - New tests cover the fixed mode on both 2.x fixtures and on sorted-key and other digests.
  - They cover unknown keys, null bounds and escaping.
  - Inline-share variants are checked against the real canonicalizer: null `credit_policy`, an unknown key, reordered keys.
  - They check that the other statuses are unchanged, that `--jobs 3` parity holds, and that the checker runs when copied alone.
- `python3 scripts/generate_prism_alerts.py --check`, `generate_prism_metrics.py --check`, `check_prism_settings.py`, `check_prism_alert_uids.py`, `check_gate_env_reads.py`: pass.
- `python3 scripts/test_prism_alert_deployment.py --snapshot <qbit-tools a007a142 templates>`: patch applies, 10 Jinja gate combinations pass.
- `cargo fmt --all -- --check`: pass. `cargo clippy --locked -p qbit-prism-server --all-targets -- -D warnings`: pass (only the 3 known vendored sqlx-core warnings).
- `bash -n scripts/prism_legacy_import_admission_gate.sh`: pass.

## What CI must cover

- the Python test shards;
- `make test-prism-legacy-range-sidecars`, which uses the real canonicalizer;
- the alert PromQL check with promtool, not run here (only a description changed);
- the full Rust suites. No Rust code changed, only one doc comment.

Refs #814

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Squashed commits:
dea2d9a 3.x.x: operator docs and the #731 checker catch up with 3.1.1 (#814)
27bec61 Merge 3.x.x (3d48a8f) into the #814 branch: the backfill passage keeps #801's note and #824's SQLSTATE rule
1fe7fba Merge 3.x.x (582849b) into the #814 branch: the deployment patch is regenerated beside #843's listener rule

Co-Authored-By: Claude Opus 5.5 <[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