Is it possible to watch for events on all descendant nodes in ZooKeeper?
Asked Answered
B

4

9

When using ZooKeeper is it possible to watch for all events on descendant znodes?

for example if I have:

     (/)
     /  \
   (/a)  (/b)
   /       \
(/c)       (/d)

Is there a way to watch the root for changes to /a, /b, /c and /d ?

Baillie answered 23/8, 2012 at 18:41 Comment(0)
W
8

There is no ZooKeeper api to do that, you will have to write your own.

If you are using Curator, you can use PathCache which maintains watches on a node, and that node's children. You can write some code to create more PathCaches as you discover descendants of the root you are watching.

If you are going to roll your own version of this, it is actually quite tricky to get right. This blog describes some of the problems.

Wort answered 24/8, 2012 at 4:7 Comment(0)
L
1

I know this is an old question. I just post here for reference for some other person like me.

I'm looking for this too. Following the tip of @sbridges, I think I could do like this.

PathChildrenCache pathcache = new PathChildrenCache(client, "/some/path", true);

PathChildrenCacheListener listner1 = new PathChildrenCacheListener() {
    Map<String, PathChildrenCache> listener2s = new HashMap<String, PathChildrenCache>();
    @Override
    public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
        String path = event.getData().getPath();

        if(event.getType() == Type.CHILD_ADDED) {
            PathChildrenCache cache2 = new PathChildrenCache(client, path, true);
            cache2.getListenable().addListener(this);
            cache2.start();
            listener2s.put(path, cache2);
        }
        else if(event.getType() == Type.CHILD_REMOVED) {
            PathChildrenCache cache2 = listener2s.remove(path);
            if(cache2 != null) cache2.close();
        }

        System.out.println("" + Thread.currentThread().getId() + "\t" + event);
    }
};
pathcache.getListenable().addListener(listner1);

If someone find something wrong, please tell me.

Lattimore answered 5/11, 2014 at 13:33 Comment(1)
You can loose the child's events between getting notification (CHILD_ADDED) and registering your watch on the childSeabolt
C
1

Use the TreeCache instead of the PathCache

Clang answered 23/12, 2018 at 19:44 Comment(2)
This is a new feature? Since which version?Lattimore
@SswaterShi It's been around since v2.7 (issues.apache.org/jira/browse/CURATOR-33)Clang
R
1

It's possible by using Persistent, Recursive Watches if you upgrade your zookeeper version to 3.6.0. see Persistent, Recursive Watches and Zookeeper Watchers

Also there is Curator Persistent Recursive Watcher support too.

Rocky answered 1/4, 2021 at 1:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.