GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — [fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZooKeeperClient.clientCreator by oneby-wang · Pull Request #25910 · apache/pulsar

Conversation

@oneby-wang

@oneby-wang oneby-wang commented Jun 1, 2026

Copy link
Copy Markdown
Contributor

Motivation

ZKSessionTest.testReacquireLeadershipAfterSessionLost can observe unstable metadata session events after a ZooKeeper session expires and PulsarZooKeeperClient creates a replacement client.

Failure test case1:

java.lang.AssertionError: expected [SessionReestablished] but found [ConnectionLost]
	at org.testng.Assert.fail(Assert.java:111)
	at org.testng.Assert.failNotEquals(Assert.java:1590)
	at org.testng.Assert.assertEqualsImpl(Assert.java:150)
	at org.testng.Assert.assertEquals(Assert.java:132)
	at org.testng.Assert.assertEquals(Assert.java:644)
	at org.apache.pulsar.metadata.ZKSessionTest.testReacquireLeadershipAfterSessionLost(ZKSessionTest.java:230)
	at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
	at java.base/java.lang.reflect.Method.invoke(Method.java:580)
	at org.testng.internal.invokers.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:141)
	at org.testng.internal.invokers.InvokeMethodRunnable.runOne(InvokeMethodRunnable.java:47)
	at org.testng.internal.invokers.InvokeMethodRunnable.call(InvokeMethodRunnable.java:76)
	at org.testng.internal.invokers.InvokeMethodRunnable.call(InvokeMethodRunnable.java:11)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:317)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1144)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:642)
	at java.base/java.lang.Thread.run(Thread.java:1583)

Failure test case2:

java.util.concurrent.CompletionException: org.apache.pulsar.metadata.api.MetadataStoreException: org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired
	at java.base/java.util.concurrent.CompletableFuture.encodeThrowable(CompletableFuture.java:332)
	at java.base/java.util.concurrent.CompletableFuture.completeThrowable(CompletableFuture.java:347)
	at java.base/java.util.concurrent.CompletableFuture$UniRun.tryFire(CompletableFuture.java:781)
	at java.base/java.util.concurrent.CompletableFuture.postComplete(CompletableFuture.java:510)
	at java.base/java.util.concurrent.CompletableFuture.completeExceptionally(CompletableFuture.java:2194)
	at org.apache.pulsar.metadata.impl.ZKMetadataStore.lambda$batchOperation$7(ZKMetadataStore.java:253)
	at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
	at org.apache.pulsar.metadata.impl.ZKMetadataStore.lambda$batchOperation$9(ZKMetadataStore.java:253)
	at org.apache.pulsar.metadata.impl.PulsarZooKeeperClient$3$1.processResult(PulsarZooKeeperClient.java:524)
	at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:702)
	at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:541)
Caused by: org.apache.pulsar.metadata.api.MetadataStoreException: org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired
	at org.apache.pulsar.metadata.impl.ZKMetadataStore.getException(ZKMetadataStore.java:528)
	at org.apache.pulsar.metadata.impl.ZKMetadataStore.lambda$batchOperation$9(ZKMetadataStore.java:252)
	... 3 more
Caused by: org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired
	at org.apache.zookeeper.KeeperException.create(KeeperException.java:133)
	at org.apache.zookeeper.KeeperException.create(KeeperException.java:53)
	at org.apache.pulsar.metadata.impl.ZKMetadataStore.getException(ZKMetadataStore.java:518)
	... 4 more
Cause 1: org.apache.pulsar.metadata.api.MetadataStoreException: org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired
	at app//org.apache.pulsar.metadata.impl.ZKMetadataStore.getException(ZKMetadataStore.java:528)
	at app//org.apache.pulsar.metadata.impl.ZKMetadataStore.lambda$batchOperation$9(ZKMetadataStore.java:252)
	at app//org.apache.pulsar.metadata.impl.PulsarZooKeeperClient$3$1.processResult(PulsarZooKeeperClient.java:524)
	at app//org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:702)
	at app//org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:541)
Caused by: org.apache.zookeeper.KeeperException$SessionExpiredException: KeeperErrorCode = Session expired
	at app//org.apache.zookeeper.KeeperException.create(KeeperException.java:133)
	at app//org.apache.zookeeper.KeeperException.create(KeeperException.java:53)
	at app//org.apache.pulsar.metadata.impl.ZKMetadataStore.getException(ZKMetadataStore.java:518)
	... 4 more

The race happens during the handoff from the expired ZooKeeper instance to the new one:

  • ZooKeeper can deliver SyncConnected while the new client is still being constructed.
  • ZooKeeperWatcherBase forwards that session event to child watchers.
  • Those child watchers can run before PulsarZooKeeperClient publishes the new ZooKeeper handle.
  • During that window, follow-up operations can still be routed to the old expired handle, and an old async session probe can later overwrite the state of the newly established session.

This can produce extra or incomplete session transitions around ConnectionLost, SessionLost, Reconnected, and SessionReestablished.

Modifications

This change keeps the reconnect flow local to PulsarZooKeeperClient and ZKSessionWatcher.

  • PulsarZooKeeperClient now creates replacement ZooKeeper clients with a forwarding watcher instead of passing watcherManager directly.
  • The forwarding watcher waits until the new ZooKeeper handle has been published before forwarding events to watcherManager.
  • The new handle is published before releasing the forwarding watcher, and waitForConnection() runs after that release because it depends on the forwarded SyncConnected event.
  • ZKSessionWatcher records the session id used for its async exists("/") probe and only applies the probe result if the current session id still matches.
  • The session-id check and state transition are guarded by the same synchronized section so a stale probe cannot race with a new-session event and overwrite the new session state.

Verifying this change

  • Make sure that the change passes the CI checks.

Does this pull request potentially affect one of the following parts:

If the box was checked, please highlight the changes

  • 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.

All reactions
oneby-wang added 4 commits May 31, 2026 08:56
@oneby-wang
Reproduce flaky test
e9ed0c7
@oneby-wang
Fix race condition
80684b1
@oneby-wang
Fix race condition using simpler way
2eb9cb5
@oneby-wang
Remove sleep code
0bdec6d
@oneby-wang oneby-wang changed the title Test reacquire leadership after session lost flaky test [fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZooKeeperClient.clientCreator() Jun 1, 2026
@oneby-wang oneby-wang changed the title [fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZooKeeperClient.clientCreator() [fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZooKeeperClient.clientCreator Jun 1, 2026
@oneby-wang
Modify comment
02e6690
lhotari
lhotari reviewed Jun 1, 2026
View reviewed changes
Comment thread pulsar-metadata/src/main/java/org/apache/pulsar/metadata/impl/PulsarZooKeeperClient.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@oneby-wang
Address pr comment
45dd9ea
@oneby-wang

oneby-wang commented Jun 1, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

@lhotari
I noticed that after receiving a ZooKeeper Expired event, PulsarZooKeeperClient creates a new ZooKeeper instance while continuing to use the same watcherManager.

With the current direct forwarding approach, events from the new ZooKeeper instance are delayed until the new handle is published. However, if the old expired client still has any queued or late events delivered to the same watcherManager(I'm not sure if this will happen.), those stale events could still affect the session state and potentially make the state transitions inconsistent.

Do you think we should add a lightweight generation fencing mechanism here, so that only events from the currently active ZooKeeper instance are forwarded/processed, and stale events from previous instances are ignored?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

lhotari
lhotari approved these changes Jun 2, 2026
View reviewed changes

@lhotari lhotari left a comment

Copy link
Copy Markdown
Member

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.

LGTM, great work @oneby-wang

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

oneby-wang reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@lhotari
lhotari merged commit 5627c01 into apache:master Jun 2, 2026
43 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@lhotari lhotari added this to the 5.0.0-M1 milestone Jun 2, 2026
@lhotari lhotari added release/4.2.2 release/4.0.11 labels Jun 2, 2026
lhotari pushed a commit that referenced this pull request Jun 3, 2026
@oneby-wang @lhotari
[fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZ…
155cb02
…ooKeeperClient.clientCreator (#25910)

(cherry picked from commit 5627c01)
@lhotari lhotari added the cherry-picked/branch-4.2 label Jun 3, 2026
lhotari pushed a commit that referenced this pull request Jun 3, 2026
@oneby-wang @lhotari
[fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZ…
9ebfc3b
…ooKeeperClient.clientCreator (#25910)

(cherry picked from commit 5627c01)
@lhotari lhotari added the cherry-picked/branch-4.0 label Jun 3, 2026
priyanshu-ctds pushed a commit to datastax/pulsar that referenced this pull request Jun 9, 2026
@oneby-wang @priyanshu-ctds
[fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZ…
4817234
…ooKeeperClient.clientCreator (apache#25910)

(cherry picked from commit 5627c01)
(cherry picked from commit 9ebfc3b)
@Technoboy- Technoboy- assigned oneby-wang Jun 24, 2026
@lhotari lhotari mentioned this pull request Jul 25, 2026
[fix][meta] Fix race condition in ZooKeeper session reconnection leading to SESSIONEXPIRED Errors #25237
Closed
1 of 14 tasks
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Aug 28, 2026
@oneby-wang @nodece
[fix][meta] Fix ZooKeeper session reconnect race condition in PulsarZ…
6593c77
…ooKeeperClient.clientCreator (apache#25910)

(cherry picked from commit 5627c01)
@lhotari lhotari mentioned this pull request Sep 8, 2026
[fix][broker] Fix ownership-generation races in OwnershipCache removeOwnership and lock-expiry cleanup #26197
Merged
1 of 11 tasks
@nodece nodece mentioned this pull request Nov 5, 2026
[fix][ml][broker][client] Sync branch-as-4.0 with apache branch-4.0 (2026-09 to 2026-11, 159 commits) ascentstream/pulsar#109
Merged
4 of 5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@lhotari lhotari lhotari approved these changes

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@oneby-wang oneby-wang

Labels

cherry-picked/branch-4.0 cherry-picked/branch-4.2 release/4.0.11 release/4.2.2

Projects

None yet

Milestone

5.0.0-M1

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

2 participants

@oneby-wang @lhotari
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.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.