Search in sources :

Example 11 with FencedLock

use of com.hazelcast.cp.lock.FencedLock in project hazelcast by hazelcast.

the class UnsafeFencedLockMigrationTest method whenLockIsBlocked_thenBackupIsReplicated.

@Test
public void whenLockIsBlocked_thenBackupIsReplicated() {
    Config config = new Config();
    HazelcastInstance hz1 = factory.newHazelcastInstance(config);
    HazelcastInstance hz2 = factory.newHazelcastInstance(config);
    FencedLock lock = hz2.getCPSubsystem().getLock("lock@" + generateKeyOwnedBy(hz1));
    CountDownLatch latch = new CountDownLatch(1);
    spawn(() -> {
        lock.lock();
        latch.countDown();
        // wait until other thread blocks
        LockService lockService = getNodeEngineImpl(hz1).getService(LockService.SERVICE_NAME);
        assertTrueEventually(() -> {
            LockRegistry registry = lockService.getRegistryOrNull(lock.getGroupId());
            assertThat(registry.getLiveOperations(), hasSize(1));
        });
        lock.unlock();
    });
    assertOpenEventually(latch);
    lock.lock();
    waitAllForSafeState(hz1, hz2);
    hz1.getLifecycleService().terminate();
    lock.unlock();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FencedLock(com.hazelcast.cp.lock.FencedLock) Config(com.hazelcast.config.Config) CountDownLatch(java.util.concurrent.CountDownLatch) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 12 with FencedLock

use of com.hazelcast.cp.lock.FencedLock in project hazelcast by hazelcast.

the class CPMemberAddRemoveTest method test_sessionClosedOnCPSubsystemReset.

@Test
public void test_sessionClosedOnCPSubsystemReset() throws Exception {
    HazelcastInstance[] instances = newInstances(3, 3, 1);
    instances[0].getCPSubsystem().getAtomicLong("long1").set(1);
    instances[0].getCPSubsystem().getAtomicLong("long1@custom").set(2);
    FencedLock lock = instances[3].getCPSubsystem().getLock("lock");
    lock.lock();
    instances[0].getCPSubsystem().getCPSubsystemManagementService().reset().toCompletableFuture().get();
    assertTrueEventually(() -> {
        ProxySessionManagerService service = getNodeEngineImpl(instances[3]).getService(ProxySessionManagerService.SERVICE_NAME);
        assertEquals(NO_SESSION_ID, service.getSession((RaftGroupId) lock.getGroupId()));
    });
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance) FencedLock(com.hazelcast.cp.lock.FencedLock) ProxySessionManagerService(com.hazelcast.cp.internal.session.ProxySessionManagerService) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) Test(org.junit.Test) SlowTest(com.hazelcast.test.annotation.SlowTest)

Example 13 with FencedLock

use of com.hazelcast.cp.lock.FencedLock in project hazelcast by hazelcast.

the class AbstractFencedLockAdvancedTest method testActiveSessionIsNotClosedWhenPendingWaitKey.

@Test
public void testActiveSessionIsNotClosedWhenPendingWaitKey() {
    FencedLock other = null;
    for (HazelcastInstance instance : instances) {
        if (instance != proxyInstance) {
            other = instance.getCPSubsystem().getLock(lock.getName());
            break;
        }
    }
    assertNotNull(other);
    // lock from another instance
    other.lock();
    spawn(() -> {
        lock.tryLock(30, TimeUnit.MINUTES);
    });
    assertTrueEventually(() -> {
        for (HazelcastInstance instance : instances) {
            RaftSessionService sessionService = getNodeEngineImpl(instance).getService(RaftSessionService.SERVICE_NAME);
            assertEquals(2, sessionService.getAllSessions(lock.getGroupId()).get().size());
        }
    });
    assertTrueAllTheTime(() -> {
        for (HazelcastInstance instance : instances) {
            RaftSessionService sessionService = getNodeEngineImpl(instance).getService(RaftSessionService.SERVICE_NAME);
            assertEquals(2, sessionService.getAllSessions(lock.getGroupId()).get().size());
        }
    }, 20);
}
Also used : RaftSessionService(com.hazelcast.cp.internal.session.RaftSessionService) FencedLock(com.hazelcast.cp.lock.FencedLock) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Test(org.junit.Test)

Aggregations

FencedLock (com.hazelcast.cp.lock.FencedLock)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)10 Test (org.junit.Test)10 ParallelJVMTest (com.hazelcast.test.annotation.ParallelJVMTest)8 QuickTest (com.hazelcast.test.annotation.QuickTest)7 Config (com.hazelcast.config.Config)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)2 CPSubsystem (com.hazelcast.cp.CPSubsystem)1 ProxySessionManagerService (com.hazelcast.cp.internal.session.ProxySessionManagerService)1 RaftSessionService (com.hazelcast.cp.internal.session.RaftSessionService)1 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.impl.HazelcastInstanceFactory.newHazelcastInstance)1 HazelcastOSGiTestUtil.createHazelcastOSGiInstance (com.hazelcast.osgi.impl.HazelcastOSGiTestUtil.createHazelcastOSGiInstance)1 SlowTest (com.hazelcast.test.annotation.SlowTest)1 InvocationInfo (com.sun.ejb.InvocationInfo)1 MethodLockInfo (com.sun.ejb.MethodLockInfo)1 HashSet (java.util.HashSet)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1