protected void removeNodeAndUpdateServers(final String hostToRemove) { log.warn("Removing host {}", hostToRemove); discoveredServerList.remove(hostToRemove); if (log.isInfoEnabled()) { log.info("Discovered server pool is now: {}", StringUtils.join(discoveredServerList, ",")); } if (!discoveredServerList.isEmpty()) { client.setServers(discoveredServerList); } else { client.setServers(bootstrapServerList); } }
@Override protected void runOneIteration() throws Exception { JestResult result; try { result = client.execute(action); } catch (CouldNotConnectException cnce) { // Can't connect to this node, remove it from the list log.error("Connect exception executing NodesInfo!", cnce); removeNodeAndUpdateServers(cnce.getHost()); return; // do not elevate the exception since that will stop the scheduled calls. // throw new RuntimeException("Error executing NodesInfo!", e); } catch (Exception e) { log.error("Error executing NodesInfo!", e); client.setServers(bootstrapServerList); return; // do not elevate the exception since that will stop the scheduled calls. // throw new RuntimeException("Error executing NodesInfo!", e); } 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(); // get as a JsonElement first as some nodes in the cluster may not have an http_address if (host.has(PUBLISH_ADDRESS_KEY)) { JsonElement addressElement = host.get(PUBLISH_ADDRESS_KEY); if (!addressElement.isJsonNull()) { String httpAddress = getHttpAddress(addressElement.getAsString()); if (httpAddress != null) httpHosts.add(httpAddress); } } } } if (log.isDebugEnabled()) { log.debug( "Discovered {} HTTP hosts: {}", httpHosts.size(), StringUtils.join(httpHosts, ",")); } discoveredServerList = httpHosts; client.setServers(discoveredServerList); } else { log.warn("NodesInfo request resulted in error: {}", result.getErrorMessage()); client.setServers(bootstrapServerList); } }
@Override protected void runOneIteration() throws Exception { NodesInfo action = new NodesInfo.Builder().build(); JestResult result = null; try { result = client.execute(action); } catch (Exception e) { logger.error("Error executing NodesInfo!", e); // do not elevate the exception since that will stop the scheduled calls. // throw new RuntimeException("Error executing NodesInfo!", e); } if (result != null) { 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 = (JsonObject) entry.getValue(); String http_address = host.get("http_address").getAsString(); if (null != http_address) { String cleanHttpAddress = "http://" + http_address.substring(6, http_address.length() - 1); httpHosts.add(cleanHttpAddress); } } } logger.info("Discovered Http Hosts: {}", httpHosts); client.setServers(httpHosts); } }