@Override public void stateChanged(LifecycleEvent event) { logger.info("stateChanged; event: {}", event); if (LifecycleState.STARTED == event.getState()) { xtxCache = nodeEngine.getHazelcastInstance().getMap(CN_XDM_TRANSACTION); xddCache = nodeEngine.getHazelcastInstance().getMap(CN_XDM_DOCUMENT); keyCache = nodeEngine.getHazelcastInstance().getMap(CN_XDM_KEY); //readCatalog(catalog); // too early //checkPopulation(nodeEngine.getClusterService().getSize()); } else if (LifecycleState.SHUTTING_DOWN == event.getState()) { xtxCache.flush(); xddCache.flush(); logger.info("stateChanged; Maps were flushed"); } }
@Test public void testGetConnection() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final CountDownLatch latch = new CountDownLatch(2); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { latch.countDown(); return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); connectionManager.getConnection(); assertEquals(connection, connectionManager.getConnection()); verify(binder).bind(connection, credentials); assertEquals(connection, connectionManager.getConnection()); assertEquals(1, latch.getCount()); assertArrayEquals(new Object[]{LifecycleState.CLIENT_CONNECTION_OPENING}, lifecycleEvents.toArray()); }
@Test public void testGetConnectionWhenThereIsNoConnection() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { return null; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); connectionManager.getConnection(); assertEquals(null, connectionManager.getConnection()); assertEquals(null, connectionManager.getConnection()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
@Test public void testDifferentMemberAdded() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); Cluster cluster = mock(Cluster.class); InetSocketAddress inetSocketAddress2 = new InetSocketAddress("hostname", 5702); Member member = mock(Member.class); when(member.getInetSocketAddress()).thenReturn(inetSocketAddress2); MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED); connectionManager.memberAdded(membershipEvent); connectionManager.getClusterMembers().contains(inetSocketAddress2); assertEquals(2, connectionManager.getClusterMembers().size()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
@Test public void testMemberRemoved() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); Cluster cluster = mock(Cluster.class); Member member = mock(Member.class); when(member.getInetSocketAddress()).thenReturn(inetSocketAddress); MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_REMOVED); connectionManager.memberRemoved(membershipEvent); assertEquals(0, connectionManager.getClusterMembers().size()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
@Test public void testShouldExecuteOnDisconnect() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { return connection; } }; assertTrue(connectionManager.shouldExecuteOnDisconnect(connection)); assertFalse(connectionManager.shouldExecuteOnDisconnect(connection)); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
boolean waitFor(LifecycleEvent.LifecycleState state, int seconds) { long remainingMillis = TimeUnit.SECONDS.toMillis(seconds); while (remainingMillis >= 0) { LifecycleEvent.LifecycleState received = null; try { long now = Clock.currentTimeMillis(); received = eventQueue.poll(remainingMillis, TimeUnit.MILLISECONDS); remainingMillis -= (Clock.currentTimeMillis() - now); } catch (InterruptedException e) { return false; } if (received != null && received == state) { return true; } } return false; }
@Test public void testConfigLifecycleListener() throws InterruptedException { ClientConfig config = new ClientConfig(); final BlockingQueue<LifecycleEvent> q = new LinkedBlockingQueue<LifecycleEvent>(); config.addListener(new LifecycleListener() { public void stateChanged(final LifecycleEvent event) { q.offer(event); System.out.println(event); } }); Hazelcast.getDefaultInstance(); HazelcastClient client = HazelcastClient.newHazelcastClient(config); Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTING), q.poll(3, TimeUnit.SECONDS)); Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENING), q.poll(3, TimeUnit.SECONDS)); Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_OPENED), q.poll(3, TimeUnit.SECONDS)); Assert.assertEquals(new LifecycleEvent(LifecycleState.STARTED), q.poll(3, TimeUnit.SECONDS)); client.shutdown(); // Assert.assertEquals(new LifecycleEvent(LifecycleState.CLIENT_CONNECTION_LOST), q.poll(3, TimeUnit.SECONDS)); Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTTING_DOWN), q.poll(3, TimeUnit.SECONDS)); Assert.assertEquals(new LifecycleEvent(LifecycleState.SHUTDOWN), q.poll(3, TimeUnit.SECONDS)); }
@Test public void testGetConnection() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final CountDownLatch latch = new CountDownLatch(2); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { latch.countDown(); return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); connectionManager.getConnection(); assertEquals(connection, connectionManager.getConnection()); verify(binder).bind(connection, credentials); assertEquals(connection, connectionManager.getConnection()); assertEquals(1, latch.getCount()); Thread.sleep(100); // wait a little events to be fired assertArrayEquals(new Object[]{LifecycleState.CLIENT_CONNECTION_OPENING}, lifecycleEvents.toArray()); }
public void stateChanged(LifecycleEvent event) { if (event.getState().equals(LifecycleState.SHUTDOWN)) { leaveCluster(); } else if (event.getState().equals(LifecycleState.STARTED)) { joinCluster(); } }
public void fireLifecycleEvent(final LifecycleState lifecycleState) { callAsync(new Callable<Object>() { public Object call() throws Exception { fireLifecycleEvent(new LifecycleEvent(lifecycleState)); return null; } }); }
private LifecycleServiceClientImpl createLifecycleServiceClientImpl(HazelcastClient hazelcastClient, final List<LifecycleState> lifecycleEvents) { final LifecycleServiceClientImpl lifecycleService = new LifecycleServiceClientImpl(hazelcastClient); lifecycleService.addLifecycleListener(new LifecycleListener() { public void stateChanged(LifecycleEvent event) { lifecycleEvents.add(event.getState()); } }); return lifecycleService; }
@Test public void testDestroyConnection() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final CountDownLatch latch = new CountDownLatch(2); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { latch.countDown(); return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); assertEquals(connection, connectionManager.getConnection()); connectionManager.destroyConnection(connection); connectionManager.getConnection(); assertTrue(latch.await(1, TimeUnit.SECONDS)); assertArrayEquals(new Object[]{LifecycleState.CLIENT_CONNECTION_OPENING, LifecycleState.CLIENT_CONNECTION_LOST, LifecycleState.CLIENT_CONNECTION_OPENING}, lifecycleEvents.toArray()); }
@Test public void testSameMemberAdded() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final CountDownLatch latch = new CountDownLatch(2); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { latch.countDown(); return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); Cluster cluster = mock(Cluster.class); Member member = mock(Member.class); when(member.getInetSocketAddress()).thenReturn(inetSocketAddress); MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED); connectionManager.memberAdded(membershipEvent); connectionManager.getClusterMembers().contains(inetSocketAddress); assertEquals(1, connectionManager.getClusterMembers().size()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
@Test public void testUpdateMembers() throws Exception { HazelcastClient client = mock(HazelcastClient.class); Cluster cluster = mock(Cluster.class); when(client.getCluster()).thenReturn(cluster); Set<Member> members = new HashSet<Member>(); Member member1 = mock(Member.class); Member member2 = mock(Member.class); Member member3 = mock(Member.class); InetSocketAddress inetSocketAddress1 = new InetSocketAddress("localhost", 9701); InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 9702); InetSocketAddress inetSocketAddress3 = new InetSocketAddress("localhost", 9703); when(member1.getInetSocketAddress()).thenReturn(inetSocketAddress1); when(member2.getInetSocketAddress()).thenReturn(inetSocketAddress2); when(member3.getInetSocketAddress()).thenReturn(inetSocketAddress3); members.add(member1); members.add(member2); members.add(member3); when(cluster.getMembers()).thenReturn(members); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); connectionManager.updateMembers(); assertTrue(connectionManager.getClusterMembers().contains(inetSocketAddress1)); assertTrue(connectionManager.getClusterMembers().contains(inetSocketAddress2)); assertTrue(connectionManager.getClusterMembers().contains(inetSocketAddress3)); assertFalse(connectionManager.getClusterMembers().contains(inetSocketAddress)); assertEquals(3, connectionManager.getClusterMembers().size()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
/** * On {@link com.hazelcast.core.LifecycleEvent.LifecycleState} MERGED event all the topic listeners for the local node is added back. Since the data structures except for * IMaps are not merged after a split brain scenario within Hazelcast (data structures from MERGED nodes are * discarded) * @param lifecycleEvent {@link LifecycleEvent} */ @Override public void stateChanged(LifecycleEvent lifecycleEvent) { try { log.info("Hazelcast instance lifecycle changed state to " + lifecycleEvent.getState()); if (lifecycleEvent.getState() == LifecycleEvent.LifecycleState.MERGED) { log.info("Hazelcast cluster merge detected after a split brain. Updating unmerged data structures"); AndesContext.getInstance().getClusterNotificationListenerManager().reInitializeListener(); AndesSubscriptionManager andesSubscriptionManager = AndesContext.getInstance() .getAndesSubscriptionManager(); if(null != andesSubscriptionManager) { andesSubscriptionManager.updateSubscriptionsAfterClusterMerge(); } else { log.error("Andes Subscription Manager is not set. Local subscriptions are not synced with the " + "main cluster"); } // Notify that network partition has occurred. networkPartitionDetector.networkPartitionMerged(); } else if (lifecycleEvent.getState() == LifecycleState.SHUTDOWN){ networkPartitionDetector.clusterOutageOccurred(); } } catch (Throwable e) { log.error("Error occurred while handling Hazelcast state change event " + lifecycleEvent.getState(), e); } }
public void stateChanged(final LifecycleEvent event) { if (event.getState() == LifecycleState.STARTED && managementCenterConfig.isEnabled()) { try { start(); } catch (Exception e) { logger.log(Level.SEVERE, "ManagementCenterService could not be started!", e); } } }
@Test public void testDestroyConnection() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final CountDownLatch latch = new CountDownLatch(2); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { latch.countDown(); return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); assertEquals(connection, connectionManager.getConnection()); connectionManager.destroyConnection(connection); connectionManager.getConnection(); assertTrue(latch.await(1, TimeUnit.SECONDS)); Thread.sleep(100); // wait a little events to be fired assertArrayEquals(new Object[]{LifecycleState.CLIENT_CONNECTION_OPENING, LifecycleState.CLIENT_CONNECTION_LOST, LifecycleState.CLIENT_CONNECTION_OPENING}, lifecycleEvents.toArray()); }
@Test @Ignore public void testSameMemberAdded() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final CountDownLatch latch = new CountDownLatch(2); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { latch.countDown(); return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); Cluster cluster = mock(Cluster.class); Member member = mock(Member.class); when(member.getInetSocketAddress()).thenReturn(inetSocketAddress); MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED); connectionManager.memberAdded(membershipEvent); connectionManager.getClusterMembers().contains(inetSocketAddress); assertEquals(1, connectionManager.getClusterMembers().size()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
@Test @Ignore public void testDifferentMemberAdded() throws Exception { HazelcastClient client = mock(HazelcastClient.class); InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 5701); final Connection connection = mock(Connection.class); final List<LifecycleState> lifecycleEvents = new ArrayList<LifecycleState>(); final LifecycleServiceClientImpl lifecycleService = createLifecycleServiceClientImpl(client, lifecycleEvents); ClientConfig clientConfig = new ClientConfig(); clientConfig.setCredentials(credentials).addInetSocketAddress(inetSocketAddress).setConnectionTimeout(60000); ConnectionManager connectionManager = new ConnectionManager(client, clientConfig, lifecycleService) { protected Connection getNextConnection() { return connection; } }; ClientBinder binder = mock(ClientBinder.class); connectionManager.setBinder(binder); Cluster cluster = mock(Cluster.class); InetSocketAddress inetSocketAddress2 = new InetSocketAddress("hostname", 5702); Member member = mock(Member.class); when(member.getInetSocketAddress()).thenReturn(inetSocketAddress2); MembershipEvent membershipEvent = new MembershipEvent(cluster, member, MembershipEvent.MEMBER_ADDED); connectionManager.memberAdded(membershipEvent); connectionManager.getClusterMembers().contains(inetSocketAddress2); assertEquals(2, connectionManager.getClusterMembers().size()); assertArrayEquals(new Object[0], lifecycleEvents.toArray()); }
public void fireLifecycleEvent(LifecycleState lifecycleState) { fireLifecycleEvent(new LifecycleEvent(lifecycleState)); }
public void splitBrain(boolean multicast) throws Exception { Config c1 = new Config(); c1.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(multicast); c1.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(!multicast); c1.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1"); c1.getNetworkConfig().getInterfaces().clear(); c1.getNetworkConfig().getInterfaces().addInterface("127.0.0.1"); c1.getNetworkConfig().getInterfaces().setEnabled(true); Config c2 = new Config(); c2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(multicast); c2.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(!multicast); c2.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1"); c2.getNetworkConfig().getInterfaces().clear(); c2.getNetworkConfig().getInterfaces().addInterface("127.0.0.1"); c2.getNetworkConfig().getInterfaces().setEnabled(true); c1.getGroupConfig().setName("differentGroup"); c2.getGroupConfig().setName("sameGroup"); c1.setProperty(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS, "5"); c1.setProperty(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS, "3"); c2.setProperty(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS, "5"); c2.setProperty(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS, "3"); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c1); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c2); LifecycleCountingListener l = new LifecycleCountingListener(); h2.getLifecycleService().addLifecycleListener(l); int size = 500; for (int i = 0; i < size; i++) { h2.getMap("default").put(i, "value" + i); h2.getMultiMap("default").put(i, "value" + i); h2.getMultiMap("default").put(i, "value0" + i); } for (int i = 100; i < size + 100; i++) { h1.getMap("default").put(i, "value" + i); h1.getMultiMap("default").put(i, "value" + i); h1.getMultiMap("default").put(i, "value0" + i); } assertEquals(size, h2.getMap("default").size()); assertEquals(2 * size, h2.getMultiMap("default").size()); assertEquals(size, h1.getMap("default").size()); assertEquals(2 * size, h1.getMultiMap("default").size()); assertEquals(1, h1.getCluster().getMembers().size()); assertEquals(1, h2.getCluster().getMembers().size()); Thread.sleep(2000); c1.getGroupConfig().setName("sameGroup"); assertTrue(l.waitFor(LifecycleState.MERGED, 40)); assertEquals(1, l.getCount(LifecycleState.MERGING)); assertEquals(1, l.getCount(LifecycleState.RESTARTING)); assertEquals(1, l.getCount(LifecycleState.RESTARTED)); assertEquals(1, l.getCount(LifecycleState.MERGED)); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); Thread.sleep(2000); int newMapSize = size + 100; int newMultiMapSize = 2 * newMapSize; assertEquals(newMapSize, h1.getMap("default").size()); assertEquals(newMapSize, h2.getMap("default").size()); assertEquals(newMultiMapSize, h2.getMultiMap("default").size()); assertEquals(newMultiMapSize, h1.getMultiMap("default").size()); }
LifecycleCountingListener() { for (LifecycleEvent.LifecycleState state : LifecycleEvent.LifecycleState.values()) { counter.put(state, new AtomicInteger(0)); } }
int getCount(LifecycleEvent.LifecycleState state) { return counter.get(state).get(); }
@Test(timeout = 180000) public void testTcpIpSplitBrainStillWorksWhenTargetDisappears() throws Exception { // The ports are ordered like this so h3 will always attempt to merge with h1 Config c1 = buildConfig(false).setPort(25701); Config c2 = buildConfig(false).setPort(25704); Config c3 = buildConfig(false).setPort(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.getLifecycleService().shutdown(); } else if (event.getState() == LifecycleState.MERGED) { System.out.println("h3 restarted"); 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); 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()); }
@Test public void splitBrain() throws Exception { boolean multicast = true; Config c1 = new Config(); c1.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(multicast); c1.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(!multicast); c1.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1"); c1.getNetworkConfig().getInterfaces().clear(); c1.getNetworkConfig().getInterfaces().addInterface("127.0.0.1"); c1.getNetworkConfig().getInterfaces().setEnabled(true); Config c2 = new Config(); c2.getNetworkConfig().getJoin().getMulticastConfig().setEnabled(multicast); c2.getNetworkConfig().getJoin().getTcpIpConfig().setEnabled(!multicast); c2.getNetworkConfig().getJoin().getTcpIpConfig().addMember("127.0.0.1"); c2.getNetworkConfig().getInterfaces().clear(); c2.getNetworkConfig().getInterfaces().addInterface("127.0.0.1"); c2.getNetworkConfig().getInterfaces().setEnabled(true); c1.getGroupConfig().setName("differentGroup"); c2.getGroupConfig().setName("sameGroup"); c1.setProperty(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS, "5"); c1.setProperty(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS, "3"); c2.setProperty(GroupProperties.PROP_MERGE_FIRST_RUN_DELAY_SECONDS, "5"); c2.setProperty(GroupProperties.PROP_MERGE_NEXT_RUN_DELAY_SECONDS, "3"); HazelcastInstance h1 = Hazelcast.newHazelcastInstance(c1); HazelcastInstance h2 = Hazelcast.newHazelcastInstance(c2); HazelcastClient client2 = TestUtility.newHazelcastClient(c2.getGroupConfig().getName(), c2.getGroupConfig().getPassword(), "127.0.0.1:5702"); client2.getTopic("def").addMessageListener(new MessageListener<Object>() { public void onMessage(Message message) { } }); LifecycleCountingListener l = new LifecycleCountingListener(); h2.getLifecycleService().addLifecycleListener(l); for (int i = 0; i < 500; i++) { h2.getMap("default").put(i, "value" + i); h2.getMultiMap("default").put(i, "value" + i); h2.getMultiMap("default").put(i, "value0" + i); } assertEquals(500, h2.getMap("default").size()); assertEquals(1000, h2.getMultiMap("default").size()); assertEquals(1, h1.getCluster().getMembers().size()); assertEquals(1, h2.getCluster().getMembers().size()); Thread.sleep(2000); c1.getGroupConfig().setName("sameGroup"); Assert.assertTrue(l.waitFor(LifecycleState.MERGED, 40)); Assert.assertEquals(1, l.getCount(LifecycleState.MERGING)); Assert.assertEquals(1, l.getCount(LifecycleState.RESTARTING)); Assert.assertEquals(1, l.getCount(LifecycleState.RESTARTED)); Assert.assertEquals(1, l.getCount(LifecycleState.MERGED)); assertEquals(2, h1.getCluster().getMembers().size()); assertEquals(2, h2.getCluster().getMembers().size()); assertEquals(500, h1.getMap("default").size()); assertEquals(500, h2.getMap("default").size()); assertEquals(1000, h2.getMultiMap("default").size()); assertEquals(1000, h1.getMultiMap("default").size()); Thread.sleep(10000); }