Search in sources :

Example 1 with QuorumException

use of com.hazelcast.quorum.QuorumException 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 QuorumException

use of com.hazelcast.quorum.QuorumException in project hazelcast by hazelcast.

the class CacheQuorumListenerTest method testCustomResolverFiresQuorumFailureEvent.

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

        @Override
        public void onChange(QuorumEvent quorumEvent) {
            if (!quorumEvent.isPresent()) {
                countDownLatch.countDown();
            }
        }
    });
    String cacheName = randomString();
    String quorumName = randomString();
    QuorumConfig quorumConfig = new QuorumConfig();
    quorumConfig.setName(quorumName);
    quorumConfig.setEnabled(true);
    quorumConfig.addListenerConfig(listenerConfig);
    quorumConfig.setQuorumFunctionImplementation(new QuorumFunction() {

        @Override
        public boolean apply(Collection<Member> members) {
            return false;
        }
    });
    config.getCacheConfig(cacheName).setQuorumName(quorumName);
    config.addQuorumConfig(quorumConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    HazelcastServerCachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(instance);
    Cache<Object, Object> cache = cachingProvider.getCacheManager().getCache(cacheName);
    try {
        cache.put(generateKeyOwnedBy(instance), 1);
        fail("Expected a QuorumException");
    } catch (QuorumException expected) {
        EmptyStatement.ignore(expected);
    }
    assertOpenEventually(countDownLatch, 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) QuorumFunction(com.hazelcast.quorum.QuorumFunction) Member(com.hazelcast.core.Member) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with QuorumException

use of com.hazelcast.quorum.QuorumException in project hazelcast by hazelcast.

the class CacheQuorumListenerTest method testQuorumEventsFiredWhenNodeCountBelowThenAboveThreshold.

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

        public void onChange(QuorumEvent quorumEvent) {
            if (quorumEvent.isPresent()) {
                aboveLatch.countDown();
            } else {
                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);
    HazelcastServerCachingProvider cachingProvider1 = HazelcastServerCachingProvider.createCachingProvider(instance1);
    Cache<Object, Object> cache = cachingProvider1.getCacheManager().getCache(cacheName);
    try {
        cache.put(generateKeyOwnedBy(instance1), 1);
        fail("Expected a QuorumException");
    } catch (QuorumException expected) {
        EmptyStatement.ignore(expected);
    }
    assertOpenEventually(belowLatch, 15);
    factory.newHazelcastInstance(config);
    factory.newHazelcastInstance(config);
    cache.put(generateKeyOwnedBy(instance1), 1);
    assertOpenEventually(aboveLatch, 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) 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 4 with QuorumException

use of com.hazelcast.quorum.QuorumException in project hazelcast by hazelcast.

the class CacheQuorumListenerTest 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();
            }
        }
    }));
    CacheSimpleConfig fourNodeCacheConfig = new CacheSimpleConfig();
    fourNodeCacheConfig.setName("fourNode");
    fourNodeCacheConfig.setQuorumName(fourNodeQuorumName);
    CacheSimpleConfig threeNodeCacheConfig = new CacheSimpleConfig();
    threeNodeCacheConfig.setName("threeNode");
    threeNodeCacheConfig.setQuorumName(threeNodeQuorumName);
    Config config = new Config();
    config.addCacheConfig(fourNodeCacheConfig);
    config.addQuorumConfig(fourNodeQuorumConfig);
    config.addCacheConfig(threeNodeCacheConfig);
    config.addQuorumConfig(threeNodeQuorumConfig);
    HazelcastInstance h1 = factory.newHazelcastInstance(config);
    factory.newHazelcastInstance(config);
    HazelcastServerCachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(h1);
    Cache<Object, Object> fourNode = cachingProvider.getCacheManager().getCache("fourNode");
    Cache<Object, Object> threeNode = cachingProvider.getCacheManager().getCache("threeNode");
    try {
        threeNode.put(generateKeyOwnedBy(h1), "bar");
        fail("Expected a QuorumException");
    } catch (QuorumException expected) {
        EmptyStatement.ignore(expected);
    }
    try {
        fourNode.put(generateKeyOwnedBy(h1), "bar");
        fail("Expected a QuorumException");
    } catch (QuorumException expected) {
        EmptyStatement.ignore(expected);
    }
    assertOpenEventually(quorumFailureLatch, 15);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) CacheSimpleConfig(com.hazelcast.config.CacheSimpleConfig) 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) 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 5 with QuorumException

use of com.hazelcast.quorum.QuorumException in project hazelcast by hazelcast.

the class CacheQuorumListenerTest 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 cacheName = randomString();
    String quorumName = randomString();
    QuorumConfig quorumConfig = new QuorumConfig(quorumName, true, 3);
    quorumConfig.addListenerConfig(listenerConfig);
    config.getCacheConfig(cacheName).setQuorumName(quorumName);
    config.addQuorumConfig(quorumConfig);
    HazelcastInstance instance = createHazelcastInstance(config);
    HazelcastServerCachingProvider cachingProvider = HazelcastServerCachingProvider.createCachingProvider(instance);
    Cache<Object, Object> cache = cachingProvider.getCacheManager().getCache(cacheName);
    try {
        cache.put(generateKeyOwnedBy(instance), 1);
        fail("Expected a QuorumException");
    } catch (QuorumException expected) {
        EmptyStatement.ignore(expected);
    }
    assertOpenEventually(countDownLatch, 15);
}
Also used : QuorumConfig(com.hazelcast.config.QuorumConfig) QuorumException(com.hazelcast.quorum.QuorumException) QuorumListenerConfig(com.hazelcast.config.QuorumListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) 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) HazelcastServerCachingProvider(com.hazelcast.cache.impl.HazelcastServerCachingProvider) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

HazelcastServerCachingProvider (com.hazelcast.cache.impl.HazelcastServerCachingProvider)5 CacheSimpleConfig (com.hazelcast.config.CacheSimpleConfig)5 Config (com.hazelcast.config.Config)5 QuorumConfig (com.hazelcast.config.QuorumConfig)5 QuorumListenerConfig (com.hazelcast.config.QuorumListenerConfig)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)5 QuorumEvent (com.hazelcast.quorum.QuorumEvent)5 QuorumException (com.hazelcast.quorum.QuorumException)5 QuorumListener (com.hazelcast.quorum.QuorumListener)5 ParallelTest (com.hazelcast.test.annotation.ParallelTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Test (org.junit.Test)5 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)3 Member (com.hazelcast.core.Member)1 QuorumFunction (com.hazelcast.quorum.QuorumFunction)1 Collection (java.util.Collection)1