use of com.hazelcast.cp.internal.session.SessionExpiredException in project hazelcast by hazelcast.
the class AbstractFencedLockProxy method tryLockAndGetFence.
@Override
public final long tryLockAndGetFence(long time, @Nonnull TimeUnit unit) {
checkNotNull(unit);
long threadId = getThreadId();
UUID invocationUid = newUnsecureUUID();
long timeoutMillis = Math.max(0, unit.toMillis(time));
long start;
for (; ; ) {
start = Clock.currentTimeMillis();
long sessionId = acquireSession();
verifyLockedSessionIdIfPresent(threadId, sessionId, true);
try {
long fence = doTryLock(sessionId, threadId, invocationUid, timeoutMillis).joinInternal();
if (fence != INVALID_FENCE) {
lockedSessionIds.put(threadId, sessionId);
} else {
releaseSession(sessionId);
}
return fence;
} catch (WaitKeyCancelledException e) {
releaseSession(sessionId);
return INVALID_FENCE;
} catch (SessionExpiredException e) {
invalidateSession(sessionId);
verifyNoLockedSessionIdPresent(threadId);
timeoutMillis -= (Clock.currentTimeMillis() - start);
if (timeoutMillis <= 0) {
return INVALID_FENCE;
}
} catch (Throwable t) {
releaseSession(sessionId);
throw rethrow(t);
}
}
}
use of com.hazelcast.cp.internal.session.SessionExpiredException in project hazelcast by hazelcast.
the class AbstractBlockingService method onSessionClose.
@Override
public final void onSessionClose(CPGroupId groupId, long sessionId) {
ResourceRegistry<W, R> registry = registries.get(groupId);
if (registry == null) {
if (logger.isFineEnabled()) {
logger.fine("Resource registry of " + groupId + " not found to handle closed Session[" + sessionId + "]");
}
return;
}
List<Long> expiredWaitKeys = new ArrayList<>();
Long2ObjectHashMap<Object> completedWaitKeys = new Long2ObjectHashMap<>();
registry.closeSession(sessionId, expiredWaitKeys, completedWaitKeys);
if (logger.isFineEnabled() && (expiredWaitKeys.size() > 0 || completedWaitKeys.size() > 0)) {
logger.fine("Closed Session[" + sessionId + "] in " + groupId + " expired wait key commit indices: " + expiredWaitKeys + " completed wait keys: " + completedWaitKeys);
}
completeFutures(groupId, expiredWaitKeys, new SessionExpiredException());
raftService.completeFutures(groupId, completedWaitKeys.entrySet());
}
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 must be positive!");
long timeoutMs = max(0, unit.toMillis(timeout));
long threadId = getThreadId();
UUID invocationUid = newUnsecureUUID();
long start;
for (; ; ) {
start = Clock.currentTimeMillis();
long sessionId = sessionManager.acquireSession(this.groupId, permits);
try {
ClientMessage request = SemaphoreAcquireCodec.encodeRequest(groupId, objectName, sessionId, threadId, invocationUid, permits, timeoutMs);
HazelcastClientInstanceImpl client = getClient();
ClientMessage response = new ClientInvocation(client, request, objectName).invoke().joinInternal();
boolean acquired = SemaphoreAcquireCodec.decodeResponse(response);
if (!acquired) {
sessionManager.releaseSession(this.groupId, sessionId, permits);
}
return acquired;
} catch (SessionExpiredException e) {
sessionManager.invalidateSession(this.groupId, sessionId);
timeoutMs -= (Clock.currentTimeMillis() - start);
if (timeoutMs <= 0) {
return false;
}
} catch (WaitKeyCancelledException e) {
sessionManager.releaseSession(this.groupId, sessionId, permits);
return false;
} catch (RuntimeException e) {
sessionManager.releaseSession(this.groupId, sessionId, permits);
throw e;
}
}
}
use of com.hazelcast.cp.internal.session.SessionExpiredException in project hazelcast by hazelcast.
the class SessionAwareSemaphoreProxy method acquire.
@Override
public void acquire(int permits) {
checkPositive("permits", permits);
long threadId = getThreadId();
UUID invocationUid = newUnsecureUUID();
for (; ; ) {
long sessionId = sessionManager.acquireSession(this.groupId, permits);
try {
ClientMessage request = SemaphoreAcquireCodec.encodeRequest(groupId, objectName, sessionId, threadId, invocationUid, permits, -1);
HazelcastClientInstanceImpl client = getClient();
new ClientInvocation(client, request, objectName).invoke().joinInternal();
return;
} catch (SessionExpiredException e) {
sessionManager.invalidateSession(this.groupId, sessionId);
} catch (WaitKeyCancelledException e) {
sessionManager.releaseSession(this.groupId, sessionId, permits);
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.");
} catch (RuntimeException e) {
sessionManager.releaseSession(this.groupId, sessionId, permits);
throw e;
}
}
}
Aggregations