Skip to content

fix(instance-manager): stop renewing the primary lease when a replica stops being the target - #11677

Merged
leonardoce merged 5 commits into
mainfrom
dev/primary-lease-yield
Oct 23, 2026
Merged

leonardoce merged 5 commits into
mainfrom
dev/primary-lease-yield

Conversation

@mnencia

@mnencia mnencia commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

A replica named target primary takes the primary lease and keeps renewing it even when the operator moves the target to another replica before it promotes. The new target sees a record that keeps changing, so its observation window never completes and the cluster has no primary until that replica restarts. The lease runnable never stops renewing a record that names this pod.

The instance reconciler now asks the lease runnable to yield when this pod is not the target primary, PostgreSQL is in recovery and no promotion was requested in this process. This runs before the early returns for a fenced or down instance, in the same single-worker reconcile that promotes, so it never interleaves with a promotion. A promotion that failed leaves the pod unable to yield for the rest of the process, which is the safe side.

Yield stops the renewal loop and the step-down monitor and writes nothing. The record keeps naming this pod and goes stale, so the new target takes it over after the normal observation window, not through the empty-holder fast path that stays reserved for a primary that stopped cleanly. A yielded pod that exits leaves the record as it is. A write already in flight can still land once, which restarts the window once. The next Acquire, which only the target primary makes, takes the lease back. A reconcile blocked in Acquire cannot yield until it returns.

Closes #11736


Reviewer notes

No e2e covers this yet, because the scenario needs a replica held back before it promotes and a second target flip inside that window, which would be flaky; the unit specs cover the runnable (including a second runnable taking the lease over) and the reconciler decision. An earlier, larger version of the same fix was part of #11357 and was moved out to keep that PR smaller.

@mnencia mnencia added bug 🐛 Something isn't working no-issue do not backport This PR must not be backported - it will be in the next minor release labels Oct 16, 2026
@mnencia
mnencia force-pushed the dev/primary-lease-yield branch from d4f08d8 to 18e65d9 Compare October 17, 2026 17:15
@mnencia

mnencia commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia

mnencia commented Oct 17, 2026

Copy link
Copy Markdown
Member Author

@cnpg-bot cnpg-bot added the ok to merge 👌 This PR can be merged label Oct 17, 2026
@mnencia
mnencia force-pushed the dev/primary-lease-yield branch from 18e65d9 to a8457b5 Compare October 18, 2026 20:41
@mnencia

mnencia commented Oct 18, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia

mnencia commented Oct 18, 2026

Copy link
Copy Markdown
Member Author

@mnencia
mnencia force-pushed the dev/primary-lease-yield branch 2 times, most recently from 4a7da3a to 406efb7 Compare October 19, 2026 15:22
@mnencia
mnencia marked this pull request as ready for review October 19, 2026 15:24
@mnencia
mnencia requested a review from a team as a code owner October 19, 2026 15:24
@mnencia

mnencia commented Oct 19, 2026

Copy link
Copy Markdown
Member Author

/test

@github-actions

Copy link
Copy Markdown
Contributor

@mnencia
mnencia force-pushed the dev/primary-lease-yield branch from 406efb7 to 129d014 Compare October 19, 2026 15:54
@armru
armru force-pushed the dev/primary-lease-yield branch from 129d014 to 0c24df0 Compare October 21, 2026 09:29
@mnencia
mnencia force-pushed the dev/primary-lease-yield branch 2 times, most recently from 9402a7c to 1e36f33 Compare October 21, 2026 15:53
@gbartolini gbartolini linked an issue Oct 22, 2026 that may be closed by this pull request
@gbartolini gbartolini added this to the 1.31.0 milestone Oct 22, 2026
@gbartolini

Copy link
Copy Markdown
Contributor

Related to #11721

mnencia and others added 2 commits October 22, 2026 18:00
…arget

A replica named target primary takes the primary lease and keeps
renewing it even when the operator moves the target to another replica
before it promotes. The new target sees a record that keeps changing, so
its observation window never completes and the cluster has no primary
until that replica restarts. The lease runnable never stops renewing a
record that names this pod.

The instance reconciler now asks the lease runnable to yield when this
pod is not the target primary, PostgreSQL is in recovery and no
promotion was requested in this process. This runs before the early
returns for a fenced or down instance, in the same single-worker
reconcile that promotes, so it never interleaves with a promotion. A
promotion that failed leaves the pod unable to yield for the rest of the
process, which is the safe side.

Yield stops the renewal loop and the step-down monitor and writes
nothing. The record keeps naming this pod and goes stale, so the new
target takes it over after the normal observation window, not through
the empty-holder fast path that stays reserved for a primary that
stopped cleanly. A yielded pod that exits leaves the record as it is. A
write already in flight can still land once, which restarts the window
once. The next `Acquire`, which only the target primary makes, takes the
lease back. A reconcile blocked in `Acquire` cannot yield until it
returns.

Assisted-by: Claude
Signed-off-by: Marco Nenciarini <[email protected]>
Every replica that is not the target primary yields on each reconcile,
so at exit Release found the yield flag set on almost every replica and
logged at Info that it was leaving the record for a new target, even on
pods that never held the lease. The yield already logs at Info when it
gives up a lease this pod held, so the exit line moves to Debug.

The comment on the yield in Reconcile said it ran before any early
return, while the certificate, admission and plugin checks can return
before it. It now names the returns it precedes.

Signed-off-by: Armando Ruocco <[email protected]>
@mnencia
mnencia force-pushed the dev/primary-lease-yield branch from 082d562 to 7bc9828 Compare October 22, 2026 16:01
mnencia and others added 2 commits October 22, 2026 18:37
A yielded replica keeps polling the lease, and that is what keeps the
liveness watchdog fed once the lease has been held. Nothing asserted it.

Assisted-by: Claude
Signed-off-by: Marco Nenciarini <[email protected]>
Signed-off-by: Leonardo Cecchi <[email protected]>
The model made a claim lead at once, so a replica that holds the lease
without having promoted could not be expressed. A claim now only holds
the lease, and leading needs a promotion request and a later completion.
A holder that lost the target before asking to promote yields: it stops
renewing and leaves the record to go stale for the new target.

NoTwoLeaders still holds, and it fails without the promotion request
guard on the yield. MaxRounds drops from 15 to 12 to keep the check
within a few minutes.

Assisted-by: Claude
Signed-off-by: Marco Nenciarini <[email protected]>
@leonardoce
leonardoce merged commit a33a3ee into main Oct 23, 2026
36 checks passed
@leonardoce
leonardoce deleted the dev/primary-lease-yield branch October 23, 2026 10:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug 🐛 Something isn't working do not backport This PR must not be backported - it will be in the next minor release no-issue ok to merge 👌 This PR can be merged

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Stop renewing the primary lease when a replica stops being the target primary

5 participants