Skip to content

3.1 dual-writer (4/5): dual-writer readiness and Stratum gating - #769

Merged
RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/p31-readiness-routing
Oct 19, 2026
Merged

RobertClarke merged 1 commit into
3.x.xfrom
RobertClarke/p31-readiness-routing

Conversation

@RobertClarke

@RobertClarke RobertClarke commented Oct 10, 2026 •

Copy link
Copy Markdown
Member

Caution

Do not merge: part of the 3.1 stack, teed up for review.

Stack position 4/5 of the PRISM 3.1 dual-writer stack: #766 (1/5, identity and peer sync), #768 (2/5, window cuts), #767 (3/5, carry owner), this PR, then #763 (5/5, E2E). Its base is #767's branch, RobertClarke/p31-carry-owner, so this diff is the readiness work alone.

PRISM 3.1 keeps mining online when either node of the pair dies by routing on readiness. This PR is what a frontend tells the balancer, and how its Stratum listeners follow it. It adds the /healthz dual_writer object, and gates readiness on the own-log latch and on a local writer. It also adds Stratum listeners that accept only while the frontend admits miners, the token-protected readiness-only endpoint the Hashbalancer checks, a liveness container healthcheck for dual mode, and their metrics.

With PRISM_DUAL_WRITER off and PRISM_READINESS_PORT unset, the defaults, /healthz, the healthcheck and the Stratum listeners behave as in 3.0 and no readiness statement runs.

The doc is docs/prism-dual-writer-readiness.md. The Hashbalancer's routing on it is a separate PR in the balancer's repository.

What changes

  • Readiness in dual mode (coordinator.rs, readiness/dual_writer.rs). ok and ready also need:

    • the own log caught up: the peer sync's startup latch (PeerSyncStatus.own_log_caught_up), which a lost peer never clears;
    • the writer local: the frontend's own database names PRISM_NODE_INDEX in its never-copied qbit_prism_node_identity row, is not in recovery and is not read-only.

    status names the first that fails (own-log-behind, then writer-not-local), after 3.0's runtime-stalled and job-delivery-stalled. /healthz gains dual_writer: {node_index, carry_owner, own_log_caught_up, peer_sync, writer_path}. writer_path is local, remote (the row names the peer), unidentified (no row), read_only or unanswered. It comes from one statement on the frontend's own pool, reused for a second and bounded at two seconds, and it never reads the peer.

  • Admission (readiness/admission.rs): starting, admitting, grace, withdrawn.

    • A readiness dip shorter than PRISM_READINESS_GRACE_SECONDS (default 10, 0 to 120), counted from the last publication that found the frontend ready, keeps admitting. Every new tip and payout revision causes such a dip.
    • own-log-behind and writer-not-local withdraw at once.
    • A database that stops answering, dead or hung, withdraws the frontend about 4 s after its last answer. In dual mode the writer probe and the health reads are one refresh on the frontend's own two-connection pool, with 2 s acquire, statement and lock timeouts. So a saturated ledger pool never reads as a dead database. At most one refresh runs at a time, and at most one per second, however many callers ask (mining.get_health included). The heartbeat runs as its own task, so publications keep coming.
    • Gated tests check this: with the database frozen, /readyz turned 503 about 3 s later; with every ledger connection held busy, the writer stays local; 50 concurrent health calls share one refresh.
    • A decision older than the health freshness budget admits nothing.

    Reported as /healthz admission in dual mode or with the endpoint on.

  • Stratum gating, dual mode only (listen.rs, stratum.rs, server.rs). Each listener binds at startup, before the coordinator, as in 3.0, but listens only while the frontend admits:

    • no TCP handshake completes before readiness, after a withdrawal, or while the decision is stale;
    • a withdrawal resets queued connections and refuses new ones (RST);
    • a definite fault (own log behind, or the database answering remote, unidentified or read_only) also closes established sessions. Each session finishes the request in hand, gets a 3 s drain, and only then is aborted, so no share lands in a database that is not this node's. After a not-ready or unanswered withdrawal, sessions carry on until the balancer closes them.

    The accept loop is shared with 3.0's listener, which is otherwise unchanged and is still what a single writer runs. The peer sync's own wait stays on the refresh and submit loops; the listeners follow admission alone, so there is one gate.

  • Readiness endpoint (readiness/endpoint.rs). Settings: PRISM_READINESS_PORT (off by default), PRISM_READINESS_BIND (127.0.0.1), PRISM_READINESS_TOKEN or _TOKEN_FILE (a group-readable 0440 file is fine).

    • GET|HEAD /readyz with X-Qbit-Healthcheck-Token answers 200 ready, 503 not ready, 401 (the token is checked first) or 404.
    • Plain text, no-store, Connection: close.
    • Served with hyper's HTTP/1 server, already in the graph through axum. Each connection gets one request and a 2 s deadline. At most 256 connections are served at once, and at most 8 per source address (an IPv6 source counts by its /64). A client that holds connections open therefore cannot starve the balancer's checks on a public address.
  • Container healthcheck (tools.rs). In dual mode qbit-prism-server healthcheck checks liveness. Routing uses /readyz, and a converge must not time out while a node catches up after a restart, restore or rebuild.

    • It passes while the frontend is ready, catching up on its own log, or inside the grace, a single slow writer probe included.
    • It fails on no answer, a stale snapshot, a stalled runtime or job delivery, a writer that is remote, unidentified or read_only (or unanswered once admission has withdrawn), or readiness lost past the grace.
    • A single writer's healthcheck and self-check keep 3.0's readiness rule.
  • Metrics:

    • qbit_prism_admission_admitting, _state{state} and _withdrawals_total{reason};
    • qbit_prism_stratum_listener_accepting{listener};
    • qbit_prism_dual_writer_writer_path{path}, _node_index and _carry_owner;
    • qbit_prism_readiness_requests_total{result}.

    They are declared without samples unless dual mode or the endpoint exposes admission. In a stale snapshot admission_admitting and stratum_listener_accepting read 0, while admission_state keeps the last decision. The inventory, metadata and observability contract are updated, and the pins are combined with the carry-owner families.

  • Settings: five names in config/native-settings.txt; check_prism_settings.py learns PRISM_READINESS_TOKEN_FILE; docs/prism-configuration.md documents them, and the HA reference's readiness contract points to the new doc.

How it was tested

  • Local /code-review medium rounds and a read-only fact-check of every doc claim against the code; findings fixed.
  • cargo fmt --check and cargo clippy --all-targets; check_prism_settings.py and generate_prism_metrics.py --check pass.
  • Unit tests (109 in the touched modules): the admission state machine (the grace runs from the last ready publication), its signal's freshness and exact stale boundary, the endpoint's token, answers, deadline and both connection caps, the reserved address, the writer-path classification and its unanswered streak, the metrics and a stale snapshot's samples, and the healthcheck's liveness rule.
  • tests/healthcheck_cli.rs: the healthcheck binary against canned /healthz answers in both modes.
  • tests/stratum_admission_gate.rs, at the socket level: refusal until admitted, refusal again after a withdrawal while an established session is served, closure when the decision goes stale, a definite fault closing established sessions (after answering a request in flight), and a silent database leaving them open.
  • Gated, against PostgreSQL 16 with PRISM_TEST_REQUIRE_INTEGRATION=1:
    • tests/dual_writer_readiness.rs: the latch and the identity at the coordinator (unidentified, then local; remote for a database personalised as the peer), a database turned read-only and back, the peer lost after the latch, the writer staying local with every ledger connection busy (it fails when pointed back at the ledger pool), a burst of 50 health calls sharing one refresh, a single writer's unchanged health.
    • tests/readiness_frontend.rs, the server binary against the fake node:
      • in dual mode, not yet admitted, every Stratum connect is refused and /readyz answers 503 and 401;
      • a single writer listens from startup and answers 200;
      • five payout revision bumps under readiness polling never leave 200, and a rebuild held open keeps 200 for the whole default grace, turns 503 only after it, and is readmitted;
      • the healthcheck passes for a dual-mode frontend catching up and fails for one on the other node's database;
      • an admitted dual-mode frontend (its own log verified on this server, the peer unreachable) answers 200 and accepts Stratum; with its database frozen behind a proxy it answers 503 within 10 s and refuses Stratum, and it is readmitted once the database answers again.
    • The carry-owner and window-cut suites this branch sits on (carry_owner_postgres, carry_owner_adoption, dual_writer_windows) pass on it.
  • Ten gated tests are registered in test/prism-gated-tests.txt.

Notes

  • A dual-writer frontend opens up to two more PostgreSQL connections, for its health path; their checkouts appear in the ledger's pool-acquire histogram and the pool-acquire census.
  • CI's pool-acquire census fails on this base already (sites in the earlier PRs of the stack); this branch adds no error to it.
  • Under heavy host I/O the fixture's own migration can hit a statement timeout; reruns passed.
  • On the pair, publish the endpoint so client source addresses are preserved (Docker's iptables publishing does), or the per-source cap would count every checker as one source.
  • Linux lets a second socket bind an address that a reserved one holds while neither listens. So two frontends misconfigured with one address both start, and the second to be admitted fails to listen and exits.

🤖 Generated with Claude Code


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

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 10, 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-19T19:40:31.311930Z 2c5d7b8 New commits
ℹ️ 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.

@blacksmith-sh

This comment has been minimized.

@RobertClarke
RobertClarke force-pushed the RobertClarke/p31-carry-owner branch from 359539d to 7a2ea81 Compare October 10, 2026 11:40
@RobertClarke
RobertClarke force-pushed the RobertClarke/p31-readiness-routing branch from 2daef48 to 6cc825b Compare October 10, 2026 12:16

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c2eae8d28e

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/qbit-prism-server/src/stratum.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: bd227caef6

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread crates/qbit-prism-server/src/listen.rs Resolved

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 9549ef2907

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@chatgpt-codex-connector chatgpt-codex-connector 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.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: ae28d7ec58

ℹ️ 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".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

@RobertClarke
RobertClarke force-pushed the RobertClarke/p31-carry-owner branch from 4b1e8e5 to 21cf3ae Compare October 19, 2026 19:15
Rebased onto 3.x.x after #767 was squash-merged (44fd3e4), as one commit:
the branch merged D3 heads, so a commit-by-commit replay conflicts. The
tree is identical to the reviewed head 571fc6b.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@RobertClarke
RobertClarke changed the base branch from RobertClarke/p31-carry-owner to 3.x.x October 19, 2026 19:37
@RobertClarke
RobertClarke force-pushed the RobertClarke/p31-readiness-routing branch from 571fc6b to 2c5d7b8 Compare October 19, 2026 19:37
@RobertClarke
RobertClarke merged commit 92559d7 into 3.x.x Oct 19, 2026
39 checks passed
RobertClarke added a commit that referenced this pull request Oct 19, 2026
The notes cover everything since v3.0.0-rc.6: #770's import-audits fix
(#765) and #760's idle-connection bound (#759), which would have shipped as
rc.7, and the 3.1 dual writer (#766, #768, #767, #769, #763), off by
default. They describe the new settings, migrations 027, 028 and 031 (029
and 030 reserved), the upgrade from rc.6 with the migrate run while rc.6
serves, the rollbacks, and the dual writer's known limits. The migration
guide's import-audits paragraph names this candidate in place of the
skipped rc.7.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
RobertClarke added a commit that referenced this pull request Oct 20, 2026
* 3.x.x: version 3.1.0-rc.1

VERSION, the workspace version and both lockfiles move to 3.1.0-rc.1.
The fuzz lockfile also picks up hyper and hyper-util, which #769 added to
the server without refreshing it. The 3.0.0 and development-line notes
point at the new candidate; its own notes follow in this pull request.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* 3.x.x: 3.1.0-rc.1 release notes

The notes cover everything since v3.0.0-rc.6: #770's import-audits fix
(#765) and #760's idle-connection bound (#759), which would have shipped as
rc.7, and the 3.1 dual writer (#766, #768, #767, #769, #763), off by
default. They describe the new settings, migrations 027, 028 and 031 (029
and 030 reserved), the upgrade from rc.6 with the migrate run while rc.6
serves, the rollbacks, and the dual writer's known limits. The migration
guide's import-audits paragraph names this candidate in place of the
skipped rc.7.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* 3.x.x: 3.1.0-rc.1 notes: #773's pre-migrate tests

Co-Authored-By: Claude Opus 5.5 <[email protected]>

* 3.x.x: 3.1.0-rc.1 notes: the rollback to one writer stops and syncs the peer first

Codex on #774: DOWNGRADE fences nothing, so the peer must be stopped, and its
tail pulled, before the single-writer start; declare the tail lost only for a
peer that never returns with its own database.

Co-Authored-By: Claude Opus 5.5 <[email protected]>

---------

Co-authored-by: Claude Opus 5.5 <[email protected]>
@RobertClarke
RobertClarke deleted the RobertClarke/p31-readiness-routing branch November 3, 2026 20:47
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