Search in sources :

Example 1 with OwnershipAcquireFailedException

use of org.apache.distributedlog.exceptions.OwnershipAcquireFailedException 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 2 with OwnershipAcquireFailedException

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

the class TestDistributedLock method testLockReacquireFailure.

private void testLockReacquireFailure(boolean checkOwnershipAndReacquire) throws Exception {
    String lockPath = "/test-lock-re-acquire-failure-" + checkOwnershipAndReacquire + "-" + System.currentTimeMillis();
    String clientId = "test-lock-re-acquire";
    createLockPath(zkc.get(), lockPath);
    SessionLockFactory lockFactory0 = createLockFactory(clientId, zkc0);
    ZKDistributedLock lock0 = new ZKDistributedLock(lockStateExecutor, lockFactory0, lockPath, Long.MAX_VALUE, NullStatsLogger.INSTANCE);
    Utils.ioResult(lock0.asyncAcquire());
    final CountDownLatch lock1DoneLatch = new CountDownLatch(1);
    SessionLockFactory lockFactory1 = createLockFactory(clientId, zkc);
    final ZKDistributedLock lock1 = new ZKDistributedLock(lockStateExecutor, lockFactory1, lockPath, Long.MAX_VALUE, NullStatsLogger.INSTANCE);
    Thread lock1Thread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                Utils.ioResult(lock1.asyncAcquire());
                lock1DoneLatch.countDown();
            } catch (Exception e) {
                logger.error("Error on acquiring lock1 : ", e);
            }
        }
    }, "lock1-thread");
    lock1Thread.start();
    List<String> children;
    do {
        Thread.sleep(1);
        children = getLockWaiters(zkc, lockPath);
    } while (children.size() < 2);
    assertEquals(2, children.size());
    assertTrue(lock0.haveLock());
    assertFalse(lock1.haveLock());
    assertEquals(((ZKSessionLock) lock0.getInternalLock()).getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    assertEquals(((ZKSessionLock) lock1.getInternalLock()).getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(1))));
    logger.info("Expiring session on lock0");
    ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);
    logger.info("Session on lock0 is expired");
    lock1DoneLatch.await();
    assertFalse(lock0.haveLock());
    assertTrue(lock1.haveLock());
    if (checkOwnershipAndReacquire) {
        try {
            checkLockAndReacquire(lock0, true);
            fail("Should fail check write lock since lock is already held by other people");
        } catch (OwnershipAcquireFailedException oafe) {
            assertEquals(((ZKSessionLock) lock1.getInternalLock()).getLockId().getLeft(), oafe.getCurrentOwner());
        }
        try {
            checkLockAndReacquire(lock0, false);
            fail("Should fail check write lock since lock is already held by other people");
        } catch (OwnershipAcquireFailedException oafe) {
            assertEquals(((ZKSessionLock) lock1.getInternalLock()).getLockId().getLeft(), oafe.getCurrentOwner());
        }
    } else {
        logger.info("Waiting lock0 to attempt acquisition after session expired");
        // session expire will trigger lock re-acquisition
        CompletableFuture<ZKDistributedLock> asyncLockAcquireFuture;
        do {
            Thread.sleep(1);
            asyncLockAcquireFuture = lock0.getLockReacquireFuture();
        } while (null == asyncLockAcquireFuture);
        try {
            Utils.ioResult(asyncLockAcquireFuture);
            fail("Should fail check write lock since lock is already held by other people");
        } catch (OwnershipAcquireFailedException oafe) {
            assertEquals(((ZKSessionLock) lock1.getInternalLock()).getLockId().getLeft(), oafe.getCurrentOwner());
        }
        try {
            checkLockAndReacquire(lock0, false);
            fail("Should fail check write lock since lock is already held by other people");
        } catch (OwnershipAcquireFailedException oafe) {
            assertEquals(((ZKSessionLock) lock1.getInternalLock()).getLockId().getLeft(), oafe.getCurrentOwner());
        }
    }
    children = getLockWaiters(zkc, lockPath);
    assertEquals(1, children.size());
    assertFalse(lock0.haveLock());
    assertTrue(lock1.haveLock());
    assertEquals(((ZKSessionLock) lock1.getInternalLock()).getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    Utils.ioResult(lock0.asyncClose());
    Utils.ioResult(lock1.asyncClose());
    children = getLockWaiters(zkc, lockPath);
    assertEquals(0, children.size());
}
Also used : OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) CountDownLatch(java.util.concurrent.CountDownLatch) LockingException(org.apache.distributedlog.exceptions.LockingException) UnexpectedException(org.apache.distributedlog.exceptions.UnexpectedException) IOException(java.io.IOException) OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException)

Example 3 with OwnershipAcquireFailedException

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

the class TestZKSessionLock method testLockWhenSiblingUseDifferentLockId.

private void testLockWhenSiblingUseDifferentLockId(long timeout, final boolean isUnlock) throws Exception {
    String lockPath = "/test-lock-when-sibling-use-different-lock-id-" + timeout + "-" + isUnlock + "-" + System.currentTimeMillis();
    String clientId0 = "client-id-0";
    String clientId1 = "client-id-1";
    createLockPath(zkc.get(), lockPath);
    final ZKSessionLock lock0_0 = new ZKSessionLock(zkc0, lockPath, clientId0, lockStateExecutor);
    final ZKSessionLock lock0_1 = new ZKSessionLock(zkc0, lockPath, clientId0, lockStateExecutor);
    final ZKSessionLock lock1 = new ZKSessionLock(zkc, lockPath, clientId1, lockStateExecutor);
    lock0_0.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    // lock1 wait for the lock ownership.
    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();
    // check lock1 is waiting for lock0_0
    List<String> children = awaitWaiters(2, zkc, lockPath);
    assertEquals(2, children.size());
    assertEquals(State.CLAIMED, lock0_0.getLockState());
    assertEquals(lock0_0.getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    awaitState(State.WAITING, lock1);
    assertEquals(lock1.getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(1))));
    final CountDownLatch lock0DoneLatch = new CountDownLatch(1);
    final AtomicReference<String> ownerFromLock0 = new AtomicReference<String>(null);
    Thread lock0Thread = null;
    if (timeout == 0) {
        try {
            lock0_1.tryLock(0, TimeUnit.MILLISECONDS);
            fail("Should fail on locking if sibling is using differnt lock id.");
        } catch (OwnershipAcquireFailedException oafe) {
            assertEquals(clientId0, oafe.getCurrentOwner());
        }
        assertEquals(State.CLOSED, lock0_1.getLockState());
        children = getLockWaiters(zkc, lockPath);
        assertEquals(2, children.size());
        assertEquals(State.CLAIMED, lock0_0.getLockState());
        assertEquals(lock0_0.getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
        assertEquals(State.WAITING, lock1.getLockState());
        assertEquals(lock1.getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(1))));
    } else {
        lock0Thread = new Thread(new Runnable() {

            @Override
            public void run() {
                try {
                    lock0_1.tryLock(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
                    if (isUnlock) {
                        lock0DoneLatch.countDown();
                    }
                } catch (OwnershipAcquireFailedException oafe) {
                    if (!isUnlock) {
                        ownerFromLock0.set(oafe.getCurrentOwner());
                        lock0DoneLatch.countDown();
                    }
                } catch (LockingException le) {
                    logger.error("Failed on locking lock0_1 : ", le);
                }
            }
        }, "lock0-thread");
        lock0Thread.start();
        // check lock1 is waiting for lock0_0
        children = awaitWaiters(3, zkc, lockPath);
        assertEquals(3, children.size());
        assertEquals(State.CLAIMED, lock0_0.getLockState());
        assertEquals(lock0_0.getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
        awaitState(State.WAITING, lock1);
        assertEquals(lock1.getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(1))));
        awaitState(State.WAITING, lock0_1);
        assertEquals(lock0_1.getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(2))));
    }
    if (isUnlock) {
        lock0_0.unlock();
    } else {
        ZooKeeperClientUtils.expireSession(zkc0, zkServers, sessionTimeoutMs);
    }
    lock1DoneLatch.await();
    lock1Thread.join();
    // check the state of lock0_0
    if (isUnlock) {
        assertEquals(State.CLOSED, lock0_0.getLockState());
    } else {
        assertEquals(State.EXPIRED, lock0_0.getLockState());
    }
    if (timeout == 0) {
        children = getLockWaiters(zkc, lockPath);
        assertEquals(1, children.size());
        assertEquals(State.CLAIMED, lock1.getLockState());
        assertEquals(lock1.getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    } else {
        assertNotNull(lock0Thread);
        if (!isUnlock) {
            // both lock0_0 and lock0_1 would be expired
            lock0DoneLatch.await();
            lock0Thread.join();
            assertEquals(clientId0, ownerFromLock0.get());
            assertEquals(State.CLOSED, lock0_1.getLockState());
            children = getLockWaiters(zkc, lockPath);
            assertEquals(1, children.size());
            assertEquals(State.CLAIMED, lock1.getLockState());
            assertEquals(lock1.getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
        } else {
            children = getLockWaiters(zkc, lockPath);
            assertEquals(2, children.size());
            assertEquals(State.CLAIMED, lock1.getLockState());
            assertEquals(lock1.getLockId(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
            assertEquals(State.WAITING, lock0_1.getLockState());
            assertEquals(lock0_1.getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(1))));
        }
    }
    lock1.unlock();
    if (timeout != 0 && isUnlock) {
        lock0DoneLatch.await();
        lock0Thread.join();
        children = getLockWaiters(zkc, lockPath);
        assertEquals(1, children.size());
        assertEquals(State.CLAIMED, lock0_1.getLockState());
        assertEquals(lock0_1.getLockId(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    }
}
Also used : LockingException(org.apache.distributedlog.exceptions.LockingException) OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 4 with OwnershipAcquireFailedException

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

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(), Utils.ioResult(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(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    awaitState(State.WAITING, lock1);
    assertEquals(lock1.getLockId(), Utils.ioResult(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(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
}
Also used : OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) LockingException(org.apache.distributedlog.exceptions.LockingException) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with OwnershipAcquireFailedException

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

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(), Utils.ioResult(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(), Utils.ioResult(asyncParseClientID(zkc0.get(), lockPath, children.get(0))));
    awaitState(State.WAITING, lock1_1);
    assertEquals(lock1_1.getLockId(), Utils.ioResult(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(), Utils.ioResult(asyncParseClientID(zkc.get(), lockPath, children.get(0))));
    lock1_1.unlock();
}
Also used : OwnershipAcquireFailedException(org.apache.distributedlog.exceptions.OwnershipAcquireFailedException) LockingException(org.apache.distributedlog.exceptions.LockingException) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

OwnershipAcquireFailedException (org.apache.distributedlog.exceptions.OwnershipAcquireFailedException)7 LockingException (org.apache.distributedlog.exceptions.LockingException)5 CountDownLatch (java.util.concurrent.CountDownLatch)4 IOException (java.io.IOException)2 UnexpectedException (org.apache.distributedlog.exceptions.UnexpectedException)2 Test (org.junit.Test)2 Stopwatch (com.google.common.base.Stopwatch)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 CancellationException (java.util.concurrent.CancellationException)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 FutureEventListener (org.apache.bookkeeper.common.concurrent.FutureEventListener)1 SafeRunnable (org.apache.bookkeeper.util.SafeRunnable)1 Pair (org.apache.commons.lang3.tuple.Pair)1 AsyncLogReader (org.apache.distributedlog.api.AsyncLogReader)1 DistributedLogManager (org.apache.distributedlog.api.DistributedLogManager)1 DLInterruptedException (org.apache.distributedlog.exceptions.DLInterruptedException)1 LockCancelledException (org.apache.distributedlog.exceptions.LockCancelledException)1 ZKException (org.apache.distributedlog.exceptions.ZKException)1