Github user maoling commented on a diff in the pull request:
https://github.com/apache/zookeeper/pull/618#discussion_r240568921
--- Diff:
zookeeper-docs/src/documentation/content/xdocs/zookeeperTutorial.xml ---
@@ -177,58 +165,106 @@ a boolean flag that enables the process to set a
watch. In the code the flag is
* @throws KeeperException
* @throws InterruptedException
*/
+ boolean enter() throws Exception {
+ boolean readyPathExists = zk.exists(readyPath, watcher) !=
null;
- boolean enter() throws KeeperException, InterruptedException{
- zk.create(root + "/" + name, new byte[0], Ids.OPEN_ACL_UNSAFE,
- CreateMode.EPHEMERAL_SEQUENTIAL);
- while (true) {
- synchronized (mutex) {
- List<String> list = zk.getChildren(root, true);
+ zk.create(ourPath, new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
+ return readyPathExists || internalEnter();
+ }
- if (list.size() < size) {
- mutex.wait();
- } else {
- return true;
+ private synchronized boolean internalEnter() throws Exception {
+ boolean result = true;
+ List<String> list = zk.getChildren(barrierPath, false);
+ do {
+ if (list.size() >= size) {
+ try {
+ zk.create(readyPath, new byte[0],
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
+ } catch (KeeperException.NodeExistsException ignore) {
+ // ignore
+ }
+ break;
+ } else {
+ if (!hasBeenNotified.get()) {
+ wait();
}
}
- }
+ } while (false);
--- End diff --
@nameof
you can test this code by printing some logs to verify the
correctnessï¼wrongï¼ of what you thinkï¼hahahaï¼
---