Search in sources :

Example 6 with AcquirePermitsOp

use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.

the class SessionAwareSemaphoreProxy method acquire.

@Override
public void acquire(int permits) {
    checkPositive(permits, "Permits must be positive!");
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    for (; ; ) {
        long sessionId = acquireSession(permits);
        RaftOp op = new AcquirePermitsOp(objectName, sessionId, threadId, invocationUid, permits, -1L);
        try {
            invocationManager.invoke(groupId, op).joinInternal();
            return;
        } catch (SessionExpiredException e) {
            invalidateSession(sessionId);
        } catch (WaitKeyCancelledException e) {
            releaseSession(sessionId, permits);
            throw new IllegalStateException("Semaphore[" + objectName + "] not acquired because the acquire call " + "on the CP group is cancelled, possibly because of another indeterminate call from the same thread.");
        } catch (RuntimeException e) {
            releaseSession(sessionId, permits);
            throw e;
        }
    }
}
Also used : WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftOp(com.hazelcast.cp.internal.RaftOp) SessionExpiredException(com.hazelcast.cp.internal.session.SessionExpiredException) UuidUtil.newUnsecureUUID(com.hazelcast.internal.util.UuidUtil.newUnsecureUUID) UUID(java.util.UUID)

Example 7 with AcquirePermitsOp

use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp in project hazelcast by hazelcast.

the class SessionlessSemaphoreProxy method acquire.

@Override
public void acquire(int permits) {
    checkPositive(permits, "Permits must be positive!");
    long clusterWideThreadId = getOrCreateUniqueThreadId();
    RaftOp op = new AcquirePermitsOp(objectName, NO_SESSION_ID, clusterWideThreadId, newUnsecureUUID(), permits, -1L);
    try {
        invocationManager.invoke(groupId, op).joinInternal();
    } catch (WaitKeyCancelledException e) {
        throw new IllegalStateException("Semaphore[" + objectName + "] not acquired because the acquire call " + "on the CP group is cancelled, possibly because of another indeterminate call from the same thread.");
    }
}
Also used : WaitKeyCancelledException(com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException) AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftOp(com.hazelcast.cp.internal.RaftOp)

Example 8 with AcquirePermitsOp

use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp 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 9 with AcquirePermitsOp

use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp 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 10 with AcquirePermitsOp

use of com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp 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)

Aggregations

AcquirePermitsOp (com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp)18 UuidUtil.newUnsecureUUID (com.hazelcast.internal.util.UuidUtil.newUnsecureUUID)15 UUID (java.util.UUID)15 RaftGroupId (com.hazelcast.cp.internal.RaftGroupId)13 WaitKeyCancelledException (com.hazelcast.cp.internal.datastructures.exception.WaitKeyCancelledException)13 Test (org.junit.Test)13 RaftInvocationManager (com.hazelcast.cp.internal.RaftInvocationManager)12 RaftOp (com.hazelcast.cp.internal.RaftOp)6 ISemaphore (com.hazelcast.cp.ISemaphore)2 SessionExpiredException (com.hazelcast.cp.internal.session.SessionExpiredException)2 RaftService (com.hazelcast.cp.internal.RaftService)1 ExpireWaitKeysOp (com.hazelcast.cp.internal.datastructures.spi.blocking.operation.ExpireWaitKeysOp)1 AbstractProxySessionManager (com.hazelcast.cp.internal.session.AbstractProxySessionManager)1 BiTuple (com.hazelcast.internal.util.BiTuple)1 NodeEngineImpl (com.hazelcast.spi.impl.NodeEngineImpl)1 PartitionSpecificRunnable (com.hazelcast.spi.impl.PartitionSpecificRunnable)1 OperationServiceImpl (com.hazelcast.spi.impl.operationservice.impl.OperationServiceImpl)1 Accessors.getNodeEngineImpl (com.hazelcast.test.Accessors.getNodeEngineImpl)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1