Search in sources :

Example 6 with OwnershipAcquireFailedException

use of org.apache.distributedlog.exceptions.OwnershipAcquireFailedException in project bookkeeper by apache.

the class ZKSessionLock method asyncTryLock.

@Override
public CompletableFuture<LockWaiter> asyncTryLock(final long timeout, final TimeUnit unit) {
    final CompletableFuture<String> result = new CompletableFuture<String>();
    final boolean wait = DistributedLogConstants.LOCK_IMMEDIATE != timeout;
    if (wait) {
        asyncTryLock(wait, result);
    } else {
        // try to check locks first
        zk.getChildren(lockPath, null, new AsyncCallback.Children2Callback() {

            @Override
            public void processResult(final int rc, String path, Object ctx, final List<String> children, Stat stat) {
                lockStateExecutor.executeOrdered(lockPath, new SafeRunnable() {

                    @Override
                    public void safeRun() {
                        if (!lockState.inState(State.INIT)) {
                            result.completeExceptionally(new LockStateChangedException(lockPath, lockId, State.INIT, lockState.getState()));
                            return;
                        }
                        if (KeeperException.Code.OK.intValue() != rc) {
                            result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc)));
                            return;
                        }
                        FailpointUtils.checkFailPointNoThrow(FailpointUtils.FailPointName.FP_LockTryAcquire);
                        Collections.sort(children, MEMBER_COMPARATOR);
                        if (children.size() > 0) {
                            asyncParseClientID(zk, lockPath, children.get(0)).whenCompleteAsync(new FutureEventListener<Pair<String, Long>>() {

                                @Override
                                public void onSuccess(Pair<String, Long> owner) {
                                    if (!checkOrClaimLockOwner(owner, result)) {
                                        acquireFuture.complete(false);
                                    }
                                }

                                @Override
                                public void onFailure(final Throwable cause) {
                                    result.completeExceptionally(cause);
                                }
                            }, lockStateExecutor.chooseThread(lockPath));
                        } else {
                            asyncTryLock(wait, result);
                        }
                    }
                });
            }
        }, null);
    }
    final CompletableFuture<Boolean> waiterAcquireFuture = FutureUtils.createFuture();
    waiterAcquireFuture.whenComplete((value, cause) -> acquireFuture.completeExceptionally(cause));
    return result.thenApply(new Function<String, LockWaiter>() {

        @Override
        public LockWaiter apply(final String currentOwner) {
            final Exception acquireException = new OwnershipAcquireFailedException(lockPath, currentOwner);
            FutureUtils.within(acquireFuture, timeout, unit, acquireException, lockStateExecutor, lockPath).whenComplete(new FutureEventListener<Boolean>() {

                @Override
                public void onSuccess(Boolean acquired) {
                    completeOrFail(acquireException);
                }

                @Override
                public void onFailure(final Throwable acquireCause) {
                    completeOrFail(acquireException);
                }

                private void completeOrFail(final Throwable acquireCause) {
                    if (isLockHeld()) {
                        waiterAcquireFuture.complete(true);
                    } else {
                        asyncUnlock().whenComplete(new FutureEventListener<Void>() {

                            @Override
                            public void onSuccess(Void value) {
                                waiterAcquireFuture.completeExceptionally(acquireCause);
                            }

                            @Override
                            public void onFailure(Throwable cause) {
                                waiterAcquireFuture.completeExceptionally(acquireCause);
                            }
                        });
                    }
                }
            });
            return new LockWaiter(lockId.getLeft(), currentOwner, waiterAcquireFuture);
        }
    });
}
Also used : AsyncCallback(org.apache.zookeeper.AsyncCallback) CompletableFuture(java.util.concurrent.CompletableFuture) Stat(org.apache.zookeeper.data.Stat) Pair(org.apache.commons.lang3.tuple.Pair) OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LockingException(org.apache.distributedlog.exceptions.LockingException) TimeoutException(java.util.concurrent.TimeoutException) UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) ZKException(org.apache.distributedlog.exceptions.ZKException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) FutureEventListener(org.apache.bookkeeper.common.concurrent.FutureEventListener)

Example 7 with OwnershipAcquireFailedException

use of org.apache.distributedlog.exceptions.OwnershipAcquireFailedException in project bookkeeper by apache.

the class ZKSessionLock method tryLock.

@Override
public void tryLock(long timeout, TimeUnit unit) throws LockingException {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    CompletableFuture<LockWaiter> tryFuture = asyncTryLock(timeout, unit);
    LockWaiter waiter = waitForTry(stopwatch, tryFuture);
    boolean acquired = waiter.waitForAcquireQuietly();
    if (!acquired) {
        throw new OwnershipAcquireFailedException(lockPath, waiter.getCurrentOwner());
    }
}
Also used : OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) Stopwatch(com.google.common.base.Stopwatch)

Aggregations

OwnershipAcquireFailedException (org.apache.distributedlog.exceptions.OwnershipAcquireFailedException)7 LockingException (org.apache.distributedlog.exceptions.LockingException)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IOException (java.io.IOException)2 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)2 Test (org.junit.Test)2 Stopwatch (com.google.common.base.Stopwatch)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)1 LockCancelledException (org.apache.distributedlog.exceptions.LockCancelledException)1 ZKException (org.apache.distributedlog.exceptions.ZKException)1