Search in sources :

Example 26 with RaftOp

use of com.hazelcast.cp.internal.RaftOp in project hazelcast by hazelcast.

the class AbstractSemaphoreAdvancedTest method testInactiveSessionsAreEventuallyClosed.

@Test
public void testInactiveSessionsAreEventuallyClosed() throws ExecutionException, InterruptedException {
    semaphore.init(1);
    semaphore.acquire();
    RaftGroupId groupId = getGroupId();
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftSessionService sessionService = getNodeEngineImpl(instance).getService(RaftSessionService.SERVICE_NAME);
            assertFalse(sessionService.getAllSessions(groupId).get().isEmpty());
        }
    });
    AbstractProxySessionManager sessionManager = getSessionManager();
    long sessionId = sessionManager.getSession(groupId);
    assertNotEquals(NO_SESSION_ID, sessionId);
    // Not using semaphore.release(), because we want to keep sending session HBs.
    RaftOp op = new ReleasePermitsOp(objectName, sessionId, getThreadId(), newUnsecureUUID(), 1);
    invokeRaftOp(groupId, op).get();
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftSessionService service1 = getNodeEngineImpl(instance).getService(RaftSessionService.SERVICE_NAME);
            assertTrue(service1.getAllSessions(groupId).get().isEmpty());
        }
        assertEquals(NO_SESSION_ID, sessionManager.getSession(groupId));
    });
}
Also used : RaftSessionService(com.hazelcast.cp.internal.session.RaftSessionService) ReleasePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.ReleasePermitsOp) HazelcastInstance(com.hazelcast.core.HazelcastInstance) AbstractProxySessionManager(com.hazelcast.cp.internal.session.AbstractProxySessionManager) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) RaftOp(com.hazelcast.cp.internal.RaftOp) Test(org.junit.Test)

Example 27 with RaftOp

use of com.hazelcast.cp.internal.RaftOp 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());
}
Also used : RaftService(com.hazelcast.cp.internal.RaftService) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) ISemaphore(com.hazelcast.cp.ISemaphore) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Accessors.getNodeEngineImpl(com.hazelcast.test.Accessors.getNodeEngineImpl) RaftOp(com.hazelcast.cp.internal.RaftOp) CountDownLatch(java.util.concurrent.CountDownLatch) ExpireWaitKeysOp(com.hazelcast.cp.internal.datastructures.spi.blocking.operation.ExpireWaitKeysOp) AbstractProxySessionManager(com.hazelcast.cp.internal.session.AbstractProxySessionManager) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) PartitionSpecificRunnable(com.hazelcast.spi.impl.PartitionSpecificRunnable) Map(java.util.Map) BiTuple(com.hazelcast.internal.util.BiTuple) Test(org.junit.Test)

Example 28 with RaftOp

use of com.hazelcast.cp.internal.RaftOp 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);
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) RaftService(com.hazelcast.cp.internal.RaftService) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Accessors.getNodeEngineImpl(com.hazelcast.test.Accessors.getNodeEngineImpl) RaftOp(com.hazelcast.cp.internal.RaftOp) CountDownLatch(java.util.concurrent.CountDownLatch) ExpireWaitKeysOp(com.hazelcast.cp.internal.datastructures.spi.blocking.operation.ExpireWaitKeysOp) PartitionSpecificRunnable(com.hazelcast.spi.impl.PartitionSpecificRunnable) Map(java.util.Map) BiTuple(com.hazelcast.internal.util.BiTuple) Test(org.junit.Test)

Aggregations

RaftOp (com.hazelcast.cp.internal.RaftOp)28 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)7 UUID (java.util.UUID)7 AcquirePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp)6 SessionExpiredException (com.hazelcast.cp.internal.session.SessionExpiredException)5 CPGroupId (com.hazelcast.cp.CPGroupId)4 RaftGroupId (com.hazelcast.cp.internal.RaftGroupId)4 WaitKeyCancelledException (com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException)4 ChangePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.ChangePermitsOp)4 ReleasePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.ReleasePermitsOp)4 DrainPermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.DrainPermitsOp)3 Test (org.junit.Test)3 RaftService (com.hazelcast.cp.internal.RaftService)2 ApplyOp (com.hazelcast.cp.internal.datastructures.atomicref.operation.ApplyOp)2 TryLockOp (com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp)2 ExpireWaitKeysOp (com.hazelcast.cp.internal.datastructures.spi.blocking.operation.ExpireWaitKeysOp)2 AbstractProxySessionManager (com.hazelcast.cp.internal.session.AbstractProxySessionManager)2 BiTuple (com.hazelcast.internal.util.BiTuple)2 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)2 PartitionSpecificRunnable (com.hazelcast.spi.impl.PartitionSpecificRunnable)2