Log Zookeeper session on connection state change

In order to identify e.g. the ephemeral owner of a node in Zookeeper it
is helpful to log the Zookeeper session ID when the connection is
established or changes.

Change-Id: I8a3645497ff73541b15d18ba32c20a7629792418
This commit is contained in:
Simon Westphahl 2024-03-27 12:17:49 +01:00
parent e1ad2443a3
commit 9aea549305
No known key found for this signature in database
1 changed files with 10 additions and 3 deletions

View File

@ -83,8 +83,13 @@ class ZooKeeperClient(object):
.. warning:: This method must not block. .. warning:: This method must not block.
""" """
session_id = 0
if client_id := self.client.client_id:
session_id, _ = client_id
if state == KazooState.LOST: if state == KazooState.LOST:
self.log.debug("ZooKeeper connection: LOST") self.log.debug("ZooKeeper connection (session: %#x): LOST",
session_id)
self.was_lost = True self.was_lost = True
for listener in self.on_connection_lost_listeners: for listener in self.on_connection_lost_listeners:
try: try:
@ -92,14 +97,16 @@ class ZooKeeperClient(object):
except Exception: except Exception:
self.log.exception("Exception calling listener:") self.log.exception("Exception calling listener:")
elif state == KazooState.SUSPENDED: elif state == KazooState.SUSPENDED:
self.log.debug("ZooKeeper connection: SUSPENDED") self.log.debug("ZooKeeper connection (session: %#x): SUSPENDED",
session_id)
for listener in self.on_suspended_listeners: for listener in self.on_suspended_listeners:
try: try:
listener() listener()
except Exception: except Exception:
self.log.exception("Exception calling listener:") self.log.exception("Exception calling listener:")
else: else:
self.log.debug("ZooKeeper connection: CONNECTED") self.log.debug("ZooKeeper connection (session: %#x): CONNECTED",
session_id)
# Create a throwaway thread since zk operations can't # Create a throwaway thread since zk operations can't
# happen in this one. # happen in this one.
if self.was_lost: if self.was_lost: