use of com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException in project distributedlog by twitter.
the class TestDistributedLogService method testAcquireStreams.
@Test(timeout = 60000)
public void testAcquireStreams() throws Exception {
String streamName = testName.getMethodName();
StreamImpl s0 = createUnstartedStream(service, streamName);
s0.suspendAcquiring();
DistributedLogServiceImpl service1 = createService(serverConf, dlConf);
StreamImpl s1 = createUnstartedStream(service1, streamName);
s1.suspendAcquiring();
// create write ops
WriteOp op0 = createWriteOp(service, streamName, 0L);
s0.submit(op0);
WriteOp op1 = createWriteOp(service1, streamName, 1L);
s1.submit(op1);
// check pending size
assertEquals("Write Op 0 should be pending in service 0", 1, s0.numPendingOps());
assertEquals("Write Op 1 should be pending in service 1", 1, s1.numPendingOps());
// start acquiring s0
s0.resumeAcquiring().start();
WriteResponse wr0 = Await.result(op0.result());
assertEquals("Op 0 should succeed", StatusCode.SUCCESS, wr0.getHeader().getCode());
assertEquals("Service 0 should acquire stream", StreamStatus.INITIALIZED, s0.getStatus());
assertNotNull(s0.getManager());
assertNotNull(s0.getWriter());
assertNull(s0.getLastException());
// start acquiring s1
s1.resumeAcquiring().start();
WriteResponse wr1 = Await.result(op1.result());
assertEquals("Op 1 should fail", StatusCode.FOUND, wr1.getHeader().getCode());
assertEquals("Service 1 should be in BACKOFF state", StreamStatus.BACKOFF, s1.getStatus());
assertNotNull(s1.getManager());
assertNull(s1.getWriter());
assertNotNull(s1.getLastException());
assertTrue(s1.getLastException() instanceof OwnershipAcquireFailedException);
service1.shutdown();
}
use of com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException 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();
}
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();
}
use of com.twitter.distributedlog.exceptions.OwnershipAcquireFailedException in project distributedlog by twitter.
the class AbstractStreamOp method fail.
/**
* Fail with current <i>owner</i> and its reason <i>t</i>
*
* @param cause
* failure reason
*/
@Override
public void fail(Throwable cause) {
if (cause instanceof OwnershipAcquireFailedException) {
// Ownership exception is a control exception, not an error, so we don't stat
// it with the other errors.
OwnershipAcquireFailedException oafe = (OwnershipAcquireFailedException) cause;
fail(ResponseUtils.ownerToHeader(oafe.getCurrentOwner()));
} else {
opStatsLogger.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
fail(ResponseUtils.exceptionToHeader(cause));
}
}
Aggregations