Search in sources :

Example 6 with FencedLock

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

the class FencedLockLongAwaitTest method when_longWaitOperationIsNotCommitted_then_itFailsWithOperationTimeoutException.

@Test(timeout = 300000)
public void when_longWaitOperationIsNotCommitted_then_itFailsWithOperationTimeoutException() {
    HazelcastInstance apInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
    FencedLock lock = apInstance.getCPSubsystem().getLock(proxyName);
    spawn(lock::lock);
    assertTrueEventually(() -> assertTrue(lock.isLocked()));
    HazelcastInstance leader = getLeaderInstance(instances, groupId);
    for (int i = 0; i < groupSize; i++) {
        HazelcastInstance instance = instances[i];
        if (instance != leader) {
            instance.getLifecycleService().terminate();
        }
    }
    try {
        lock.lock();
        fail();
    } catch (OperationTimeoutException ignored) {
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FencedLock(com.hazelcast.cp.lock.FencedLock) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 7 with FencedLock

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

the class FencedLockLongAwaitTest method when_tryLockTimeoutPassesDuringLostMajority_then_operationTimeoutIsReceived.

@Test(timeout = 300000)
public void when_tryLockTimeoutPassesDuringLostMajority_then_operationTimeoutIsReceived() throws Exception {
    HazelcastInstance apInstance = factory.newHazelcastInstance(createConfig(groupSize, groupSize));
    FencedLock lock = apInstance.getCPSubsystem().getLock(proxyName);
    lock.lock();
    Future<Object> future = spawn(() -> lock.tryLock(callTimeoutSeconds + 5, SECONDS));
    HazelcastInstance leader = getLeaderInstance(instances, groupId);
    assertTrueEventually(() -> {
        LockService service = getNodeEngineImpl(leader).getService(LockService.SERVICE_NAME);
        assertEquals(1, service.getLiveOperations(groupId).size());
    });
    for (HazelcastInstance instance : instances) {
        if (instance != leader) {
            instance.getLifecycleService().terminate();
        }
    }
    try {
        future.get();
        fail();
    } catch (ExecutionException e) {
        assertTrue(e.getCause() instanceof OperationTimeoutException);
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FencedLock(com.hazelcast.cp.lock.FencedLock) OperationTimeoutException(com.hazelcast.core.OperationTimeoutException) ExecutionException(java.util.concurrent.ExecutionException) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 8 with FencedLock

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

the class FencedLockLongAwaitTest method testLongAwait.

private void testLongAwait(HazelcastInstance instance) throws ExecutionException, InterruptedException {
    FencedLock lock = instance.getCPSubsystem().getLock(proxyName);
    lock.lock();
    Future<Object> f1 = spawn(() -> {
        if (!lock.tryLock(5, TimeUnit.MINUTES)) {
            throw new IllegalStateException();
        }
        lock.unlock();
        return null;
    });
    Future<Object> f2 = spawn(() -> {
        lock.lock();
        lock.unlock();
        return null;
    });
    assertTrueEventually(() -> {
        LockService service = getNodeEngineImpl(instance).getService(LockService.SERVICE_NAME);
        assertEquals(2, service.getLiveOperations(groupId).size());
    }, 30);
    assertTrueAllTheTime(() -> {
        LockService service = getNodeEngineImpl(instance).getService(LockService.SERVICE_NAME);
        assertEquals(2, service.getLiveOperations(groupId).size());
    }, callTimeoutSeconds + 5);
    lock.unlock();
    assertCompletesEventually(f1);
    assertCompletesEventually(f2);
    f1.get();
    f2.get();
}
Also used : FencedLock(com.hazelcast.cp.lock.FencedLock)

Example 9 with FencedLock

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

the class UnsafeFencedLockMigrationTest method whenLockIsMigrated_thenSessionInformationShouldMigrate.

@Test
public void whenLockIsMigrated_thenSessionInformationShouldMigrate() {
    Config config = new Config();
    config.setProperty(ClusterProperty.PARTITION_COUNT.getName(), "2");
    HazelcastInstance hz1 = factory.newHazelcastInstance(config);
    // Create two locks on each partition,
    // so one of them is guaranteed to be migrated
    // after new instance is started.
    FencedLock lock1 = hz1.getCPSubsystem().getLock(generateName(hz1, 0));
    FencedLock lock2 = hz1.getCPSubsystem().getLock(generateName(hz1, 1));
    lock1.lock();
    lock2.lock();
    // Start new instance to trigger migration
    HazelcastInstance hz2 = factory.newHazelcastInstance(config);
    waitAllForSafeState(hz1, hz2);
    // Unlock after lock is migrated
    lock1.unlock();
    lock2.unlock();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FencedLock(com.hazelcast.cp.lock.FencedLock) Config(com.hazelcast.config.Config) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test)

Example 10 with FencedLock

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

the class UnsafeFencedLockMigrationTest method whenLockIsMigrated_thenWaitingOpsShouldBeNotified.

@Test
public void whenLockIsMigrated_thenWaitingOpsShouldBeNotified() throws Exception {
    Config config = new Config();
    config.setProperty(ClusterProperty.PARTITION_COUNT.getName(), "2");
    HazelcastInstance hz1 = factory.newHazelcastInstance(config);
    // Create two locks on each partition,
    // so one of them is guaranteed to be migrated
    // after new instance is started.
    FencedLock lock1 = hz1.getCPSubsystem().getLock(generateName(hz1, 0));
    FencedLock lock2 = hz1.getCPSubsystem().getLock(generateName(hz1, 1));
    lock1.lock();
    lock2.lock();
    Future waitingLock1 = spawn(lock1::lock);
    Future waitingLock2 = spawn(lock2::lock);
    // Ensure waiting op is registered
    LockService lockService = getNodeEngineImpl(hz1).getService(LockService.SERVICE_NAME);
    assertTrueEventually(() -> {
        LockRegistry registry1 = lockService.getRegistryOrNull(lock1.getGroupId());
        assertThat(registry1.getLiveOperations(), hasSize(1));
        LockRegistry registry2 = lockService.getRegistryOrNull(lock2.getGroupId());
        assertThat(registry2.getLiveOperations(), hasSize(1));
    });
    // Start new instance to trigger migration
    HazelcastInstance hz2 = factory.newHazelcastInstance(config);
    waitAllForSafeState(hz1, hz2);
    // Unlock after lock is migrated
    lock1.unlock();
    lock2.unlock();
    // Waiting op is triggered after unlock
    waitingLock1.get();
    waitingLock2.get();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) FencedLock(com.hazelcast.cp.lock.FencedLock) Config(com.hazelcast.config.Config) Future(java.util.concurrent.Future) ParallelJVMTest(com.hazelcast.test.annotation.ParallelJVMTest) QuickTest(com.hazelcast.test.annotation.QuickTest) 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