Search in sources :

Example 41 with RaftGroupId

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

the class AbstractFencedLockFailureTest method testIsLockedCallInitializesLockedSessionId.

@Test(timeout = 300_000)
public void testIsLockedCallInitializesLockedSessionId() {
    lock.lock();
    lock.unlock();
    // there is a session id now
    long threadId = getThreadId();
    RaftGroupId groupId = lock.getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    assertNotEquals(NO_SESSION_ID, sessionId);
    RaftInvocationManager invocationManager = getRaftInvocationManager();
    invocationManager.invoke(groupId, new LockOp(objectName, sessionId, threadId, newUnsecureUUID())).joinInternal();
    boolean locked = lock.isLocked();
    assertTrue(locked);
    Long lockedSessionId = lock.getLockedSessionId(threadId);
    assertNotNull(lockedSessionId);
    assertEquals(sessionId, (long) lockedSessionId);
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) LockOp(com.hazelcast.cp.internal.datastructures.lock.operation.LockOp) TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) Test(org.junit.Test)

Example 42 with RaftGroupId

use of com.hazelcast.cp.internal.RaftGroupId 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 43 with RaftGroupId

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

the class AbstractSemaphoreAdvancedTest method testRetriedDrainPermitsAppliedOnlyOnce.

@Test
public void testRetriedDrainPermitsAppliedOnlyOnce() throws ExecutionException, InterruptedException {
    semaphore.increasePermits(3);
    RaftGroupId groupId = getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    assertNotEquals(NO_SESSION_ID, sessionId);
    UUID invUid = newUnsecureUUID();
    int drained1 = this.<Integer>invokeRaftOp(groupId, new DrainPermitsOp(objectName, sessionId, getThreadId(), invUid)).joinInternal();
    assertEquals(3, drained1);
    assertEquals(0, semaphore.availablePermits());
    spawn(() -> semaphore.increasePermits(1)).get();
    int drained2 = this.<Integer>invokeRaftOp(groupId, new DrainPermitsOp(objectName, sessionId, getThreadId(), invUid)).joinInternal();
    assertEquals(3, drained2);
    assertEquals(1, semaphore.availablePermits());
}
Also used : RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) DrainPermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.DrainPermitsOp) Test(org.junit.Test)

Example 44 with RaftGroupId

use of com.hazelcast.cp.internal.RaftGroupId 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 45 with RaftGroupId

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

the class AbstractSemaphoreAdvancedTest method testRetriedIncreasePermitsAppliedOnlyOnce.

@Test
public void testRetriedIncreasePermitsAppliedOnlyOnce() throws InterruptedException {
    semaphore.init(1);
    semaphore.acquire();
    semaphore.release();
    // we guarantee that there is a session id now...
    RaftGroupId groupId = getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    assertNotEquals(NO_SESSION_ID, sessionId);
    UUID invUid = newUnsecureUUID();
    invokeRaftOp(groupId, new ChangePermitsOp(objectName, sessionId, getThreadId(), invUid, 1)).joinInternal();
    invokeRaftOp(groupId, new ChangePermitsOp(objectName, sessionId, getThreadId(), invUid, 1)).joinInternal();
    assertEquals(2, semaphore.availablePermits());
}
Also used : ChangePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.ChangePermitsOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Aggregations

RaftGroupId (com.hazelcast.cp.internal.RaftGroupId)59 Test (org.junit.Test)51 RaftInvocationManager (com.hazelcast.cp.internal.RaftInvocationManager)30 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)30 UUID (java.util.UUID)30 TryLockOp (com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp)16 WaitKeyCancelledException (com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException)13 AcquirePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp)13 AbstractProxySessionManager (com.hazelcast.cp.internal.session.AbstractProxySessionManager)10 LockOp (com.hazelcast.cp.internal.datastructures.lock.operation.LockOp)9 LockOwnershipLostException (com.hazelcast.cp.lock.exception.LockOwnershipLostException)6 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 RaftOp (com.hazelcast.cp.internal.RaftOp)4 RaftService (com.hazelcast.cp.internal.RaftService)4 InternalCompletableFuture (com.hazelcast.spi.impl.InternalCompletableFuture)4 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)3 PartitionSpecificRunnable (com.hazelcast.spi.impl.PartitionSpecificRunnable)3