Class SubTreeCache

java.lang.Object
org.apache.bookkeeper.util.SubTreeCache

public class SubTreeCache extends Object
Caching layer for traversing and monitoring changes on a znode subtree.

ZooKeeper does not provide a way to perform a recursive watch on a subtree. In order to detect changes to a subtree, we need to maintain a cache of nodes which have been listed and have not changed since. This would mirror the set of nodes with live watches in ZooKeeper (since we can't cancel them at the moment).

In order to avoid having to pre-read the whole subtree up front, we'll weaken the guarantee to only require firing the watcher for updates on nodes read since the watcher was registered which happened after the read. We'll also permit spurious events elsewhere in the tree to avoid having to distinguish between nodes which were read before and after a watch was established.

Finally, we'll allow (require, even) the user to cancel a registered watcher once no longer interested.

  • Field Details

    • provider

    • pendingWatchers

      Set<org.apache.zookeeper.Watcher> pendingWatchers
    • cachedNodes

      Map<String,org.apache.bookkeeper.util.SubTreeCache.SubTreeNode> cachedNodes
  • Constructor Details

  • Method Details

    • getChildren

      public List<String> getChildren(String path) throws org.apache.zookeeper.KeeperException, InterruptedException
      Returns children of node.
      Parameters:
      path - Path of which to get children
      Returns:
      Children of path
      Throws:
      org.apache.zookeeper.KeeperException
      InterruptedException
    • registerWatcher

      public void registerWatcher(org.apache.zookeeper.Watcher watcher)
      Register a watcher.

      See class header for semantics.

      Parameters:
      watcher - watcher to register
    • cancelWatcher

      public void cancelWatcher(org.apache.zookeeper.Watcher watcher)
      Cancel a watcher (noop if not registered or already fired).
      Parameters:
      watcher - Watcher object to cancel
    • registerWatcherWithGuard

      public SubTreeCache.WatchGuard registerWatcherWithGuard(org.apache.zookeeper.Watcher watcher)
      Register watcher and get interest guard object which can be used with try-with-resources.

      It's important not to leak watchers into this structure. The returned WatchGuard can be used to ensure that the watch is unregistered upon exiting a scope.

      Parameters:
      watcher - Watcher to register