Most efficient way to create a path in zookeeper where root elements of the path may or may not exist?

自闭症网瘾萝莉.ら 提交于 2019-12-03 07:09:53

An exist call can be made with 1 round trip from the server to the client.

A create call has the same round trip, but create is a write operation that entails a couple more round trips between the servers in the zk cluster, so a create is a little more expensive that an exist.

So the total time for your algorithm is,

Time for 1 read op * Probability node already exists + (Time for 1 write op) * (1 - Probability the node already exists).

So either of if(!exist()) create() vs create() could be faster. In the end it probably doesn't matter.

If you want to be really fast, you can use the async api so that you can create all the components of your path without waiting for the server to respond to requests 1 by 1.

final AtomicBoolean success = new AtomicBoolean(false);
final CountdownLatch latch = new CountdownLatch(1);
StringCallback cb = new StringCallback() {
    processResult(int rc, String path, Object ctx, String name) {
        if(name.equals(pathString ) {
            //wait for the last path
            success.set(rc == KeeperException.Code.NODEEXISTS ||
                        rc == KeeperException.Code.OK);
            latch.countDown();
        }
    }
};

StringBuilder path = new StringBuilder();
for (String pathElement : pathParts) {
    path.append(UNIX_FILE_SEPARATOR).append(pathElement);
    String pathString = path.toString();
    //send requests to create all parts of the path without waiting for the
    //results of previous calls to return
    zooKeeper.create(pathString, null, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, cb);
}
latch.await();
if(!success.get()) { 
    throw ...
}

You can use Netflix's curator library which makes using zookeeper much simpler

client.create().withMode(CreateMode.PERSISTENT).forPath("/root/child1/child2/child3", new byte[0]).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).creatingParentsIfNeeded();
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!