From 4c4ea87b7f2ac757e286bf5725d2cfb0f70ec410 Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Mon, 23 Dec 2013 11:40:34 +0100 Subject: [PATCH] ESExporter moves failed hosts to the end of the host list to avoid retrying them every 5s. --- .../marvel/monitor/exporter/ESExporter.java | 49 ++++++++++++------- 1 file changed, 31 insertions(+), 18 deletions(-) diff --git a/exporter/src/main/java/org/elasticsearch/marvel/monitor/exporter/ESExporter.java b/exporter/src/main/java/org/elasticsearch/marvel/monitor/exporter/ESExporter.java index 4255e0b14ffa..9ce5468c6c72 100644 --- a/exporter/src/main/java/org/elasticsearch/marvel/monitor/exporter/ESExporter.java +++ b/exporter/src/main/java/org/elasticsearch/marvel/monitor/exporter/ESExporter.java @@ -53,7 +53,7 @@ import java.util.Map; public class ESExporter extends AbstractLifecycleComponent implements StatsExporter { - final String[] hosts; + volatile String[] hosts; final String indexPrefix; final DateTimeFormatter indexTimeFormatter; final int timeout; @@ -239,24 +239,37 @@ public class ESExporter extends AbstractLifecycleComponent implement } private HttpURLConnection openConnection(String method, String uri, String contentType) { - for (String host : hosts) { - try { - URL templateUrl = new URL("http://" + host + uri); - HttpURLConnection conn = (HttpURLConnection) templateUrl.openConnection(); - conn.setRequestMethod(method); - conn.setConnectTimeout(timeout); - if (contentType != null) { - conn.setRequestProperty("Content-Type", XContentType.SMILE.restContentType()); - } - conn.setUseCaches(false); - if (method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT")) { - conn.setDoOutput(true); - } - conn.connect(); + int hostIndex = 0; + try { + for (; hostIndex < hosts.length; hostIndex++) { + String host = hosts[hostIndex]; + try { + URL templateUrl = new URL("http://" + host + uri); + HttpURLConnection conn = (HttpURLConnection) templateUrl.openConnection(); + conn.setRequestMethod(method); + conn.setConnectTimeout(timeout); + if (contentType != null) { + conn.setRequestProperty("Content-Type", XContentType.SMILE.restContentType()); + } + conn.setUseCaches(false); + if (method.equalsIgnoreCase("POST") || method.equalsIgnoreCase("PUT")) { + conn.setDoOutput(true); + } + conn.connect(); - return conn; - } catch (IOException e) { - logger.error("error connecting to [{}]", e, host); + return conn; + } catch (IOException e) { + logger.error("error connecting to [{}]", e, host); + } + } + } finally { + if (hostIndex > 0 && hostIndex < hosts.length) { + logger.debug("moving [{}] failed hosts to the end of the list", hostIndex + 1); + String[] newHosts = new String[hosts.length]; + System.arraycopy(hosts, hostIndex, newHosts, 0, hosts.length - hostIndex); + System.arraycopy(hosts, 0, newHosts, hosts.length - hostIndex - 1, hostIndex + 1); + hosts = newHosts; + logger.debug("preferred target host is now [{}]", hosts[0]); } }