Search in sources :

Example 1 with WaitKeyCancelledException

use of com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException in project hazelcast by hazelcast.

the class AbstractFencedLockFailureTest method testNewLockCancelsPendingLockRequest.

@Test(timeout = 300_000)
public void testNewLockCancelsPendingLockRequest() {
    lockByOtherThread();
    // there is a session id now
    RaftGroupId groupId = lock.getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    RaftInvocationManager invocationManager = getRaftInvocationManager();
    UUID invUid1 = newUnsecureUUID();
    UUID invUid2 = newUnsecureUUID();
    InternalCompletableFuture<Object> f = invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid1, MINUTES.toMillis(5)));
    assertTrueEventually(() -> {
        LockService service = getNodeEngineImpl(primaryInstance).getService(LockService.SERVICE_NAME);
        LockRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertEquals(1, registry.getWaitTimeouts().size());
    });
    invocationManager.invoke(groupId, new LockOp(objectName, sessionId, getThreadId(), invUid2));
    try {
        f.joinInternal();
        fail();
    } catch (WaitKeyCancelledException ignored) {
    }
}
Also used : TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) LockOp(com.hazelcast.cp.internal.datastructures.lock.operation.LockOp) TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 2 with WaitKeyCancelledException

use of com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException in project hazelcast by hazelcast.

the class AbstractFencedLockFailureTest method testNewUnlockCancelsPendingLockRequest.

@Test(timeout = 300_000)
public void testNewUnlockCancelsPendingLockRequest() {
    lockByOtherThread();
    // there is a session id now
    RaftGroupId groupId = lock.getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    RaftInvocationManager invocationManager = getRaftInvocationManager();
    UUID invUid = newUnsecureUUID();
    InternalCompletableFuture<Object> f = invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, MINUTES.toMillis(5)));
    assertTrueEventually(() -> {
        LockService service = getNodeEngineImpl(primaryInstance).getService(LockService.SERVICE_NAME);
        LockRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertEquals(1, registry.getWaitTimeouts().size());
    });
    try {
        lock.unlock();
        fail();
    } catch (IllegalMonitorStateException ignored) {
    }
    try {
        f.joinInternal();
        fail();
    } catch (WaitKeyCancelledException ignored) {
    }
}
Also used : TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 3 with WaitKeyCancelledException

use of com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException in project hazelcast by hazelcast.

the class AbstractFencedLockProxy method lockInterruptibly.

@Override
public void lockInterruptibly() throws InterruptedException {
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    for (; ; ) {
        long sessionId = acquireSession();
        verifyLockedSessionIdIfPresent(threadId, sessionId, true);
        try {
            long fence = doLock(sessionId, threadId, invocationUid).get();
            if (fence != INVALID_FENCE) {
                lockedSessionIds.put(threadId, sessionId);
                return;
            }
            throw new LockAcquireLimitReachedException("Lock[" + objectName + "] reentrant lock limit is already reached!");
        } catch (SessionExpiredException e) {
            invalidateSession(sessionId);
            verifyNoLockedSessionIdPresent(threadId);
        } catch (WaitKeyCancelledException e) {
            releaseSession(sessionId);
            throw new IllegalMonitorStateException("Lock[" + objectName + "] not acquired because the lock call " + "on the CP group is cancelled, possibly because of another indeterminate call from the same thread.");
        } catch (Throwable t) {
            releaseSession(sessionId);
            if (t instanceof InterruptedException) {
                throw (InterruptedException) t;
            } else {
                throw rethrow(t);
            }
        }
    }
}
Also used : WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) LockAcquireLimitReachedException(com.hazelcast.cp.lock.exception.LockAcquireLimitReachedException)

Example 4 with WaitKeyCancelledException

use of com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException in project hazelcast by hazelcast.

the class SessionlessSemaphoreProxy method tryAcquire.

@Override
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
    checkPositive("permits", permits);
    long clusterWideThreadId = getOrCreateUniqueThreadId();
    long timeoutMs = max(0, unit.toMillis(timeout));
    RaftOp op = new AcquirePermitsOp(objectName, NO_SESSION_ID, clusterWideThreadId, newUnsecureUUID(), permits, timeoutMs);
    try {
        return invocationManager.<Boolean>invoke(groupId, op).joinInternal();
    } catch (WaitKeyCancelledException e) {
        throw new IllegalStateException("Semaphore[" + objectName + "] not acquired because the acquire call " + "on the CP group is cancelled, possibly because of another indeterminate call from the same thread.");
    }
}
Also used : WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftOp(com.hazelcast.cp.internal.RaftOp)

Example 5 with WaitKeyCancelledException

use of com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException in project hazelcast by hazelcast.

the class SessionAwareSemaphoreProxy method tryAcquire.

@Override
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
    checkPositive("permits", permits);
    long timeoutMs = max(0, unit.toMillis(timeout));
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    long start;
    for (; ; ) {
        start = Clock.currentTimeMillis();
        long sessionId = acquireSession(permits);
        RaftOp op = new AcquirePermitsOp(objectName, sessionId, threadId, invocationUid, permits, timeoutMs);
        try {
            InternalCompletableFuture<Boolean> f = invocationManager.invoke(groupId, op);
            boolean acquired = f.joinInternal();
            if (!acquired) {
                releaseSession(sessionId, permits);
            }
            return acquired;
        } catch (SessionExpiredException e) {
            invalidateSession(sessionId);
            timeoutMs -= (Clock.currentTimeMillis() - start);
            if (timeoutMs <= 0) {
                return false;
            }
        } catch (WaitKeyCancelledException e) {
            releaseSession(sessionId, permits);
            return false;
        } catch (RuntimeException e) {
            releaseSession(sessionId, permits);
            throw e;
        }
    }
}
Also used : WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftOp(com.hazelcast.cp.internal.RaftOp) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID)

Aggregations

WaitKeyCancelledException (com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException)22 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)20 UUID (java.util.UUID)20 AcquirePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp)13 RaftGroupId (com.hazelcast.cp.internal.RaftGroupId)12 RaftInvocationManager (com.hazelcast.cp.internal.RaftInvocationManager)12 Test (org.junit.Test)12 SessionExpiredException (com.hazelcast.cp.internal.session.SessionExpiredException)7 RaftOp (com.hazelcast.cp.internal.RaftOp)4 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)3 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)3 TryLockOp (com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp)3 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)2 LockAcquireLimitReachedException (com.hazelcast.cp.lock.exception.LockAcquireLimitReachedException)2 LockOp (com.hazelcast.cp.internal.datastructures.lock.operation.LockOp)1