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) {
}
}
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);
}
}
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();
}
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();
}
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();
}
Aggregations