Search in sources :

Example 1 with MembershipAdapter

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

the class ClientClusterProxyTest method removeMembershipListener.

@Test
public void removeMembershipListener() throws Exception {
    Cluster cluster = client().getCluster();
    String regId = cluster.addMembershipListener(new MembershipAdapter());
    assertTrue(cluster.removeMembershipListener(regId));
}
Also used : MembershipAdapter(com.hazelcast.core.MembershipAdapter) Cluster(com.hazelcast.core.Cluster) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 2 with MembershipAdapter

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

the class ClientTxnTest method testTxnRollbackOnServerCrash.

@Test
public void testTxnRollbackOnServerCrash() throws Exception {
    final String queueName = randomString();
    final TransactionContext context = client.newTransactionContext();
    CountDownLatch txnRollbackLatch = new CountDownLatch(1);
    final CountDownLatch memberRemovedLatch = new CountDownLatch(1);
    context.beginTransaction();
    final TransactionalQueue queue = context.getQueue(queueName);
    queue.offer(randomString());
    client.getCluster().addMembershipListener(new MembershipAdapter() {

        @Override
        public void memberRemoved(MembershipEvent membershipEvent) {
            memberRemovedLatch.countDown();
        }
    });
    server.getLifecycleService().terminate();
    try {
        context.commitTransaction();
        fail("commit should throw exception !");
    } catch (Exception e) {
        context.rollbackTransaction();
        txnRollbackLatch.countDown();
    }
    assertOpenEventually(txnRollbackLatch);
    assertOpenEventually(memberRemovedLatch);
    final IQueue<Object> q = client.getQueue(queueName);
    assertNull(q.poll());
    assertEquals(0, q.size());
}
Also used : TransactionalQueue(com.hazelcast.core.TransactionalQueue) TransactionContext(com.hazelcast.transaction.TransactionContext) MembershipEvent(com.hazelcast.core.MembershipEvent) MembershipAdapter(com.hazelcast.core.MembershipAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Example 3 with MembershipAdapter

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

use of com.hazelcast.core.MembershipAdapter 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 5 with MembershipAdapter

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

the class SplitBrainHandlerTest method testClusterMerge_when_split_not_detected_by_slave_and_restart_during_merge.

@Test
public // https://github.com/hazelcast/hazelcast/issues/8137
void testClusterMerge_when_split_not_detected_by_slave_and_restart_during_merge() 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(), "40");
    config.setProperty(GroupProperty.MAX_JOIN_MERGE_TARGET_SECONDS.getName(), "10");
    NetworkConfig networkConfig = config.getNetworkConfig();
    networkConfig.getJoin().getMulticastConfig().setEnabled(false);
    networkConfig.getJoin().getTcpIpConfig().setEnabled(true).addMember("127.0.0.1:5701").addMember("127.0.0.1:5702").addMember("127.0.0.1:5703");
    networkConfig.setPort(5702);
    HazelcastInstance hz2 = newHazelcastInstance(config, "test-node2", new FirewallingNodeContext());
    networkConfig.setPort(5703);
    HazelcastInstance hz3 = newHazelcastInstance(config, "test-node3", new FirewallingNodeContext());
    networkConfig.setPort(5701);
    HazelcastInstance hz1 = newHazelcastInstance(config, "test-node1", 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 mergingLatch = new CountDownLatch(1);
    final CountDownLatch mergeLatch = new CountDownLatch(1);
    LifecycleListener lifecycleListener = new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (event.getState() == LifecycleState.MERGING) {
                mergingLatch.countDown();
            }
            if (event.getState() == LifecycleState.MERGED) {
                mergeLatch.countDown();
            }
        }
    };
    hz1.getLifecycleService().addLifecycleListener(lifecycleListener);
    FirewallingTcpIpConnectionManager cm1 = getFireWalledConnectionManager(hz1);
    FirewallingTcpIpConnectionManager cm2 = getFireWalledConnectionManager(hz2);
    FirewallingTcpIpConnectionManager cm3 = getFireWalledConnectionManager(hz3);
    cm1.block(n2.address);
    cm1.block(n3.address);
    n2.clusterService.removeAddress(n1.address, null);
    n3.clusterService.removeAddress(n1.address, null);
    cm2.block(n1.address);
    cm3.block(n1.address);
    assertTrue(splitLatch.await(20, TimeUnit.SECONDS));
    assertEquals(2, hz2.getCluster().getMembers().size());
    assertEquals(2, hz3.getCluster().getMembers().size());
    assertEquals(3, hz1.getCluster().getMembers().size());
    cm1.unblock(n2.address);
    cm1.unblock(n3.address);
    cm2.unblock(n1.address);
    cm3.unblock(n1.address);
    assertTrue(mergingLatch.await(60, TimeUnit.SECONDS));
    hz2.getLifecycleService().terminate();
    hz2 = newHazelcastInstance(config, "test-node2", new FirewallingNodeContext());
    n2 = TestUtil.getNode(hz2);
    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(n3.getThisAddress(), n1.getMasterAddress());
    assertEquals(n3.getThisAddress(), n2.getMasterAddress());
    assertEquals(n3.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) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) FirewallingTcpIpConnectionManager(com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Aggregations

MembershipAdapter (com.hazelcast.core.MembershipAdapter)11 MembershipEvent (com.hazelcast.core.MembershipEvent)9 CountDownLatch (java.util.concurrent.CountDownLatch)7 Test (org.junit.Test)7 ParallelTest (com.hazelcast.test.annotation.ParallelTest)5 QuickTest (com.hazelcast.test.annotation.QuickTest)5 HazelcastInstance (com.hazelcast.core.HazelcastInstance)4 Config (com.hazelcast.config.Config)3 JoinConfig (com.hazelcast.config.JoinConfig)3 ListenerConfig (com.hazelcast.config.ListenerConfig)3 NetworkConfig (com.hazelcast.config.NetworkConfig)3 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)3 Node (com.hazelcast.instance.Node)3 FirewallingTcpIpConnectionManager (com.hazelcast.nio.tcp.FirewallingTcpIpConnectionManager)3 Member (com.hazelcast.core.Member)2 TransactionalQueue (com.hazelcast.core.TransactionalQueue)2 AssertTask (com.hazelcast.test.AssertTask)2 NightlyTest (com.hazelcast.test.annotation.NightlyTest)2 TransactionContext (com.hazelcast.transaction.TransactionContext)2 Cluster (com.hazelcast.core.Cluster)1