Search in sources :

Example 11 with LockingException

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

the class TestAsyncReaderLock method testReaderLockSessionExpires.

@Test(timeout = 60000)
public void testReaderLockSessionExpires() throws Exception {
    String name = runtime.getMethodName();
    URI uri = createDLMURI("/" + name);
    ensureURICreated(uri);
    Namespace ns0 = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    DistributedLogManager dlm0 = ns0.openLog(name);
    BKAsyncLogWriter writer = (BKAsyncLogWriter) (dlm0.startAsyncLogSegmentNonPartitioned());
    writer.write(DLMTestUtil.getLogRecordInstance(1L));
    writer.write(DLMTestUtil.getLogRecordInstance(2L));
    writer.closeAndComplete();
    Namespace ns1 = NamespaceBuilder.newBuilder().conf(conf).uri(uri).build();
    DistributedLogManager dlm1 = ns1.openLog(name);
    CompletableFuture<AsyncLogReader> futureReader1 = dlm1.getAsyncLogReaderWithLock(DLSN.InitialDLSN);
    AsyncLogReader reader1 = Utils.ioResult(futureReader1);
    ZooKeeperClientUtils.expireSession(((BKNamespaceDriver) ns1.getNamespaceDriver()).getWriterZKC(), zkServers, 1000);
    // The result of expireSession is somewhat non-deterministic with this lock.
    // It may fail with LockingException or it may succesfully reacquire, so for
    // the moment rather than make it deterministic we accept either result.
    boolean success = false;
    try {
        Utils.ioResult(reader1.readNext());
        success = true;
    } catch (LockingException ex) {
    }
    if (success) {
        Utils.ioResult(reader1.readNext());
    }
    Utils.close(reader1);
    dlm0.close();
    ns0.close();
    dlm1.close();
    ns1.close();
}
Also used : LockingException(org.apache.distributedlog.exceptions.LockingException) AsyncLogReader(org.apache.distributedlog.api.AsyncLogReader) DistributedLogManager(org.apache.distributedlog.api.DistributedLogManager) URI(java.net.URI) Namespace(org.apache.distributedlog.api.namespace.Namespace) Test(org.junit.Test)

Example 12 with LockingException

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

the class ZKSessionLock method waitForTry.

synchronized LockWaiter waitForTry(Stopwatch stopwatch, CompletableFuture<LockWaiter> tryFuture) throws LockingException {
    boolean success = false;
    boolean stateChanged = false;
    LockWaiter waiter;
    try {
        waiter = FutureUtils.result(tryFuture, lockOpTimeout, TimeUnit.MILLISECONDS);
        success = true;
    } catch (LockStateChangedException ex) {
        stateChanged = true;
        throw ex;
    } catch (LockingException ex) {
        throw ex;
    } catch (TimeoutException toe) {
        tryTimeouts.inc();
        throw new LockingException(lockPath, "Timeout during try phase of lock acquire", toe);
    } catch (Exception ex) {
        String message = getLockId() + " failed to lock " + lockPath;
        throw new LockingException(lockPath, message, ex);
    } finally {
        if (success) {
            tryStats.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
        } else {
            tryStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
        }
        // Exception, i.e. an Error
        if (!success && !stateChanged) {
            unlock();
        }
    }
    return waiter;
}
Also used : LockingException(org.apache.distributedlog.exceptions.LockingException) 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) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with LockingException

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

the class ZKDistributedLock method reacquireLock.

private CompletableFuture<ZKDistributedLock> reacquireLock(boolean throwLockAcquireException) throws LockingException {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    CompletableFuture<ZKDistributedLock> lockPromise;
    synchronized (this) {
        if (closed) {
            throw newLockClosedException();
        }
        if (null != lockReacquireException) {
            if (throwLockAcquireException) {
                throw lockReacquireException;
            } else {
                return null;
            }
        }
        if (null != lockReacquireFuture) {
            return lockReacquireFuture;
        }
        LOG.info("reacquiring lock at {}", lockPath);
        lockReacquireFuture = lockPromise = new CompletableFuture<ZKDistributedLock>();
        lockReacquireFuture.whenComplete(new FutureEventListener<ZKDistributedLock>() {

            @Override
            public void onSuccess(ZKDistributedLock lock) {
                // if re-acquire successfully, clear the state.
                synchronized (ZKDistributedLock.this) {
                    lockReacquireFuture = null;
                }
                reacquireStats.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
            }

            @Override
            public void onFailure(Throwable cause) {
                synchronized (ZKDistributedLock.this) {
                    if (cause instanceof LockingException) {
                        lockReacquireException = (LockingException) cause;
                    } else {
                        lockReacquireException = new LockingException(lockPath, "Exception on re-acquiring lock", cause);
                    }
                }
                reacquireStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS), TimeUnit.MICROSECONDS);
            }
        });
    }
    reacquireCountUpdater.incrementAndGet(this);
    internalReacquireLock(new AtomicInteger(Integer.MAX_VALUE), 0, lockPromise);
    return lockPromise;
}
Also used : LockingException(org.apache.distributedlog.exceptions.LockingException) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stopwatch(com.google.common.base.Stopwatch)

Aggregations

LockingException (org.apache.distributedlog.exceptions.LockingException)13 CountDownLatch (java.util.concurrent.CountDownLatch)6 OwnershipAcquireFailedException (org.apache.distributedlog.exceptions.OwnershipAcquireFailedException)5 IOException (java.io.IOException)4 Test (org.junit.Test)4 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)3 ZKException (org.apache.distributedlog.exceptions.ZKException)3 URI (java.net.URI)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)2 Namespace (org.apache.distributedlog.api.namespace.Namespace)2 DynamicDistributedLogConfiguration (org.apache.distributedlog.config.DynamicDistributedLogConfiguration)2 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)1 Stopwatch (com.google.common.base.Stopwatch)1 Closeable (java.io.Closeable)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 List (java.util.List)1 CompletionStage (java.util.concurrent.CompletionStage)1