use of com.twitter.distributedlog.exceptions.LockingException in project distributedlog by twitter.
the class TestZKSessionLock method testLockOnNonExistedLock.
/**
* Test lock on non existed lock.
*
* - lock should fail on a non existed lock.
*
* @throws Exception
*/
@Test(timeout = 60000)
public void testLockOnNonExistedLock() throws Exception {
String lockPath = "/test-lock-on-non-existed-lock";
String clientId = "test-lock-on-non-existed-lock";
ZKSessionLock lock = new ZKSessionLock(zkc, lockPath, clientId, lockStateExecutor);
// lock
try {
lock.tryLock(0, TimeUnit.MILLISECONDS);
fail("Should fail on locking a non-existed lock.");
} catch (LockingException le) {
Throwable cause = le.getCause();
assertTrue(cause instanceof KeeperException);
assertEquals(KeeperException.Code.NONODE, ((KeeperException) cause).code());
}
assertEquals(State.CLOSED, lock.getLockState());
// lock should failed on a failure lock
try {
lock.tryLock(0, TimeUnit.MILLISECONDS);
fail("Should fail on locking a failure lock.");
} catch (LockStateChangedException lsce) {
// expected
}
assertEquals(State.CLOSED, lock.getLockState());
}
use of com.twitter.distributedlog.exceptions.LockingException in project distributedlog by twitter.
the class ZKDistributedLock method reacquireLock.
private Future<ZKDistributedLock> reacquireLock(boolean throwLockAcquireException) throws LockingException {
final Stopwatch stopwatch = Stopwatch.createStarted();
Promise<ZKDistributedLock> lockPromise;
synchronized (this) {
if (closed) {
throw newLockClosedException();
}
if (null != lockReacquireException) {
if (throwLockAcquireException) {
throw lockReacquireException;
} else {
return null;
}
}
if (null != lockReacquireFuture) {
return lockReacquireFuture;
}
LOG.info("reacquiring lock at {}", lockPath);
lockReacquireFuture = lockPromise = new Promise<ZKDistributedLock>();
lockReacquireFuture.addEventListener(new FutureEventListener<ZKDistributedLock>() {
@Override
public void onSuccess(ZKDistributedLock lock) {
// if re-acquire successfully, clear the state.
synchronized (ZKDistributedLock.this) {
lockReacquireFuture = null;
}
reacquireStats.registerSuccessfulEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
}
@Override
public void onFailure(Throwable cause) {
synchronized (ZKDistributedLock.this) {
if (cause instanceof LockingException) {
lockReacquireException = (LockingException) cause;
} else {
lockReacquireException = new LockingException(lockPath, "Exception on re-acquiring lock", cause);
}
}
reacquireStats.registerFailedEvent(stopwatch.elapsed(TimeUnit.MICROSECONDS));
}
});
}
reacquireCount.incrementAndGet();
internalReacquireLock(new AtomicInteger(Integer.MAX_VALUE), 0, lockPromise);
return lockPromise;
}
Aggregations