Search in sources :

Example 1 with ILock

use of com.hazelcast.core.ILock in project sonarqube by SonarSource.

the class AppStateClusterImpl method tryToLockWebLeader.

@Override
public boolean tryToLockWebLeader() {
    IAtomicReference<String> leader = hzInstance.getAtomicReference(LEADER);
    if (leader.get() == null) {
        ILock lock = hzInstance.getLock(LEADER);
        lock.lock();
        try {
            if (leader.get() == null) {
                leader.set(getLocalUuid());
                return true;
            } else {
                return false;
            }
        } finally {
            lock.unlock();
        }
    } else {
        return false;
    }
}
Also used : ILock(com.hazelcast.core.ILock)

Example 2 with ILock

use of com.hazelcast.core.ILock in project sonarqube by SonarSource.

the class AppStateClusterImpl method registerSonarQubeVersion.

@Override
public void registerSonarQubeVersion(String sonarqubeVersion) {
    IAtomicReference<String> sqVersion = hzInstance.getAtomicReference(SONARQUBE_VERSION);
    if (sqVersion.get() == null) {
        ILock lock = hzInstance.getLock(SONARQUBE_VERSION);
        lock.lock();
        try {
            if (sqVersion.get() == null) {
                sqVersion.set(sonarqubeVersion);
            }
        } finally {
            lock.unlock();
        }
    }
    String clusterVersion = sqVersion.get();
    if (!sqVersion.get().equals(sonarqubeVersion)) {
        hzInstance.shutdown();
        throw new IllegalStateException(String.format("The local version %s is not the same as the cluster %s", sonarqubeVersion, clusterVersion));
    }
}
Also used : ILock(com.hazelcast.core.ILock)

Example 3 with ILock

use of com.hazelcast.core.ILock in project hazelcast by hazelcast.

the class ClientRegressionWithMockNetworkTest method testLock_WhenClientAndOwnerNodeDiesTogether.

private void testLock_WhenClientAndOwnerNodeDiesTogether(boolean smart) throws InterruptedException {
    hazelcastFactory.newHazelcastInstance();
    final ClientConfig clientConfig = new ClientConfig();
    clientConfig.getNetworkConfig().setSmartRouting(smart);
    final int tryCount = 5;
    for (int i = 0; i < tryCount; i++) {
        final HazelcastInstance instance = hazelcastFactory.newHazelcastInstance();
        final HazelcastInstance client = hazelcastFactory.newHazelcastClient(clientConfig);
        final ILock lock = client.getLock("lock");
        assertTrue(lock.tryLock(1, TimeUnit.MINUTES));
        //with client is dead, lock should be released.
        client.getLifecycleService().terminate();
        instance.getLifecycleService().terminate();
    }
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ClientConfig(com.hazelcast.client.config.ClientConfig) ILock(com.hazelcast.core.ILock)

Example 4 with ILock

use of com.hazelcast.core.ILock in project hazelcast by hazelcast.

the class LegacyClientConditionTest method testDestroyLockWhenOtherWaitingOnConditionAwait.

@Test(expected = DistributedObjectDestroyedException.class)
public void testDestroyLockWhenOtherWaitingOnConditionAwait() {
    final ILock lock = client.getLock("testDestroyLockWhenOtherWaitingOnConditionAwait");
    final ICondition condition = lock.newCondition("condition");
    final CountDownLatch latch = new CountDownLatch(1);
    new Thread(new Runnable() {

        public void run() {
            try {
                latch.await(30, TimeUnit.SECONDS);
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            lock.destroy();
        }
    }).start();
    lock.lock();
    try {
        latch.countDown();
        condition.await();
    } catch (InterruptedException e) {
    }
    lock.unlock();
}
Also used : ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with ILock

use of com.hazelcast.core.ILock in project hazelcast by hazelcast.

the class LegacyClientConditionTest method testLockConditionSimpleUsage.

@Test
public void testLockConditionSimpleUsage() throws InterruptedException {
    final String name = randomString();
    final ILock lock = client.getLock(name);
    final ICondition condition = lock.newCondition(randomString());
    final AtomicInteger count = new AtomicInteger(0);
    final CountDownLatch threadGetTheLock = new CountDownLatch(1);
    Thread t = new Thread(new Runnable() {

        public void run() {
            lock.lock();
            threadGetTheLock.countDown();
            try {
                if (lock.isLockedByCurrentThread()) {
                    count.incrementAndGet();
                }
                condition.await();
                if (lock.isLockedByCurrentThread()) {
                    count.incrementAndGet();
                }
            } catch (InterruptedException ignored) {
            } finally {
                lock.unlock();
            }
        }
    });
    t.start();
    assertOpenEventually(threadGetTheLock);
    lock.lock();
    assertEquals(true, lock.isLocked());
    condition.signal();
    lock.unlock();
    t.join();
    assertEquals(2, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ILock(com.hazelcast.core.ILock) CountDownLatch(java.util.concurrent.CountDownLatch) ICondition(com.hazelcast.core.ICondition) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

ILock (com.hazelcast.core.ILock)77 Test (org.junit.Test)71 ParallelTest (com.hazelcast.test.annotation.ParallelTest)44 QuickTest (com.hazelcast.test.annotation.QuickTest)44 HazelcastInstance (com.hazelcast.core.HazelcastInstance)43 CountDownLatch (java.util.concurrent.CountDownLatch)37 ICondition (com.hazelcast.core.ICondition)33 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)20 Config (com.hazelcast.config.Config)11 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)9 AssertTask (com.hazelcast.test.AssertTask)7 TestThread (com.hazelcast.test.TestThread)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)6 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)3 IOException (java.io.IOException)3 ClientConfig (com.hazelcast.client.config.ClientConfig)2 OperationTimeoutException (com.hazelcast.core.OperationTimeoutException)2 Data (com.hazelcast.nio.serialization.Data)2 TimeoutException (java.util.concurrent.TimeoutException)2 InternalLockNamespace (com.hazelcast.concurrent.lock.InternalLockNamespace)1