private InternalAcquireResult internalAcquire1Lease()

in curator-recipes/src/main/java/org/apache/curator/framework/recipes/locks/InterProcessSemaphoreV2.java [305:388]


    private InternalAcquireResult internalAcquire1Lease(
            ImmutableList.Builder<Lease> builder, long startMs, boolean hasWait, long waitMs) throws Exception {
        if (client.getState() != CuratorFrameworkState.STARTED) {
            return InternalAcquireResult.RETURN_NULL;
        }

        if (hasWait) {
            long thisWaitMs = getThisWaitMs(startMs, waitMs);
            if (!lock.acquire(thisWaitMs, TimeUnit.MILLISECONDS)) {
                return InternalAcquireResult.RETURN_NULL;
            }
        } else {
            lock.acquire();
        }

        Lease lease = null;
        boolean success = false;

        try {
            PathAndBytesable<String> createBuilder = client.create()
                    .creatingParentContainersIfNeeded()
                    .withProtection()
                    .withMode(CreateMode.EPHEMERAL_SEQUENTIAL);
            String path = (nodeData != null)
                    ? createBuilder.forPath(ZKPaths.makePath(leasesPath, LEASE_BASE_NAME), nodeData)
                    : createBuilder.forPath(ZKPaths.makePath(leasesPath, LEASE_BASE_NAME));
            String nodeName = ZKPaths.getNodeFromPath(path);
            lease = makeLease(path);

            if (debugAcquireLatch != null) {
                debugAcquireLatch.await();
            }

            try {
                synchronized (this) {
                    for (; ; ) {
                        List<String> children;
                        try {
                            children =
                                    client.getChildren().usingWatcher(watcher).forPath(leasesPath);
                        } catch (Exception e) {
                            if (debugFailedGetChildrenLatch != null) {
                                debugFailedGetChildrenLatch.countDown();
                            }
                            throw e;
                        }
                        if (!children.contains(nodeName)) {
                            log.error("Sequential path not found: " + path);
                            return InternalAcquireResult.RETRY_DUE_TO_MISSING_NODE;
                        }

                        if (children.size() <= maxLeases) {
                            break;
                        }
                        if (hasWait) {
                            long thisWaitMs = getThisWaitMs(startMs, waitMs);
                            if (thisWaitMs <= 0) {
                                return InternalAcquireResult.RETURN_NULL;
                            }
                            if (debugWaitLatch != null) {
                                debugWaitLatch.countDown();
                            }
                            wait(thisWaitMs);
                        } else {
                            if (debugWaitLatch != null) {
                                debugWaitLatch.countDown();
                            }
                            wait();
                        }
                    }
                    success = true;
                }
            } finally {
                if (!success) {
                    returnLease(lease);
                }
                client.removeWatchers();
            }
        } finally {
            lock.release();
        }
        builder.add(Preconditions.checkNotNull(lease));
        return InternalAcquireResult.CONTINUE;
    }