Search in sources :

Example 1 with SessionExpiredException

use of com.hazelcast.cp.internal.session.SessionExpiredException 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 2 with SessionExpiredException

use of com.hazelcast.cp.internal.session.SessionExpiredException in project hazelcast by hazelcast.

the class SessionAwareSemaphoreProxy method release.

@Override
public void release(int permits) {
    checkPositive(permits, "Permits must be positive!");
    long sessionId = sessionManager.getSession(groupId);
    if (sessionId == NO_SESSION_ID) {
        throw newIllegalStateException(null);
    }
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    try {
        ClientMessage request = SemaphoreReleaseCodec.encodeRequest(groupId, objectName, sessionId, threadId, invocationUid, permits);
        HazelcastClientInstanceImpl client = getClient();
        new ClientInvocation(client, request, objectName).invoke().joinInternal();
    } catch (SessionExpiredException e) {
        sessionManager.invalidateSession(this.groupId, sessionId);
        throw newIllegalStateException(e);
    } finally {
        sessionManager.releaseSession(this.groupId, sessionId, permits);
    }
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID)

Example 3 with SessionExpiredException

use of com.hazelcast.cp.internal.session.SessionExpiredException in project hazelcast by hazelcast.

the class SessionAwareSemaphoreProxy method doChangePermits.

private void doChangePermits(int delta) {
    long sessionId = sessionManager.acquireSession(groupId);
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    try {
        ClientMessage request = SemaphoreChangeCodec.encodeRequest(groupId, objectName, sessionId, threadId, invocationUid, delta);
        new ClientInvocation(getClient(), request, objectName).invoke().joinInternal();
    } catch (SessionExpiredException e) {
        sessionManager.invalidateSession(this.groupId, sessionId);
        throw newIllegalStateException(e);
    } finally {
        sessionManager.releaseSession(this.groupId, sessionId);
    }
}
Also used : ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID)

Example 4 with SessionExpiredException

use of com.hazelcast.cp.internal.session.SessionExpiredException in project hazelcast by hazelcast.

the class SessionAwareSemaphoreProxy method drainPermits.

@Override
public int drainPermits() {
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    for (; ; ) {
        long sessionId = sessionManager.acquireSession(this.groupId, DRAIN_SESSION_ACQ_COUNT);
        try {
            ClientMessage request = SemaphoreDrainCodec.encodeRequest(groupId, objectName, sessionId, threadId, invocationUid);
            HazelcastClientInstanceImpl client = getClient();
            ClientMessage response = new ClientInvocation(client, request, objectName).invoke().joinInternal();
            int count = SemaphoreDrainCodec.decodeResponse(response);
            sessionManager.releaseSession(groupId, sessionId, DRAIN_SESSION_ACQ_COUNT - count);
            return count;
        } catch (SessionExpiredException e) {
            sessionManager.invalidateSession(this.groupId, sessionId);
        } catch (RuntimeException e) {
            sessionManager.releaseSession(this.groupId, sessionId, DRAIN_SESSION_ACQ_COUNT);
            throw e;
        }
    }
}
Also used : HazelcastClientInstanceImpl(com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl) ClientInvocation(com.hazelcast.client.impl.spi.impl.ClientInvocation) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) ClientMessage(com.hazelcast.client.impl.protocol.ClientMessage) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID)

Example 5 with SessionExpiredException

use of com.hazelcast.cp.internal.session.SessionExpiredException 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

SessionExpiredException (com.hazelcast.cp.internal.session.SessionExpiredException)14 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)13 UUID (java.util.UUID)13 WaitKeyCancelledException (com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException)7 ClientMessage (com.hazelcast.client.impl.protocol.ClientMessage)5 ClientInvocation (com.hazelcast.client.impl.spi.impl.ClientInvocation)5 RaftOp (com.hazelcast.cp.internal.RaftOp)5 HazelcastClientInstanceImpl (com.hazelcast.client.impl.clientside.HazelcastClientInstanceImpl)4 AcquirePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp)2 LockAcquireLimitReachedException (com.hazelcast.cp.lock.exception.LockAcquireLimitReachedException)2 ChangePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.ChangePermitsOp)1 DrainPermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.DrainPermitsOp)1 ReleasePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.ReleasePermitsOp)1 Long2ObjectHashMap (com.hazelcast.internal.util.collection.Long2ObjectHashMap)1 ArrayList (java.util.ArrayList)1