GeezerVison is showing github.com — [fix][meta] Fix repeated metadata recovery failures after a ZooKeeper session expires by Radiancebobo · Pull Request #26845 · apache/pulsar
Conversation
This pull request is a clean replacement for #26254. It carries forward the ZooKeeper session-recovery fix and incorporates the review feedback on preserving ZooKeeper connection states in the scheduled probe path.
Motivation
During ZooKeeper session-expired recovery, the broker can close the old ZooKeeper handle and create a new ZooKeeper client. The old handle may emit KeeperState.Closed as part of this local close sequence.
Before this change, ZKSessionWatcher treated every ZooKeeper KeeperState other than Expired and Disconnected as a successful reconnection. As a result, KeeperState.Closed could be translated into Reconnected and SessionReestablished, even though Closed only means that the old ZooKeeper handle was closed; it does not prove that a new session has connected to the ZooKeeper quorum.
The scheduled connection probe had a related issue: it treated a successful exists("/") operation as SyncConnected without checking the ZooKeeper client's actual state. This could misclassify a read-only connection or a closed/auth-failed client as a writable, recovered session.
Reconnected and SessionReestablished trigger metadata recovery work, including persistent watch recreation, resource lock revalidation, and leader-election revalidation. Publishing these events before the new ZooKeeper session is healthy can start recovery against an unstable or expired session and lead to repeated recovery failures.
Modifications
- Made
ZKSessionWatcher state handling explicit. Only KeeperState.SyncConnected publishes Reconnected; SessionReestablished is published only when recovering from SessionLost.
- Kept
Closed from being treated as a successful reconnection, both when received as a watcher event and when observed by a connection probe.
- Preserved the ZooKeeper client's actual state in successful probe results:
CONNECTED maps to SyncConnected.
CONNECTEDREADONLY maps to ConnectedReadOnly.
AUTH_FAILED maps to AuthFailed, and an AUTHFAILED probe result is handled as authentication failure.
CLOSED maps to Closed; connecting or otherwise non-writable states map to Disconnected.
- Handled
ConnectedReadOnly separately from Disconnected: it can publish ConnectionLost for an established session, but it does not start the disconnect timeout or mark the session lost solely because the connection is read-only. A later writable connection publishes Reconnected without incorrectly reporting SessionReestablished when no session loss occurred.
- Retained the session-ID check that prevents a stale asynchronous probe from overwriting the state of a newer ZooKeeper session.
- Added and extended
ZKSessionWatcherTest coverage for watcher-event and probe paths, including auth failure, read-only connections, closed handles, and writable recovery.
Verifying this change
- Make sure that the change passes the CI checks.
The focused tests and style checks can be run with:
./gradlew :pulsar-metadata:test --tests org.apache.pulsar.metadata.impl.ZKSessionWatcherTest --tests org.apache.pulsar.metadata.ZKSessionTest
./gradlew :pulsar-metadata:checkstyleMain :pulsar-metadata:checkstyleTest
Does this pull request potentially affect any of the following parts?
- Dependencies (add or upgrade a dependency)
- The public API
- The schema
- The default values of configurations
- The threading model
- The binary protocol
- The REST endpoints
- The admin CLI options
- The metrics
- Anything that affects deployment
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@void-ptr974 Thanks for pointing this out. You were right that the scheduled checkConnectionStatus() probe could bypass the explicit state handling: AUTHFAILED could be treated as SyncConnected, and a successful exists("/") probe did not distinguish a writable connection from a read-only one.
I updated the probe to map AUTHFAILED explicitly and to derive the state for successful probes from zk.getState(). ConnectedReadOnly is now handled separately from Disconnected: for an established session, it reports ConnectionLost while waiting for a writable connection, without starting the disconnection timeout or marking the session lost solely because the connection is read-only. Recovery events are emitted only after SyncConnected, and SessionReestablished is emitted only when a previously lost session is restored.
I also added deterministic tests for the scheduled probe path, including the AUTHFAILED, read-only, and closed-client cases.
Verification passed:
./gradlew :pulsar-metadata:test --tests org.apache.pulsar.metadata.impl.ZKSessionWatcherTest --tests org.apache.pulsar.metadata.ZKSessionTest
./gradlew :pulsar-metadata:checkstyleMain :pulsar-metadata:checkstyleTest
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for addressing the earlier feedback. The code changes look good to me.
If we could add a couple of regression tests to lock in the following behaviors, that would make the coverage stronger:
-
After session expiration, closing the old handle must not trigger recovery or clear the session-loss state. Recovery notifications should only be emitted once writable connectivity is established, with Reconnected followed by SessionReestablished. Repeated watcher events or successful probes for the same recovery should not produce duplicate notifications.
-
A connection that remains read-only must not be treated as recovered or lose its session solely because the monitor timeout has elapsed. While read probes continue succeeding in read-only mode, only one ConnectionLost should be emitted. Once writable connectivity returns, Reconnected should be emitted without SessionReestablished, provided no session loss occurred in between.
These are non-blocking test suggestions. LGTM.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
|
@void-ptr974 Thanks for the suggestions. I strengthened the regression coverage for both scenarios.
-
The session-expiration test now verifies that repeated Closed events and a successful probe on the closed handle do not trigger recovery or clear SessionLost. Once writable connectivity is restored, it verifies the exact Reconnected followed by SessionReestablished sequence. Additional watcher events and successful probes are also exercised to ensure that recovery notifications are not duplicated.
-
The read-only test now keeps successful probes in CONNECTEDREADONLY beyond the monitor timeout and verifies that only one ConnectionLost is emitted, without SessionLost. Once the connection becomes writable, it verifies that only Reconnected is emitted, without SessionReestablished or duplicate notifications.
Verification passed:
./gradlew :pulsar-metadata:test --tests org.apache.pulsar.metadata.impl.ZKSessionWatcherTest --tests org.apache.pulsar.metadata.ZKSessionTest -PtestRetryCount=0
./gradlew :pulsar-metadata:checkstyleMain :pulsar-metadata:checkstyleTest
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for adding the tests! Looks good to me.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for tracking down the recovery loop in #26254 and for the follow-up tests. I confirmed the root cause: before this change the default: branch of ZKSessionWatcher.checkState turned KeeperState.Closed (emitted by the old handle when PulsarZooKeeperClient closes it in clientCreator) into Reconnected and, after SessionLost, SessionReestablished. That made ZKMetadataStore.receivedSessionEvent re-add the persistent watch and revalidate locks against a not-yet-connected handle. Gating the recovery events on SyncConnected fixes that, and the probe now reports the real client state instead of assuming SyncConnected on every successful read.
I have two concerns about the extra state handling that goes beyond the Closed fix, one of which can change behaviour for existing deployments (the AuthFailed comment), and two smaller points (read-only log volume, a timing race in one test). The minimal fix for the reported loop is case SyncConnected replacing default, so the AuthFailed and ConnectedReadOnly policy changes and the extraction of the Disconnected and SyncConnected blocks into helper methods could also go in a separate PR if that is easier to review.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
@lhotari Thanks for the detailed review. I addressed the remaining issues.
- AuthFailed is now distinguished using the actual ZooKeeper client state. An AuthFailed event while the client is still CONNECTING is treated as an intermediate state and does not publish SessionLost. Once the client reaches terminal AUTH_FAILED, SessionLost is published once and the existing ZooKeeper handle recreation path is scheduled.
- Reconnect scheduling is deduplicated with an atomic guard, so repeated Expired or terminal AuthFailed events cannot enqueue multiple reconnect tasks.
- ConnectedReadOnly now logs WARN only when entering read-only mode. Repeated probes use DEBUG while waiting for a writable connection.
- Recovery events are emitted only after SyncConnected. A read-only-to-writable transition emits Reconnected, while SessionReestablished is emitted only when a previously lost session is restored.
- The tests use a test-only constructor without the scheduled probe, so state transitions are deterministic and do not depend on wall-clock timing.
Added or updated test coverage includes:
- AuthFailed while CONNECTING does not publish SessionLost.
- Terminal AUTH_FAILED publishes exactly one SessionLost.
- Terminal authentication failure followed by SyncConnected produces:
SessionLost -> Reconnected -> SessionReestablished.
- Terminal authentication failure recreates the ZooKeeper handle.
- Repeated read-only probes do not publish SessionLost.
- Read-only-to-writable recovery emits only Reconnected.
- Closed old handles do not produce duplicate recovery events.
Verification passed:
./gradlew :pulsar-metadata:test
--tests org.apache.pulsar.metadata.impl.ZKSessionWatcherTest
--tests org.apache.pulsar.metadata.ZKSessionTest
--tests org.apache.pulsar.metadata.MetadataStoreTest.testTerminalAuthFailedRecreatesZookeeperHandle
:pulsar-metadata:checkstyleMain
:pulsar-metadata:checkstyleTest
-PtestRetryCount=0
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
|
FYI, #26861 also changes ZKSessionWatcher.checkState, but it fixes a different issue, #26860. The two PRs address 2 separate problems and complement each other:
- This PR keeps the watcher from announcing a recovery before the new session is healthy: a
Closed event of the old handle, a read-only connection, an authentication failure or a probe that only saw exists("/") succeed must not be reported as Reconnected or SessionReestablished.
- [fix][meta] Don't lose SessionReestablished when a listener invalidates the new session #26861 keeps a recovery from never being announced. When the session is reestablished,
ZKMetadataStore recreates its persistent watch while it's notified of SessionReestablished. If that fails, it marks the session invalid with setSessionInvalid(), so that the next session sends SessionReestablished again. But checkState sets currentStatus = SessionReestablished after the listeners return, which overwrites the invalid status on the same thread. The next session's SyncConnected then finds the status reestablished and sends nothing, and LockManagerImpl never revalidates the broker's locks. In [Bug][meta] SessionReestablished is lost when recreating ZKMetadataStore's persistent watch fails, so the broker never revalidates its locks #26860, this left a broker serving bundles that another broker had taken over after its metadata session expired.
The handleSyncConnected() in this PR still updates the status after notifying the listener, so the #26860 sequence would lose SessionReestablished with this PR too. #26861 updates the status before notifying, and adds setSessionReplaced(), so that the replaced session's own events, such as a connection check that still succeeds before PulsarZooKeeperClient closes it, can't reestablish it before the new session connects.
Where the two overlap: both stop treating Closed as a reconnection. Whichever PR is merged second will need to be rebased: #26861's change would move into handleSyncConnected(), and its Closed handling would be dropped in favor of this PR's.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
lhotari
changed the title
[fix][metadata]Solve the problem of broker log looping and request failure stagnation after network packet loss recovery- #26254
[fix][metadata] Fix repeated metadata recovery failures after a ZooKeeper session expires
Oct 12, 2026
lhotari
changed the title
[fix][metadata] Fix repeated metadata recovery failures after a ZooKeeper session expires
[fix][meta] Fix repeated metadata recovery failures after a ZooKeeper session expires
Oct 12, 2026
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The read-only logging and test-determinism points are fixed. The AuthFailed point is addressed in intent, but the new handle-recreation path has the gaps that @Denovo1998 raised in the three open threads on this PR (PulsarZooKeeperClient.java:417, PulsarZooKeeperClient.java:433, ZKSessionWatcher.java:212). I re-checked them against this head and they reproduce by reading the code:
reconnectScheduled is only cleared in the creator's finally (PulsarZooKeeperClient.java:190), after waitForConnection() has returned. An Expired event from the replacement in that window is dropped at PulsarZooKeeperClient.java:431, leaving an expired handle installed with nothing queued.
ZKSessionWatcher reads zk.getState() when the queued AuthFailed is processed, but PulsarZooKeeperClient.getState() delegates to the current handle, which may already be the replacement. The SessionLost decision should be captured when the event is received, before the handle is replaced.
- The BookKeeper connection latch is only reset on
Expired, so after AuthFailed the reconnect task's waitForConnection() can return before the new session connects.
Please address those before this merges. One smaller new item is on the new MetadataStoreTest case.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
…lean
# Conflicts:
# pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/ZKSessionWatcher.java
# pulsar-metadata/src/test/java/org/apache/pulsar/metadata/impl/ZKSessionWatcherTest.java
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
@Radiancebobo #26861 has been merged. Please resolve merge conflicts.
Okay, I will merge and push the new commit.
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No new commits since my last review (head is still f873e430), so the open points are unchanged. Three of my threads were marked resolved without a code change. The first of those (the AuthFailed thread) still has gaps that I re-checked against this head:
reconnectScheduled is cleared only in the creator's finally (PulsarZooKeeperClient.java:190), after waitForConnection() returns. An Expired or terminal AuthFailed event in that window is dropped at PulsarZooKeeperClient.java:431-433, leaving a dead handle with nothing queued.
- The
AuthFailed check reads getState(), which delegates to the current handle (PulsarZooKeeperClient.java:768-773). If the handle was already replaced, the event is judged by the replacement's state. ZKSessionWatcher.java:212 has the same problem for events processed later.
- BookKeeper's
ZooKeeperWatcherBase resets its connection latch only on Expired, not on AuthFailed. After an earlier successful connection, waitForConnection() at PulsarZooKeeperClient.java:170 can return before the new session is connected.
These match the three open points from @Denovo1998. Please fix them, or explain why they do not apply, before this merges. The test race on MetadataStoreTest is still open too. The PR also has merge conflicts with master, as you noted.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Preserve terminal events during reconnect, capture handle state with session events, and wait for replacement connectivity before completing reconnects. Add deterministic regression coverage for stale handles, probes, read-only connections, and reconnect races.
Assisted-by: Codex
|
@lhotari @Denovo1998
I pushed new commit to solve the remaining review points.
Reconnect requests from a replacement handle are now retained by handle generation while a reconnect is in progress and processed after the current attempt finishes. Duplicate invalidations from an older handle are ignored.
Watcher events now carry the originating handle's generation, session ID, and ZooKeeper client state. This ensures that queued terminal AuthFailed events are evaluated against the handle that produced them, rather than against a replacement handle.
Each replacement handle now waits on its own connection latch instead of reusing BookKeeper's connection latch.
I also moved the terminal-authentication regression test into PulsarZooKeeperClientTest and made it wait for reconnect bookkeeping to complete. Additional coverage was added for reconnect races, stale watcher events, blocked child watchers, and scheduled probe behavior.
Verification passed:
./gradlew :pulsar-metadata:test \
--tests org.apache.pulsar.metadata.impl.PulsarZooKeeperClientTest \
--tests org.apache.pulsar.metadata.impl.ZKSessionWatcherTest \
--tests org.apache.pulsar.metadata.ZKSessionTest \
:pulsar-metadata:checkstyleMain \
:pulsar-metadata:checkstyleTest \
-PtestRetryCount=0 \
--console=plain \
--rerun-tasks
Could you please take another look?
|
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
Reviewers
lhotari
Awaiting requested review from lhotari
+1 more reviewer
Reviewers whose approvals may not affect merge requirements
Uh oh!
There was an error while loading. Please reload this page.
Development
Successfully merging this pull request may close these issues.
Uh oh!
There was an error while loading. Please reload this page.
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.
|