Search in sources :

Example 1 with QuorumListenerConfig

use of com.hazelcast.config.QuorumListenerConfig in project hazelcast by hazelcast.

the class CacheQuorumListenerTest method testQuorumEventProvidesCorrectMemberListSize.

@Test
public void testQuorumEventProvidesCorrectMemberListSize() {
    final CountDownLatch belowLatch = new CountDownLatch(1);
    Config config = new Config();
    QuorumListenerConfig listenerConfig = new QuorumListenerConfig();
    listenerConfig.setImplementation(new QuorumListener() {

        public void onChange(QuorumEvent quorumEvent) {
            if (!quorumEvent.isPresent()) {
                Collection<Member> currentMembers = quorumEvent.getCurrentMembers();
                assertEquals(3, quorumEvent.getThreshold());
                assertTrue(currentMembers.size() < quorumEvent.getThreshold());
                belowLatch.countDown();
            }
        }
    });
    String cacheName = randomString();
    String quorumName = randomString();
    QuorumConfig quorumConfig = new QuorumConfig(quorumName, true, 3);
    quorumConfig.addListenerConfig(listenerConfig);
    config.getCacheConfig(cacheName).setQuorumName(quorumName);
    config.addQuorumConfig(quorumConfig);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    HazelcastInstance instance1 = factory.newHazelcastInstance(config);
    factory.newHazelcastInstance(config);
    HazelcastServerCachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(instance1);
    Cache<Object, Object> cache = cachingProvider.getCacheManager().getCache(cacheName);
    try {
        cache.put(generateKeyOwnedBy(instance1), 1);
        fail("Expected a QuorumException");
    } catch (QuorumException expected) {
        EmptyStatement.ignore(expected);
    }
    assertOpenEventually(belowLatch, 15);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) QuorumConfig(com.hazelcast.config.QuorumConfig) Config(com.hazelcast.config.Config) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) QuorumEvent(com.hazelcast.quorum.QuorumEvent) QuorumListener(com.hazelcast.quorum.QuorumListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumException(com.hazelcast.quorum.QuorumException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Collection(java.util.Collection) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with QuorumListenerConfig

use of com.hazelcast.config.QuorumListenerConfig in project hazelcast by hazelcast.

the class QuorumListenerTest method testQuorumFailureEventFiredWhenNodeCountBelowThreshold.

@Test
public void testQuorumFailureEventFiredWhenNodeCountBelowThreshold() {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Config config = new Config();
    QuorumListenerConfig listenerConfig = new QuorumListenerConfig();
    listenerConfig.setImplementation(new QuorumListener() {

        public void onChange(QuorumEvent quorumEvent) {
            if (!quorumEvent.isPresent()) {
                countDownLatch.countDown();
            }
        }
    });
    String mapName = randomMapName();
    String quorumName = randomString();
    QuorumConfig quorumConfig = new QuorumConfig(quorumName, true, 3);
    quorumConfig.addListenerConfig(listenerConfig);
    config.getMapConfig(mapName).setQuorumName(quorumName);
    config.addQuorumConfig(quorumConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    IMap<Object, Object> map = instance.getMap(mapName);
    try {
        map.put(generateKeyOwnedBy(instance), 1);
    } catch (Exception e) {
        e.printStackTrace();
    }
    assertOpenEventually(countDownLatch, 15);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) MapConfig(com.hazelcast.config.MapConfig) QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumEvent(com.hazelcast.quorum.QuorumEvent) QuorumListener(com.hazelcast.quorum.QuorumListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with QuorumListenerConfig

use of com.hazelcast.config.QuorumListenerConfig in project hazelcast by hazelcast.

the class QuorumListenerTest method testQuorumFailureEventFiredWhenNodeCountDropsBelowThreshold.

@Test
public void testQuorumFailureEventFiredWhenNodeCountDropsBelowThreshold() {
    final CountDownLatch countDownLatch = new CountDownLatch(1);
    Config config = new Config();
    QuorumListenerConfig listenerConfig = new QuorumListenerConfig();
    listenerConfig.setImplementation(new QuorumListener() {

        public void onChange(QuorumEvent quorumEvent) {
            if (!quorumEvent.isPresent()) {
                countDownLatch.countDown();
            }
        }
    });
    String mapName = randomMapName();
    String quorumName = randomString();
    QuorumConfig quorumConfig = new QuorumConfig(quorumName, true, 3);
    quorumConfig.addListenerConfig(listenerConfig);
    config.getMapConfig(mapName).setQuorumName(quorumName);
    config.addQuorumConfig(quorumConfig);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    factory.newHazelcastInstance(config);
    factory.newHazelcastInstance();
    HazelcastInstance hz = factory.newHazelcastInstance();
    hz.shutdown();
    assertOpenEventually(countDownLatch, 15);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) MapConfig(com.hazelcast.config.MapConfig) QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumEvent(com.hazelcast.quorum.QuorumEvent) QuorumListener(com.hazelcast.quorum.QuorumListener) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 4 with QuorumListenerConfig

use of com.hazelcast.config.QuorumListenerConfig in project hazelcast by hazelcast.

the class QuorumListenerTest method testDifferentQuorumsGetCorrectEvents.

@Test
public void testDifferentQuorumsGetCorrectEvents() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(3);
    final CountDownLatch quorumFailureLatch = new CountDownLatch(2);
    String fourNodeQuorumName = "fourNode";
    QuorumConfig fourNodeQuorumConfig = new QuorumConfig(fourNodeQuorumName, true, 4);
    fourNodeQuorumConfig.addListenerConfig(new QuorumListenerConfig(new QuorumListener() {

        public void onChange(QuorumEvent quorumEvent) {
            if (!quorumEvent.isPresent()) {
                quorumFailureLatch.countDown();
            }
        }
    }));
    String threeNodeQuorumName = "threeNode";
    QuorumConfig threeNodeQuorumConfig = new QuorumConfig(threeNodeQuorumName, true, 3);
    threeNodeQuorumConfig.addListenerConfig(new QuorumListenerConfig(new QuorumListener() {

        public void onChange(QuorumEvent quorumEvent) {
            if (!quorumEvent.isPresent()) {
                quorumFailureLatch.countDown();
            }
        }
    }));
    MapConfig fourNodeMapConfig = new MapConfig("fourNode");
    fourNodeMapConfig.setQuorumName(fourNodeQuorumName);
    MapConfig threeNodeMapConfig = new MapConfig("threeNode");
    threeNodeMapConfig.setQuorumName(threeNodeQuorumName);
    Config config = new Config();
    config.addMapConfig(fourNodeMapConfig);
    config.addQuorumConfig(fourNodeQuorumConfig);
    config.addMapConfig(threeNodeMapConfig);
    config.addQuorumConfig(threeNodeQuorumConfig);
    factory.newHazelcastInstance(config);
    factory.newHazelcastInstance(config);
    assertOpenEventually(quorumFailureLatch);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) Config(com.hazelcast.config.Config) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) MapConfig(com.hazelcast.config.MapConfig) QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumEvent(com.hazelcast.quorum.QuorumEvent) QuorumListener(com.hazelcast.quorum.QuorumListener) MapConfig(com.hazelcast.config.MapConfig) CountDownLatch(java.util.concurrent.CountDownLatch) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with QuorumListenerConfig

use of com.hazelcast.config.QuorumListenerConfig in project hazelcast by hazelcast.

the class BaseQuorumListenerTest method testCustomResolverFiresQuorumFailureEvent.

@Test
public void testCustomResolverFiresQuorumFailureEvent() {
    CountDownLatch quorumNotPresent = new CountDownLatch(1);
    QuorumListenerConfig listenerConfig = new QuorumListenerConfig(quorumListener(null, quorumNotPresent));
    String distributedObjectName = randomString();
    String quorumName = randomString();
    QuorumConfig quorumConfig = new QuorumConfig().setName(quorumName).setEnabled(true).addListenerConfig(listenerConfig).setQuorumFunctionImplementation(new QuorumFunction() {

        @Override
        public boolean apply(Collection<Member> members) {
            return false;
        }
    });
    Config config = new Config().addQuorumConfig(quorumConfig);
    addQuorumConfig(config, distributedObjectName, quorumName);
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory();
    factory.newHazelcastInstance(config);
    factory.newHazelcastInstance();
    assertOpenEventually(quorumNotPresent, 15);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) Config(com.hazelcast.config.Config) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) QuorumConfig(com.hazelcast.config.QuorumConfig) CountDownLatch(java.util.concurrent.CountDownLatch) Member(com.hazelcast.core.Member) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

QuorumConfig (com.hazelcast.config.QuorumConfig)15 QuorumListenerConfig (com.hazelcast.config.QuorumListenerConfig)15 Config (com.hazelcast.config.Config)13 ParallelTest (com.hazelcast.test.annotation.ParallelTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 CountDownLatch (java.util.concurrent.CountDownLatch)13 Test (org.junit.Test)13 QuorumEvent (com.hazelcast.quorum.QuorumEvent)11 QuorumListener (com.hazelcast.quorum.QuorumListener)11 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)10 HazelcastInstance (com.hazelcast.core.HazelcastInstance)7 MapConfig (com.hazelcast.config.MapConfig)6 HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)5 CacheSimpleConfig (com.hazelcast.config.CacheSimpleConfig)5 QuorumException (com.hazelcast.quorum.QuorumException)5 Member (com.hazelcast.core.Member)3 Collection (java.util.Collection)3 QuorumFunction (com.hazelcast.quorum.QuorumFunction)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1