diff --git a/client-sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java b/client-sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java index 3da6e2b95cd..7cdb51066cb 100644 --- a/client-sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java +++ b/client-sniffer/src/main/java/org/elasticsearch/client/sniff/HostsSniffer.java @@ -52,9 +52,9 @@ public class HostsSniffer { private final Scheme scheme; private final JsonFactory jsonFactory = new JsonFactory(); - protected HostsSniffer(RestClient restClient, long sniffRequestTimeout, Scheme scheme) { + protected HostsSniffer(RestClient restClient, long sniffRequestTimeoutMillis, Scheme scheme) { this.restClient = restClient; - this.sniffRequestParams = Collections.singletonMap("timeout", sniffRequestTimeout + "ms"); + this.sniffRequestParams = Collections.singletonMap("timeout", sniffRequestTimeoutMillis + "ms"); this.scheme = scheme; } @@ -155,7 +155,7 @@ public class HostsSniffer { public static final long DEFAULT_SNIFF_REQUEST_TIMEOUT = TimeUnit.SECONDS.toMillis(1); private final RestClient restClient; - private long sniffRequestTimeout = DEFAULT_SNIFF_REQUEST_TIMEOUT; + private long sniffRequestTimeoutMillis = DEFAULT_SNIFF_REQUEST_TIMEOUT; private Scheme scheme; private Builder(RestClient restClient) { @@ -164,15 +164,14 @@ public class HostsSniffer { } /** - * Sets the sniff request timeout to be passed in as a query string parameter to elasticsearch. - * Allows to halt the request without any failure, as only the nodes that have responded - * within this timeout will be returned. + * Sets the sniff request timeout (in milliseconds) to be passed in as a query string parameter to elasticsearch. + * Allows to halt the request without any failure, as only the nodes that have responded within this timeout will be returned. */ - public Builder setSniffRequestTimeout(int sniffRequestTimeout) { - if (sniffRequestTimeout <= 0) { - throw new IllegalArgumentException("sniffRequestTimeout must be greater than 0"); + public Builder setSniffRequestTimeoutMillis(int sniffRequestTimeoutMillis) { + if (sniffRequestTimeoutMillis <= 0) { + throw new IllegalArgumentException("sniffRequestTimeoutMillis must be greater than 0"); } - this.sniffRequestTimeout = sniffRequestTimeout; + this.sniffRequestTimeoutMillis = sniffRequestTimeoutMillis; return this; } @@ -189,7 +188,7 @@ public class HostsSniffer { * Creates a new {@link HostsSniffer} instance given the provided configuration */ public HostsSniffer build() { - return new HostsSniffer(restClient, sniffRequestTimeout, scheme); + return new HostsSniffer(restClient, sniffRequestTimeoutMillis, scheme); } } } diff --git a/client-sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java b/client-sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java index cd5d1d93148..37240741b3d 100644 --- a/client-sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java +++ b/client-sniffer/src/main/java/org/elasticsearch/client/sniff/Sniffer.java @@ -72,17 +72,17 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab private final HostsSniffer hostsSniffer; private final RestClient restClient; - private final long sniffInterval; - private final long sniffAfterFailureDelay; + private final long sniffIntervalMillis; + private final long sniffAfterFailureDelayMillis; private final ScheduledExecutorService scheduledExecutorService; private final AtomicBoolean running = new AtomicBoolean(false); private ScheduledFuture scheduledFuture; - private Task(HostsSniffer hostsSniffer, RestClient restClient, long sniffInterval, long sniffAfterFailureDelay) { + private Task(HostsSniffer hostsSniffer, RestClient restClient, long sniffIntervalMillis, long sniffAfterFailureDelayMillis) { this.hostsSniffer = hostsSniffer; this.restClient = restClient; - this.sniffInterval = sniffInterval; - this.sniffAfterFailureDelay = sniffAfterFailureDelay; + this.sniffIntervalMillis = sniffIntervalMillis; + this.sniffAfterFailureDelayMillis = sniffAfterFailureDelayMillis; this.scheduledExecutorService = Executors.newScheduledThreadPool(1); scheduleNextRun(0); } @@ -104,11 +104,11 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab @Override public void run() { - sniff(null, sniffInterval); + sniff(null, sniffIntervalMillis); } void sniffOnFailure(HttpHost failedHost) { - sniff(failedHost, sniffAfterFailureDelay); + sniff(failedHost, sniffAfterFailureDelayMillis); } void sniff(HttpHost excludeHost, long nextSniffDelayMillis) { @@ -158,9 +158,9 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab private final RestClient restClient; private final HostsSniffer hostsSniffer; - private long sniffInterval = DEFAULT_SNIFF_INTERVAL; + private long sniffIntervalMillis = DEFAULT_SNIFF_INTERVAL; private boolean sniffOnFailure = true; - private long sniffAfterFailureDelay = DEFAULT_SNIFF_AFTER_FAILURE_DELAY; + private long sniffAfterFailureDelayMillis = DEFAULT_SNIFF_AFTER_FAILURE_DELAY; /** * Creates a new builder instance by providing the {@link RestClient} that will be used to communicate with elasticsearch, @@ -174,21 +174,21 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab } /** - * Sets the interval between consecutive ordinary sniff executions. Will be honoured when sniffOnFailure is disabled or - * when there are no failures between consecutive sniff executions. - * @throws IllegalArgumentException if sniffInterval is not greater than 0 + * Sets the interval between consecutive ordinary sniff executions in milliseconds. Will be honoured when + * sniffOnFailure is disabled or when there are no failures between consecutive sniff executions. + * @throws IllegalArgumentException if sniffIntervalMillis is not greater than 0 */ - public Builder setSniffInterval(int sniffInterval) { - if (sniffInterval <= 0) { - throw new IllegalArgumentException("sniffInterval must be greater than 0"); + public Builder setSniffIntervalMillis(int sniffIntervalMillis) { + if (sniffIntervalMillis <= 0) { + throw new IllegalArgumentException("sniffIntervalMillis must be greater than 0"); } - this.sniffInterval = sniffInterval; + this.sniffIntervalMillis = sniffIntervalMillis; return this; } /** * Enables/disables sniffing on failure. If enabled, at each failure nodes will be reloaded, and a new sniff execution will - * be scheduled after a shorter time than usual (sniffAfterFailureDelay). + * be scheduled after a shorter time than usual (sniffAfterFailureDelayMillis). */ public Builder setSniffOnFailure(boolean sniffOnFailure) { this.sniffOnFailure = sniffOnFailure; @@ -196,13 +196,13 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab } /** - * Sets the delay of a sniff execution scheduled after a failure. + * Sets the delay of a sniff execution scheduled after a failure (in milliseconds) */ - public Builder setSniffAfterFailureDelay(int sniffAfterFailureDelay) { - if (sniffAfterFailureDelay <= 0) { - throw new IllegalArgumentException("sniffAfterFailureDelay must be greater than 0"); + public Builder setSniffAfterFailureDelayMillis(int sniffAfterFailureDelayMillis) { + if (sniffAfterFailureDelayMillis <= 0) { + throw new IllegalArgumentException("sniffAfterFailureDelayMillis must be greater than 0"); } - this.sniffAfterFailureDelay = sniffAfterFailureDelay; + this.sniffAfterFailureDelayMillis = sniffAfterFailureDelayMillis; return this; } @@ -210,7 +210,7 @@ public final class Sniffer extends RestClient.FailureListener implements Closeab * Creates the {@link Sniffer} based on the provided configuration. */ public Sniffer build() { - return new Sniffer(restClient, hostsSniffer, sniffInterval, sniffOnFailure, sniffAfterFailureDelay); + return new Sniffer(restClient, hostsSniffer, sniffIntervalMillis, sniffOnFailure, sniffAfterFailureDelayMillis); } } } diff --git a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferBuilderTests.java b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferBuilderTests.java index 8f355ecc47a..38baccf931e 100644 --- a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferBuilderTests.java +++ b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/HostsSnifferBuilderTests.java @@ -50,7 +50,7 @@ public class HostsSnifferBuilderTests extends LuceneTestCase { } try { - HostsSniffer.builder(client).setSniffRequestTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); + HostsSniffer.builder(client).setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); fail("should have failed"); } catch(IllegalArgumentException e) { assertEquals(e.getMessage(), "sniffRequestTimeout must be greater than 0"); @@ -61,7 +61,7 @@ public class HostsSnifferBuilderTests extends LuceneTestCase { builder.setScheme(RandomPicks.randomFrom(random(), HostsSniffer.Scheme.values())); } if (random().nextBoolean()) { - builder.setSniffRequestTimeout(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); + builder.setSniffRequestTimeoutMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); } assertNotNull(builder.build()); } diff --git a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java index 863830370b6..3fb152906b4 100644 --- a/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java +++ b/client-sniffer/src/test/java/org/elasticsearch/client/sniff/SnifferBuilderTests.java @@ -55,14 +55,14 @@ public class SnifferBuilderTests extends LuceneTestCase { } try { - Sniffer.builder(client, hostsSniffer).setSniffInterval(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); + Sniffer.builder(client, hostsSniffer).setSniffIntervalMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); fail("should have failed"); } catch(IllegalArgumentException e) { assertEquals("sniffInterval must be greater than 0", e.getMessage()); } try { - Sniffer.builder(client, hostsSniffer).setSniffAfterFailureDelay(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); + Sniffer.builder(client, hostsSniffer).setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); fail("should have failed"); } catch(IllegalArgumentException e) { assertEquals("sniffAfterFailureDelay must be greater than 0", e.getMessage()); @@ -74,10 +74,10 @@ public class SnifferBuilderTests extends LuceneTestCase { Sniffer.Builder builder = Sniffer.builder(client, hostsSniffer); if (random().nextBoolean()) { - builder.setSniffInterval(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); + builder.setSniffIntervalMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); } if (random().nextBoolean()) { - builder.setSniffAfterFailureDelay(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); + builder.setSniffAfterFailureDelayMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); } if (random().nextBoolean()) { builder.setSniffOnFailure(random().nextBoolean()); diff --git a/client/src/main/java/org/elasticsearch/client/RestClient.java b/client/src/main/java/org/elasticsearch/client/RestClient.java index f0e4d702f57..81354ae7a85 100644 --- a/client/src/main/java/org/elasticsearch/client/RestClient.java +++ b/client/src/main/java/org/elasticsearch/client/RestClient.java @@ -86,15 +86,15 @@ public final class RestClient implements Closeable { //we don't rely on default headers supported by HttpClient as those cannot be replaced, plus it would get hairy //when we create the HttpClient instance on our own as there would be two different ways to set the default headers. private final Header[] defaultHeaders; - private final long maxRetryTimeout; + private final long maxRetryTimeoutMillis; private final AtomicInteger lastHostIndex = new AtomicInteger(0); private volatile Set hosts; private final ConcurrentMap blacklist = new ConcurrentHashMap<>(); private volatile FailureListener failureListener = new FailureListener(); - private RestClient(CloseableHttpClient client, long maxRetryTimeout, Header[] defaultHeaders, HttpHost[] hosts) { + private RestClient(CloseableHttpClient client, long maxRetryTimeoutMillis, Header[] defaultHeaders, HttpHost[] hosts) { this.client = client; - this.maxRetryTimeout = maxRetryTimeout; + this.maxRetryTimeoutMillis = maxRetryTimeoutMillis; this.defaultHeaders = defaultHeaders; setHosts(hosts); } @@ -139,7 +139,7 @@ public final class RestClient implements Closeable { HttpRequestBase request = createHttpRequest(method, uri, entity); setHeaders(request, headers); //we apply a soft margin so that e.g. if a request took 59 seconds and timeout is set to 60 we don't do another attempt - long retryTimeout = Math.round(this.maxRetryTimeout / (float)100 * 98); + long retryTimeout = Math.round(this.maxRetryTimeoutMillis / (float)100 * 98); IOException lastSeenException = null; long startTime = System.nanoTime(); Iterator hostIterator = nextHost(); @@ -358,16 +358,16 @@ public final class RestClient implements Closeable { * Rest client builder. Helps creating a new {@link RestClient}. */ public static final class Builder { - public static final int DEFAULT_CONNECT_TIMEOUT = 1000; - public static final int DEFAULT_SOCKET_TIMEOUT = 10000; - public static final int DEFAULT_MAX_RETRY_TIMEOUT = DEFAULT_SOCKET_TIMEOUT; - public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT = 500; + public static final int DEFAULT_CONNECT_TIMEOUT_MILLIS = 1000; + public static final int DEFAULT_SOCKET_TIMEOUT_MILLIS = 10000; + public static final int DEFAULT_MAX_RETRY_TIMEOUT_MILLIS = DEFAULT_SOCKET_TIMEOUT_MILLIS; + public static final int DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS = 500; private static final Header[] EMPTY_HEADERS = new Header[0]; private final HttpHost[] hosts; private CloseableHttpClient httpClient; - private int maxRetryTimeout = DEFAULT_MAX_RETRY_TIMEOUT; + private int maxRetryTimeout = DEFAULT_MAX_RETRY_TIMEOUT_MILLIS; private Header[] defaultHeaders = EMPTY_HEADERS; /** @@ -392,16 +392,16 @@ public final class RestClient implements Closeable { } /** - * Sets the maximum timeout to honour in case of multiple retries of the same request. - * {@link #DEFAULT_MAX_RETRY_TIMEOUT} if not specified. + * Sets the maximum timeout (in milliseconds) to honour in case of multiple retries of the same request. + * {@link #DEFAULT_MAX_RETRY_TIMEOUT_MILLIS} if not specified. * - * @throws IllegalArgumentException if maxRetryTimeout is not greater than 0 + * @throws IllegalArgumentException if maxRetryTimeoutMillis is not greater than 0 */ - public Builder setMaxRetryTimeout(int maxRetryTimeout) { - if (maxRetryTimeout <= 0) { - throw new IllegalArgumentException("maxRetryTimeout must be greater than 0"); + public Builder setMaxRetryTimeoutMillis(int maxRetryTimeoutMillis) { + if (maxRetryTimeoutMillis <= 0) { + throw new IllegalArgumentException("maxRetryTimeoutMillis must be greater than 0"); } - this.maxRetryTimeout = maxRetryTimeout; + this.maxRetryTimeout = maxRetryTimeoutMillis; return this; } @@ -446,9 +446,9 @@ public final class RestClient implements Closeable { connectionManager.setMaxTotal(30); //default timeouts are all infinite - RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(DEFAULT_CONNECT_TIMEOUT) - .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT) - .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT).build(); + RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(DEFAULT_CONNECT_TIMEOUT_MILLIS) + .setSocketTimeout(DEFAULT_SOCKET_TIMEOUT_MILLIS) + .setConnectionRequestTimeout(DEFAULT_CONNECTION_REQUEST_TIMEOUT_MILLIS).build(); return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestConfig).build(); } } diff --git a/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java b/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java index 75587ed8db1..e4e024457a4 100644 --- a/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java +++ b/client/src/test/java/org/elasticsearch/client/RestClientBuilderTests.java @@ -53,7 +53,7 @@ public class RestClientBuilderTests extends LuceneTestCase { } try { - RestClient.builder(new HttpHost("localhost", 9200)).setMaxRetryTimeout(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); + RestClient.builder(new HttpHost("localhost", 9200)).setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(random(), Integer.MIN_VALUE, 0)); fail("should have failed"); } catch(IllegalArgumentException e) { assertEquals("maxRetryTimeout must be greater than 0", e.getMessage()); @@ -91,7 +91,7 @@ public class RestClientBuilderTests extends LuceneTestCase { builder.setDefaultHeaders(headers); } if (random().nextBoolean()) { - builder.setMaxRetryTimeout(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); + builder.setMaxRetryTimeoutMillis(RandomInts.randomIntBetween(random(), 1, Integer.MAX_VALUE)); } try (RestClient restClient = builder.build()) { assertNotNull(restClient);