Remove connection renewal in state handler

Regenerating the ZK connection was fraught with issues, including
duplicate connections, strange failures to reconnect, and various other
wonkiness.

Instead let Kazoo handle states sensibly. Kazoo moves to SUSPENDED state
when it loses connectivity, and stays there indefinitely (based on
cursory tests). And Kazoo seems to always resume from this just fine on
its own. Thus all that hackery did nothing but complicate reconnection.

This therefore turns the listener into a purely informational function,
providing logs of when/why it failed, and we also add some additional
output messages during initial connection and final disconnection.
This commit is contained in:
Joshua Boniface 2021-07-07 11:50:14 -04:00
parent 65d14ccd92
commit 80f04ce8ee
1 changed files with 4 additions and 16 deletions

View File

@ -125,23 +125,9 @@ class ZKHandler(object):
# #
def listener(self, state): def listener(self, state):
if state == KazooState.CONNECTED: if state == KazooState.CONNECTED:
self.log('Connection to Zookeeper started', state='o') self.log('Connection to Zookeeper resumed', state='o')
else: else:
self.log('Connection to Zookeeper lost', state='w') self.log('Connection to Zookeeper lost with state {}'.format(state), state='w')
while True:
time.sleep(0.5)
_zk_conn = KazooClient(hosts=self.coordinators)
try:
_zk_conn.start()
except Exception:
del _zk_conn
continue
self.zk_conn = _zk_conn
self.zk_conn.add_listener(self.listener)
break
def connect(self, persistent=False): def connect(self, persistent=False):
""" """
@ -149,6 +135,7 @@ class ZKHandler(object):
""" """
try: try:
self.zk_conn.start() self.zk_conn.start()
self.log('Connection to Zookeeper started', state='o')
if persistent: if persistent:
self.zk_conn.add_listener(self.listener) self.zk_conn.add_listener(self.listener)
except Exception as e: except Exception as e:
@ -162,6 +149,7 @@ class ZKHandler(object):
""" """
self.zk_conn.stop() self.zk_conn.stop()
self.zk_conn.close() self.zk_conn.close()
self.log('Connection to Zookeeper terminated', state='o')
# #
# Schema helper actions # Schema helper actions