Search in sources :

Example 11 with OwnershipAcquireFailedException

use of com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException 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)

Example 12 with OwnershipAcquireFailedException

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

the class TestZKSessionLock method testLockWhenSomeoneHeldLock.

/**
     * Test lock if the lock is already held by someone else. Any lock in this situation will
     * fail with current owner.
     *
     * @param timeout
     *          timeout to wait for the lock
     * @throws Exception
     */
private void testLockWhenSomeoneHeldLock(long timeout) throws Exception {
    String lockPath = "/test-lock-nowait-" + timeout + "-" + System.currentTimeMillis();
    String clientId0 = "test-lock-nowait-0-" + System.currentTimeMillis();
    String clientId1 = "test-lock-nowait-1-" + System.currentTimeMillis();
    String clientId2 = "test-lock-nowait-2-" + System.currentTimeMillis();
    createLockPath(zkc.get(), lockPath);
    ZKSessionLock lock0 = new ZKSessionLock(zkc0, lockPath, clientId0, lockStateExecutor);
    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))));
    try {
        lock1.tryLock(timeout, TimeUnit.MILLISECONDS);
        fail("lock1 should fail on locking since lock0 is holding the lock.");
    } catch (OwnershipAcquireFailedException oafe) {
        assertEquals(lock0.getLockId().getLeft(), oafe.getCurrentOwner());
    }
    // verification after lock1 tryLock
    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))));
    lock0.unlock();
    // verification after unlock lock0
    assertEquals(State.CLOSED, lock0.getLockState());
    assertEquals(0, getLockWaiters(zkc, lockPath).size());
    ZKSessionLock lock2 = new ZKSessionLock(zkc, lockPath, clientId2, lockStateExecutor);
    lock2.tryLock(timeout, TimeUnit.MILLISECONDS);
    // verification after lock2 lock
    assertEquals(State.CLOSED, lock0.getLockState());
    assertEquals(State.CLOSED, lock1.getLockState());
    assertEquals(State.CLAIMED, lock2.getLockState());
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertEquals(lock2.getLockId(), Await.result(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    lock2.unlock();
}
Also used : OwnershipAcquireFailedException(com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException) ZKSessionLock(com.twitter.distributedlog.lock.ZKSessionLock)

Aggregations

OwnershipAcquireFailedException (com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException)12 LockingException (com.twitter.distributedlog.exceptions.LockingException)4 ZKSessionLock (com.twitter.distributedlog.lock.ZKSessionLock)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)4 Stopwatch (com.google.common.base.Stopwatch)2 AsyncLogWriter (com.twitter.distributedlog.AsyncLogWriter)2 AlreadyClosedException (com.twitter.distributedlog.exceptions.AlreadyClosedException)2 DLException (com.twitter.distributedlog.exceptions.DLException)2 FutureEventListener (com.twitter.util.FutureEventListener)2 Promise (com.twitter.util.Promise)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 DLInterruptedException (com.twitter.distributedlog.exceptions.DLInterruptedException)1 InvalidStreamNameException (com.twitter.distributedlog.exceptions.InvalidStreamNameException)1 StreamUnavailableException (com.twitter.distributedlog.exceptions.StreamUnavailableException)1 UnexpectedException (com.twitter.distributedlog.exceptions.UnexpectedException)1 ZKException (com.twitter.distributedlog.exceptions.ZKException)1 StreamImpl (com.twitter.distributedlog.service.stream.StreamImpl)1 WriteOp (com.twitter.distributedlog.service.stream.WriteOp)1