Search in sources :

Example 26 with RaftGroupId

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

the class ProxySessionManagerService method onShutdown.

@Override
public boolean onShutdown(long timeout, TimeUnit unit) {
    ILogger logger = nodeEngine.getLogger(getClass());
    Map<RaftGroupId, InternalCompletableFuture<Object>> futures = shutdown();
    long remainingTimeNanos = unit.toNanos(timeout);
    boolean successful = true;
    while (remainingTimeNanos > 0 && futures.size() > 0) {
        Iterator<Entry<RaftGroupId, InternalCompletableFuture<Object>>> it = futures.entrySet().iterator();
        while (it.hasNext()) {
            Entry<RaftGroupId, InternalCompletableFuture<Object>> entry = it.next();
            RaftGroupId groupId = entry.getKey();
            InternalCompletableFuture<Object> f = entry.getValue();
            if (f.isDone()) {
                it.remove();
                try {
                    f.get();
                    logger.fine("Session closed for " + groupId);
                } catch (Exception e) {
                    logger.warning("Close session failed for " + groupId, e);
                    successful = false;
                }
            }
        }
        try {
            Thread.sleep(SHUTDOWN_TASK_PERIOD_IN_MILLIS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
            return false;
        }
        remainingTimeNanos -= MILLISECONDS.toNanos(SHUTDOWN_TASK_PERIOD_IN_MILLIS);
    }
    return successful && futures.isEmpty();
}
Also used : InternalCompletableFuture(com.hazelcast.spi.impl.InternalCompletableFuture) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) Entry(java.util.Map.Entry) ILogger(com.hazelcast.logging.ILogger)

Example 27 with RaftGroupId

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

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

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

Example 30 with RaftGroupId

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

the class AbstractFencedLockBasicTest method testReentrantTryLockWithTimeoutFails_whenSessionClosed.

@Test
public void testReentrantTryLockWithTimeoutFails_whenSessionClosed() throws ExecutionException, InterruptedException {
    long fence = lock.lockAndGetFence();
    assertValidFence(fence);
    AbstractProxySessionManager sessionManager = getSessionManager(proxyInstance);
    RaftGroupId groupId = (RaftGroupId) lock.getGroupId();
    long sessionId = sessionManager.getSession(groupId);
    assertNotEquals(NO_SESSION_ID, sessionId);
    closeSession(instances[0], groupId, sessionId);
    assertTrueEventually(() -> assertNotEquals(sessionId, sessionManager.getSession(groupId)));
    try {
        lock.tryLock(1, TimeUnit.SECONDS);
    } catch (LockOwnershipLostException ignored) {
    }
}
Also used : AbstractProxySessionManager(com.hazelcast.cp.internal.session.AbstractProxySessionManager) RaftGroupId(com.hazelcast.cp.internal.RaftGroupId) LockOwnershipLostException(com.hazelcast.cp.lock.exception.LockOwnershipLostException) 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