Repository navigation
Harden public hashrate-series reads: statement deadline, bucket vocabulary, in-process stale-while-revalidate - #218
Conversation
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 62994d6290
ℹ️ 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".
| else lambda: bounded_public_dispatch( | ||
| coordinator, path, query, reads_database=reads_database | ||
| ) |
There was a problem hiding this comment.
Apply the deadline to artifact database lookups
For a cold /public/v1/artifacts/{sha256} request, artifact() calls the ledger's dashboard_public_artifact_document, and the endpoint registry classifies that operation as READ_SLOT; however, path_reads_database() returns false for immutable endpoints, so the value passed here causes bounded_public_dispatch() to skip the new statement-timeout scope. Requests using distinct or uncached hashes can therefore continue occupying Postgres read slots without the advertised deadline. Keep the immutable exemption for staleness/replica decisions, but classify database access separately when deciding whether to arm the timeout.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in b8b2945: a new path_occupies_read_slot() (registry READ_SLOT access, immutable or not) now decides the deadline, so a cold artifact lookup is bounded, while path_reads_database() keeps its immutable exemption for the replica/outage gates. Wiring test updated to assert the split explicitly.
| for stale_key in [ | ||
| stored_key | ||
| for stored_key, stored in self._entries.items() | ||
| if stored_key != key and stored.expires_at <= now | ||
| ]: |
There was a problem hiding this comment.
Preserve stale entries during capacity reaping
When the cache exceeds PRISM_PUBLIC_CACHE_MAX_ENTRIES, this loop removes every entry whose TTL has expired, even when that entry remains eligible for the newly added stale-while-revalidate window. Consequently, once a full cache receives another key, otherwise servable stale entries are discarded and their next visitors block on origin recomputation instead of receiving the promised stale response. The entry needs to retain its stale-window bound so reaping removes only entries past both TTL and SWR.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in b8b2945: entries record their own stale_until (TTL + the SWR window they were stored under) and capacity reaping now removes only entries past that bound, so a full cache sheds dead weight without discarding promised stale-servable answers. Added a test where a dead entry is reaped while an expired-but-within-window entry survives and still serves STALE.
| def test_a_ledger_deadline_becomes_a_503_read_timeout_and_is_never_cached(self) -> None: | ||
| ledger = TimingOutPublicLedger() | ||
| base = self.serve(ledger) | ||
| with patch.dict(os.environ, {}, clear=True): |
There was a problem hiding this comment.
Preserve proxy bypass settings in localhost tests
In environments that define an HTTP proxy, clearing all of os.environ also removes NO_PROXY while urllib.request may retain a previously initialized proxy opener. The localhost request is then sent through the proxy rather than to the test server; running the added cache/bucket tests before this class reproduced 403 responses in all three statement-timeout tests. Patch only PRISM_PUBLIC_READ_STATEMENT_TIMEOUT_SECONDS (deleting that single key when testing the default), or explicitly use a no-proxy opener.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in b8b2945: the three statement-timeout tests no longer clear the environment — they patch only PRISM_PUBLIC_READ_STATEMENT_TIMEOUT_SECONDS (popping just that key for the default-value test), so NO_PROXY and friends survive on proxy-configured machines.
Three independent hardenings on the public read tier, each standing alone ahead of the aggregate-rollup work. Statement deadline on public origin computations. A request the fronting proxy abandoned at its ~15s timeout kept its Postgres scan running to completion, holding one of the four bounded read slots against every other public read and competing with the share writer's I/O. Database-backed routes now run dispatch under the ledger's statement_timeout scope (PRISM_PUBLIC_READ_STATEMENT_TIMEOUT_SECONDS, default 20s, 0 disables), duck-typed exactly like the coordinator's block-submitter scope so scope-less ledgers keep working. A ledger-reported deadline becomes a 503 with the new read_timeout error code, no-store and never cached, so one expensive miss cannot poison the response cache with a refusal. On 2.x.x the read service is the only public dispatch site; the coordinator's listener refuses /public/v1 outright, so no second call site exists to wrap. Static bucket-per-range vocabulary on the hashrate series. range=all&bucket=5m was a legal unauthenticated full-table scan whose response outgrows the cache size cap, so every such request re-scanned the ledger with no cache in front of it. Each range now admits a fixed bucket set (1w: 5m/1h/1d; 1m: 1h/1d; 6m and all: 1d), rejected with a 400 naming the allowed buckets. bucket=auto always resolves inside the allowed sets, and the deployed frontend only ever sends auto, so no real client breaks. The vocabulary and its check are importable for the block-marker series to reuse. In-process stale-while-revalidate. The cache policy already carried stale_while_revalidate_seconds but only advertised it to CDNs; an expired in-process entry was dropped and the next requester blocked for the full recompute. An expired entry inside the window is now served immediately with its honest Age while at most one background refresh per key recomputes through the ordinary status/size gates; concurrent stale hits share that refresh, and a failed refresh keeps the stale entry servable and frees the slot for a retry. The in-process window is clamped to the route's staleness budget minus its TTL, so a stale serve can never trip the unconditional budget refusal that the blocking path used to satisfy by recomputing, and it is withdrawn entirely while the database is unreachable so the outage contract is unchanged. The read service counts the new STALE state in its cache metrics. No response body schema changes: only the new 400/503 error paths and cache/header behavior. The dashboard README and OpenAPI document the vocabulary, the deadline knob, and the in-process revalidation window. Co-Authored-By: Claude Fable 5 <[email protected]>
62994d6 to
1fb8704
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 1fb8704af8
ℹ️ 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".
| with statement_timeout(float(timeout_seconds)): | ||
| return public_api.dispatch(coordinator, path, query) |
There was a problem hiding this comment.
Enforce one deadline across the entire public dispatch
When a route performs several ledger operations, this scope does not provide the documented per-request deadline: PsqlShareLedger.statement_timeout() gives every admission and SQL statement a fresh budget. For example, /public/v1/miners/{recipient_id} performs multiple sequential ledger reads, so after the proxy abandons the request those reads can each consume nearly 20 seconds and the dispatch can continue far beyond the configured limit. Use an operation-wide deadline (while retaining server-side statement cancellation) so the remaining budget decreases across the complete dispatch.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in a18bea7: the dispatch now runs under the ledger's operation_timeout — one budget counting down across every ledger read in the request, with each statement still cancelled server-side on the remaining budget. A wiring test pins that a multi-read route (miner detail) enters the scope exactly once.
…cache entries
Review follow-ups on the public read hardening.
The statement deadline armed on path_reads_database(), whose
immutable-content exemption answers a staleness question, not a cost one: a
cold /public/v1/artifacts/{sha256} occupies a bounded Postgres read slot
exactly like any other cold read, for as long as the lookup takes. A
separate path_occupies_read_slot() predicate -- registry READ_SLOT access,
immutable or not -- now decides the deadline, while the replica and outage
gates keep their exemption.
Capacity reaping removed every entry past its TTL, including ones still
inside the stale-while-revalidate window the lookup path had just promised
to serve. Entries record their own stale_until at store time and reaping
removes only entries past it, so a full cache sheds dead weight without
turning a promised STALE answer into a blocking recompute.
The statement-timeout tests cleared the whole process environment, which
also dropped NO_PROXY: on a proxy-configured machine the localhost request
then went through the proxy and failed. They now patch only the deadline
knob itself.
Co-Authored-By: Claude Fable 5 <[email protected]>
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b8b2945306
ℹ️ 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".
| args=(key, ttl_seconds, stale_while_revalidate_seconds, compute, refresh), | ||
| name="prism-public-cache-refresh", | ||
| daemon=True, | ||
| ).start() |
There was a problem hiding this comment.
Clear the refresh slot when thread startup fails
If the process cannot create another thread during a burst of stale keys, Thread.start() raises after this key has already been inserted into _inflight. Because no refresh thread then executes the cleanup, subsequent stale hits never retry the refresh and, once the stale window expires, requests wait indefinitely on an event that cannot be set. Catch the startup failure and remove or complete the registered inflight entry before propagating or falling back.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Addressed in a18bea7: a Thread.start() failure now frees the registered inflight slot and sets its event, so coalesced waiters wake and the next stale hit retries the refresh; the stale answer is still served. Covered by a test that patches Thread.start to raise and then verifies the slot is free and a later refresh succeeds.
…d failure Second-round review follow-ups on the public read hardening. The deadline armed the ledger's statement_timeout scope, which hands every admission and SQL statement a fresh budget: a route that performs several sequential ledger reads (the miner detail page does) could run many multiples of the configured limit after the proxy hung up. The scope is operation_timeout now -- one budget counting down across the entire dispatch, with every statement still cancelled server-side on whatever remains -- and a wiring test pins that a multi-read dispatch enters the scope exactly once. The stale-serve path registered its background-refresh slot before Thread.start(), so a thread-creation failure under resource pressure stranded the slot: later stale hits never retried the refresh and, past the window, blocking requests waited on an event nobody could set. Startup failure now frees the slot and wakes any coalesced waiter; the stale answer is still served and the next stale hit retries. Co-Authored-By: Claude Fable 5 <[email protected]>
Problem
On prismpool.io,
/public/v1/hashrate-serieswithrange=1m|6m|alldies at the fronting proxy's ~15s timeout (504) whilerange=1wanswers in under a second. Beyond the slow query itself (fixed separately by the rollups PR), the read path had three structural weaknesses:PRISM_POSTGRES_READ_CONCURRENCY) and competing with the share writer's I/O.range=all&bucket=5mwas a legal unauthenticated request whose response outgrowsPRISM_PUBLIC_CACHE_MAX_RESPONSE_BYTES, so every such request bypassed the cache and re-scanned the full ledger.stale-while-revalidateto CDNs.Changes
statement_timeoutscope (PRISM_PUBLIC_READ_STATEMENT_TIMEOUT_SECONDS, default 20s, 0 disables), duck-typed like the coordinator's block-submitter scope. A ledger-reported deadline becomes a503 read_timeout, no-store and never cached.bucket=autoalways resolves inside the sets; the deployed frontend only sendsauto, so no real client breaks.Agewhile at most one background refresh per key recomputes through the ordinary gates. The window is clamped to the route's staleness budget minus its TTL so a stale serve can never trip the unconditional budget refusal, and it is withdrawn entirely while the database is unreachable so the outage contract is unchanged.No response body schema changes; only new 400/503 error paths and cache/header behavior. Docs and OpenAPI updated.
Verification
test_prism_job_buildersplice tests).make test-prism-postgres-ledgergreen on Linux.Notes for review
/public/v1, so there is no second call site to wrap.🤖 Generated with Claude Code
Need help on this PR? Tag
@codesmith-botwith what you need. Autofix is disabled.