Search in sources :

Example 1 with MembershipListener

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

the class ClusterMembershipTest method testRemoveMembershipListener.

@Test
public void testRemoveMembershipListener() {
    TestHazelcastInstanceFactory factory = createHazelcastInstanceFactory(2);
    HazelcastInstance hz1 = factory.newHazelcastInstance();
    Cluster cluster = hz1.getCluster();
    MembershipListener membershipListener = mock(MembershipListener.class);
    String id = cluster.addMembershipListener(membershipListener);
    boolean removed = cluster.removeMembershipListener(id);
    assertTrue(removed);
    // now we add a member
    HazelcastInstance hz2 = factory.newHazelcastInstance();
    // and verify that the listener isn't called.
    verify(membershipListener, never()).memberAdded(any(MembershipEvent.class));
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) InitialMembershipEvent(com.hazelcast.core.InitialMembershipEvent) MembershipEvent(com.hazelcast.core.MembershipEvent) Cluster(com.hazelcast.core.Cluster) InitialMembershipListener(com.hazelcast.core.InitialMembershipListener) 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 2 with MembershipListener

use of com.hazelcast.core.MembershipListener 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 3 with MembershipListener

use of com.hazelcast.core.MembershipListener 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 4 with MembershipListener

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

the class ClientMemberAttributeTest method testRemoveAttributes.

@Test(timeout = 120000)
public void testRemoveAttributes() throws Exception {
    Config c = new Config();
    JoinConfig join = c.getNetworkConfig().getJoin();
    join.getTcpIpConfig().addMember("127.0.0.1").setEnabled(true);
    join.getMulticastConfig().setEnabled(false);
    HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(c);
    Member m1 = h1.getCluster().getLocalMember();
    m1.setIntAttribute("Test", 123);
    HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(c);
    assertEquals(2, h2.getCluster().getMembers().size());
    Member member = null;
    for (Member m : h2.getCluster().getMembers()) {
        if (m == h2.getCluster().getLocalMember()) {
            continue;
        }
        member = m;
    }
    assertNotNull(member);
    assertEquals(m1, member);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(123, (int) member.getIntAttribute("Test"));
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch latch = new CountDownLatch(3);
    final MembershipListener listener = new LatchMembershipListener(latch);
    h2.getCluster().addMembershipListener(listener);
    h1.getCluster().addMembershipListener(listener);
    client.getCluster().addMembershipListener(listener);
    m1.removeAttribute("Test");
    // Force sleep to distribute value
    assertOpenEventually(latch);
    assertNull(member.getIntAttribute("Test"));
    boolean found = false;
    Collection<Member> members = client.getCluster().getMembers();
    for (Member m : members) {
        if (m.equals(m1)) {
            assertNull(m.getIntAttribute("Test"));
            found = true;
        }
    }
    assertTrue(found);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ListenerConfig(com.hazelcast.config.ListenerConfig) JoinConfig(com.hazelcast.config.JoinConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig) JoinConfig(com.hazelcast.config.JoinConfig) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 5 with MembershipListener

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

the class ClientMemberAttributeTest method testChangeAttributes.

@Test(timeout = 120000)
public void testChangeAttributes() throws Exception {
    Config c = new Config();
    JoinConfig join = c.getNetworkConfig().getJoin();
    join.getTcpIpConfig().addMember("127.0.0.1").setEnabled(true);
    join.getMulticastConfig().setEnabled(false);
    HazelcastInstance h1 = hazelcastFactory.newHazelcastInstance(c);
    Member m1 = h1.getCluster().getLocalMember();
    m1.setIntAttribute("Test", 123);
    HazelcastInstance h2 = hazelcastFactory.newHazelcastInstance(c);
    assertEquals(2, h2.getCluster().getMembers().size());
    Member member = null;
    for (Member m : h2.getCluster().getMembers()) {
        if (m == h2.getCluster().getLocalMember()) {
            continue;
        }
        member = m;
    }
    assertNotNull(member);
    assertEquals(m1, member);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(123, (int) member.getIntAttribute("Test"));
    HazelcastInstance client = hazelcastFactory.newHazelcastClient();
    final CountDownLatch latch = new CountDownLatch(3);
    final MembershipListener listener = new LatchMembershipListener(latch);
    h2.getCluster().addMembershipListener(listener);
    h1.getCluster().addMembershipListener(listener);
    client.getCluster().addMembershipListener(listener);
    m1.setIntAttribute("Test", 321);
    // Force sleep to distribute value
    assertOpenEventually(latch);
    assertNotNull(member.getIntAttribute("Test"));
    assertEquals(321, (int) member.getIntAttribute("Test"));
    boolean found = false;
    Collection<Member> members = client.getCluster().getMembers();
    for (Member m : members) {
        if (m.equals(m1)) {
            assertEquals(321, (int) m.getIntAttribute("Test"));
            found = true;
        }
    }
    assertTrue(found);
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) ListenerConfig(com.hazelcast.config.ListenerConfig) JoinConfig(com.hazelcast.config.JoinConfig) ClientConfig(com.hazelcast.client.config.ClientConfig) Config(com.hazelcast.config.Config) MemberAttributeConfig(com.hazelcast.config.MemberAttributeConfig) JoinConfig(com.hazelcast.config.JoinConfig) CountDownLatch(java.util.concurrent.CountDownLatch) MembershipListener(com.hazelcast.core.MembershipListener) Member(com.hazelcast.core.Member) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

MembershipListener (com.hazelcast.core.MembershipListener)13 HazelcastInstance (com.hazelcast.core.HazelcastInstance)10 ParallelTest (com.hazelcast.test.annotation.ParallelTest)9 QuickTest (com.hazelcast.test.annotation.QuickTest)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 Test (org.junit.Test)9 Member (com.hazelcast.core.Member)7 ListenerConfig (com.hazelcast.config.ListenerConfig)6 TestHazelcastInstanceFactory (com.hazelcast.test.TestHazelcastInstanceFactory)6 Config (com.hazelcast.config.Config)5 MembershipEvent (com.hazelcast.core.MembershipEvent)5 JoinConfig (com.hazelcast.config.JoinConfig)4 MemberAttributeEvent (com.hazelcast.core.MemberAttributeEvent)4 ClientConfig (com.hazelcast.client.config.ClientConfig)3 MemberAttributeConfig (com.hazelcast.config.MemberAttributeConfig)3 Cluster (com.hazelcast.core.Cluster)2 InitialMembershipListener (com.hazelcast.core.InitialMembershipListener)2 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)1 NetworkConfig (com.hazelcast.config.NetworkConfig)1 ClientListener (com.hazelcast.core.ClientListener)1