Uh oh!
There was an error while loading. Please reload this page.
Fix race in primary election. - #371
Merged
Merged
Conversation
The methods `Init`` and `PostInit`` each decided primary-vs-standby by reading the Consul "cluster initialized" flag and acting on it separately, with no atomicity between the two. When nodes booted close together, more than one could see the flag unset and independently self-initialize as primary, leaving a node with a fresh, credential-less local database that then failed forever trying to register as a standby against the real primary (repmgr/flypgadmin roles don't exist). Replace both checks with `Store.TryClaimPrimary``, a single atomic Consul CAS (ModifyIndex: 0) keyed by machine ID, so exactly one node ever wins the claim, and a node retrying after its own crash can tell it already won rather than deferring to itself as "someone else."
NullHypothesis
commented
Aug 31, 2026
ContributorAuthor
I'm going to ignore the "cyclomatic complexity" complaint because the PR isn't making anything worse. In fact, it's removing a bit of code. |
NullHypothesis
commented
Aug 31, 2026
ContributorAuthor
Here's how I tested this: This command currently fails for me. The leader passes all its health checks and thinks it's the leader but the other two machines (or at least one other machine) often disagree. Using the patched postgres-flex image (I built it locally and manually uploaded to our registry), the command succeeds: |
Uh oh!
There was an error while loading. Please reload this page.
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 freeto 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.
The methods
InitandPostIniteach decided primary-vs-standby by reading the Consul "cluster initialized" flag and acting on it separately, with no atomicity between the two. When nodes booted close together, more than one could see the flag unset and independently self-initialize as primary, leaving a node with a fresh, credential-less local database that then failed forever trying to register as a standby against the real primary (repmgr/flypgadmin roles don't exist).Replace both checks with
Store.TryClaimPrimary, a single atomic Consul CAS (ModifyIndex: 0) keyed by machine ID, so exactly one node ever wins the claim, and a node retrying after its own crash can tell it already won rather than deferring to itself as "someone else."