Can I recursively create a path in Zookeeper?

你说的曾经没有我的故事 提交于 2021-02-07 18:20:24

问题


I'm pulling ZooKeeper into a project for some concurrency management, and the first thing I tried was something that, to me, was quite obvious (using the zkpython binding):

zh = zookeeper.init('localhost:2181')
zookeeper.create(zh, '/path/to/a/node', '', [ZOO_OPEN_ACL_UNSAFE])

And I got back a NoNodeException for my trouble.

After reflecting on this and reviewing the docs (such as they are), I've been unable to find a way to do the equivalent of a mkdir -p where ZooKeeper will create the missing parent nodes for me.

Am I missing anything, or am I just stuck issuing separate create()s for each part of a path whether I like it or not?


回答1:


You're stuck to issue separate create()s for each element of the path. Zookeeper has only atomic operations build in. Creating a path recursively is not an atomic operation anymore. Zookeeper could not know, what you want it to do, if the operation hangs after creating half of the path elements. I don't know, if there's already a Zookeeper helper library in python. There is one in java (zkClient) that will let you create recursive paths by calling create() multiple times.




回答2:


If you issue separate create()s, you might get interrupted or fail when you're halfway through. To make the call atomic, you can use the new multi() API. See this answer.

If the path or a part of it might already exist, then waiting for each create() to finish before issuing the next would be unnecessarily slow. In this case, you can use the asynchronous API to speed up the process. See this answer.

If you just want to avoid the extra calls, you can use Netflix's curator library which has a creatingParentsIfNeeded method, but be advised that it might be slow. See this answer.




回答3:


Kazoo has an ensure_path(path) operation, although it isn't considered atomic. Using this would at least save you the need to write your own code for a recursive create.



来源:https://stackoverflow.com/questions/3340756/can-i-recursively-create-a-path-in-zookeeper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!