private String pathInForeground()

in curator-framework/src/main/java/org/apache/curator/framework/imps/CreateBuilderImpl.java [1147:1225]


    private String pathInForeground(final String path, final byte[] data, final List<ACL> aclList) throws Exception {
        OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("CreateBuilderImpl-Foreground");

        final AtomicBoolean firstTime = new AtomicBoolean(true);
        String returnPath = RetryLoop.callWithRetry(client.getZookeeperClient(), new Callable<String>() {
            @Override
            public String call() throws Exception {
                boolean localFirstTime = firstTime.getAndSet(false) && !debugForceFindProtectedNode;
                protectedMode.checkSetSessionId(client, createMode);

                String createdPath = null;
                if (!localFirstTime && protectedMode.doProtected()) {
                    debugForceFindProtectedNode = false;
                    createdPath = findProtectedNodeInForeground(path);
                }

                if (createdPath == null) {
                    try {
                        try {
                            if (failBeforeNextCreateForTesting) {
                                failBeforeNextCreateForTesting = false;
                                throw new KeeperException.ConnectionLossException();
                            }
                            createdPath =
                                    client.getZooKeeper().create(path, data, aclList, createMode, storingStat, ttl);
                        } catch (KeeperException.NoNodeException e) {
                            if (createParentsIfNeeded) {
                                ZKPaths.mkdirs(
                                        client.getZooKeeper(),
                                        path,
                                        false,
                                        acling.getACLProviderForParents(),
                                        createParentsAsContainers);
                                createdPath = client.getZooKeeper()
                                        .create(path, data, acling.getAclList(path), createMode, storingStat, ttl);
                            } else {
                                throw e;
                            }
                        }
                    } catch (KeeperException.NodeExistsException e) {
                        if (setDataIfExists) {
                            Stat setStat = client.getZooKeeper().setData(path, data, setDataIfExistsVersion);
                            if (storingStat != null) {
                                DataTree.copyStat(setStat, storingStat);
                            }
                            createdPath = path;
                        } else if (idempotent) {
                            if (failNextIdempotentCheckForTesting) {
                                failNextIdempotentCheckForTesting = false;
                                throw new KeeperException.ConnectionLossException();
                            }
                            Stat getStat = new Stat();
                            byte[] existingData = client.getZooKeeper().getData(path, false, getStat);
                            // check to see if data version == 0 and data matches the idempotent case
                            if (IdempotentUtils.matches(0, data, getStat.getVersion(), existingData)) {
                                if (storingStat != null) {
                                    DataTree.copyStat(getStat, storingStat);
                                }
                                createdPath = path;
                            } else {
                                throw e;
                            }
                        } else {
                            throw e;
                        }
                    }
                }

                if (failNextCreateForTesting) {
                    failNextCreateForTesting = false;
                    throw new KeeperException.ConnectionLossException();
                }
                return createdPath;
            }
        });

        trace.setRequestBytesLength(data).setPath(path).commit();
        return returnPath;
    }