Nextdoor/ndserviceregistry — pull request #44 by diranged (2014-06-03 19:15:50+00:00)
Title: Issue #39: More efficient Watches and fewer zk.get() calls.

The old code was highly reliable, but it resulted in many zk.get() calls
being sent off to Zookeeper in the event that data changed. It also
worked around some very old Kazoo bugs that are no longer in place.

This patch completely reworks the watcher.Watcher object. The code now
directly calls kazoo.recipe.watcher.DataWatcher during the __init__
process to get things kicked off. It passes in a self._update() method
that will be executed every time the data/stat for a node changes.

This new self._update() method no longer calls zk.get() to get the
updated data/stat because its properly passed back from Zookeeper ->
Kazoo -> Watcher. This saves at least 1 zk.get() call on every single
data update.

Additionally, the self._update() method now determines whether or not a
kazoo.recipe.watcher.ChildrenWatcher object should be created (and
whether it has been or not), and creates it when appropriate. This
ChildrenWatcher object calls the self._update_children() method any time
the children are updated.

The new self._update_children() method determines whether it should call
a zk.get() for each of the children in a path (the *old* behavior), or
whether it should simply store the list of children and nothing else
(the *future* behavior). This newer behavior will prevent any zk.get()
calls from being executed at all when data or children changes occur.

Part of the reason for the code re-organization was to make it easier to
unit test it. These new methods are all unit tested.