Skip to content

[fix][meta] Don't lose SessionReestablished when a listener invalidates the new session - #26861

Merged
merlimat merged 1 commit into
masterfrom
lh-fix-zk-session-reestablished-lost
Oct 12, 2026
Merged

merlimat merged 1 commit into
masterfrom
lh-fix-zk-session-reestablished-lost

Conversation

@lhotari

@lhotari lhotari commented Oct 12, 2026

Copy link
Copy Markdown
Member

Fixes #26860

Motivation

When a broker's ZooKeeper session is lost and a new one is established, ZKSessionWatcher sends Reconnected and SessionReestablished. ZKMetadataStore recreates its persistent watch before it forwards SessionReestablished to the other listeners, such as LockManagerImpl, which revalidates the broker's locks, its bundle ownerships among them. When recreating the watch fails, the store marks the session invalid with ZKSessionWatcher.setSessionInvalid() and makes PulsarZooKeeperClient replace the session, so that the next session sends SessionReestablished again.

When the watch fails while ZKSessionWatcher.checkState is still notifying the listeners, on the same thread, checkState overwrites the invalid status with SessionReestablished after the listeners return. The replaced handle's Closed event and the new session's SyncConnected then send nothing, and the lock managers never get SessionReestablished.

In a run of network/metadata-takeover with the network conditions of the Pulsar Performance Testing Framework (#26859), this left a broker whose session had expired with bundle ownerships that another broker had taken over: both brokers served the bundles' topics and fenced each other's ledgers, and about 16,000 sends were never acknowledged. #26860 has the broker's log.

Modifications

  • ZKSessionWatcher.checkState sets the status to SessionReestablished before notifying the listeners, and decides whether to send SessionReestablished from the previous status. A status that a listener sets during the notification stays in effect. SessionReestablished is sent even if notifying Reconnected throws.
  • ZKMetadataStore, when it fails to recreate its watch on a PulsarZooKeeperClient, marks the session replaced with the new ZKSessionWatcher.setSessionReplaced(), which records the session's id. Until another session connects, the replaced session's own events, such as a periodic connection check that still succeeds before PulsarZooKeeperClient closes it, don't reestablish it: otherwise the new session's SyncConnected would find the status SessionReestablished already and send nothing, the same loss. With another ZooKeeper client, which isn't replaced, the store marks the session invalid as before, so that the next connection of the same session sends SessionReestablished and the store tries the watch again.
  • checkState ignores Closed events. A Closed event comes from a ZooKeeper handle that was closed: the previous handle when PulsarZooKeeperClient replaces an expired session, or the client when the store closes. It says nothing about the current handle's connection. Before, it fell into the default branch, which treats any other state as a reconnection, so a replaced handle's Closed event could send SessionReestablished before the new session was connected.

Verifying this change

  • Make sure that the change passes the CI checks.

This change added tests and can be verified as follows:

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

…es the new session

ZKSessionWatcher.checkState overwrote the status that a listener set while it was notified: when
ZKMetadataStore failed to recreate its persistent watch on SessionReestablished and marked the
session invalid, checkState set SessionReestablished again after the listeners returned, so the
next session sent nothing and the lock managers never revalidated their locks.

- checkState sets the status before notifying the listeners and decides from the previous status
  whether to send SessionReestablished, so a status set during the notification stays in effect.
- ZKMetadataStore marks a PulsarZooKeeperClient's session replaced, by its session id, so that the
  replaced session's own events don't reestablish it before the next session connects.
- checkState ignores Closed events, which come from a closed ZooKeeper handle and say nothing about
  the current handle's connection.

Fixes #26860

Assisted-by: Claude Code (claude-opus-5-5)
@merlimat
merlimat merged commit cc346d2 into master Oct 12, 2026
44 checks passed
lhotari added a commit that referenced this pull request Oct 12, 2026
lhotari added a commit that referenced this pull request Oct 13, 2026
lhotari added a commit that referenced this pull request Oct 13, 2026
nodece pushed a commit to ascentstream/pulsar that referenced this pull request Nov 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug][meta] SessionReestablished is lost when recreating ZKMetadataStore's persistent watch fails, so the broker never revalidates its locks

2 participants