Search in sources :

Example 1 with ZkReleaseLockException

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);
}
Also used : ZookeeperDistributedLock(org.apache.kylin.job.lock.zookeeper.ZookeeperDistributedLock) DistributedLock(org.apache.kylin.common.lock.DistributedLock) ZkReleaseLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException) Test(org.junit.Test)

Example 2 with ZkReleaseLockException

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));
}
Also used : DistributedLock(org.apache.kylin.common.lock.DistributedLock) ZkPeekLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockInterruptException) ZkReleaseLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException) ZkReleaseLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockInterruptException) ZkReleaseLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException) ZkPeekLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockInterruptException) Test(org.junit.Test)

Example 3 with ZkReleaseLockException

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;
    }
}
Also used : ZkPeekLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockInterruptException) ZkReleaseLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockInterruptException) ZkReleaseLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException) ZkReleaseLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockInterruptException) ZkReleaseLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException) KeeperException(org.apache.zookeeper.KeeperException) ZkPeekLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockException) ZkAcquireLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkAcquireLockException) ZkPeekLockInterruptException(org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockInterruptException) ZkPeekLockException(org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockException)

Aggregations

ZkReleaseLockException (org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockException)3 DistributedLock (org.apache.kylin.common.lock.DistributedLock)2 ZkPeekLockInterruptException (org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockInterruptException)2 ZkReleaseLockInterruptException (org.apache.kylin.job.lock.zookeeper.exception.ZkReleaseLockInterruptException)2 Test (org.junit.Test)2 ZookeeperDistributedLock (org.apache.kylin.job.lock.zookeeper.ZookeeperDistributedLock)1 ZkAcquireLockException (org.apache.kylin.job.lock.zookeeper.exception.ZkAcquireLockException)1 ZkPeekLockException (org.apache.kylin.job.lock.zookeeper.exception.ZkPeekLockException)1 KeeperException (org.apache.zookeeper.KeeperException)1