NodeChecker extends AbstractScheduledService { @Override protected void runOneIteration() throws Exception { JestResult result; try { result = client.execute(action); } catch (CouldNotConnectException cnce) { log.error("Connect exception executing NodesInfo!", cnce); removeNodeAndUpdateServers(cnce.getHost()); return; } catch (Exception e) { log.error("Error executing NodesInfo!", e); client.setServers(bootstrapServerList); return; } if (result.isSucceeded()) { LinkedHashSet<String> httpHosts = new LinkedHashSet<String>(); JsonObject jsonMap = result.getJsonObject(); JsonObject nodes = (JsonObject) jsonMap.get("nodes"); if (nodes != null) { for (Entry<String, JsonElement> entry : nodes.entrySet()) { JsonObject host = entry.getValue().getAsJsonObject(); JsonElement addressElement = null; if (host.has("version")) { int majorVersion = Integer.parseInt(Splitter.on('.').splitToList(host.get("version").getAsString()).get(0)); if (majorVersion >= 5) { JsonObject http = host.getAsJsonObject("http"); if (http != null && http.has(PUBLISH_ADDRESS_KEY_V5)) addressElement = http.get(PUBLISH_ADDRESS_KEY_V5); } } if (addressElement == null) { if (host.has(PUBLISH_ADDRESS_KEY)) addressElement = host.get(PUBLISH_ADDRESS_KEY); } if (addressElement != null && !addressElement.isJsonNull()) { String httpAddress = getHttpAddress(addressElement.getAsString()); if(httpAddress != null) httpHosts.add(httpAddress); } } } if (log.isDebugEnabled()) { log.debug("Discovered {} HTTP hosts: {}", httpHosts.size(), Joiner.on(',').join(httpHosts)); } discoveredServerList = httpHosts; client.setServers(discoveredServerList); } else { log.warn("NodesInfo request resulted in error: {}", result.getErrorMessage()); client.setServers(bootstrapServerList); } } NodeChecker(JestClient jestClient, ClientConfig clientConfig);   }
@Test public void testWithResolvedWithoutHostnameAddressWithCustomScheme() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, new ClientConfig.Builder("http: .discoveryEnabled(true) .discoveryFrequency(1l, TimeUnit.SECONDS) .defaultSchemeForDiscoveredNodes("https") .build()); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "http_address", "inet[/192.168.2.7:9200]" ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("https: }
@Test public void testEsVersion5() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "version", "5.0.1", "http", ImmutableMap.of("publish_address", "192.168.2.7:9200") ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("http: }
@Test public void testWithResolvedWithoutHostnameAddress() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "http_address", "inet[/192.168.2.7:9200]" ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("http: }
@Test public void testWithResolvedWithHostnameAddress() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "http_address", "inet[searchly.com/192.168.2.7:9200]" ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("http: }
@Test public void testWithUnresolvedAddress() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "http_address", "inet[192.168.2.7:9200]" ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("http: }
@Test public void testWithInvalidUnresolvedAddress() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "http_address", "inet[192.168.2.7:]" ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(0, servers.size()); }
@Test public void testWithInvalidResolvedAddress() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node_name", ImmutableMap.of( "http_address", "inet[gg/192.168.2.7:]" ) ) )); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(0, servers.size()); }
@Test public void testNodesInfoExceptionUsesBootstrapServerList() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); when(jestClient.execute(isA(Action.class))).thenThrow(Exception.class); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("http: }
@Test public void testNodesInfoFailureUsesBootstrapServerList() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setSucceeded(false); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); Set servers = argument.getValue(); assertEquals(1, servers.size()); assertEquals("http: }
@Test public void testNodesInfoExceptionRemovesServerFromList() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); JestResult result = new JestResult(new Gson()); result.setJsonMap(ImmutableMap.<String, Object>of( "ok", "true", "nodes", ImmutableMap.of( "node1", ImmutableMap.of( "http_address", "inet[/192.168.2.7:9200]"), "node2", ImmutableMap.of( "http_address", "inet[/192.168.2.8:9200]"), "node3", ImmutableMap.of( "http_address", "inet[/192.168.2.9:9200]")))); result.setSucceeded(true); when(jestClient.execute(isA(Action.class))).thenReturn(result); nodeChecker.runOneIteration(); verify(jestClient).execute(isA(Action.class)); ArgumentCaptor<LinkedHashSet> argument = ArgumentCaptor.forClass(LinkedHashSet.class); verify(jestClient).setServers(argument.capture()); verify(jestClient).execute(isA(Action.class)); Set servers = argument.getValue(); assertEquals(3, servers.size()); when(jestClient.execute(isA(Action.class))).thenThrow(new CouldNotConnectException("http: nodeChecker.runOneIteration(); verify(jestClient, times(2)).execute(isA(Action.class)); verify(jestClient, times(2)).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); servers = argument.getValue(); assertEquals(2, servers.size()); Iterator serversItr = servers.iterator(); assertEquals("http: assertEquals("http: when(jestClient.execute(isA(Action.class))).thenThrow(new CouldNotConnectException("http: nodeChecker.runOneIteration(); verify(jestClient, times(3)).execute(isA(Action.class)); verify(jestClient, times(3)).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); servers = argument.getValue(); assertEquals(1, servers.size()); serversItr = servers.iterator(); assertEquals("http: when(jestClient.execute(isA(Action.class))).thenThrow(new CouldNotConnectException("http: nodeChecker.runOneIteration(); verify(jestClient, times(4)).execute(isA(Action.class)); verify(jestClient, times(4)).setServers(argument.capture()); verifyNoMoreInteractions(jestClient); servers = argument.getValue(); assertEquals(1, servers.size()); serversItr = servers.iterator(); assertEquals("http: }
@Test public void testNodesInfoExceptionBeforeNodesDiscovered() throws Exception { NodeChecker nodeChecker = new NodeChecker(jestClient, clientConfig); when(jestClient.execute(isA(Action.class))).thenThrow(new CouldNotConnectException("http: nodeChecker.runOneIteration(); assertNotNull(nodeChecker.discoveredServerList); assertEquals(0, nodeChecker.discoveredServerList.size()); }