Search in sources :

Example 6 with MembershipEvent

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

the class ClusterMembershipTest method newAddMemberListener.

private static MembershipAdapter newAddMemberListener(final CountDownLatch eventLatch) {
    return new MembershipAdapter() {

        // flag to check listener is not called concurrently
        final AtomicBoolean flag = new AtomicBoolean(false);

        public void memberAdded(MembershipEvent membershipEvent) {
            if (flag.compareAndSet(false, true)) {
                sleepMillis((int) (Math.random() * 500) + 50);
                eventLatch.countDown();
                flag.set(false);
            }
        }
    };
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter)

Example 7 with MembershipEvent

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

the class SplitBrainHandlerTest method testClusterMerge_when_split_not_detected_by_master.

private void testClusterMerge_when_split_not_detected_by_master(boolean multicastEnabled) throws InterruptedException {
    Config config = new Config();
    String groupName = generateRandomString(10);
    config.getGroupConfig().setName(groupName);
    config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "10");
    config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "10");
    config.setProperty(GroupProperty.MAX_NO_HEARTBEAT_SECONDS.getName(), "15");
    config.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "10");
    config.setProperty(GroupProperty.MAX_JOIN_MERGE_TARGET_SECONDS.getName(), "10");
    NetworkConfig networkConfig = config.getNetworkConfig();
    networkConfig.getJoin().getMulticastConfig().setEnabled(multicastEnabled);
    networkConfig.getJoin().getTcpIpConfig().setEnabled(!multicastEnabled).addMember("127.0.0.1");
    HazelcastInstance hz1 = newHazelcastInstance(config, "test-node1", new FirewallingNodeContext());
    HazelcastInstance hz2 = newHazelcastInstance(config, "test-node2", new FirewallingNodeContext());
    HazelcastInstance hz3 = newHazelcastInstance(config, "test-node3", new FirewallingNodeContext());
    final Node n1 = TestUtil.getNode(hz1);
    Node n2 = TestUtil.getNode(hz2);
    Node n3 = TestUtil.getNode(hz3);
    final CountDownLatch splitLatch = new CountDownLatch(2);
    MembershipAdapter membershipAdapter = new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent event) {
            if (n1.getLocalMember().equals(event.getMember())) {
                splitLatch.countDown();
            }
        }
    };
    hz2.getCluster().addMembershipListener(membershipAdapter);
    hz3.getCluster().addMembershipListener(membershipAdapter);
    final CountDownLatch mergeLatch = new CountDownLatch(1);
    hz1.getLifecycleService().addLifecycleListener(new MergedEventLifeCycleListener(mergeLatch));
    FirewallingTcpIpConnectionManager cm1 = getFireWalledConnectionManager(hz1);
    FirewallingTcpIpConnectionManager cm2 = getFireWalledConnectionManager(hz2);
    FirewallingTcpIpConnectionManager cm3 = getFireWalledConnectionManager(hz3);
    // block n2 & n3 on n1
    cm1.block(n2.address);
    cm1.block(n3.address);
    // remove and block n1 on n2 & n3
    n2.clusterService.removeAddress(n1.address, null);
    n3.clusterService.removeAddress(n1.address, null);
    cm2.block(n1.address);
    cm3.block(n1.address);
    assertTrue(splitLatch.await(120, TimeUnit.SECONDS));
    assertEquals(3, hz1.getCluster().getMembers().size());
    assertEquals(2, hz2.getCluster().getMembers().size());
    assertEquals(2, hz3.getCluster().getMembers().size());
    // unblock n2 on n1 and n1 on n2 & n3
    // n1 still blocks access to n3
    cm1.unblock(n2.address);
    cm2.unblock(n1.address);
    cm3.unblock(n1.address);
    assertTrue(mergeLatch.await(120, TimeUnit.SECONDS));
    assertEquals(3, hz1.getCluster().getMembers().size());
    assertEquals(3, hz2.getCluster().getMembers().size());
    assertEquals(3, hz3.getCluster().getMembers().size());
    assertEquals(n2.getThisAddress(), n1.getMasterAddress());
    assertEquals(n2.getThisAddress(), n2.getMasterAddress());
    assertEquals(n2.getThisAddress(), n3.getMasterAddress());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance) ListenerConfig(com.hazelcast.config.ListenerConfig) JoinConfig(com.hazelcast.config.JoinConfig) Config(com.hazelcast.config.Config) NetworkConfig(com.hazelcast.config.NetworkConfig) MembershipEvent(com.hazelcast.core.MembershipEvent) Node(com.hazelcast.instance.Node) NetworkConfig(com.hazelcast.config.NetworkConfig) MembershipAdapter(com.hazelcast.core.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) FirewallingTcpIpConnectionManager(com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager)

Example 8 with MembershipEvent

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

the class SplitBrainHandlerTest method testMergeAfterSplitBrain.

private void testMergeAfterSplitBrain(boolean multicast) throws InterruptedException {
    String groupName = generateRandomString(10);
    Config config = new Config();
    config.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "5");
    config.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "3");
    config.getGroupConfig().setName(groupName);
    NetworkConfig networkConfig = config.getNetworkConfig();
    JoinConfig join = networkConfig.getJoin();
    join.getMulticastConfig().setEnabled(multicast);
    join.getTcpIpConfig().setEnabled(!multicast);
    join.getTcpIpConfig().addMember("127.0.0.1");
    HazelcastInstance h1 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance h2 = Hazelcast.newHazelcastInstance(config);
    HazelcastInstance h3 = Hazelcast.newHazelcastInstance(config);
    final CountDownLatch splitLatch = new CountDownLatch(2);
    h3.getCluster().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            splitLatch.countDown();
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    });
    final CountDownLatch mergeLatch = new CountDownLatch(1);
    h3.getLifecycleService().addLifecycleListener(new MergedEventLifeCycleListener(mergeLatch));
    closeConnectionBetween(h1, h3);
    closeConnectionBetween(h2, h3);
    assertTrue(splitLatch.await(10, TimeUnit.SECONDS));
    assertEquals(2, h1.getCluster().getMembers().size());
    assertEquals(2, h2.getCluster().getMembers().size());
    assertEquals(1, h3.getCluster().getMembers().size());
    assertTrue(mergeLatch.await(30, TimeUnit.SECONDS));
    assertEquals(3, h1.getCluster().getMembers().size());
    assertEquals(3, h2.getCluster().getMembers().size());
    assertEquals(3, h3.getCluster().getMembers().size());
    assertEquals(ACTIVE, h1.getCluster().getClusterState());
    assertEquals(ACTIVE, h2.getCluster().getClusterState());
    assertEquals(ACTIVE, h3.getCluster().getClusterState());
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) HazelcastInstanceFactory.newHazelcastInstance(com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance) MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) ListenerConfig(com.hazelcast.config.ListenerConfig) JoinConfig(com.hazelcast.config.JoinConfig) Config(com.hazelcast.config.Config) NetworkConfig(com.hazelcast.config.NetworkConfig) MembershipEvent(com.hazelcast.core.MembershipEvent) NetworkConfig(com.hazelcast.config.NetworkConfig) JoinConfig(com.hazelcast.config.JoinConfig) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener)

Example 9 with MembershipEvent

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

the class QueueAdvancedTest method testDeadTaker.

/**
     * Test for issue 730 (Google).
     */
@Test
public void testDeadTaker() throws Exception {
    Config config = new Config();
    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    config.addListenerConfig(new ListenerConfig().setImplementation(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            shutdownLatch.countDown();
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    }));
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance[] instances = factory.newInstances(config);
    final HazelcastInstance h1 = instances[0];
    final HazelcastInstance h2 = instances[1];
    warmUpPartitions(h1, h2);
    final IQueue<String> q1 = h1.getQueue("default");
    final IQueue<String> q2 = h2.getQueue("default");
    final CountDownLatch startLatch = new CountDownLatch(1);
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                assertTrue("Expected startLatch.await() to succeed within 10 seconds", startLatch.await(10, SECONDS));
                Thread.sleep(5000);
                h2.getLifecycleService().terminate();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                startLatch.countDown();
                String value = q2.take();
                fail("Should not be able to take value from queue, but got: " + value);
            } catch (HazelcastInstanceNotActiveException e) {
                EmptyStatement.ignore(e);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }).start();
    assertTrue("Expected shutdownLatch.await() to succeed within 1 minute", shutdownLatch.await(1, MINUTES));
    q1.offer("item");
    assertEquals(1, q1.size());
    assertEquals("item", q1.poll());
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) ListenerConfig(com.hazelcast.config.ListenerConfig) Config(com.hazelcast.config.Config) MembershipEvent(com.hazelcast.core.MembershipEvent) CountDownLatch(java.util.concurrent.CountDownLatch) TestThread(com.hazelcast.test.TestThread) ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) HazelcastInstance(com.hazelcast.core.HazelcastInstance) MembershipListener(com.hazelcast.core.MembershipListener) TestHazelcastInstanceFactory(com.hazelcast.test.TestHazelcastInstanceFactory) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 10 with MembershipEvent

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

the class AbstractListenersOnReconnectTest method testListenersTerminateRandomNode.

private void testListenersTerminateRandomNode() {
    setupListener();
    terminateRandomNode();
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    final CountDownLatch memberAddedLatch = new CountDownLatch(1);
    clientInstanceImpl.getClientClusterService().addMembershipListener(new MembershipListener() {

        @Override
        public void memberAdded(MembershipEvent membershipEvent) {
            memberAddedLatch.countDown();
        }

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
        }

        @Override
        public void memberAttributeChanged(MemberAttributeEvent memberAttributeEvent) {
        }
    });
    factory.newHazelcastInstance();
    assertOpenEventually(memberAddedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : MemberAttributeEvent(com.hazelcast.core.MemberAttributeEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener)

Aggregations

MembershipEvent (com.hazelcast.core.MembershipEvent)22 CountDownLatch (java.util.concurrent.CountDownLatch)10 Test (org.junit.Test)10 InitialMembershipEvent (com.hazelcast.core.InitialMembershipEvent)9 MembershipAdapter (com.hazelcast.core.MembershipAdapter)9 HazelcastInstance (com.hazelcast.core.HazelcastInstance)8 Member (com.hazelcast.core.Member)6 ParallelTest (com.hazelcast.test.annotation.ParallelTest)6 QuickTest (com.hazelcast.test.annotation.QuickTest)6 Config (com.hazelcast.config.Config)5 ListenerConfig (com.hazelcast.config.ListenerConfig)5 JoinConfig (com.hazelcast.config.JoinConfig)4 NetworkConfig (com.hazelcast.config.NetworkConfig)4 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)4 AssertTask (com.hazelcast.test.AssertTask)4 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)3 MembershipListener (com.hazelcast.core.MembershipListener)3 Node (com.hazelcast.instance.Node)3 FirewallingTcpIpConnectionManager (com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager)3 TransactionalQueue (com.hazelcast.core.TransactionalQueue)2