Search in sources :

Example 46 with RaftGroupId

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

the class AbstractSemaphoreAdvancedTest method testRetriedDecreasePermitsAppliedOnlyOnce.

@Test
public void testRetriedDecreasePermitsAppliedOnlyOnce() throws InterruptedException {
    semaphore.init(2);
    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(1, 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)

Example 47 with RaftGroupId

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

the class AbstractSessionAwareSemaphoreBasicTest method testNoDuplicateRelease_whenSessionExpires.

@Test
public void testNoDuplicateRelease_whenSessionExpires() throws InterruptedException, ExecutionException {
    semaphore.init(5);
    semaphore.acquire(3);
    RaftGroupId groupId = getGroupId(semaphore);
    long session = getSessionManager(proxyInstance).getSession(groupId);
    assertNotEquals(NO_SESSION_ID, session);
    boolean sessionClosed = this.<Boolean>invokeRaftOp(groupId, new CloseSessionOp(session)).get();
    assertTrue(sessionClosed);
    assertEquals(5, semaphore.availablePermits());
    try {
        semaphore.release(1);
        fail();
    } catch (IllegalStateException expected) {
        if (expected.getCause() != null) {
            assertInstanceOf(SessionExpiredException.class, expected.getCause());
        }
    }
}
Also used : CloseSessionOp(com.hazelcast.cp.internal.session.operation.CloseSessionOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) Test(org.junit.Test)

Example 48 with RaftGroupId

use of com.hazelcast.cp.internal.RaftGroupId 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) {
    }
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 49 with RaftGroupId

use of com.hazelcast.cp.internal.RaftGroupId 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) {
    }
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 50 with RaftGroupId

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

the class AbstractSemaphoreFailureTest method testExpiredAndRetriedTryAcquireRequestReceivesFailureResponse.

@Test(timeout = 300_000)
public void testExpiredAndRetriedTryAcquireRequestReceivesFailureResponse() throws InterruptedException, ExecutionException {
    assumeFalse(isJDKCompatible());
    semaphore.init(1);
    semaphore.acquire();
    final RaftGroupId groupId = getGroupId(semaphore);
    long sessionId = getSessionId(proxyInstance, groupId);
    long threadId = getThreadId(groupId);
    UUID invUid = newUnsecureUUID();
    RaftInvocationManager invocationManager = getRaftInvocationManager(proxyInstance);
    InternalCompletableFuture<Boolean> f1 = invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid, 1, SECONDS.toMillis(5)));
    assertFalse(f1.joinInternal());
    spawn(() -> semaphore.release()).get();
    InternalCompletableFuture<Boolean> f2 = invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid, 1, SECONDS.toMillis(5)));
    assertFalse(f2.joinInternal());
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) 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