Skip to content

Commit

Permalink
fix(recipe): conn hangs when TreeCache refreshing
Browse files Browse the repository at this point in the history
The previous implementation of session watcher triggers blocked operations.
If there is a huge tree in ZooKeeper, reconnecting event will lead an
initialized TreeCache into a bad performance state, because the connection
routine was blocked by the session watcher of TreeCache.

This commit put those blocked operations into a background queue to fix this.

There is an example code snippet:

    from kazoo.client import KazooClient
    from kazoo.recipe.cache import TreeCache

    client = KazooClient()
    client.start()

    cache = TreeCache(client, '/a-huge-tree')
    cache.start()

    # Wait the cache be initialized and trigger a connection lost event.

    client.get_children('/')  # The connection is still broken

The patch of this commit has been used in the production environment of
https://github.com/eleme.
  • Loading branch information
tonyseek authored and jeffwidman committed Mar 23, 2018
1 parent db0c2d4 commit 1119413
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions kazoo/recipe/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,9 +189,9 @@ def _session_watcher(self, state):
if state == KazooState.SUSPENDED:
self._publish_event(TreeEvent.CONNECTION_SUSPENDED)
elif state == KazooState.CONNECTED:
with handle_exception(self._error_listeners):
self._root.on_reconnected()
self._publish_event(TreeEvent.CONNECTION_RECONNECTED)
# The session watcher should not be blocked
self._in_background(self._root.on_reconnected)
self._publish_event(TreeEvent.CONNECTION_RECONNECTED)
elif state == KazooState.LOST:
self._is_initialized = False
self._publish_event(TreeEvent.CONNECTION_LOST)
Expand Down

0 comments on commit 1119413

Please sign in to comment.