Skip to content

ZOOKEEPER-5131: LeaderElectionSupport fails on a withdrawn offer and leaves its own offer behind - #2506

Open
SpookyJumpyBeans wants to merge 1 commit into
apache:masterfrom
SpookyJumpyBeans:ZOOKEEPER-5131
Open

SpookyJumpyBeans wants to merge 1 commit into
apache:masterfrom
SpookyJumpyBeans:ZOOKEEPER-5131

Conversation

@SpookyJumpyBeans

@SpookyJumpyBeans SpookyJumpyBeans commented Nov 14, 2026 •

Copy link
Copy Markdown

https://issues.apache.org/jira/browse/ZOOKEEPER-5131

LeaderElectionSupport lists the election offers and then reads each one with a separate getData. If a competing offer was withdrawn in between, getData failed with NoNode and start() moved to FAILED. The failed election also kept its own offer, so later contenders waited behind an offer that was no longer taking part, and no one became leader until the failed instance called stop() or its session ended.

Changes

  • toLeaderOffers() skips an offer that disappears while the offers are read. getLeaderHostName() goes through the same method, so it no longer fails in this race either; it can return the next contender's host.
  • When the election fails, in start() or in an election run after a watch, it removes its own offer and only then sends FAILED. A listener that starts the election again on FAILED therefore gets a new offer that is kept. If the offer can't be removed, it is kept for stop() as before.
  • If our own offer was removed by someone else, the election now sends FAILED instead of stopping without any event.
  • A NodeDeleted watch runs the election holding the same lock as start() and stop(), so a concurrent stop() can't be followed by a FAILED event. Other watch events (connection state) don't take the lock. Listeners for a watch-triggered election are now called holding the instance lock, as they already were from start(), so they must not block on that client's event thread.
  • The election doesn't overwrite STOP when a listener stopped it during the election.
  • start() and stop() restore the interrupt status. process() runs on the client's event thread and does not, so the shared client keeps delivering events.
  • zooKeeper is now volatile, since it is read both with and without the lock (SpotBugs IS2_INCONSISTENT_SYNC).

Not changed

  • A connection loss during the create in makeOffer() can still leave an offer whose path the client never learned, until the session ends.
  • stop() doesn't remove watches, and a listener that calls stop() on ELECTED_START still gets ELECTED_COMPLETE afterwards.

Tests

New tests in LeaderElectionSupportTest:

Test What it checks On master
testCompetingOfferDeletedWhileReadingOffers a competing offer withdrawn between listing and reading; asserts the race happened fails (FAILED)
testFailedStartRemovesItsOffer start() fails after its offer was created fails (offer left behind)
testFailureAfterWatchRemovesOffer the election run after a watch fails fails (offer left behind)
testRestartFromFailedListenerKeepsNewOffer a FAILED listener starts the election again fails
testOwnOfferMissingFailsElection our own offer was removed by someone else fails (no event at all)
testStopDuringElectionAfterWatchSendsNoFailure stop() from another thread while a watch-triggered election runs fails
testStopFromReadyListenerSendsNoFailure a READY_START listener calls stop() passes (guards against a regression in an earlier version of this change)
testStopAfterOfferSendsNoFailure an OFFER_COMPLETE listener calls stop() passes (same)

How the tests were run: Maven currently skips the recipe tests that extend ClientBase (ZOOKEEPER-5132, #2508), so I ran the class with the JUnit console launcher:

java -ea -jar junit-platform-console-standalone-1.6.2.jar -cp "target/classes;target/test-classes;<module test classpath>" --select-class org.apache.zookeeper.recipes.leader.LeaderElectionSupportTest

All 16 tests pass (3 runs); on master 6 of the new tests fail as listed. -Pfull-build apache-rat:check verify -DskipTests spotbugs:check checkstyle:check passes for the module.

@JHSUYU

JHSUYU commented Nov 19, 2026

Copy link
Copy Markdown

@SpookyJumpyBeans Please fix the pr-merge issue

…leaves its own offer behind

LeaderElectionSupport lists the offers and then reads each one. If a
competing offer was withdrawn in between, getData failed with NoNode
and start() moved to FAILED, an ordinary race. The failed election also
left its own offer in place, so later contenders waited behind an offer
that was no longer taking part, until stop() or the session ended.

Skip an offer that disappears while the offers are read; this also
keeps getLeaderHostName from failing in the same race. When the
election fails, in start() or after a watch, remove its offer before
sending the failed event, if it can be removed. If our own offer has
been removed by someone else, fail the election instead of stopping
without an event, and don't overwrite STOP when a listener stopped the
election. A NodeDeleted watch now runs the election holding the same
lock as start() and stop(), so a stop() can't interleave with it;
listeners are called holding that lock. start() and stop() restore the
interrupt status; process() runs on the client's event thread and does
not.
@JHSUYU

JHSUYU commented Nov 22, 2026

Copy link
Copy Markdown

@kezhuw

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.

2 participants