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))));
}
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();
}
Aggregations