Repository navigation
feat(db): let only 1 dipper process write, enforced by the database - #858
Merged
Merged
Conversation
Dipper's flows assume a single process acts: the signer's nonce and the locks that keep 2 jobs off the same agreement live in the process. Nothing enforced it. When a pod is evicted, deleted or its node drained, Kubernetes starts the replacement while the old pod is still shutting down, and for that time 2 processes act. The writer fence makes the database enforce it. Every session of a dipper process carries a random token of that process (the setting `dipper.writer` and the name `dipper/<token>`). The table `dipper_writer` names the process allowed to write, and a statement trigger on every table refuses a write from any other session with SQLSTATE DP001. A process another one has taken over from is refused however late it finds out, so 2 processes overlapping behave like the older one crashing, which the flows survive. - A migration adds the 2 tables and the functions; startup fences every table that lacks the trigger, so a table a later migration adds is fenced without anyone having to remember. Migrations run on a session marked `operator`, which the fence lets through, as it does manual work. - Just before the services start, the process waits while another one is the healthy writer, takes over once it hands over or has been silent for 20 seconds, waits for the statements the old one had in flight, and ends its sessions, which frees the jobs and session locks it held. - A heartbeat keeps the writer the writer until the last step of its stop, which hands over. A process that finds it has been replaced, at a heartbeat or at its first refused write, stops through the usual teardown. - Chain broadcasts aren't database writes, so `send_transaction` asks the database before each attempt, inside a transaction a takeover waits for. - `SELECT dipper_fence_set(false)` switches the fence off, across restarts. Tests: the existing registry suite now runs through the fence, and new tests cover who may write, takeover and handover, the cut, the broadcast guard and the exit of a replaced process, against a real Postgres. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ITTED, and bound the RPC timeout - A broadcast's writer check runs on a 1-connection pool of its own, as the heartbeat has a connection of its own, so the main pool's sizing is what it was: a config sized exactly for its worker loops keeps starting. - The check's transaction is opened as READ COMMITTED whatever the database's default, and the guard function refuses any other level, as the write fence does: under REPEATABLE READ each check would have read the token as the first one did and missed a takeover. - `chain_client.request_timeout` is held to at most 12s, below the 15s a takeover waits for a broadcast attempt of the process it replaces, so no attempt can outlast it unless its process is frozen. - Tests: a write already queued behind a takeover is refused once it commits (the lock is taken before the token is read); writes and broadcast checks outside READ COMMITTED are refused with DP002; a broadcast guard sees a takeover whatever the default isolation; a broadcast whose check fails sends nothing; the timeout bound. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The handover becomes the 5th capped step after the worker's drain; the stop's ceiling counts it (72s, under the 75s grace). The k8s README keeps the writer fence's text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The writer fence's tests start their Postgres on its Unix socket only, as #861 does for the rest, and the registry's test databases keep the operator session and the writer pool on top of it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
cjorge-thegraph
marked this pull request as ready for review
November 23, 2026 20:32
MoonBoi9001
pushed a commit
that referenced
this pull request
Nov 23, 2026
🤖 I have created a release *beep* *boop* --- ## [0.3.4](v0.3.3...v0.3.4) (2026-11-23) ### Added * **config:** type secrets as such and read config from several files ([#857](#857)) ([fe92407](fe92407)) * **db:** connect to Postgres over TLS ([#847](#847)) ([2673181](2673181)) * **db:** let only 1 dipper process write, enforced by the database ([#858](#858)) ([6f88900](6f88900)) * **db:** make the database password optional ([#850](#850)) ([cb5d3b1](cb5d3b1)) * **metrics:** serve Prometheus metrics and optionally log as JSON ([#851](#851)) ([523819e](523819e)) * **rpc:** expose effective indexing targets and chain identity ([#860](#860)) ([aafc0ce](aafc0ce)) * **startup:** wait for Postgres and IISA instead of exiting ([#849](#849)) ([d1406d4](d1406d4)) ### Fixed * **db:** sslmode=verify-ca skips the hostname check, as libpq does ([#859](#859)) ([5fec97d](5fec97d)) * **k8s:** roll dipper out one pod at a time ([#846](#846)) ([de57e46](de57e46)) * **shutdown:** stop taking jobs first and fit the teardown in its budget ([#854](#854)) ([a1bc47d](a1bc47d)) ### Tests * run each test's Postgres on its Unix socket only ([#861](#861)) ([bc9e2b2](bc9e2b2)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why
Dipper's flows assume a single process acts. The signer's nonce, the reassessment lock and the Studio consumer's ordering all live in the process, and its sweeps and chain listener are built to repair what a crash of that 1 process leaves behind. Nothing enforced that there is only 1.
strategy: Recreatecovers rollouts only: when a pod is evicted, deleted or its node drained, Kubernetes starts the replacement while the old pod still has up to 90 seconds to stop, and for that time 2 processes act. They can hand out the same nonce, both register agreements for 1 request, apply Studio records out of order, or land an offer after dipper cancelled it.What this does
The database enforces the single writer, with a writer fence:
dipper.writer, and the namedipper/<token>inpg_stat_activity. Both are set once, indb::connect; every connection dipper opens derives from the pool's options.dipper_writerholds the current writer's token. A statement-level trigger on every table takes a shared advisory lock, then reads the token, and refuses a write from any other session with SQLSTATEDP001. A replaced process is refused however late it learns of it, so 2 processes overlapping behave like the older one crashing.send_transactionasks the database before each attempt, inside a READ COMMITTED transaction a takeover waits for, on a 1-connection pool of its own. A replaced process starts no new attempt. The 1 exception: an attempt already in flight when its process froze can still land after the takeover.chain_client.request_timeoutis held at 12 s or less, so otherwise an attempt in flight ends within the takeover's 15 s wait.operator, which the fence lets through, as it does manual work. Startup fences every table that lacks the trigger, so a table a later migration adds is fenced without anyone having to remember.SELECT dipper_fence_set(false)switches the fence off, and it stays off across restarts until switched on again.For database writes, timing only decides how soon a handover happens. If every timing value were wrong, the worst outcomes would be a takeover from a process that was still healthy (that process then behaves as crashed) or a late takeover (downtime), never 2 writers. For chain broadcasts, there is the 1 exception above: a transaction a frozen process had already started sending.
Changes
dipper-pgregistry/migrations/20261120000000_add_writer_fence.sqldipper-pgregistry/src/writer_fence.rsbin/dipper-service/src/writer.rsdb.rs,main.rsoperatorsession; the takeover before the services start; a 1-connection pool for the broadcast check; the handover as the stop's last step, counted in the stop's ceiling (72 s, under the 75 s grace)chain_clientAlloyChainClient::newtakes aBroadcastGate;send_transactionchecks it before each attemptdipper/<token>/studio-request-lock), so a takeover frees itconfig.rschain_client.request_timeoutat most 12 s; the writer events alerted on by default. The main pool's sizing is unchangedk8s/README.md,deployment.yamlOperating it
Manual SQL that writes needs
SET dipper.writer = 'operator';first, orPGOPTIONS='-c dipper.writer=operator'.k8s/README.mdlists how to see the writer, hand over from a writer that won't stop, and switch the fence off. New log events:writer_taken,writer_waiting,writer_superseded,writer_drain_blocked,writer_fence_off,writer_fence_attach_waiting,writer_refused_while_writer.Requirements: Postgres 14 or later (both environments run 16), READ COMMITTED (the fence refuses writes at any other level), sessions that keep their settings (direct or the Cloud SQL Auth Proxy, not a transaction-mode pooler), dipper's sessions on 1 database role, no prepared transactions, and
chain_client.request_timeoutof 12 s or less (default 10 s).Rollout and rollback
Recreatestopping the old pod first, as every rollout does today.SELECT dipper_fence_set(false);as dipper's database role, then deploy the previous release. Its migrator already tolerates a newer schema.chain_client.request_timeoutis 12 s or less (startup refuses more), and don't drain nodes until the fenced release is the one running, since an older pod has no broadcast guard.psqlclient pod, needSET dipper.writer = 'operator';first; they are refused otherwise.PGOPTIONS='-c dipper.writer=operator' psql …thenSHOW dipper.writer).Testing
DP002; a broadcast guard sees a takeover whatever the database's default isolation; a broadcast whose check fails sends nothing; the request-timeout bound.main(sqlx 0.9, Rust 1.94, with fix(shutdown): stop taking jobs first and fit the teardown in its budget #854):cargo +nightly fmt --check,cargo clippy --all-targets --all-features -D warnings, andcargo nextest run --all-featurespass locally.Not in this PR
Unresponsivewhose offer, queued before a crash, lands and is accepted on chain is neither applied by the listener nor ended by its repair path (ended_but_acceptedcovers onlyRejectedandCanceledByRequester). A separate small PR.psqlclient pod should say toSET dipper.writer = 'operator'before a manual write.🤖 Generated with Claude Code