kazoo
kazoo copied to clipboard
How to auto recovery ephemeral node when reconected?
I use kazoo.KazooRetry try to reconect, but current ephemeral node not recovery.
I want the ephemeral node automatic recovery. so what should do?
code:
retry = KazooRetry(max_tries=-1, delay=1)
zk_client = KazooClient(hosts=zk_hosts, connection_retry=retry)
def listener(state):
if state == KazooState.CONNECTED:
print("state: ", state)
return False
zk_client.add_listener(listener)
zk_client.start()
node_path = "/test_env/test_node"
zk_client.create(node_path, value=b"test", ephemeral=True)
while True:
try:
node_exist = True if zk_client.connected and zk_client.exists(node_path) else False
except kazoo.exceptions.ConnectionLoss:
node_exist = False
print("connected:", zk_client.connected, "node_exists:", node_exist)
time.sleep(5)
output:
state: CONNECTED
connected: True node_exists: True
connected: True node_exists: True
Connection dropped: outstanding heartbeat ping not received
Transition to CONNECTING
connected: False node_exists: False
Connection dropped: socket connection error: Can't assign requested address
Connection dropped: socket connection error: Can't assign requested address
Connection dropped: socket connection error: Can't assign requested address
connected: False node_exists: False
Connection dropped: socket connection error: Can't assign requested address
Connection dropped: socket connection error: Can't assign requested address
Connection dropped: socket connection error: Can't assign requested address
connected: False node_exists: False
connected: False node_exists: False
Session has expired
state: CONNECTED
connected: True node_exists: False
connected: True node_exists: False