Search in sources :

Example 1 with AcquirePermitsOp

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

the class SessionlessSemaphoreProxy method tryAcquire.

@Override
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
    checkPositive("permits", permits);
    long clusterWideThreadId = getOrCreateUniqueThreadId();
    long timeoutMs = max(0, unit.toMillis(timeout));
    RaftOp op = new AcquirePermitsOp(objectName, NO_SESSION_ID, clusterWideThreadId, newUnsecureUUID(), permits, timeoutMs);
    try {
        return invocationManager.<Boolean>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 2 with AcquirePermitsOp

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

the class SessionAwareSemaphoreProxy method tryAcquire.

@Override
public boolean tryAcquire(int permits, long timeout, TimeUnit unit) {
    checkPositive("permits", permits);
    long timeoutMs = max(0, unit.toMillis(timeout));
    long threadId = getThreadId();
    UUID invocationUid = newUnsecureUUID();
    long start;
    for (; ; ) {
        start = Clock.currentTimeMillis();
        long sessionId = acquireSession(permits);
        RaftOp op = new AcquirePermitsOp(objectName, sessionId, threadId, invocationUid, permits, timeoutMs);
        try {
            InternalCompletableFuture<Boolean> f = invocationManager.invoke(groupId, op);
            boolean acquired = f.joinInternal();
            if (!acquired) {
                releaseSession(sessionId, permits);
            }
            return acquired;
        } catch (SessionExpiredException e) {
            invalidateSession(sessionId);
            timeoutMs -= (Clock.currentTimeMillis() - start);
            if (timeoutMs <= 0) {
                return false;
            }
        } catch (WaitKeyCancelledException e) {
            releaseSession(sessionId, permits);
            return false;
        } 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 3 with AcquirePermitsOp

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

the class AcquirePermitsMessageTask method processMessage.

@Override
protected void processMessage() {
    RaftOp op = new AcquirePermitsOp(parameters.name, parameters.sessionId, parameters.threadId, parameters.invocationUid, parameters.permits, parameters.timeoutMs);
    invoke(parameters.groupId, op);
}
Also used : AcquirePermitsOp(com.hazelcast.cp.internal.datastructures.semaphore.operation.AcquirePermitsOp) RaftOp(com.hazelcast.cp.internal.RaftOp)

Example 4 with AcquirePermitsOp

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

the class AbstractSemaphoreFailureTest method testDrainCancelsPendingAcquireRequestWhenNotAcquired.

@Test(timeout = 300_000)
public void testDrainCancelsPendingAcquireRequestWhenNotAcquired() 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 invUid = newUnsecureUUID();
    RaftInvocationManager invocationManager = getRaftInvocationManager(proxyInstance);
    InternalCompletableFuture<Object> f = invocationManager.invoke(groupId, new AcquirePermitsOp(objectName, sessionId, threadId, invUid, 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());
    });
    semaphore.drainPermits();
    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 5 with AcquirePermitsOp

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

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