Search in sources :

Example 6 with LockingException

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

the class TestZKSessionLock method testWaitForLockReleased.

/**
     * Test lock wait for the lock owner to release the lock. The lock waiter should acquire lock successfully
     * if the lock owner unlock or it is expired.
     *
     * @param lockPath
     *          lock path
     * @param isUnlock
     *          whether to unlock or expire the lock
     * @throws Exception
     */
private void testWaitForLockReleased(String lockPath, boolean isUnlock) throws Exception {
    String clientId0 = "test-wait-for-lock-released-0-" + System.currentTimeMillis();
    String clientId1 = "test-wait-for-lock-released-1-" + System.currentTimeMillis();
    createLockPath(zkc.get(), lockPath);
    final ZKSessionLock lock0 = new ZKSessionLock(zkc0, lockPath, clientId0, lockStateExecutor);
    final ZKSessionLock lock1 = new ZKSessionLock(zkc, lockPath, clientId1, lockStateExecutor);
    lock0.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    // verification after lock0 lock
    assertEquals(State.CLAIMED, lock0.getLockState());
    List<String> children = getLockWaiters(zkc0, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock0.getLockId(), Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    final CountDownLatch lock1DoneLatch = new CountDownLatch(1);
    Thread lock1Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                lock1.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
                lock1DoneLatch.countDown();
            } catch (LockingException e) {
                logger.error("Failed on locking lock1 : ", e);
            }
        }
    }, "lock1-thread");
    lock1Thread.start();
    // ensure lock1 is waiting for lock0
    children = awaitWaiters(2, zkc, lockPath);
    if (isUnlock) {
        lock0.unlock();
    } else {
        ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);
    }
    lock1DoneLatch.await();
    lock1Thread.join();
    // verification after lock2 lock
    if (isUnlock) {
        assertEquals(State.CLOSED, lock0.getLockState());
    } else {
        assertEquals(State.EXPIRED, lock0.getLockState());
    }
    assertEquals(State.CLAIMED, lock1.getLockState());
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock1.getLockId(), Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    lock1.unlock();
}
Also used : LockingException(com.twitter.distributedlog.exceptions.LockingException) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 7 with LockingException

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

the class TestZKSessionLock method testLockUseSameClientIdButDifferentSessions.

private void testLockUseSameClientIdButDifferentSessions(boolean isUnlock) throws Exception {
    String lockPath = "/test-lock-use-same-client-id-but-different-sessions-" + isUnlock + System.currentTimeMillis();
    String clientId = "test-lock-use-same-client-id-but-different-sessions";
    createLockPath(zkc.get(), lockPath);
    final ZKSessionLock lock0 = new ZKSessionLock(zkc0, lockPath, clientId, lockStateExecutor);
    lock0.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    // lock1_0 couldn't claim ownership since owner is in a different zk session.
    final ZKSessionLock lock1_0 = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor);
    try {
        lock1_0.tryLock(0, TimeUnit.MILLISECONDS);
        fail("Should fail locking since the lock is held in a different zk session.");
    } catch (OwnershipAcquireFailedException oafe) {
        assertEquals(clientId, oafe.getCurrentOwner());
    }
    assertEquals(State.CLOSED, lock1_0.getLockState());
    List<String> children = getLockWaiters(zkc0, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock0.getLockId(), Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    // lock1_1 would wait the ownership
    final ZKSessionLock lock1_1 = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor);
    final CountDownLatch lock1DoneLatch = new CountDownLatch(1);
    Thread lock1Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                lock1_1.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
                lock1DoneLatch.countDown();
            } catch (LockingException e) {
                logger.error("Failed on locking lock1 : ", e);
            }
        }
    }, "lock1-thread");
    lock1Thread.start();
    // check lock1 is waiting for lock0
    children = awaitWaiters(2, zkc, lockPath);
    logger.info("Found {} lock waiters : {}", children.size(), children);
    assertEquals(2, children.size());
    assertEquals(State.CLAIMED, lock0.getLockState());
    assertEquals(lock0.getLockId(), Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    awaitState(State.WAITING, lock1_1);
    assertEquals(lock1_1.getLockId(), Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(1))));
    if (isUnlock) {
        lock0.unlock();
    } else {
        ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);
    }
    lock1DoneLatch.await();
    lock1Thread.join();
    // verification
    if (isUnlock) {
        assertEquals(State.CLOSED, lock0.getLockState());
    } else {
        assertEquals(State.EXPIRED, lock0.getLockState());
    }
    assertEquals(State.CLAIMED, lock1_1.getLockState());
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock1_1.getLockId(), Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    lock1_1.unlock();
}
Also used : OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) LockingException(com.twitter.distributedlog.exceptions.LockingException) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with LockingException

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

the class TestZKSessionLock method testSessionExpiredBeforeLock.

/**
     * Test Session Expired Before Lock does locking. The lock should be closed since
     * all zookeeper operations would be failed.
     *
     * @param timeout
     *          timeout to wait for the lock
     * @throws Exception
     */
private void testSessionExpiredBeforeLock(long timeout) throws Exception {
    String lockPath = "/test-session-expired-before-lock-" + timeout + "-" + System.currentTimeMillis();
    String clientId = "test-session-expired-before-lock-" + System.currentTimeMillis();
    createLockPath(zkc.get(), lockPath);
    final AtomicInteger expireCounter = new AtomicInteger(0);
    final CountDownLatch expiredLatch = new CountDownLatch(1);
    LockListener listener = new LockListener() {

        @Override
        public void onExpired() {
            expireCounter.incrementAndGet();
        }
    };
    final ZKSessionLock lock = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor).setLockListener(listener);
    // expire session
    ZooKeeperClientUtils.expireSession(zkc, zkServers, sessionTimeoutMs);
    // submit a runnable to lock state executor to ensure any state changes happened when session expired
    lockStateExecutor.submit(lockPath, new SafeRunnable() {

        @Override
        public void safeRun() {
            expiredLatch.countDown();
        }
    });
    expiredLatch.await();
    // no watcher was registered if never acquired lock successfully
    assertEquals(State.INIT, lock.getLockState());
    try {
        lock.tryLock(timeout, TimeUnit.MILLISECONDS);
        fail("Should fail locking using an expired lock");
    } catch (LockingException le) {
        assertTrue(le.getCause() instanceof KeeperException.SessionExpiredException);
    }
    assertEquals(State.CLOSED, lock.getLockState());
    List<String> children = getLockWaiters(zkc, lockPath);
    assertEquals(0, children.size());
}
Also used : LockingException(com.twitter.distributedlog.exceptions.LockingException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 9 with LockingException

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

the class TestZKSessionLock method testTryAcquireTimeout.

/**
     * Test try acquire timeout.
     *
     * @throws Exception
     */
@Test(timeout = 60000)
public void testTryAcquireTimeout() throws Exception {
    String name = testNames.getMethodName();
    String lockPath = "/" + name;
    String clientId = name;
    createLockPath(zkc.get(), lockPath);
    ZKSessionLock lock = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor, 1, /* op timeout */
    NullStatsLogger.INSTANCE, new DistributedLockContext());
    try {
        FailpointUtils.setFailpoint(FailpointUtils.FailPointName.FP_LockTryAcquire, new DelayFailpointAction(60 * 60 * 1000));
        lock.tryLock(0, TimeUnit.MILLISECONDS);
        assertEquals(State.CLOSED, lock.getLockState());
    } catch (LockingException le) {
    } catch (Exception e) {
        fail("expected locking exception");
    } finally {
        FailpointUtils.removeFailpoint(FailpointUtils.FailPointName.FP_LockTryAcquire);
    }
}
Also used : LockingException(com.twitter.distributedlog.exceptions.LockingException) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock) LockingException(com.twitter.distributedlog.exceptions.LockingException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) Test(org.junit.Test)

Example 10 with LockingException

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

the class TestZKSessionLock method testSessionExpiredForLockWaiter.

@Test(timeout = 60000)
public void testSessionExpiredForLockWaiter() throws Exception {
    String lockPath = "/test-session-expired-for-lock-waiter";
    String clientId0 = "test-session-expired-for-lock-waiter-0";
    String clientId1 = "test-session-expired-for-lock-waiter-1";
    createLockPath(zkc.get(), lockPath);
    final ZKSessionLock lock0 = new ZKSessionLock(zkc0, lockPath, clientId0, lockStateExecutor);
    lock0.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    assertEquals(State.CLAIMED, lock0.getLockState());
    List<String> children = getLockWaiters(zkc0, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock0.getLockId(), Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    final ZKSessionLock lock1 = new ZKSessionLock(zkc, lockPath, clientId1, lockStateExecutor);
    final CountDownLatch lock1DoneLatch = new CountDownLatch(1);
    Thread lock1Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                lock1.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
            } catch (OwnershipAcquireFailedException oafe) {
                lock1DoneLatch.countDown();
            } catch (LockingException e) {
                logger.error("Failed on locking lock1 : ", e);
            }
        }
    }, "lock1-thread");
    lock1Thread.start();
    // check lock1 is waiting for lock0
    children = awaitWaiters(2, zkc, lockPath);
    assertEquals(2, children.size());
    assertEquals(State.CLAIMED, lock0.getLockState());
    assertEquals(lock0.getLockId(), Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    awaitState(State.WAITING, lock1);
    assertEquals(lock1.getLockId(), Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(1))));
    // expire lock1
    ZooKeeperClientUtils.expireSession(zkc, zkServers, sessionTimeoutMs);
    lock1DoneLatch.countDown();
    lock1Thread.join();
    assertEquals(State.CLAIMED, lock0.getLockState());
    assertEquals(State.CLOSED, lock1.getLockState());
    children = getLockWaiters(zkc0, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock0.getLockId(), Await.result(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
}
Also used : OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) LockingException(com.twitter.distributedlog.exceptions.LockingException) SafeRunnable(org.apache.bookkeeper.util.SafeRunnable) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

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