Search in sources :

Example 11 with LockingException

use of com.twitter.distributedlog.exceptions.LockingException in project distributedlog by twitter.

the class TestZKSessionLock method testLockOnNonExistedLock.

/**
     * Test lock on non existed lock.
     *
     * - lock should fail on a non existed lock.
     *
     * @throws Exception
     */
@Test(timeout = 60000)
public void testLockOnNonExistedLock() throws Exception {
    String lockPath = "/test-lock-on-non-existed-lock";
    String clientId = "test-lock-on-non-existed-lock";
    ZKSessionLock lock = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor);
    // lock
    try {
        lock.tryLock(0, TimeUnit.MILLISECONDS);
        fail("Should fail on locking a non-existed lock.");
    } catch (LockingException le) {
        Throwable cause = le.getCause();
        assertTrue(cause instanceof KeeperException);
        assertEquals(KeeperException.Code.NONODE, ((KeeperException) cause).code());
    }
    assertEquals(State.CLOSED, lock.getLockState());
    // lock should failed on a failure lock
    try {
        lock.tryLock(0, TimeUnit.MILLISECONDS);
        fail("Should fail on locking a failure lock.");
    } catch (LockStateChangedException lsce) {
    // expected
    }
    assertEquals(State.CLOSED, lock.getLockState());
}
Also used : LockingException(com.twitter.distributedlog.exceptions.LockingException) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 12 with LockingException

use of com.twitter.distributedlog.exceptions.LockingException in project distributedlog by twitter.

the class ZKDistributedLock method reacquireLock.

private Future<ZKDistributedLock> reacquireLock(boolean throwLockAcquireException) throws LockingException {
    final Stopwatch stopwatch = Stopwatch.createStarted();
    Promise<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 Promise<ZKDistributedLock>();
        lockReacquireFuture.addEventListener(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));
            }

            @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));
            }
        });
    }
    reacquireCount.incrementAndGet();
    internalReacquireLock(new AtomicInteger(Integer.MAX_VALUE), 0, lockPromise);
    return lockPromise;
}
Also used : Promise(com.twitter.util.Promise) LockingException(com.twitter.distributedlog.exceptions.LockingException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Stopwatch(com.google.common.base.Stopwatch)

Aggregations

LockingException (com.twitter.distributedlog.exceptions.LockingException)12 ZKSessionLock (com.twitter.distributedlog.lock.ZKSessionLock)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)5 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)5 Test (org.junit.Test)5 IOException (java.io.IOException)3 KeeperException (org.apache.zookeeper.KeeperException)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Stopwatch (com.google.common.base.Stopwatch)1 DynamicDistributedLogConfiguration (com.twitter.distributedlog.config.DynamicDistributedLogConfiguration)1 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 ZKException (com.twitter.distributedlog.exceptions.ZKException)1 DistributedLock (com.twitter.distributedlog.lock.DistributedLock)1 DistributedLogNamespace (com.twitter.distributedlog.namespace.DistributedLogNamespace)1 OrderedFutureEventListener (com.twitter.distributedlog.util.FutureUtils.OrderedFutureEventListener)1 FutureEventListener (com.twitter.util.FutureEventListener)1 Promise (com.twitter.util.Promise)1 TimeoutException (com.twitter.util.TimeoutException)1