use of com.hazelcast.cp.internal.RaftService in project hazelcast by hazelcast.
the class AbstractFencedLockFailureTest method testRetriedWaitKeysAreExpiredTogether.
@Test(timeout = 300_000)
public void testRetriedWaitKeysAreExpiredTogether() {
CountDownLatch releaseLatch = new CountDownLatch(1);
spawn(() -> {
lock.lock();
assertOpenEventually(releaseLatch);
lock.unlock();
});
assertTrueEventually(() -> assertTrue(lock.isLocked()));
// there is a session id now
RaftGroupId groupId = lock.getGroupId();
long sessionId = getSessionManager().getSession(groupId);
assertNotEquals(NO_SESSION_ID, sessionId);
RaftInvocationManager invocationManager = getRaftInvocationManager();
UUID invUid = newUnsecureUUID();
BiTuple[] lockWaitTimeoutKeyRef = new BiTuple[1];
InternalCompletableFuture<Long> f1 = invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, SECONDS.toMillis(300)));
NodeEngineImpl nodeEngine = getNodeEngineImpl(primaryInstance);
LockService service = nodeEngine.getService(LockService.SERVICE_NAME);
assertTrueEventually(() -> {
LockRegistry registry = service.getRegistryOrNull(groupId);
assertNotNull(registry);
Map<BiTuple<String, UUID>, BiTuple<Long, Long>> waitTimeouts = registry.getWaitTimeouts();
assertEquals(1, waitTimeouts.size());
lockWaitTimeoutKeyRef[0] = waitTimeouts.keySet().iterator().next();
});
InternalCompletableFuture<Long> f2 = invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, SECONDS.toMillis(300)));
assertTrueEventually(() -> {
RaftService raftService = nodeEngine.getService(RaftService.SERVICE_NAME);
int partitionId = raftService.getCPGroupPartitionId(groupId);
LockRegistry 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() {
Lock lock = registry.getResourceOrNull(objectName);
Map<Object, WaitKeyContainer<LockInvocationKey>> waitKeys = lock.getInternalWaitKeysMap();
verified[0] = (waitKeys.size() == 1 && waitKeys.values().iterator().next().retryCount() == 1);
latch.countDown();
}
});
assertOpenEventually(latch);
assertTrue(verified[0]);
});
RaftOp op = new ExpireWaitKeysOp(LockService.SERVICE_NAME, Collections.singletonList(lockWaitTimeoutKeyRef[0]));
invocationManager.invoke(groupId, op).joinInternal();
assertTrueEventually(() -> assertTrue(service.getRegistryOrNull(groupId).getWaitTimeouts().isEmpty()));
releaseLatch.countDown();
assertTrueEventually(() -> assertFalse(lock.isLocked()));
long fence1 = f1.joinInternal();
long fence2 = f2.joinInternal();
assertInvalidFence(fence1);
assertInvalidFence(fence2);
}
use of com.hazelcast.cp.internal.RaftService 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());
}
Aggregations