Class SubTreeCache
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.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic interfaceA tree provider.classA watch guard. -
Field Summary
FieldsModifier and TypeFieldDescription(package private) Set<org.apache.zookeeper.Watcher>(package private) SubTreeCache.TreeProvider -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidcancelWatcher(org.apache.zookeeper.Watcher watcher) Cancel a watcher (noop if not registered or already fired).getChildren(String path) Returns children of node.voidregisterWatcher(org.apache.zookeeper.Watcher watcher) Register a watcher.registerWatcherWithGuard(org.apache.zookeeper.Watcher watcher) Register watcher and get interest guard object which can be used with try-with-resources.
-
Field Details
-
provider
SubTreeCache.TreeProvider provider -
pendingWatchers
Set<org.apache.zookeeper.Watcher> pendingWatchers -
cachedNodes
-
-
Constructor Details
-
SubTreeCache
-
-
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.KeeperExceptionInterruptedException
-
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
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
-