Search in sources :

Example 61 with CancellationException

use of java.util.concurrent.CancellationException in project etcd-java by IBM.

the class PersistentLeaseKey method putKey.

// called only from our serialized executor context
protected void putKey(long leaseId) {
    if (leaseId == 0L || closeFuture != null)
        return;
    if (updateFuture != null && !updateFuture.isDone()) {
        // if the cancellation wins then putKey will be immediately retried
        updateFuture.cancel(false);
        return;
    }
    // execute a transaction which either sets the lease on an existing key
    // or creates the key with the lease if it doesn't exist
    PutRequest.Builder putBld = PutRequest.newBuilder().setKey(key).setLease(leaseId);
    KvClient.FluentTxnRequest req = client.getKvClient().txnIf().exists(key).backoffRetry(() -> closeFuture == null && isActive());
    ListenableFuture<? extends Object> fut;
    ListenableFuture<TxnResponse> txnFut;
    if (rangeCache == null) {
        fut = txnFut = req.then().put(putBld.setIgnoreValue(true)).elseDo().put(putBld.setIgnoreValue(false).setValue(defaultValue)).async();
    } else {
        RangeRequest getOp = RangeRequest.newBuilder().setKey(key).build();
        txnFut = req.then().put(putBld.setIgnoreValue(true)).get(getOp).elseDo().put(putBld.setIgnoreValue(false).setValue(defaultValue)).get(getOp).async();
        fut = Futures.transform(txnFut, (Function<TxnResponse, Object>) tr -> rangeCache.offerUpdate(tr.getResponses(1).getResponseRange().getKvs(0), false));
    }
    if (!isDone())
        fut = Futures.transform(fut, (Function<Object, Object>) r -> set(key));
    // this callback is to trigger an immediate retry in case the attempt was cancelled by a more
    // recent lease state change to active
    Futures.addCallback(fut, (FutureListener<Object>) (v, t) -> {
        if (t instanceof CancellationException && isActive())
            putKey(leaseId);
    }, executor);
    updateFuture = fut;
}
Also used : LeaseState(com.ibm.etcd.client.lease.PersistentLease.LeaseState) MoreExecutors(com.google.common.util.concurrent.MoreExecutors) Function(com.google.common.base.Function) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) CancellationException(java.util.concurrent.CancellationException) Executor(java.util.concurrent.Executor) EtcdClient(com.ibm.etcd.client.EtcdClient) FutureListener(com.ibm.etcd.client.FutureListener) KvClient(com.ibm.etcd.client.kv.KvClient) SettableFuture(com.google.common.util.concurrent.SettableFuture) TxnResponse(com.ibm.etcd.api.TxnResponse) SerializingExecutor(com.ibm.etcd.client.SerializingExecutor) ByteString(com.google.protobuf.ByteString) ListenerObserver(com.ibm.etcd.client.ListenerObserver) Futures(com.google.common.util.concurrent.Futures) PersistentLease(com.ibm.etcd.client.lease.PersistentLease) RangeRequest(com.ibm.etcd.api.RangeRequest) PutRequest(com.ibm.etcd.api.PutRequest) AbstractFuture(com.google.common.util.concurrent.AbstractFuture) Function(com.google.common.base.Function) CancellationException(java.util.concurrent.CancellationException) RangeRequest(com.ibm.etcd.api.RangeRequest) PutRequest(com.ibm.etcd.api.PutRequest) KvClient(com.ibm.etcd.client.kv.KvClient) TxnResponse(com.ibm.etcd.api.TxnResponse)

Example 62 with CancellationException

use of java.util.concurrent.CancellationException in project bookkeeper by apache.

the class ZKDistributedLock method asyncAcquire.

/**
 * Asynchronously acquire the lock. Technically the try phase of this operation--which adds us to the waiter
 * list--is executed synchronously, but the lock wait itself doesn't block.
 */
@Override
public synchronized CompletableFuture<ZKDistributedLock> asyncAcquire() {
    if (null != lockAcquireFuture) {
        return FutureUtils.exception(new UnexpectedException("Someone is already acquiring/acquired lock " + lockPath));
    }
    final CompletableFuture<ZKDistributedLock> promise = FutureUtils.createFuture();
    promise.whenComplete((zkDistributedLock, throwable) -> {
        if (null == throwable || !(throwable instanceof CancellationException)) {
            return;
        }
        lockStateExecutor.executeOrdered(lockPath, () -> asyncClose());
    });
    final Stopwatch stopwatch = Stopwatch.createStarted();
    promise.whenComplete(new FutureEventListener<ZKDistributedLock>() {

        @Override
        public void onSuccess(ZKDistributedLock lock) {
            acquireStats.registerSuccessfulEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
        }

        @Override
        public void onFailure(Throwable cause) {
            acquireStats.registerFailedEvent(stopwatch.stop().elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
            // release the lock if fail to acquire
            asyncClose();
        }
    });
    this.lockAcquireFuture = promise;
    lockStateExecutor.executeOrdered(lockPath, () -> doAsyncAcquireWithSemaphore(promise, lockTimeout));
    return promise;
}
Also used : UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) CancellationException(java.util.concurrent.CancellationException) Stopwatch(com.google.common.base.Stopwatch)

Example 63 with CancellationException

use of java.util.concurrent.CancellationException in project bookkeeper by apache.

the class ZKSessionLockFactory method createLock.

@Override
public CompletableFuture<SessionLock> createLock(String lockPath, DistributedLockContext context) {
    AtomicInteger numRetries = new AtomicInteger(lockCreationRetries);
    final AtomicReference<Throwable> interruptedException = new AtomicReference<Throwable>(null);
    CompletableFuture<SessionLock> createPromise = FutureUtils.createFuture();
    createPromise.whenComplete((value, cause) -> {
        if (null != cause && cause instanceof CancellationException) {
            interruptedException.set(cause);
        }
    });
    createLock(lockPath, context, interruptedException, numRetries, createPromise, 0L);
    return createPromise;
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CancellationException(java.util.concurrent.CancellationException) AtomicReference(java.util.concurrent.atomic.AtomicReference)

Example 64 with CancellationException

use of java.util.concurrent.CancellationException in project bookkeeper by apache.

the class TestAsyncReaderLock method testReaderLockFutureCancelledWhileWaiting.

@Test(timeout = 60000)
public void testReaderLockFutureCancelledWhileWaiting() throws Exception {
    String name = runtime.getMethodName();
    DistributedLogManager dlm0 = createNewDLM(conf, name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm0.startAsyncLogSegmentNonPartitioned());
    writer.write(DLMTestUtil.getLogRecordInstance(1L));
    writer.write(DLMTestUtil.getLogRecordInstance(2L));
    writer.closeAndComplete();
    DistributedLogManager dlm1 = createNewDLM(conf, name);
    CompletableFuture<AsyncLogReader> futureReader1 = dlm1.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
    AsyncLogReader reader1 = Utils.ioResult(futureReader1);
    DistributedLogManager dlm2 = createNewDLM(conf, name);
    CompletableFuture<AsyncLogReader> futureReader2 = dlm2.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
    try {
        futureReader2.cancel(true);
        Utils.ioResult(futureReader2);
        fail("Should fail getting log reader as it is cancelled");
    } catch (CancellationException ce) {
    } catch (LockClosedException ex) {
    } catch (LockCancelledException ex) {
    } catch (OwnershipAcquireFailedException oafe) {
    }
    futureReader2 = dlm2.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
    Utils.close(reader1);
    Utils.ioResult(futureReader2);
    dlm0.close();
    dlm1.close();
    dlm2.close();
}
Also used : OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) LockCancelledException(org.apache.distributedlog.exceptions.LockCancelledException) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) CancellationException(java.util.concurrent.CancellationException) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) LockClosedException(org.apache.distributedlog.lock.LockClosedException) Test(org.junit.Test)

Example 65 with CancellationException

use of java.util.concurrent.CancellationException in project bookkeeper by apache.

the class ZKLogStreamMetadataStore method ensureReadLockPathExist.

// 
// Create Read Lock
// 
private CompletableFuture<Void> ensureReadLockPathExist(final LogMetadata logMetadata, final String readLockPath) {
    final CompletableFuture<Void> promise = new CompletableFuture<Void>();
    promise.whenComplete((value, cause) -> {
        if (cause instanceof CancellationException) {
            FutureUtils.completeExceptionally(promise, new LockCancelledException(readLockPath, "Could not ensure read lock path", cause));
        }
    });
    Optional<String> parentPathShouldNotCreate = Optional.of(logMetadata.getLogRootPath());
    Utils.zkAsyncCreateFullPathOptimisticRecursive(zooKeeperClient, readLockPath, parentPathShouldNotCreate, new byte[0], zooKeeperClient.getDefaultACL(), CreateMode.PERSISTENT, new org.apache.zookeeper.AsyncCallback.StringCallback() {

        @Override
        public void processResult(final int rc, final String path, Object ctx, String name) {
            if (KeeperException.Code.NONODE.intValue() == rc) {
                FutureUtils.completeExceptionally(promise, new LogNotFoundException(String.format("Log %s does not exist or has been deleted", logMetadata.getFullyQualifiedName())));
            } else if (KeeperException.Code.OK.intValue() == rc) {
                FutureUtils.complete(promise, null);
                LOG.trace("Created path {}.", path);
            } else if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                FutureUtils.complete(promise, null);
                LOG.trace("Path {} is already existed.", path);
            } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
                FutureUtils.completeExceptionally(promise, new ZooKeeperClient.ZooKeeperConnectionException(path));
            } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
                FutureUtils.completeExceptionally(promise, new DLInterruptedException(path));
            } else {
                FutureUtils.completeExceptionally(promise, KeeperException.create(KeeperException.Code.get(rc)));
            }
        }
    }, null);
    return promise;
}
Also used : LockCancelledException(org.apache.distributedlog.exceptions.LockCancelledException) AsyncCallback(org.apache.zookeeper.AsyncCallback) ZooKeeperConnectionException(org.apache.distributedlog.ZooKeeperClient.ZooKeeperConnectionException) CompletableFuture(java.util.concurrent.CompletableFuture) CancellationException(java.util.concurrent.CancellationException) ZooKeeperClient(org.apache.distributedlog.ZooKeeperClient) DLInterruptedException(org.apache.distributedlog.exceptions.DLInterruptedException) LogNotFoundException(org.apache.distributedlog.exceptions.LogNotFoundException)

Aggregations

CancellationException (java.util.concurrent.CancellationException)505 ExecutionException (java.util.concurrent.ExecutionException)150 Test (org.junit.Test)91 TimeoutException (java.util.concurrent.TimeoutException)76 ArrayList (java.util.ArrayList)47 CountDownLatch (java.util.concurrent.CountDownLatch)46 Future (java.util.concurrent.Future)46 IOException (java.io.IOException)42 ExecutorService (java.util.concurrent.ExecutorService)30 RejectedExecutionException (java.util.concurrent.RejectedExecutionException)29 CompletableFuture (java.util.concurrent.CompletableFuture)28 Callable (java.util.concurrent.Callable)27 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)27 List (java.util.List)24 Map (java.util.Map)23 HashMap (java.util.HashMap)22 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 TimeUnit (java.util.concurrent.TimeUnit)20 CharStream (org.antlr.v4.runtime.CharStream)20 CommonTokenStream (org.antlr.v4.runtime.CommonTokenStream)20