Search in sources :

Example 11 with RaftInvocationManager

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

the class AbstractSemaphoreFailureTest method testRetriedAcquireReceivesPermitsOnlyOnce.

@Test(timeout = 300_000)
public void testRetriedAcquireReceivesPermitsOnlyOnce() throws InterruptedException, ExecutionException {
    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();
    RaftInvocationManager invocationManager = getRaftInvocationManager(proxyInstance);
    InternalCompletableFuture<Object> f1 = 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());
    });
    spawn(() -> {
        try {
            semaphore.tryAcquire(20, 5, TimeUnit.MINUTES);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    assertTrueEventually(() -> {
        SemaphoreService service = getNodeEngineImpl(primaryInstance).getService(SemaphoreService.SERVICE_NAME);
        SemaphoreRegistry registry = service.getRegistryOrNull(groupId);
        assertEquals(2, registry.getWaitTimeouts().size());
    });
    InternalCompletableFuture<Object> f2 = 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);
        Semaphore semaphore = registry.getResourceOrNull(objectName);
        assertEquals(2, semaphore.getInternalWaitKeysMap().size());
    });
    spawn(() -> semaphore.increasePermits(3)).get();
    f1.joinInternal();
    f2.joinInternal();
    assertEquals(2, semaphore.availablePermits());
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) ISemaphore(com.hazelcast.cp.ISemaphore) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 12 with RaftInvocationManager

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

the class AbstractCPMessageTask method invoke.

protected void invoke(CPGroupId groupId, RaftOp op) {
    RaftInvocationManager invocationManager = getInvocationManager();
    InternalCompletableFuture<Object> future = invocationManager.invoke(groupId, op, false);
    future.whenCompleteAsync(this);
}
Also used : RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager)

Example 13 with RaftInvocationManager

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

the class AbstractFencedLockFailureTest method testRetriedUnlockIsSuccessfulAfterLockedByAnotherEndpoint.

@Test(timeout = 300_000)
public void testRetriedUnlockIsSuccessfulAfterLockedByAnotherEndpoint() {
    lock.lock();
    RaftGroupId groupId = lock.getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    RaftInvocationManager invocationManager = getRaftInvocationManager();
    UUID invUid = newUnsecureUUID();
    invocationManager.invoke(groupId, new UnlockOp(objectName, sessionId, getThreadId(), invUid)).joinInternal();
    lockByOtherThread();
    invocationManager.invoke(groupId, new UnlockOp(objectName, sessionId, getThreadId(), invUid)).joinInternal();
}
Also used : UnlockOp(com.hazelcast.cp.internal.datastructures.lock.operation.UnlockOp) RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 14 with RaftInvocationManager

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

the class AbstractFencedLockFailureTest method testExpiredAndRetriedTryLockRequestReceivesFailureResponse.

@Test(timeout = 300_000)
public void testExpiredAndRetriedTryLockRequestReceivesFailureResponse() {
    final CountDownLatch unlockLatch = new CountDownLatch(1);
    spawn(() -> {
        lock.lock();
        assertOpenEventually(unlockLatch);
        lock.unlock();
    });
    assertTrueEventually(() -> assertTrue(lock.isLocked()));
    final RaftGroupId groupId = lock.getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    RaftInvocationManager invocationManager = getRaftInvocationManager(proxyInstance);
    UUID invUid = newUnsecureUUID();
    InternalCompletableFuture<Long> f1 = invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, SECONDS.toMillis(5)));
    long fence1 = f1.joinInternal();
    assertEquals(INVALID_FENCE, fence1);
    unlockLatch.countDown();
    assertTrueEventually(() -> assertFalse(lock.isLocked()));
    InternalCompletableFuture<Long> f2 = invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, SECONDS.toMillis(5)));
    long fence2 = f2.joinInternal();
    assertEquals(INVALID_FENCE, fence2);
}
Also used : TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) CountDownLatch(java.util.concurrent.CountDownLatch) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Test(org.junit.Test)

Example 15 with RaftInvocationManager

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

the class AbstractFencedLockFailureTest method testRetriedTryLockWithTimeoutDoesNotCancelPendingLockRequest.

@Test(timeout = 300_000)
public void testRetriedTryLockWithTimeoutDoesNotCancelPendingLockRequest() {
    lockByOtherThread();
    // there is a session id now
    RaftGroupId groupId = lock.getGroupId();
    long sessionId = getSessionManager().getSession(groupId);
    RaftInvocationManager invocationManager = getRaftInvocationManager();
    UUID invUid = newUnsecureUUID();
    invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, MINUTES.toMillis(5)));
    NodeEngineImpl nodeEngine = getNodeEngineImpl(primaryInstance);
    LockService service = nodeEngine.getService(LockService.SERVICE_NAME);
    assertTrueEventually(() -> {
        LockRegistry registry = service.getRegistryOrNull(groupId);
        assertNotNull(registry);
        assertNotNull(registry.getResourceOrNull(objectName));
        assertEquals(1, registry.getWaitTimeouts().size());
    });
    invocationManager.invoke(groupId, new TryLockOp(objectName, sessionId, getThreadId(), invUid, MINUTES.toMillis(5)));
    assertTrueEventually(() -> {
        RaftService raftService = getNodeEngineImpl(primaryInstance).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();
            }
        });
        latch.await(60, SECONDS);
        assertTrue(verified[0]);
    });
}
Also used : TryLockOp(com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp) NodeEngineImpl(com.hazelcast.spi.impl.NodeEngineImpl) Accessors.getNodeEngineImpl(com.hazelcast.test.Accessors.getNodeEngineImpl) RaftInvocationManager(com.hazelcast.cp.internal.RaftInvocationManager) RaftService(com.hazelcast.cp.internal.RaftService) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) CountDownLatch(java.util.concurrent.CountDownLatch) PartitionSpecificRunnable(com.hazelcast.spi.impl.PartitionSpecificRunnable) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID) Map(java.util.Map) OperationServiceImpl(com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl) Test(org.junit.Test)

Aggregations

RaftInvocationManager (com.hazelcast.cp.internal.RaftInvocationManager)35 Test (org.junit.Test)32 RaftGroupId (com.hazelcast.cp.internal.RaftGroupId)30 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)25 UUID (java.util.UUID)25 TryLockOp (com.hazelcast.cp.internal.datastructures.lock.operation.TryLockOp)16 WaitKeyCancelledException (com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException)12 AcquirePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp)12 LockOp (com.hazelcast.cp.internal.datastructures.lock.operation.LockOp)9 CountDownLatch (java.util.concurrent.CountDownLatch)4 CPGroup (com.hazelcast.cp.CPGroup)2 CPGroupId (com.hazelcast.cp.CPGroupId)2 CPGroupDestroyedException (com.hazelcast.cp.exception.CPGroupDestroyedException)2 RaftService (com.hazelcast.cp.internal.RaftService)2 GetRaftGroupOp (com.hazelcast.cp.internal.raftop.metadata.GetRaftGroupOp)2 TriggerDestroyRaftGroupOp (com.hazelcast.cp.internal.raftop.metadata.TriggerDestroyRaftGroupOp)2 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)2 PartitionSpecificRunnable (com.hazelcast.spi.impl.PartitionSpecificRunnable)2 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)2 Accessors.getNodeEngineImpl (com.hazelcast.test.Accessors.getNodeEngineImpl)2