use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.
the class SessionAwareSemaphoreProxy method acquire.
@Override
public void acquire(int permits) {
checkPositive(permits, "Permits must be positive!");
long threadId = getThreadId();
UUID invocationUid = newUnsecureUUID();
for (; ; ) {
long sessionId = acquireSession(permits);
RaftOp op = new AcquirePermitsOp(objectName, sessionId, threadId, invocationUid, permits, -1L);
try {
invocationManager.invoke(groupId, op).joinInternal();
return;
} catch (SessionExpiredException e) {
invalidateSession(sessionId);
} catch (WaitKeyCancelledException e) {
releaseSession(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) {
releaseSession(sessionId, permits);
throw e;
}
}
}
use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.
the class SessionlessSemaphoreProxy method acquire.
@Override
public void acquire(int permits) {
checkPositive(permits, "Permits must be positive!");
long clusterWideThreadId = getOrCreateUniqueThreadId();
RaftOp op = new AcquirePermitsOp(objectName, NO_SESSION_ID, clusterWideThreadId, newUnsecureUUID(), permits, -1L);
try {
invocationManager.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.");
}
}
use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.
the class AbstractSemaphoreAdvancedTest method testRetriedWaitKeysAreExpiredTogether.
@Test
public void testRetriedWaitKeysAreExpiredTogether() {
semaphore.init(1);
CountDownLatch releaseLatch = new CountDownLatch(1);
spawn(() -> {
try {
semaphore.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
assertOpenEventually(releaseLatch);
semaphore.release();
});
assertTrueEventually(() -> assertEquals(0, semaphore.availablePermits()));
// there is a session id now
RaftGroupId groupId = getGroupId();
AbstractProxySessionManager sessionManager = getSessionManager();
long sessionId = sessionManager.getSession(groupId);
assertNotEquals(NO_SESSION_ID, sessionId);
UUID invUid = newUnsecureUUID();
BiTuple[] acquireWaitTimeoutKeyRef = new BiTuple[1];
InternalCompletableFuture<Boolean> f1 = invokeRaftOp(groupId, new AcquirePermitsOp(objectName, sessionId, getThreadId(), invUid, 1, SECONDS.toMillis(300)));
assertTrueEventually(() -> {
NodeEngineImpl nodeEngine = getNodeEngineImpl(primaryInstance);
SemaphoreService service = nodeEngine.getService(SemaphoreService.SERVICE_NAME);
SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
Map<BiTuple<String, UUID>, BiTuple<Long, Long>> waitTimeouts = registry.getWaitTimeouts();
assertEquals(1, waitTimeouts.size());
acquireWaitTimeoutKeyRef[0] = waitTimeouts.keySet().iterator().next();
});
InternalCompletableFuture<Boolean> f2 = invokeRaftOp(groupId, new AcquirePermitsOp(objectName, sessionId, getThreadId(), invUid, 1, SECONDS.toMillis(300)));
assertTrueEventually(() -> {
NodeEngineImpl nodeEngine = getNodeEngineImpl(primaryInstance);
RaftService raftService = getNodeEngineImpl(primaryInstance).getService(RaftService.SERVICE_NAME);
int partitionId = raftService.getCPGroupPartitionId(groupId);
SemaphoreService service = nodeEngine.getService(SemaphoreService.SERVICE_NAME);
SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
boolean[] verified = new boolean[1];
CountDownLatch latch = new CountDownLatch(1);
OperationServiceImpl operationService = nodeEngine.getOperationService();
operationService.execute(new PartitionSpecificRunnable() {
@Override
public int getPartitionId() {
return partitionId;
}
@Override
public void run() {
Semaphore semaphore = registry.getResourceOrNull(objectName);
Map<Object, WaitKeyContainer<AcquireInvocationKey>> waitKeys = semaphore.getInternalWaitKeysMap();
verified[0] = (waitKeys.size() == 1 && waitKeys.values().iterator().next().retryCount() == 1);
latch.countDown();
}
});
assertOpenEventually(latch);
assertTrue(verified[0]);
});
RaftOp op = new ExpireWaitKeysOp(SemaphoreService.SERVICE_NAME, Collections.singletonList(acquireWaitTimeoutKeyRef[0]));
invokeRaftOp(groupId, op).joinInternal();
assertTrueEventually(() -> {
NodeEngineImpl nodeEngine = getNodeEngineImpl(primaryInstance);
SemaphoreService service = nodeEngine.getService(SemaphoreService.SERVICE_NAME);
assertTrue(service.getRegistryOrNull(groupId).getWaitTimeouts().isEmpty());
});
releaseLatch.countDown();
assertTrueEventually(() -> assertEquals(1, semaphore.availablePermits()));
assertFalse(f1.joinInternal());
assertFalse(f2.joinInternal());
}
use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.
the class AbstractSemaphoreFailureTest method testNewTryAcquireWithoutTimeoutCancelsPendingAcquireRequestsWhenNotAcquired.
@Test(timeout = 300_000)
public void testNewTryAcquireWithoutTimeoutCancelsPendingAcquireRequestsWhenNotAcquired() throws InterruptedException {
semaphore.init(1);
semaphore.acquire();
semaphore.release();
// if the session-aware semaphore is used, we guarantee that there is a session id now...
RaftGroupId groupId = getGroupId(semaphore);
long sessionId = getSessionId(proxyInstance, groupId);
long threadId = getThreadId(groupId);
UUID invUid1 = newUnsecureUUID();
UUID invUid2 = newUnsecureUUID();
RaftInvocationManager invocationManager = getRaftInvocationManager(proxyInstance);
InternalCompletableFuture<Object> f = invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid1, 2, MINUTES.toMillis(5)));
assertTrueEventually(() -> {
SemaphoreService service = getNodeEngineImpl(primaryInstance).getService(SemaphoreService.SERVICE_NAME);
SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
assertEquals(1, registry.getWaitTimeouts().size());
});
invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid2, 1, 0));
try {
f.joinInternal();
fail();
} catch (WaitKeyCancelledException ignored) {
}
}
use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.
the class AbstractSemaphoreFailureTest method testNewTryAcquireWithTimeoutCancelsPendingAcquireRequestWhenNotAcquired.
@Test(timeout = 300_000)
public void testNewTryAcquireWithTimeoutCancelsPendingAcquireRequestWhenNotAcquired() throws InterruptedException {
semaphore.init(1);
semaphore.acquire();
semaphore.release();
// if the session-aware semaphore is used, we guarantee that there is a session id now...
RaftGroupId groupId = getGroupId(semaphore);
long sessionId = getSessionId(proxyInstance, groupId);
long threadId = getThreadId(groupId);
UUID invUid1 = newUnsecureUUID();
UUID invUid2 = newUnsecureUUID();
RaftInvocationManager invocationManager = getRaftInvocationManager(proxyInstance);
InternalCompletableFuture<Object> f = invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid1, 2, MINUTES.toMillis(5)));
assertTrueEventually(() -> {
SemaphoreService service = getNodeEngineImpl(primaryInstance).getService(SemaphoreService.SERVICE_NAME);
SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
assertEquals(1, registry.getWaitTimeouts().size());
});
invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid2, 1, 100));
try {
f.joinInternal();
fail();
} catch (WaitKeyCancelledException ignored) {
}
}
Aggregations