Search in sources :

Example 21 with LifecycleEvent

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

the class AbstractListenersOnReconnectTest method testListenersTerminateOwnerNode.

private void testListenersTerminateOwnerNode() {
    setupListener();
    final CountDownLatch disconnectedLatch = new CountDownLatch(1);
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    client.getLifecycleService().addLifecycleListener(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            if (LifecycleEvent.LifecycleState.CLIENT_DISCONNECTED == event.getState()) {
                disconnectedLatch.countDown();
            }
            if (LifecycleEvent.LifecycleState.CLIENT_CONNECTED == event.getState()) {
                connectedLatch.countDown();
            }
        }
    });
    HazelcastClientInstanceImpl clientInstanceImpl = getHazelcastClientInstanceImpl(client);
    HazelcastInstance server = getOwnerServer(factory, clientInstanceImpl);
    server.getLifecycleService().terminate();
    factory.newHazelcastInstance();
    assertClusterSizeEventually(clusterSize, client);
    assertOpenEventually(disconnectedLatch);
    assertOpenEventually(connectedLatch);
    validateRegistrationsAndListenerFunctionality();
}
Also used : HazelcastInstance(com.hazelcast.core.HazelcastInstance) LifecycleEvent(com.hazelcast.core.LifecycleEvent) HazelcastClientInstanceImpl(com.hazelcast.client.impl.HazelcastClientInstanceImpl) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 22 with LifecycleEvent

use of com.hazelcast.core.LifecycleEvent 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)

Example 23 with LifecycleEvent

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

the class SplitBrainHandlerTest method testMulticastJoin_DuringSplitBrainHandlerRunning.

@Test
public void testMulticastJoin_DuringSplitBrainHandlerRunning() throws InterruptedException {
    String groupName = generateRandomString(10);
    final CountDownLatch latch = new CountDownLatch(1);
    Config config1 = new Config();
    // bigger port to make sure address.hashCode() check pass during merge!
    config1.getNetworkConfig().setPort(5901);
    config1.getGroupConfig().setName(groupName);
    config1.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "5");
    config1.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "0");
    config1.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "0");
    config1.addListenerConfig(new ListenerConfig(new LifecycleListener() {

        public void stateChanged(final LifecycleEvent event) {
            switch(event.getState()) {
                case MERGING:
                case MERGED:
                    latch.countDown();
                default:
                    break;
            }
        }
    }));
    Hazelcast.newHazelcastInstance(config1);
    Thread.sleep(5000);
    Config config2 = new Config();
    config2.getGroupConfig().setName(groupName);
    config2.getNetworkConfig().setPort(5701);
    config2.setProperty(GroupProperty.WAIT_SECONDS_BEFORE_JOIN.getName(), "5");
    config2.setProperty(GroupProperty.MERGE_FIRST_RUN_DELAY_SECONDS.getName(), "0");
    config2.setProperty(GroupProperty.MERGE_NEXT_RUN_DELAY_SECONDS.getName(), "0");
    Hazelcast.newHazelcastInstance(config2);
    assertFalse("Latch should not be countdown!", latch.await(3, TimeUnit.SECONDS));
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) ListenerConfig(com.hazelcast.config.ListenerConfig) JoinConfig(com.hazelcast.config.JoinConfig) Config(com.hazelcast.config.Config) NetworkConfig(com.hazelcast.config.NetworkConfig) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 24 with LifecycleEvent

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

the class SplitBrainHandlerTest method testTcpIpSplitBrainStillWorks_WhenTargetDisappears.

@Test
public void testTcpIpSplitBrainStillWorks_WhenTargetDisappears() throws Exception {
    // The ports are ordered like this so h3 will always attempt to merge with h1
    Config c1 = buildConfig(false, 25701);
    Config c2 = buildConfig(false, 25704);
    Config c3 = buildConfig(false, 25703);
    List<String> clusterOneMembers = Arrays.asList("127.0.0.1:25701");
    List<String> clusterTwoMembers = Arrays.asList("127.0.0.1:25704");
    List<String> clusterThreeMembers = Arrays.asList("127.0.0.1:25703");
    c1.getNetworkConfig().getJoin().getTcpIpConfig().setMembers(clusterOneMembers);
    c2.getNetworkConfig().getJoin().getTcpIpConfig().setMembers(clusterTwoMembers);
    c3.getNetworkConfig().getJoin().getTcpIpConfig().setMembers(clusterThreeMembers);
    final HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c1);
    final HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c2);
    final CountDownLatch latch = new CountDownLatch(1);
    c3.addListenerConfig(new ListenerConfig(new LifecycleListener() {

        public void stateChanged(final LifecycleEvent event) {
            if (event.getState() == LifecycleState.MERGING) {
                h1.shutdown();
            } else if (event.getState() == LifecycleState.MERGED) {
                latch.countDown();
            }
        }
    }));
    final HazelcastInstance h3 = Hazelcast.newHazelcastInstance(c3);
    // We should have three clusters of one
    assertEquals(1, h1.getCluster().getMembers().size());
    assertEquals(1, h2.getCluster().getMembers().size());
    assertEquals(1, h3.getCluster().getMembers().size());
    List<String> allMembers = Arrays.asList("127.0.0.1:25701", "127.0.0.1:25704", "127.0.0.1:25703");
    h3.getConfig().getNetworkConfig().getJoin().getTcpIpConfig().setMembers(allMembers);
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    // Both nodes from cluster two should have joined cluster one
    assertFalse(h1.getLifecycleService().isRunning());
    assertEquals(2, h2.getCluster().getMembers().size());
    assertEquals(2, h3.getCluster().getMembers().size());
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) 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) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) NightlyTest(com.hazelcast.test.annotation.NightlyTest) Test(org.junit.Test)

Example 25 with LifecycleEvent

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

the class NodeShutdownEventsTest method testNodeShutdown_firesLifecycleEvents_afterJoinFailure.

/**
     * When a node fails due to a join time out, it will be shutdowned.
     * In that scenario we are expecting lifecycle events (SHUTTING_DOWN & SHUTDOWN)
     * to be fired locally.
     */
@Test
public void testNodeShutdown_firesLifecycleEvents_afterJoinFailure() throws Exception {
    // Only expecting SHUTTING_DOWN & SHUTDOWN events so latch count should be 2.
    final CountDownLatch shutdownEventCount = new CountDownLatch(2);
    final Config config1 = new Config();
    final Config config2 = new Config();
    // force join failure.
    config2.setProperty(GroupProperty.MAX_JOIN_SECONDS.getName(), "-100");
    // add lifecycle listener.
    final ListenerConfig listenerConfig = new ListenerConfig();
    listenerConfig.setImplementation(new LifecycleListener() {

        @Override
        public void stateChanged(LifecycleEvent event) {
            // Only expecting SHUTTING_DOWN & SHUTDOWN.
            if (LifecycleEvent.LifecycleState.SHUTTING_DOWN.equals(event.getState()) || LifecycleEvent.LifecycleState.SHUTDOWN.equals(event.getState())) {
                shutdownEventCount.countDown();
            }
        }
    });
    config2.addListenerConfig(listenerConfig);
    final HazelcastInstance node1 = Hazelcast.newHazelcastInstance(config1);
    try {
        final HazelcastInstance node2 = Hazelcast.newHazelcastInstance(config2);
    } catch (IllegalStateException e) {
    // ignore IllegalStateException since we are only testing lifecyle events.
    }
    assertOpenEventually(shutdownEventCount);
}
Also used : ListenerConfig(com.hazelcast.config.ListenerConfig) HazelcastInstance(com.hazelcast.core.HazelcastInstance) Config(com.hazelcast.config.Config) ListenerConfig(com.hazelcast.config.ListenerConfig) LifecycleEvent(com.hazelcast.core.LifecycleEvent) LifecycleListener(com.hazelcast.core.LifecycleListener) CountDownLatch(java.util.concurrent.CountDownLatch) QuickTest(com.hazelcast.test.annotation.QuickTest) Test(org.junit.Test) ParallelTest(com.hazelcast.test.annotation.ParallelTest)

Aggregations

LifecycleEvent (com.hazelcast.core.LifecycleEvent)25 LifecycleListener (com.hazelcast.core.LifecycleListener)25 HazelcastInstance (com.hazelcast.core.HazelcastInstance)22 CountDownLatch (java.util.concurrent.CountDownLatch)22 Test (org.junit.Test)20 ClientConfig (com.hazelcast.client.config.ClientConfig)13 ParallelTest (com.hazelcast.test.annotation.ParallelTest)13 QuickTest (com.hazelcast.test.annotation.QuickTest)13 NightlyTest (com.hazelcast.test.annotation.NightlyTest)9 Config (com.hazelcast.config.Config)8 ListenerConfig (com.hazelcast.config.ListenerConfig)6 HazelcastClientInstanceImpl (com.hazelcast.client.impl.HazelcastClientInstanceImpl)5 AssertTask (com.hazelcast.test.AssertTask)5 JoinConfig (com.hazelcast.config.JoinConfig)3 NetworkConfig (com.hazelcast.config.NetworkConfig)3 DistributedObject (com.hazelcast.core.DistributedObject)3 ExecutionException (java.util.concurrent.ExecutionException)3 LifecycleService (com.hazelcast.core.LifecycleService)2 HazelcastInstanceFactory.newHazelcastInstance (com.hazelcast.instance.HazelcastInstanceFactory.newHazelcastInstance)2 Node (com.hazelcast.instance.Node)2