use of org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException in project kylin by apache.
the class ITZookeeperDistributedLockTest method testErrorCases.
@Test
public void testErrorCases() {
DistributedLock c = factory.lockForClient("client1");
DistributedLock d = factory.lockForClient("client2");
String path = ZK_PFX + "/testErrorCases";
assertTrue(c.isLocked(path) == false);
assertTrue(d.peekLock(path) == null);
assertTrue(c.lock(path));
assertTrue(d.lock(path) == false);
assertTrue(d.isLocked(path) == true);
assertEquals(c.getClient(), d.peekLock(path));
try {
d.unlock(path);
fail();
} catch (ZkReleaseLockException ex) {
// expected
}
c.unlock(path);
assertTrue(d.isLocked(path) == false);
d.lock(path);
d.unlock(path);
}
use of org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException in project kylin by apache.
the class ZookeeperDistributedLockTest method testTwoClientUnlockWhenCatchInterruptExceptionOnPeekLock.
@Test
public void testTwoClientUnlockWhenCatchInterruptExceptionOnPeekLock() {
String path = ZK_PFX + "/test_interrupt_lock";
DistributedLock lock1 = factory.lockForClient("client_1");
DistributedLock lock2 = factory.lockForClient("client_2");
assertFalse(lock1.isLocked(path));
assertFalse(lock2.isLocked(path));
// lock first by client_1
assertTrue(lock1.lock(path));
assertFalse(lock2.lock(path));
assertTrue(lock1.isLockedByMe(path));
assertFalse(lock2.isLockedByMe(path));
// mock lock for client_2 to simulate lock when an InterruptException caught
DistributedLock spy2 = Mockito.spy(lock2);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy2).peekLock(Mockito.anyString());
try {
spy2.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkReleaseLockException expected: lock was held by client_1
assertTrue(e instanceof ZkReleaseLockException);
}
// should not release lock because lock was held by client_1
Mockito.reset(spy2);
assertTrue(lock1.isLocked(path));
assertTrue(lock2.isLocked(path));
assertTrue(lock1.isLockedByMe(path));
assertFalse(lock2.isLockedByMe(path));
// mock lock for client_1 to simulate lock when an InterruptException caught
DistributedLock spy1 = Mockito.spy(lock1);
// mock interruptException when peekLock only once
Mockito.doThrow(new ZkPeekLockInterruptException("mock interrupt")).doCallRealMethod().when(spy1).peekLock(Mockito.anyString());
try {
spy1.unlock(path);
fail("should throw exception");
} catch (Exception e) {
// ZkPeekLockInterruptException expected
assertTrue(e instanceof ZkPeekLockInterruptException);
}
// should release lock because lock was held by client_1
Mockito.reset(spy1);
assertFalse(lock1.isLocked(path));
assertFalse(lock2.isLocked(path));
assertFalse(lock1.isLockedByMe(path));
assertFalse(lock2.isLockedByMe(path));
}
use of org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException in project kylin by apache.
the class ZookeeperDistributedLock method unlock.
@Override
public void unlock(String lockPath) {
logger.debug("{} trying to unlock {}", client, lockPath);
// peek owner first
String owner;
ZkPeekLockInterruptException peekLockInterruptException = null;
try {
owner = peekLock(lockPath);
} catch (ZkPeekLockInterruptException zie) {
// re-peek owner of lock when interrupted
owner = peekLock(lockPath);
peekLockInterruptException = zie;
} catch (ZkPeekLockException ze) {
// this exception should be thrown to diagnose even it may cause unlock failed
logger.error("{} failed to peekLock when unlock at {}", client, lockPath, ze);
throw ze;
}
// then unlock
ZkReleaseLockInterruptException unlockInterruptException = null;
try {
unlockInternal(owner, lockPath);
} catch (ZkReleaseLockInterruptException zlie) {
// re-unlock once when interrupted
unlockInternal(owner, lockPath);
unlockInterruptException = zlie;
} catch (Exception ex) {
throw new ZkReleaseLockException("Error while " + client + " trying to unlock " + lockPath, ex);
}
// need re-throw interrupt exception to avoid swallowing it
if (peekLockInterruptException != null) {
throw peekLockInterruptException;
}
if (unlockInterruptException != null) {
throw unlockInterruptException;
}
}
Aggregations